VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c@ 32059

Last change on this file since 32059 was 32059, checked in by vboxsync, 14 years ago

crOpenGL: remove test hack

  • 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 "state.h"
8#include "cr_mem.h"
9#include "cr_error.h"
10#include "cr_spu.h"
11
12#ifdef CHROMIUM_THREADSAFE
13CRtsd __contextTSD;
14#else
15CRContext *__currentContext = NULL;
16#endif
17
18CRStateBits *__currentBits = NULL;
19GLboolean g_availableContexts[CR_MAX_CONTEXTS];
20
21static CRSharedState *gSharedState=NULL;
22
23static CRContext *defaultContext = NULL;
24
25
26
27/**
28 * Allocate a new shared state object.
29 * Contains texture objects, display lists, etc.
30 */
31static CRSharedState *
32crStateAllocShared(void)
33{
34 CRSharedState *s = (CRSharedState *) crCalloc(sizeof(CRSharedState));
35 if (s) {
36 s->textureTable = crAllocHashtable();
37 s->dlistTable = crAllocHashtable();
38 s->buffersTable = crAllocHashtable();
39 s->fbTable = crAllocHashtable();
40 s->rbTable = crAllocHashtable();
41 s->refCount = 1; /* refcount is number of contexts using this state */
42 s->saveCount = 0;
43 }
44 return s;
45}
46
47
48
49/**
50 * Callback used for crFreeHashtable().
51 */
52static void
53DeleteTextureCallback(void *texObj)
54{
55#ifndef IN_GUEST
56 diff_api.DeleteTextures(1, &((CRTextureObj *)texObj)->hwid);
57#endif
58 crStateDeleteTextureObject((CRTextureObj *) texObj);
59}
60
61/**
62 * Decrement shared state's refcount and delete when it hits zero.
63 */
64DECLEXPORT(void)
65crStateFreeShared(CRSharedState *s)
66{
67 s->refCount--;
68 if (s->refCount <= 0) {
69 if (s==gSharedState)
70 {
71 gSharedState = NULL;
72 }
73 crFreeHashtable(s->textureTable, DeleteTextureCallback);
74 crFreeHashtable(s->dlistTable, crFree); /* call crFree for each entry */
75 crFreeHashtable(s->buffersTable, crStateFreeBufferObject);
76 crFreeHashtable(s->fbTable, crStateFreeFBO);
77 crFreeHashtable(s->rbTable, crStateFreeRBO);
78 crFree(s);
79 }
80}
81
82DECLEXPORT(void) STATE_APIENTRY
83crStateShareContext(GLboolean value)
84{
85 CRContext *pCtx = GetCurrentContext();
86 CRASSERT(pCtx && pCtx->shared);
87
88 if (value)
89 {
90 if (pCtx->shared == gSharedState)
91 {
92 return;
93 }
94
95 crDebug("Context(%i) shared", pCtx->id);
96
97 if (!gSharedState)
98 {
99 gSharedState = pCtx->shared;
100 }
101 else
102 {
103 crStateFreeShared(pCtx->shared);
104 pCtx->shared = gSharedState;
105 gSharedState->refCount++;
106 }
107 }
108 else
109 {
110 if (pCtx->shared != gSharedState)
111 {
112 return;
113 }
114
115 crDebug("Context(%i) unshared", pCtx->id);
116
117 if (gSharedState->refCount==1)
118 {
119 gSharedState = NULL;
120 }
121 else
122 {
123 pCtx->shared = crStateAllocShared();
124 pCtx->shared->id = pCtx->id;
125 crStateFreeShared(gSharedState);
126 }
127 }
128}
129
130/*
131 * Helper for crStateCreateContext, below.
132 */
133static CRContext *
134crStateCreateContextId(int i, const CRLimitsState *limits,
135 GLint visBits, CRContext *shareCtx)
136{
137 CRContext *ctx = (CRContext *) crCalloc( sizeof( *ctx ) );
138 int j;
139 int node32 = i >> 5;
140 int node = i & 0x1f;
141
142 ctx->pImage = NULL;
143
144 ctx->id = i;
145 ctx->flush_func = NULL;
146 for (j=0;j<CR_MAX_BITARRAY;j++){
147 if (j == node32) {
148 ctx->bitid[j] = (1 << node);
149 } else {
150 ctx->bitid[j] = 0;
151 }
152 ctx->neg_bitid[j] = ~(ctx->bitid[j]);
153 }
154
155 if (shareCtx) {
156 CRASSERT(shareCtx->shared);
157 ctx->shared = shareCtx->shared;
158 ctx->shared->refCount ++;
159 }
160 else {
161 ctx->shared = crStateAllocShared();
162 ctx->shared->id = ctx->id;
163 }
164
165 /* use Chromium's OpenGL defaults */
166 crStateLimitsInit( &(ctx->limits) );
167 crStateExtensionsInit( &(ctx->limits), &(ctx->extensions) );
168
169 crStateBufferObjectInit( ctx ); /* must precede client state init! */
170 crStateClientInit( &(ctx->client) );
171
172 crStateBufferInit( ctx );
173 crStateCurrentInit( ctx );
174 crStateEvaluatorInit( ctx );
175 crStateFogInit( ctx );
176 crStateHintInit( ctx );
177 crStateLightingInit( ctx );
178 crStateLineInit( ctx );
179 crStateListsInit( ctx );
180 crStateMultisampleInit( ctx );
181 crStateOcclusionInit( ctx );
182 crStatePixelInit( ctx );
183 crStatePolygonInit( ctx );
184 crStatePointInit( ctx );
185 crStateProgramInit( ctx );
186 crStateRegCombinerInit( ctx );
187 crStateStencilInit( ctx );
188 crStateTextureInit( ctx );
189 crStateTransformInit( ctx );
190 crStateViewportInit ( ctx );
191 crStateFramebufferObjectInit(ctx);
192 crStateGLSLInit(ctx);
193
194 /* This has to come last. */
195 crStateAttribInit( &(ctx->attrib) );
196
197 ctx->renderMode = GL_RENDER;
198
199 /* Initialize values that depend on the visual mode */
200 if (visBits & CR_DOUBLE_BIT) {
201 ctx->limits.doubleBuffer = GL_TRUE;
202 }
203 if (visBits & CR_RGB_BIT) {
204 ctx->limits.redBits = 8;
205 ctx->limits.greenBits = 8;
206 ctx->limits.blueBits = 8;
207 if (visBits & CR_ALPHA_BIT) {
208 ctx->limits.alphaBits = 8;
209 }
210 }
211 else {
212 ctx->limits.indexBits = 8;
213 }
214 if (visBits & CR_DEPTH_BIT) {
215 ctx->limits.depthBits = 24;
216 }
217 if (visBits & CR_STENCIL_BIT) {
218 ctx->limits.stencilBits = 8;
219 }
220 if (visBits & CR_ACCUM_BIT) {
221 ctx->limits.accumRedBits = 16;
222 ctx->limits.accumGreenBits = 16;
223 ctx->limits.accumBlueBits = 16;
224 if (visBits & CR_ALPHA_BIT) {
225 ctx->limits.accumAlphaBits = 16;
226 }
227 }
228 if (visBits & CR_STEREO_BIT) {
229 ctx->limits.stereo = GL_TRUE;
230 }
231 if (visBits & CR_MULTISAMPLE_BIT) {
232 ctx->limits.sampleBuffers = 1;
233 ctx->limits.samples = 4;
234 ctx->multisample.enabled = GL_TRUE;
235 }
236
237 if (visBits & CR_OVERLAY_BIT) {
238 ctx->limits.level = 1;
239 }
240
241 return ctx;
242}
243
244/*@todo crStateAttribDestroy*/
245static void
246crStateFreeContext(CRContext *ctx)
247{
248 crStateClientDestroy( &(ctx->client) );
249 crStateLimitsDestroy( &(ctx->limits) );
250 crStateBufferObjectDestroy( ctx );
251 crStateEvaluatorDestroy( ctx );
252 crStateListsDestroy( ctx );
253 crStateLightingDestroy( ctx );
254 crStateOcclusionDestroy( ctx );
255 crStateProgramDestroy( ctx );
256 crStateTextureDestroy( ctx );
257 crStateTransformDestroy( ctx );
258 crStateFreeShared(ctx->shared);
259 crStateFramebufferObjectDestroy(ctx);
260 crStateGLSLDestroy(ctx);
261 if (ctx->pImage) crFree(ctx->pImage);
262 crFree( ctx );
263}
264
265
266/*
267 * Allocate the state (dirty) bits data structures.
268 * This should be called before we create any contexts.
269 * We'll also create the default/NULL context at this time and make
270 * it the current context by default. This means that if someone
271 * tries to set GL state before calling MakeCurrent() they'll be
272 * modifying the default state object, and not segfaulting on a NULL
273 * pointer somewhere.
274 */
275void crStateInit(void)
276{
277 unsigned int i;
278
279 /* Purely initialize the context bits */
280 if (!__currentBits) {
281 __currentBits = (CRStateBits *) crCalloc( sizeof(CRStateBits) );
282 crStateClientInitBits( &(__currentBits->client) );
283 crStateLightingInitBits( &(__currentBits->lighting) );
284 } else
285 crWarning("State tracker is being re-initialized..\n");
286
287 for (i=0;i<CR_MAX_CONTEXTS;i++)
288 g_availableContexts[i] = 0;
289
290 if (defaultContext) {
291 /* Free the default/NULL context.
292 * Ensures context bits are reset */
293 crStateFreeContext(defaultContext);
294#ifdef CHROMIUM_THREADSAFE
295 crSetTSD(&__contextTSD, NULL);
296#else
297 __currentContext = NULL;
298#endif
299 }
300
301 /* Reset diff_api */
302 crMemZero(&diff_api, sizeof(SPUDispatchTable));
303
304 /* Allocate the default/NULL context */
305 defaultContext = crStateCreateContextId(0, NULL, CR_RGB_BIT, NULL);
306 CRASSERT(g_availableContexts[0] == 0);
307 g_availableContexts[0] = 1; /* in use forever */
308
309#ifdef CHROMIUM_THREADSAFE
310 crSetTSD(&__contextTSD, defaultContext);
311#else
312 __currentContext = defaultContext;
313#endif
314}
315
316void crStateDestroy(void)
317{
318 if (__currentBits)
319 {
320 crStateClientDestroyBits(&(__currentBits->client));
321 crStateLightingDestroyBits(&(__currentBits->lighting));
322 crFree(__currentBits);
323 __currentBits = NULL;
324 }
325
326#ifdef CHROMIUM_THREADSAFE
327 crFreeTSD(&__contextTSD);
328#endif
329}
330
331/*
332 * Notes on context switching and the "default context".
333 *
334 * See the paper "Tracking Graphics State for Networked Rendering"
335 * by Ian Buck, Greg Humphries and Pat Hanrahan for background
336 * information about how the state tracker and context switching
337 * works.
338 *
339 * When we make a new context current, we call crStateSwitchContext()
340 * in order to transform the 'from' context into the 'to' context
341 * (i.e. the old context to the new context). The transformation
342 * is accomplished by calling GL functions through the 'diff_api'
343 * so that the downstream GL machine (represented by the __currentContext
344 * structure) is updated to reflect the new context state. Finally,
345 * we point __currentContext to the new context.
346 *
347 * A subtle problem we have to deal with is context destruction.
348 * This issue arose while testing with Glean. We found that when
349 * the currently bound context was getting destroyed that state
350 * tracking was incorrect when a subsequent new context was activated.
351 * In DestroyContext, the __hwcontext was being set to NULL and effectively
352 * going away. Later in MakeCurrent we had no idea what the state of the
353 * downstream GL machine was (since __hwcontext was gone). This meant
354 * we had nothing to 'diff' against and the downstream GL machine was
355 * in an unknown state.
356 *
357 * The solution to this problem is the "default/NULL" context. The
358 * default context is created the first time CreateContext is called
359 * and is never freed. Whenever we get a crStateMakeCurrent(NULL) call
360 * or destroy the currently bound context in crStateDestroyContext()
361 * we call crStateSwitchContext() to switch to the default context and
362 * then set the __currentContext pointer to point to the default context.
363 * This ensures that the dirty bits are updated and the diff_api functions
364 * are called to keep the downstream GL machine in a known state.
365 * Finally, the __hwcontext variable is no longer needed now.
366 *
367 * Yeah, this is kind of a mind-bender, but it really solves the problem
368 * pretty cleanly.
369 *
370 * -Brian
371 */
372
373
374CRContext *
375crStateCreateContext(const CRLimitsState *limits, GLint visBits, CRContext *share)
376{
377 int i;
378
379 /* Must have created the default context via crStateInit() first */
380 CRASSERT(defaultContext);
381
382 for (i = 1 ; i < CR_MAX_CONTEXTS ; i++)
383 {
384 if (!g_availableContexts[i])
385 {
386 g_availableContexts[i] = 1; /* it's no longer available */
387 return crStateCreateContextId( i, limits, visBits, share );
388 }
389 }
390 crError( "Out of available contexts in crStateCreateContexts (max %d)",
391 CR_MAX_CONTEXTS );
392 /* never get here */
393 return NULL;
394}
395
396CRContext *
397crStateCreateContextEx(const CRLimitsState *limits, GLint visBits, CRContext *share, GLint presetID)
398{
399 if (presetID>0)
400 {
401 CRASSERT(!g_availableContexts[presetID]);
402 g_availableContexts[presetID] = 1;
403 return crStateCreateContextId(presetID, limits, visBits, share);
404 }
405 else return crStateCreateContext(limits, visBits, share);
406}
407
408void crStateDestroyContext( CRContext *ctx )
409{
410 CRContext *current = GetCurrentContext();
411
412 if (current == ctx) {
413 /* destroying the current context - have to be careful here */
414 CRASSERT(defaultContext);
415 /* Check to see if the differencer exists first,
416 we may not have one, aka the packspu */
417 if (diff_api.AlphaFunc)
418 crStateSwitchContext(current, defaultContext);
419#ifdef CHROMIUM_THREADSAFE
420 crSetTSD(&__contextTSD, defaultContext);
421#else
422 __currentContext = defaultContext;
423#endif
424 /* ensure matrix state is also current */
425 crStateMatrixMode(defaultContext->transform.matrixMode);
426 }
427 g_availableContexts[ctx->id] = 0;
428
429 crStateFreeContext(ctx);
430}
431
432
433void crStateMakeCurrent( CRContext *ctx )
434{
435 CRContext *current = GetCurrentContext();
436
437 if (ctx == NULL)
438 ctx = defaultContext;
439
440 if (current == ctx)
441 return; /* no-op */
442
443 CRASSERT(ctx);
444
445 if (current) {
446 /* Check to see if the differencer exists first,
447 we may not have one, aka the packspu */
448 if (diff_api.AlphaFunc)
449 crStateSwitchContext( current, ctx );
450 }
451
452#ifdef CHROMIUM_THREADSAFE
453 crSetTSD(&__contextTSD, ctx);
454#else
455 __currentContext = ctx;
456#endif
457
458 /* ensure matrix state is also current */
459 crStateMatrixMode(ctx->transform.matrixMode);
460}
461
462
463/*
464 * As above, but don't call crStateSwitchContext().
465 */
466void crStateSetCurrent( CRContext *ctx )
467{
468 CRContext *current = GetCurrentContext();
469
470 if (ctx == NULL)
471 ctx = defaultContext;
472
473 if (current == ctx)
474 return; /* no-op */
475
476 CRASSERT(ctx);
477
478#ifdef CHROMIUM_THREADSAFE
479 crSetTSD(&__contextTSD, ctx);
480#else
481 __currentContext = ctx;
482#endif
483
484 /* ensure matrix state is also current */
485 crStateMatrixMode(ctx->transform.matrixMode);
486}
487
488
489CRContext *crStateGetCurrent(void)
490{
491 return GetCurrentContext();
492}
493
494
495void crStateUpdateColorBits(void)
496{
497 /* This is a hack to force updating the 'current' attribs */
498 CRStateBits *sb = GetCurrentBits();
499 FILLDIRTY(sb->current.dirty);
500 FILLDIRTY(sb->current.vertexAttrib[VERT_ATTRIB_COLOR0]);
501}
502
503
504void STATE_APIENTRY
505crStateChromiumParameteriCR( GLenum target, GLint value )
506{
507 /* This no-op function helps smooth code-gen */
508}
509
510void STATE_APIENTRY
511crStateChromiumParameterfCR( GLenum target, GLfloat value )
512{
513 /* This no-op function helps smooth code-gen */
514}
515
516void STATE_APIENTRY
517crStateChromiumParametervCR( GLenum target, GLenum type, GLsizei count, const GLvoid *values )
518{
519 /* This no-op function helps smooth code-gen */
520}
521
522void STATE_APIENTRY
523crStateGetChromiumParametervCR( GLenum target, GLuint index, GLenum type, GLsizei count, GLvoid *values )
524{
525 /* This no-op function helps smooth code-gen */
526}
527
528void STATE_APIENTRY
529crStateReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
530 GLenum format, GLenum type, GLvoid *pixels )
531{
532 /* This no-op function helps smooth code-gen */
533}
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