VirtualBox

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

Last change on this file since 46086 was 43487, checked in by vboxsync, 12 years ago

crOpenGL/wddm: basics for miniport-based visible rects processing (guest part); export flushToHost command

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