VirtualBox

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

Last change on this file since 78341 was 78341, checked in by vboxsync, 6 years ago

Config.kmk,Additions/common/crOpenGL,VBox/GuestHost/OpenGL,HostServices/SharedOpenGL: Remove CHROMIUM_THREADSAFE define and apply the current default

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