VirtualBox

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

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

crOpenGL: OSX backend rework; oddscreen rendering generalization

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