VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/include/cr_pack.h@ 52260

最後變更 在這個檔案從52260是 52260,由 vboxsync 提交於 11 年 前

crOpenGL: proper support of GetErr being called from display list, better error handling

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 18.3 KB
 
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
25extern "C" {
26#endif
27
28typedef struct CRPackContext_t CRPackContext;
29
30/**
31 * Packer buffer
32 */
33typedef 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
49typedef void (*CRPackFlushFunc)(void *arg);
50typedef void (*CRPackSendHugeFunc)(CROpcode, void *);
51typedef void (*CRPackErrorHandlerFunc)(int line, const char *file, GLenum error, const char *info);
52
53#define CRPACKBLOCKSTATE_OP_BEGIN 0x01
54#define CRPACKBLOCKSTATE_OP_NEWLIST 0x02
55#define CRPACKBLOCKSTATE_OP_BEGINQUERY 0x04
56#define CRPACKBLOCKSTATE_OP_ALL 0x07
57
58#define CRPACKBLOCKSTATE_IS_OP_STARTED(_state, _op) (!!((_state) & (_op)))
59
60#define CRPACKBLOCKSTATE_IS_STARTED(_state) (!!(_state))
61
62#define CRPACKBLOCKSTATE_OP_START(_state, _op) do { \
63 Assert(!CRPACKBLOCKSTATE_IS_OP_STARTED(_state, _op)); \
64 (_state) |= (_op); \
65 } while (0)
66
67#define CRPACKBLOCKSTATE_OP_STOP(_state, _op) do { \
68 Assert(CRPACKBLOCKSTATE_IS_OP_STARTED(_state, _op)); \
69 (_state) &= ~(_op); \
70 } while (0)
71
72/**
73 * Packer context
74 */
75struct CRPackContext_t
76{
77 CRPackBuffer buffer; /**< not a pointer, see comments in pack_buffer.c */
78 CRPackFlushFunc Flush;
79 void *flush_arg;
80 CRPackSendHugeFunc SendHuge;
81 CRPackErrorHandlerFunc Error;
82 CRCurrentStatePointers current;
83 uint32_t u32CmdBlockState;
84 GLvectorf bounds_min, bounds_max;
85 int updateBBOX;
86 int swapping;
87 CRPackBuffer *currentBuffer;
88#ifdef CHROMIUM_THREADSAFE
89 CRmutex mutex;
90#endif
91 char *file; /**< for debugging only */
92 int line; /**< for debugging only */
93};
94
95#if !defined(IN_RING0)
96# define CR_PACKER_CONTEXT_ARGSINGLEDECL
97# define CR_PACKER_CONTEXT_ARGDECL
98# define CR_PACKER_CONTEXT_ARG
99# define CR_PACKER_CONTEXT_ARGCTX(C)
100# ifdef CHROMIUM_THREADSAFE
101extern CRtsd _PackerTSD;
102# define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = (CRPackContext *) crGetTSD(&_PackerTSD)
103# define CR_LOCK_PACKER_CONTEXT(PC) crLockMutex(&((PC)->mutex))
104# define CR_UNLOCK_PACKER_CONTEXT(PC) crUnlockMutex(&((PC)->mutex))
105# else
106extern DLLDATA(CRPackContext) cr_packer_globals;
107# define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = &cr_packer_globals
108# define CR_LOCK_PACKER_CONTEXT(PC)
109# define CR_UNLOCK_PACKER_CONTEXT(PC)
110# endif
111extern int cr_packer_cmd_blocks_enabled;
112#else /* if defined IN_RING0 */
113# define CR_PACKER_CONTEXT_ARGSINGLEDECL CRPackContext *_pCtx
114# define CR_PACKER_CONTEXT_ARGDECL CR_PACKER_CONTEXT_ARGSINGLEDECL,
115# define CR_PACKER_CONTEXT_ARG _pCtx,
116# define CR_PACKER_CONTEXT_ARGCTX(C) C,
117# define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = _pCtx
118# define CR_LOCK_PACKER_CONTEXT(PC)
119# define CR_UNLOCK_PACKER_CONTEXT(PC)
120#endif
121
122extern DECLEXPORT(CRPackContext *) crPackNewContext(int swapping);
123extern DECLEXPORT(void) crPackDeleteContext(CRPackContext *pc);
124extern DECLEXPORT(void) crPackSetContext( CRPackContext *pc );
125extern DECLEXPORT(CRPackContext *) crPackGetContext( void );
126
127extern DECLEXPORT(void) crPackSetBuffer( CRPackContext *pc, CRPackBuffer *buffer );
128extern DECLEXPORT(void) crPackSetBufferDEBUG( const char *file, int line, CRPackContext *pc, CRPackBuffer *buffer );
129extern DECLEXPORT(void) crPackReleaseBuffer( CRPackContext *pc );
130extern DECLEXPORT(void) crPackResetPointers( CRPackContext *pc );
131
132extern DECLEXPORT(int) crPackMaxOpcodes( int buffer_size );
133extern DECLEXPORT(int) crPackMaxData( int buffer_size );
134extern DECLEXPORT(void) crPackInitBuffer( CRPackBuffer *buffer, void *buf, int size, int mtu
135#ifdef IN_RING0
136 , unsigned int num_opcodes
137#endif
138 );
139
140extern DECLEXPORT(void) crPackFlushFunc( CRPackContext *pc, CRPackFlushFunc ff );
141extern DECLEXPORT(void) crPackFlushArg( CRPackContext *pc, void *flush_arg );
142extern DECLEXPORT(void) crPackSendHugeFunc( CRPackContext *pc, CRPackSendHugeFunc shf );
143extern DECLEXPORT(void) crPackErrorFunction( CRPackContext *pc, CRPackErrorHandlerFunc errf );
144extern DECLEXPORT(void) crPackOffsetCurrentPointers( int offset );
145extern DECLEXPORT(void) crPackNullCurrentPointers( void );
146
147extern DECLEXPORT(void) crPackResetBoundingBox( CRPackContext *pc );
148extern DECLEXPORT(GLboolean) crPackGetBoundingBox( CRPackContext *pc,
149 GLfloat *xmin, GLfloat *ymin, GLfloat *zmin,
150 GLfloat *xmax, GLfloat *ymax, GLfloat *zmax);
151
152extern DECLEXPORT(void) crPackAppendBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer );
153extern DECLEXPORT(void) crPackAppendBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer, const CRrecti *bounds );
154extern DECLEXPORT(int) crPackCanHoldBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer );
155extern DECLEXPORT(int) crPackCanHoldBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *buffer );
156
157#if defined(LINUX) || defined(WINDOWS)
158#define CR_UNALIGNED_ACCESS_OKAY
159#else
160#undef CR_UNALIGNED_ACCESS_OKAY
161#endif
162#ifndef IN_RING0
163extern DECLEXPORT(void) crWriteUnalignedDouble( void *buffer, double d );
164extern DECLEXPORT(void) crWriteSwappedDouble( void *buffer, double d );
165#endif
166
167extern DECLEXPORT(void) *crPackAlloc( CR_PACKER_CONTEXT_ARGDECL unsigned int len );
168extern DECLEXPORT(void) crHugePacket( CR_PACKER_CONTEXT_ARGDECL CROpcode op, void *ptr );
169extern DECLEXPORT(void) crPackFree( CR_PACKER_CONTEXT_ARGDECL void *ptr );
170extern DECLEXPORT(void) crNetworkPointerWrite( CRNetworkPointer *, void * );
171
172extern DECLEXPORT(void) crPackExpandDrawArrays(GLenum mode, GLint first, GLsizei count, CRClientState *c, const GLfloat *pZva);
173extern DECLEXPORT(void) crPackExpandDrawArraysSWAP(GLenum mode, GLint first, GLsizei count, CRClientState *c, const GLfloat *pZva);
174
175extern DECLEXPORT(void) crPackUnrollDrawElements(GLsizei count, GLenum type, const GLvoid *indices);
176extern DECLEXPORT(void) crPackUnrollDrawElementsSWAP(GLsizei count, GLenum type, const GLvoid *indices);
177
178extern DECLEXPORT(void) crPackExpandDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c, const GLfloat *pZva);
179extern DECLEXPORT(void) crPackExpandDrawElementsSWAP(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c, const GLfloat *pZva);
180
181extern DECLEXPORT(void) crPackExpandDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c, const GLfloat *pZva);
182extern DECLEXPORT(void) crPackExpandDrawRangeElementsSWAP(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c, const GLfloat *pZva);
183
184extern DECLEXPORT(void) crPackExpandArrayElement(GLint index, CRClientState *c, const GLfloat *pZva);
185extern DECLEXPORT(void) crPackExpandArrayElementSWAP(GLint index, CRClientState *c, const GLfloat *pZva);
186
187extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXT( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c, const GLfloat *pZva );
188extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXTSWAP( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c, const GLfloat *pZva );
189
190extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXT( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c, const GLfloat *pZva );
191extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXTSWAP( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c, const GLfloat *pZva );
192
193extern DECLEXPORT(void) crPackCmdBlocksEnable();
194
195
196/**
197 * Return number of opcodes in given buffer.
198 */
199static INLINE int
200crPackNumOpcodes(const CRPackBuffer *buffer)
201{
202 CRASSERT(buffer->opcode_start - buffer->opcode_current >= 0);
203 return buffer->opcode_start - buffer->opcode_current;
204}
205
206DECLINLINE(bool) crPackBufferIsEmpty(CRPackBuffer *buffer)
207{
208 return crPackNumOpcodes(buffer) == 0;
209}
210
211/**
212 * Return amount of data (in bytes) in buffer.
213 */
214static INLINE int
215crPackNumData(const CRPackBuffer *buffer)
216{
217 CRASSERT(buffer->data_current - buffer->data_start >= 0);
218 return buffer->data_current - buffer->data_start; /* in bytes */
219}
220
221
222static INLINE int
223crPackCanHoldOpcode(const CRPackContext *pc, int num_opcode, int num_data)
224{
225 int fitsInMTU, opcodesFit, dataFits;
226
227 CRASSERT(pc->currentBuffer);
228
229 fitsInMTU = (((pc->buffer.data_current - pc->buffer.opcode_current - 1
230 + num_opcode + num_data
231 + 0x3 ) & ~0x3) + sizeof(CRMessageOpcodes)
232 <= pc->buffer.mtu);
233 opcodesFit = (pc->buffer.opcode_current - num_opcode >= pc->buffer.opcode_end);
234 dataFits = (pc->buffer.data_current + num_data <= pc->buffer.data_end);
235
236 return fitsInMTU && opcodesFit && dataFits;
237}
238
239
240#define CR_PACK_SPECIAL_OP( _pc, _op) \
241 do { \
242 data_ptr = pc->buffer.data_current; \
243 (_pc)->buffer.data_current += 4; \
244 WRITE_OPCODE( (_pc), (_op) ); \
245 WRITE_DATA( 0, GLuint, 0xdeadbeef ); \
246 data_ptr = NULL; /* <- sanity*/ \
247 } while (0)
248
249#define CR_CMDBLOCK_OP( _pc, _op) \
250 do { \
251 CR_PACK_SPECIAL_OP( _pc, _op); \
252 } while (0)
253
254#define CR_CMDBLOCK_IS_STARTED( pc, op ) CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)
255
256#define CR_CMDBLOCK_BEGIN( pc, op ) \
257 do { \
258 CR_LOCK_PACKER_CONTEXT(pc); \
259 if (!cr_packer_cmd_blocks_enabled) break; \
260 if (!CRPACKBLOCKSTATE_IS_STARTED((pc)->u32CmdBlockState)) { \
261 THREADASSERT( pc ); \
262 CRASSERT( (pc)->currentBuffer ); \
263 if (!crPackBufferIsEmpty(&(pc)->buffer)) { \
264 if ((*(pc)->buffer.opcode_start) != CR_NOP_OPCODE) { \
265 (pc)->Flush( (pc)->flush_arg ); \
266 Assert(crPackCanHoldOpcode( (pc), 1, 4 ) ); \
267 CR_CMDBLOCK_OP( (pc), CR_CMDBLOCKBEGIN_OPCODE ); \
268 } \
269 else { \
270 (*(pc)->buffer.opcode_start) = CR_CMDBLOCKBEGIN_OPCODE; \
271 } \
272 } \
273 else { \
274 Assert(crPackCanHoldOpcode( (pc), 1, 4 ) ); \
275 CR_CMDBLOCK_OP( (pc), CR_CMDBLOCKBEGIN_OPCODE ); \
276 } \
277 } \
278 Assert(!CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)); \
279 CRPACKBLOCKSTATE_OP_START((pc)->u32CmdBlockState, op); \
280 Assert(CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)); \
281 } while (0)
282
283#define CR_CMDBLOCK_END( pc, op ) \
284 do { \
285 if (!cr_packer_cmd_blocks_enabled) break; \
286 Assert(CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)); \
287 CRPACKBLOCKSTATE_OP_STOP((pc)->u32CmdBlockState, op); \
288 Assert(!CRPACKBLOCKSTATE_IS_OP_STARTED((pc)->u32CmdBlockState, op)); \
289 if (!CRPACKBLOCKSTATE_IS_STARTED((pc)->u32CmdBlockState)) { \
290 THREADASSERT( pc ); \
291 CRASSERT( (pc)->currentBuffer ); \
292 if (!crPackBufferIsEmpty(&(pc)->buffer)) { \
293 if ((*(pc)->buffer.opcode_start) != CR_CMDBLOCKBEGIN_OPCODE) {\
294 if ( !crPackCanHoldOpcode( pc, 1, 4 ) ) { \
295 (pc)->Flush( (pc)->flush_arg ); \
296 Assert(crPackCanHoldOpcode( pc, 1, 4 ) ); \
297 } \
298 CR_CMDBLOCK_OP( pc, CR_CMDBLOCKEND_OPCODE ); \
299 (pc)->Flush( (pc)->flush_arg ); \
300 } \
301 else { \
302 (*(pc)->buffer.opcode_start) = CR_NOP_OPCODE; \
303 } \
304 } \
305 else { \
306 Assert(crPackCanHoldOpcode( pc, 1, 4 ) ); \
307 CR_CMDBLOCK_OP( pc, CR_CMDBLOCKEND_OPCODE ); \
308 (pc)->Flush( pc->flush_arg ); \
309 } \
310 } \
311 } while (0)
312
313/**
314 * Alloc space for a message of 'len' bytes (plus 1 opcode).
315 * Only flush if buffer is full.
316 */
317#define CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH(pc, len, lock) \
318 do { \
319 THREADASSERT( pc ); \
320 if (lock) CR_LOCK_PACKER_CONTEXT(pc); \
321 CRASSERT( pc->currentBuffer ); \
322 if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
323 pc->Flush( pc->flush_arg ); \
324 CRASSERT(crPackCanHoldOpcode( pc, 1, (len) ) ); \
325 } \
326 data_ptr = pc->buffer.data_current; \
327 pc->buffer.data_current += (len); \
328 } while (0)
329
330/**
331 * As above, flush if the buffer contains vertex data and we're
332 * no longer inside glBegin/glEnd.
333 */
334#define CR_GET_BUFFERED_POINTER( pc, len ) \
335 do { \
336 CR_LOCK_PACKER_CONTEXT(pc); \
337 CRASSERT( pc->currentBuffer ); \
338 if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
339 CRASSERT( 0 ); /* should never be here currently */ \
340 pc->Flush( pc->flush_arg ); \
341 pc->buffer.holds_BeginEnd = 0; \
342 } \
343 CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
344 } while (0)
345
346/**
347 * As above, but without lock.
348 */
349#define CR_GET_BUFFERED_POINTER_NOLOCK( pc, len ) \
350 do { \
351 CRASSERT( pc->currentBuffer ); \
352 if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
353 CRASSERT( 0 ); /* should never be here currently */ \
354 pc->Flush( pc->flush_arg ); \
355 pc->buffer.holds_BeginEnd = 0; \
356 } \
357 CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
358 } while (0)
359
360
361/**
362 * As above, but for vertex data between glBegin/End (counts vertices).
363 */
364#define CR_GET_BUFFERED_COUNT_POINTER( pc, len ) \
365 do { \
366 CR_LOCK_PACKER_CONTEXT(pc); \
367 CRASSERT( pc->currentBuffer ); \
368 if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
369 pc->Flush( pc->flush_arg ); \
370 CRASSERT( crPackCanHoldOpcode( pc, 1, (len) ) ); \
371 } \
372 data_ptr = pc->buffer.data_current; \
373 pc->current.vtx_count++; \
374 pc->buffer.data_current += (len); \
375 } while (0)
376
377
378/**
379 * Allocate space for a msg/command that has no arguments, such
380 * as glFinish().
381 */
382#define CR_GET_BUFFERED_POINTER_NO_ARGS( pc ) \
383 CR_GET_BUFFERED_POINTER( pc, 4 ); \
384 WRITE_DATA( 0, GLuint, 0xdeadbeef )
385
386#define WRITE_DATA( offset, type, data ) \
387 *( (type *) (data_ptr + (offset))) = (data)
388
389/* Write data to current location and auto increment */
390#define WRITE_DATA_AI(type, data) \
391 { \
392 *((type*) (data_ptr)) = (data); \
393 data_ptr += sizeof(type); \
394 }
395
396#ifdef CR_UNALIGNED_ACCESS_OKAY
397#define WRITE_DOUBLE( offset, data ) \
398 WRITE_DATA( offset, GLdouble, data )
399#else
400# ifndef IN_RING0
401# define WRITE_DOUBLE( offset, data ) \
402 crWriteUnalignedDouble( data_ptr + (offset), (data) )
403# else
404# define WRITE_DOUBLE( offset, data ) \
405 AssertReleaseFailed()
406# endif
407#endif
408
409#ifndef IN_RING0
410#define WRITE_SWAPPED_DOUBLE( offset, data ) \
411 crWriteSwappedDouble( data_ptr + (offset), (data) )
412#else
413#define WRITE_SWAPPED_DOUBLE( offset, data ) \
414 AssertReleaseFailed()
415#endif
416
417#define WRITE_OPCODE( pc, opcode ) \
418 *(pc->buffer.opcode_current--) = (unsigned char) opcode
419
420#define WRITE_NETWORK_POINTER( offset, data ) \
421 crNetworkPointerWrite( (CRNetworkPointer *) ( data_ptr + (offset) ), (data) )
422
423#ifdef __cplusplus
424}
425#endif
426
427#endif /* CR_PACK_H */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette