VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_context.cpp@ 78263

Last change on this file since 78263 was 78263, checked in by vboxsync, 6 years ago

Config.kmk,GuestHost\OpenGL,HostServices\SharedOpenGL: Fix a bunch of compiler warnings and enable them again

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