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