2 Copyright (c) 2009 Apple Inc.
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:
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
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.
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.
30 #include "glxclient.h"
31 #include "apple_glx.h"
33 #include "apple_glx_drawable.h"
35 static bool surface_make_current(struct apple_glx_context
*ac
,
36 struct apple_glx_drawable
*d
);
38 static void surface_destroy(Display
* dpy
, struct apple_glx_drawable
*d
);
41 static struct apple_glx_drawable_callbacks callbacks
= {
42 .type
= APPLE_GLX_DRAWABLE_SURFACE
,
43 .make_current
= surface_make_current
,
44 .destroy
= surface_destroy
48 update_viewport_and_scissor(Display
* dpy
, GLXDrawable drawable
)
52 unsigned int width
= 0, height
= 0, bd
, depth
;
54 XGetGeometry(dpy
, drawable
, &root
, &x
, &y
, &width
, &height
, &bd
, &depth
);
56 glViewport(0, 0, width
, height
);
57 glScissor(0, 0, width
, height
);
61 surface_make_current(struct apple_glx_context
*ac
,
62 struct apple_glx_drawable
*d
)
64 struct apple_glx_surface
*s
= &d
->types
.surface
;
67 assert(APPLE_GLX_DRAWABLE_SURFACE
== d
->type
);
69 apple_glx_diagnostic("%s: ac->context_obj %p s->surface_id %u\n",
70 __func__
, (void *) ac
->context_obj
, s
->surface_id
);
72 error
= xp_attach_gl_context(ac
->context_obj
, s
->surface_id
);
75 fprintf(stderr
, "error: xp_attach_gl_context returned: %d\n", error
);
80 if (!ac
->made_current
) {
82 * The first time a new context is made current the glViewport
83 * and glScissor should be updated.
85 update_viewport_and_scissor(ac
->drawable
->display
,
86 ac
->drawable
->drawable
);
87 ac
->made_current
= true;
90 apple_glx_diagnostic("%s: drawable 0x%lx\n", __func__
, d
->drawable
);
96 surface_destroy(Display
* dpy
, struct apple_glx_drawable
*d
)
98 struct apple_glx_surface
*s
= &d
->types
.surface
;
100 apple_glx_diagnostic("%s: s->surface_id %u\n", __func__
, s
->surface_id
);
102 xp_error error
= xp_destroy_surface(s
->surface_id
);
105 fprintf(stderr
, "xp_destroy_surface error: %d\n", (int) error
);
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.
113 if (!s
->pending_destroy
) {
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.
119 XAppleDRIDestroySurface(d
->display
, DefaultScreen(d
->display
),
123 ("%s: destroyed a surface for drawable 0x%lx uid %u\n", __func__
,
124 d
->drawable
, s
->uid
);
128 /* Return true if an error occured. */
130 create_surface(Display
* dpy
, int screen
, struct apple_glx_drawable
*d
)
132 struct apple_glx_surface
*s
= &d
->types
.surface
;
136 id
= apple_glx_get_client_id();
140 assert(None
!= d
->drawable
);
142 s
->pending_destroy
= false;
144 if (XAppleDRICreateSurface(dpy
, screen
, d
->drawable
, id
, key
, &s
->uid
)) {
147 error
= xp_import_surface(key
, &s
->surface_id
);
150 fprintf(stderr
, "error: xp_import_surface returned: %d\n", error
);
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 */
159 return true; /* unable to create a surface. */
162 /* Return true if an error occured. */
163 /* This returns a referenced object via resultptr. */
165 apple_glx_surface_create(Display
* dpy
, int screen
,
166 GLXDrawable drawable
,
167 struct apple_glx_drawable
** resultptr
)
169 struct apple_glx_drawable
*d
;
171 if (apple_glx_drawable_create(dpy
, screen
, drawable
, &d
, &callbacks
))
174 /* apple_glx_drawable_create creates a locked and referenced object. */
176 if (create_surface(dpy
, screen
, d
)) {
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.
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.
200 apple_glx_surface_destroy(unsigned int uid
)
202 struct apple_glx_drawable
*d
;
204 d
= apple_glx_drawable_find_by_uid(uid
, APPLE_GLX_DRAWABLE_REFERENCE
205 | APPLE_GLX_DRAWABLE_LOCK
);
208 d
->types
.surface
.pending_destroy
= true;
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
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()).