VirtualBox

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

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

crOpenGL/wddm: fix refcounting for shared ctx in shapshots

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