VirtualBox

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

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

wddm/crOpenGL: moreon kernel-based cr commands submission

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