Path: blob/21.2-virgl/src/glx/apple/apple_glx_surface.c
4560 views
/*1Copyright (c) 2009 Apple Inc.23Permission is hereby granted, free of charge, to any person4obtaining a copy of this software and associated documentation files5(the "Software"), to deal in the Software without restriction,6including without limitation the rights to use, copy, modify, merge,7publish, distribute, sublicense, and/or sell copies of the Software,8and to permit persons to whom the Software is furnished to do so,9subject to the following conditions:1011The above copyright notice and this permission notice shall be12included in all copies or substantial portions of the Software.1314THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT18HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,19WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21DEALINGS IN THE SOFTWARE.2223Except as contained in this notice, the name(s) of the above24copyright holders shall not be used in advertising or otherwise to25promote the sale, use or other dealings in this Software without26prior written authorization.27*/28#include <assert.h>29#include "glxclient.h"30#include "apple_glx.h"31#include "appledri.h"32#include "apple_glx_drawable.h"3334static bool surface_make_current(struct apple_glx_context *ac,35struct apple_glx_drawable *d);3637static void surface_destroy(Display * dpy, struct apple_glx_drawable *d);383940static struct apple_glx_drawable_callbacks callbacks = {41.type = APPLE_GLX_DRAWABLE_SURFACE,42.make_current = surface_make_current,43.destroy = surface_destroy44};4546static void47update_viewport_and_scissor(Display * dpy, GLXDrawable drawable)48{49Window root;50int x, y;51unsigned int width = 0, height = 0, bd, depth;5253XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &bd, &depth);5455apple_glapi_oglfw_viewport_scissor(0, 0, width, height);56}5758static bool59surface_make_current(struct apple_glx_context *ac,60struct apple_glx_drawable *d)61{62struct apple_glx_surface *s = &d->types.surface;63xp_error error;6465assert(APPLE_GLX_DRAWABLE_SURFACE == d->type);6667apple_glx_diagnostic("%s: ac->context_obj %p s->surface_id %u\n",68__func__, (void *) ac->context_obj, s->surface_id);6970error = xp_attach_gl_context(ac->context_obj, s->surface_id);7172if (error) {73fprintf(stderr, "error: xp_attach_gl_context returned: %d\n", error);74return true;75}767778if (!ac->made_current) {79/*80* The first time a new context is made current the glViewport81* and glScissor should be updated.82*/83update_viewport_and_scissor(ac->drawable->display,84ac->drawable->drawable);85ac->made_current = true;86}8788apple_glx_diagnostic("%s: drawable 0x%lx\n", __func__, d->drawable);8990return false;91}9293static void94surface_destroy(Display * dpy, struct apple_glx_drawable *d)95{96struct apple_glx_surface *s = &d->types.surface;9798apple_glx_diagnostic("%s: s->surface_id %u\n", __func__, s->surface_id);99100xp_error error = xp_destroy_surface(s->surface_id);101102if (error) {103fprintf(stderr, "xp_destroy_surface error: %d\n", (int) error);104}105106/*107* Check if this surface destroy came from the surface being destroyed108* on the server. If s->pending_destroy is true, then it did, and109* we don't want to try to destroy the surface on the server.110*/111if (!s->pending_destroy) {112/*113* Warning: this causes other routines to be called (potentially)114* from surface_notify_handler. It's probably best to not have115* any locks at this point locked.116*/117XAppleDRIDestroySurface(d->display, DefaultScreen(d->display),118d->drawable);119120apple_glx_diagnostic121("%s: destroyed a surface for drawable 0x%lx uid %u\n", __func__,122d->drawable, s->uid);123}124}125126/* Return true if an error occurred. */127static bool128create_surface(Display * dpy, int screen, struct apple_glx_drawable *d)129{130struct apple_glx_surface *s = &d->types.surface;131unsigned int key[2];132xp_client_id id;133134id = apple_glx_get_client_id();135if (0 == id)136return true;137138assert(None != d->drawable);139140s->pending_destroy = false;141142if (XAppleDRICreateSurface(dpy, screen, d->drawable, id, key, &s->uid)) {143xp_error error;144145error = xp_import_surface(key, &s->surface_id);146147if (error) {148fprintf(stderr, "error: xp_import_surface returned: %d\n", error);149return true;150}151152apple_glx_diagnostic("%s: created a surface for drawable 0x%lx"153" with uid %u\n", __func__, d->drawable, s->uid);154return false; /*success */155}156157return true; /* unable to create a surface. */158}159160/* Return true if an error occurred. */161/* This returns a referenced object via resultptr. */162bool163apple_glx_surface_create(Display * dpy, int screen,164GLXDrawable drawable,165struct apple_glx_drawable ** resultptr)166{167struct apple_glx_drawable *d;168169if (apple_glx_drawable_create(dpy, screen, drawable, &d, &callbacks))170return true;171172/* apple_glx_drawable_create creates a locked and referenced object. */173174if (create_surface(dpy, screen, d)) {175d->unlock(d);176d->destroy(d);177return true;178}179180*resultptr = d;181182d->unlock(d);183184return false;185}186187/*188* All surfaces are reference counted, and surfaces are only created189* when the window is made current. When all contexts no longer reference190* a surface drawable the apple_glx_drawable gets destroyed, and thus191* its surface is destroyed.192*193* However we can make the destruction occur a bit sooner by setting194* pending_destroy, which is then checked for in glViewport by195* apple_glx_context_update.196*/197void198apple_glx_surface_destroy(unsigned int uid)199{200struct apple_glx_drawable *d;201202d = apple_glx_drawable_find_by_uid(uid, APPLE_GLX_DRAWABLE_REFERENCE203| APPLE_GLX_DRAWABLE_LOCK);204205if (d) {206d->types.surface.pending_destroy = true;207d->release(d);208209/*210* We release 2 references to the surface. One was acquired by211* the find, and the other was leftover from a context, or212* the surface being displayed, so the destroy() will decrease it213* once more.214*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 processed217* by a glViewport callback (see apple_glx_context_update()).218*/219if (!d->destroy(d)) {220/* apple_glx_drawable_find_by_uid returns a locked drawable */221d->unlock(d);222}223}224}225226227