VirtualBox

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

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

crOpenGL,wined3d,wddm: second part of fixing resource leakage, basics for cr commands submission from r0 miniport driver (for r0 visible region reporting, WPF 3D rendering fixes w/o Aero, etc.)

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