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.
34 #include <sys/types.h>
38 #include "apple_glx.h"
39 #include "apple_cgl.h"
40 #include "apple_visual.h"
41 #include "apple_glx_drawable.h"
43 #include "glxconfig.h"
45 static bool pixmap_make_current(struct apple_glx_context
*ac
,
46 struct apple_glx_drawable
*d
);
48 static void pixmap_destroy(Display
* dpy
, struct apple_glx_drawable
*d
);
50 static struct apple_glx_drawable_callbacks callbacks
= {
51 .type
= APPLE_GLX_DRAWABLE_PIXMAP
,
52 .make_current
= pixmap_make_current
,
53 .destroy
= pixmap_destroy
57 pixmap_make_current(struct apple_glx_context
*ac
,
58 struct apple_glx_drawable
*d
)
61 struct apple_glx_pixmap
*p
= &d
->types
.pixmap
;
63 assert(APPLE_GLX_DRAWABLE_PIXMAP
== d
->type
);
65 cglerr
= apple_cgl
.set_current_context(p
->context_obj
);
67 if (kCGLNoError
!= cglerr
) {
68 fprintf(stderr
, "set current context: %s\n",
69 apple_cgl
.error_string(cglerr
));
73 cglerr
= apple_cgl
.set_off_screen(p
->context_obj
, p
->width
, p
->height
,
76 if (kCGLNoError
!= cglerr
) {
77 fprintf(stderr
, "set off screen: %s\n", apple_cgl
.error_string(cglerr
));
82 if (!ac
->made_current
) {
83 apple_glapi_oglfw_viewport_scissor(0, 0, p
->width
, p
->height
);
84 ac
->made_current
= true;
91 pixmap_destroy(Display
* dpy
, struct apple_glx_drawable
*d
)
93 struct apple_glx_pixmap
*p
= &d
->types
.pixmap
;
95 if (p
->pixel_format_obj
)
96 (void) apple_cgl
.destroy_pixel_format(p
->pixel_format_obj
);
99 (void) apple_cgl
.destroy_context(p
->context_obj
);
101 XAppleDRIDestroyPixmap(dpy
, p
->xpixmap
);
104 if (munmap(p
->buffer
, p
->size
))
107 if (-1 == close(p
->fd
))
110 if (shm_unlink(p
->path
))
111 perror("shm_unlink");
114 apple_glx_diagnostic("destroyed pixmap buffer for: 0x%lx\n", d
->drawable
);
117 /* Return true if an error occurred. */
119 apple_glx_pixmap_create(Display
* dpy
, int screen
, Pixmap pixmap
,
122 struct apple_glx_drawable
*d
;
123 struct apple_glx_pixmap
*p
;
124 bool double_buffered
;
127 const struct glx_config
*cmodes
= mode
;
129 if (apple_glx_drawable_create(dpy
, screen
, pixmap
, &d
, &callbacks
))
132 /* d is locked and referenced at this point. */
134 p
= &d
->types
.pixmap
;
139 if (!XAppleDRICreatePixmap(dpy
, screen
, pixmap
,
140 &p
->width
, &p
->height
, &p
->pitch
, &p
->bpp
,
141 &p
->size
, p
->path
, PATH_MAX
)) {
147 p
->fd
= shm_open(p
->path
, O_RDWR
, 0);
156 p
->buffer
= mmap(NULL
, p
->size
, PROT_READ
| PROT_WRITE
,
157 MAP_FILE
| MAP_SHARED
, p
->fd
, 0);
159 if (MAP_FAILED
== p
->buffer
) {
166 apple_visual_create_pfobj(&p
->pixel_format_obj
, mode
, &double_buffered
,
167 &uses_stereo
, /*offscreen */ true);
169 error
= apple_cgl
.create_context(p
->pixel_format_obj
, NULL
,
172 if (kCGLNoError
!= error
) {
178 p
->fbconfigID
= cmodes
->fbconfigID
;
182 apple_glx_diagnostic("created: pixmap buffer for 0x%lx\n", d
->drawable
);
188 apple_glx_pixmap_query(GLXPixmap pixmap
, int attr
, unsigned int *value
)
190 struct apple_glx_drawable
*d
;
191 struct apple_glx_pixmap
*p
;
194 d
= apple_glx_drawable_find_by_type(pixmap
, APPLE_GLX_DRAWABLE_PIXMAP
,
195 APPLE_GLX_DRAWABLE_LOCK
);
198 p
= &d
->types
.pixmap
;
211 case GLX_FBCONFIG_ID
:
212 *value
= p
->fbconfigID
;
223 /* Return true if the type is valid for pixmap. */
225 apple_glx_pixmap_destroy(Display
* dpy
, GLXPixmap pixmap
)
227 return !apple_glx_drawable_destroy_by_type(dpy
, pixmap
,
228 APPLE_GLX_DRAWABLE_PIXMAP
);