apple_glx_surface.c revision 0594cf70883b64692ba617d85f4f9b4e636e5c2b
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#include <assert.h>
30#include "glxclient.h"
31#include "apple_glx.h"
32#include "appledri.h"
33#include "apple_glx_drawable.h"
34
35static bool surface_make_current(struct apple_glx_context *ac,
36                                 struct apple_glx_drawable *d);
37
38static void surface_destroy(Display * dpy, struct apple_glx_drawable *d);
39
40
41static struct apple_glx_drawable_callbacks callbacks = {
42   .type = APPLE_GLX_DRAWABLE_SURFACE,
43   .make_current = surface_make_current,
44   .destroy = surface_destroy
45};
46
47static void
48update_viewport_and_scissor(Display * dpy, GLXDrawable drawable)
49{
50   Window root;
51   int x, y;
52   unsigned int width = 0, height = 0, bd, depth;
53
54   XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &bd, &depth);
55
56   glViewport(0, 0, width, height);
57   glScissor(0, 0, width, height);
58}
59
60static bool
61surface_make_current(struct apple_glx_context *ac,
62                     struct apple_glx_drawable *d)
63{
64   struct apple_glx_surface *s = &d->types.surface;
65   xp_error error;
66
67   assert(APPLE_GLX_DRAWABLE_SURFACE == d->type);
68
69   apple_glx_diagnostic("%s: ac->context_obj %p s->surface_id %u\n",
70                        __func__, (void *) ac->context_obj, s->surface_id);
71
72   error = xp_attach_gl_context(ac->context_obj, s->surface_id);
73
74   if (error) {
75      fprintf(stderr, "error: xp_attach_gl_context returned: %d\n", error);
76      return true;
77   }
78
79
80   if (!ac->made_current) {
81      /*
82       * The first time a new context is made current the glViewport
83       * and glScissor should be updated.
84       */
85      update_viewport_and_scissor(ac->drawable->display,
86                                  ac->drawable->drawable);
87      ac->made_current = true;
88   }
89
90   apple_glx_diagnostic("%s: drawable 0x%lx\n", __func__, d->drawable);
91
92   return false;
93}
94
95static void
96surface_destroy(Display * dpy, struct apple_glx_drawable *d)
97{
98   struct apple_glx_surface *s = &d->types.surface;
99
100   apple_glx_diagnostic("%s: s->surface_id %u\n", __func__, s->surface_id);
101
102   xp_error error = xp_destroy_surface(s->surface_id);
103
104   if (error) {
105      fprintf(stderr, "xp_destroy_surface error: %d\n", (int) error);
106   }
107
108   /*
109    * Check if this surface destroy came from the surface being destroyed
110    * on the server.  If s->pending_destroy is true, then it did, and
111    * we don't want to try to destroy the surface on the server.
112    */
113   if (!s->pending_destroy) {
114      /*
115       * Warning: this causes other routines to be called (potentially)
116       * from surface_notify_handler.  It's probably best to not have
117       * any locks at this point locked.
118       */
119      XAppleDRIDestroySurface(d->display, DefaultScreen(d->display),
120                              d->drawable);
121
122      apple_glx_diagnostic
123         ("%s: destroyed a surface for drawable 0x%lx uid %u\n", __func__,
124          d->drawable, s->uid);
125   }
126}
127
128/* Return true if an error occured. */
129static bool
130create_surface(Display * dpy, int screen, struct apple_glx_drawable *d)
131{
132   struct apple_glx_surface *s = &d->types.surface;
133   unsigned int key[2];
134   xp_client_id id;
135
136   id = apple_glx_get_client_id();
137   if (0 == id)
138      return true;
139
140   assert(None != d->drawable);
141
142   s->pending_destroy = false;
143
144   if (XAppleDRICreateSurface(dpy, screen, d->drawable, id, key, &s->uid)) {
145      xp_error error;
146
147      error = xp_import_surface(key, &s->surface_id);
148
149      if (error) {
150         fprintf(stderr, "error: xp_import_surface returned: %d\n", error);
151         return true;
152      }
153
154      apple_glx_diagnostic("%s: created a surface for drawable 0x%lx"
155                           " with uid %u\n", __func__, d->drawable, s->uid);
156      return false;             /*success */
157   }
158
159   return true;                 /* unable to create a surface. */
160}
161
162/* Return true if an error occured. */
163/* This returns a referenced object via resultptr. */
164bool
165apple_glx_surface_create(Display * dpy, int screen,
166                         GLXDrawable drawable,
167                         struct apple_glx_drawable ** resultptr)
168{
169   struct apple_glx_drawable *d;
170
171   if (apple_glx_drawable_create(dpy, screen, drawable, &d, &callbacks))
172      return true;
173
174   /* apple_glx_drawable_create creates a locked and referenced object. */
175
176   if (create_surface(dpy, screen, d)) {
177      d->unlock(d);
178      d->destroy(d);
179      return true;
180   }
181
182   *resultptr = d;
183
184   d->unlock(d);
185
186   return false;
187}
188
189/*
190 * All surfaces are reference counted, and surfaces are only created
191 * when the window is made current.  When all contexts no longer reference
192 * a surface drawable the apple_glx_drawable gets destroyed, and thus
193 * its surface is destroyed.
194 *
195 * However we can make the destruction occur a bit sooner by setting
196 * pending_destroy, which is then checked for in glViewport by
197 * apple_glx_context_update.
198 */
199void
200apple_glx_surface_destroy(unsigned int uid)
201{
202   struct apple_glx_drawable *d;
203
204   d = apple_glx_drawable_find_by_uid(uid, APPLE_GLX_DRAWABLE_REFERENCE
205                                      | APPLE_GLX_DRAWABLE_LOCK);
206
207   if (d) {
208      d->types.surface.pending_destroy = true;
209      d->release(d);
210      /*
211       * We release 2 references to the surface.  One was acquired by
212       * the find, and the other was leftover from a context, or
213       * the surface being displayed, so the destroy() will decrease it
214       * once more.
215       *
216       * If the surface is in a context, it will take one d->destroy(d);
217       * to actually destroy it when the pending_destroy is processed
218       * by a glViewport callback (see apple_glx_context_update()).
219       */
220      d->destroy(d);
221
222      d->unlock(d);
223   }
224}
225