File: glcarbonaglcontext.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (342 lines) | stat: -rw-r--r-- 10,259 bytes parent folder | download | duplicates (6)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Author: Mattias Gaertner

  ToDo: MultiSampling, AuxBuffers
}
unit GLCarbonAGLContext;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc, LCLType, gl, Forms,
  MacOSAll,
  AGL, CarbonProc, CarbonDef, CarbonPrivate,
  WSLCLClasses, CarbonUtils,
  Controls;

procedure LOpenGLViewport(Handle: HWND; Left, Top, Width, Height: integer);
procedure LOpenGLSwapBuffers(Handle: HWND);
function LOpenGLMakeCurrent(Handle: HWND): boolean;
function LOpenGLReleaseContext(Handle: HWND): boolean;
procedure LOpenGLClip(Handle: HWND);
function LOpenGLCreateContext(AWinControl: TWinControl;
              {%H-}WSPrivate: TWSPrivateClass; SharedControl: TWinControl;
              DoubleBuffered, RGBA: boolean;
              const RedBits, GreenBits, BlueBits: Cardinal;
              const MultiSampling, AlphaBits, DepthBits, StencilBits, AUXBuffers: Cardinal;
              const {%H-}AParams: TCreateParams): HWND;
procedure LOpenGLDestroyContextInfo(AWinControl: TWinControl);
function CreateOpenGLContextAttrList(DoubleBuffered: boolean; RGBA: boolean;
              RedBits, GreenBits, BlueBits, MultiSampling, AlphaBits, DepthBits,
              StencilBits, AUXBuffers: cardinal): PInteger;


type
  TAGLControlInfo = record
    Control: ControlRef;
    WinControl: TWinControl;
    AGLContext: TAGLContext;
  end;
  PAGLControlInfo = ^TAGLControlInfo;

var
  AGLControlInfo_FOURCC: FourCharCode;

function CreateAGLControlInfo(Control: ControlRef; AGLContext: TAGLContext;
  WinControl: TWinControl): PAGLControlInfo;
function GetAGLControlInfo(Control: ControlRef): PAGLControlInfo;
procedure FreeAGLControlInfo(Control: ControlRef);
function GetAGLContext(Control: ControlRef): TAGLContext;

implementation

procedure LOpenGLViewport(Handle: HWND; Left, Top, Width, Height: integer);
begin
  glViewport(Left,Top,Width,Height);
end;

procedure LOpenGLSwapBuffers(Handle: HWND);
var
  AGLContext: TAGLContext;
begin
  AGLContext:=GetAGLContext({%H-}ControlRef(Handle));
  aglSwapBuffers(AGLContext);
end;

function LOpenGLMakeCurrent(Handle: HWND): boolean;
var
  AGLContext: TAGLContext;
  Info: PAGLControlInfo;
  Control: TCarbonCustomControl;
begin
  Control:=TCarbonCustomControl(Handle);
  Info:=GetAGLControlInfo(Control.Widget);
  if Info=nil then exit;
  AGLContext:=Info^.AGLContext;
  Result:=aglSetCurrentContext(AGLContext)<>0;
end;

function LOpenGLReleaseContext(Handle: HWND): boolean;
begin
  Result := aglSetCurrentContext(nil)<>GL_FALSE;
end;

procedure LOpenGLClip(Handle: HWND);
var
  AGLContext: TAGLContext;
  Info: PAGLControlInfo;
  Form: TCustomForm;
  Win: WindowRef;
  //b: Rect;
  clipRgn: RgnHandle;
  Control: TCarbonCustomControl;
begin
  Control:=TCarbonCustomControl(Handle);
  Info:=GetAGLControlInfo(Control.Widget);
  debugln(['LOpenGLMakeCurrent got ingo...']);
  if Info=nil then exit;
  AGLContext:=Info^.AGLContext;
  aglSetCurrentContext(AGLContext);
  debugln(['LOpenGLMakeCurrent set current ',dbgs(Handle)]);
  Form:=GetParentForm(Info^.WinControl);
  debugln(['LOpenGLMakeCurrent got parent form: ',DbgSName(Form)]);
  Win:=TCarbonWindow(Form.Handle).Window;

  debugln(['LOpenGLMakeCurrent set clipping ...']);
  //GetWindowPortBounds(Win,b);
  clipRgn:=NewRgn;
  SetRectRgn(clipRgn,10,10,100,100);
  aglSetInteger({%H-}TAGLContext(GetWRefCon(Win)),AGL_CLIP_REGION,clipRgn);
  aglEnable({%H-}TAGLContext(GetWRefCon(Win)),AGL_CLIP_REGION);
  DisposeRgn(clipRgn);
