Debian Codegraph @

src/delphi/fmx.castleinternalglcontrol.pas

raw
1 {
2 Copyright 2022-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Control with OpenGL context (and nothing else, like CGE container)
17 on a Delphi FMX form.
18 This is like TCastleControl, but stripped from all CGE container features.
19 It is similar in concept to TOpenGLControl from LCL,
20 though implementation is independent -- we use special tricks
21 to get OpenGL context on FMX form.
22
23 This is only internal for TCastleWindow in CASTLE_WINDOW_FORM case.
24 So we don't register it, don't care about publishing stuff,
25 don't care how it looks in Delphi IDE etc. }
26 unit Fmx.CastleInternalGLControl;
27
28 {$I castleconf.inc}
29
30 interface
31
32 uses // standard units
33 {$ifdef MSWINDOWS} Windows, {$endif}
34 SysUtils, Classes,
35 // fmx
36 {$ifdef MSWINDOWS} FMX.Presentation.Win, {$endif}
37 FMX.Controls, FMX.Controls.Presentation, FMX.Types, UITypes,
38 // cge
39 CastleInternalContextBase, CastleInternalFmxUtils;
40
41 type
42 { Control with OpenGL context (and nothing else, like CGE container)
43 on a Delphi FMX form.
44 This is like TCastleControl, but stripped from all CGE container features. }
45 TOpenGLControl = class(TPresentedControl)
46 private
47 FRequirements: TGLContextRequirements;
48 { Internal platform-specific context data and initialization/finalization.
49 This is in contrast to TCastleContainer.Context, that is public
50 and manages context properties that are cross-platform and available
51 for all OpenGL(ES). }
52 FPlatformContext: TGLContext;
53 FGLInitialized: Boolean;
54 FGLUtility: TFmxOpenGLUtility;
55 procedure CreateHandle;
56 procedure DestroyHandle;
57 procedure InitializeContext;
58 procedure FinalizeContext;
59 protected
60 function DefinePresentationName: String; override;
61 procedure DoRootChanging(const NewRoot: IRoot); override;
62 procedure SetVisible(const AValue: Boolean); override;
63 public
64 OnPaint: TNotifyEvent;
65
66 constructor Create(AOwner: TComponent); override;
67 destructor Destroy; override;
68 procedure Paint; override;
69
70 procedure Invalidate; virtual;
71 procedure SwapBuffers;
72 procedure MakeCurrent;
73
74 { Context required parameters. }
75 property Requirements: TGLContextRequirements read FRequirements;
76
77 { If Handle not allocated yet, allocate it now.
78 This makes sure we have OpenGL context created.
79 Our OpenBackend must guarantee it, we want to initialize GLVersion
80 afterwards etc. }
81 procedure HandleNeeded;
82
83 { Call this continuosly. }
84 procedure Update;
85
86 { This control must always have "native style", which means
87 it has ControlType = Platform. See FMX docs about native controls:
88 https://docwiki.embarcadero.com/RADStudio/Sydney/en/FireMonkey_Native_Windows_Controls
89 Native controls are always on top of non-native controls. }
90 property ControlType default TControlType.Platform;
91
92 { Size in pixels, not scaled by anything.
93 Such size can be passed e.g. to OpenGL viewport. }
94 function PixelsWidth: Integer;
95 function PixelsHeight: Integer;
96
97 { Scaling of FMX reported mouse coordinates to pixels. }
98 function MousePosScale: Single;
99
100 { Pixels per inch, reported by system, affected by user OS-wide preferences
101 like font scaling. }
102 function PixelsPerInch: Single;
103 end;
104
105 implementation
106
107 uses FMX.Presentation.Factory, Types, FMX.Graphics,
108 CastleLog, CastleUtils, CastleInternalDelphiUtils;
109
110 {$ifdef MSWINDOWS}
111
112 { TWinNativeOpenGLControl -------------------------------------------------------- }
113
114 type
115 { Presentation for TOpenGLControl.
116 This class is necessary to manage WinAPI HWND associated with FMX control. }
117 TWinNativeOpenGLControl = class(TWinPresentation)
118 protected
119 procedure CreateHandle; override;
120 procedure DestroyHandle; override;
121 public
122 function OpenGLControl: TOpenGLControl;
123 end;
124
125 function TWinNativeOpenGLControl.OpenGLControl: TOpenGLControl;
126 begin
127 Result := Control as TOpenGLControl;
128 end;
129
130 procedure TWinNativeOpenGLControl.CreateHandle;
131 begin
132 inherited;
133 { Looking at TWinNativeMemo.CreateHandle confirms this can be called with Handle = null }
134 if Handle <> NullHWnd then
135 OpenGLControl.CreateHandle;
136 end;
137
138 procedure TWinNativeOpenGLControl.DestroyHandle;
139 begin
140 if Handle <> NullHWnd then
141 OpenGLControl.DestroyHandle;
142 inherited;
143 end;
144
145 {$endif}
146
147 { TOpenGLControl ------------------------------------------------------------ }
148
149 procedure TOpenGLControl.InitializeContext;
150 begin
151 if not FGLInitialized then
152 begin
153 FGLInitialized := true;
154 FGLUtility.ContextAdjustEarly(FPlatformContext);
155 FPlatformContext.Initialize(FRequirements);
156 // Invalidate; // would be too early, CASTLE_WINDOW_FORM will do it later
157 end;
158 end;
159
160 procedure TOpenGLControl.FinalizeContext;
161 begin
162 if FGLInitialized then
163 begin
164 FGLInitialized := false;
165 FPlatformContext.Finalize;
166 end;
167 inherited;
168 end;
169
170 constructor TOpenGLControl.Create(AOwner: TComponent);
171 begin
172 inherited;
173
174 FRequirements := TGLContextRequirements.Create(Self);
175 FRequirements.Name := 'Requirements';
176 FRequirements.SetSubComponent(true);
177
178 FPlatformContext := ContextCreateBestInstance;
179
180 FGLUtility := TFmxOpenGLUtility.Create;
181 FGLUtility.Control := Self;
182 FGLUtility.OnHandleAfterCreateEvent := CreateHandle;
183 FGLUtility.OnHandleBeforeDestroyEvent := DestroyHandle;
184
185 TabStop := true;
186 CanFocus := True;
187 Assert(not (csDesigning in ComponentState)); // this is not ready for design-time
188 ControlType := TControlType.Platform;
189 end;
190
191 destructor TOpenGLControl.Destroy;
192 begin
193 FreeAndNil(FPlatformContext);
194 FreeAndNil(FGLUtility);
195 inherited;
196 end;
197
198 procedure TOpenGLControl.CreateHandle;
199 begin
200 InitializeContext;
201 end;
202
203 procedure TOpenGLControl.DestroyHandle;
204 begin
205 FinalizeContext;
206 end;
207
208 procedure TOpenGLControl.Paint;
209 begin
210 { We must have OpenGL context at this point,
211 and on Delphi/Linux there is no way to register "on native handle creation",
212 we must manually perform native handle creation (with GL context)
213 using our FGLUtility.
214
215 Maybe in the future it will make sense to do this also from some other events,
216 like update or mouse/key press?
217 Doesn't seem necessary in practice for now.
218
219 The TOpenGLControl is only used from CASTLE_WINDOW_FORM in practice,
220 which always calls HandleNeeded manually after creation anyway. }
221 HandleNeeded;
222
223 if Assigned(OnPaint) then
224 OnPaint(Self);
225
226 // inherited not needed, and possibly causes something unnecessary
227 end;
228
229 procedure TOpenGLControl.HandleNeeded;
230 begin
231 FGLUtility.HandleNeeded;
232 end;
233
234 function TOpenGLControl.DefinePresentationName: String;
235 begin
236 Result := 'OpenGLControl-' + GetPresentationSuffix;
237 end;
238
239 procedure TOpenGLControl.Invalidate;
240 begin
241 InvalidateRect(TRectF.Create(0, 0, Width, Height));
242 end;
243
244 procedure TOpenGLControl.SwapBuffers;
245 begin
246 FPlatformContext.SwapBuffers;
247 end;
248
249 procedure TOpenGLControl.MakeCurrent;
250 begin
251 FPlatformContext.MakeCurrent;
252 end;
253
254 procedure TOpenGLControl.Update;
255 begin
256 FGLUtility.Update;
257 end;
258
259 function TOpenGLControl.PixelsWidth: Integer;
260 begin
261 Result := Round(Width * FGLUtility.Scale);
262 end;
263
264 function TOpenGLControl.PixelsHeight: Integer;
265 begin
266 Result := Round(Height * FGLUtility.Scale);
267 end;
268
269 function TOpenGLControl.MousePosScale: Single;
270 begin
271 { Note that FGLUtility.Scale seems always 1.0 when this is used
272 with Windows from CASTLE_WINDOW_FORM.
273 Though Windows usage through TCastleControl shows that we need to scale
274 mouse coordinates and sizes by FGLUtility.Scale.
275 See also src\window\castlewindow_form_fmx_scaling.md .
276
277 On Linux, both usage here (from CASTLE_WINDOW_FORM)
278 and in TCastleControl may actually show non-1.0 scaling (configure scaling
279 in GNOME to make it happen) and has to be accounted.
280
281 It seems that correct and cross-platform approach is just to always
282 scale, if we want to convert FMX sizes/coordinates to pixel sizes/coordinates
283 needed by CGE.
284 }
285
286 Result := FGLUtility.Scale;
287 end;
288
289 procedure TOpenGLControl.SetVisible(const AValue: Boolean);
290 begin
291 inherited;
292 { This happens e.g. when developer does "CastleControl.Visible := false".
293 (Not actually used by castlewindow_form.inc for now.)
294 We will recreate this handle later by FGLUtility.HandleNeeded,
295 at first paint. }
296 if not AValue then
297 FGLUtility.HandleRelease;
298 end;
299
300 procedure TOpenGLControl.DoRootChanging(const NewRoot: IRoot);
301 begin
302 inherited;
303 { This happens e.g. when developer does "CastleControl.Parent := nil"
304 or when TOpenGLControl is destroyed.
305 We will recreate this handle later by FGLUtility.HandleNeeded,
306 at first paint. }
307 FGLUtility.HandleRelease;
308 end;
309
310 function TOpenGLControl.PixelsPerInch: Single;
311 const
312 { Default value for container's Dpi, as is usually set on desktops. }
313 DefaultDpi = 96.0;
314 begin
315 Result := FGLUtility.Scale * DefaultDpi;
316 end;
317
318 {$ifdef MSWINDOWS}
319 initialization
320 { Make TWinNativeOpenGLControl used
321 for TOpenGLControl with ControlType = TControlType.Platform. }
322 TPresentationProxyFactory.Current.Register(TOpenGLControl, TControlType.Platform, TWinPresentationProxy<TWinNativeOpenGLControl>);
323 finalization
324 TPresentationProxyFactory.Current.Unregister(TOpenGLControl, TControlType.Platform, TWinPresentationProxy<TWinNativeOpenGLControl>);
325 {$endif}
326 end.