1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.opengl;
15
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.internal.*;
19 import org.eclipse.swt.internal.gtk.*;
20 import org.eclipse.swt.internal.opengl.glx.*;
21 import org.eclipse.swt.widgets.*;
22
23 /**
24 * GLCanvas is a widget capable of displaying OpenGL content.
25 *
26 * @see GLData
27 * @see <a href="http://www.eclipse.org/swt/snippets/#opengl">OpenGL snippets</a>
28 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
29 *
30 * @since 3.2
31 */
32
33 public class GLCanvas extends Canvas {
34 long context;
35 long xWindow;
36 long glWindow;
37 XVisualInfo vinfo;
38 static final int MAX_ATTRIBUTES = 32;
39
40 /**
41 * Create a GLCanvas widget using the attributes described in the GLData
42 * object provided.
43 *
44 * @param parent a composite widget
45 * @param style the bitwise OR'ing of widget styles
46 * @param data the requested attributes of the GLCanvas
47 *
48 * @exception IllegalArgumentException
49 * <ul><li>ERROR_NULL_ARGUMENT when the data is null</li>
50 * <li>ERROR_UNSUPPORTED_DEPTH when the requested attributes cannot be provided</li>
51 * </ul>
52 */
GLCanvas(Composite parent, int style, GLData data)53 public GLCanvas (Composite parent, int style, GLData data) {
54 super (parent, style);
55 if (OS.IsWin32) SWT.error (SWT.ERROR_NOT_IMPLEMENTED);
56 if (data == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
57 int glxAttrib [] = new int [MAX_ATTRIBUTES];
58 int pos = 0;
59 glxAttrib [pos++] = GLX.GLX_RGBA;
60 if (data.doubleBuffer) glxAttrib [pos++] = GLX.GLX_DOUBLEBUFFER;
61 if (data.stereo) glxAttrib [pos++] = GLX.GLX_STEREO;
62 if (data.redSize > 0) {
63 glxAttrib [pos++] = GLX.GLX_RED_SIZE;
64 glxAttrib [pos++] = data.redSize;
65 }
66 if (data.greenSize > 0) {
67 glxAttrib [pos++] = GLX.GLX_GREEN_SIZE;
68 glxAttrib [pos++] = data.greenSize;
69 }
70 if (data.blueSize > 0) {
71 glxAttrib [pos++] = GLX.GLX_BLUE_SIZE;
72 glxAttrib [pos++] = data.blueSize;
73 }
74 if (data.alphaSize > 0) {
75 glxAttrib [pos++] = GLX.GLX_ALPHA_SIZE;
76 glxAttrib [pos++] = data.alphaSize;
77 }
78 if (data.depthSize > 0) {
79 glxAttrib [pos++] = GLX.GLX_DEPTH_SIZE;
80 glxAttrib [pos++] = data.depthSize;
81 }
82 if (data.stencilSize > 0) {
83 glxAttrib [pos++] = GLX.GLX_STENCIL_SIZE;
84 glxAttrib [pos++] = data.stencilSize;
85 }
86 if (data.accumRedSize > 0) {
87 glxAttrib [pos++] = GLX.GLX_ACCUM_RED_SIZE;
88 glxAttrib [pos++] = data.accumRedSize;
89 }
90 if (data.accumGreenSize > 0) {
91 glxAttrib [pos++] = GLX.GLX_ACCUM_GREEN_SIZE;
92 glxAttrib [pos++] = data.accumGreenSize;
93 }
94 if (data.accumBlueSize > 0) {
95 glxAttrib [pos++] = GLX.GLX_ACCUM_BLUE_SIZE;
96 glxAttrib [pos++] = data.accumBlueSize;
97 }
98 if (data.accumAlphaSize > 0) {
99 glxAttrib [pos++] = GLX.GLX_ACCUM_ALPHA_SIZE;
100 glxAttrib [pos++] = data.accumAlphaSize;
101 }
102 if (data.sampleBuffers > 0) {
103 glxAttrib [pos++] = GLX.GLX_SAMPLE_BUFFERS;
104 glxAttrib [pos++] = data.sampleBuffers;
105 }
106 if (data.samples > 0) {
107 glxAttrib [pos++] = GLX.GLX_SAMPLES;
108 glxAttrib [pos++] = data.samples;
109 }
110 glxAttrib [pos++] = 0;
111 GTK.gtk_widget_realize (handle);
112 long window = GTK.gtk_widget_get_window (handle);
113
114 long xDisplay = GDK.gdk_x11_display_get_xdisplay(GDK.gdk_window_get_display(window));
115 long infoPtr = GLX.glXChooseVisual (xDisplay, OS.XDefaultScreen (xDisplay), glxAttrib);
116 if (infoPtr == 0) {
117 dispose ();
118 SWT.error (SWT.ERROR_UNSUPPORTED_DEPTH);
119 }
120 vinfo = new XVisualInfo ();
121 GLX.memmove (vinfo, infoPtr, XVisualInfo.sizeof);
122 OS.XFree (infoPtr);
123 long screen = GDK.gdk_screen_get_default ();
124 long gdkvisual = GDK.gdk_x11_screen_lookup_visual (screen, vinfo.visualid);
125 long share = data.shareContext != null ? data.shareContext.context : 0;
126 context = GLX.glXCreateContext (xDisplay, vinfo, share, true);
127 if (context == 0) SWT.error (SWT.ERROR_NO_HANDLES);
128 GdkWindowAttr attrs = new GdkWindowAttr ();
129 attrs.width = 1;
130 attrs.height = 1;
131 attrs.event_mask = GDK.GDK_KEY_PRESS_MASK | GDK.GDK_KEY_RELEASE_MASK |
132 GDK.GDK_FOCUS_CHANGE_MASK | GDK.GDK_POINTER_MOTION_MASK |
133 GDK.GDK_BUTTON_PRESS_MASK | GDK.GDK_BUTTON_RELEASE_MASK |
134 GDK.GDK_ENTER_NOTIFY_MASK | GDK.GDK_LEAVE_NOTIFY_MASK |
135 GDK.GDK_EXPOSURE_MASK | GDK.GDK_POINTER_MOTION_HINT_MASK;
136 attrs.window_type = GDK.GDK_WINDOW_CHILD;
137 attrs.visual = gdkvisual;
138 glWindow = GDK.gdk_window_new (window, attrs, GDK.GDK_WA_VISUAL);
139 GDK.gdk_window_set_user_data (glWindow, handle);
140 if ((style & SWT.NO_BACKGROUND) != 0) {
141 //TODO: implement this on GTK3 as pixmaps are gone.
142 }
143 xWindow = GDK.gdk_x11_window_get_xid (glWindow);
144 GDK.gdk_window_show (glWindow);
145
146 Listener listener = event -> {
147 switch (event.type) {
148 case SWT.Paint:
149 /**
150 * Bug in MESA. MESA does some nasty sort of polling to try
151 * and ensure that their buffer sizes match the current X state.
152 * This state can be updated using glViewport().
153 * FIXME: There has to be a better way of doing this.
154 */
155 int [] viewport = new int [4];
156 GLX.glGetIntegerv (GLX.GL_VIEWPORT, viewport);
157 GLX.glViewport (viewport [0],viewport [1],viewport [2],viewport [3]);
158 break;
159 case SWT.Resize:
160 Rectangle clientArea = DPIUtil.autoScaleUp(getClientArea());
161 GDK.gdk_window_move (glWindow, clientArea.x, clientArea.y);
162 GDK.gdk_window_resize (glWindow, clientArea.width, clientArea.height);
163 break;
164 case SWT.Dispose:
165 long window1 = GTK.gtk_widget_get_window (handle);
166 long xDisplay1 = gdk_x11_display_get_xdisplay (window1);
167 if (context != 0) {
168 if (GLX.glXGetCurrentContext () == context) {
169 GLX.glXMakeCurrent (xDisplay1, 0, 0);
170 }
171 GLX.glXDestroyContext (xDisplay1, context);
172 context = 0;
173 }
174 if (glWindow != 0) {
175 GDK.gdk_window_destroy (glWindow);
176 glWindow = 0;
177 }
178 break;
179 }
180 };
181 addListener (SWT.Resize, listener);
182 addListener (SWT.Paint, listener);
183 addListener (SWT.Dispose, listener);
184 }
185
186 /**
187 * Returns a GLData object describing the created context.
188 *
189 * @return GLData description of the OpenGL context attributes
190 * @exception SWTException <ul>
191 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
192 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
193 * </ul>
194 */
getGLData()195 public GLData getGLData () {
196 checkWidget ();
197 long window = GTK.gtk_widget_get_window (handle);
198 long xDisplay = gdk_x11_display_get_xdisplay (window);
199 GLData data = new GLData ();
200 int [] value = new int [1];
201 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_DOUBLEBUFFER, value);
202 data.doubleBuffer = value [0] != 0;
203 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_STEREO, value);
204 data.stereo = value [0] != 0;
205 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_RED_SIZE, value);
206 data.redSize = value [0];
207 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_GREEN_SIZE, value);
208 data.greenSize = value [0];
209 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_BLUE_SIZE, value);
210 data.blueSize = value [0];
211 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_ALPHA_SIZE, value);
212 data.alphaSize = value [0];
213 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_DEPTH_SIZE, value);
214 data.depthSize = value [0];
215 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_STENCIL_SIZE, value);
216 data.stencilSize = value [0];
217 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_ACCUM_RED_SIZE, value);
218 data.accumRedSize = value [0];
219 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_ACCUM_GREEN_SIZE, value);
220 data.accumGreenSize = value [0];
221 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_ACCUM_BLUE_SIZE, value);
222 data.accumBlueSize = value [0];
223 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_ACCUM_ALPHA_SIZE, value);
224 data.accumAlphaSize = value [0];
225 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_SAMPLE_BUFFERS, value);
226 data.sampleBuffers = value [0];
227 GLX.glXGetConfig (xDisplay, vinfo, GLX.GLX_SAMPLES, value);
228 data.samples = value [0];
229 return data;
230 }
231
232 /**
233 * Returns a boolean indicating whether the receiver's OpenGL context
234 * is the current context.
235 *
236 * @return true if the receiver holds the current OpenGL context,
237 * false otherwise
238 * @exception SWTException <ul>
239 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
240 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
241 * </ul>
242 */
isCurrent()243 public boolean isCurrent () {
244 checkWidget ();
245 return GLX.glXGetCurrentContext () == context;
246 }
247
248 /**
249 * Sets the OpenGL context associated with this GLCanvas to be the
250 * current GL context.
251 *
252 * @exception SWTException <ul>
253 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
254 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
255 * </ul>
256 */
setCurrent()257 public void setCurrent () {
258 checkWidget ();
259 if (GLX.glXGetCurrentContext () == context) return;
260 long window = GTK.gtk_widget_get_window (handle);
261 long xDisplay = gdk_x11_display_get_xdisplay (window);
262 GLX.glXMakeCurrent (xDisplay, xWindow, context);
263 }
264
265 /**
266 * Swaps the front and back color buffers.
267 *
268 * @exception SWTException <ul>
269 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
270 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
271 * </ul>
272 */
swapBuffers()273 public void swapBuffers () {
274 checkWidget ();
275 long window = GTK.gtk_widget_get_window (handle);
276 long xDisplay = gdk_x11_display_get_xdisplay (window);
277 GLX.glXSwapBuffers (xDisplay, xWindow);
278 }
279
gdk_x11_display_get_xdisplay(long window)280 private long gdk_x11_display_get_xdisplay(long window) {
281 return GDK.gdk_x11_display_get_xdisplay(GDK.gdk_window_get_display(window));
282 }
283 }
284