VirtualBox

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

Last change on this file since 42028 was 42028, checked in by vboxsync, 12 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: 14.2 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 pc->buffer.geometry_only = geom_only; /* restore the flag */
154 pc->buffer.holds_BeginEnd = holds_BeginEnd;
155 pc->buffer.in_BeginEnd = in_BeginEnd;
156 pc->buffer.canBarf = canBarf;
157}
158
159
160/**
161 * Return max number of opcodes that'll fit in the given buffer size.
162 * Each opcode has at least a 1-word payload, so opcodes can occupy at most
163 * 20% of the space.
164 */
165int
166crPackMaxOpcodes( int buffer_size )
167{
168 int n = ( buffer_size - sizeof(CRMessageOpcodes) ) / 5;
169 /* Don't forget to add one here in case the buffer size is not
170 * divisible by 4. Thanks to Ken Moreland for finding this.
171 */
172 n++;
173 /* round up to multiple of 4 */
174 n = (n + 0x3) & (~0x3);
175 return n;
176}
177
178
179/**
180 * Return max number of data bytes that'll fit in the given buffer size.
181 */
182int
183crPackMaxData( int buffer_size )
184{
185 int n = buffer_size - sizeof(CRMessageOpcodes);
186 n -= crPackMaxOpcodes(buffer_size);
187 return n;
188}
189
190
191/**
192 * Initialize the given CRPackBuffer object.
193 * The buffer may or may not be currently bound to a CRPackContext.
194 *
195 * Opcodes and operands are packed into a buffer in a special way.
196 * Opcodes start at opcode_start and go downward in memory while operands
197 * start at data_start and go upward in memory. The buffer is full when we
198 * either run out of opcode space or operand space.
199 *
200 * Diagram (memory addresses increase upward):
201 *
202 * data_end -> | | <- buf->pack + buf->size
203 * +---------+
204 * | |
205 * | |
206 * | operands|
207 * | |
208 * | |
209 * data_start -> +---------+
210 * opcode_start -> | |
211 * | |
212 * | opcodes |
213 * | |
214 * | |
215 * opcode_end -> +---------+ <- buf->pack
216 *
217 * \param buf the CRPackBuffer to initialize
218 * \param pack the address of the buffer for packing opcodes/operands.
219 * \param size size of the buffer, in bytes
220 * \param mtu max transmission unit size, in bytes. When the buffer
221 * has 'mtu' bytes in it, we have to send it. The MTU might
222 * be somewhat smaller than the buffer size.
223 */
224void crPackInitBuffer( CRPackBuffer *buf, void *pack, int size, int mtu )
225{
226 unsigned int num_opcodes;
227
228 CRASSERT(mtu <= size);
229
230 buf->size = size;
231 buf->mtu = mtu;
232 buf->pack = pack;
233
234 num_opcodes = crPackMaxOpcodes( buf->size );
235
236 buf->data_start =
237 (unsigned char *) buf->pack + num_opcodes + sizeof(CRMessageOpcodes);
238 buf->data_current = buf->data_start;
239 buf->data_end = (unsigned char *) buf->pack + buf->size;
240
241 buf->opcode_start = buf->data_start - 1;
242 buf->opcode_current = buf->opcode_start;
243 buf->opcode_end = buf->opcode_start - num_opcodes;
244
245 buf->geometry_only = GL_FALSE;
246 buf->holds_BeginEnd = GL_FALSE;
247 buf->in_BeginEnd = GL_FALSE;
248 buf->canBarf = GL_FALSE;
249
250 if (buf->context) {
251 /* Also reset context's packing fields */
252 CRPackContext *pc = buf->context;
253 CRASSERT(pc->currentBuffer == buf);
254 /*crMemcpy( &(pc->buffer), buf, sizeof(*buf) );*/
255 pc->buffer = *buf;
256 }
257}
258
259
260int crPackCanHoldBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *src )
261{
262 const int num_data = crPackNumData(src);
263 const int num_opcode = crPackNumOpcodes(src);
264 int res;
265 CR_GET_PACKER_CONTEXT(pc);
266 CR_LOCK_PACKER_CONTEXT(pc);
267 res = crPackCanHoldOpcode( pc, num_opcode, num_data );
268 CR_UNLOCK_PACKER_CONTEXT(pc);
269 return res;
270}
271
272
273int crPackCanHoldBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *src )
274{
275 const int len_aligned = (src->data_current - src->opcode_current - 1 + 3) & ~3;
276 CR_GET_PACKER_CONTEXT(pc);
277 /* 24 is the size of the bounds-info packet... */
278 return crPackCanHoldOpcode( pc, 1, len_aligned + 24 );
279}
280
281void crPackAppendBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *src )
282{
283 CR_GET_PACKER_CONTEXT(pc);
284 const int num_data = crPackNumData(src);
285 const int num_opcode = crPackNumOpcodes(src);
286
287 CRASSERT(num_data >= 0);
288 CRASSERT(num_opcode >= 0);
289
290 CR_LOCK_PACKER_CONTEXT(pc);
291
292 /* don't append onto ourself! */
293 CRASSERT(pc->currentBuffer);
294 CRASSERT(pc->currentBuffer != src);
295
296 if (!crPackCanHoldBuffer(CR_PACKER_CONTEXT_ARG src))
297 {
298 if (src->holds_BeginEnd)
299 {
300 crWarning( "crPackAppendBuffer: overflowed the destination!" );
301 CR_UNLOCK_PACKER_CONTEXT(pc);
302 return;
303 }
304 else
305 {
306 crError( "crPackAppendBuffer: overflowed the destination!" );
307 CR_UNLOCK_PACKER_CONTEXT(pc);
308 }
309 }
310
311 /* Copy the buffer data/operands which are at the head of the buffer */
312 crMemcpy( pc->buffer.data_current, src->data_start, num_data );
313 pc->buffer.data_current += num_data;
314
315 /* Copy the buffer opcodes which are at the tail of the buffer */
316 CRASSERT( pc->buffer.opcode_current - num_opcode >= pc->buffer.opcode_end );
317 crMemcpy( pc->buffer.opcode_current + 1 - num_opcode, src->opcode_current + 1,
318 num_opcode );
319 pc->buffer.opcode_current -= num_opcode;
320 pc->buffer.holds_BeginEnd |= src->holds_BeginEnd;
321 pc->buffer.in_BeginEnd = src->in_BeginEnd;
322 pc->buffer.holds_List |= src->holds_List;
323 CR_UNLOCK_PACKER_CONTEXT(pc);
324}
325
326
327void
328crPackAppendBoundedBuffer( CR_PACKER_CONTEXT_ARGDECL const CRPackBuffer *src, const CRrecti *bounds )
329{
330 CR_GET_PACKER_CONTEXT(pc);
331 const GLbyte *payload = (const GLbyte *) src->opcode_current + 1;
332 const int num_opcodes = crPackNumOpcodes(src);
333 const int length = src->data_current - src->opcode_current - 1;
334
335 CRASSERT(pc);
336 CR_LOCK_PACKER_CONTEXT(pc);
337 CRASSERT(pc->currentBuffer);
338 CRASSERT(pc->currentBuffer != src);
339
340 /*
341 * payload points to the block of opcodes immediately followed by operands.
342 */
343
344 if ( !crPackCanHoldBoundedBuffer( CR_PACKER_CONTEXT_ARG src ) )
345 {
346 if (src->holds_BeginEnd)
347 {
348 crWarning( "crPackAppendBoundedBuffer: overflowed the destination!" );
349 CR_UNLOCK_PACKER_CONTEXT(pc);
350 return;
351 }
352 else
353 {
354 crError( "crPackAppendBoundedBuffer: overflowed the destination!" );
355 CR_UNLOCK_PACKER_CONTEXT(pc);
356 }
357 }
358
359 if (pc->swapping)
360 crPackBoundsInfoCRSWAP( CR_PACKER_CONTEXT_ARG bounds, payload, length, num_opcodes );
361 else
362 crPackBoundsInfoCR( CR_PACKER_CONTEXT_ARG bounds, payload, length, num_opcodes );
363
364 pc->buffer.holds_BeginEnd |= src->holds_BeginEnd;
365 pc->buffer.in_BeginEnd = src->in_BeginEnd;
366 pc->buffer.holds_List |= src->holds_List;
367 CR_UNLOCK_PACKER_CONTEXT(pc);
368}
369
370
371#ifndef CHROMIUM_THREADSAFE
372static unsigned char *sanityCheckPointer = NULL;
373#endif
374
375
376/*
377 * Allocate space for a command that might be very large, such as
378 * glTexImage2D or glBufferDataARB call.
379 * The command buffer _MUST_ then be transmitted by calling crHugePacket.
380 */
381void *crPackAlloc( CR_PACKER_CONTEXT_ARGDECL unsigned int size )
382{
383 CR_GET_PACKER_CONTEXT(pc);
384 unsigned char *data_ptr;
385
386 /* include space for the length and make the payload word-aligned */
387 size = ( size + sizeof(unsigned int) + 0x3 ) & ~0x3;
388
389 CR_LOCK_PACKER_CONTEXT(pc);
390
391 if ( crPackCanHoldOpcode( pc, 1, size ) )
392 {
393 /* we can just put it in the current buffer */
394 CR_GET_BUFFERED_POINTER_NOLOCK(pc, size ); /* NOTE: this sets data_ptr */
395 }
396 else
397 {
398 /* Okay, it didn't fit. Maybe it will after we flush. */
399 CR_UNLOCK_PACKER_CONTEXT(pc);
400 pc->Flush( pc->flush_arg );
401 CR_LOCK_PACKER_CONTEXT(pc);
402 if ( crPackCanHoldOpcode( pc, 1, size ) )
403 {
404 CR_GET_BUFFERED_POINTER_NOLOCK(pc, size ); /* NOTE: this sets data_ptr */
405 }
406 else
407 {
408 /* It's really way too big, so allocate a temporary packet
409 * with space for the single opcode plus the payload &
410 * header.
411 */
412 data_ptr = (unsigned char *)
413 crAlloc( sizeof(CRMessageOpcodes) + 4 + size );
414
415 /* skip the header & opcode space */
416 data_ptr += sizeof(CRMessageOpcodes) + 4;
417 }
418 }
419
420 /* At the top of the function, we added four to the request size and
421 * rounded it up to the next multiple of four.
422 *
423 * At this point, we have:
424 *
425 * HIGH MEM | byte size - 1 | \
426 * ... |
427 * ... | - original 'size' bytes for data
428 * | operand data | |
429 * return value -> | operand data | /
430 * | byte 3 | \
431 * | byte 2 | |- These bytes will store 'size'
432 * | byte 1 | |
433 * data_ptr -> | byte 0 | /
434 * | CR opcode | <- Set in packspuHuge()
435 * | unused |
436 * | unused |
437 * | unused |
438 * | CRMessageOpcodes |
439 * | CRMessageOpcodes |
440 * ...
441 * | CRMessageOpcodes |
442 * | CRMessageOpcodes |
443 * LOW MEM +------------------+
444 */
445
446 if (pc->swapping)
447 {
448 *((unsigned int *) data_ptr) = SWAP32(size);
449 crDebug( "Just swapped the length, putting %d on the wire!", *((unsigned int *) data_ptr));
450 }
451 else
452 {
453 *((unsigned int *) data_ptr) = size;
454 }
455#ifndef CHROMIUM_THREADSAFE
456 sanityCheckPointer = data_ptr + 4;
457#endif
458 return data_ptr + 4;
459}
460
461#define IS_BUFFERED( packet ) \
462 ((unsigned char *) (packet) >= pc->buffer.data_start && \
463 (unsigned char *) (packet) < pc->buffer.data_end)
464
465
466/*
467 * Transmit a packet which was allocated with crPackAlloc.
468 */
469void crHugePacket( CR_PACKER_CONTEXT_ARGDECL CROpcode opcode, void *packet )
470{
471 CR_GET_PACKER_CONTEXT(pc);
472#ifndef CHROMIUM_THREADSAFE
473 CRASSERT(sanityCheckPointer == packet);
474 sanityCheckPointer = NULL;
475#endif
476
477 if ( IS_BUFFERED( packet ) )
478 WRITE_OPCODE( pc, opcode );
479 else
480 pc->SendHuge( opcode, packet );
481}
482
483void crPackFree( CR_PACKER_CONTEXT_ARGDECL void *packet )
484{
485 CR_GET_PACKER_CONTEXT(pc);
486
487 if ( IS_BUFFERED( packet ) )
488 {
489 CR_UNLOCK_PACKER_CONTEXT(pc);
490 return;
491 }
492
493 CR_UNLOCK_PACKER_CONTEXT(pc);
494
495 /* the pointer passed in doesn't include the space for the single
496 * opcode (4 bytes because of the alignment requirement) or the
497 * length field or the header */
498 crFree( (unsigned char *) packet - 8 - sizeof(CRMessageOpcodes) );
499}
500
501void crNetworkPointerWrite( CRNetworkPointer *dst, void *src )
502{
503 /* init CRNetworkPointer with invalid values */
504 dst->ptrAlign[0] = 0xDeadBeef;
505 dst->ptrAlign[1] = 0xCafeBabe;
506 /* copy the pointer's value into the CRNetworkPointer */
507 crMemcpy( dst, &src, sizeof(src) );
508
509 /* if either assertion fails, it probably means that a packer function
510 * (which returns a value) was called without setting up the writeback
511 * pointer, or something like that.
512 */
513 CRASSERT(dst->ptrAlign[0] != 0xffffffff);
514 CRASSERT(dst->ptrAlign[0] != 0xDeadBeef);
515}
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