summaryrefslogtreecommitdiff
path: root/src/glx/apple/apple_glx_pixmap.c
blob: fec05e02de6bb35cb2b81997d168397dad39bb63 (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
225
226
227
228
229
230
/*
 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 <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <assert.h>
#include "apple_glx.h"
#include "apple_cgl.h"
#include "apple_visual.h"
#include "apple_glx_drawable.h"
#include "appledri.h"
#include "glcontextmodes.h"

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

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

static struct apple_glx_drawable_callbacks callbacks = {
   .type = APPLE_GLX_DRAWABLE_PIXMAP,
   .make_current = pixmap_make_current,
   .destroy = pixmap_destroy
};

static bool
pixmap_make_current(struct apple_glx_context *ac,
                    struct apple_glx_drawable *d)
{
   CGLError cglerr;
   struct apple_glx_pixmap *p = &d->types.pixmap;

   assert(APPLE_GLX_DRAWABLE_PIXMAP == d->type);

   cglerr = apple_cgl.set_current_context(p->context_obj);

   if (kCGLNoError != cglerr) {
      fprintf(stderr, "set current context: %s\n",
              apple_cgl.error_string(cglerr));
      return true;
   }

   cglerr = apple_cgl.set_off_screen(p->context_obj, p->width, p->height,
                                     p->pitch, p->buffer);

   if (kCGLNoError != cglerr) {
      fprintf(stderr, "set off screen: %s\n", apple_cgl.error_string(cglerr));

      return true;
   }

   if (!ac->made_current) {
      glViewport(0, 0, p->width, p->height);
      glScissor(0, 0, p->width, p->height);
      ac->made_current = true;
   }

   return false;
}

static void
pixmap_destroy(Display * dpy, struct apple_glx_drawable *d)
{
   struct apple_glx_pixmap *p = &d->types.pixmap;

   if (p->pixel_format_obj)
      (void) apple_cgl.destroy_pixel_format(p->pixel_format_obj);

   if (p->context_obj)
      (void) apple_cgl.destroy_context(p->context_obj);

   XAppleDRIDestroyPixmap(dpy, p->xpixmap);

   if (p->buffer) {
      if (munmap(p->buffer, p->size))
         perror("munmap");

      if (-1 == close(p->fd))
         perror("close");

      if (shm_unlink(p->path))
         perror("shm_unlink");
   }

   apple_glx_diagnostic("destroyed pixmap buffer for: 0x%lx\n", d->drawable);
}

/* Return true if an error occurred. */
bool
apple_glx_pixmap_create(Display * dpy, int screen, Pixmap pixmap,
                        const void *mode)
{
   struct apple_glx_drawable *d;
   struct apple_glx_pixmap *p;
   bool double_buffered;
   bool uses_stereo;
   CGLError error;
   const struct glx_config *cmodes = mode;

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

   /* d is locked and referenced at this point. */

   p = &d->types.pixmap;

   p->xpixmap = pixmap;
   p->buffer = NULL;

   if (!XAppleDRICreatePixmap(dpy, screen, pixmap,
                              &p->width, &p->height, &p->pitch, &p->bpp,
                              &p->size, p->path, PATH_MAX)) {
      d->unlock(d);
      d->destroy(d);
      return true;
   }

   p->fd = shm_open(p->path, O_RDWR, 0);

   if (p->fd < 0) {
      perror("shm_open");
      d->unlock(d);
      d->destroy(d);
      return true;
   }

   p->buffer = mmap(NULL, p->size, PROT_READ | PROT_WRITE,
                    MAP_FILE | MAP_SHARED, p->fd, 0);

   if (MAP_FAILED == p->buffer) {
      perror("mmap");
      d->unlock(d);
      d->destroy(d);
      return true;
   }

   apple_visual_create_pfobj(&p->pixel_format_obj, mode, &double_buffered,
                             &uses_stereo, /*offscreen */ true);

   error = apple_cgl.create_context(p->pixel_format_obj, NULL,
                                    &p->context_obj);

   if (kCGLNoError != error) {
      d->unlock(d);
      d->destroy(d);
      return true;
   }

   p->fbconfigID = cmodes->fbconfigID;

   d->unlock(d);

   apple_glx_diagnostic("created: pixmap buffer for 0x%lx\n", d->drawable);

   return false;
}

bool
apple_glx_pixmap_query(GLXPixmap pixmap, int attr, unsigned int *value)
{
   struct apple_glx_drawable *d;
   struct apple_glx_pixmap *p;
   bool result = false;

   d = apple_glx_drawable_find_by_type(pixmap, APPLE_GLX_DRAWABLE_PIXMAP,
                                       APPLE_GLX_DRAWABLE_LOCK);

   if (d) {
      p = &d->types.pixmap;

      switch (attr) {
      case GLX_WIDTH:
         *value = p->width;
         result = true;
         break;

      case GLX_HEIGHT:
         *value = p->height;
         result = true;
         break;

      case GLX_FBCONFIG_ID:
         *value = p->fbconfigID;
         result = true;
         break;
      }

      d->unlock(d);
   }

   return result;
}

/* Return true if the type is valid for pixmap. */
bool
apple_glx_pixmap_destroy(Display * dpy, GLXPixmap pixmap)
{
   return !apple_glx_drawable_destroy_by_type(dpy, pixmap,
                                              APPLE_GLX_DRAWABLE_PIXMAP);
}