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 |
|
---|
17 | GLint SERVER_DISPATCH_APIENTRY
|
---|
18 | crServerDispatchCreateContext(const char *dpyName, GLint visualBits, GLint shareCtx)
|
---|
19 | {
|
---|
20 | return crServerDispatchCreateContextEx(dpyName, visualBits, shareCtx, -1, -1);
|
---|
21 | }
|
---|
22 |
|
---|
23 | GLint 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->CreateInfo.visualBits = visualBits;
|
---|
43 |
|
---|
44 | /* Since the Cr server serialized all incoming clients/contexts into
|
---|
45 | * one outgoing GL stream, we only need to create one context for the
|
---|
46 | * head SPU. We'll only have to make it current once too, below.
|
---|
47 | */
|
---|
48 | if (cr_server.firstCallCreateContext) {
|
---|
49 | cr_server.MainContextInfo.CreateInfo.visualBits = visualBits;
|
---|
50 | cr_server.MainContextInfo.SpuContext = cr_server.head_spu->dispatch_table.
|
---|
51 | CreateContext(dpyName, cr_server.MainContextInfo.CreateInfo.visualBits, shareCtx);
|
---|
52 | if (cr_server.MainContextInfo.SpuContext < 0) {
|
---|
53 | crWarning("crServerDispatchCreateContext() failed.");
|
---|
54 | crFree(pContextInfo);
|
---|
55 | return -1;
|
---|
56 | }
|
---|
57 | cr_server.MainContextInfo.pContext = crStateCreateContext(&cr_server.limits, visualBits, NULL);
|
---|
58 | CRASSERT(cr_server.MainContextInfo.pContext);
|
---|
59 | cr_server.firstCallCreateContext = GL_FALSE;
|
---|
60 | fFirst = GL_TRUE;
|
---|
61 | }
|
---|
62 | else {
|
---|
63 | /* second or third or ... context */
|
---|
64 | if (!cr_server.bUseMultipleContexts && ((visualBits & cr_server.MainContextInfo.CreateInfo.visualBits) != visualBits)) {
|
---|
65 | int oldSpuContext;
|
---|
66 |
|
---|
67 | /* the new context needs new visual attributes */
|
---|
68 | cr_server.MainContextInfo.CreateInfo.visualBits |= visualBits;
|
---|
69 | crDebug("crServerDispatchCreateContext requires new visual (0x%x).",
|
---|
70 | cr_server.MainContextInfo.CreateInfo.visualBits);
|
---|
71 |
|
---|
72 | /* Here, we used to just destroy the old rendering context.
|
---|
73 | * Unfortunately, this had the side effect of destroying
|
---|
74 | * all display lists and textures that had been loaded on
|
---|
75 | * the old context as well.
|
---|
76 | *
|
---|
77 | * Now, first try to create a new context, with a suitable
|
---|
78 | * visual, sharing display lists and textures with the
|
---|
79 | * old context. Then destroy the old context.
|
---|
80 | */
|
---|
81 |
|
---|
82 | /* create new rendering context with suitable visual */
|
---|
83 | oldSpuContext = cr_server.MainContextInfo.SpuContext;
|
---|
84 | cr_server.MainContextInfo.SpuContext = cr_server.head_spu->dispatch_table.
|
---|
85 | CreateContext(dpyName, cr_server.MainContextInfo.CreateInfo.visualBits, cr_server.MainContextInfo.SpuContext);
|
---|
86 | /* destroy old rendering context */
|
---|
87 | cr_server.head_spu->dispatch_table.DestroyContext(oldSpuContext);
|
---|
88 | if (cr_server.MainContextInfo.SpuContext < 0) {
|
---|
89 | crWarning("crServerDispatchCreateContext() failed.");
|
---|
90 | crFree(pContextInfo);
|
---|
91 | return -1;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (cr_server.bUseMultipleContexts) {
|
---|
97 | pContextInfo->SpuContext = cr_server.head_spu->dispatch_table.
|
---|
98 | CreateContext(dpyName, cr_server.MainContextInfo.CreateInfo.visualBits, cr_server.MainContextInfo.SpuContext);
|
---|
99 | if (pContextInfo->SpuContext < 0) {
|
---|
100 | crWarning("crServerDispatchCreateContext() failed.");
|
---|
101 | crStateEnableDiffOnMakeCurrent(GL_TRUE);
|
---|
102 | cr_server.bUseMultipleContexts = GL_FALSE;
|
---|
103 | if (!fFirst)
|
---|
104 | crError("creating shared context failed, while it is expected to work!");
|
---|
105 | }
|
---|
106 | else if (fFirst)
|
---|
107 | {
|
---|
108 | crStateEnableDiffOnMakeCurrent(GL_FALSE);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | pContextInfo->SpuContext = -1;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /* Now create a new state-tracker context and initialize the
|
---|
117 | * dispatch function pointers.
|
---|
118 | */
|
---|
119 | newCtx = crStateCreateContextEx(&cr_server.limits, visualBits, NULL, internalID);
|
---|
120 | if (newCtx) {
|
---|
121 | crStateSetCurrentPointers( newCtx, &(cr_server.current) );
|
---|
122 | crStateResetCurrentPointers(&(cr_server.current));
|
---|
123 | retVal = preloadCtxID<0 ? crServerGenerateID(&cr_server.idsPool.freeContextID) : preloadCtxID;
|
---|
124 |
|
---|
125 | pContextInfo->pContext = newCtx;
|
---|
126 | pContextInfo->CreateInfo.visualBits = visualBits;
|
---|
127 | pContextInfo->CreateInfo.externalID = retVal;
|
---|
128 | pContextInfo->CreateInfo.pszDpyName = dpyName ? crStrdup(dpyName) : NULL;
|
---|
129 | crHashtableAdd(cr_server.contextTable, retVal, pContextInfo);
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (retVal != -1 && !cr_server.bIsInLoadingState) {
|
---|
133 | int pos;
|
---|
134 | for (pos = 0; pos < CR_MAX_CONTEXTS; pos++) {
|
---|
135 | if (cr_server.curClient->contextList[pos] == 0) {
|
---|
136 | cr_server.curClient->contextList[pos] = retVal;
|
---|
137 | break;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | {
|
---|
143 | /* As we're using only one host context to serve all client contexts, newly created context will still
|
---|
144 | * hold last error value from any previous failed opengl call. Proper solution would be to redirect any
|
---|
145 | * client glGetError calls to our state tracker, but right now it's missing quite a lot of checks and doesn't
|
---|
146 | * reflect host driver/gpu specific issues. Thus we just reset last opengl error at context creation.
|
---|
147 | */
|
---|
148 | GLint err;
|
---|
149 |
|
---|
150 | err = cr_server.head_spu->dispatch_table.GetError();
|
---|
151 | if (err!=GL_NO_ERROR)
|
---|
152 | {
|
---|
153 | #ifdef DEBUG_misha
|
---|
154 | crDebug("Cleared gl error %#x on context creation", err);
|
---|
155 | #else
|
---|
156 | crWarning("Cleared gl error %#x on context creation", err);
|
---|
157 | #endif
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | crServerReturnValue( &retVal, sizeof(retVal) );
|
---|
162 |
|
---|
163 | return retVal;
|
---|
164 | }
|
---|
165 |
|
---|
166 | static int crServerRemoveClientContext(CRClient *pClient, GLint ctx)
|
---|
167 | {
|
---|
168 | int pos;
|
---|
169 |
|
---|
170 | for (pos = 0; pos < CR_MAX_CONTEXTS; ++pos)
|
---|
171 | {
|
---|
172 | if (pClient->contextList[pos] == ctx)
|
---|
173 | {
|
---|
174 | pClient->contextList[pos] = 0;
|
---|
175 | return true;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 |
|
---|
182 | void SERVER_DISPATCH_APIENTRY
|
---|
183 | crServerDispatchDestroyContext( GLint ctx )
|
---|
184 | {
|
---|
185 | CRContextInfo *crCtxInfo;
|
---|
186 | CRContext *crCtx;
|
---|
187 | int32_t client;
|
---|
188 | CRClientNode *pNode;
|
---|
189 | int found=false;
|
---|
190 |
|
---|
191 | crCtxInfo = (CRContextInfo *) crHashtableSearch(cr_server.contextTable, ctx);
|
---|
192 | if (!crCtxInfo) {
|
---|
193 | crWarning("CRServer: DestroyContext invalid context %d", ctx);
|
---|
194 | return;
|
---|
195 | }
|
---|
196 | crCtx = crCtxInfo->pContext;
|
---|
197 | CRASSERT(crCtx);
|
---|
198 |
|
---|
199 | crDebug("CRServer: DestroyContext context %d", ctx);
|
---|
200 |
|
---|
201 | crHashtableDelete(cr_server.contextTable, ctx, NULL);
|
---|
202 | crStateDestroyContext( crCtx );
|
---|
203 |
|
---|
204 | if (crCtxInfo->CreateInfo.pszDpyName)
|
---|
205 | crFree(crCtxInfo->CreateInfo.pszDpyName);
|
---|
206 |
|
---|
207 | if (crCtxInfo->SpuContext >= 0)
|
---|
208 | cr_server.head_spu->dispatch_table.DestroyContext(crCtxInfo->SpuContext);
|
---|
209 |
|
---|
210 | crFree(crCtxInfo);
|
---|
211 |
|
---|
212 | if (cr_server.curClient)
|
---|
213 | {
|
---|
214 | /* If we delete our current context, default back to the null context */
|
---|
215 | if (cr_server.curClient->currentCtxInfo == crCtxInfo) {
|
---|
216 | cr_server.curClient->currentContextNumber = -1;
|
---|
217 | cr_server.curClient->currentCtxInfo = &cr_server.MainContextInfo;
|
---|
218 | }
|
---|
219 |
|
---|
220 | found = crServerRemoveClientContext(cr_server.curClient, ctx);
|
---|
221 |
|
---|
222 | /*Some application call destroy context not in a thread where it was created...have do deal with it.*/
|
---|
223 | if (!found)
|
---|
224 | {
|
---|
225 | for (client=0; client<cr_server.numClients; ++client)
|
---|
226 | {
|
---|
227 | if (cr_server.clients[client]==cr_server.curClient)
|
---|
228 | continue;
|
---|
229 |
|
---|
230 | found = crServerRemoveClientContext(cr_server.clients[client], ctx);
|
---|
231 |
|
---|
232 | if (found) break;
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (!found)
|
---|
237 | {
|
---|
238 | pNode=cr_server.pCleanupClient;
|
---|
239 |
|
---|
240 | while (pNode && !found)
|
---|
241 | {
|
---|
242 | found = crServerRemoveClientContext(pNode->pClient, ctx);
|
---|
243 | pNode = pNode->next;
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | CRASSERT(found);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*Make sure this context isn't active in other clients*/
|
---|
251 | for (client=0; client<cr_server.numClients; ++client)
|
---|
252 | {
|
---|
253 | if (cr_server.clients[client]->currentCtxInfo == crCtxInfo)
|
---|
254 | {
|
---|
255 | cr_server.clients[client]->currentContextNumber = -1;
|
---|
256 | cr_server.clients[client]->currentCtxInfo = &cr_server.MainContextInfo;
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | pNode=cr_server.pCleanupClient;
|
---|
261 | while (pNode)
|
---|
262 | {
|
---|
263 | if (pNode->pClient->currentCtxInfo == crCtxInfo)
|
---|
264 | {
|
---|
265 | pNode->pClient->currentContextNumber = -1;
|
---|
266 | pNode->pClient->currentCtxInfo = &cr_server.MainContextInfo;
|
---|
267 | }
|
---|
268 | pNode = pNode->next;
|
---|
269 | }
|
---|
270 |
|
---|
271 | if (cr_server.currentCtxInfo == crCtxInfo)
|
---|
272 | {
|
---|
273 | cr_server.currentCtxInfo = &cr_server.MainContextInfo;
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | void SERVER_DISPATCH_APIENTRY
|
---|
278 | crServerDispatchMakeCurrent( GLint window, GLint nativeWindow, GLint context )
|
---|
279 | {
|
---|
280 | CRMuralInfo *mural, *oldMural;
|
---|
281 | CRContextInfo *ctxInfo = NULL;
|
---|
282 | CRContext *ctx, *oldCtx = NULL;
|
---|
283 |
|
---|
284 | if (context >= 0 && window >= 0) {
|
---|
285 | mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
|
---|
286 | if (!mural)
|
---|
287 | {
|
---|
288 | crWarning("CRServer: invalid window %d passed to crServerDispatchMakeCurrent()", window);
|
---|
289 | return;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /* Update the state tracker's current context */
|
---|
293 | ctxInfo = (CRContextInfo *) crHashtableSearch(cr_server.contextTable, context);
|
---|
294 | if (!ctxInfo) {
|
---|
295 | crWarning("CRserver: NULL context in MakeCurrent %d", context);
|
---|
296 | return;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | else {
|
---|
300 | #if 0
|
---|
301 | oldMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, cr_server.currentWindow);
|
---|
302 | if (oldMural && oldMural->bUseFBO && crServerSupportRedirMuralFBO())
|
---|
303 | {
|
---|
304 | if (!crStateGetCurrent()->framebufferobject.drawFB)
|
---|
305 | {
|
---|
306 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
|
---|
307 | }
|
---|
308 | if (!crStateGetCurrent()->framebufferobject.readFB)
|
---|
309 | {
|
---|
310 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | ctxInfo = &cr_server.MainContextInfo;
|
---|
315 | window = -1;
|
---|
316 | mural = NULL;
|
---|
317 | #endif
|
---|
318 | cr_server.bForceMakeCurrentOnClientSwitch = GL_TRUE;
|
---|
319 | return;
|
---|
320 | }
|
---|
321 |
|
---|
322 | cr_server.bForceMakeCurrentOnClientSwitch = GL_FALSE;
|
---|
323 |
|
---|
324 | ctx = ctxInfo->pContext;
|
---|
325 | CRASSERT(ctx);
|
---|
326 |
|
---|
327 | oldMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, cr_server.currentWindow);
|
---|
328 |
|
---|
329 | /* Ubuntu 11.04 hosts misbehave if context window switch is
|
---|
330 | * done with non-default framebuffer object settings.
|
---|
331 | * crStateSwichPrepare & crStateSwichPostprocess are supposed to work around this problem
|
---|
332 | * crStateSwichPrepare restores the FBO state to its default values before the context window switch,
|
---|
333 | * while crStateSwichPostprocess restores it back to the original values */
|
---|
334 | oldCtx = crStateSwichPrepare(ctx, cr_server.bUseMultipleContexts, oldMural && oldMural->bUseFBO && crServerSupportRedirMuralFBO() ? oldMural->idFBO : 0);
|
---|
335 |
|
---|
336 | /*
|
---|
337 | crDebug("**** %s client %d curCtx=%d curWin=%d", __func__,
|
---|
338 | cr_server.curClient->number, ctxPos, window);
|
---|
339 | */
|
---|
340 | cr_server.curClient->currentContextNumber = context;
|
---|
341 | cr_server.curClient->currentCtxInfo = ctxInfo;
|
---|
342 | cr_server.curClient->currentMural = mural;
|
---|
343 | cr_server.curClient->currentWindow = window;
|
---|
344 |
|
---|
345 | CRASSERT(cr_server.curClient->currentCtxInfo);
|
---|
346 | CRASSERT(cr_server.curClient->currentCtxInfo->pContext);
|
---|
347 |
|
---|
348 | /* This is a hack to force updating the 'current' attribs */
|
---|
349 | crStateUpdateColorBits();
|
---|
350 |
|
---|
351 | if (ctx)
|
---|
352 | crStateSetCurrentPointers( ctx, &(cr_server.current) );
|
---|
353 |
|
---|
354 | /* check if being made current for first time, update viewport */
|
---|
355 | #if 0
|
---|
356 | if (ctx) {
|
---|
357 | /* initialize the viewport */
|
---|
358 | if (ctx->viewport.viewportW == 0) {
|
---|
359 | ctx->viewport.viewportW = mural->width;
|
---|
360 | ctx->viewport.viewportH = mural->height;
|
---|
361 | ctx->viewport.scissorW = mural->width;
|
---|
362 | ctx->viewport.scissorH = mural->height;
|
---|
363 | }
|
---|
364 | }
|
---|
365 | #endif
|
---|
366 |
|
---|
367 | /*
|
---|
368 | crDebug("**** %s currentWindow %d newWindow %d", __func__,
|
---|
369 | cr_server.currentWindow, window);
|
---|
370 | */
|
---|
371 |
|
---|
372 | if (1/*cr_server.firstCallMakeCurrent ||
|
---|
373 | cr_server.currentWindow != window ||
|
---|
374 | cr_server.currentNativeWindow != nativeWindow*/) {
|
---|
375 | /* Since the cr server serialized all incoming contexts/clients into
|
---|
376 | * one output stream of GL commands, we only need to call the head
|
---|
377 | * SPU's MakeCurrent() function once.
|
---|
378 | * BUT, if we're rendering to multiple windows, we do have to issue
|
---|
379 | * MakeCurrent() calls sometimes. The same GL context will always be
|
---|
380 | * used though.
|
---|
381 | */
|
---|
382 | cr_server.head_spu->dispatch_table.MakeCurrent( mural->spuWindow,
|
---|
383 | nativeWindow,
|
---|
384 | ctxInfo->SpuContext >= 0
|
---|
385 | ? ctxInfo->SpuContext
|
---|
386 | : cr_server.MainContextInfo.SpuContext);
|
---|
387 | cr_server.firstCallMakeCurrent = GL_FALSE;
|
---|
388 | cr_server.currentCtxInfo = ctxInfo;
|
---|
389 | cr_server.currentWindow = window;
|
---|
390 | cr_server.currentNativeWindow = nativeWindow;
|
---|
391 | }
|
---|
392 |
|
---|
393 | /* This used to be earlier, after crStateUpdateColorBits() call */
|
---|
394 | crStateMakeCurrent( ctx );
|
---|
395 |
|
---|
396 | crStateSwichPostprocess(oldCtx, cr_server.bUseMultipleContexts, mural->bUseFBO && crServerSupportRedirMuralFBO() ? mural->idFBO : 0);
|
---|
397 |
|
---|
398 | if (!ctx->framebufferobject.drawFB
|
---|
399 | && (ctx->buffer.drawBuffer == GL_FRONT || ctx->buffer.drawBuffer == GL_FRONT_LEFT))
|
---|
400 | cr_server.curClient->currentMural->bFbDraw = GL_TRUE;
|
---|
401 |
|
---|
402 | if (!mural->bUseFBO)
|
---|
403 | {
|
---|
404 | ctx->buffer.width = mural->width;
|
---|
405 | ctx->buffer.height = mural->height;
|
---|
406 | }
|
---|
407 | else
|
---|
408 | {
|
---|
409 | ctx->buffer.width = 0;
|
---|
410 | ctx->buffer.height = 0;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|