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 apple_glapi_oglfw_viewport_scissor(0, 0, width
, height
);
60 surface_make_current(struct apple_glx_context
*ac
,
61 struct apple_glx_drawable
*d
)
63 struct apple_glx_surface
*s
= &d
->types
.surface
;
66 assert(APPLE_GLX_DRAWABLE_SURFACE
== d
->type
);
68 apple_glx_diagnostic("%s: ac->context_obj %p s->surface_id %u\n",
69 __func__
, (void *) ac
->context_obj
, s
->surface_id
);
71 error
= xp_attach_gl_context(ac
->context_obj
, s
->surface_id
);
74 fprintf(stderr
, "error: xp_attach_gl_context returned: %d\n", error
);
79 if (!ac
->made_current
) {
81 * The first time a new context is made current the glViewport
82 * and glScissor should be updated.
84 update_viewport_and_scissor(ac
->drawable
->display
,
85 ac
->drawable
->drawable
);
86 ac
->made_current
= true;
89 apple_glx_diagnostic("%s: drawable 0x%lx\n", __func__
, d
->drawable
);
95 surface_destroy(Display
* dpy
, struct apple_glx_drawable
*d
)
97 struct apple_glx_surface
*s
= &d
->types
.surface
;
99 apple_glx_diagnostic("%s: s->surface_id %u\n", __func__
, s
->surface_id
);
101 xp_error error
= xp_destroy_surface(s
->surface_id
);
104 fprintf(stderr
, "xp_destroy_surface error: %d\n", (int) error
);
108 * Check if this surface destroy came from the surface being destroyed
109 * on the server. If s->pending_destroy is true, then it did, and
110 * we don't want to try to destroy the surface on the server.
112 if (!s
->pending_destroy
) {
114 * Warning: this causes other routines to be called (potentially)
115 * from surface_notify_handler. It's probably best to not have
116 * any locks at this point locked.
118 XAppleDRIDestroySurface(d
->display
, DefaultScreen(d
->display
),
122 ("%s: destroyed a surface for drawable 0x%lx uid %u\n", __func__
,
123 d
->drawable
, s
->uid
);
127 /* Return true if an error occured. */
129 create_surface(Display
* dpy
, int screen
, struct apple_glx_drawable
*d
)
131 struct apple_glx_surface
*s
= &d
->types
.surface
;
135 id
= apple_glx_get_client_id();
139 assert(None
!= d
->drawable
);
141 s
->pending_destroy
= false;
143 if (XAppleDRICreateSurface(dpy
, screen
, d
->drawable
, id
, key
, &s
->uid
)) {
146 error
= xp_import_surface(key
, &s
->surface_id
);
149 fprintf(stderr
, "error: xp_import_surface returned: %d\n", error
);
153 apple_glx_diagnostic("%s: created a surface for drawable 0x%lx"
154 " with uid %u\n", __func__
, d
->drawable
, s
->uid
);
155 return false; /*success */
158 return true; /* unable to create a surface. */
161 /* Return true if an error occured. */
162 /* This returns a referenced object via resultptr. */
164 apple_glx_surface_create(Display
* dpy
, int screen
,
165 GLXDrawable drawable
,
166 struct apple_glx_drawable
** resultptr
)
168 struct apple_glx_drawable
*d
;
170 if (apple_glx_drawable_create(dpy
, screen
, drawable
, &d
, &callbacks
))
173 /* apple_glx_drawable_create creates a locked and referenced object. */
175 if (create_surface(dpy
, screen
, d
)) {
189 * All surfaces are reference counted, and surfaces are only created
190 * when the window is made current. When all contexts no longer reference
191 * a surface drawable the apple_glx_drawable gets destroyed, and thus
192 * its surface is destroyed.
194 * However we can make the destruction occur a bit sooner by setting
195 * pending_destroy, which is then checked for in glViewport by
196 * apple_glx_context_update.
199 apple_glx_surface_destroy(unsigned int uid
)
201 struct apple_glx_drawable
*d
;
203 d
= apple_glx_drawable_find_by_uid(uid
, APPLE_GLX_DRAWABLE_REFERENCE
204 | APPLE_GLX_DRAWABLE_LOCK
);
207 d
->types
.surface
.pending_destroy
= true;
210 * We release 2 references to the surface. One was acquired by
211 * the find, and the other was leftover from a context, or
212 * the surface being displayed, so the destroy() will decrease it
215 * If the surface is in a context, it will take one d->destroy(d);
216 * to actually destroy it when the pending_destroy is processed
217 * by a glViewport callback (see apple_glx_context_update()).