VirtualBox

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

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

crOpenGL: window destuction fix

  • 屬性 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 int spuContext;
182
183 crCtxInfo = (CRContextInfo *) crHashtableSearch(cr_server.contextTable, ctx);
184 if (!crCtxInfo) {
185 crWarning("CRServer: DestroyContext invalid context %d", ctx);
186 return;
187 }
188 crCtx = crCtxInfo->pContext;
189 CRASSERT(crCtx);
190
191 crDebug("CRServer: DestroyContext context %d", ctx);
192
193 crHashtableWalk(cr_server.muralTable, crServerCleanupMuralCtxUsageCB, crCtx);
194 crCtxInfo->currentMural = NULL;
195 crHashtableDelete(cr_server.contextTable, ctx, NULL);
196 crStateDestroyContext( crCtx );
197
198 if (crCtxInfo->CreateInfo.pszDpyName)
199 crFree(crCtxInfo->CreateInfo.pszDpyName);
200
201 spuContext = 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 if (spuContext >= 0)
270 cr_server.head_spu->dispatch_table.DestroyContext(spuContext);
271}
272
273void crServerPerformMakeCurrent( CRMuralInfo *mural, CRContextInfo *ctxInfo )
274{
275 CRMuralInfo *oldMural;
276 CRContext *ctx, *oldCtx = NULL;
277 GLuint idDrawFBO, idReadFBO;
278 GLint context = ctxInfo->CreateInfo.externalID;
279 GLint window = mural->CreateInfo.externalID;
280
281 cr_server.bForceMakeCurrentOnClientSwitch = GL_FALSE;
282
283 ctx = ctxInfo->pContext;
284 CRASSERT(ctx);
285
286 oldMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, cr_server.currentWindow);
287
288 /* Ubuntu 11.04 hosts misbehave if context window switch is
289 * done with non-default framebuffer object settings.
290 * crStateSwitchPrepare & crStateSwitchPostprocess are supposed to work around this problem
291 * crStateSwitchPrepare restores the FBO state to its default values before the context window switch,
292 * while crStateSwitchPostprocess restores it back to the original values */
293 oldCtx = crStateGetCurrent();
294 if (oldMural && oldMural->fUseFBO && crServerSupportRedirMuralFBO())
295 {
296 idDrawFBO = oldMural->aidFBOs[oldMural->iCurDrawBuffer];
297 idReadFBO = oldMural->aidFBOs[oldMural->iCurReadBuffer];
298 }
299 else
300 {
301 idDrawFBO = 0;
302 idReadFBO = 0;
303 }
304 crStateSwitchPrepare(cr_server.bUseMultipleContexts ? NULL : ctx, oldCtx, idDrawFBO, idReadFBO);
305
306 /*
307 crDebug("**** %s client %d curCtx=%d curWin=%d", __func__,
308 cr_server.curClient->number, ctxPos, window);
309 */
310 cr_server.curClient->currentContextNumber = context;
311 cr_server.curClient->currentCtxInfo = ctxInfo;
312 cr_server.curClient->currentMural = mural;
313 cr_server.curClient->currentWindow = window;
314
315 CRASSERT(cr_server.curClient->currentCtxInfo);
316 CRASSERT(cr_server.curClient->currentCtxInfo->pContext);
317
318 /* This is a hack to force updating the 'current' attribs */
319 crStateUpdateColorBits();
320
321 if (ctx)
322 crStateSetCurrentPointers( ctx, &(cr_server.current) );
323
324 /* check if being made current for first time, update viewport */
325#if 0
326 if (ctx) {
327 /* initialize the viewport */
328 if (ctx->viewport.viewportW == 0) {
329 ctx->viewport.viewportW = mural->width;
330 ctx->viewport.viewportH = mural->height;
331 ctx->viewport.scissorW = mural->width;
332 ctx->viewport.scissorH = mural->height;
333 }
334 }
335#endif
336
337 /*
338 crDebug("**** %s currentWindow %d newWindow %d", __func__,
339 cr_server.currentWindow, window);
340 */
341
342 if (1/*cr_server.firstCallMakeCurrent ||
343 cr_server.currentWindow != window ||
344 cr_server.currentNativeWindow != nativeWindow*/) {
345 /* Since the cr server serialized all incoming contexts/clients into
346 * one output stream of GL commands, we only need to call the head
347 * SPU's MakeCurrent() function once.
348 * BUT, if we're rendering to multiple windows, we do have to issue
349 * MakeCurrent() calls sometimes. The same GL context will always be
350 * used though.
351 */
352 cr_server.head_spu->dispatch_table.MakeCurrent( mural->spuWindow,
353 0,
354 ctxInfo->SpuContext >= 0
355 ? ctxInfo->SpuContext
356 : cr_server.MainContextInfo.SpuContext);
357
358 CR_STATE_SHAREDOBJ_USAGE_SET(mural, ctx);
359 if (cr_server.currentCtxInfo)
360 cr_server.currentCtxInfo->currentMural = NULL;
361 ctxInfo->currentMural = mural;
362
363 cr_server.firstCallMakeCurrent = GL_FALSE;
364 cr_server.currentCtxInfo = ctxInfo;
365 cr_server.currentWindow = window;
366 cr_server.currentNativeWindow = 0;
367 cr_server.currentMural = mural;
368 }
369
370 /* This used to be earlier, after crStateUpdateColorBits() call */
371 crStateMakeCurrent( ctx );
372
373 if (mural && mural->fUseFBO && crServerSupportRedirMuralFBO())
374 {
375 GLuint id = crServerMuralFBOIdxFromBufferName(mural, ctx->buffer.drawBuffer);
376 if (id != mural->iCurDrawBuffer)
377 {
378 crWarning("DBO draw buffer changed on make current");
379 mural->iCurDrawBuffer = id;
380 }
381
382 id = crServerMuralFBOIdxFromBufferName(mural, ctx->buffer.readBuffer);
383 if (id != mural->iCurReadBuffer)
384 {
385 crWarning("DBO read buffer changed on make current");
386 mural->iCurReadBuffer = id;
387 }
388
389 idDrawFBO = mural->aidFBOs[mural->iCurDrawBuffer];
390 idReadFBO = mural->aidFBOs[mural->iCurReadBuffer];
391 }
392 else
393 {
394 idDrawFBO = 0;
395 idReadFBO = 0;
396 }
397 crStateSwitchPostprocess(ctx, cr_server.bUseMultipleContexts ? NULL : oldCtx, idDrawFBO, idReadFBO);
398
399 if (!ctx->framebufferobject.drawFB
400 && (ctx->buffer.drawBuffer == GL_FRONT || ctx->buffer.drawBuffer == GL_FRONT_LEFT))
401 cr_server.curClient->currentMural->bFbDraw = GL_TRUE;
402
403 if (!mural->fUseFBO)
404 {
405 ctx->buffer.width = mural->width;
406 ctx->buffer.height = mural->height;
407 }
408 else
409 {
410 ctx->buffer.width = 0;
411 ctx->buffer.height = 0;
412 }
413}
414
415
416void SERVER_DISPATCH_APIENTRY
417crServerDispatchMakeCurrent( GLint window, GLint nativeWindow, GLint context )
418{
419 CRMuralInfo *mural;
420 CRContextInfo *ctxInfo = NULL;
421
422 if (context >= 0 && window >= 0) {
423 mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
424 if (!mural)
425 {
426 crWarning("CRServer: invalid window %d passed to crServerDispatchMakeCurrent()", window);
427 return;
428 }
429
430 /* Update the state tracker's current context */
431 ctxInfo = (CRContextInfo *) crHashtableSearch(cr_server.contextTable, context);
432 if (!ctxInfo) {
433 crWarning("CRserver: NULL context in MakeCurrent %d", context);
434 return;
435 }
436 }
437 else {
438#if 0
439 oldMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, cr_server.currentWindow);
440 if (oldMural && oldMural->bUseFBO && crServerSupportRedirMuralFBO())
441 {
442 if (!crStateGetCurrent()->framebufferobject.drawFB)
443 {
444 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
445 }
446 if (!crStateGetCurrent()->framebufferobject.readFB)
447 {
448 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
449 }
450 }
451
452 ctxInfo = &cr_server.MainContextInfo;
453 window = -1;
454 mural = NULL;
455#endif
456 cr_server.bForceMakeCurrentOnClientSwitch = GL_TRUE;
457 return;
458 }
459
460 crServerPerformMakeCurrent( mural, ctxInfo );
461}
462
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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