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
|
---|
13 | static bool __isContextTLSInited = false;
|
---|
14 | CRtsd __contextTSD;
|
---|
15 | #else
|
---|
16 | CRContext *__currentContext = NULL;
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | CRStateBits *__currentBits = NULL;
|
---|
20 | CRContext *g_pAvailableContexts[CR_MAX_CONTEXTS];
|
---|
21 | uint32_t g_cContexts = 0;
|
---|
22 |
|
---|
23 | static CRSharedState *gSharedState=NULL;
|
---|
24 |
|
---|
25 | static CRContext *defaultContext = NULL;
|
---|
26 |
|
---|
27 | GLboolean g_bVBoxEnableDiffOnMakeCurrent = GL_TRUE;
|
---|
28 |
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Allocate a new shared state object.
|
---|
32 | * Contains texture objects, display lists, etc.
|
---|
33 | */
|
---|
34 | static CRSharedState *
|
---|
35 | crStateAllocShared(void)
|
---|
36 | {
|
---|
37 | CRSharedState *s = (CRSharedState *) crCalloc(sizeof(CRSharedState));
|
---|
38 | if (s) {
|
---|
39 | s->textureTable = crAllocHashtable();
|
---|
40 | s->dlistTable = crAllocHashtable();
|
---|
41 | s->buffersTable = crAllocHashtable();
|
---|
42 | s->fbTable = crAllocHashtable();
|
---|
43 | s->rbTable = crAllocHashtable();
|
---|
44 | s->refCount = 1; /* refcount is number of contexts using this state */
|
---|
45 | s->saveCount = 0;
|
---|
46 | }
|
---|
47 | return s;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Callback used for crFreeHashtable().
|
---|
54 | */
|
---|
55 | DECLEXPORT(void)
|
---|
56 | crStateDeleteTextureCallback(void *texObj)
|
---|
57 | {
|
---|
58 | #ifndef IN_GUEST
|
---|
59 | diff_api.DeleteTextures(1, &((CRTextureObj *)texObj)->hwid);
|
---|
60 | #endif
|
---|
61 | crStateDeleteTextureObject((CRTextureObj *) texObj);
|
---|
62 | }
|
---|
63 |
|
---|
64 | typedef struct CR_STATE_RELEASEOBJ
|
---|
65 | {
|
---|
66 | CRContext *pCtx;
|
---|
67 | CRSharedState *s;
|
---|
68 | } CR_STATE_RELEASEOBJ, *PCR_STATE_RELEASEOBJ;
|
---|
69 |
|
---|
70 | static void ReleaseTextureCallback(unsigned long key, void *data1, void *data2)
|
---|
71 | {
|
---|
72 | PCR_STATE_RELEASEOBJ pData = (PCR_STATE_RELEASEOBJ)data2;
|
---|
73 | CRTextureObj *pObj = (CRTextureObj *)data1;
|
---|
74 | CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pData->pCtx);
|
---|
75 | if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
|
---|
76 | crHashtableDelete(pData->s->textureTable, key, crStateDeleteTextureCallback);
|
---|
77 | }
|
---|
78 |
|
---|
79 | static void ReleaseBufferObjectCallback(unsigned long key, void *data1, void *data2)
|
---|
80 | {
|
---|
81 | PCR_STATE_RELEASEOBJ pData = (PCR_STATE_RELEASEOBJ)data2;
|
---|
82 | CRBufferObject *pObj = (CRBufferObject *)data1;
|
---|
83 | CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pData->pCtx);
|
---|
84 | if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
|
---|
85 | crHashtableDelete(pData->s->buffersTable, key, crStateFreeBufferObject);
|
---|
86 | }
|
---|
87 |
|
---|
88 | static void ReleaseFBOCallback(unsigned long key, void *data1, void *data2)
|
---|
89 | {
|
---|
90 | PCR_STATE_RELEASEOBJ pData = (PCR_STATE_RELEASEOBJ)data2;
|
---|
91 | CRFramebufferObject *pObj = (CRFramebufferObject *)data1;
|
---|
92 | CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pData->pCtx);
|
---|
93 | if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
|
---|
94 | crHashtableDelete(pData->s->fbTable, key, crStateFreeFBO);
|
---|
95 | }
|
---|
96 |
|
---|
97 | static void ReleaseRBOCallback(unsigned long key, void *data1, void *data2)
|
---|
98 | {
|
---|
99 | PCR_STATE_RELEASEOBJ pData = (PCR_STATE_RELEASEOBJ)data2;
|
---|
100 | CRRenderbufferObject *pObj = (CRRenderbufferObject *)data1;
|
---|
101 | CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pData->pCtx);
|
---|
102 | if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
|
---|
103 | crHashtableDelete(pData->s->rbTable, key, crStateFreeRBO);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Decrement shared state's refcount and delete when it hits zero.
|
---|
108 | */
|
---|
109 | DECLEXPORT(void)
|
---|
110 | crStateFreeShared(CRContext *pContext, CRSharedState *s)
|
---|
111 | {
|
---|
112 | s->refCount--;
|
---|
113 | Assert(s->refCount >= 0);
|
---|
114 | if (s->refCount <= 0) {
|
---|
115 | if (s==gSharedState)
|
---|
116 | {
|
---|
117 | gSharedState = NULL;
|
---|
118 | }
|
---|
119 | crFreeHashtable(s->textureTable, crStateDeleteTextureCallback);
|
---|
120 | crFreeHashtable(s->dlistTable, crFree); /* call crFree for each entry */
|
---|
121 | crFreeHashtable(s->buffersTable, crStateFreeBufferObject);
|
---|
122 | crFreeHashtable(s->fbTable, crStateFreeFBO);
|
---|
123 | crFreeHashtable(s->rbTable, crStateFreeRBO);
|
---|
124 | crFree(s);
|
---|
125 | }
|
---|
126 | else if (pContext)
|
---|
127 | {
|
---|
128 | /* evaluate usage bits*/
|
---|
129 | CR_STATE_RELEASEOBJ CbData;
|
---|
130 | CbData.pCtx = pContext;
|
---|
131 | CbData.s = s;
|
---|
132 | crHashtableWalk(s->textureTable, ReleaseTextureCallback, &CbData);
|
---|
133 | crHashtableWalk(s->buffersTable, ReleaseBufferObjectCallback , &CbData);
|
---|
134 | crHashtableWalk(s->fbTable, ReleaseFBOCallback, &CbData);
|
---|
135 | crHashtableWalk(s->rbTable, ReleaseRBOCallback, &CbData);
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | DECLEXPORT(CRSharedState *) crStateGlobalSharedAcquire()
|
---|
140 | {
|
---|
141 | if (!gSharedState)
|
---|
142 | {
|
---|
143 | crWarning("No Global Shared State!");
|
---|
144 | return NULL;
|
---|
145 | }
|
---|
146 | gSharedState->refCount++;
|
---|
147 | return gSharedState;
|
---|
148 | }
|
---|
149 |
|
---|
150 | DECLEXPORT(void) crStateGlobalSharedRelease()
|
---|
151 | {
|
---|
152 | crStateFreeShared(NULL, gSharedState);
|
---|
153 | }
|
---|
154 |
|
---|
155 | DECLEXPORT(void) STATE_APIENTRY
|
---|
156 | crStateShareContext(GLboolean value)
|
---|
157 | {
|
---|
158 | CRContext *pCtx = GetCurrentContext();
|
---|
159 | CRASSERT(pCtx && pCtx->shared);
|
---|
160 |
|
---|
161 | if (value)
|
---|
162 | {
|
---|
163 | if (pCtx->shared == gSharedState)
|
---|
164 | {
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | crDebug("Context(%i) shared", pCtx->id);
|
---|
169 |
|
---|
170 | if (!gSharedState)
|
---|
171 | {
|
---|
172 | gSharedState = pCtx->shared;
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | crStateFreeShared(pCtx, pCtx->shared);
|
---|
177 | pCtx->shared = gSharedState;
|
---|
178 | gSharedState->refCount++;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | else
|
---|
182 | {
|
---|
183 | if (pCtx->shared != gSharedState)
|
---|
184 | {
|
---|
185 | return;
|
---|
186 | }
|
---|
187 |
|
---|
188 | crDebug("Context(%i) unshared", pCtx->id);
|
---|
189 |
|
---|
190 | if (gSharedState->refCount==1)
|
---|
191 | {
|
---|
192 | gSharedState = NULL;
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | pCtx->shared = crStateAllocShared();
|
---|
197 | pCtx->shared->id = pCtx->id;
|
---|
198 | crStateFreeShared(pCtx, gSharedState);
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | DECLEXPORT(GLboolean) STATE_APIENTRY
|
---|
204 | crStateContextIsShared(CRContext *pCtx)
|
---|
205 | {
|
---|
206 | return pCtx->shared==gSharedState;
|
---|
207 | }
|
---|
208 |
|
---|
209 | DECLEXPORT(void) STATE_APIENTRY
|
---|
210 | crStateSetSharedContext(CRContext *pCtx)
|
---|
211 | {
|
---|
212 | if (gSharedState)
|
---|
213 | {
|
---|
214 | crWarning("crStateSetSharedContext: shared is being changed from %p to %p", gSharedState, pCtx->shared);
|
---|
215 | }
|
---|
216 |
|
---|
217 | gSharedState = pCtx->shared;
|
---|
218 | }
|
---|
219 |
|
---|
220 | #ifdef CHROMIUM_THREADSAFE
|
---|
221 | static void
|
---|
222 | crStateFreeContext(CRContext *ctx);
|
---|
223 | static DECLCALLBACK(void) crStateContextDtor(void *pvCtx)
|
---|
224 | {
|
---|
225 | crStateFreeContext((CRContext*)pvCtx);
|
---|
226 | }
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Helper for crStateCreateContext, below.
|
---|
231 | */
|
---|
232 | static CRContext *
|
---|
233 | crStateCreateContextId(int i, const CRLimitsState *limits,
|
---|
234 | GLint visBits, CRContext *shareCtx)
|
---|
235 | {
|
---|
236 | CRContext *ctx;
|
---|
237 | int j;
|
---|
238 | int node32 = i >> 5;
|
---|
239 | int node = i & 0x1f;
|
---|
240 |
|
---|
241 | if (g_pAvailableContexts[i] != NULL)
|
---|
242 | {
|
---|
243 | crWarning("trying to create context with used id");
|
---|
244 | return NULL;
|
---|
245 | }
|
---|
246 |
|
---|
247 | ctx = (CRContext *) crCalloc( sizeof( *ctx ) );
|
---|
248 | if (!ctx)
|
---|
249 | {
|
---|
250 | crWarning("failed to allocate context");
|
---|
251 | return NULL;
|
---|
252 | }
|
---|
253 | g_pAvailableContexts[i] = ctx;
|
---|
254 | ++g_cContexts;
|
---|
255 | CRASSERT(g_cContexts < RT_ELEMENTS(g_pAvailableContexts));
|
---|
256 | ctx->id = i;
|
---|
257 | #ifdef CHROMIUM_THREADSAFE
|
---|
258 | VBoxTlsRefInit(ctx, crStateContextDtor);
|
---|
259 | #endif
|
---|
260 | ctx->flush_func = NULL;
|
---|
261 | for (j=0;j<CR_MAX_BITARRAY;j++){
|
---|
262 | if (j == node32) {
|
---|
263 | ctx->bitid[j] = (1 << node);
|
---|
264 | } else {
|
---|
265 | ctx->bitid[j] = 0;
|
---|
266 | }
|
---|
267 | ctx->neg_bitid[j] = ~(ctx->bitid[j]);
|
---|
268 | }
|
---|
269 |
|
---|
270 | if (shareCtx) {
|
---|
271 | CRASSERT(shareCtx->shared);
|
---|
272 | ctx->shared = shareCtx->shared;
|
---|
273 | ctx->shared->refCount ++;
|
---|
274 | }
|
---|
275 | else {
|
---|
276 | ctx->shared = crStateAllocShared();
|
---|
277 | ctx->shared->id = ctx->id;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /* use Chromium's OpenGL defaults */
|
---|
281 | crStateLimitsInit( &(ctx->limits) );
|
---|
282 | crStateExtensionsInit( &(ctx->limits), &(ctx->extensions) );
|
---|
283 |
|
---|
284 | crStateBufferObjectInit( ctx ); /* must precede client state init! */
|
---|
285 | crStateClientInit( ctx );
|
---|
286 |
|
---|
287 | crStateBufferInit( ctx );
|
---|
288 | crStateCurrentInit( ctx );
|
---|
289 | crStateEvaluatorInit( ctx );
|
---|
290 | crStateFogInit( ctx );
|
---|
291 | crStateHintInit( ctx );
|
---|
292 | crStateLightingInit( ctx );
|
---|
293 | crStateLineInit( ctx );
|
---|
294 | crStateListsInit( ctx );
|
---|
295 | crStateMultisampleInit( ctx );
|
---|
296 | crStateOcclusionInit( ctx );
|
---|
297 | crStatePixelInit( ctx );
|
---|
298 | crStatePolygonInit( ctx );
|
---|
299 | crStatePointInit( ctx );
|
---|
300 | crStateProgramInit( ctx );
|
---|
301 | crStateRegCombinerInit( ctx );
|
---|
302 | crStateStencilInit( ctx );
|
---|
303 | crStateTextureInit( ctx );
|
---|
304 | crStateTransformInit( ctx );
|
---|
305 | crStateViewportInit ( ctx );
|
---|
306 | crStateFramebufferObjectInit(ctx);
|
---|
307 | crStateGLSLInit(ctx);
|
---|
308 |
|
---|
309 | /* This has to come last. */
|
---|
310 | crStateAttribInit( &(ctx->attrib) );
|
---|
311 |
|
---|
312 | ctx->renderMode = GL_RENDER;
|
---|
313 |
|
---|
314 | /* Initialize values that depend on the visual mode */
|
---|
315 | if (visBits & CR_DOUBLE_BIT) {
|
---|
316 | ctx->limits.doubleBuffer = GL_TRUE;
|
---|
317 | }
|
---|
318 | if (visBits & CR_RGB_BIT) {
|
---|
319 | ctx->limits.redBits = 8;
|
---|
320 | ctx->limits.greenBits = 8;
|
---|
321 | ctx->limits.blueBits = 8;
|
---|
322 | if (visBits & CR_ALPHA_BIT) {
|
---|
323 | ctx->limits.alphaBits = 8;
|
---|
324 | }
|
---|
325 | }
|
---|
326 | else {
|
---|
327 | ctx->limits.indexBits = 8;
|
---|
328 | }
|
---|
329 | if (visBits & CR_DEPTH_BIT) {
|
---|
330 | ctx->limits.depthBits = 24;
|
---|
331 | }
|
---|
332 | if (visBits & CR_STENCIL_BIT) {
|
---|
333 | ctx->limits.stencilBits = 8;
|
---|
334 | }
|
---|
335 | if (visBits & CR_ACCUM_BIT) {
|
---|
336 | ctx->limits.accumRedBits = 16;
|
---|
337 | ctx->limits.accumGreenBits = 16;
|
---|
338 | ctx->limits.accumBlueBits = 16;
|
---|
339 | if (visBits & CR_ALPHA_BIT) {
|
---|
340 | ctx->limits.accumAlphaBits = 16;
|
---|
341 | }
|
---|
342 | }
|
---|
343 | if (visBits & CR_STEREO_BIT) {
|
---|
344 | ctx->limits.stereo = GL_TRUE;
|
---|
345 | }
|
---|
346 | if (visBits & CR_MULTISAMPLE_BIT) {
|
---|
347 | ctx->limits.sampleBuffers = 1;
|
---|
348 | ctx->limits.samples = 4;
|
---|
349 | ctx->multisample.enabled = GL_TRUE;
|
---|
350 | }
|
---|
351 |
|
---|
352 | if (visBits & CR_OVERLAY_BIT) {
|
---|
353 | ctx->limits.level = 1;
|
---|
354 | }
|
---|
355 |
|
---|
356 | return ctx;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*@todo crStateAttribDestroy*/
|
---|
360 | static void
|
---|
361 | crStateFreeContext(CRContext *ctx)
|
---|
362 | {
|
---|
363 | #ifndef DEBUG_misha
|
---|
364 | CRASSERT(g_pAvailableContexts[ctx->id] == ctx);
|
---|
365 | #endif
|
---|
366 | if (g_pAvailableContexts[ctx->id] == ctx)
|
---|
367 | {
|
---|
368 | g_pAvailableContexts[ctx->id] = NULL;
|
---|
369 | --g_cContexts;
|
---|
370 | CRASSERT(g_cContexts < RT_ELEMENTS(g_pAvailableContexts));
|
---|
371 | }
|
---|
372 | else
|
---|
373 | {
|
---|
374 | #ifndef DEBUG_misha
|
---|
375 | crWarning("freeing context %p, id(%d) not being in the context list", ctx, ctx->id);
|
---|
376 | #endif
|
---|
377 | }
|
---|
378 |
|
---|
379 | crStateClientDestroy( ctx );
|
---|
380 | crStateLimitsDestroy( &(ctx->limits) );
|
---|
381 | crStateBufferObjectDestroy( ctx );
|
---|
382 | crStateEvaluatorDestroy( ctx );
|
---|
383 | crStateListsDestroy( ctx );
|
---|
384 | crStateLightingDestroy( ctx );
|
---|
385 | crStateOcclusionDestroy( ctx );
|
---|
386 | crStateProgramDestroy( ctx );
|
---|
387 | crStateTextureDestroy( ctx );
|
---|
388 | crStateTransformDestroy( ctx );
|
---|
389 | crStateFreeShared(ctx, ctx->shared);
|
---|
390 | crStateFramebufferObjectDestroy(ctx);
|
---|
391 | crStateGLSLDestroy(ctx);
|
---|
392 | if (ctx->buffer.pFrontImg) crFree(ctx->buffer.pFrontImg);
|
---|
393 | if (ctx->buffer.pBackImg) crFree(ctx->buffer.pBackImg);
|
---|
394 | crFree( ctx );
|
---|
395 | }
|
---|
396 |
|
---|
397 | #ifdef CHROMIUM_THREADSAFE
|
---|
398 | # ifndef RT_OS_WINDOWS
|
---|
399 | static DECLCALLBACK(void) crStateThreadTlsDtor(void *pvValue)
|
---|
400 | {
|
---|
401 | CRContext *pCtx = (CRContext*)pvValue;
|
---|
402 | VBoxTlsRefRelease(pCtx);
|
---|
403 | }
|
---|
404 | # endif
|
---|
405 | #endif
|
---|
406 |
|
---|
407 | /*
|
---|
408 | * Allocate the state (dirty) bits data structures.
|
---|
409 | * This should be called before we create any contexts.
|
---|
410 | * We'll also create the default/NULL context at this time and make
|
---|
411 | * it the current context by default. This means that if someone
|
---|
412 | * tries to set GL state before calling MakeCurrent() they'll be
|
---|
413 | * modifying the default state object, and not segfaulting on a NULL
|
---|
414 | * pointer somewhere.
|
---|
415 | */
|
---|
416 | void crStateInit(void)
|
---|
417 | {
|
---|
418 | unsigned int i;
|
---|
419 |
|
---|
420 | /* Purely initialize the context bits */
|
---|
421 | if (!__currentBits) {
|
---|
422 | __currentBits = (CRStateBits *) crCalloc( sizeof(CRStateBits) );
|
---|
423 | crStateClientInitBits( &(__currentBits->client) );
|
---|
424 | crStateLightingInitBits( &(__currentBits->lighting) );
|
---|
425 | } else
|
---|
426 | {
|
---|
427 | #ifndef DEBUG_misha
|
---|
428 | crWarning("State tracker is being re-initialized..\n");
|
---|
429 | #endif
|
---|
430 | }
|
---|
431 |
|
---|
432 | for (i=0;i<CR_MAX_CONTEXTS;i++)
|
---|
433 | g_pAvailableContexts[i] = NULL;
|
---|
434 | g_cContexts = 0;
|
---|
435 |
|
---|
436 | #ifdef CHROMIUM_THREADSAFE
|
---|
437 | if (!__isContextTLSInited)
|
---|
438 | {
|
---|
439 | # ifndef RT_OS_WINDOWS
|
---|
440 | /* tls destructor is implemented for all platforms except windows*/
|
---|
441 | crInitTSDF(&__contextTSD, crStateThreadTlsDtor);
|
---|
442 | # else
|
---|
443 | /* windows should do cleanup via DllMain THREAD_DETACH notification */
|
---|
444 | crInitTSD(&__contextTSD);
|
---|
445 | # endif
|
---|
446 | __isContextTLSInited = 1;
|
---|
447 | }
|
---|
448 | #endif
|
---|
449 |
|
---|
450 | if (defaultContext) {
|
---|
451 | /* Free the default/NULL context.
|
---|
452 | * Ensures context bits are reset */
|
---|
453 | #ifdef CHROMIUM_THREADSAFE
|
---|
454 | SetCurrentContext(NULL);
|
---|
455 | VBoxTlsRefRelease(defaultContext);
|
---|
456 | #else
|
---|
457 | crStateFreeContext(defaultContext);
|
---|
458 | __currentContext = NULL;
|
---|
459 | #endif
|
---|
460 | }
|
---|
461 |
|
---|
462 | /* Reset diff_api */
|
---|
463 | crMemZero(&diff_api, sizeof(SPUDispatchTable));
|
---|
464 |
|
---|
465 | Assert(!gSharedState);
|
---|
466 | gSharedState = NULL;
|
---|
467 |
|
---|
468 | /* Allocate the default/NULL context */
|
---|
469 | CRASSERT(g_pAvailableContexts[0] == NULL);
|
---|
470 | defaultContext = crStateCreateContextId(0, NULL, CR_RGB_BIT, NULL);
|
---|
471 | CRASSERT(g_pAvailableContexts[0] == defaultContext);
|
---|
472 | CRASSERT(g_cContexts == 1);
|
---|
473 | #ifdef CHROMIUM_THREADSAFE
|
---|
474 | SetCurrentContext(defaultContext);
|
---|
475 | #else
|
---|
476 | __currentContext = defaultContext;
|
---|
477 | #endif
|
---|
478 | }
|
---|
479 |
|
---|
480 | void crStateDestroy(void)
|
---|
481 | {
|
---|
482 | int i;
|
---|
483 | if (__currentBits)
|
---|
484 | {
|
---|
485 | crStateClientDestroyBits(&(__currentBits->client));
|
---|
486 | crStateLightingDestroyBits(&(__currentBits->lighting));
|
---|
487 | crFree(__currentBits);
|
---|
488 | __currentBits = NULL;
|
---|
489 | }
|
---|
490 |
|
---|
491 | SetCurrentContext(NULL);
|
---|
492 |
|
---|
493 | for (i = CR_MAX_CONTEXTS-1; i >= 0; i--)
|
---|
494 | {
|
---|
495 | if (g_pAvailableContexts[i])
|
---|
496 | {
|
---|
497 | #ifdef CHROMIUM_THREADSAFE
|
---|
498 | if (VBoxTlsRefIsFunctional(g_pAvailableContexts[i]))
|
---|
499 | VBoxTlsRefRelease(g_pAvailableContexts[i]);
|
---|
500 | #else
|
---|
501 | crStateFreeContext(g_pAvailableContexts[i]);
|
---|
502 | #endif
|
---|
503 | }
|
---|
504 | }
|
---|
505 |
|
---|
506 | /* default context was stored in g_pAvailableContexts[0], so it was destroyed already */
|
---|
507 | defaultContext = NULL;
|
---|
508 |
|
---|
509 |
|
---|
510 | #ifdef CHROMIUM_THREADSAFE
|
---|
511 | crFreeTSD(&__contextTSD);
|
---|
512 | __isContextTLSInited = 0;
|
---|
513 | #endif
|
---|
514 | }
|
---|
515 |
|
---|
516 | /*
|
---|
517 | * Notes on context switching and the "default context".
|
---|
518 | *
|
---|
519 | * See the paper "Tracking Graphics State for Networked Rendering"
|
---|
520 | * by Ian Buck, Greg Humphries and Pat Hanrahan for background
|
---|
521 | * information about how the state tracker and context switching
|
---|
522 | * works.
|
---|
523 | *
|
---|
524 | * When we make a new context current, we call crStateSwitchContext()
|
---|
525 | * in order to transform the 'from' context into the 'to' context
|
---|
526 | * (i.e. the old context to the new context). The transformation
|
---|
527 | * is accomplished by calling GL functions through the 'diff_api'
|
---|
528 | * so that the downstream GL machine (represented by the __currentContext
|
---|
529 | * structure) is updated to reflect the new context state. Finally,
|
---|
530 | * we point __currentContext to the new context.
|
---|
531 | *
|
---|
532 | * A subtle problem we have to deal with is context destruction.
|
---|
533 | * This issue arose while testing with Glean. We found that when
|
---|
534 | * the currently bound context was getting destroyed that state
|
---|
535 | * tracking was incorrect when a subsequent new context was activated.
|
---|
536 | * In DestroyContext, the __hwcontext was being set to NULL and effectively
|
---|
537 | * going away. Later in MakeCurrent we had no idea what the state of the
|
---|
538 | * downstream GL machine was (since __hwcontext was gone). This meant
|
---|
539 | * we had nothing to 'diff' against and the downstream GL machine was
|
---|
540 | * in an unknown state.
|
---|
541 | *
|
---|
542 | * The solution to this problem is the "default/NULL" context. The
|
---|
543 | * default context is created the first time CreateContext is called
|
---|
544 | * and is never freed. Whenever we get a crStateMakeCurrent(NULL) call
|
---|
545 | * or destroy the currently bound context in crStateDestroyContext()
|
---|
546 | * we call crStateSwitchContext() to switch to the default context and
|
---|
547 | * then set the __currentContext pointer to point to the default context.
|
---|
548 | * This ensures that the dirty bits are updated and the diff_api functions
|
---|
549 | * are called to keep the downstream GL machine in a known state.
|
---|
550 | * Finally, the __hwcontext variable is no longer needed now.
|
---|
551 | *
|
---|
552 | * Yeah, this is kind of a mind-bender, but it really solves the problem
|
---|
553 | * pretty cleanly.
|
---|
554 | *
|
---|
555 | * -Brian
|
---|
556 | */
|
---|
557 |
|
---|
558 |
|
---|
559 | CRContext *
|
---|
560 | crStateCreateContext(const CRLimitsState *limits, GLint visBits, CRContext *share)
|
---|
561 | {
|
---|
562 | return crStateCreateContextEx(limits, visBits, share, -1);
|
---|
563 | }
|
---|
564 |
|
---|
565 | CRContext *
|
---|
566 | crStateCreateContextEx(const CRLimitsState *limits, GLint visBits, CRContext *share, GLint presetID)
|
---|
567 | {
|
---|
568 | /* Must have created the default context via crStateInit() first */
|
---|
569 | CRASSERT(defaultContext);
|
---|
570 |
|
---|
571 | if (presetID>0)
|
---|
572 | {
|
---|
573 | if(g_pAvailableContexts[presetID])
|
---|
574 | {
|
---|
575 | crWarning("requesting to create context with already allocated id");
|
---|
576 | return NULL;
|
---|
577 | }
|
---|
578 | }
|
---|
579 | else
|
---|
580 | {
|
---|
581 | int i;
|
---|
582 |
|
---|
583 | for (i = 1 ; i < CR_MAX_CONTEXTS ; i++)
|
---|
584 | {
|
---|
585 | if (!g_pAvailableContexts[i])
|
---|
586 | {
|
---|
587 | presetID = i;
|
---|
588 | break;
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 | if (presetID<=0)
|
---|
593 | {
|
---|
594 | crError( "Out of available contexts in crStateCreateContexts (max %d)",
|
---|
595 | CR_MAX_CONTEXTS );
|
---|
596 | /* never get here */
|
---|
597 | return NULL;
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | return crStateCreateContextId(presetID, limits, visBits, share);
|
---|
602 | }
|
---|
603 |
|
---|
604 | void crStateDestroyContext( CRContext *ctx )
|
---|
605 | {
|
---|
606 | CRContext *current = GetCurrentContext();
|
---|
607 |
|
---|
608 | if (current == ctx) {
|
---|
609 | /* destroying the current context - have to be careful here */
|
---|
610 | CRASSERT(defaultContext);
|
---|
611 | /* Check to see if the differencer exists first,
|
---|
612 | we may not have one, aka the packspu */
|
---|
613 | if (diff_api.AlphaFunc)
|
---|
614 | crStateSwitchContext(current, defaultContext);
|
---|
615 | #ifdef CHROMIUM_THREADSAFE
|
---|
616 | SetCurrentContext(defaultContext);
|
---|
617 | #else
|
---|
618 | __currentContext = defaultContext;
|
---|
619 | #endif
|
---|
620 | /* ensure matrix state is also current */
|
---|
621 | crStateMatrixMode(defaultContext->transform.matrixMode);
|
---|
622 | }
|
---|
623 |
|
---|
624 | #ifdef CHROMIUM_THREADSAFE
|
---|
625 | VBoxTlsRefMarkDestroy(ctx);
|
---|
626 | # ifdef IN_GUEST
|
---|
627 | if (VBoxTlsRefCountGet(ctx) > 1 && ctx->shared == gSharedState)
|
---|
628 | {
|
---|
629 | /* we always need to free the global shared state to prevent the situation when guest thinks the shared objects are still valid, while host destroys them */
|
---|
630 | crStateFreeShared(ctx, ctx->shared);
|
---|
631 | ctx->shared = crStateAllocShared();
|
---|
632 | }
|
---|
633 | # endif
|
---|
634 | VBoxTlsRefRelease(ctx);
|
---|
635 | #else
|
---|
636 | crStateFreeContext(ctx);
|
---|
637 | #endif
|
---|
638 | }
|
---|
639 |
|
---|
640 | GLboolean crStateEnableDiffOnMakeCurrent(GLboolean fEnable)
|
---|
641 | {
|
---|
642 | GLboolean bOld = g_bVBoxEnableDiffOnMakeCurrent;
|
---|
643 | g_bVBoxEnableDiffOnMakeCurrent = fEnable;
|
---|
644 | return bOld;
|
---|
645 | }
|
---|
646 |
|
---|
647 | void crStateMakeCurrent( CRContext *ctx )
|
---|
648 | {
|
---|
649 | CRContext *current = GetCurrentContext();
|
---|
650 |
|
---|
651 | if (ctx == NULL)
|
---|
652 | ctx = defaultContext;
|
---|
653 |
|
---|
654 | if (current == ctx)
|
---|
655 | return; /* no-op */
|
---|
656 |
|
---|
657 | CRASSERT(ctx);
|
---|
658 |
|
---|
659 | if (g_bVBoxEnableDiffOnMakeCurrent && current) {
|
---|
660 | /* Check to see if the differencer exists first,
|
---|
661 | we may not have one, aka the packspu */
|
---|
662 | if (diff_api.AlphaFunc)
|
---|
663 | crStateSwitchContext( current, ctx );
|
---|
664 | }
|
---|
665 |
|
---|
666 | #ifdef CHROMIUM_THREADSAFE
|
---|
667 | SetCurrentContext(ctx);
|
---|
668 | #else
|
---|
669 | __currentContext = ctx;
|
---|
670 | #endif
|
---|
671 |
|
---|
672 | /* ensure matrix state is also current */
|
---|
673 | crStateMatrixMode(ctx->transform.matrixMode);
|
---|
674 | }
|
---|
675 |
|
---|
676 |
|
---|
677 | /*
|
---|
678 | * As above, but don't call crStateSwitchContext().
|
---|
679 | */
|
---|
680 | void crStateSetCurrent( CRContext *ctx )
|
---|
681 | {
|
---|
682 | CRContext *current = GetCurrentContext();
|
---|
683 |
|
---|
684 | if (ctx == NULL)
|
---|
685 | ctx = defaultContext;
|
---|
686 |
|
---|
687 | if (current == ctx)
|
---|
688 | return; /* no-op */
|
---|
689 |
|
---|
690 | CRASSERT(ctx);
|
---|
691 |
|
---|
692 | #ifdef CHROMIUM_THREADSAFE
|
---|
693 | SetCurrentContext(ctx);
|
---|
694 | #else
|
---|
695 | __currentContext = ctx;
|
---|
696 | #endif
|
---|
697 |
|
---|
698 | /* ensure matrix state is also current */
|
---|
699 | crStateMatrixMode(ctx->transform.matrixMode);
|
---|
700 | }
|
---|
701 |
|
---|
702 |
|
---|
703 | CRContext *crStateGetCurrent(void)
|
---|
704 | {
|
---|
705 | return GetCurrentContext();
|
---|
706 | }
|
---|
707 |
|
---|
708 |
|
---|
709 | void crStateUpdateColorBits(void)
|
---|
710 | {
|
---|
711 | /* This is a hack to force updating the 'current' attribs */
|
---|
712 | CRStateBits *sb = GetCurrentBits();
|
---|
713 | FILLDIRTY(sb->current.dirty);
|
---|
714 | FILLDIRTY(sb->current.vertexAttrib[VERT_ATTRIB_COLOR0]);
|
---|
715 | }
|
---|
716 |
|
---|
717 |
|
---|
718 | void STATE_APIENTRY
|
---|
719 | crStateChromiumParameteriCR( GLenum target, GLint value )
|
---|
720 | {
|
---|
721 | /* This no-op function helps smooth code-gen */
|
---|
722 | }
|
---|
723 |
|
---|
724 | void STATE_APIENTRY
|
---|
725 | crStateChromiumParameterfCR( GLenum target, GLfloat value )
|
---|
726 | {
|
---|
727 | /* This no-op function helps smooth code-gen */
|
---|
728 | }
|
---|
729 |
|
---|
730 | void STATE_APIENTRY
|
---|
731 | crStateChromiumParametervCR( GLenum target, GLenum type, GLsizei count, const GLvoid *values )
|
---|
732 | {
|
---|
733 | /* This no-op function helps smooth code-gen */
|
---|
734 | }
|
---|
735 |
|
---|
736 | void STATE_APIENTRY
|
---|
737 | crStateGetChromiumParametervCR( GLenum target, GLuint index, GLenum type, GLsizei count, GLvoid *values )
|
---|
738 | {
|
---|
739 | /* This no-op function helps smooth code-gen */
|
---|
740 | }
|
---|
741 |
|
---|
742 | void STATE_APIENTRY
|
---|
743 | crStateReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
|
---|
744 | GLenum format, GLenum type, GLvoid *pixels )
|
---|
745 | {
|
---|
746 | /* This no-op function helps smooth code-gen */
|
---|
747 | }
|
---|
748 |
|
---|
749 | void crStateVBoxDetachThread()
|
---|
750 | {
|
---|
751 | /* release the context ref so that it can be freed */
|
---|
752 | SetCurrentContext(NULL);
|
---|
753 | }
|
---|
754 |
|
---|
755 |
|
---|
756 | void crStateVBoxAttachThread()
|
---|
757 | {
|
---|
758 | }
|
---|
759 |
|
---|
760 | GLint crStateVBoxCreateContext( GLint con, const char * dpyName, GLint visual, GLint shareCtx )
|
---|
761 | {
|
---|
762 | return 0;
|
---|
763 | }
|
---|
764 |
|
---|
765 | GLint crStateVBoxWindowCreate( GLint con, const char *dpyName, GLint visBits )
|
---|
766 | {
|
---|
767 | return 0;
|
---|
768 | }
|
---|
769 |
|
---|
770 | void crStateVBoxWindowDestroy( GLint con, GLint window )
|
---|
771 | {
|
---|
772 | }
|
---|
773 |
|
---|
774 | GLint crStateVBoxConCreate(struct VBOXUHGSMI *pHgsmi)
|
---|
775 | {
|
---|
776 | return 0;
|
---|
777 | }
|
---|
778 |
|
---|
779 | void crStateVBoxConDestroy(GLint con)
|
---|
780 | {
|
---|
781 | }
|
---|
782 |
|
---|
783 | void crStateVBoxConFlush(GLint con)
|
---|
784 | {
|
---|
785 | }
|
---|