VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_window.c@ 44860

Last change on this file since 44860 was 44860, checked in by vboxsync, 12 years ago

crOpenGL: redraw impl for X11/glx hosts; enable offscreen rendering for X11/glx hosts

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.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 "server.h"
8#include "server_dispatch.h"
9#include "cr_mem.h"
10#include "cr_rand.h"
11#include "cr_string.h"
12
13GLint SERVER_DISPATCH_APIENTRY
14crServerDispatchWindowCreate(const char *dpyName, GLint visBits)
15{
16 return crServerDispatchWindowCreateEx(dpyName, visBits, -1);
17}
18
19GLint crServerMuralInit(CRMuralInfo *mural, const char *dpyName, GLint visBits, GLint preloadWinID)
20{
21 CRMuralInfo *defaultMural;
22 GLint dims[2];
23 GLint windowID = -1;
24 GLint spuWindow;
25 VBOXVR_TEXTURE Tex = {0};
26
27 int rc = CrVrScrCompositorInit(&mural->Compositor);
28 if (!RT_SUCCESS(rc))
29 {
30 crWarning("CrVrScrCompositorInit failed, rc %d", rc);
31 return -1;
32 }
33
34 /*
35 * Have first SPU make a new window.
36 */
37 spuWindow = cr_server.head_spu->dispatch_table.WindowCreate( dpyName, visBits );
38 if (spuWindow < 0) {
39 CrVrScrCompositorTerm(&mural->Compositor);
40 return spuWindow;
41 }
42
43 /* get initial window size */
44 cr_server.head_spu->dispatch_table.GetChromiumParametervCR(GL_WINDOW_SIZE_CR, spuWindow, GL_INT, 2, dims);
45
46 Tex.width = dims[0];
47 Tex.height = dims[1];
48 Tex.target = GL_TEXTURE_2D;
49 Tex.hwid = 0;
50 CrVrScrCompositorEntryInit(&mural->CEntry, &Tex);
51
52 defaultMural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, 0);
53 CRASSERT(defaultMural);
54 mural->gX = 0;
55 mural->gY = 0;
56 mural->width = dims[0];
57 mural->height = dims[1];
58
59 mural->spuWindow = spuWindow;
60 mural->screenId = 0;
61 mural->bVisible = GL_FALSE;
62 mural->fUseFBO = CR_SERVER_REDIR_NONE;
63
64 mural->cVisibleRects = 0;
65 mural->pVisibleRects = NULL;
66 mural->bReceivedRects = GL_FALSE;
67
68 mural->pvOutputRedirectInstance = NULL;
69
70 /* generate ID for this new window/mural (special-case for file conns) */
71 if (cr_server.curClient && cr_server.curClient->conn->type == CR_FILE)
72 windowID = spuWindow;
73 else
74 windowID = preloadWinID<0 ? (GLint)crHashtableAllocKeys( cr_server.muralTable, 1 ) : preloadWinID;
75
76 mural->CreateInfo.visualBits = visBits;
77 mural->CreateInfo.externalID = windowID;
78 mural->CreateInfo.pszDpyName = dpyName ? crStrdup(dpyName) : NULL;
79
80 CR_STATE_SHAREDOBJ_USAGE_INIT(mural);
81
82 crServerSetupOutputRedirect(mural);
83
84 return windowID;
85}
86
87GLint
88crServerDispatchWindowCreateEx(const char *dpyName, GLint visBits, GLint preloadWinID)
89{
90 CRMuralInfo *mural;
91 GLint windowID = -1;
92
93 dpyName = "";
94
95 if (cr_server.sharedWindows) {
96 int pos, j;
97
98 /* find empty position in my (curclient) windowList */
99 for (pos = 0; pos < CR_MAX_WINDOWS; pos++) {
100 if (cr_server.curClient->windowList[pos] == 0) {
101 break;
102 }
103 }
104 if (pos == CR_MAX_WINDOWS) {
105 crWarning("Too many windows in crserver!");
106 return -1;
107 }
108
109 /* Look if any other client has a window for this slot */
110 for (j = 0; j < cr_server.numClients; j++) {
111 if (cr_server.clients[j]->windowList[pos] != 0) {
112 /* use that client's window */
113 windowID = cr_server.clients[j]->windowList[pos];
114 cr_server.curClient->windowList[pos] = windowID;
115 crServerReturnValue( &windowID, sizeof(windowID) ); /* real return value */
116 crDebug("CRServer: client %p sharing window %d",
117 cr_server.curClient, windowID);
118 return windowID;
119 }
120 }
121 }
122
123
124 /*
125 * Create a new mural for the new window.
126 */
127 mural = (CRMuralInfo *) crCalloc(sizeof(CRMuralInfo));
128 if (!mural)
129 {
130 crWarning("crCalloc failed!");
131 return -1;
132 }
133
134 windowID = crServerMuralInit(mural, dpyName, visBits, preloadWinID);
135 if (windowID < 0)
136 {
137 crWarning("crServerMuralInit failed!");
138 crServerReturnValue( &windowID, sizeof(windowID) );
139 crFree(mural);
140 return windowID;
141 }
142
143 crHashtableAdd(cr_server.muralTable, windowID, mural);
144
145 crDebug("CRServer: client %p created new window %d (SPU window %d)",
146 cr_server.curClient, windowID, mural->spuWindow);
147
148 if (windowID != -1 && !cr_server.bIsInLoadingState) {
149 int pos;
150 for (pos = 0; pos < CR_MAX_WINDOWS; pos++) {
151 if (cr_server.curClient->windowList[pos] == 0) {
152 cr_server.curClient->windowList[pos] = windowID;
153 break;
154 }
155 }
156 }
157
158 /* ensure we have a dummy mural created right away to avoid potential deadlocks on VM shutdown */
159 crServerGetDummyMural(mural->CreateInfo.visualBits);
160
161 crServerReturnValue( &windowID, sizeof(windowID) );
162 return windowID;
163}
164
165static int crServerRemoveClientWindow(CRClient *pClient, GLint window)
166{
167 int pos;
168
169 for (pos = 0; pos < CR_MAX_WINDOWS; ++pos)
170 {
171 if (pClient->windowList[pos] == window)
172 {
173 pClient->windowList[pos] = 0;
174 return true;
175 }
176 }
177
178 return false;
179}
180
181void crServerMuralTerm(CRMuralInfo *mural)
182{
183 if (mural->pvOutputRedirectInstance)
184 {
185 cr_server.outputRedirect.CROREnd(mural->pvOutputRedirectInstance);
186 mural->pvOutputRedirectInstance = NULL;
187 }
188
189 crServerRedirMuralFBO(mural, CR_SERVER_REDIR_NONE);
190 crServerDeleteMuralFBO(mural);
191
192 if (cr_server.currentMural == mural)
193 {
194 CRMuralInfo *dummyMural = crServerGetDummyMural(cr_server.MainContextInfo.CreateInfo.visualBits);
195 /* reset the current context to some dummy values to ensure render spu does not switch to a default "0" context,
196 * which might lead to muralFBO (offscreen rendering) gl entities being created in a scope of that context */
197 cr_server.head_spu->dispatch_table.MakeCurrent(dummyMural->spuWindow, 0, cr_server.MainContextInfo.SpuContext);
198 cr_server.currentWindow = -1;
199 cr_server.currentMural = NULL;
200 }
201 else
202 {
203 CRASSERT(cr_server.currentWindow != mural->CreateInfo.externalID);
204 }
205
206
207 cr_server.head_spu->dispatch_table.WindowDestroy( mural->spuWindow );
208
209 if (mural->pVisibleRects)
210 {
211 crFree(mural->pVisibleRects);
212 }
213
214 if (mural->CreateInfo.pszDpyName)
215 crFree(mural->CreateInfo.pszDpyName);
216}
217
218static void crServerCleanupCtxMuralRefsCB(unsigned long key, void *data1, void *data2)
219{
220 CRContextInfo *ctxInfo = (CRContextInfo *) data1;
221 CRMuralInfo *mural = (CRMuralInfo *) data2;
222
223 if (ctxInfo->currentMural == mural)
224 ctxInfo->currentMural = NULL;
225}
226
227void SERVER_DISPATCH_APIENTRY
228crServerDispatchWindowDestroy( GLint window )
229{
230 CRMuralInfo *mural;
231 int32_t client;
232 CRClientNode *pNode;
233 int found=false;
234
235 if (!window)
236 {
237 crWarning("Unexpected attempt to delete default mural, ignored!");
238 return;
239 }
240
241 mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
242 if (!mural) {
243 crWarning("CRServer: invalid window %d passed to WindowDestroy()", window);
244 return;
245 }
246
247 crDebug("CRServer: Destroying window %d (spu window %d)", window, mural->spuWindow);
248
249 crHashtableWalk(cr_server.contextTable, crServerCleanupCtxMuralRefsCB, mural);
250
251 crServerMuralTerm(mural);
252
253 CRASSERT(cr_server.currentWindow != window);
254
255 if (cr_server.curClient)
256 {
257 if (cr_server.curClient->currentMural == mural)
258 {
259 cr_server.curClient->currentMural = NULL;
260 cr_server.curClient->currentWindow = -1;
261 }
262
263 found = crServerRemoveClientWindow(cr_server.curClient, window);
264
265 /*Same as with contexts, some apps destroy it not in a thread where it was created*/
266 if (!found)
267 {
268 for (client=0; client<cr_server.numClients; ++client)
269 {
270 if (cr_server.clients[client]==cr_server.curClient)
271 continue;
272
273 found = crServerRemoveClientWindow(cr_server.clients[client], window);
274
275 if (found) break;
276 }
277 }
278
279 if (!found)
280 {
281 pNode=cr_server.pCleanupClient;
282
283 while (pNode && !found)
284 {
285 found = crServerRemoveClientWindow(pNode->pClient, window);
286 pNode = pNode->next;
287 }
288 }
289
290 CRASSERT(found);
291 }
292
293 /*Make sure this window isn't active in other clients*/
294 for (client=0; client<cr_server.numClients; ++client)
295 {
296 if (cr_server.clients[client]->currentMural == mural)
297 {
298 cr_server.clients[client]->currentMural = NULL;
299 cr_server.clients[client]->currentWindow = -1;
300 }
301 }
302
303 pNode=cr_server.pCleanupClient;
304 while (pNode)
305 {
306 if (pNode->pClient->currentMural == mural)
307 {
308 pNode->pClient->currentMural = NULL;
309 pNode->pClient->currentWindow = -1;
310 }
311 pNode = pNode->next;
312 }
313
314 CrVrScrCompositorTerm(&mural->Compositor);
315
316 crHashtableDelete(cr_server.muralTable, window, crFree);
317}
318
319void crServerMuralSize(CRMuralInfo *mural, GLint width, GLint height)
320{
321 if (mural->width != width || mural->height != height)
322 {
323 VBOXVR_TEXTURE Tex;
324 Tex.width = width;
325 Tex.height = height;
326 Tex.target = GL_TEXTURE_2D;
327 Tex.hwid = 0;
328 CrVrScrCompositorLock(&mural->Compositor);
329 CrVrScrCompositorEntryRemove(&mural->Compositor, &mural->CEntry);
330 CrVrScrCompositorUnlock(&mural->Compositor);
331 CrVrScrCompositorEntryInit(&mural->CEntry, &Tex);
332 mural->width = width;
333 mural->height = height;
334 }
335
336 if (cr_server.curClient && cr_server.curClient->currentMural == mural)
337 {
338 crStateGetCurrent()->buffer.width = mural->width;
339 crStateGetCurrent()->buffer.height = mural->height;
340 }
341
342 crServerCheckMuralGeometry(mural);
343
344 cr_server.head_spu->dispatch_table.WindowSize(mural->spuWindow, width, height);
345}
346
347void SERVER_DISPATCH_APIENTRY
348crServerDispatchWindowSize( GLint window, GLint width, GLint height )
349{
350 CRMuralInfo *mural;
351
352 /* crDebug("CRServer: Window %d size %d x %d", window, width, height);*/
353 mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
354 if (!mural) {
355#if EXTRA_WARN
356 crWarning("CRServer: invalid window %d passed to WindowSize()", window);
357#endif
358 return;
359 }
360
361 crServerMuralSize(mural, width, height);
362
363 /* Work-around Intel driver bug */
364 CRASSERT(!cr_server.curClient
365 || !cr_server.curClient->currentMural
366 || cr_server.curClient->currentMural == mural);
367 if (cr_server.curClient && cr_server.curClient->currentMural == mural)
368 {
369 CRContextInfo * ctxInfo = cr_server.currentCtxInfo;
370 CRASSERT(ctxInfo);
371 crServerDispatchMakeCurrent(window, 0, ctxInfo->CreateInfo.externalID);
372 }
373}
374
375
376void SERVER_DISPATCH_APIENTRY
377crServerDispatchWindowPosition( GLint window, GLint x, GLint y )
378{
379 CRMuralInfo *mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
380 RTPOINT Pos;
381 /* crDebug("CRServer: Window %d pos %d, %d", window, x, y);*/
382 if (!mural) {
383#if EXTRA_WARN
384 crWarning("CRServer: invalid window %d passed to WindowPosition()", window);
385#endif
386 return;
387 }
388 mural->gX = x;
389 mural->gY = y;
390
391 Pos.x = x;
392 Pos.y = y;
393
394 CrVrScrCompositorLock(&mural->Compositor);
395 CrVrScrCompositorEntryPosSet(&mural->Compositor, &mural->CEntry, &Pos);
396 CrVrScrCompositorUnlock(&mural->Compositor);
397
398 crServerCheckMuralGeometry(mural);
399}
400
401void SERVER_DISPATCH_APIENTRY
402crServerDispatchWindowVisibleRegion( GLint window, GLint cRects, GLint *pRects )
403{
404 CRMuralInfo *mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
405 if (!mural) {
406#if EXTRA_WARN
407 crWarning("CRServer: invalid window %d passed to WindowVisibleRegion()", window);
408#endif
409 return;
410 }
411
412 if (mural->pVisibleRects)
413 {
414 crFree(mural->pVisibleRects);
415 mural->pVisibleRects = NULL;
416 }
417
418 mural->cVisibleRects = cRects;
419 mural->bReceivedRects = GL_TRUE;
420 if (cRects)
421 {
422 mural->pVisibleRects = (GLint*) crAlloc(4*sizeof(GLint)*cRects);
423 if (!mural->pVisibleRects)
424 {
425 crError("Out of memory in crServerDispatchWindowVisibleRegion");
426 }
427 crMemcpy(mural->pVisibleRects, pRects, 4*sizeof(GLint)*cRects);
428 }
429
430 cr_server.head_spu->dispatch_table.WindowVisibleRegion(mural->spuWindow, cRects, pRects);
431
432 if (mural->pvOutputRedirectInstance)
433 {
434 /* @todo the code assumes that RTRECT == four GLInts. */
435 cr_server.outputRedirect.CRORVisibleRegion(mural->pvOutputRedirectInstance,
436 cRects, (RTRECT *)pRects);
437 }
438
439 CrVrScrCompositorLock(&mural->Compositor);
440 CrVrScrCompositorEntryRegionsSet(&mural->Compositor, &mural->CEntry, NULL, cRects, (const RTRECT *)pRects);
441 CrVrScrCompositorUnlock(&mural->Compositor);
442}
443
444
445
446void SERVER_DISPATCH_APIENTRY
447crServerDispatchWindowShow( GLint window, GLint state )
448{
449 CRMuralInfo *mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
450 if (!mural) {
451#if EXTRA_WARN
452 crWarning("CRServer: invalid window %d passed to WindowShow()", window);
453#endif
454 return;
455 }
456
457 if (mural->fUseFBO != CR_SERVER_REDIR_FBO_RAM)
458 {
459 cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, state);
460 }
461
462 mural->bVisible = state;
463}
464
465
466GLint
467crServerSPUWindowID(GLint serverWindow)
468{
469 CRMuralInfo *mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, serverWindow);
470 if (!mural) {
471#if EXTRA_WARN
472 crWarning("CRServer: invalid window %d passed to crServerSPUWindowID()",
473 serverWindow);
474#endif
475 return -1;
476 }
477 return mural->spuWindow;
478}
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