VirtualBox

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

Last change on this file since 38989 was 38989, checked in by vboxsync, 14 years ago

crOpenGL: reset last opengl error on context creation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.6 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 "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 CRCreateInfo_t *pCreateInfo;
28
29 if (shareCtx > 0) {
30 crWarning("CRServer: context sharing not implemented.");
31 shareCtx = 0;
32 }
33
34 /* Since the Cr server serialized all incoming clients/contexts into
35 * one outgoing GL stream, we only need to create one context for the
36 * head SPU. We'll only have to make it current once too, below.
37 */
38 if (cr_server.firstCallCreateContext) {
39 cr_server.SpuContextVisBits = visualBits;
40 cr_server.SpuContext = cr_server.head_spu->dispatch_table.
41 CreateContext(dpyName, cr_server.SpuContextVisBits, shareCtx);
42 if (cr_server.SpuContext < 0) {
43 crWarning("crServerDispatchCreateContext() failed.");
44 return -1;
45 }
46 cr_server.firstCallCreateContext = GL_FALSE;
47 }
48 else {
49 /* second or third or ... context */
50 if ((visualBits & cr_server.SpuContextVisBits) != visualBits) {
51 int oldSpuContext;
52
53 /* the new context needs new visual attributes */
54 cr_server.SpuContextVisBits |= visualBits;
55 crDebug("crServerDispatchCreateContext requires new visual (0x%x).",
56 cr_server.SpuContextVisBits);
57
58 /* Here, we used to just destroy the old rendering context.
59 * Unfortunately, this had the side effect of destroying
60 * all display lists and textures that had been loaded on
61 * the old context as well.
62 *
63 * Now, first try to create a new context, with a suitable
64 * visual, sharing display lists and textures with the
65 * old context. Then destroy the old context.
66 */
67
68 /* create new rendering context with suitable visual */
69 oldSpuContext = cr_server.SpuContext;
70 cr_server.SpuContext = cr_server.head_spu->dispatch_table.
71 CreateContext(dpyName, cr_server.SpuContextVisBits, cr_server.SpuContext);
72 /* destroy old rendering context */
73 cr_server.head_spu->dispatch_table.DestroyContext(oldSpuContext);
74 if (cr_server.SpuContext < 0) {
75 crWarning("crServerDispatchCreateContext() failed.");
76 return -1;
77 }
78 }
79 }
80
81 /* Now create a new state-tracker context and initialize the
82 * dispatch function pointers.
83 */
84 newCtx = crStateCreateContextEx(&cr_server.limits, visualBits, NULL, internalID);
85 if (newCtx) {
86 crStateSetCurrentPointers( newCtx, &(cr_server.current) );
87 crStateResetCurrentPointers(&(cr_server.current));
88 retVal = preloadCtxID<0 ? crServerGenerateID(&cr_server.idsPool.freeContextID) : preloadCtxID;
89 crHashtableAdd(cr_server.contextTable, retVal, newCtx);
90
91 pCreateInfo = (CRCreateInfo_t *) crAlloc(sizeof(CRCreateInfo_t));
92 pCreateInfo->pszDpyName = dpyName ? crStrdup(dpyName) : NULL;
93 pCreateInfo->visualBits = visualBits;
94 pCreateInfo->internalID = newCtx->id;
95 crHashtableAdd(cr_server.pContextCreateInfoTable, retVal, pCreateInfo);
96 }
97
98 if (retVal != -1 && !cr_server.bIsInLoadingState) {
99 int pos;
100 for (pos = 0; pos < CR_MAX_CONTEXTS; pos++) {
101 if (cr_server.curClient->contextList[pos] == 0) {
102 cr_server.curClient->contextList[pos] = retVal;
103 break;
104 }
105 }
106 }
107
108 {
109 /* As we're using only one host context to serve all client contexts, newly created context will still
110 * hold last error value from any previous failed opengl call. Proper solution would be to redirect any
111 * client glGetError calls to our state tracker, but right now it's missing quite a lot of checks and doesn't
112 * reflect host driver/gpu specific issues. Thus we just reset last opengl error at context creation.
113 */
114 GLint err;
115
116 err = cr_server.head_spu->dispatch_table.GetError();
117 if (err!=GL_NO_ERROR)
118 {
119 crWarning("Cleared gl error %#x on context creation", err);
120 }
121 }
122
123 crServerReturnValue( &retVal, sizeof(retVal) );
124
125 return retVal;
126}
127
128static int crServerRemoveClientContext(CRClient *pClient, GLint ctx)
129{
130 int pos;
131
132 for (pos = 0; pos < CR_MAX_CONTEXTS; ++pos)
133 {
134 if (pClient->contextList[pos] == ctx)
135 {
136 pClient->contextList[pos] = 0;
137 return true;
138 }
139 }
140
141 return false;
142}
143
144void SERVER_DISPATCH_APIENTRY
145crServerDispatchDestroyContext( GLint ctx )
146{
147 CRContext *crCtx;
148 int32_t client;
149 CRClientNode *pNode;
150 int found=false;
151
152 crCtx = (CRContext *) crHashtableSearch(cr_server.contextTable, ctx);
153 if (!crCtx) {
154 crWarning("CRServer: DestroyContext invalid context %d", ctx);
155 return;
156 }
157
158 crDebug("CRServer: DestroyContext context %d", ctx);
159
160 crHashtableDelete(cr_server.contextTable, ctx, NULL);
161 crStateDestroyContext( crCtx );
162 crHashtableDelete(cr_server.pContextCreateInfoTable, ctx, crServerCreateInfoDeleteCB);
163
164 if (cr_server.curClient)
165 {
166 /* If we delete our current context, default back to the null context */
167 if (cr_server.curClient->currentCtx == crCtx) {
168 cr_server.curClient->currentContextNumber = -1;
169 cr_server.curClient->currentCtx = cr_server.DummyContext;
170 }
171
172 found = crServerRemoveClientContext(cr_server.curClient, ctx);
173
174 /*Some application call destroy context not in a thread where it was created...have do deal with it.*/
175 if (!found)
176 {
177 for (client=0; client<cr_server.numClients; ++client)
178 {
179 if (cr_server.clients[client]==cr_server.curClient)
180 continue;
181
182 found = crServerRemoveClientContext(cr_server.clients[client], ctx);
183
184 if (found) break;
185 }
186 }
187
188 if (!found)
189 {
190 pNode=cr_server.pCleanupClient;
191
192 while (pNode && !found)
193 {
194 found = crServerRemoveClientContext(pNode->pClient, ctx);
195 pNode = pNode->next;
196 }
197 }
198
199 CRASSERT(found);
200 }
201
202 /*Make sure this context isn't active in other clients*/
203 for (client=0; client<cr_server.numClients; ++client)
204 {
205 if (cr_server.clients[client]->currentCtx == crCtx)
206 {
207 cr_server.clients[client]->currentContextNumber = -1;
208 cr_server.clients[client]->currentCtx = cr_server.DummyContext;
209 }
210 }
211
212 pNode=cr_server.pCleanupClient;
213 while (pNode)
214 {
215 if (pNode->pClient->currentCtx == crCtx)
216 {
217 pNode->pClient->currentContextNumber = -1;
218 pNode->pClient->currentCtx = cr_server.DummyContext;
219 }
220 pNode = pNode->next;
221 }
222}
223
224
225void SERVER_DISPATCH_APIENTRY
226crServerDispatchMakeCurrent( GLint window, GLint nativeWindow, GLint context )
227{
228 CRMuralInfo *mural, *oldMural;
229 CRContext *ctx;
230
231 if (context >= 0 && window >= 0) {
232 mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
233 if (!mural)
234 {
235 crWarning("CRServer: invalid window %d passed to crServerDispatchMakeCurrent()", window);
236 return;
237 }
238
239 /* Update the state tracker's current context */
240 ctx = (CRContext *) crHashtableSearch(cr_server.contextTable, context);
241 if (!ctx) {
242 crWarning("CRserver: NULL context in MakeCurrent %d", context);
243 return;
244 }
245 }
246 else {
247 oldMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, cr_server.currentWindow);
248 if (oldMural && oldMural->bUseFBO && crServerSupportRedirMuralFBO())
249 {
250 if (!crStateGetCurrent()->framebufferobject.drawFB)
251 {
252 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
253 }
254 if (!crStateGetCurrent()->framebufferobject.readFB)
255 {
256 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
257 }
258 }
259
260 ctx = cr_server.DummyContext;
261 window = -1;
262 mural = NULL;
263 return;
264 }
265
266 /*
267 crDebug("**** %s client %d curCtx=%d curWin=%d", __func__,
268 cr_server.curClient->number, ctxPos, window);
269 */
270 cr_server.curClient->currentContextNumber = context;
271 cr_server.curClient->currentCtx = ctx;
272 cr_server.curClient->currentMural = mural;
273 cr_server.curClient->currentWindow = window;
274
275 CRASSERT(cr_server.curClient->currentCtx);
276
277 /* This is a hack to force updating the 'current' attribs */
278 crStateUpdateColorBits();
279
280 if (ctx)
281 crStateSetCurrentPointers( ctx, &(cr_server.current) );
282
283 /* check if being made current for first time, update viewport */
284#if 0
285 if (ctx) {
286 /* initialize the viewport */
287 if (ctx->viewport.viewportW == 0) {
288 ctx->viewport.viewportW = mural->width;
289 ctx->viewport.viewportH = mural->height;
290 ctx->viewport.scissorW = mural->width;
291 ctx->viewport.scissorH = mural->height;
292 }
293 }
294#endif
295
296 /*
297 crDebug("**** %s currentWindow %d newWindow %d", __func__,
298 cr_server.currentWindow, window);
299 */
300
301 oldMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, cr_server.currentWindow);
302
303 if (1/*cr_server.firstCallMakeCurrent ||
304 cr_server.currentWindow != window ||
305 cr_server.currentNativeWindow != nativeWindow*/) {
306 /* Since the cr server serialized all incoming contexts/clients into
307 * one output stream of GL commands, we only need to call the head
308 * SPU's MakeCurrent() function once.
309 * BUT, if we're rendering to multiple windows, we do have to issue
310 * MakeCurrent() calls sometimes. The same GL context will always be
311 * used though.
312 */
313 cr_server.head_spu->dispatch_table.MakeCurrent( mural->spuWindow,
314 nativeWindow,
315 cr_server.SpuContext );
316 cr_server.firstCallMakeCurrent = GL_FALSE;
317 cr_server.currentWindow = window;
318 cr_server.currentNativeWindow = nativeWindow;
319 }
320
321 /* This used to be earlier, after crStateUpdateColorBits() call */
322 crStateMakeCurrent( ctx );
323
324 if (oldMural != mural && crServerSupportRedirMuralFBO())
325 {
326 if (!crStateGetCurrent()->framebufferobject.drawFB)
327 {
328 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, mural->bUseFBO ? mural->idFBO:0);
329 }
330 if (!crStateGetCurrent()->framebufferobject.readFB)
331 {
332 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, mural->bUseFBO ? mural->idFBO:0);
333 }
334 }
335
336 if (!mural->bUseFBO)
337 {
338 ctx->buffer.width = mural->width;
339 ctx->buffer.height = mural->height;
340 }
341 else
342 {
343 ctx->buffer.width = 0;
344 ctx->buffer.height = 0;
345 }
346}
347
Note: See TracBrowser for help on using the repository browser.

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