| 1 | # TODO: Unsuccessfully coping with FMX scaling on Windows for CASTLE_WINDOWS_FORM |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | FMX on Windows does scaling, just like FMX on Linux. However, it seems there are significant differences in where is it applied between Windows and Linux, and these affect our code. |
| 6 | |
| 7 | This file documents how we tried, and failed, to cope with it in `CASTLE_WINDOWS_FORM` backend, for Windows. And what it means. |
| 8 | |
| 9 | Important note: |
| 10 | |
| 11 | **The problem outlined in this document doesn't affect any real application using CGE**. That's because we don't use `CASTLE_WINDOWS_FORM` on Windows by default. The only reason to test `CASTLE_WINDOWS_FORM` on Windows is for experimenting (and comparing with Linux, that relies on `CASTLE_WINDOWS_FORM`). For real applications: |
| 12 | |
| 13 | - TCastleWindow applications on Windows use `CASTLE_WINDOW_WINAPI` by default, not `CASTLE_WINDOWS_FORM`. And `CASTLE_WINDOW_WINAPI` works perfectly, and doesn't use FMX at all. |
| 14 | |
| 15 | - TCastleControl (with both FMX and VCL) has no problem. FMX scaling in case of Windows with TCastleControl is handled perfectly (and we actually see non-1.0 scaling values, despite being unable to see them from `CASTLE_WINDOWS_FORM`). |
| 16 | |
| 17 | ## What does FMX scaling on Windows do |
| 18 | |
| 19 | The scaling happens when you activate scaling in Windows 10 settings. |
| 20 | |
| 21 | 1. Requesting Form size like 200x200 doesn't actually give you OpenGLControl with pixel size exact 200x200 (and it's not because of window frame -- see multi_window form size differences). |
| 22 | |
| 23 | Actually it seems PixelsWidth/PixelsHeight of TOpenGLControl with FMX on Windows are incorrect, they are scaled, but they should not be. |
| 24 | |
| 25 | TODO: In the ideal world, our `FormSizeRequestPrecisely` should do `Result := Round(Result / FmxScreenScale);` to account for it. But it fails, see below. |
| 26 | |
| 27 | 2. FMX `Screen.Width` and `Screen.Height` are scaled too. |
| 28 | |
| 29 | TODO: In the ideal world, our `Application.ScreenWidth` and `Application.ScreenHeight` should use `FmxScreenScale` too. |
| 30 | |
| 31 | Note: FMX on Linux doesn't scale `Screen.Width` and `Screen.Height`, it seems. So possibly |
| 32 | this would have to be done only on Windows (or non-Linux). |
| 33 | |
| 34 | ## What happens with screen size |
| 35 | |
| 36 | It seems that |
| 37 | |
| 38 | - on Windows, FMX actually scales the Screen.Width by Windows scaling (same as used by FGLUtiliy.Scale). |
| 39 | |
| 40 | - on Linux, FMX reports unscaled Screen.Width. It is real screen size in pixels, with cut off margins for GNOME 3 top bar / sidebar. |
| 41 | |
| 42 | Current code adjusts better to FMX on Linux behavior. |
| 43 | So |
| 44 | |
| 45 | - We assume that Screen.Width is in real device pixels. |
| 46 | |
| 47 | - Our FormSizeRequestPrecisely on FMXLinux also accounts to make sure TCastleWindow.Width sets the requested size in real device pixels, so e.g. setting size like "Window.Width := Screen.Width * 0.75" makes sense. |
| 48 | |
| 49 | TODO: On FMX on Windows, "Window.Width := Screen.Width * 0.75" also works, but because 2 bugs "cancel each other out": `Screen.Width` is scaled, but also our `FormSizeRequestPrecisely` lets to set scaled value. So "Window.Width := Screen.Width * 0.75" works as you expect, but e.g. "Window.Width := 200" (testcase: multi_window) is actually wrong, the resulting window will not have requested size. |
| 50 | |
| 51 | ## Failed attempts |
| 52 | |
| 53 | The failure comes to the fact we cannot ever get the internal scaling used by FMX on Windows. We tried: |
| 54 | |
| 55 | ```delphi |
| 56 | {$ifdef MSWINDOWS} Windows, FMX.Platform.Win, FMX.Helpers.Win, {$endif} |
| 57 | ``` |
| 58 | |
| 59 | 1. `IFMXScreenService.GetScreenScale`. |
| 60 | |
| 61 | ```delphi |
| 62 | function FmxScreenScale: Single; |
| 63 | var |
| 64 | Service: IFMXScreenService; |
| 65 | begin |
| 66 | if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService) then |
| 67 | begin |
| 68 | Service := IFMXScreenService(TPlatformServices.Current.GetPlatformService(IFMXScreenService)); |
| 69 | Result := Service.GetScreenScale; |
| 70 | WritelnLog('FMX IFMXScreenService.GetScreenScale %f', [Result]); |
| 71 | end else |
| 72 | Result := 1; |
| 73 | end; |
| 74 | ``` |
| 75 | |
| 76 | And in FormSizeRequestPrecisely: |
| 77 | |
| 78 | ```delphi |
| 79 | Result := Round(Result / FmxScreenScale); |
| 80 | ``` |
| 81 | |
| 82 | But: IFMXScreenService is avaiable OK, but GetScreenScale is always 1.0. |
| 83 | |
| 84 | 2. Get scale using `GetWndScale(TWinWindowHandle(Form.Handle).Wnd)`. |
| 85 | |
| 86 | But: this is always 1.0. |
| 87 | |
| 88 | 3. `GetScaleFromDC`, like FMX code does: |
| 89 | |
| 90 | ```delphi |
| 91 | function GetScaleFromDC: Single; |
| 92 | var |
| 93 | DC: HDC; |
| 94 | begin |
| 95 | DC := GetDC(0); |
| 96 | try |
| 97 | Result := GetDCScale(DC); |
| 98 | finally |
| 99 | ReleaseDC(0, DC); |
| 100 | end; |
| 101 | end; |
| 102 | ``` |
| 103 | But: this is always 1.0 again... |
| 104 | |
| 105 | 4. Crazy experiments: `TWinWindowHandle(Form.Handle).SetForcedScaleForForm(1.0);` |
| 106 | |
| 107 | has no effect |
| 108 | |
| 109 | Remaining ideas: |
| 110 | |
| 111 | - Test `TDisplay.Scale`. |
| 112 | |
| 113 | Note that some `TDisplay` docs seem outdated, and not mention this field (maybe was added around Delphi 11?) http://docwiki.embarcadero.com/RADStudio/Sydney/en/Multiple_Display_Support . |
| 114 | |
| 115 | Test TWinMultiDisplay code, PrimaryDisplay.Scale. |
| 116 | |
| 117 | ## What would be solved? |
| 118 | |
| 119 | - Maybe we could unify Windows and Linux versions handling of FMX scaling, in |
| 120 | |
| 121 | - `FormSizeRequestPrecisely` |
| 122 | - `Application.ScreenWidth` and `Application.ScreenHeight` |
| 123 | |
| 124 | Right now, instead, `FormSizeRequestPrecisely` has Linux-specific code, and `Application.ScreenWidth` and `Application.ScreenHeight` do nothing (as nothing needs to be done on Linux). |
| 125 | |
| 126 | - Using `CASTLE_WINDOWS_FORM` on Windows would work better: |
| 127 | |
| 128 | - `multi_window` requests windows with size 200x200, but they are not actually 200x200. |
| 129 | |
| 130 | - Maximizing any window shows that FMX TOpenGLControl.PixelsWidth/PixelsHeight are scaled, but they should not be. We do not know the actual window size in pixels. |
| 131 | |
| 132 | - `TTestCastleWindow.TestViewportPositionTo` would not fail. It assumes we really can create window with requested size. |