Skip to content

Commit

Permalink
Suppress BackendWindowHelper::GetDisplayFramebufferScale / forced by …
Browse files Browse the repository at this point in the history
…ImGui backends!
  • Loading branch information
pthom committed Apr 12, 2024
1 parent f10510c commit ac2bf15
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 82 deletions.
1 change: 0 additions & 1 deletion src/hello_imgui/doc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ Under windows, it will always be (1,1). Under macOS / linux, it will reflect the
It will typically be (2,2) on a macOS retina screen.
Notes:
- As a convenience, `ImGui::GetIO().DisplayFramebufferScale` is mirrored in `HelloImGui::DpiAwareParams::roDisplayFramebufferScale`.
- You cannot change DisplayFramebufferScale manually, it will be reset at each new frame, by asking the platform backend.
Expand Down
40 changes: 14 additions & 26 deletions src/hello_imgui/doc_params.md
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,9 @@ struct DpiAwareParams
{
// `dpiWindowSizeFactor`
// factor by which window size should be multiplied to get a similar
// visible size on different OSes. This affects the size of the window,
// but *not* the size of widgets and text.
// physical size on different OSes (as if they were all displayed on a 96 PPI screen).
// This affects the size of native app windows,
// but *not* imgui Windows, and *not* the size of widgets and text.
// In a standard environment (i.e. outside of Hello ImGui), an application with a size of 960x480 pixels,
// may have a physical size (in mm or inches) that varies depending on the screen DPI, and the OS.
//
Expand All @@ -903,36 +904,23 @@ struct DpiAwareParams

// `fontRenderingScale`
// factor (that is either 1 or < 1.) by which fonts glyphs should be scaled at rendering time.
// On macOS retina screens, it will be 0.5, since macOS APIs hide the real resolution of the screen.
// Changing this value will *not* change the visible font size on the screen, however it will
// affect the size of the loaded glyphs.
// For example, if fontRenderingScale=0.5 (which is the default on a macOS retina screen),
// a font size of 16 will be loaded as if it was 32, and will be rendered at half size.
// This leads to a better rendering quality on some platforms.
// On macOS retina screens, it will be 0.5, since macOS APIs hide the real resolution of the screen.
// Changing this value will *not* change the visible font size on the screen, however it will
// affect the size of the loaded glyphs.
// For example, if fontRenderingScale=0.5 (which is the default on a macOS retina screen),
// a font size of 16 will be loaded as if it was 32, and will be rendered at half size.
// This leads to a better rendering quality on some platforms.
// (This parameter will be used to set ImGui::GetIO().FontGlobalScale at startup)
float fontRenderingScale = 0.0f;

// `dpiFontLoadingFactor`
// factor by which font size should be multiplied at loading time to get a similar
// visible size on different OSes.
// The size will be equivalent to a size given for a 96 PPI screen
float DpiFontLoadingFactor() {
// factor by which font size should be multiplied at loading time to get a similar
// visible size on different OSes.
// The size will be equivalent to a size given for a 96 PPI screen
float DpiFontLoadingFactor() const {
float r = dpiWindowSizeFactor / fontRenderingScale;
return r;
};

// `roDisplayFramebufferScale`
// When rendering, the internal frame buffer size might be bigger than the
// size (in "virtual screen coordinate" pixels) of the window.
// For example:
// - on macOS retina screens, the frame buffer size will be twice the window size
// (and roDisplayFramebufferScale will be 2, 2)
// - on Windows, the frame buffer size will be the same as the window size.
// (since virtual screen coordinate pixels are the same as physical screen pixels on Windows)
//
// This is an output-only value: it will be set by the platform backend
// after it was initialized (any change you make to it at startup will not be used)
// It mirrors the value inside ImGui::GetIO().DisplayFramebufferScale.
ImVec2 roDisplayFramebufferScale = ImVec2(0.f, 0.f);
};

// ----------------------------------------------------------------------------
Expand Down
15 changes: 0 additions & 15 deletions src/hello_imgui/dpi_aware.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,6 @@ struct DpiAwareParams
float r = dpiWindowSizeFactor / fontRenderingScale;
return r;
};

