VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_context.c@ 44352

最後變更 在這個檔案從44352是 44290,由 vboxsync 提交於 12 年 前

crOpenGL: saved state fixes & improvements

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 15.6 KB
 
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 "cr_spu.h"
8#include "chromium.h"
9#include "cr_error.h"
10#include "cr_net.h"
11#include "cr_rand.h"
12#include "server_dispatch.h"
13#include "server.h"
14#include "cr_mem.h"
15#include "cr_string.h"
16
17GLint SERVER_DISPATCH_APIENTRY
18crServerDispatchCreateContext(const char *dpyName, GLint visualBits, GLint shareCtx)
19{
20 return crServerDispatchCreateContextEx(dpyName, visualBits, shareCtx, -1, -1);
21}
22
23GLint crServerDispatchCreateContextEx(const char *dpyName, GLint visualBits, GLint shareCtx, GLint preloadCtxID, int32_t internalID)
24{
25 GLint retVal = -1;
26 CRContext *newCtx;
27 CRContextInfo *pContextInfo;
28 GLboolean fFirst = GL_FALSE;
29
30 if (shareCtx > 0) {
31 crWarning("CRServer: context sharing not implemented.");
32 shareCtx = 0;
33 }
34
35 pContextInfo = (CRContextInfo *) crAlloc(sizeof (CRContextInfo));
36 if (!pContextInfo)
37 {
38 crWarning("failed to alloc context info!");
39 return -1;
40 }
41
42 pContextInfo->currentMural = NULL;
43
44 pContextInfo->CreateInfo.visualBits = visualBits;
45
46 /* Since the Cr server serialized all incoming clients/contexts into
47 * one outgoing GL stream, we only need to create one context for the
48 * head SPU. We'll only have to make it current once too, below.
49 */
50 if (cr_server.firstCallCreateContext) {
51 cr_server.MainContextInfo.CreateInfo.visualBits = visualBits;
52 cr_server.MainContextInfo.SpuContext = cr_server.head_spu->dispatch_table.
53 CreateContext(dpyName, cr_server.MainContextInfo.CreateInfo.visualBits, shareCtx);
54 if (cr_server.MainContextInfo.SpuContext < 0) {
55 crWarning("crServerDispatchCreateContext() failed.");
56 crFree(pContextInfo);
57 return -1;
58 }
59 cr_server.MainContextInfo.pContext = crStateCreateContext(&cr_server.limits, visualBits, NULL);
60 CRASSERT(cr_server.MainContextInfo.pContext);
61 cr_server.firstCallCreateContext = GL_FALSE;
62 fFirst = GL_TRUE;
63 }
64 else {
65 /* second or third or ... context */
66 if (!cr_server.bUseMultipleContexts && ((visualBits & cr_server.MainContextInfo.CreateInfo.visualBits) != visualBits)) {
67 int oldSpuContext;
68
69 /* the new context needs new visual attributes */
70 cr_server.MainContextInfo.CreateInfo.visualBits |= visualBits;
71 crDebug("crServerDispatchCreateContext requires new visual (0x%x).",
72 cr_server.MainContextInfo.CreateInfo.visualBits);
73
74 /* Here, we used to just destroy the old rendering context.
75 * Unfortunately, this had the side effect of destroying
76 * all display lists and textures that had been loaded on
77 * the old context as well.
78 *
79 * Now, first try to create a new context, with a suitable
80 * visual, sharing display lists and textures with the
81 * old context. Then destroy the old context.
82 */
83
84 /* create new rendering context with suitable visual */
85 oldSpuContext = cr_server.MainContextInfo.SpuContext;
86 cr_server.MainContextInfo.SpuContext = cr_server.head_spu->dispatch_table.
87 CreateContext(dpyName, cr_server.MainContextInfo.CreateInfo.visualBits, cr_server.MainContextInfo.SpuContext);
88 /* destroy old rendering context */
89 cr_server.head_spu->dispatch_table.DestroyContext(oldSpuContext);
90 if (cr_server.MainContextInfo.SpuContext < 0) {
91 crWarning("crServerDispatchCreateContext() failed.");
92 crFree(pContextInfo);
93 return -1;
94 }
95 }
96 }
97
98 if (cr_server.bUseMultipleContexts) {
99 pContextInfo->SpuContext = cr_server.head_spu->dispatch_table.
100 CreateContext(dpyName, cr_server.MainContextInfo.CreateInfo.visualBits, cr_server.MainContextInfo.SpuContext);
101 if (pContextInfo->SpuContext < 0) {
102 crWarning("crServerDispatchCreateContext() failed.");
103 crStateEnableDiffOnMakeCurrent(GL_TRUE);
104 cr_server.bUseMultipleContexts = GL_FALSE;
105 if (!fFirst)
106 crError("creating shared context failed, while it is expected to work!");
107 }
108 else if (fFirst)
109 {
110 crStateEnableDiffOnMakeCurrent(GL_FALSE);
111 }
112 }
113 else
114 {
115 pContextInfo->SpuContext = -1;
116 }
117
118 /* Now create a new state-tracker context and initialize the
119 * dispatch function pointers.
120 */
121 newCtx = crStateCreateContextEx(&cr_server.limits, visualBits, NULL, internalID);
122 if (newCtx) {
123 crStateSetCurrentPointers( newCtx, &(cr_server.current) );
124 crStateResetCurrentPointers(&(cr_server.current));
125 retVal = preloadCtxID<0 ? (GLint)crHashtableAllocKeys( cr_server.contextTable, 1 ) : preloadCtxID;
126
127 pContextInfo->pContext = newCtx;
128 pContextInfo->CreateInfo.visualBits = visualBits;
129 pContextInfo->CreateInfo.externalID = retVal;
130 pContextInfo->CreateInfo.pszDpyName = dpyName ? crStrdup(dpyName) : NULL;
131 crHashtableAdd(cr_server.contextTable, retVal, pContextInfo);
132 }
133
134 if (retVal != -1 && !cr_server.bIsInLoadingState) {
135 int pos;
136 for (pos = 0; pos < CR_MAX_CONTEXTS; pos++) {
137 if (cr_server.curClient->contextList[pos] == 0) {
138 cr_server.curClient->contextList[pos] = retVal;
139 break;
140 }
141 }
142 }
143
144 crServerReturnValue( &retVal, sizeof(retVal) );
145
146 return retVal;
147}
148
149static int crServerRemoveClientContext(CRClient *pClient, GLint ctx)
150{
151 int pos;
152
153 for (pos = 0; pos < CR_MAX_CONTEXTS; ++pos)
154 {
155 if (pClient->contextList[pos] == ctx)
156 {
157 pClient->contextList[pos] = 0;
158 return true;
159 }
160 }
161
162 return false;
163}
164
165static void crServerCleanupMuralCtxUsageCB(unsigned long key, void *data1, void *data2)
166{
167 CRMuralInfo *mural = (CRMuralInfo *) data1;
168 CRContext *ctx = (CRContext *) data2;
169
170 CR_STATE_SHAREDOBJ_USAGE_CLEAR(mural, ctx);
171}
172
173void SERVER_DISPATCH_APIENTRY
174crServerDispatchDestroyContext( GLint ctx )
175{
176 CRContextInfo *crCtxInfo;
177 CRContext *crCtx;
178 int32_t client;
179 CRClientNode *pNode;
180 int found=false;
181
182 crCtxInfo = (CRContextInfo *) crHashtableSearch(cr_server.contextTable, ctx);
183 if (!crCtxInfo) {
184 crWarning("CRServer: DestroyContext invalid context %d", ctx);
185 return;
186 }
187 crCtx = crCtxInfo->pContext;
188 CRASSERT(crCtx);
189
190 crDebug("CRServer: DestroyContext context %d", ctx);
191
192 crHashtableWalk(cr_server.muralTable, crServerCleanupMuralCtxUsageCB, crCtx);
193 crCtxInfo->currentMural = NULL;
194 crHashtableDelete(cr_server.contextTable, ctx, NULL);
195 crStateDestroyContext( crCtx );
196
197 if (crCtxInfo->CreateInfo.pszDpyName)
198 crFree(crCtxInfo->CreateInfo.pszDpyName);
199
200 if (crCtxInfo->SpuContext >= 0)
201 cr_server.head_spu->dispatch_table.DestroyContext(crCtxInfo->SpuContext);
202
203 crFree(crCtxInfo);
204
205 if (cr_server.curClient)
206 {
207 /* If we delete our current context, default back to the null context */
208 if (cr_server.curClient->currentCtxInfo == crCtxInfo) {
209 cr_server.curClient->currentContextNumber = -1;
210 cr_server.curClient->currentCtxInfo = &cr_server.MainContextInfo;
211 }
212
213 found = crServerRemoveClientContext(cr_server.curClient, ctx);
214
215 /*Some application call destroy context not in a thread where it was created...have do deal with it.*/
216 if (!found)
217 {
218 for (client=0; client<cr_server.numClients; ++client)
219 {
220 if (cr_server.clients[client]==cr_server.curClient)
221 continue;
222
223 found = crServerRemoveClientContext(cr_server.clients[client], ctx);
224
225 if (found) break;
226 }
227 }
228
229 if (!found)
230 {
231 pNode=cr_server.pCleanupClient;
232
233 while (pNode && !found)
234 {
235 found = crServerRemoveClientContext(pNode->pClient, ctx);
236 pNode = pNode->next;
237 }
238 }
239
240 CRASSERT(found);
241 }
242
243 /*Make sure this context isn't active in other clients*/
244 for (client=0; client<cr_server.numClients; ++client)
245 {
246 if (cr_server.clients[client]->currentCtxInfo == crCtxInfo)
247 {
248 cr_server.clients[client]->currentContextNumber = -1;
249 cr_server.clients[client]->currentCtxInfo = &cr_server.MainContextInfo;
250 }
251 }
252
253 pNode=cr_server.pCleanupClient;
254 while (pNode)
255 {
256 if (pNode->pClient->currentCtxInfo == crCtxInfo)
257 {
258 pNode->pClient->currentContextNumber = -1;
259 pNode->pClient->currentCtxInfo = &cr_server.MainContextInfo;
260 }
261 pNode = pNode->next;
262 }
263
264 if (cr_server.currentCtxInfo == crCtxInfo)
265 {
266 cr_server.currentCtxInfo = &cr_server.MainContextInfo;
267 }
268}
269
270void crServerPerformMakeCurrent( CRMuralInfo *mural, CRContextInfo *ctxInfo )
271{
272 CRMuralInfo *oldMural;
273 CRContext *ctx, *oldCtx = NULL;
274 GLuint idDrawFBO, idReadFBO;
275 GLint context = ctxInfo->CreateInfo.externalID;
276 GLint window = mural->CreateInfo.externalID;
277
278 cr_server.bForceMakeCurrentOnClientSwitch = GL_FALSE;
279
280 ctx = ctxInfo->pContext;
281 CRASSERT(ctx);
282
283 oldMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, cr_server.currentWindow);
284
285 /* Ubuntu 11.04 hosts misbehave if context window switch is
286 * done with non-default framebuffer object settings.
287 * crStateSwitchPrepare & crStateSwitchPostprocess are supposed to work around this problem
288 * crStateSwitchPrepare restores the FBO state to its default values before the context window switch,
289 * while crStateSwitchPostprocess restores it back to the original values */
290 oldCtx = crStateGetCurrent();
291 if (oldMural && oldMural->fUseFBO && crServerSupportRedirMuralFBO())
292 {
293 idDrawFBO = oldMural->aidFBOs[oldMural->iCurDrawBuffer];
294 idReadFBO = oldMural->aidFBOs[oldMural->iCurReadBuffer];
295 }
296 else
297 {
298 idDrawFBO = 0;
299 idReadFBO = 0;
300 }
301 crStateSwitchPrepare(cr_server.bUseMultipleContexts ? NULL : ctx, oldCtx, idDrawFBO, idReadFBO);
302
303 /*
304 crDebug("**** %s client %d curCtx=%d curWin=%d", __func__,
305 cr_server.curClient->number, ctxPos, window);
306 */
307 cr_server.curClient->currentContextNumber = context;
308 cr_server.curClient->currentCtxInfo = ctxInfo;
309 cr_server.curClient->currentMural = mural;
310 cr_server.curClient->currentWindow = window;
311
312 CRASSERT(cr_server.curClient->currentCtxInfo);
313 CRASSERT(cr_server.curClient->currentCtxInfo->pContext);
314
315 /* This is a hack to force updating the 'current' attribs */
316 crStateUpdateColorBits();
317
318 if (ctx)
319 crStateSetCurrentPointers( ctx, &(cr_server.current) );
320
321 /* check if being made current for first time, update viewport */
322#if 0
323 if (ctx) {
324 /* initialize the viewport */
325 if (ctx->viewport.viewportW == 0) {
326 ctx->viewport.viewportW = mural->width;
327 ctx->viewport.viewportH = mural->height;
328 ctx->viewport.scissorW = mural->width;
329 ctx->viewport.scissorH = mural->height;
330 }
331 }
332#endif
333
334 /*
335 crDebug("**** %s currentWindow %d newWindow %d", __func__,
336 cr_server.currentWindow, window);
337 */
338
339 if (1/*cr_server.firstCallMakeCurrent ||
340 cr_server.currentWindow != window ||
341 cr_server.currentNativeWindow != nativeWindow*/) {
342 /* Since the cr server serialized all incoming contexts/clients into
343 * one output stream of GL commands, we only need to call the head
344 * SPU's MakeCurrent() function once.
345 * BUT, if we're rendering to multiple windows, we do have to issue
346 * MakeCurrent() calls sometimes. The same GL context will always be
347 * used though.
348 */
349 cr_server.head_spu->dispatch_table.MakeCurrent( mural->spuWindow,
350 0,
351 ctxInfo->SpuContext >= 0
352 ? ctxInfo->SpuContext
353 : cr_server.MainContextInfo.SpuContext);
354
355 CR_STATE_SHAREDOBJ_USAGE_SET(mural, ctx);
356 if (cr_server.currentCtxInfo)
357 cr_server.currentCtxInfo->currentMural = NULL;
358 ctxInfo->currentMural = mural;
359
360 cr_server.firstCallMakeCurrent = GL_FALSE;
361 cr_server.currentCtxInfo = ctxInfo;
362 cr_server.currentWindow = window;
363 cr_server.currentNativeWindow = 0;
364 cr_server.currentMural = mural;
365 }
366
367 /* This used to be earlier, after crStateUpdateColorBits() call */
368 crStateMakeCurrent( ctx );
369
370 if (mural && mural->fUseFBO && crServerSupportRedirMuralFBO())
371 {
372 GLuint id = crServerMuralFBOIdxFromBufferName(mural, ctx->buffer.drawBuffer);
373 if (id != mural->iCurDrawBuffer)
374 {
375 crWarning("DBO draw buffer changed on make current");
376 mural->iCurDrawBuffer = id;
377 }
378
379 id = crServerMuralFBOIdxFromBufferName(mural, ctx->buffer.readBuffer);
380 if (id != mural->iCurReadBuffer)
381 {
382 crWarning("DBO read buffer changed on make current");
383 mural->iCurReadBuffer = id;
384 }
385
386 idDrawFBO = mural->aidFBOs[mural->iCurDrawBuffer];
387 idReadFBO = mural->aidFBOs[mural->iCurReadBuffer];
388 }
389 else
390 {
391 idDrawFBO = 0;
392 idReadFBO = 0;
393 }
394 crStateSwitchPostprocess(ctx, cr_server.bUseMultipleContexts ? NULL : oldCtx, idDrawFBO, idReadFBO);
395
396 if (!ctx->framebufferobject.drawFB
397 && (ctx->buffer.drawBuffer == GL_FRONT || ctx->buffer.drawBuffer == GL_FRONT_LEFT))
398 cr_server.curClient->currentMural->bFbDraw = GL_TRUE;
399
400 if (!mural->fUseFBO)
401 {
402 ctx->buffer.width = mural->width;
403 ctx->buffer.height = mural->height;
404 }
405 else
406 {
407 ctx->buffer.width = 0;
408 ctx->buffer.height = 0;
409 }
410}
411
412
413void SERVER_DISPATCH_APIENTRY
414crServerDispatchMakeCurrent( GLint window, GLint nativeWindow, GLint context )
415{
416 CRMuralInfo *mural;
417 CRContextInfo *ctxInfo = NULL;
418
419 if (context >= 0 && window >= 0) {
420 mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
421 if (!mural)
422 {
423 crWarning("CRServer: invalid window %d passed to crServerDispatchMakeCurrent()", window);
424 return;
425 }
426
427 /* Update the state tracker's current context */
428 ctxInfo = (CRContextInfo *) crHashtableSearch(cr_server.contextTable, context);
429 if (!ctxInfo) {
430 crWarning("CRserver: NULL context in MakeCurrent %d", context);
431 return;
432 }
433 }
434 else {
435#if 0
436 oldMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, cr_server.currentWindow);
437 if (oldMural && oldMural->bUseFBO && crServerSupportRedirMuralFBO())
438 {
439 if (!crStateGetCurrent()->framebufferobject.drawFB)
440 {
441 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
442 }
443 if (!crStateGetCurrent()->framebufferobject.readFB)
444 {
445 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
446 }
447 }
448
449 ctxInfo = &cr_server.MainContextInfo;
450 window = -1;
451 mural = NULL;
452#endif
453 cr_server.bForceMakeCurrentOnClientSwitch = GL_TRUE;
454 return;
455 }
456
457 crServerPerformMakeCurrent( mural, ctxInfo );
458}
459
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette