1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved.
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #ifndef CR_PACK_H
|
---|
8 | #define CR_PACK_H
|
---|
9 |
|
---|
10 | #include "cr_compiler.h"
|
---|
11 | #include "cr_error.h"
|
---|
12 | #include "cr_protocol.h"
|
---|
13 | #include "cr_opcodes.h"
|
---|
14 | #include "cr_endian.h"
|
---|
15 | #include "state/cr_statetypes.h"
|
---|
16 | #include "state/cr_currentpointers.h"
|
---|
17 | #include "state/cr_client.h"
|
---|
18 | #ifdef CHROMIUM_THREADSAFE
|
---|
19 | #include "cr_threads.h"
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #include <iprt/types.h>
|
---|
23 |
|
---|
24 | #ifdef __cplusplus
|
---|
25 | extern "C" {
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | typedef struct CRPackContext_t CRPackContext;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Packer buffer
|
---|
32 | */
|
---|
33 | typedef struct
|
---|
34 | {
|
---|
35 | void *pack; /**< the actual storage space/buffer */
|
---|
36 | unsigned int size; /**< size of pack[] buffer */
|
---|
37 | unsigned int mtu;
|
---|
38 | unsigned char *data_start, *data_current, *data_end;
|
---|
39 | unsigned char *opcode_start, *opcode_current, *opcode_end;
|
---|
40 | GLboolean geometry_only; /**< just used for debugging */
|
---|
41 | GLboolean holds_BeginEnd;
|
---|
42 | GLboolean in_BeginEnd;
|
---|
43 | GLboolean canBarf;
|
---|
44 | GLboolean holds_List;
|
---|
45 | GLboolean in_List;
|
---|
46 | CRPackContext *context;
|
---|
47 | } CRPackBuffer;
|
---|
48 |
|
---|
49 | typedef void (*CRPackFlushFunc)(void *arg);
|
---|
50 | typedef void (*CRPackSendHugeFunc)(CROpcode, void *);
|
---|
51 | typedef void (*CRPackErrorHandlerFunc)(int line, const char *file, GLenum error, const char *info);
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Packer context
|
---|
56 | */
|
---|
57 | struct CRPackContext_t
|
---|
58 | {
|
---|
59 | CRPackBuffer buffer; /**< not a pointer, see comments in pack_buffer.c */
|
---|
60 | CRPackFlushFunc Flush;
|
---|
61 | void *flush_arg;
|
---|
62 | CRPackSendHugeFunc SendHuge;
|
---|
63 | CRPackErrorHandlerFunc Error;
|
---|
64 | CRCurrentStatePointers current;
|
---|
65 | GLvectorf bounds_min, bounds_max;
|
---|
66 | int updateBBOX;
|
---|
67 | int swapping;
|
---|
68 | CRPackBuffer *currentBuffer;
|
---|
69 | CRmutex mutex;
|
---|
70 | char *file; /**< for debugging only */
|
---|
71 | int line; /**< for debugging only */
|
---|
72 | };
|
---|
73 |
|
---|
74 |
|
---|
75 | extern DECLEXPORT(CRPackContext *) crPackNewContext(int swapping);
|
---|
76 | extern DECLEXPORT(void) crPackDeleteContext(CRPackContext *pc);
|
---|
77 | extern DECLEXPORT(void) crPackSetContext( CRPackContext *pc );
|
---|
78 | extern DECLEXPORT(CRPackContext *) crPackGetContext( void );
|
---|
79 |
|
---|
80 | extern DECLEXPORT(void) crPackSetBuffer( CRPackContext *pc, CRPackBuffer *buffer );
|
---|
81 | extern DECLEXPORT(void) crPackSetBufferDEBUG( const char *file, int line, CRPackContext *pc, CRPackBuffer *buffer );
|
---|
82 | extern DECLEXPORT(void) crPackReleaseBuffer( CRPackContext *pc );
|
---|
83 | extern DECLEXPORT(void) crPackResetPointers( CRPackContext *pc );
|
---|
84 |
|
---|
85 | extern DECLEXPORT(int) crPackMaxOpcodes( int buffer_size );
|
---|
86 | extern DECLEXPORT(int) crPackMaxData( int buffer_size );
|
---|
87 | extern DECLEXPORT(void) crPackInitBuffer( CRPackBuffer *buffer, void *buf, int size, int mtu );
|
---|
88 | extern DECLEXPORT(void) crPackFlushFunc( CRPackContext *pc, CRPackFlushFunc ff );
|
---|
89 | extern DECLEXPORT(void) crPackFlushArg( CRPackContext *pc, void *flush_arg );
|
---|
90 | extern DECLEXPORT(void) crPackSendHugeFunc( CRPackContext *pc, CRPackSendHugeFunc shf );
|
---|
91 | extern DECLEXPORT(void) crPackErrorFunction( CRPackContext *pc, CRPackErrorHandlerFunc errf );
|
---|
92 | extern DECLEXPORT(void) crPackOffsetCurrentPointers( int offset );
|
---|
93 | extern DECLEXPORT(void) crPackNullCurrentPointers( void );
|
---|
94 |
|
---|
95 | extern DECLEXPORT(void) crPackResetBoundingBox( CRPackContext *pc );
|
---|
96 | extern DECLEXPORT(GLboolean) crPackGetBoundingBox( CRPackContext *pc,
|
---|
97 | GLfloat *xmin, GLfloat *ymin, GLfloat *zmin,
|
---|
98 | GLfloat *xmax, GLfloat *ymax, GLfloat *zmax);
|
---|
99 |
|
---|
100 | extern DECLEXPORT(void) crPackAppendBuffer( const CRPackBuffer *buffer );
|
---|
101 | extern DECLEXPORT(void) crPackAppendBoundedBuffer( const CRPackBuffer *buffer, const CRrecti *bounds );
|
---|
102 | extern DECLEXPORT(int) crPackCanHoldBuffer( const CRPackBuffer *buffer );
|
---|
103 | extern DECLEXPORT(int) crPackCanHoldBoundedBuffer( const CRPackBuffer *buffer );
|
---|
104 |
|
---|
105 | #if defined(LINUX) || defined(WINDOWS)
|
---|
106 | #define CR_UNALIGNED_ACCESS_OKAY
|
---|
107 | #else
|
---|
108 | #undef CR_UNALIGNED_ACCESS_OKAY
|
---|
109 | #endif
|
---|
110 | extern DECLEXPORT(void) crWriteUnalignedDouble( void *buffer, double d );
|
---|
111 | extern DECLEXPORT(void) crWriteSwappedDouble( void *buffer, double d );
|
---|
112 |
|
---|
113 | extern DECLEXPORT(void) *crPackAlloc( unsigned int len );
|
---|
114 | extern DECLEXPORT(void) crHugePacket( CROpcode op, void *ptr );
|
---|
115 | extern DECLEXPORT(void) crPackFree( void *ptr );
|
---|
116 | extern DECLEXPORT(void) crNetworkPointerWrite( CRNetworkPointer *, void * );
|
---|
117 |
|
---|
118 | extern DECLEXPORT(void) crPackExpandDrawArrays(GLenum mode, GLint first, GLsizei count, CRClientState *c);
|
---|
119 | extern DECLEXPORT(void) crPackExpandDrawArraysSWAP(GLenum mode, GLint first, GLsizei count, CRClientState *c);
|
---|
120 |
|
---|
121 | extern DECLEXPORT(void) crPackUnrollDrawElements(GLsizei count, GLenum type, const GLvoid *indices);
|
---|
122 | extern DECLEXPORT(void) crPackUnrollDrawElementsSWAP(GLsizei count, GLenum type, const GLvoid *indices);
|
---|
123 |
|
---|
124 | extern DECLEXPORT(void) crPackExpandDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
|
---|
125 | extern DECLEXPORT(void) crPackExpandDrawElementsSWAP(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
|
---|
126 |
|
---|
127 | extern DECLEXPORT(void) crPackExpandDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
|
---|
128 | extern DECLEXPORT(void) crPackExpandDrawRangeElementsSWAP(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
|
---|
129 |
|
---|
130 | extern DECLEXPORT(void) crPackExpandArrayElement(GLint index, CRClientState *c);
|
---|
131 | extern DECLEXPORT(void) crPackExpandArrayElementSWAP(GLint index, CRClientState *c);
|
---|
132 |
|
---|
133 | extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXT( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c );
|
---|
134 | extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXTSWAP( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c );
|
---|
135 |
|
---|
136 | extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXT( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c );
|
---|
137 | extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXTSWAP( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c );
|
---|
138 |
|
---|
139 |
|
---|
140 | #ifdef CHROMIUM_THREADSAFE
|
---|
141 | extern CRtsd _PackerTSD;
|
---|
142 | #define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = (CRPackContext *) crGetTSD(&_PackerTSD)
|
---|
143 | #define CR_LOCK_PACKER_CONTEXT(PC) crLockMutex(&((PC)->mutex))
|
---|
144 | #define CR_UNLOCK_PACKER_CONTEXT(PC) crUnlockMutex(&((PC)->mutex))
|
---|
145 | #else
|
---|
146 | extern DLLDATA(CRPackContext) cr_packer_globals;
|
---|
147 | #define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = &cr_packer_globals
|
---|
148 | #define CR_LOCK_PACKER_CONTEXT(PC)
|
---|
149 | #define CR_UNLOCK_PACKER_CONTEXT(PC)
|
---|
150 | #endif
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Return number of opcodes in given buffer.
|
---|
155 | */
|
---|
156 | static INLINE int
|
---|
157 | crPackNumOpcodes(const CRPackBuffer *buffer)
|
---|
158 | {
|
---|
159 | CRASSERT(buffer->opcode_start - buffer->opcode_current >= 0);
|
---|
160 | return buffer->opcode_start - buffer->opcode_current;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Return amount of data (in bytes) in buffer.
|
---|
166 | */
|
---|
167 | static INLINE int
|
---|
168 | crPackNumData(const CRPackBuffer *buffer)
|
---|
169 | {
|
---|
170 | CRASSERT(buffer->data_current - buffer->data_start >= 0);
|
---|
171 | return buffer->data_current - buffer->data_start; /* in bytes */
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | static INLINE int
|
---|
176 | crPackCanHoldOpcode(const CRPackContext *pc, int num_opcode, int num_data)
|
---|
177 | {
|
---|
178 | int fitsInMTU, opcodesFit, dataFits;
|
---|
179 |
|
---|
180 | CRASSERT(pc->currentBuffer);
|
---|
181 |
|
---|
182 | fitsInMTU = (((pc->buffer.data_current - pc->buffer.opcode_current - 1
|
---|
183 | + num_opcode + num_data
|
---|
184 | + 0x3 ) & ~0x3) + sizeof(CRMessageOpcodes)
|
---|
185 | <= pc->buffer.mtu);
|
---|
186 | opcodesFit = (pc->buffer.opcode_current - num_opcode >= pc->buffer.opcode_end);
|
---|
187 | dataFits = (pc->buffer.data_current + num_data <= pc->buffer.data_end);
|
---|
188 |
|
---|
189 | return fitsInMTU && opcodesFit && dataFits;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Alloc space for a message of 'len' bytes (plus 1 opcode).
|
---|
195 | * Only flush if buffer is full.
|
---|
196 | */
|
---|
197 | #define CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH(pc, len, lock) \
|
---|
198 | do { \
|
---|
199 | THREADASSERT( pc ); \
|
---|
200 | if (lock) CR_LOCK_PACKER_CONTEXT(pc); \
|
---|
201 | CRASSERT( pc->currentBuffer ); \
|
---|
202 | if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
|
---|
203 | pc->Flush( pc->flush_arg ); \
|
---|
204 | CRASSERT(crPackCanHoldOpcode( pc, 1, (len) ) ); \
|
---|
205 | } \
|
---|
206 | data_ptr = pc->buffer.data_current; \
|
---|
207 | pc->buffer.data_current += (len); \
|
---|
208 | } while (0)
|
---|
209 |
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * As above, flush if the buffer contains vertex data and we're
|
---|
213 | * no longer inside glBegin/glEnd.
|
---|
214 | */
|
---|
215 | #define CR_GET_BUFFERED_POINTER( pc, len ) \
|
---|
216 | do { \
|
---|
217 | CR_LOCK_PACKER_CONTEXT(pc); \
|
---|
218 | CRASSERT( pc->currentBuffer ); \
|
---|
219 | if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
|
---|
220 | pc->Flush( pc->flush_arg ); \
|
---|
221 | pc->buffer.holds_BeginEnd = 0; \
|
---|
222 | } \
|
---|
223 | CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
|
---|
224 | } while (0)
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * As above, but without lock.
|
---|
228 | */
|
---|
229 | #define CR_GET_BUFFERED_POINTER_NOLOCK( pc, len ) \
|
---|
230 | do { \
|
---|
231 | CRASSERT( pc->currentBuffer ); \
|
---|
232 | if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
|
---|
233 | pc->Flush( pc->flush_arg ); \
|
---|
234 | pc->buffer.holds_BeginEnd = 0; \
|
---|
235 | } \
|
---|
236 | CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
|
---|
237 | } while (0)
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * As above, but for vertex data between glBegin/End (counts vertices).
|
---|
242 | */
|
---|
243 | #define CR_GET_BUFFERED_COUNT_POINTER( pc, len ) \
|
---|
244 | do { \
|
---|
245 | CR_LOCK_PACKER_CONTEXT(pc); \
|
---|
246 | CRASSERT( pc->currentBuffer ); \
|
---|
247 | if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
|
---|
248 | pc->Flush( pc->flush_arg ); \
|
---|
249 | CRASSERT( crPackCanHoldOpcode( pc, 1, (len) ) ); \
|
---|
250 | } \
|
---|
251 | data_ptr = pc->buffer.data_current; \
|
---|
252 | pc->current.vtx_count++; \
|
---|
253 | pc->buffer.data_current += (len); \
|
---|
254 | } while (0)
|
---|
255 |
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Allocate space for a msg/command that has no arguments, such
|
---|
259 | * as glFinish().
|
---|
260 | */
|
---|
261 | #define CR_GET_BUFFERED_POINTER_NO_ARGS( pc ) \
|
---|
262 | CR_GET_BUFFERED_POINTER( pc, 4 ); \
|
---|
263 | WRITE_DATA( 0, GLuint, 0xdeadbeef )
|
---|
264 |
|
---|
265 | #define WRITE_DATA( offset, type, data ) \
|
---|
266 | *( (type *) (data_ptr + (offset))) = (data)
|
---|
267 |
|
---|
268 | /* Write data to current location and auto increment */
|
---|
269 | #define WRITE_DATA_AI(type, data) \
|
---|
270 | { \
|
---|
271 | *((type*) (data_ptr)) = (data); \
|
---|
272 | data_ptr += sizeof(type); \
|
---|
273 | }
|
---|
274 |
|
---|
275 | #ifdef CR_UNALIGNED_ACCESS_OKAY
|
---|
276 | #define WRITE_DOUBLE( offset, data ) \
|
---|
277 | WRITE_DATA( offset, GLdouble, data )
|
---|
278 | #else
|
---|
279 | #define WRITE_DOUBLE( offset, data ) \
|
---|
280 | crWriteUnalignedDouble( data_ptr + (offset), (data) )
|
---|
281 | #endif
|
---|
282 |
|
---|
283 | #define WRITE_SWAPPED_DOUBLE( offset, data ) \
|
---|
284 | crWriteSwappedDouble( data_ptr + (offset), (data) )
|
---|
285 |
|
---|
286 | #define WRITE_OPCODE( pc, opcode ) \
|
---|
287 | *(pc->buffer.opcode_current--) = (unsigned char) opcode
|
---|
288 |
|
---|
289 | #define WRITE_NETWORK_POINTER( offset, data ) \
|
---|
290 | crNetworkPointerWrite( (CRNetworkPointer *) ( data_ptr + (offset) ), (data) )
|
---|
291 |
|
---|
292 | #ifdef __cplusplus
|
---|
293 | }
|
---|
294 | #endif
|
---|
295 |
|
---|
296 | #endif /* CR_PACK_H */
|
---|