// `roDisplayFramebufferScale`
// When rendering, the internal frame buffer size might be bigger than the
// size (in "virtual screen coordinate" pixels) of the window.
// For example:
// - on macOS retina screens, the frame buffer size will be twice the window size
// (and roDisplayFramebufferScale will be 2, 2)
// - on Windows, the frame buffer size will be the same as the window size.
// (since virtual screen coordinate pixels are the same as physical screen pixels on Windows)
//
// This is an output-only value: it will be set by the platform backend
// after it was initialized (any change you make to it at startup will not be used)
// It mirrors the value inside ImGui::GetIO().DisplayFramebufferScale.
ImVec2 roDisplayFramebufferScale = ImVec2(0.f, 0.f);
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -203,7 +189,6 @@ Under windows, it will always be (1,1). Under macOS / linux, it will reflect the
It will typically be (2,2) on a macOS retina screen.
Notes:
- As a convenience, `ImGui::GetIO().DisplayFramebufferScale` is mirrored in `HelloImGui::DpiAwareParams::roDisplayFramebufferScale`.
- You cannot change DisplayFramebufferScale manually, it will be reset at each new frame, by asking the platform backend.
Expand Down
26 changes: 6 additions & 20 deletions src/hello_imgui/internal/backend_impls/abstract_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,19 +451,18 @@ void _CheckDpiAwareParamsChanges(const std::string& msg)
printf("dpiAwareParams: fontRenderingScale=%f\n", dpiAwareParams.fontRenderingScale);
printf(" ImGui FontGlobalScale: %f\n", ImGui::GetIO().FontGlobalScale);
printf("dpiAwareParams: DpiFontLoadingFactor()=%f\n", dpiAwareParams.DpiFontLoadingFactor());
printf("dpiAwareParams: roDisplayFramebufferScale=%f, %f\n", dpiAwareParams.roDisplayFramebufferScale.x, dpiAwareParams.roDisplayFramebufferScale.y);
printf("dpiAwareParams: io.DisplayFramebufferScale=%f, %f\n", io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
auto fbs = ImGui::GetIO().DisplayFramebufferScale;
printf(" ImGui DisplayFramebufferScale: %f,%f\n", fbs.x, fbs.y);
printf("-------------------------------------------------------------\n");
}

if (dpiAwareParams.fontRenderingScale != io.FontGlobalScale)
{
printf("Warning: fontRenderingScale != ImGui::GetIO().FontGlobalScale\n");
}
if (dpiAwareParams.roDisplayFramebufferScale.x != io.DisplayFramebufferScale.x)
bool didFontGlobalScaleChange = dpiAwareParams.fontRenderingScale != io.FontGlobalScale;

if (didFontGlobalScaleChange)
{
printf("Warning: roDisplayFramebufferScale != ImGui::GetIO().DisplayFramebufferScale\n");
printf("Warning: didDpiAwareParamsChange=:true\n");
dpiAwareParams.fontRenderingScale = io.FontGlobalScale;
}
}

Expand Down Expand Up @@ -517,19 +516,6 @@ void AbstractRunner::SetupDpiAwareParams()
}
ImGui::GetIO().FontGlobalScale = params.dpiAwareParams.fontRenderingScale;

//
// DisplayFramebufferScale
//
ImVec2 displayFramebufferScale = mBackendWindowHelper->GetDisplayFramebufferScale(mWindow);
// Note: ImGui_ImplGlfw_NewFrame, ImGui_ImplSDL2_NewFrame, ... will also set ImGui::GetIO().DisplayFramebufferScale
// (using their own backend abstraction).
// There is a slight chance of discrepancy here, However, GetDisplayFramebufferScale() and DearImGui
// do get the same results for SDL and GLFW under Windows Linux macOS, Android, iOS.
auto& io = ImGui::GetIO();
io.DisplayFramebufferScale = displayFramebufferScale;
// dpiAwareParams.roDisplayFramebufferScale is an output-only value (for information only)
params.dpiAwareParams.roDisplayFramebufferScale = displayFramebufferScale;

