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