VirtualBox

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

Last change on this file since 39847 was 39265, checked in by vboxsync, 13 years ago

crOpenGL: flush on glEnd if some begin-end data was sent to host already - fix Win8+WinSAT halt

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.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
53typedef enum
54{
55 CRPackBeginEndStateNone = 0, /* not in begin end */
56 CRPackBeginEndStateStarted, /* begin issued */
57 CRPackBeginEndStateFlushDone /* begin issued & buffer flash is done thus part of commands is issued to host */
58} CRPackBeginEndState;
59/**
60 * Packer context
61 */
62struct CRPackContext_t
63{
64 CRPackBuffer buffer; /**< not a pointer, see comments in pack_buffer.c */
65 CRPackFlushFunc Flush;
66 void *flush_arg;
67 CRPackSendHugeFunc SendHuge;
68 CRPackErrorHandlerFunc Error;
69 CRCurrentStatePointers current;
70 CRPackBeginEndState enmBeginEndState;
71 GLvectorf bounds_min, bounds_max;
72 int updateBBOX;
73 int swapping;
74 CRPackBuffer *currentBuffer;
75 CRmutex mutex;
76 char *file; /**< for debugging only */
77 int line; /**< for debugging only */
78};
79
80
81extern DECLEXPORT(CRPackContext *) crPackNewContext(int swapping);
82extern DECLEXPORT(void) crPackDeleteContext(CRPackContext *pc);
83extern DECLEXPORT(void) crPackSetContext( CRPackContext *pc );
84extern DECLEXPORT(CRPackContext *) crPackGetContext( void );
85
86extern DECLEXPORT(void) crPackSetBuffer( CRPackContext *pc, CRPackBuffer *buffer );
87extern DECLEXPORT(void) crPackSetBufferDEBUG( const char *file, int line, CRPackContext *pc, CRPackBuffer *buffer );
88extern DECLEXPORT(void) crPackReleaseBuffer( CRPackContext *pc );
89extern DECLEXPORT(void) crPackResetPointers( CRPackContext *pc );
90
91extern DECLEXPORT(int) crPackMaxOpcodes( int buffer_size );
92extern DECLEXPORT(int) crPackMaxData( int buffer_size );
93extern DECLEXPORT(void) crPackInitBuffer( CRPackBuffer *buffer, void *buf, int size, int mtu );
94extern DECLEXPORT(void) crPackFlushFunc( CRPackContext *pc, CRPackFlushFunc ff );
95extern DECLEXPORT(void) crPackFlushArg( CRPackContext *pc, void *flush_arg );
96extern DECLEXPORT(void) crPackSendHugeFunc( CRPackContext *pc, CRPackSendHugeFunc shf );
97extern DECLEXPORT(void) crPackErrorFunction( CRPackContext *pc, CRPackErrorHandlerFunc errf );
98extern DECLEXPORT(void) crPackOffsetCurrentPointers( int offset );
99extern DECLEXPORT(void) crPackNullCurrentPointers( void );
100
101extern DECLEXPORT(void) crPackResetBoundingBox( CRPackContext *pc );
102extern DECLEXPORT(GLboolean) crPackGetBoundingBox( CRPackContext *pc,
103 GLfloat *xmin, GLfloat *ymin, GLfloat *zmin,
104 GLfloat *xmax, GLfloat *ymax, GLfloat *zmax);
105
106extern DECLEXPORT(void) crPackAppendBuffer( const CRPackBuffer *buffer );
107extern DECLEXPORT(void) crPackAppendBoundedBuffer( const CRPackBuffer *buffer, const CRrecti *bounds );
108extern DECLEXPORT(int) crPackCanHoldBuffer( const CRPackBuffer *buffer );
109extern DECLEXPORT(int) crPackCanHoldBoundedBuffer( const CRPackBuffer *buffer );
110
111#if defined(LINUX) || defined(WINDOWS)
112#define CR_UNALIGNED_ACCESS_OKAY
113#else
114#undef CR_UNALIGNED_ACCESS_OKAY
115#endif
116extern DECLEXPORT(void) crWriteUnalignedDouble( void *buffer, double d );
117extern DECLEXPORT(void) crWriteSwappedDouble( void *buffer, double d );
118
119extern DECLEXPORT(void) *crPackAlloc( unsigned int len );
120extern DECLEXPORT(void) crHugePacket( CROpcode op, void *ptr );
121extern DECLEXPORT(void) crPackFree( void *ptr );
122extern DECLEXPORT(void) crNetworkPointerWrite( CRNetworkPointer *, void * );
123
124extern DECLEXPORT(void) crPackExpandDrawArrays(GLenum mode, GLint first, GLsizei count, CRClientState *c);
125extern DECLEXPORT(void) crPackExpandDrawArraysSWAP(GLenum mode, GLint first, GLsizei count, CRClientState *c);
126
127extern DECLEXPORT(void) crPackUnrollDrawElements(GLsizei count, GLenum type, const GLvoid *indices);
128extern DECLEXPORT(void) crPackUnrollDrawElementsSWAP(GLsizei count, GLenum type, const GLvoid *indices);
129
130extern DECLEXPORT(void) crPackExpandDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
131extern DECLEXPORT(void) crPackExpandDrawElementsSWAP(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
132
133extern DECLEXPORT(void) crPackExpandDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
134extern DECLEXPORT(void) crPackExpandDrawRangeElementsSWAP(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c);
135
136extern DECLEXPORT(void) crPackExpandArrayElement(GLint index, CRClientState *c);
137extern DECLEXPORT(void) crPackExpandArrayElementSWAP(GLint index, CRClientState *c);
138
139extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXT( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c );
140extern DECLEXPORT(void) crPackExpandMultiDrawArraysEXTSWAP( GLenum mode, GLint *first, GLsizei *count, GLsizei primcount, CRClientState *c );
141
142extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXT( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c );
143extern DECLEXPORT(void) crPackExpandMultiDrawElementsEXTSWAP( GLenum mode, const GLsizei *count, GLenum type, const GLvoid **indices, GLsizei primcount, CRClientState *c );
144
145
146#ifdef CHROMIUM_THREADSAFE
147extern CRtsd _PackerTSD;
148#define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = (CRPackContext *) crGetTSD(&_PackerTSD)
149#define CR_LOCK_PACKER_CONTEXT(PC) crLockMutex(&((PC)->mutex))
150#define CR_UNLOCK_PACKER_CONTEXT(PC) crUnlockMutex(&((PC)->mutex))
151#else
152extern DLLDATA(CRPackContext) cr_packer_globals;
153#define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = &cr_packer_globals
154#define CR_LOCK_PACKER_CONTEXT(PC)
155#define CR_UNLOCK_PACKER_CONTEXT(PC)
156#endif
157
158
159/**
160 * Return number of opcodes in given buffer.
161 */
162static INLINE int
163crPackNumOpcodes(const CRPackBuffer *buffer)
164{
165 CRASSERT(buffer->opcode_start - buffer->opcode_current >= 0);
166 return buffer->opcode_start - buffer->opcode_current;
167}
168
169
170/**
171 * Return amount of data (in bytes) in buffer.
172 */
173static INLINE int
174crPackNumData(const CRPackBuffer *buffer)
175{
176 CRASSERT(buffer->data_current - buffer->data_start >= 0);
177 return buffer->data_current - buffer->data_start; /* in bytes */
178}
179
180
181static INLINE int
182crPackCanHoldOpcode(const CRPackContext *pc, int num_opcode, int num_data)
183{
184 int fitsInMTU, opcodesFit, dataFits;
185
186 CRASSERT(pc->currentBuffer);
187
188 fitsInMTU = (((pc->buffer.data_current - pc->buffer.opcode_current - 1
189 + num_opcode + num_data
190 + 0x3 ) & ~0x3) + sizeof(CRMessageOpcodes)
191 <= pc->buffer.mtu);
192 opcodesFit = (pc->buffer.opcode_current - num_opcode >= pc->buffer.opcode_end);
193 dataFits = (pc->buffer.data_current + num_data <= pc->buffer.data_end);
194
195 return fitsInMTU && opcodesFit && dataFits;
196}
197
198
199/**
200 * Alloc space for a message of 'len' bytes (plus 1 opcode).
201 * Only flush if buffer is full.
202 */
203#define CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH(pc, len, lock) \
204 do { \
205 THREADASSERT( pc ); \
206 if (lock) CR_LOCK_PACKER_CONTEXT(pc); \
207 CRASSERT( pc->currentBuffer ); \
208 if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
209 pc->Flush( pc->flush_arg ); \
210 CRASSERT(crPackCanHoldOpcode( pc, 1, (len) ) ); \
211 if (pc->enmBeginEndState == CRPackBeginEndStateStarted) { \
212 pc->enmBeginEndState = CRPackBeginEndStateFlushDone; \
213 } \
214 } \
215 data_ptr = pc->buffer.data_current; \
216 pc->buffer.data_current += (len); \
217 } while (0)
218
219
220/**
221 * As above, flush if the buffer contains vertex data and we're
222 * no longer inside glBegin/glEnd.
223 */
224#define CR_GET_BUFFERED_POINTER( pc, len ) \
225 do { \
226 CR_LOCK_PACKER_CONTEXT(pc); \
227 CRASSERT( pc->currentBuffer ); \
228 if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
229 CRASSERT( 0 ); /* should never be here currently */ \
230 pc->Flush( pc->flush_arg ); \
231 pc->buffer.holds_BeginEnd = 0; \
232 } \
233 CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
234 } while (0)
235
236/**
237 * As above, but without lock.
238 */
239#define CR_GET_BUFFERED_POINTER_NOLOCK( pc, len ) \
240 do { \
241 CRASSERT( pc->currentBuffer ); \
242 if ( pc->buffer.holds_BeginEnd && !pc->buffer.in_BeginEnd ) { \
243 CRASSERT( 0 ); /* should never be here currently */ \
244 pc->Flush( pc->flush_arg ); \
245 pc->buffer.holds_BeginEnd = 0; \
246 } \
247 CR_GET_BUFFERED_POINTER_NO_BEGINEND_FLUSH( pc, len, GL_FALSE ); \
248 } while (0)
249
250
251/**
252 * As above, but for vertex data between glBegin/End (counts vertices).
253 */
254#define CR_GET_BUFFERED_COUNT_POINTER( pc, len ) \
255 do { \
256 CR_LOCK_PACKER_CONTEXT(pc); \
257 CRASSERT( pc->currentBuffer ); \
258 if ( !crPackCanHoldOpcode( pc, 1, (len) ) ) { \
259 pc->Flush( pc->flush_arg ); \
260 CRASSERT( crPackCanHoldOpcode( pc, 1, (len) ) ); \
261 if (pc->enmBeginEndState == CRPackBeginEndStateStarted) { \
262 pc->enmBeginEndState = CRPackBeginEndStateFlushDone; \
263 } \
264 } \
265 data_ptr = pc->buffer.data_current; \
266 pc->current.vtx_count++; \
267 pc->buffer.data_current += (len); \
268 } while (0)
269
270
271/**
272 * Allocate space for a msg/command that has no arguments, such
273 * as glFinish().
274 */
275#define CR_GET_BUFFERED_POINTER_NO_ARGS( pc ) \
276 CR_GET_BUFFERED_POINTER( pc, 4 ); \
277 WRITE_DATA( 0, GLuint, 0xdeadbeef )
278
279#define WRITE_DATA( offset, type, data ) \
280 *( (type *) (data_ptr + (offset))) = (data)
281
282/* Write data to current location and auto increment */
283#define WRITE_DATA_AI(type, data) \
284 { \
285 *((type*) (data_ptr)) = (data); \
286 data_ptr += sizeof(type); \
287 }
288
289#ifdef CR_UNALIGNED_ACCESS_OKAY
290#define WRITE_DOUBLE( offset, data ) \
291 WRITE_DATA( offset, GLdouble, data )
292#else
293#define WRITE_DOUBLE( offset, data ) \
294 crWriteUnalignedDouble( data_ptr + (offset), (data) )
295#endif
296
297#define WRITE_SWAPPED_DOUBLE( offset, data ) \
298 crWriteSwappedDouble( data_ptr + (offset), (data) )
299
300#define WRITE_OPCODE( pc, opcode ) \
301 *(pc->buffer.opcode_current--) = (unsigned char) opcode
302
303#define WRITE_NETWORK_POINTER( offset, data ) \
304 crNetworkPointerWrite( (CRNetworkPointer *) ( data_ptr + (offset) ), (data) )
305
306#ifdef __cplusplus
307}
308#endif
309
310#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