Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / glx / apple / apple_glx_pixmap.c
1 /*
2  Copyright (c) 2009 Apple Inc.
3  
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:
11  
12  The above copyright notice and this permission notice shall be
13  included in all copies or substantial portions of the Software.
14  
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.
23  
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.
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <pthread.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/mman.h>
36 #include <unistd.h>
37 #include <assert.h>
38 #include "apple_glx.h"
39 #include "apple_cgl.h"
40 #include "apple_visual.h"
41 #include "apple_glx_drawable.h"
42 #include "appledri.h"
43 #include "glxconfig.h"
44
45 static bool pixmap_make_current(struct apple_glx_context *ac,
46                                 struct apple_glx_drawable *d);
47
48 static void pixmap_destroy(Display * dpy, struct apple_glx_drawable *d);
49
50 static struct apple_glx_drawable_callbacks callbacks = {
51    .type = APPLE_GLX_DRAWABLE_PIXMAP,
52    .make_current = pixmap_make_current,
53    .destroy = pixmap_destroy
54 };
55
56 static bool
57 pixmap_make_current(struct apple_glx_context *ac,
58                     struct apple_glx_drawable *d)
59 {
60    CGLError cglerr;
61    struct apple_glx_pixmap *p = &d->types.pixmap;
62
63    assert(APPLE_GLX_DRAWABLE_PIXMAP == d->type);
64
65    cglerr = apple_cgl.set_current_context(p->context_obj);
66
67    if (kCGLNoError != cglerr) {
68       fprintf(stderr, "set current context: %s\n",
69               apple_cgl.error_string(cglerr));
70       return true;
71    }
72
73    cglerr = apple_cgl.set_off_screen(p->context_obj, p->width, p->height,
74                                      p->pitch, p->buffer);
75
76    if (kCGLNoError != cglerr) {
77       fprintf(stderr, "set off screen: %s\n", apple_cgl.error_string(cglerr));
78
79       return true;
80    }
81
82    if (!ac->made_current) {
83       apple_glapi_oglfw_viewport_scissor(0, 0, p->width, p->height);
84       ac->made_current = true;
85    }
86
87    return false;
88 }
89
90 static void
91 pixmap_destroy(Display * dpy, struct apple_glx_drawable *d)
92 {
93    struct apple_glx_pixmap *p = &d->types.pixmap;
94
95    if (p->pixel_format_obj)
96       (void) apple_cgl.destroy_pixel_format(p->pixel_format_obj);
97
98    if (p->context_obj)
99       (void) apple_cgl.destroy_context(p->context_obj);
100
101    XAppleDRIDestroyPixmap(dpy, p->xpixmap);
102
103    if (p->buffer) {
104       if (munmap(p->buffer, p->size))
105          perror("munmap");
106
107       if (-1 == close(p->fd))
108          perror("close");
109
110       if (shm_unlink(p->path))
111          perror("shm_unlink");
112    }
113
114    apple_glx_diagnostic("destroyed pixmap buffer for: 0x%lx\n", d->drawable);
115 }
116
117 /* Return true if an error occurred. */
118 bool
119 apple_glx_pixmap_create(Display * dpy, int screen, Pixmap pixmap,
120                         const void *mode)
121 {
122    struct apple_glx_drawable *d;
123    struct apple_glx_pixmap *p;
124    bool double_buffered;
125    bool uses_stereo;
126    CGLError error;
127    const struct glx_config *cmodes = mode;
128
129    if (apple_glx_drawable_create(dpy, screen, pixmap, &d, &callbacks))
130       return true;
131
132    /* d is locked and referenced at this point. */
133
134    p = &d->types.pixmap;
135
136    p->xpixmap = pixmap;
137    p->buffer = NULL;
138
139    if (!XAppleDRICreatePixmap(dpy, screen, pixmap,
140                               &p->width, &p->height, &p->pitch, &p->bpp,
141                               &p->size, p->path, PATH_MAX)) {
142       d->unlock(d);
143       d->destroy(d);
144       return true;
145    }
146
147    p->fd = shm_open(p->path, O_RDWR, 0);
148
149    if (p->fd < 0) {
150       perror("shm_open");
151       d->unlock(d);
152       d->destroy(d);
153       return true;
154    }
155
156    p->buffer = mmap(NULL, p->size, PROT_READ | PROT_WRITE,
157                     MAP_FILE | MAP_SHARED, p->fd, 0);
158
159    if (MAP_FAILED == p->buffer) {
160       perror("mmap");
161       d->unlock(d);
162       d->destroy(d);
163       return true;
164    }
165
166    apple_visual_create_pfobj(&p->pixel_format_obj, mode, &double_buffered,
167                              &uses_stereo, /*offscreen */ true);
168
169    error = apple_cgl.create_context(p->pixel_format_obj, NULL,
170                                     &p->context_obj);
171
172    if (kCGLNoError != error) {
173       d->unlock(d);
174       d->destroy(d);
175       return true;
176    }
177
178    p->fbconfigID = cmodes->fbconfigID;
179
180    d->unlock(d);
181
182    apple_glx_diagnostic("created: pixmap buffer for 0x%lx\n", d->drawable);
183
184    return false;
185 }
186
187 bool
188 apple_glx_pixmap_query(GLXPixmap pixmap, int attr, unsigned int *value)
189 {
190    struct apple_glx_drawable *d;
191    struct apple_glx_pixmap *p;
192    bool result = false;
193
194    d = apple_glx_drawable_find_by_type(pixmap, APPLE_GLX_DRAWABLE_PIXMAP,
195                                        APPLE_GLX_DRAWABLE_LOCK);
196
197    if (d) {
198       p = &d->types.pixmap;
199
200       switch (attr) {
201       case GLX_WIDTH:
202          *value = p->width;
203          result = true;
204          break;
205
206       case GLX_HEIGHT:
207          *value = p->height;
208          result = true;
209          break;
210
211       case GLX_FBCONFIG_ID:
212          *value = p->fbconfigID;
213          result = true;
214          break;
215       }
216
217       d->unlock(d);
218    }
219
220    return result;
221 }
222
223 /* Return true if the type is valid for pixmap. */
224 bool
225 apple_glx_pixmap_destroy(Display * dpy, GLXPixmap pixmap)
226 {
227    return !apple_glx_drawable_destroy_by_type(dpy, pixmap,
228                                               APPLE_GLX_DRAWABLE_PIXMAP);
229 }