Browse Source

Platform: deduplicate DPI handling code.

And compilation of it. It should be enough to have just an OBJECT
library added to each, instead of including a header which includes the
world.
pull/331/head
Vladimír Vondruš 7 years ago
parent
commit
d98efb241d
  1. 125
      src/Magnum/Platform/CMakeLists.txt
  2. 3
      src/Magnum/Platform/GlfwApplication.cpp
  3. 24
      src/Magnum/Platform/Implementation/DpiScaling.cpp
  4. 53
      src/Magnum/Platform/Implementation/DpiScaling.h
  5. 6
      src/Magnum/Platform/Implementation/DpiScaling.mm
  6. 4
      src/Magnum/Platform/Sdl2Application.cpp

125
src/Magnum/Platform/CMakeLists.txt

@ -23,7 +23,9 @@
# DEALINGS IN THE SOFTWARE.
#
# Headers
# Code shared by more application/context implementations
set(MagnumPlatform_SRCS )
set(MagnumPlatform_HEADERS
GLContext.h
Platform.h
@ -31,10 +33,63 @@ set(MagnumPlatform_HEADERS
ScreenedApplication.h
ScreenedApplication.hpp)
set(MagnumPlatform_PRIVATE_HEADERS )
if(BUILD_DEPRECATED AND TARGET_GL)
list(APPEND MagnumPlatform_HEADERS Context.h)
endif()
# DPI scaling queries only for Sdl2Application and GlfwApplication at the
# moment, build the files only then
if(WITH_GLFWAPPLICATION OR WITH_SDL2APPLICATION)
# List of libraries to link when using the MagnumPlatformObjects target
# TODO: use target_link_libraries() when we are on a CMake version that
# supports it (3.12?)
set(MagnumPlatform_LINK_LIBRARIES )
set(MagnumPlatform_COMPILE_DEFINITIONS )
list(APPEND MagnumPlatform_PRIVATE_HEADERS Implementation/DpiScaling.h)
if(CORRADE_TARGET_APPLE)
# We can't build both DpiScaling.cpp and DpiScaling.mm as they both
# result in DpiScaling.o and Xcode/CMake gets confused, so including
# the cpp from the mm instead
list(APPEND MagnumPlatform_SRCS Implementation/DpiScaling.mm)
else()
list(APPEND MagnumPlatform_SRCS Implementation/DpiScaling.cpp)
endif()
add_library(MagnumPlatformObjects OBJECT
${MagnumPlatform_SRCS}
${MagnumPlatform_HEADERS}
${MagnumPlatform_PRIVATE_HEADERS})
target_include_directories(MagnumPlatformObjects PUBLIC $<TARGET_PROPERTY:MagnumGL,INTERFACE_INCLUDE_DIRECTORIES>)
if(BUILD_STATIC_PIC)
set_target_properties(MagnumPlatformObjects PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()
set_target_properties(MagnumPlatformObjects PROPERTIES FOLDER "Magnum/Platform")
# Use the Foundation framework on Apple to query the DPI awareness
if(CORRADE_TARGET_APPLE)
find_library(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY Foundation)
find_path(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR NAMES NSBundle.h)
mark_as_advanced(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY
_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR)
list(APPEND MagnumPlatform_LINK_LIBRARIES ${_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY})
target_include_directories(MagnumPlatformObjects PRIVATE ${_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR})
# If there is X11, ask it for DPI
elseif(CORRADE_TARGET_UNIX)
find_package(X11)
if(X11_FOUND)
# Not linking to X11, we dlopen() instead
target_include_directories(MagnumPlatformObjects PRIVATE ${X11_X11_INCLUDE_PATH})
target_compile_definitions(MagnumPlatformObjects PUBLIC "_MAGNUM_PLATFORM_USE_X11")
list(APPEND MagnumPlatform_LINK_LIBRARIES ${CMAKE_DL_LIBS})
list(APPEND MagnumPlatform_COMPILE_DEFINITIONS "_MAGNUM_PLATFORM_USE_X11")
endif()
endif()
endif()
# Files to display in project view of IDEs only (filled in below)
set(MagnumPlatform_FILES )
@ -124,14 +179,13 @@ if(WITH_GLFWAPPLICATION)
message(FATAL_ERROR "GLFW library, required by GlfwApplication, was not found. Set WITH_GLFWAPPLICATION to OFF to skip building it.")
endif()
set(MagnumGlfwApplication_SRCS GlfwApplication.cpp)
set(MagnumGlfwApplication_SRCS
$<TARGET_OBJECTS:MagnumPlatformObjects>
GlfwApplication.cpp)
set(MagnumGlfwApplication_HEADERS GlfwApplication.h)
if(TARGET_GL)
list(APPEND MagnumGlfwApplication_SRCS ${MagnumSomeContext_OBJECTS})
endif()
if(CORRADE_TARGET_APPLE)
list(APPEND MagnumGlfwApplication_SRCS Implementation/dpiScaling.mm)
endif()
add_library(MagnumGlfwApplication STATIC
${MagnumGlfwApplication_SRCS}
@ -141,7 +195,12 @@ if(WITH_GLFWAPPLICATION)
FOLDER "Magnum/Platform")
# Assuming that PIC is not needed because the Application lib is always
# linked to the executable and not to any intermediate shared lib
target_link_libraries(MagnumGlfwApplication PUBLIC Magnum GLFW::GLFW)
# TODO: use MagnumPlatformObjects instead of ${MagnumPlatform_*} when
# CMake supports it
target_link_libraries(MagnumGlfwApplication PUBLIC Magnum GLFW::GLFW
${MagnumPlatform_LINK_LIBRARIES})
target_compile_definitions(MagnumGlfwApplication PRIVATE
${MagnumPlatform_COMPILE_DEFINITIONS})
if(TARGET_GL)
target_link_libraries(MagnumGlfwApplication PUBLIC
MagnumGL
@ -149,26 +208,6 @@ if(WITH_GLFWAPPLICATION)
${MagnumSomeContext_LIBRARY})
endif()
# Use the Foundation framework on Apple to query the DPI awareness
if(CORRADE_TARGET_APPLE)
find_library(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY Foundation)
mark_as_advanced(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY)
find_path(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR NAMES NSBundle.h)
mark_as_advanced(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR)
target_link_libraries(MagnumGlfwApplication PUBLIC ${_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY})
target_include_directories(MagnumGlfwApplication PRIVATE ${_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR})
# If there is X11, ask it for DPI
elseif(CORRADE_TARGET_UNIX)
find_package(X11)
if(X11_FOUND)
# Not linking to X11, we dlopen() instead
target_include_directories(MagnumGlfwApplication PRIVATE ${X11_X11_INCLUDE_PATH})
target_link_libraries(MagnumGlfwApplication PUBLIC ${CMAKE_DL_LIBS})
target_compile_definitions(MagnumGlfwApplication PRIVATE "_MAGNUM_PLATFORM_USE_X11")
endif()
endif()
# Link also EGL library, if on ES (and not on WebGL)
if(MAGNUM_TARGET_GLES AND NOT MAGNUM_TARGET_DESKTOP_GLES AND NOT MAGNUM_TARGET_WEBGL)
find_package(EGL REQUIRED)
@ -233,14 +272,13 @@ if(WITH_SDL2APPLICATION)
message(FATAL_ERROR "SDL2 library, required by Sdl2Application, was not found. Set WITH_SDL2APPLICATION to OFF to skip building it.")
endif()
set(MagnumSdl2Application_SRCS Sdl2Application.cpp)
set(MagnumSdl2Application_SRCS
$<TARGET_OBJECTS:MagnumPlatformObjects>
Sdl2Application.cpp)
set(MagnumSdl2Application_HEADERS Sdl2Application.h)
if(TARGET_GL)
list(APPEND MagnumSdl2Application_SRCS ${MagnumSomeContext_OBJECTS})
endif()
if(CORRADE_TARGET_APPLE)
list(APPEND MagnumSdl2Application_SRCS Implementation/dpiScaling.mm)
endif()
add_library(MagnumSdl2Application STATIC
${MagnumSdl2Application_SRCS}
@ -250,7 +288,12 @@ if(WITH_SDL2APPLICATION)
FOLDER "Magnum/Platform")
# Assuming that PIC is not needed because the Application lib is always
# linked to the executable and not to any intermediate shared lib
target_link_libraries(MagnumSdl2Application PUBLIC Magnum SDL2::SDL2)
# TODO: use MagnumPlatformObjects instead of ${MagnumPlatform_*} when
# CMake supports it
target_link_libraries(MagnumSdl2Application PUBLIC Magnum SDL2::SDL2
${MagnumPlatform_LINK_LIBRARIES})
target_compile_definitions(MagnumSdl2Application PRIVATE
${MagnumPlatform_COMPILE_DEFINITIONS})
if(TARGET_GL)
target_link_libraries(MagnumSdl2Application PUBLIC
MagnumGL
@ -258,26 +301,6 @@ if(WITH_SDL2APPLICATION)
${MagnumSomeContext_LIBRARY})
endif()
# Use the Foundation framework on Apple to query the DPI awareness
if(CORRADE_TARGET_APPLE)
find_library(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY Foundation)
mark_as_advanced(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY)
find_path(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR NAMES NSBundle.h)
mark_as_advanced(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR)
target_link_libraries(MagnumSdl2Application PUBLIC ${_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY})
target_include_directories(MagnumSdl2Application PRIVATE ${_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR})
# If there is X11, ask it for DPI
elseif(CORRADE_TARGET_UNIX)
find_package(X11)
if(X11_FOUND)
# Not linking to X11, we dlopen() instead
target_include_directories(MagnumSdl2Application PRIVATE ${X11_X11_INCLUDE_PATH})
target_link_libraries(MagnumSdl2Application PUBLIC ${CMAKE_DL_LIBS})
target_compile_definitions(MagnumSdl2Application PRIVATE "_MAGNUM_PLATFORM_USE_X11")
endif()
endif()
# Link also EGL library, if on ES (and not on WebGL)
if(MAGNUM_TARGET_GLES AND NOT MAGNUM_TARGET_DESKTOP_GLES AND NOT MAGNUM_TARGET_WEBGL)
find_package(EGL REQUIRED)

3
src/Magnum/Platform/GlfwApplication.cpp

@ -28,11 +28,12 @@
#include <cstring>
#include <tuple>
#include <Corrade/Utility/Arguments.h>
#include <Corrade/Utility/String.h>
#include <Corrade/Utility/Unicode.h>
#include "Magnum/Platform/ScreenedApplication.hpp"
#include "Magnum/Platform/Implementation/dpiScaling.hpp"
#include "Magnum/Platform/Implementation/DpiScaling.h"
#ifdef MAGNUM_TARGET_GL
#include "Magnum/GL/Version.h"

24
src/Magnum/Platform/Implementation/dpiScaling.hpp → src/Magnum/Platform/Implementation/DpiScaling.cpp

@ -25,26 +25,29 @@
DEALINGS IN THE SOFTWARE.
*/
#include "DpiScaling.h"
#include <Corrade/Utility/Arguments.h>
#include "Magnum/Magnum.h"
#ifdef _MAGNUM_PLATFORM_USE_X11
#include <cstring>
#include <dlfcn.h>
#include <X11/Xresource.h>
#include <Corrade/Containers/ScopeGuard.h>
#include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/Debug.h>
#undef None
#endif
#ifdef CORRADE_TARGET_EMSCRIPTEN
#include <emscripten/html5.h>
#include <emscripten/emscripten.h>
#endif
namespace Magnum { namespace Platform { namespace Implementation {
namespace {
inline Utility::Arguments windowScalingArguments() {
Utility::Arguments windowScalingArguments() {
Utility::Arguments args{"magnum"};
args.addOption("dpi-scaling", "default")
.setFromEnvironment("dpi-scaling")
@ -60,10 +63,7 @@ inline Utility::Arguments windowScalingArguments() {
}
#ifdef _MAGNUM_PLATFORM_USE_X11
/* Returns DPI scaling for current X11 instance. Because X11 (as opposed to
Wayland) doesn't have per-monitor scaling, it's fetched from the default
display. */
inline Float x11DpiScaling() {
Float x11DpiScaling() {
/* If the end app links to X11, these symbols will be available in a global
scope and we can use that to query the DPI. If not, then those symbols
won't be and that's okay -- it may be using Wayland or something else. */
@ -124,17 +124,11 @@ inline Float x11DpiScaling() {
#endif
#ifdef CORRADE_TARGET_EMSCRIPTEN
inline Float emscriptenDpiScaling() {
Float emscriptenDpiScaling() {
return Float(emscripten_get_device_pixel_ratio());
}
#endif
}
#ifdef CORRADE_TARGET_APPLE
bool isAppleBundleHiDpiEnabled();
#endif
}}}
#endif

53
src/Magnum/Platform/Implementation/DpiScaling.h

@ -0,0 +1,53 @@
#ifndef Magnum_Platform_Implementation_DpiScaling_h
#define Magnum_Platform_Implementation_DpiScaling_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <Corrade/Utility/Utility.h>
#include "Magnum/Magnum.h"
namespace Magnum { namespace Platform { namespace Implementation {
Utility::Arguments windowScalingArguments();
#ifdef _MAGNUM_PLATFORM_USE_X11
/* Returns DPI scaling for current X11 instance. Because X11 (as opposed to
Wayland) doesn't have per-monitor scaling, it's fetched from the default
display. */
Float x11DpiScaling();
#endif
#ifdef CORRADE_TARGET_EMSCRIPTEN
Float emscriptenDpiScaling();
#endif
#ifdef CORRADE_TARGET_APPLE
bool isAppleBundleHiDpiEnabled();
#endif
}}}
#endif

6
src/Magnum/Platform/Implementation/dpiScaling.mm → src/Magnum/Platform/Implementation/DpiScaling.mm

@ -25,7 +25,11 @@
#import "Foundation/NSBundle.h"
#include "dpiScaling.hpp"
#include "DpiScaling.h"
/* We can't build both DpiScaling.cpp and DpiScaling.mm as they both result in
DpiScaling.o and Xcode/CMake gets confused, so including it here instead */
#include "DpiScaling.cpp"
namespace Magnum { namespace Platform { namespace Implementation {

4
src/Magnum/Platform/Sdl2Application.cpp

@ -31,11 +31,13 @@
#include <tuple>
#else
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#endif
#include <Corrade/Utility/Arguments.h>
#include "Magnum/Math/Range.h"
#include "Magnum/Platform/ScreenedApplication.hpp"
#include "Magnum/Platform/Implementation/dpiScaling.hpp"
#include "Magnum/Platform/Implementation/DpiScaling.h"
#ifdef MAGNUM_TARGET_GL
#include "Magnum/GL/Version.h"

Loading…
Cancel
Save