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
|
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.opengl;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.carbon.*;
import org.eclipse.swt.internal.opengl.carbon.*;
/**
* GLCanvas is a widget capable of displaying OpenGL content.
*
* WARNING API STILL UNDER CONSTRUCTION AND SUBJECT TO CHANGE
*
* @since 3.2
*/
public class GLCanvas extends Canvas {
int context;
int pixelFormat;
static final int MAX_ATTRIBUTES = 32;
/**
* Create a GLCanvas widget using the attributes described in the GLData
* object provided.
*
* @param parent a composite widget
* @param style the bitwise OR'ing of widget styles
* @param data the requested attributes of the GLCanvas
*
* @exception IllegalArgumentException
* <ul><li>ERROR_NULL_ARGUMENT when the data is null
* <li>ERROR_UNSUPPORTED_DEPTH when the requested attributes cannot be provided</ul>
* </ul>
*
* WARNING API STILL UNDER CONSTRUCTION AND SUBJECT TO CHANGE
*
* @since 3.2
*/
public GLCanvas (Composite parent, int style, GLData data) {
super (parent, style);
if (data == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
int aglAttrib [] = new int [MAX_ATTRIBUTES];
int pos = 0;
aglAttrib [pos++] = AGL.AGL_RGBA;
if (data.doubleBuffer) aglAttrib [pos++] = AGL.AGL_DOUBLEBUFFER;
if (data.stereo) aglAttrib [pos++] = AGL.AGL_STEREO;
if (data.redSize > 0) {
aglAttrib [pos++] = AGL.AGL_RED_SIZE;
aglAttrib [pos++] = data.redSize;
}
if (data.greenSize > 0) {
aglAttrib [pos++] = AGL.AGL_GREEN_SIZE;
aglAttrib [pos++] = data.greenSize;
}
if (data.blueSize > 0) {
aglAttrib [pos++] = AGL.AGL_BLUE_SIZE;
aglAttrib [pos++] = data.blueSize;
}
if (data.alphaSize > 0) {
aglAttrib [pos++] = AGL.AGL_ALPHA_SIZE;
aglAttrib [pos++] = data.alphaSize;
}
if (data.depthSize > 0) {
aglAttrib [pos++] = AGL.AGL_DEPTH_SIZE;
aglAttrib [pos++] = data.depthSize;
}
if (data.stencilSize > 0) {
aglAttrib [pos++] = AGL.AGL_STENCIL_SIZE;
aglAttrib [pos++] = data.stencilSize;
}
if (data.accumRedSize > 0) {
aglAttrib [pos++] = AGL.AGL_ACCUM_RED_SIZE;
aglAttrib [pos++] = data.accumRedSize;
}
if (data.accumGreenSize > 0) {
aglAttrib [pos++] = AGL.AGL_ACCUM_GREEN_SIZE;
aglAttrib [pos++] = data.accumGreenSize;
}
if (data.accumBlueSize > 0) {
aglAttrib [pos++] = AGL.AGL_ACCUM_BLUE_SIZE;
aglAttrib [pos++] = data.accumBlueSize;
}
if (data.accumAlphaSize > 0) {
aglAttrib [pos++] = AGL.AGL_ACCUM_ALPHA_SIZE;
aglAttrib [pos++] = data.accumAlphaSize;
}
if (data.sampleBuffers > 0) {
aglAttrib [pos++] = AGL.AGL_SAMPLE_BUFFERS_ARB;
aglAttrib [pos++] = data.sampleBuffers;
}
if (data.samples > 0) {
aglAttrib [pos++] = AGL.AGL_SAMPLES_ARB;
aglAttrib [pos++] = data.samples;
}
aglAttrib [pos++] = AGL.AGL_NONE;
pixelFormat = AGL.aglChoosePixelFormat (0, 0, aglAttrib);
if (pixelFormat == 0) {
dispose ();
SWT.error (SWT.ERROR_UNSUPPORTED_DEPTH);
}
//FIXME- share lists
//context = AGL.aglCreateContext (pixelFormat, share == null ? 0 : share.context);
context = AGL.aglCreateContext (pixelFormat, 0);
int window = OS.GetControlOwner (handle);
int port = OS.GetWindowPort (window);
AGL.aglSetDrawable (context, port);
Listener listener = new Listener () {
public void handleEvent (Event event) {
switch (event.type) {
case SWT.Dispose:
AGL.aglDestroyContext (context);
AGL.aglDestroyPixelFormat (pixelFormat);
// Remove listeners
Shell shell = getShell();
shell.removeListener(SWT.Resize, this);
shell.removeListener(SWT.Show, this);
shell.removeListener(SWT.Hide, this);
Control c = GLCanvas.this;
do {
c.removeListener(SWT.Show, this);
c.removeListener(SWT.Hide, this);
c = c.getParent();
} while (c != shell);
break;
case SWT.Resize:
case SWT.Hide:
case SWT.Show:
getDisplay().asyncExec(new Runnable() {
public void run() {
fixBounds();
}
});
break;
}
}
};
addListener (SWT.Resize, listener);
Shell shell = getShell();
shell.addListener(SWT.Resize, listener);
shell.addListener(SWT.Show, listener);
shell.addListener(SWT.Hide, listener);
Control c = this;
do {
c.addListener(SWT.Show, listener);
c.addListener(SWT.Hide, listener);
c = c.getParent();
} while (c != shell);
addListener (SWT.Dispose, listener);
}
void fixBounds () {
GCData data = new GCData ();
int gc = internal_new_GC (data);
Rect bounds = new Rect ();
OS.GetRegionBounds (data.visibleRgn, bounds);
int width = bounds.right - bounds.left;
int height = bounds.bottom - bounds.top;
Rect rect = new Rect ();
int window = OS.GetControlOwner (handle);
int port = OS.GetWindowPort (window);
OS.GetPortBounds (port, rect);
int [] glbounds = new int [4];
glbounds[0] = bounds.left;
glbounds[1] = rect.bottom - rect.top - bounds.top - height;
glbounds[2] = width;
glbounds[3] = height;
AGL.aglSetInteger (context, AGL.AGL_BUFFER_RECT, glbounds);
AGL.aglEnable (context, AGL.AGL_BUFFER_RECT);
AGL.aglSetInteger (context, AGL.AGL_CLIP_REGION, data.visibleRgn);
AGL.aglUpdateContext (context);
internal_dispose_GC (gc, data);
}
/**
* Returns a GLData object describing the created context.
*
* @return GLData description of the OpenGL context attributes
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* WARNING API STILL UNDER CONSTRUCTION AND SUBJECT TO CHANGE
*
* @since 3.2
*/
public GLData getGLData () {
checkWidget ();
GLData data = new GLData ();
int [] value = new int [1];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_DOUBLEBUFFER, value);
data.doubleBuffer = value [0] != 0;
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_STEREO, value);
data.stereo = value [0] != 0;
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_RED_SIZE, value);
data.redSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_GREEN_SIZE, value);
data.greenSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_BLUE_SIZE, value);
data.blueSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ALPHA_SIZE, value);
data.alphaSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_DEPTH_SIZE, value);
data.depthSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_STENCIL_SIZE, value);
data.stencilSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_RED_SIZE, value);
data.accumRedSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_GREEN_SIZE, value);
data.accumGreenSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_BLUE_SIZE, value);
data.accumBlueSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_ALPHA_SIZE, value);
data.accumAlphaSize = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_SAMPLE_BUFFERS_ARB, value);
data.sampleBuffers = value [0];
AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_SAMPLES_ARB, value);
data.samples = value [0];
return data;
}
/**
* Returns a boolean indicating whether the receiver's OpenGL context
* is the current context.
*
* @return true if the receiver holds the current OpenGL context,
* false otherwise
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* WARNING API STILL UNDER CONSTRUCTION AND SUBJECT TO CHANGE
*
* @since 3.2
*/
public boolean isCurrent () {
checkWidget ();
return AGL.aglGetCurrentContext () == context;
}
/**
* Sets the OpenGL context associated with this GLCanvas to be the
* current GL context.
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* WARNING API STILL UNDER CONSTRUCTION AND SUBJECT TO CHANGE
*
* @since 3.2
*/
public void setCurrent () {
checkWidget ();
if (AGL.aglGetCurrentContext () != context) {
AGL.aglSetCurrentContext (context);
}
}
/**
* Swaps the front and back color buffers.
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* WARNING API STILL UNDER CONSTRUCTION AND SUBJECT TO CHANGE
*
* @since 3.2
*/
public void swapBuffers () {
checkWidget ();
AGL.aglSwapBuffers (context);
}
}
|