• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  Copyright (c) 2009 Apple Inc.
3 
4  Permission is hereby granted, free of charge, to any person
5  obtaining a copy of this software and associated documentation files
6  (the "Software"), to deal in the Software without restriction,
7  including without limitation the rights to use, copy, modify, merge,
8  publish, distribute, sublicense, and/or sell copies of the Software,
9  and to permit persons to whom the Software is furnished to do so,
10  subject to the following conditions:
11 
12  The above copyright notice and this permission notice shall be
13  included in all copies or substantial portions of the Software.
14 
15  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19  HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 
24  Except as contained in this notice, the name(s) of the above
25  copyright holders shall not be used in advertising or otherwise to
26  promote the sale, use or other dealings in this Software without
27  prior written authorization.
28 */
29 
30 /* Must be before OpenGL.framework is included.  Remove once fixed:
31  * <rdar://problem/7872773>
32  */
33 #include <GL/gl.h>
34 #include <GL/glext.h>
35 #define __gltypes_h_ 1
36 
37 /* Must be first for:
38  * <rdar://problem/6953344>
39  */
40 #include "apple_glx_context.h"
41 #include "apple_glx_drawable.h"
42 
43 #include <stdlib.h>
44 #include <pthread.h>
45 #include <assert.h>
46 #include "apple_glx.h"
47 #include "glxconfig.h"
48 #include "apple_cgl.h"
49 
50 /* mesa defines in glew.h, Apple in glext.h.
51  * Due to namespace nightmares, just do it here.
52  */
53 #ifndef GL_TEXTURE_RECTANGLE_EXT
54 #define GL_TEXTURE_RECTANGLE_EXT 0x84F5
55 #endif
56 
57 static bool pbuffer_make_current(struct apple_glx_context *ac,
58                                  struct apple_glx_drawable *d);
59 
60 static void pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d);
61 
62 static struct apple_glx_drawable_callbacks callbacks = {
63    .type = APPLE_GLX_DRAWABLE_PBUFFER,
64    .make_current = pbuffer_make_current,
65    .destroy = pbuffer_destroy
66 };
67 
68 
69 /* Return true if an error occurred. */
70 bool
pbuffer_make_current(struct apple_glx_context * ac,struct apple_glx_drawable * d)71 pbuffer_make_current(struct apple_glx_context *ac,
72                      struct apple_glx_drawable *d)
73 {
74    struct apple_glx_pbuffer *pbuf = &d->types.pbuffer;
75    CGLError cglerr;
76 
77    assert(APPLE_GLX_DRAWABLE_PBUFFER == d->type);
78 
79    cglerr = apple_cgl.set_pbuffer(ac->context_obj, pbuf->buffer_obj, 0, 0, 0);
80 
81    if (kCGLNoError != cglerr) {
82       fprintf(stderr, "set_pbuffer: %s\n", apple_cgl.error_string(cglerr));
83       return true;
84    }
85 
86    if (!ac->made_current) {
87       apple_glapi_oglfw_viewport_scissor(0, 0, pbuf->width, pbuf->height);
88       ac->made_current = true;
89    }
90 
91    apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d->drawable);
92 
93    return false;
94 }
95 
96 void
pbuffer_destroy(Display * dpy,struct apple_glx_drawable * d)97 pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d)
98 {
99    struct apple_glx_pbuffer *pbuf = &d->types.pbuffer;
100 
101    assert(APPLE_GLX_DRAWABLE_PBUFFER == d->type);
102 
103    apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n",
104                         d->drawable);
105 
106    apple_cgl.destroy_pbuffer(pbuf->buffer_obj);
107    XFreePixmap(dpy, pbuf->xid);
108 }
109 
110 /* Return true if an error occurred. */
111 bool
apple_glx_pbuffer_destroy(Display * dpy,GLXPbuffer pbuf)112 apple_glx_pbuffer_destroy(Display * dpy, GLXPbuffer pbuf)
113 {
114    return !apple_glx_drawable_destroy_by_type(dpy, pbuf,
115                                               APPLE_GLX_DRAWABLE_PBUFFER);
116 }
117 
118 /* Return true if an error occurred. */
119 bool
apple_glx_pbuffer_create(Display * dpy,GLXFBConfig config,int width,int height,int * errorcode,GLXPbuffer * result)120 apple_glx_pbuffer_create(Display * dpy, GLXFBConfig config,
121                          int width, int height, int *errorcode,
122                          GLXPbuffer * result)
123 {
124    struct apple_glx_drawable *d;
125    struct apple_glx_pbuffer *pbuf = NULL;
126    CGLError err;
127    Window root;
128    int screen;
129    Pixmap xid;
130    struct glx_config *modes = (struct glx_config *) config;
131 
132    root = DefaultRootWindow(dpy);
133    screen = DefaultScreen(dpy);
134 
135    /*
136     * This pixmap is only used for a persistent XID.
137     * The XC-MISC extension cleans up XIDs and reuses them transparently,
138     * so we need to retain a server-side reference.
139     */
140    xid = XCreatePixmap(dpy, root, (unsigned int) 1,
141                        (unsigned int) 1, DefaultDepth(dpy, screen));
142 
143    if (None == xid) {
144       *errorcode = BadAlloc;
145       return true;
146    }
147 
148    if (apple_glx_drawable_create(dpy, screen, xid, &d, &callbacks)) {
149       *errorcode = BadAlloc;
150       return true;
151    }
152 
153    /* The lock is held in d from create onward. */
154    pbuf = &d->types.pbuffer;
155 
156    pbuf->xid = xid;
157    pbuf->width = width;
158    pbuf->height = height;
159 
160    err = apple_cgl.create_pbuffer(width, height, GL_TEXTURE_RECTANGLE_EXT,
161                                   (modes->alphaBits > 0) ? GL_RGBA : GL_RGB,
162                                   0, &pbuf->buffer_obj);
163 
164    if (kCGLNoError != err) {
165       d->unlock(d);
166       d->destroy(d);
167       *errorcode = BadMatch;
168       return true;
169    }
170 
171    pbuf->fbconfigID = modes->fbconfigID;
172 
173    pbuf->event_mask = 0;
174 
175    *result = pbuf->xid;
176 
177    d->unlock(d);
178 
179    return false;
180 }
181 
182 
183 
184 /* Return true if an error occurred. */
185 static bool
get_max_size(int * widthresult,int * heightresult)186 get_max_size(int *widthresult, int *heightresult)
187 {
188    CGLContextObj oldcontext;
189    GLint ar[2];
190 
191    oldcontext = apple_cgl.get_current_context();
192 
193    if (!oldcontext) {
194       /*
195        * There is no current context, so we need to make one in order
196        * to call glGetInteger.
197        */
198       CGLPixelFormatObj pfobj;
199       CGLError err;
200       CGLPixelFormatAttribute attr[10];
201       int c = 0;
202       GLint vsref = 0;
203       CGLContextObj newcontext;
204 
205       attr[c++] = kCGLPFAColorSize;
206       attr[c++] = 32;
207       attr[c++] = 0;
208 
209       err = apple_cgl.choose_pixel_format(attr, &pfobj, &vsref);
210       if (kCGLNoError != err) {
211          if (getenv("LIBGL_DIAGNOSTIC")) {
212             printf("choose_pixel_format error in %s: %s\n", __func__,
213                    apple_cgl.error_string(err));
214          }
215 
216          return true;
217       }
218 
219 
220       err = apple_cgl.create_context(pfobj, NULL, &newcontext);
221 
222       if (kCGLNoError != err) {
223          if (getenv("LIBGL_DIAGNOSTIC")) {
224             printf("create_context error in %s: %s\n", __func__,
225                    apple_cgl.error_string(err));
226          }
227 
228          apple_cgl.destroy_pixel_format(pfobj);
229 
230          return true;
231       }
232 
233       err = apple_cgl.set_current_context(newcontext);
234 
235       if (kCGLNoError != err) {
236          printf("set_current_context error in %s: %s\n", __func__,
237                 apple_cgl.error_string(err));
238          return true;
239       }
240 
241 
242       glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar);
243 
244       apple_cgl.set_current_context(oldcontext);
245       apple_cgl.destroy_context(newcontext);
246       apple_cgl.destroy_pixel_format(pfobj);
247    }
248    else {
249       /* We have a valid context. */
250 
251       glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar);
252    }
253 
254    *widthresult = ar[0];
255    *heightresult = ar[1];
256 
257    return false;
258 }
259 
260 bool
apple_glx_pbuffer_query(GLXPbuffer p,int attr,unsigned int * value)261 apple_glx_pbuffer_query(GLXPbuffer p, int attr, unsigned int *value)
262 {
263    bool result = false;
264    struct apple_glx_drawable *d;
265    struct apple_glx_pbuffer *pbuf;
266 
267    d = apple_glx_drawable_find_by_type(p, APPLE_GLX_DRAWABLE_PBUFFER,
268                                        APPLE_GLX_DRAWABLE_LOCK);
269 
270    if (d) {
271       pbuf = &d->types.pbuffer;
272 
273       switch (attr) {
274       case GLX_WIDTH:
275          *value = pbuf->width;
276          result = true;
277          break;
278 
279       case GLX_HEIGHT:
280          *value = pbuf->height;
281          result = true;
282          break;
283 
284       case GLX_PRESERVED_CONTENTS:
285          *value = true;
286          result = true;
287          break;
288 
289       case GLX_LARGEST_PBUFFER:{
290             int width, height;
291             if (get_max_size(&width, &height)) {
292                fprintf(stderr, "internal error: "
293                        "unable to find the largest pbuffer!\n");
294             }
295             else {
296                *value = width;
297                result = true;
298             }
299          }
300          break;
301 
302       case GLX_FBCONFIG_ID:
303          *value = pbuf->fbconfigID;
304          result = true;
305          break;
306       }
307 
308       d->unlock(d);
309    }
310 
311    return result;
312 }
313 
314 bool
apple_glx_pbuffer_set_event_mask(GLXDrawable drawable,unsigned long mask)315 apple_glx_pbuffer_set_event_mask(GLXDrawable drawable, unsigned long mask)
316 {
317    struct apple_glx_drawable *d;
318    bool result = false;
319 
320    d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER,
321                                        APPLE_GLX_DRAWABLE_LOCK);
322 
323    if (d) {
324       d->types.pbuffer.event_mask = mask;
325       result = true;
326       d->unlock(d);
327    }
328 
329    return result;
330 }
331 
332 bool
apple_glx_pbuffer_get_event_mask(GLXDrawable drawable,unsigned long * mask)333 apple_glx_pbuffer_get_event_mask(GLXDrawable drawable, unsigned long *mask)
334 {
335    struct apple_glx_drawable *d;
336    bool result = false;
337 
338    d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER,
339                                        APPLE_GLX_DRAWABLE_LOCK);
340    if (d) {
341       *mask = d->types.pbuffer.event_mask;
342       result = true;
343       d->unlock(d);
344    }
345 
346    return result;
347 }
348