apple_glx_pbuffer.c revision 6ddf66e9230ee862ac341c4767cf6b3b2dd2552b
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 "glcontextmodes.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
57static bool pbuffer_make_current(struct apple_glx_context *ac,
58                                 struct apple_glx_drawable *d);
59
60static void pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d);
61
62static 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. */
70bool
71pbuffer_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      glViewport(0, 0, pbuf->width, pbuf->height);
88      glScissor(0, 0, pbuf->width, pbuf->height);
89      ac->made_current = true;
90   }
91
92   apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d->drawable);
93
94   return false;
95}
96
97void
98pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d)
99{
100   struct apple_glx_pbuffer *pbuf = &d->types.pbuffer;
101
102   assert(APPLE_GLX_DRAWABLE_PBUFFER == d->type);
103
104   apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n",
105                        d->drawable);
106
107   apple_cgl.destroy_pbuffer(pbuf->buffer_obj);
108   XFreePixmap(dpy, pbuf->xid);
109}
110
111/* Return true if an error occurred. */
112bool
113apple_glx_pbuffer_destroy(Display * dpy, GLXPbuffer pbuf)
114{
115   return !apple_glx_drawable_destroy_by_type(dpy, pbuf,
116                                              APPLE_GLX_DRAWABLE_PBUFFER);
117}
118
119/* Return true if an error occurred. */
120bool
121apple_glx_pbuffer_create(Display * dpy, GLXFBConfig config,
122                         int width, int height, int *errorcode,
123                         GLXPbuffer * result)
124{
125   struct apple_glx_drawable *d;
126   struct apple_glx_pbuffer *pbuf = NULL;
127   CGLError err;
128   Window root;
129   int screen;
130   Pixmap xid;
131   struct glx_config *modes = (__GLcontextModes *) config;
132
133   root = DefaultRootWindow(dpy);
134   screen = DefaultScreen(dpy);
135
136   /*
137    * This pixmap is only used for a persistent XID.
138    * The XC-MISC extension cleans up XIDs and reuses them transparently,
139    * so we need to retain a server-side reference.
140    */
141   xid = XCreatePixmap(dpy, root, (unsigned int) 1,
142                       (unsigned int) 1, DefaultDepth(dpy, screen));
143
144   if (None == xid) {
145      *errorcode = BadAlloc;
146      return true;
147   }
148
149   if (apple_glx_drawable_create(dpy, screen, xid, &d, &callbacks)) {
150      *errorcode = BadAlloc;
151      return true;
152   }
153
154   /* The lock is held in d from create onward. */
155   pbuf = &d->types.pbuffer;
156
157   pbuf->xid = xid;
158   pbuf->width = width;
159   pbuf->height = height;
160
161   err = apple_cgl.create_pbuffer(width, height, GL_TEXTURE_RECTANGLE_EXT,
162                                  (modes->alphaBits > 0) ? GL_RGBA : GL_RGB,
163                                  0, &pbuf->buffer_obj);
164
165   if (kCGLNoError != err) {
166      d->unlock(d);
167      d->destroy(d);
168      *errorcode = BadMatch;
169      return true;
170   }
171
172   pbuf->fbconfigID = modes->fbconfigID;
173
174   pbuf->event_mask = 0;
175
176   *result = pbuf->xid;
177
178   d->unlock(d);
179
180   return false;
181}
182
183
184
185/* Return true if an error occurred. */
186static bool
187get_max_size(int *widthresult, int *heightresult)
188{
189   CGLContextObj oldcontext;
190   GLint ar[2];
191
192   oldcontext = apple_cgl.get_current_context();
193
194   if (!oldcontext) {
195      /*
196       * There is no current context, so we need to make one in order
197       * to call glGetInteger.
198       */
199      CGLPixelFormatObj pfobj;
200      CGLError err;
201      CGLPixelFormatAttribute attr[10];
202      int c = 0;
203      GLint vsref = 0;
204      CGLContextObj newcontext;
205
206      attr[c++] = kCGLPFAColorSize;
207      attr[c++] = 32;
208      attr[c++] = 0;
209
210      err = apple_cgl.choose_pixel_format(attr, &pfobj, &vsref);
211      if (kCGLNoError != err) {
212         if (getenv("LIBGL_DIAGNOSTIC")) {
213            printf("choose_pixel_format error in %s: %s\n", __func__,
214                   apple_cgl.error_string(err));
215         }
216
217         return true;
218      }
219
220
221      err = apple_cgl.create_context(pfobj, NULL, &newcontext);
222
223      if (kCGLNoError != err) {
224         if (getenv("LIBGL_DIAGNOSTIC")) {
225            printf("create_context error in %s: %s\n", __func__,
226                   apple_cgl.error_string(err));
227         }
228
229         apple_cgl.destroy_pixel_format(pfobj);
230
231         return true;
232      }
233
234      err = apple_cgl.set_current_context(newcontext);
235
236      if (kCGLNoError != err) {
237         printf("set_current_context error in %s: %s\n", __func__,
238                apple_cgl.error_string(err));
239         return true;
240      }
241
242
243      glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar);
244
245      apple_cgl.set_current_context(oldcontext);
246      apple_cgl.destroy_context(newcontext);
247      apple_cgl.destroy_pixel_format(pfobj);
248   }
249   else {
250      /* We have a valid context. */
251
252      glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar);
253   }
254
255   *widthresult = ar[0];
256   *heightresult = ar[1];
257
258   return false;
259}
260
261bool
262apple_glx_pbuffer_query(GLXPbuffer p, int attr, unsigned int *value)
263{
264   bool result = false;
265   struct apple_glx_drawable *d;
266   struct apple_glx_pbuffer *pbuf;
267
268   d = apple_glx_drawable_find_by_type(p, APPLE_GLX_DRAWABLE_PBUFFER,
269                                       APPLE_GLX_DRAWABLE_LOCK);
270
271   if (d) {
272      pbuf = &d->types.pbuffer;
273
274      switch (attr) {
275      case GLX_WIDTH:
276         *value = pbuf->width;
277         result = true;
278         break;
279
280      case GLX_HEIGHT:
281         *value = pbuf->height;
282         result = true;
283         break;
284
285      case GLX_PRESERVED_CONTENTS:
286         *value = true;
287         result = true;
288         break;
289
290      case GLX_LARGEST_PBUFFER:{
291            int width, height;
292            if (get_max_size(&width, &height)) {
293               fprintf(stderr, "internal error: "
294                       "unable to find the largest pbuffer!\n");
295            }
296            else {
297               *value = width;
298               result = true;
299            }
300         }
301         break;
302
303      case GLX_FBCONFIG_ID:
304         *value = pbuf->fbconfigID;
305         result = true;
306         break;
307      }
308
309      d->unlock(d);
310   }
311
312   return result;
313}
314
315bool
316apple_glx_pbuffer_set_event_mask(GLXDrawable drawable, unsigned long mask)
317{
318   struct apple_glx_drawable *d;
319   bool result = false;
320
321   d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER,
322                                       APPLE_GLX_DRAWABLE_LOCK);
323
324   if (d) {
325      d->types.pbuffer.event_mask = mask;
326      result = true;
327      d->unlock(d);
328   }
329
330   return result;
331}
332
333bool
334apple_glx_pbuffer_get_event_mask(GLXDrawable drawable, unsigned long *mask)
335{
336   struct apple_glx_drawable *d;
337   bool result = false;
338
339   d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER,
340                                       APPLE_GLX_DRAWABLE_LOCK);
341   if (d) {
342      *mask = d->types.pbuffer.event_mask;
343      result = true;
344      d->unlock(d);
345   }
346
347   return result;
348}
349