VirtualBox

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

Last change on this file since 27159 was 25575, checked in by vboxsync, 15 years ago

crOpenGL: free textures on guest appliction termination (#5206)

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