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