end;

procedure ResizeOGLControl(AWidget: TCarbonWidget);
var
  info  : PAGLControlInfo;
  r     : array [0..3] of Integer;
  bnd   : HIRect;
  str   : MacOSAll.Rect;
  win   : WindowRef;
begin
  info:=GetAGLControlInfo(AWidget.Widget);
  if not Assigned(info) then Exit;
  win:=HIViewGetWindow(AWidget.Widget);
  if not Assigned(win) then Exit;
  GetWindowBounds(win, kWindowStructureRgn, str{%H-});
  HIViewGetBounds(AWidget.Widget, bnd{%H-});
  HIViewConvertPoint(bnd.origin, AWidget.Widget, nil);

  r[0]:=Round(bnd.origin.x);
  r[1]:=Round((str.bottom-str.top)- bnd.origin.y-bnd.size.height);
  r[2]:=Round(bnd.size.width);
  r[3]:=Round(bnd.size.height);

  aglEnable(info^.aglContext, AGL_BUFFER_RECT);
  aglSetInteger(info^.aglContext, AGL_BUFFER_RECT, @r[0]);
end;

function CarbonGLControl_Resize(ANextHandler: EventHandlerCallRef;
  AEvent: EventRef;
  AWidget: TCarbonWidget): OSStatus; {$IFDEF darwin}mwpascal;{$ENDIF}
begin
  Result:=CallNextEventHandler(ANextHandler, AEvent);
  ResizeOGLControl(AWidget);
end;

function CarbonGLControl_WindowChange(ANextHandler: EventHandlerCallRef;
  AEvent: EventRef;
  AWidget: TCarbonWidget): OSStatus; {$IFDEF darwin}mwpascal;{$ENDIF}
var
  win : WindowRef;
begin
  Result:=CallNextEventHandler(ANextHandler, AEvent);
  if GetEventParameter(AEvent, kEventParamControlCurrentOwningWindow, typeWindowRef,
      nil, sizeof(win), nil, @win) = noErr then
    if Assigned(win) then ResizeOGLControl(AWidget);
end;

function LOpenGLCreateContext(AWinControl: TWinControl;
  WSPrivate: TWSPrivateClass; SharedControl: TWinControl;
  DoubleBuffered, RGBA: boolean;
  const RedBits, GreenBits, BlueBits: Cardinal;
  const MultiSampling, AlphaBits, DepthBits, StencilBits, AUXBuffers: Cardinal;
  const AParams: TCreateParams): HWND;
var
  disp: GDHandle;
  aglPixFmt: TAGLPixelFormat;
  aglContext: TAGLContext;
  Control: TCarbonCustomControl;
  AttrList: PInteger;
  C: TCreateParams;
  AGLInfo: PAGLControlInfo;
  TempSpec: EventTypeSpec;
begin
  Result:=0;
  if AWinControl.Parent=nil then
    RaiseGDBException('GLCarbonAGLContext.LOpenGLCreateContext no parent');

  C.X := AWinControl.Left;
  C.Y := AWinControl.Top;
  C.Width := AWinControl.Width;
  C.Height := AWinControl.Height;
  // create a custom control
  Control := TCarbonCustomControl.Create(AWinControl, C);
  //debugln(['LOpenGLCreateContext ',dbgsName(AWinControl)]);

  // create the AGL context
  disp := GetMainDevice ();
  AttrList:=CreateOpenGLContextAttrList(DoubleBuffered,RGBA,
    RedBits,GreenBits,BlueBits,
    MultiSampling,AlphaBits,DepthBits,StencilBits,AUXBuffers);
  aglPixFmt := aglChoosePixelFormat (@disp, 1, AttrList);
  System.FreeMem(AttrList);
  aglContext := aglCreateContext (aglPixFmt, NIL);
  aglDestroyPixelFormat(aglPixFmt);

  aglSetDrawable(aglContext,
    GetWindowPort(TCarbonWindow(GetParentForm(AWinControl).Handle).Window));
  AGLControlInfo_FOURCC := MakeFourCC('ACI ');

  AGLInfo:=CreateAGLControlInfo(Control.Widget, AGLContext, AWinControl);
  if AGLInfo<>GetAGLControlInfo(Control.Widget) then
    RaiseGDBException('GLCarbonAGLContext.LOpenGLCreateContext inconsistency');

  ResizeOGLControl(Control);
  TempSpec:=MakeEventSpec(kEventClassControl, kEventControlBoundsChanged);
  InstallControlEventHandler(Control.Widget, RegisterEventHandler(@CarbonGLControl_Resize),
    1, @TempSpec, Control, nil);
  TempSpec:=MakeEventSpec(kEventClassControl, kEventControlOwningWindowChanged);
  // The control might be embeded into a window after its creation.
  // See example for this in bug report #17244
  InstallControlEventHandler(Control.Widget, RegisterEventHandler(@CarbonGLControl_WindowChange),
    1, @TempSpec, Control, nil);

  Result:=HWnd(Control);
  //debugln(['LOpenGLCreateContext ',dbgs(Result)]);
