VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/packer/pack_buffer.c@ 53592

Last change on this file since 53592 was 46757, checked in by vboxsync, 11 years ago

wddm/crOpenGL: r0-based visible regions handling, r0-based chromium commands submission debugged, more on new presentation mechanism, cleanup, etc.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.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#include "cr_mem.h"
8#include "cr_string.h"
9#include "packer.h"
10#include "cr_error.h"
11#include "cr_protocol.h"
12#ifndef IN_RING0
13#include "cr_unpack.h"
14#endif
15
16#ifndef IN_RING0
17void crWriteUnalignedDouble( void *buffer, double d )
18{
19 unsigned int *ui = (unsigned int *) buffer;
20 ui[0] = ((unsigned int *) &d)[0];
21 ui[1] = ((unsigned int *) &d)[1];
22}
23
24void crWriteSwappedDouble( void *buffer, double d )
25{
26 unsigned int *ui = (unsigned int *) buffer;
27 ui[0] = SWAP32(((unsigned int *) &d)[1]);
28 ui[1] = SWAP32(((unsigned int *) &d)[0]);
29}
30
31double crReadUnalignedDouble( const void *buffer )
32{
33 const unsigned int *ui = (unsigned int *) buffer;
34 double d;
35 ((unsigned int *) &d)[0] = ui[0];
36 ((unsigned int *) &d)[1] = ui[1];
37 return d;
38}
39#endif
40/*
41 * We need the packer to run as efficiently as possible. To avoid one
42 * pointer dereference from the CRPackContext to the current CRPackBuffer,
43 * we keep a _copy_ of the current CRPackBuffer in the CRPackContext and
44 * operate on the fields in CRPackContext, rather than the CRPackBuffer.
45 *
46 * To keep things in sync, when we change a context's
47 * buffer, we have to use the crPackSet/GetBuffer() functions.
48 */
49
50void crPackSetBuffer( CRPackContext *pc, CRPackBuffer *buffer )
51{
52 CRASSERT( pc );
53 CRASSERT( buffer );
54
55 if (pc->currentBuffer == buffer)
56 return; /* re-bind is no-op */
57
58 if (pc->currentBuffer) {
59 /* Another buffer currently bound to this packer (shouldn't normally occur)
60 * Release it. Fixes Ensight issue.
61 */
62 crPackReleaseBuffer(pc);
63 }
64
65 CRASSERT( pc->currentBuffer == NULL); /* release if NULL? */
66 CRASSERT( buffer->context == NULL );
67
68 /* bind context to buffer */
69 pc->currentBuffer = buffer;
70 buffer->context = pc;
71
72 /* update the context's packing fields with those from the buffer */
73 pc->buffer = *buffer; /* struct copy */
74}
75
76#ifndef IN_RING0
77/* This is useful for debugging packer problems */
78void crPackSetBufferDEBUG( const char *file, int line,
79 CRPackContext *pc, CRPackBuffer *buffer)
80
81{
82 crPackSetBuffer( pc, buffer );
83 /* record debugging info */
84 pc->file = crStrdup(file);
85 pc->line = line;
86}
87#endif
88
89/*
90 * Release the buffer currently attached to the context.
91 * Update/resync data structures.
92 */
93void crPackReleaseBuffer( CRPackContext *pc )
94{
95 CRPackBuffer *buf;
96 CRASSERT( pc );
97
98 if (!pc->currentBuffer) {
99 crWarning("crPackReleaseBuffer called with no current buffer");
100 return; /* nothing to do */
101 }
102
103 CRASSERT( pc->currentBuffer->context == pc );
104
105 /* buffer to release */
106 buf = pc->currentBuffer;
107
108 /* copy context's fields back into the buffer to update it */
109 *buf = pc->buffer; /* struct copy */
110
111 /* unbind buffer from context */
112 buf->context = NULL;
113 pc->currentBuffer = NULL;
114
115 /* zero-out context's packing fields just to be safe */
116 crMemZero(&(pc->buffer), sizeof(pc->buffer));
117
118 /* update the debugging fields */
119 if (pc->file)
120 crFree(pc->file);
121 pc->file = NULL;
122 pc->line = -1;
123}
124
125void crPackFlushFunc( CRPackContext *pc, CRPackFlushFunc ff )
126{
127 pc->Flush = ff;
128}
129
130void crPackFlushArg( CRPackContext *pc, void *flush_arg )
131{
132 pc->flush_arg = flush_arg;
133}
134
135void crPackSendHugeFunc( CRPackContext *pc, CRPackSendHugeFunc shf )
136{
137 pc->SendHuge = shf;
138}
139
140/*
141 * This basically resets the buffer attached to <pc> to the default, empty
142 * state.
143 */
144void crPackResetPointers( CRPackContext *pc )
145{
146 const GLboolean geom_only = pc->buffer.geometry_only; /* save this flag */
147 const GLboolean holds_BeginEnd = pc->buffer.holds_BeginEnd;
148 const GLboolean in_BeginEnd = pc->buffer.in_BeginEnd;
149 const GLboolean canBarf = pc->buffer.canBarf;
150 CRPackBuffer *buf = pc->currentBuffer;
151 CRASSERT(buf);
152 crPackInitBuffer( buf, buf->pack, buf->size, buf->mtu
153#ifdef IN_RING0
154 , 0
155#endif
156 );
157 pc->buffer.geometry_only = geom_only; /* restore the flag */
158 pc->buffer.holds_BeginEnd = holds_BeginEnd;
159 pc->buffer.in_BeginEnd = in_BeginEnd;
160 pc->buffer.canBarf = canBarf;
161}
162
163
164/**
165 * Return max number of opcodes that'll fit in the given buffer size.
166 * Each opcode has at least a 1-word payload, so opcodes can occupy at most
167 * 20% of the space.
168 */
169int
170crPackMaxOpcodes( int buffer_size )
171{
172 int n = ( buffer_size - sizeof(CRMessageOpcodes) ) / 5;
173 /* Don't forget to add one here in case the buffer size is not
174 * divisible by 4. Thanks to Ken Moreland for finding this.
175 */
176 n++;
177 /* round up to multiple of 4 */
178 n = (n + 0x3) & (~0x3);
179 return n;
180}
181
182
183/**
184 * Return max number of data bytes that'll fit in the given buffer size.
185 */
186int
187crPackMaxData( int buffer_size )
188{
189 int n = buffer_size - sizeof(CRMessageOpcodes);
190 n -= crPackMaxOpcodes(buffer_size);
191 return n;
192}
193
194
195/**
196 * Initialize the given CRPackBuffer object.
197 * The buffer may or may not be currently bound to a CRPackContext.
198 *
199 * Opcodes and operands are packed into a buffer in a special way.
200 * Opcodes start at opcode_start and go downward in memory while operands
201 * start at data_start and go upward in memory. The buffer is full when we
202 * either run out of opcode space or operand space.
203 *
204 * Diagram (memory addresses increase upward):
205 *
206 * data_end -> | | <- buf->pack + buf->size
207 * +---------+
208 * | |
209 * | |
210 * | operands|
211 * | |
212 * | |
213 * data_start -> +---------+
214 * opcode_start -> | |
215 * | |
216 * | opcodes |
217 * | |
218 * | |
219 * opcode_end -> +---------+ <- buf->pack
220 *
221 * \param buf the CRPackBuffer to initialize
222 * \param pack the address of the buffer for packing opcodes/operands.
223 * \param size size of the buffer, in bytes
224 * \param mtu max transmission unit size, in bytes. When the buffer
225 * has 'mtu' bytes in it, we have to send it. The MTU might
226 * be somewhat smaller than the buffer size.
227 */
228void crPackInitBuffer( CRPackBuffer *buf, void *pack, int size, int mtu
229#ifdef IN_RING0
230 , unsigned int num_opcodes
231#endif
232 )
233{
234#ifndef IN_RING0
235 unsigned int num_opcodes;
236#endif
237
238 CRASSERT(mtu <= size);
239
240 buf->size = size;
241 buf->mtu = mtu;
242 buf->pack = pack;
243
244#ifdef IN_RING0
245 if(num_opcodes)
246 {
247 num_opcodes = (num_opcodes + 0x3) & (~0x3);
248 }
249 else
250#endif
251 {
252 num_opcodes = crPackMaxOpcodes( buf->size );
253 }
254
255 buf->data_start =
256 (unsigned char *) buf->pack + num_opcodes + sizeof(CRMessageOpcodes);
257 buf->data_current = buf->data_start;
258 buf->data_end = (unsigned char *) buf->pack + buf->size;
259
260 buf->opcode_start = buf->data_start - 1;
261 buf->opcode_current = buf->opcode_start;
262 buf->opcode_end = buf->opcode_start - num_opcodes;
263
264 buf->geometry_only = GL_FALSE;
265 buf->holds_BeginEnd = GL_FALSE;
266 buf->in_BeginEnd = GL_FALSE;
267 buf->canBarf = GL_FALSE;
268
269 if (buf->context) {
270 /* Also reset context's packing fields */
271 CRPackContext *pc = buf->context;
272 CRASSERT(pc->currentBuffer == buf);
273 /*crMemcpy( &(pc->buffer), buf, sizeof(*buf) );*/
274 pc->buffer = *buf;
275 }
276}
277
278
279int crPackCanHoldBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *src )
280{
281 const int num_data = crPackNumData(src);
282 const int num_opcode = crPackNumOpcodes(src);
283 int res;
284 CR_GET_PACKER_CONTEXT(pc);
285 CR_LOCK_PACKER_CONTEXT(pc);
286 res = crPackCanHoldOpcode( pc, num_opcode, num_data );
287 CR_UNLOCK_PACKER_CONTEXT(pc);
288 return res;
289}
290
291
292int crPackCanHoldBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *src )
293{
294 const int len_aligned = (src->data_current - src->opcode_current - 1 + 3) & ~3;
295 CR_GET_PACKER_CONTEXT(pc);
296 /* 24 is the size of the bounds-info packet... */
297 return crPackCanHoldOpcode( pc, 1, len_aligned + 24 );
298}
299
300void crPackAppendBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *src )
301{
302 CR_GET_PACKER_CONTEXT(pc);
303 const int num_data = crPackNumData(src);
304 const int num_opcode = crPackNumOpcodes(src);
305
306 CRASSERT(num_data >= 0);
307 CRASSERT(num_opcode >= 0);
308
309 CR_LOCK_PACKER_CONTEXT(pc);
310
311 /* don't append onto ourself! */
312 CRASSERT(pc->currentBuffer);
313 CRASSERT(pc->currentBuffer != src);
314
315 if (!crPackCanHoldBuffer(CR_PACKER_CONTEXT_ARG src))
316 {
317 if (src->holds_BeginEnd)
318 {
319 crWarning( "crPackAppendBuffer: overflowed the destination!" );
320 CR_UNLOCK_PACKER_CONTEXT(pc);
321 return;
322 }
323 else
324 {
325 crError( "crPackAppendBuffer: overflowed the destination!" );
326 CR_UNLOCK_PACKER_CONTEXT(pc);
327 }
328 }
329
330 /* Copy the buffer data/operands which are at the head of the buffer */
331 crMemcpy( pc->buffer.data_current, src->data_start, num_data );
332 pc->buffer.data_current += num_data;
333
334 /* Copy the buffer opcodes which are at the tail of the buffer */
335 CRASSERT( pc->buffer.opcode_current - num_opcode >= pc->buffer.opcode_end );
336 crMemcpy( pc->buffer.opcode_current + 1 - num_opcode, src->opcode_current + 1,
337 num_opcode );
338 pc->buffer.opcode_current -= num_opcode;
339 pc->buffer.holds_BeginEnd |= src->holds_BeginEnd;
340 pc->buffer.in_BeginEnd = src->in_BeginEnd;
341 pc->buffer.holds_List |= src->holds_List;
342 CR_UNLOCK_PACKER_CONTEXT(pc);
343}
344
345
346void
347crPackAppendBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *src, const CRrecti *bounds )
348{
349 CR_GET_PACKER_CONTEXT(pc);
350 const GLbyte *payload = (const GLbyte *) src->opcode_current + 1;
351 const int num_opcodes = crPackNumOpcodes(src);
352 const int length = src->data_current - src->opcode_current - 1;
353
354 CRASSERT(pc);
355 CR_LOCK_PACKER_CONTEXT(pc);
356 CRASSERT(pc->currentBuffer);
357 CRASSERT(pc->currentBuffer != src);
358
359 /*
360 * payload points to the block of opcodes immediately followed by operands.
361 */
362
363 if ( !crPackCanHoldBoundedBuffer( CR_PACKER_CONTEXT_ARG src ) )
364 {
365 if (src->holds_BeginEnd)
366 {
367 crWarning( "crPackAppendBoundedBuffer: overflowed the destination!" );
368 CR_UNLOCK_PACKER_CONTEXT(pc);
369 return;
370 }
371 else
372 {
373 crError( "crPackAppendBoundedBuffer: overflowed the destination!" );
374 CR_UNLOCK_PACKER_CONTEXT(pc);
375 }
376 }
377
378 if (pc->swapping)
379 crPackBoundsInfoCRSWAP( CR_PACKER_CONTEXT_ARG bounds, payload, length, num_opcodes );
380 else
381 crPackBoundsInfoCR( CR_PACKER_CONTEXT_ARG bounds, payload, length, num_opcodes );
382
383 pc->buffer.holds_BeginEnd |= src->holds_BeginEnd;
384 pc->buffer.in_BeginEnd = src->in_BeginEnd;
385 pc->buffer.holds_List |= src->holds_List;
386 CR_UNLOCK_PACKER_CONTEXT(pc);
387}
388
389
390#ifndef CHROMIUM_THREADSAFE
391static unsigned char *sanityCheckPointer = NULL;
392#endif
393
394
395/*
396 * Allocate space for a command that might be very large, such as
397 * glTexImage2D or glBufferDataARB call.
398 * The command buffer _MUST_ then be transmitted by calling crHugePacket.
399 */
400void *crPackAlloc( CR_PACKER_CONTEXT_ARGDECL unsigned int size )
401{
402 CR_GET_PACKER_CONTEXT(pc);
403 unsigned char *data_ptr;
404
405 /* include space for the length and make the payload word-aligned */
406 size = ( size + sizeof(unsigned int) + 0x3 ) & ~0x3;
407
408 CR_LOCK_PACKER_CONTEXT(pc);
409
410 if ( crPackCanHoldOpcode( pc, 1, size ) )
411 {
412 /* we can just put it in the current buffer */
413 CR_GET_BUFFERED_POINTER_NOLOCK(pc, size ); /* NOTE: this sets data_ptr */
414 }
415 else
416 {
417 /* Okay, it didn't fit. Maybe it will after we flush. */
418 CR_UNLOCK_PACKER_CONTEXT(pc);
419 pc->Flush( pc->flush_arg );
420 CR_LOCK_PACKER_CONTEXT(pc);
421 if ( crPackCanHoldOpcode( pc, 1, size ) )
422 {
423 CR_GET_BUFFERED_POINTER_NOLOCK(pc, size ); /* NOTE: this sets data_ptr */
424 }
425 else
426 {
427 /* It's really way too big, so allocate a temporary packet
428 * with space for the single opcode plus the payload &
429 * header.
430 */
431 data_ptr = (unsigned char *)
432 crAlloc( sizeof(CRMessageOpcodes) + 4 + size );
433
434 /* skip the header & opcode space */
435 data_ptr += sizeof(CRMessageOpcodes) + 4;
436 }
437 }
438
439 /* At the top of the function, we added four to the request size and
440 * rounded it up to the next multiple of four.
441 *
442 * At this point, we have:
443 *
444 * HIGH MEM | byte size - 1 | \
445 * ... |
446 * ... | - original 'size' bytes for data
447 * | operand data | |
448 * return value -> | operand data | /
449 * | byte 3 | \
450 * | byte 2 | |- These bytes will store 'size'
451 * | byte 1 | |
452 * data_ptr -> | byte 0 | /
453 * | CR opcode | <- Set in packspuHuge()
454 * | unused |
455 * | unused |
456 * | unused |
457 * | CRMessageOpcodes |
458 * | CRMessageOpcodes |
459 * ...
460 * | CRMessageOpcodes |
461 * | CRMessageOpcodes |
462 * LOW MEM +------------------+
463 */
464
465 if (pc->swapping)
466 {
467 *((unsigned int *) data_ptr) = SWAP32(size);
468 crDebug( "Just swapped the length, putting %d on the wire!", *((unsigned int *) data_ptr));
469 }
470 else
471 {
472 *((unsigned int *) data_ptr) = size;
473 }
474#ifndef CHROMIUM_THREADSAFE
475 sanityCheckPointer = data_ptr + 4;
476#endif
477 return data_ptr + 4;
478}
479
480#define IS_BUFFERED( packet ) \
481 ((unsigned char *) (packet) >= pc->buffer.data_start && \
482 (unsigned char *) (packet) < pc->buffer.data_end)
483
484
485/*
486 * Transmit a packet which was allocated with crPackAlloc.
487 */
488void crHugePacket( CR_PACKER_CONTEXT_ARGDECL CROpcode opcode, void *packet )
489{
490 CR_GET_PACKER_CONTEXT(pc);
491#ifndef CHROMIUM_THREADSAFE
492 CRASSERT(sanityCheckPointer == packet);
493 sanityCheckPointer = NULL;
494#endif
495
496 if ( IS_BUFFERED( packet ) )
497 WRITE_OPCODE( pc, opcode );
498 else
499 pc->SendHuge( opcode, packet );
500}
501
502void crPackFree( CR_PACKER_CONTEXT_ARGDECL void *packet )
503{
504 CR_GET_PACKER_CONTEXT(pc);
505
506 if ( IS_BUFFERED( packet ) )
507 {
508 CR_UNLOCK_PACKER_CONTEXT(pc);
509 return;
510 }
511
512 CR_UNLOCK_PACKER_CONTEXT(pc);
513
514 /* the pointer passed in doesn't include the space for the single
515 * opcode (4 bytes because of the alignment requirement) or the
516 * length field or the header */
517 crFree( (unsigned char *) packet - 8 - sizeof(CRMessageOpcodes) );
518}
519
520void crNetworkPointerWrite( CRNetworkPointer *dst, void *src )
521{
522 /* init CRNetworkPointer with invalid values */
523 dst->ptrAlign[0] = 0xDeadBeef;
524 dst->ptrAlign[1] = 0xCafeBabe;
525 /* copy the pointer's value into the CRNetworkPointer */
526 crMemcpy( dst, &src, sizeof(src) );
527
528 /* if either assertion fails, it probably means that a packer function
529 * (which returns a value) was called without setting up the writeback
530 * pointer, or something like that.
531 */
532 CRASSERT(dst->ptrAlign[0] != 0xffffffff);
533 CRASSERT(dst->ptrAlign[0] != 0xDeadBeef);
534}
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