VirtualBox

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

Last change on this file since 70939 was 69474, checked in by vboxsync, 7 years ago

*: scm updates - header files should have 'svn:keywords=Id Revision' too (doesn't mean they have to use them).

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

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