VirtualBox

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

Last change on this file since 16969 was 15532, checked in by vboxsync, 16 years ago

crOpenGL: export to OSE

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