1848b8605Smrg/*
2848b8605Smrg Copyright (c) 2008, 2009 Apple Inc.
3848b8605Smrg
4848b8605Smrg Permission is hereby granted, free of charge, to any person
5848b8605Smrg obtaining a copy of this software and associated documentation files
6848b8605Smrg (the "Software"), to deal in the Software without restriction,
7848b8605Smrg including without limitation the rights to use, copy, modify, merge,
8848b8605Smrg publish, distribute, sublicense, and/or sell copies of the Software,
9848b8605Smrg and to permit persons to whom the Software is furnished to do so,
10848b8605Smrg subject to the following conditions:
11848b8605Smrg
12848b8605Smrg The above copyright notice and this permission notice shall be
13848b8605Smrg included in all copies or substantial portions of the Software.
14848b8605Smrg
15848b8605Smrg THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16848b8605Smrg EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17848b8605Smrg MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18848b8605Smrg NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19848b8605Smrg HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20848b8605Smrg WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21848b8605Smrg OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22848b8605Smrg DEALINGS IN THE SOFTWARE.
23848b8605Smrg
24848b8605Smrg Except as contained in this notice, the name(s) of the above
25848b8605Smrg copyright holders shall not be used in advertising or otherwise to
26848b8605Smrg promote the sale, use or other dealings in this Software without
27848b8605Smrg prior written authorization.
28848b8605Smrg*/
29848b8605Smrg#ifndef APPLE_GLX_DRAWABLE_H
30848b8605Smrg#define APPLE_GLX_DRAWABLE_H
31848b8605Smrg
32848b8605Smrg/* Must be first for:
33848b8605Smrg * <rdar://problem/6953344>
34848b8605Smrg */
35848b8605Smrg#include "apple_glx_context.h"
36848b8605Smrg
37848b8605Smrg#include <pthread.h>
38848b8605Smrg#include <stdbool.h>
39848b8605Smrg#include <limits.h>
40848b8605Smrg#include <GL/glx.h>
41848b8605Smrg#define XP_NO_X_HEADERS
42848b8605Smrg#include <Xplugin.h>
43848b8605Smrg#undef XP_NO_X_HEADERS
44848b8605Smrg
45848b8605Smrgenum
46848b8605Smrg{
47848b8605Smrg   APPLE_GLX_DRAWABLE_SURFACE = 1,
48848b8605Smrg   APPLE_GLX_DRAWABLE_PBUFFER,
49848b8605Smrg   APPLE_GLX_DRAWABLE_PIXMAP
50848b8605Smrg};
51848b8605Smrg
52848b8605Smrg/* The flag for the find routine. */
53848b8605Smrgenum
54848b8605Smrg{
55848b8605Smrg   APPLE_GLX_DRAWABLE_LOCK = 2,
56848b8605Smrg   APPLE_GLX_DRAWABLE_REFERENCE = 4
57848b8605Smrg};
58848b8605Smrg
59848b8605Smrgstruct apple_glx_context;
60848b8605Smrgstruct apple_glx_drawable;
61848b8605Smrg
62848b8605Smrgstruct apple_glx_surface
63848b8605Smrg{
64848b8605Smrg   xp_surface_id surface_id;
65848b8605Smrg   unsigned int uid;
66848b8605Smrg   bool pending_destroy;
67848b8605Smrg};
68848b8605Smrg
69848b8605Smrgstruct apple_glx_pbuffer
70848b8605Smrg{
71848b8605Smrg   GLXPbuffer xid;              /* our pixmap */
72848b8605Smrg   int width, height;
73848b8605Smrg   GLint fbconfigID;
74848b8605Smrg   CGLPBufferObj buffer_obj;
75848b8605Smrg   unsigned long event_mask;
76848b8605Smrg};
77848b8605Smrg
78848b8605Smrgstruct apple_glx_pixmap
79848b8605Smrg{
80848b8605Smrg   GLXPixmap xpixmap;
81848b8605Smrg   void *buffer;
82848b8605Smrg   int width, height, pitch, /*bytes per pixel */ bpp;
83848b8605Smrg   size_t size;
84848b8605Smrg   char path[PATH_MAX];
85848b8605Smrg   int fd;
86848b8605Smrg   CGLPixelFormatObj pixel_format_obj;
87848b8605Smrg   CGLContextObj context_obj;
88848b8605Smrg   GLint fbconfigID;
89848b8605Smrg};
90848b8605Smrg
91848b8605Smrgstruct apple_glx_drawable_callbacks
92848b8605Smrg{
93848b8605Smrg   int type;
94848b8605Smrg     bool(*make_current) (struct apple_glx_context * ac,
95848b8605Smrg                          struct apple_glx_drawable * d);
96848b8605Smrg   void (*destroy) (Display * dpy, struct apple_glx_drawable * d);
97848b8605Smrg};
98848b8605Smrg
99848b8605Smrgstruct apple_glx_drawable
100848b8605Smrg{
101848b8605Smrg   Display *display;
102848b8605Smrg   int reference_count;
103848b8605Smrg   GLXDrawable drawable;
104848b8605Smrg   int type;                    /* APPLE_GLX_DRAWABLE_* */
105848b8605Smrg
106848b8605Smrg   union
107848b8605Smrg   {
108848b8605Smrg      struct apple_glx_pixmap pixmap;
109848b8605Smrg      struct apple_glx_pbuffer pbuffer;
110848b8605Smrg      struct apple_glx_surface surface;
111848b8605Smrg   } types;
112848b8605Smrg
113848b8605Smrg   struct apple_glx_drawable_callbacks callbacks;
114848b8605Smrg
115848b8605Smrg   /*
116848b8605Smrg    * This mutex protects the reference count and any other drawable data.
117848b8605Smrg    * It's used to prevent an early release of a drawable.
118848b8605Smrg    */
119848b8605Smrg   pthread_mutex_t mutex;
120848b8605Smrg   void (*lock) (struct apple_glx_drawable * agd);
121848b8605Smrg   void (*unlock) (struct apple_glx_drawable * agd);
122848b8605Smrg
123848b8605Smrg   void (*reference) (struct apple_glx_drawable * agd);
124848b8605Smrg   void (*release) (struct apple_glx_drawable * agd);
125848b8605Smrg
126848b8605Smrg     bool(*destroy) (struct apple_glx_drawable * agd);
127848b8605Smrg
128848b8605Smrg     bool(*is_pbuffer) (struct apple_glx_drawable * agd);
129848b8605Smrg
130848b8605Smrg     bool(*is_pixmap) (struct apple_glx_drawable * agd);
131848b8605Smrg
132848b8605Smrg/*BEGIN These are used for the mixed mode drawing... */
133848b8605Smrg   int width, height;
134848b8605Smrg   int row_bytes;
135848b8605Smrg   char path[PATH_MAX];
136848b8605Smrg   int fd;                      /* The file descriptor for this drawable's shared memory. */
137848b8605Smrg   void *buffer;                /* The memory for the drawable.  Typically shared memory. */
138848b8605Smrg   size_t buffer_length;
139848b8605Smrg     /*END*/ struct apple_glx_drawable *previous, *next;
140848b8605Smrg};
141848b8605Smrg
142848b8605Smrgstruct apple_glx_context;
143848b8605Smrg
144848b8605Smrg/* May return NULL if not found */
145848b8605Smrgstruct apple_glx_drawable *apple_glx_find_drawable(Display * dpy,
146848b8605Smrg                                                   GLXDrawable drawable);
147848b8605Smrg
148848b8605Smrg/* Returns true on error and locks the agd result with a reference. */
149848b8605Smrgbool apple_glx_drawable_create(Display * dpy,
150848b8605Smrg                               int screen,
151848b8605Smrg                               GLXDrawable drawable,
152848b8605Smrg                               struct apple_glx_drawable **agd,
153848b8605Smrg                               struct apple_glx_drawable_callbacks
154848b8605Smrg                               *callbacks);
155848b8605Smrg
156848b8605Smrg/* Returns true on error */
157848b8605Smrgbool apple_glx_create_drawable(Display * dpy,
158848b8605Smrg                               struct apple_glx_context *ac,
159848b8605Smrg                               GLXDrawable drawable,
160848b8605Smrg                               struct apple_glx_drawable **agd);
161848b8605Smrg
162848b8605Smrgvoid apple_glx_garbage_collect_drawables(Display * dpy);
163848b8605Smrg
164848b8605Smrg/*
165848b8605Smrg * This returns the total number of drawables.
166848b8605Smrg * It's mostly intended for debugging and introspection.
167848b8605Smrg */
168848b8605Smrgunsigned int apple_glx_get_drawable_count(void);
169848b8605Smrg
170848b8605Smrgstruct apple_glx_drawable *apple_glx_drawable_find_by_type(GLXDrawable
171848b8605Smrg                                                           drawable, int type,
172848b8605Smrg                                                           int flags);
173848b8605Smrg
174848b8605Smrgstruct apple_glx_drawable *apple_glx_drawable_find(GLXDrawable drawable,
175848b8605Smrg                                                   int flags);
176848b8605Smrg
177848b8605Smrg
178848b8605Smrgbool apple_glx_drawable_destroy_by_type(Display * dpy, GLXDrawable drawable,
179848b8605Smrg                                        int type);
180848b8605Smrg
181848b8605Smrgstruct apple_glx_drawable *apple_glx_drawable_find_by_uid(unsigned int uid,
182848b8605Smrg                                                          int flags);
183848b8605Smrg
184848b8605Smrg/* Surfaces */
185848b8605Smrg
186848b8605Smrgbool apple_glx_surface_create(Display * dpy, int screen, GLXDrawable drawable,
187848b8605Smrg                              struct apple_glx_drawable **resultptr);
188848b8605Smrg
189848b8605Smrgvoid apple_glx_surface_destroy(unsigned int uid);
190848b8605Smrg
191848b8605Smrg/* Pbuffers */
192848b8605Smrg
193848b8605Smrg/* Returns true if an error occurred. */
194848b8605Smrgbool apple_glx_pbuffer_create(Display * dpy, GLXFBConfig config,
195848b8605Smrg                              int width, int height, int *errorcode,
196848b8605Smrg                              GLXPbuffer * pbuf);
197848b8605Smrg
198848b8605Smrg/* Returns true if the pbuffer was invalid. */
199848b8605Smrgbool apple_glx_pbuffer_destroy(Display * dpy, GLXPbuffer pbuf);
200848b8605Smrg
201848b8605Smrg/* Returns true if the pbuffer was valid and the attribute. */
202848b8605Smrgbool apple_glx_pbuffer_query(GLXDrawable d, int attribute,
203848b8605Smrg                             unsigned int *value);
204848b8605Smrg
205848b8605Smrg/* Returns true if the GLXDrawable is a valid GLXPbuffer, and the mask is set. */
206848b8605Smrgbool apple_glx_pbuffer_set_event_mask(GLXDrawable d, unsigned long mask);
207848b8605Smrg
208848b8605Smrg/* Returns true if the GLXDrawable is a valid GLXPbuffer, and the *mask is set. */
209848b8605Smrgbool apple_glx_pbuffer_get_event_mask(GLXDrawable d, unsigned long *mask);
210848b8605Smrg
211848b8605Smrg
212848b8605Smrg/* Pixmaps */
213848b8605Smrg
214848b8605Smrg/* mode is a struct glx_config * */
215848b8605Smrg/* Returns true if an error occurred. */
216848b8605Smrgbool apple_glx_pixmap_create(Display * dpy, int screen, Pixmap pixmap,
217848b8605Smrg                             const void *mode);
218848b8605Smrg
219848b8605Smrg/* Returns true if an error occurred. */
220848b8605Smrgbool apple_glx_pixmap_destroy(Display * dpy, Pixmap pixmap);
221848b8605Smrg
222848b8605Smrgbool apple_glx_pixmap_query(GLXPixmap pixmap, int attribute,
223848b8605Smrg                            unsigned int *value);
224848b8605Smrg
225848b8605Smrg
226848b8605Smrg
227848b8605Smrg#endif
228