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