Initial commit
[profile/ivi/simulator-opengl.git] / libGL / common.h
1 /*
2  *  Guest-side implementation of GL/GLX API. Replacement of standard libGL.so
3  *
4  *  Copyright (c) 2006,2007 Even Rouault
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, 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 DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #ifndef __INCLUDE_COMMON_H
26
27 #include <X11/X.h>
28 #include <X11/Xutil.h>
29 #include <X11/extensions/Xfixes.h>
30 #include <X11/extensions/XShm.h>
31 #include <sys/ipc.h>
32 #include <sys/shm.h>
33
34 #include "mesa_glx.h"
35
36 #include "range_alloc.h"
37
38 #define ENABLE_THREAD_SAFETY
39
40 extern int debug_gl;
41 extern int debug_array_ptr;
42 extern int disable_optim;
43
44 #define IS_GLX_CALL(x) (x >= glXChooseVisual_func && x <= glXDestroyPixmap_func)
45
46 #define SIZE_BUFFER_COMMAND (1024*64)
47 //#define MAX_SIZE_BUFFER_COMMAND (1024*1024*10)
48 #define MAX_SIZE_BUFFER_COMMAND (1024*1024*200)
49
50 #define POINTER_TO_ARG(x)            (long)(void*)(x)
51 #define CHAR_TO_ARG(x)               (long)(x)
52 #define SHORT_TO_ARG(x)              (long)(x)
53 #define INT_TO_ARG(x)                (long)(x)
54 #define UNSIGNED_CHAR_TO_ARG(x)      (long)(x)
55 #define UNSIGNED_SHORT_TO_ARG(x)     (long)(x)
56 #define UNSIGNED_INT_TO_ARG(x)       (long)(x)
57 #define FLOAT_TO_ARG(x)              (long)*((int*)(&x))
58 #define DOUBLE_TO_ARG(x)             (long)(&x)
59
60 #define NOT_IMPLEMENTED(x) log_gl(#x " not implemented !\n")
61 //#define PROVIDE_STUB_IMPLEMENTATION
62
63 #define CHECK_ARGS(x, y) (1 / ((sizeof(x)/sizeof(x[0])) == (sizeof(y)/sizeof(y[0])) ? 1 : 0)) ? x : x, y
64
65 #define EXT_FUNC(x) x
66
67 #define CONCAT(a, b) a##b
68 #define DEFINE_EXT(glFunc, paramsDecl, paramsCall)  GLAPI void APIENTRY CONCAT(glFunc,EXT) paramsDecl { glFunc paramsCall; }
69
70 #ifndef MIN
71 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
72 #endif
73 #ifndef MAX
74 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
75 #endif
76
77
78 #define NB_MAX_TEXTURES 16
79 #define MY_GL_MAX_VERTEX_ATTRIBS_ARB 16
80 #define MY_GL_MAX_VERTEX_ATTRIBS_NV 16
81 #define MY_GL_MAX_VARIANT_POINTER_EXT 16
82
83 #define CHECK_PROC(x)
84 #define CHECK_PROC_WITH_RET(x)
85
86 //static void log_gl(const char* format, ...);
87
88 #include "opengl_utils.h"
89
90 typedef struct
91 {
92   int size;
93   int type;
94   int stride;
95   const void* ptr;
96
97   int index;
98   int normalized;
99
100   int enabled;
101   int vbo_name;
102
103   unsigned int last_crc;
104 } ClientArray;
105
106 typedef struct {
107     GLboolean swapEndian;
108     GLboolean lsbFirst;
109     GLuint rowLength;
110     GLuint imageHeight;
111     GLuint skipRows;
112     GLuint skipPixels;
113     GLuint skipImages;
114     GLuint alignment;
115 } PixelStoreMode;
116
117 typedef struct
118 {
119   int format;
120   int stride;
121   const void* ptr;
122 } InterleavedArrays;
123
124 typedef struct {
125   ClientArray vertexArray;
126   ClientArray normalArray;
127   ClientArray colorArray;
128   ClientArray secondaryColorArray;
129   ClientArray indexArray;
130   ClientArray edgeFlagArray;
131   ClientArray weightArray;
132   ClientArray matrixIndexArray;
133   ClientArray fogCoordArray;
134   ClientArray texCoordArray[NB_MAX_TEXTURES];
135   ClientArray vertexAttribArray[MY_GL_MAX_VERTEX_ATTRIBS_ARB];
136   ClientArray vertexAttribArrayNV[MY_GL_MAX_VERTEX_ATTRIBS_NV];
137   ClientArray variantPointer[MY_GL_MAX_VARIANT_POINTER_EXT];
138   ClientArray elementArrayATI;
139   InterleavedArrays interleavedArrays;
140 } ClientArrays;
141
142
143 typedef struct
144 {
145   GLbitfield     mask;
146   PixelStoreMode pack;
147   PixelStoreMode unpack;
148   ClientArrays   arrays;
149   int clientActiveTexture;
150   int selectBufferSize;
151   void* selectBufferPtr;
152   int feedbackBufferSize;
153   void* feedbackBufferPtr;
154 } ClientState;
155
156 #define MAX_MATRIX_STACK_SIZE  64
157 #define NB_GL_MATRIX    (3+NB_MAX_TEXTURES+31)
158 typedef struct
159 {
160   double val[16];
161 } Matrixd;
162
163 typedef struct
164 {
165   Matrixd stack[MAX_MATRIX_STACK_SIZE];
166   Matrixd current;
167   int sp;
168 } GLMatrix;
169
170 typedef struct
171 {
172   int x;
173   int y;
174   int width;
175   int height;
176 } ViewportStruct;
177
178 typedef struct
179 {
180   int width;
181   int height;
182   int map_state;
183 } WindowInfoStruct;
184
185 typedef struct
186 {
187   int id;
188   int datatype;
189   int components;
190 } Symbol;
191
192 typedef struct
193 {
194   Symbol* tab;
195   int count;
196 } Symbols;
197
198 typedef struct
199 {
200   int texture;
201   int level;
202
203   int width;
204   int height;
205 } Texture2DDim;
206
207 typedef struct
208 {
209   float mode;
210   float density;
211   float start;
212   float end;
213   float index;
214   float color[4];
215 } Fog;
216
217 typedef struct
218 {
219   int size;
220   void* ptr;
221   int mapped;
222   int usage;
223   int access;
224 } Buffer;
225
226 typedef struct
227 {
228   int start;
229   int length;
230 } IntRange;
231
232 typedef struct
233 {
234   IntRange* ranges;
235   int nb;
236   int maxNb;
237 } IntSetRanges;
238
239 typedef struct
240 {
241   int bufferid;
242   int size;
243   void* ptr;
244   void* ptrMapped;
245   IntSetRanges updatedRangesAfterMapping;
246   void* ptrUpdatedWhileMapped;
247 } ObjectBufferATI;
248
249 typedef struct
250 {
251   GLuint program;
252   char* txt;
253   int location;
254 } UniformLocation;
255
256
257 #define MAX_CLIENT_STATE_STACK_SIZE 16
258 #define MAX_SERVER_STATE_STACK_SIZE 16
259
260 #define N_CLIP_PLANES   8
261 typedef struct
262 {
263   GLbitfield     mask;
264   int            matrixMode;
265   int            bindTexture2D; /* optimization for openquartz  */
266   int            bindTextureRectangle;
267   int            linesmoothEnabled; /* optimization for googleearth */
268   int            lightingEnabled;
269   int            texture2DEnabled;
270   int            blendEnabled;
271   int            scissorTestEnabled;
272   int            vertexProgramEnabled;
273   int            fogEnabled;
274   int            depthFunc;
275   Fog            fog;
276
277   Texture2DDim*  texture2DCache;
278   int            texture2DCacheDim;
279   Texture2DDim*  textureProxy2DCache;
280   int            textureProxy2DCacheDim;
281   Texture2DDim*  textureRectangleCache;
282   int            textureRectangleCacheDim;
283   Texture2DDim*  textureProxyRectangleCache;
284   int            textureProxyRectangleCacheDim;
285
286   double         clipPlanes[N_CLIP_PLANES][4];
287 } ServerState;
288
289
290 typedef struct
291 {
292   XImage *image;
293   XShmSegmentInfo shminfo;
294   char *buffer;
295   int w;
296   int h;
297   int use_shm;
298 } RendererData;
299
300
301 typedef struct
302 {
303   int ref;
304   Display* display;
305   GLXContext context;
306   GLXDrawable current_drawable;
307   GLXDrawable current_read_drawable;
308   struct timeval last_swap_buffer_time;
309   GLXContext shareList;
310   XFixesCursorImage last_cursor;
311   GLXPbuffer pbuffer;
312
313   RendererData *renderer_data;
314
315   int isAssociatedToFBConfigVisual;
316
317   ClientState client_state_stack[MAX_CLIENT_STATE_STACK_SIZE];
318   ClientState client_state;
319   int client_state_sp;
320
321   ServerState server_stack[MAX_SERVER_STATE_STACK_SIZE];
322   ServerState current_server_state;
323   int server_sp;
324
325   int            arrayBuffer; /* optimization for ww2d */
326   int            elementArrayBuffer;
327   int            pixelUnpackBuffer;
328   int            pixelPackBuffer;
329
330   Buffer arrayBuffers[32768];
331   Buffer elementArrayBuffers[32768];
332   Buffer pixelUnpackBuffers[32768];
333   Buffer pixelPackBuffers[32768];
334
335   RangeAllocator ownTextureAllocator;
336   RangeAllocator* textureAllocator;
337   RangeAllocator ownBufferAllocator;
338   RangeAllocator* bufferAllocator;
339   RangeAllocator ownListAllocator;
340   RangeAllocator* listAllocator;
341
342   Symbols symbols;
343
344   ObjectBufferATI objectBuffersATI[32768];
345
346   UniformLocation* uniformLocations;
347   int countUniformLocations;
348
349   WindowInfoStruct last_win_state;
350   ViewportStruct viewport;
351   ViewportStruct scissorbox;
352   int drawable_width;
353   int drawable_height;
354
355   int currentRasterPosKnown;
356   float currentRasterPos[4];
357
358   char* tuxRacerBuffer;
359
360   int activeTexture;
361
362   int isBetweenBegin;
363
364   int lastGlError;
365
366   int isBetweenLockArrays;
367   int locked_first;
368   int locked_count;
369
370   GLMatrix matrix[NB_GL_MATRIX];
371 } GLState;
372
373 extern int nbGLStates;
374 extern GLState** glstates;
375 extern GLState* default_gl_state;
376
377 #define __INCLUDE_COMMON_H
378 #endif /* __INCLUDE_COMMON_H */
379