summaryrefslogtreecommitdiffstats
path: root/src/glx/apple/apple_glx_surface.c
blob: 6db2910a464de7137e6ead62b620a482567f1c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 Copyright (c) 2009 Apple Inc.
 
 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation files
 (the "Software"), to deal in the Software without restriction,
 including without limitation the rights to use, copy, modify, merge,
 publish, distribute, sublicense, and/or sell copies of the Software,
 and to permit persons to whom the Software is furnished to do so,
 subject to the following conditions:
 
 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
 HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 DEALINGS IN THE SOFTWARE.
 
 Except as contained in this notice, the name(s) of the above
 copyright holders shall not be used in advertising or otherwise to
 promote the sale, use or other dealings in this Software without
 prior written authorization.
*/
#include <assert.h>
#include "glxclient.h"
#include "apple_glx.h"
#include "appledri.h"
#include "apple_glx_drawable.h"

static bool surface_make_current(struct apple_glx_context *ac,
                                 struct apple_glx_drawable *d);

static void surface_destroy(Display * dpy, struct apple_glx_drawable *d);


static struct apple_glx_drawable_callbacks callbacks = {
   .type = APPLE_GLX_DRAWABLE_SURFACE,
   .make_current = surface_make_current,
   .destroy = surface_destroy
};

static void
update_viewport_and_scissor(Display * dpy, GLXDrawable drawable)
{
   Window root;
   int x, y;
   unsigned int width = 0, height = 0, bd, depth;

   XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &bd, &depth);

   glViewport(0, 0, width, height);
   glScissor(0, 0, width, height);
}

static bool
surface_make_current(struct apple_glx_context *ac,
                     struct apple_glx_drawable *d)
{
   struct apple_glx_surface *s = &d->types.surface;
   xp_error error;

   assert(APPLE_GLX_DRAWABLE_SURFACE == d->type);

   apple_glx_diagnostic("%s: ac->context_obj %p s->surface_id %u\n",
                        __func__, (void *) ac->context_obj, s->surface_id);

   error = xp_attach_gl_context(ac->context_obj, s->surface_id);

   if (error) {
      fprintf(stderr, "error: xp_attach_gl_context returned: %d\n", error);
      return true;
   }


   if (!ac->made_current) {
      /* 
       * The first time a new context is made current the glViewport
       * and glScissor should be updated.
       */
      update_viewport_and_scissor(ac->drawable->display,
                                  ac->drawable->drawable);
      ac->made_current = true;
   }

   apple_glx_diagnostic("%s: drawable 0x%lx\n", __func__, d->drawable);

   return false;
}

static void
surface_destroy(Display * dpy, struct apple_glx_drawable *d)
{
   struct apple_glx_surface *s = &d->types.surface;

   apple_glx_diagnostic("%s: s->surface_id %u\n", __func__, s->surface_id);

   xp_error error = xp_destroy_surface(s->surface_id);

   if (error) {
      fprintf(stderr, "xp_destroy_surface error: %d\n", (int) error);
   }

   /* 
    * Check if this surface destroy came from the surface being destroyed
    * on the server.  If s->pending_destroy is true, then it did, and 
    * we don't want to try to destroy the surface on the server.
    */
   if (!s->pending_destroy) {
      /*
       * Warning: this causes other routines to be called (potentially)
       * from surface_notify_handler.  It's probably best to not have
       * any locks at this point locked.
       */
      XAppleDRIDestroySurface(d->display, DefaultScreen(d->display),
                              d->drawable);

      apple_glx_diagnostic
         ("%s: destroyed a surface for drawable 0x%lx uid %u\n", __func__,
          d->drawable, s->uid);
   }
}

/* Return true if an error occured. */
static bool
create_surface(Display * dpy, int screen, struct apple_glx_drawable *d)
{
   struct apple_glx_surface *s = &d->types.surface;
   unsigned int key[2];
   xp_client_id id;

   id = apple_glx_get_client_id();
   if (0 == id)
      return true;

   assert(None != d->drawable);

   s->pending_destroy = false;

   if (XAppleDRICreateSurface(dpy, screen, d->drawable, id, key, &s->uid)) {
      xp_error error;

      error = xp_import_surface(key, &s->surface_id);

      if (error) {
         fprintf(stderr, "error: xp_import_surface returned: %d\n", error);
         return true;
      }

      apple_glx_diagnostic("%s: created a surface for drawable 0x%lx"
                           " with uid %u\n", __func__, d->drawable, s->uid);
      return false;             /*success */
   }

   return true;                 /* unable to create a surface. */
}

/* Return true if an error occured. */
/* This returns a referenced object via resultptr. */
bool
apple_glx_surface_create(Display * dpy, int screen,
                         GLXDrawable drawable,
                         struct apple_glx_drawable ** resultptr)
{
   struct apple_glx_drawable *d;

   if (apple_glx_drawable_create(dpy, screen, drawable, &d, &callbacks))
      return true;

   /* apple_glx_drawable_create creates a locked and referenced object. */

   if (create_surface(dpy, screen, d)) {
      d->unlock(d);
      d->destroy(d);
      return true;
   }

   *resultptr = d;

   d->unlock(d);

   return false;
}

/*
 * All surfaces are reference counted, and surfaces are only created
 * when the window is made current.  When all contexts no longer reference
 * a surface drawable the apple_glx_drawable gets destroyed, and thus
 * its surface is destroyed.  
 *
 * However we can make the destruction occur a bit sooner by setting
 * pending_destroy, which is then checked for in glViewport by
 * apple_glx_context_update.
 */
void
apple_glx_surface_destroy(unsigned int uid)
{
   struct apple_glx_drawable *d;

   d = apple_glx_drawable_find_by_uid(uid, APPLE_GLX_DRAWABLE_REFERENCE
                                      | APPLE_GLX_DRAWABLE_LOCK);

   if (d) {
      d->types.surface.pending_destroy = true;
      d->release(d);
      /* 
       * We release 2 references to the surface.  One was acquired by
       * the find, and the other was leftover from a context, or 
       * the surface being displayed, so the destroy() will decrease it
       * once more.
       *
       * If the surface is in a context, it will take one d->destroy(d);
       * to actually destroy it when the pending_destroy is processed
       * by a glViewport callback (see apple_glx_context_update()).
       */
      d->destroy(d);

      d->unlock(d);
   }
}