_CheckDpiAwareParamsChanges("Setup End");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ namespace HelloImGui { namespace BackendApi

virtual void WaitForEventTimeout(double timeout_seconds) = 0;

// Return the ratio FrameBufferSize / WindowSize
virtual ImVec2 GetDisplayFramebufferScale(WindowPointer window) = 0;
// (ImGui backends handle this by themselves)
//virtual ImVec2 GetDisplayFramebufferScale(WindowPointer window) = 0;

// Return the ratio by which the window size should be scaled to account for HighDPI
// (i.e. the same size given at creation create the same physical size in mm on the screen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,10 @@ namespace HelloImGui { namespace BackendApi
glfwWaitEventsTimeout(timeout_seconds);
}

ImVec2 GlfwWindowHelper::GetDisplayFramebufferScale(HelloImGui::BackendApi::WindowPointer window)
ImVec2 _GetWindowContentScale(HelloImGui::BackendApi::WindowPointer window)
{
float x_scale, y_scale;
glfwGetWindowContentScale((GLFWwindow *) window, &x_scale, &y_scale);
printf("GlfwWindowHelper::GetDisplayFramebufferScale: %f, %f\n", x_scale, y_scale);
return ImVec2(x_scale, y_scale);
}

Expand All @@ -202,7 +201,7 @@ namespace HelloImGui { namespace BackendApi
#ifdef __APPLE__
return 1.f;
#else
ImVec2 scale = GetDisplayFramebufferScale(window);
ImVec2 scale = _GetWindowContentScale(window);
return scale.x;
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace HelloImGui { namespace BackendApi

void WaitForEventTimeout(double timeout_seconds) override;

ImVec2 GetDisplayFramebufferScale(WindowPointer window) override;
float GetWindowSizeDpiScaleFactor(WindowPointer window) override;

void HideWindow(WindowPointer window) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ namespace HelloImGui { namespace BackendApi
std::this_thread::sleep_for(std::chrono::milliseconds((int)(timeout_seconds * 1000)));
}

ImVec2 GetDisplayFramebufferScale(WindowPointer window) override { return NullConfig::GetDisplayFramebufferScale(); }
float GetWindowSizeDpiScaleFactor(WindowPointer window) override { return NullConfig::GetWindowSizeDpiScaleFactor(); }

void HideWindow(WindowPointer window) override {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,6 @@ namespace HelloImGui { namespace BackendApi
SDL_WaitEventTimeout(NULL, timeout_ms);
}

ImVec2 SdlWindowHelper::GetDisplayFramebufferScale(WindowPointer window)
{
int win_w, win_h, fb_w, fb_h;
SDL_GetWindowSize((SDL_Window *) window, &win_w, &win_h);
SDL_GL_GetDrawableSize((SDL_Window *) window, &fb_w, &fb_h);
return ImVec2((float)fb_w / win_w, (float)fb_h / win_h);
}


float SdlWindowHelper::GetWindowSizeDpiScaleFactor(WindowPointer window)
{
#if TARGET_OS_MAC // is true for any software platform that's derived from macOS, which includes iOS, watchOS, and tvOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace HelloImGui { namespace BackendApi

void WaitForEventTimeout(double timeout_seconds) override;

ImVec2 GetDisplayFramebufferScale(WindowPointer window) override;
float GetWindowSizeDpiScaleFactor(WindowPointer window) override;

void HideWindow(WindowPointer window) override;
Expand Down
3 changes: 0 additions & 3 deletions src/hello_imgui/internal/backend_impls/null_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ namespace NullConfig
return {GetScreenBounds()};
}

ImVec2 GetDisplayFramebufferScale() {
return {1.f, 1.f};
}
float GetWindowSizeDpiScaleFactor() { return 1.f; }

}
Expand Down

0 comments on commit ac2bf15

Please sign in to comment.