1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/*
* Copyright 2023 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include <emscripten/html5.h>
// installing mock devicePixelRatio (independent of the browser/screen resolution)
static void installMockDevicePixelRatio() {
printf("installing mock devicePixelRatio...\n");
EM_ASM(
GLFW.mockDevicePixelRatio = 1.0;
GLFW.getDevicePixelRatio = () => { console.log("mock getDevicePixelRatio"); return GLFW.mockDevicePixelRatio; };
);
}
static void setDevicePixelRatio(float ratio) {
printf("setDevicePixelRatio %.0f\n", ratio);
EM_ASM({
GLFW.mockDevicePixelRatio = $0;
// mocking/simulating the fact that an event should be raised when devicePixelRatio changes
if (GLFW.devicePixelRatioMQL) {
GLFW.onDevicePixelRatioChange();
}
}, ratio);
}
static void setGLFWIsHiDPIAware(GLFWwindow *window, bool isHiDPIAware) {
printf("setGLFWIsHiDPIAware %s\n", isHiDPIAware ? "true" : "false");
glfwSetWindowAttrib(window, GLFW_SCALE_TO_MONITOR, isHiDPIAware ? GLFW_TRUE : GLFW_FALSE);
}
static void checkWindowSize(GLFWwindow *window, int expectedWidth, int expectedHeight, float ratio) {
// first check the window size
int w, h;
glfwGetWindowSize(window, &w, &h);
printf("windowSize => %d == %d && %d == %d\n", w, expectedWidth, h, expectedHeight);
assert(w == expectedWidth && h == expectedHeight);
// second check the frame buffer size
int fbw, fbh;
glfwGetFramebufferSize(window, &fbw, &fbh);
printf("framebufferSize => %d == %d && %d == %d\n", fbw, (int) (expectedWidth * ratio), fbh, (int) (expectedHeight * ratio));
assert(fbw == (int) (expectedWidth * ratio) && fbh == (int) (expectedHeight * ratio));
}
static bool getGLFWIsHiDPIAware() {
return EM_ASM_INT(return GLFW.isHiDPIAware() ? 1 : 0) != 0;
}
static void checkHiDPIAware(GLFWwindow *window, bool expectedAwareness) {
assert(getGLFWIsHiDPIAware() == expectedAwareness);
assert(glfwGetWindowAttrib(window, GLFW_SCALE_TO_MONITOR) == (expectedAwareness ? GLFW_TRUE : GLFW_FALSE));
}
int main() {
GLFWwindow* window;
assert(glfwInit() == GL_TRUE);
installMockDevicePixelRatio();
// by default, GLFW is NOT Hi DPI aware
assert(!getGLFWIsHiDPIAware());
// Use case 1: GLFW is NOT Hi DPI Aware | devicePixelRatio is 1.0
// Expected outcome is window size and frame buffer size are the same
{
printf("Use case #1\n");
window = glfwCreateWindow(640, 480, "test_glfw3_hi_dpi_aware.c | #1", NULL, NULL);
assert(window != NULL);
checkHiDPIAware(window, false);
checkWindowSize(window, 640, 480, 1.0);
glfwSetWindowSize(window, 600, 400);
checkWindowSize(window, 600, 400, 1.0);
glfwDestroyWindow(window);
}
// Use case 2: GLFW is NOT Hi DPI Aware | devicePixelRatio is 2.0
// Expected outcome is window size and frame buffer size are the same (because
// GLFW is not Hi DPI Aware)
{
printf("Use case #2\n");
setDevicePixelRatio(2.0);
window = glfwCreateWindow(640, 480, "test_glfw3_hi_dpi_aware.c | #2", NULL, NULL);
assert(window != NULL);
checkHiDPIAware(window, false);
checkWindowSize(window, 640, 480, 1.0);
glfwSetWindowSize(window, 600, 400);
checkWindowSize(window, 600, 400, 1.0);
glfwDestroyWindow(window);
}
// Use case 3: GLFW is Hi DPI Aware | devicePixelRatio is 1.0
// Expected outcome is window size and frame buffer size are the same
{
printf("Use case #3\n");
setDevicePixelRatio(1.0);
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
window = glfwCreateWindow(640, 480, "test_glfw3_hi_dpi_aware.c | #3", NULL, NULL);
assert(window != NULL);
checkHiDPIAware(window, true);
checkWindowSize(window, 640, 480, 1.0);
glfwSetWindowSize(window, 600, 400);
checkWindowSize(window, 600, 400, 1.0);
glfwDestroyWindow(window);
}
// Use case 4: GLFW is Hi DPI Aware | devicePixelRatio is 2.0
// Expected outcome is frame buffer size is 2x window size
{
printf("Use case #4\n");
setDevicePixelRatio(2.0);
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
window = glfwCreateWindow(640, 480, "test_glfw3_hi_dpi_aware.c | #4", NULL, NULL);
assert(window != NULL);
checkHiDPIAware(window, true);
checkWindowSize(window, 640, 480, 2.0);
glfwSetWindowSize(window, 600, 400);
checkWindowSize(window, 600, 400, 2.0);
glfwDestroyWindow(window);
}
// Use case 5: GLFW Hi DPI Awareness changes | devicePixelRatio 2.0
// Expected outcome is that the window sizes is adjusted automatically
{
printf("Use case #5\n");
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
window = glfwCreateWindow(640, 480, "test_glfw3_hi_dpi_aware.c | #5", NULL, NULL);
assert(window != NULL);
checkHiDPIAware(window, true);
checkWindowSize(window, 640, 480, 2.0);
setGLFWIsHiDPIAware(window, false);
checkWindowSize(window, 640, 480, 1.0);
checkHiDPIAware(window, false);
glfwDestroyWindow(window);
}
// Use case 6: GLFW is NOT Hi DPI Aware | devicePixelRatio changes
// Expected outcome is that the window sizes does not change
{
printf("Use case #6\n");
setDevicePixelRatio(1.0);
glfwDefaultWindowHints(); // reset GLFW_SCALE_TO_MONITOR
window = glfwCreateWindow(640, 480, "test_glfw3_hi_dpi_aware.c | #6", NULL, NULL);
assert(window != NULL);
checkHiDPIAware(window, false);
checkWindowSize(window, 640, 480, 1.0);
setDevicePixelRatio(2.0);
checkWindowSize(window, 640, 480, 1.0);
glfwDestroyWindow(window);
}
// Use case 7: GLFW is Hi DPI Aware | devicePixelRatio changes
// Expected outcome is that the window sizes is adjusted automatically
{
printf("Use case #7\n");
setDevicePixelRatio(2.0);
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
window = glfwCreateWindow(640, 480, "test_glfw3_hi_dpi_aware.c | #7", NULL, NULL);
assert(window != NULL);
checkHiDPIAware(window, true);
checkWindowSize(window, 640, 480, 2.0);
setDevicePixelRatio(1.0);
checkWindowSize(window, 640, 480, 1.0);
glfwDestroyWindow(window);
}
glfwTerminate();
return 0;
}
|