end;

procedure LOpenGLDestroyContextInfo(AWinControl: TWinControl);
var
  Ref: ControlRef;
  Info: PAGLControlInfo;
begin
  if not AWinControl.HandleAllocated then Exit;
  if csDesigning in AWinControl.ComponentState then Exit;
  Ref := ControlRef(TCarbonControl(AWinControl.Handle).Widget);
  Info := GetAGLControlInfo(Ref);
  if Info=nil then exit;
  aglDestroyContext(Info^.AGLContext);
  Info^.AGLContext := nil;
  FreeAGLControlInfo(Ref);
end;

function CreateOpenGLContextAttrList(DoubleBuffered: boolean; RGBA: boolean;
  RedBits, GreenBits, BlueBits, MultiSampling, AlphaBits, DepthBits,
  StencilBits, AUXBuffers: cardinal): PInteger;
var
  p: integer;

  procedure Add(i: integer);
  begin
    if Result<>nil then
      Result[p]:=i;
    inc(p);
  end;

  procedure CreateList;
  begin
    Add(AGL_WINDOW);
    if DoubleBuffered then
      Add(AGL_DOUBLEBUFFER);
    if RGBA then
      Add(AGL_RGBA);
    Add(AGL_NO_RECOVERY);
    Add(AGL_MAXIMUM_POLICY);
    Add(AGL_SINGLE_RENDERER);
    Add(AGL_RED_SIZE); Add(RedBits);
    Add(AGL_GREEN_SIZE); Add(GreenBits);
    Add(AGL_BLUE_SIZE); Add(BlueBits);
    if AlphaBits>0 then
    begin
      Add(AGL_ALPHA_SIZE);  Add(AlphaBits);
    end;
    if DepthBits>0 then
    begin
      Add(AGL_DEPTH_SIZE);  Add(DepthBits);
    end;
    if StencilBits>0 then
    begin
      Add(AGL_STENCIL_SIZE);  Add(StencilBits);
    end;
    if AUXBuffers>0 then
    begin
      Add(AGL_AUX_BUFFERS);  Add(AUXBuffers);
    end;
    if MultiSampling > 1 then
    begin
      Add(AGL_SAMPLE_BUFFERS_ARB); Add(1);
      Add(AGL_SAMPLES_ARB); Add(MultiSampling);
    end;

    Add(AGL_NONE);
  end;

begin
  Result:=nil;
  p:=0;
  CreateList;
  GetMem(Result,SizeOf(integer)*p);
  p:=0;
  CreateList;
end;

function CreateAGLControlInfo(Control: ControlRef; AGLContext: TAGLContext;
  WinControl: TWinControl): PAGLControlInfo;
begin
  New(Result);
  FillByte(Result^, SizeOf(Result^), 0);
  Result^.Control:=Control;
  Result^.WinControl:=WinControl;
  Result^.AGLContext:=AGLContext;

  SetControlProperty(Control, LAZARUS_FOURCC, AGLControlInfo_FOURCC,
                     SizeOf(Result), @Result);
end;

function GetAGLControlInfo(Control: ControlRef): PAGLControlInfo;
var
  m: LongWord;
begin
  GetControlProperty(Control, LAZARUS_FOURCC, AGLControlInfo_FOURCC,
                     SizeOf(Result), @m, @Result);
end;

procedure FreeAGLControlInfo(Control: ControlRef);
var
  Info: PAGLControlInfo;
begin
  Info:=GetAGLControlInfo(Control);
  if Info=nil then exit;
  RemoveControlProperty(Control, LAZARUS_FOURCC, AGLControlInfo_FOURCC);
  System.FreeMem(Info);
end;

function GetAGLContext(Control: ControlRef): TAGLContext;
begin
  Result:=GetAGLControlInfo(TCarbonCustomControl(Control).Widget)^.AGLContext;
end;

initialization

finalization

end.