1 /*
2  Copyright (c) 2008, 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 #ifndef APPLE_GLX_DRAWABLE_H
30 #define APPLE_GLX_DRAWABLE_H
31 
32 /* Must be first for:
33  * <rdar://problem/6953344>
34  */
35 #include "apple_glx_context.h"
36 
37 #include <pthread.h>
38 #include <stdbool.h>
39 #include <limits.h>
40 #include <GL/glx.h>
41 #define XP_NO_X_HEADERS
42 #include <Xplugin.h>
43 #undef XP_NO_X_HEADERS
44 
45 enum
46 {
47    APPLE_GLX_DRAWABLE_SURFACE = 1,
48    APPLE_GLX_DRAWABLE_PBUFFER,
49    APPLE_GLX_DRAWABLE_PIXMAP
50 };
51 
52 /* The flag for the find routine. */
53 enum
54 {
55    APPLE_GLX_DRAWABLE_LOCK = 2,
56    APPLE_GLX_DRAWABLE_REFERENCE = 4
57 };
58 
59 struct apple_glx_context;
60 struct apple_glx_drawable;
61 
62 struct apple_glx_surface
63 {
64    xp_surface_id surface_id;
65    unsigned int uid;
66    bool pending_destroy;
67 };
68 
69 struct apple_glx_pbuffer
70 {
71    GLXPbuffer xid;              /* our pixmap */
72    int width, height;
73    GLint fbconfigID;
74    CGLPBufferObj buffer_obj;
75    unsigned long event_mask;
76 };
77 
78 struct apple_glx_pixmap
79 {
80    GLXPixmap xpixmap;
81    void *buffer;
82    int width, height, pitch, /*bytes per pixel */ bpp;
83    size_t size;
84    char path[PATH_MAX];
85    int fd;
86    CGLPixelFormatObj pixel_format_obj;
87    CGLContextObj context_obj;
88    GLint fbconfigID;
89 };
90 
91 struct apple_glx_drawable_callbacks
92 {
93    int type;
94      bool(*make_current) (struct apple_glx_context * ac,
95                           struct apple_glx_drawable * d);
96    void (*destroy) (Display * dpy, struct apple_glx_drawable * d);
97 };
98 
99 struct apple_glx_drawable
100 {
101    Display *display;
102    int reference_count;
103    GLXDrawable drawable;
104    int type;                    /* APPLE_GLX_DRAWABLE_* */
105 
106    union
107    {
108       struct apple_glx_pixmap pixmap;
109       struct apple_glx_pbuffer pbuffer;
110       struct apple_glx_surface surface;
111    } types;
112 
113    struct apple_glx_drawable_callbacks callbacks;
114 
115    /*
116     * This mutex protects the reference count and any other drawable data.
117     * It's used to prevent an early release of a drawable.
118     */
119    pthread_mutex_t mutex;
120    void (*lock) (struct apple_glx_drawable * agd);
121    void (*unlock) (struct apple_glx_drawable * agd);
122 
123    void (*reference) (struct apple_glx_drawable * agd);
124    void (*release) (struct apple_glx_drawable * agd);
125 
126      bool(*destroy) (struct apple_glx_drawable * agd);
127 
128      bool(*is_pbuffer) (struct apple_glx_drawable * agd);
129 
130      bool(*is_pixmap) (struct apple_glx_drawable * agd);
131 
132 /*BEGIN These are used for the mixed mode drawing... */
133    int width, height;
134    int row_bytes;
135    char path[PATH_MAX];
136    int fd;                      /* The file descriptor for this drawable's shared memory. */
137    void *buffer;                /* The memory for the drawable.  Typically shared memory. */
138    size_t buffer_length;
139      /*END*/ struct apple_glx_drawable *previous, *next;
140 };
141 
142 struct apple_glx_context;
143 
144 /* May return NULL if not found */
145 struct apple_glx_drawable *apple_glx_find_drawable(Display * dpy,
146                                                    GLXDrawable drawable);
147 
148 /* Returns true on error and locks the agd result with a reference. */
149 bool apple_glx_drawable_create(Display * dpy,
150                                int screen,
151                                GLXDrawable drawable,
152                                struct apple_glx_drawable **agd,
153                                struct apple_glx_drawable_callbacks
154                                *callbacks);
155 
156 /* Returns true on error */
157 bool apple_glx_create_drawable(Display * dpy,
158                                struct apple_glx_context *ac,
159                                GLXDrawable drawable,
160                                struct apple_glx_drawable **agd);
161 
162 void apple_glx_garbage_collect_drawables(Display * dpy);
163 
164 /*
165  * This returns the total number of drawables.
166  * It's mostly intended for debugging and introspection.
167  */
168 unsigned int apple_glx_get_drawable_count(void);
169 
170 struct apple_glx_drawable *apple_glx_drawable_find_by_type(GLXDrawable
171                                                            drawable, int type,
172                                                            int flags);
173 
174 struct apple_glx_drawable *apple_glx_drawable_find(GLXDrawable drawable,
175                                                    int flags);
176 
177 
178 bool apple_glx_drawable_destroy_by_type(Display * dpy, GLXDrawable drawable,
179                                         int type);
180 
181 struct apple_glx_drawable *apple_glx_drawable_find_by_uid(unsigned int uid,
182                                                           int flags);
183 
184 /* Surfaces */
185 
186 bool apple_glx_surface_create(Display * dpy, int screen, GLXDrawable drawable,
187                               struct apple_glx_drawable **resultptr);
188 
189 void apple_glx_surface_destroy(unsigned int uid);
190 
191 /* Pbuffers */
192 
193 /* Returns true if an error occurred. */
194 bool apple_glx_pbuffer_create(Display * dpy, GLXFBConfig config,
195                               int width, int height, int *errorcode,
196                               GLXPbuffer * pbuf);
197 
198 /* Returns true if the pbuffer was invalid. */
199 bool apple_glx_pbuffer_destroy(Display * dpy, GLXPbuffer pbuf);
200 
201 /* Returns true if the pbuffer was valid and the attribute. */
202 bool apple_glx_pbuffer_query(GLXDrawable d, int attribute,
203                              unsigned int *value);
204 
205 /* Returns true if the GLXDrawable is a valid GLXPbuffer, and the mask is set. */
206 bool apple_glx_pbuffer_set_event_mask(GLXDrawable d, unsigned long mask);
207 
208 /* Returns true if the GLXDrawable is a valid GLXPbuffer, and the *mask is set. */
209 bool apple_glx_pbuffer_get_event_mask(GLXDrawable d, unsigned long *mask);
210 
211 
212 /* Pixmaps */
213 
214 /* mode is a struct glx_config * */
215 /* Returns true if an error occurred. */
216 bool apple_glx_pixmap_create(Display * dpy, int screen, Pixmap pixmap,
217                              const void *mode);
218 
219 /* Returns true if an error occurred. */
220 bool apple_glx_pixmap_destroy(Display * dpy, Pixmap pixmap);
221 
222 bool apple_glx_pixmap_query(GLXPixmap pixmap, int attribute,
223                             unsigned int *value);
224 
225 
226 
227 #endif
228