VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_muralfbo.c@ 41258

Last change on this file since 41258 was 41258, checked in by vboxsync, 13 years ago

crOpenGL: fix VRDP+3D for Intel graphics, more VRDP+3D fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.9 KB
Line 
1/* $Id: server_muralfbo.c 41258 2012-05-11 16:39:12Z vboxsync $ */
2
3/** @file
4 * VBox crOpenGL: Window to FBO redirect support.
5 */
6
7/*
8 * Copyright (C) 2010 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include "server.h"
20#include "cr_string.h"
21#include "cr_mem.h"
22#include "render/renderspu.h"
23
24static int crServerGetPointScreen(GLint x, GLint y)
25{
26 int i;
27
28 for (i=0; i<cr_server.screenCount; ++i)
29 {
30 if ((x>=cr_server.screen[i].x && x<cr_server.screen[i].x+(int)cr_server.screen[i].w)
31 && (y>=cr_server.screen[i].y && y<cr_server.screen[i].y+(int)cr_server.screen[i].h))
32 {
33 return i;
34 }
35 }
36
37 return -1;
38}
39
40static GLboolean crServerMuralCoverScreen(CRMuralInfo *mural, int sId)
41{
42 return mural->gX < cr_server.screen[sId].x
43 && mural->gX+(int)mural->width > cr_server.screen[sId].x+(int)cr_server.screen[sId].w
44 && mural->gY < cr_server.screen[sId].y
45 && mural->gY+(int)mural->height > cr_server.screen[sId].y+(int)cr_server.screen[sId].h;
46}
47
48/* Called when a new CRMuralInfo is created
49 * or when OutputRedirect status is changed.
50 */
51void crServerSetupOutputRedirect(CRMuralInfo *mural)
52{
53 /* Unset the previous redirect. */
54 if (mural->pvOutputRedirectInstance)
55 {
56 cr_server.outputRedirect.CROREnd(mural->pvOutputRedirectInstance);
57 mural->pvOutputRedirectInstance = NULL;
58 }
59
60 /* Setup a new redirect. */
61 if (cr_server.bUseOutputRedirect)
62 {
63 /* Query supported formats. */
64 uint32_t cbFormats = 4096;
65 char *pachFormats = (char *)crAlloc(cbFormats);
66
67 if (pachFormats)
68 {
69 int rc = cr_server.outputRedirect.CRORContextProperty(cr_server.outputRedirect.pvContext,
70 0 /* H3DOR_PROP_FORMATS */, // @todo from a header
71 pachFormats, cbFormats, &cbFormats);
72 if (RT_SUCCESS(rc))
73 {
74 if (RTStrStr(pachFormats, "H3DOR_FMT_RGBA_TOPDOWN"))
75 {
76 cr_server.outputRedirect.CRORBegin(cr_server.outputRedirect.pvContext,
77 &mural->pvOutputRedirectInstance,
78 "H3DOR_FMT_RGBA_TOPDOWN"); // @todo from a header
79 }
80 }
81
82 crFree(pachFormats);
83 }
84
85 /* If this is not NULL then there was a supported format. */
86 if (mural->pvOutputRedirectInstance)
87 {
88 cr_server.outputRedirect.CRORGeometry(mural->pvOutputRedirectInstance,
89 mural->hX, mural->hY,
90 mural->width, mural->height);
91 // @todo the code assumes that RTRECT == four of GLInts
92 cr_server.outputRedirect.CRORVisibleRegion(mural->pvOutputRedirectInstance,
93 mural->cVisibleRects, (RTRECT *)mural->pVisibleRects);
94 }
95 }
96}
97
98void crServerCheckMuralGeometry(CRMuralInfo *mural)
99{
100 int tlS, brS, trS, blS;
101 int overlappingScreenCount, primaryS, i;
102
103 if (cr_server.screenCount<2 && !cr_server.bForceOffscreenRendering)
104 {
105 CRASSERT(cr_server.screenCount>0);
106
107 mural->hX = mural->gX-cr_server.screen[0].x;
108 mural->hY = mural->gY-cr_server.screen[0].y;
109
110 cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX, mural->hY);
111
112 return;
113 }
114
115 tlS = crServerGetPointScreen(mural->gX, mural->gY);
116 brS = crServerGetPointScreen(mural->gX+mural->width-1, mural->gY+mural->height-1);
117
118 if (tlS==brS && tlS>=0)
119 {
120 overlappingScreenCount = 1;
121 primaryS = tlS;
122 }
123 else
124 {
125 trS = crServerGetPointScreen(mural->gX+mural->width-1, mural->gY);
126 blS = crServerGetPointScreen(mural->gX, mural->gY+mural->height-1);
127
128 primaryS = -1; overlappingScreenCount = 0;
129 for (i=0; i<cr_server.screenCount; ++i)
130 {
131 if ((i==tlS) || (i==brS) || (i==trS) || (i==blS)
132 || crServerMuralCoverScreen(mural, i))
133 {
134 overlappingScreenCount++;
135 primaryS = primaryS<0 ? i:primaryS;
136 }
137 }
138
139 if (!overlappingScreenCount)
140 {
141 primaryS = 0;
142 }
143 }
144
145 if (primaryS!=mural->screenId)
146 {
147 mural->screenId = primaryS;
148
149 renderspuSetWindowId(cr_server.screen[primaryS].winID);
150 renderspuReparentWindow(mural->spuWindow);
151 renderspuSetWindowId(cr_server.screen[0].winID);
152 }
153
154 mural->hX = mural->gX-cr_server.screen[primaryS].x;
155 mural->hY = mural->gY-cr_server.screen[primaryS].y;
156
157 if (overlappingScreenCount<2 && !cr_server.bForceOffscreenRendering)
158 {
159 if (mural->bUseFBO)
160 {
161 crServerRedirMuralFBO(mural, GL_FALSE);
162 crServerDeleteMuralFBO(mural);
163 }
164
165 cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX, mural->hY);
166 }
167 else
168 {
169 if (mural->spuWindow)
170 {
171 if (!mural->bUseFBO)
172 {
173 crServerRedirMuralFBO(mural, GL_TRUE);
174 }
175 else
176 {
177 if (mural->width!=mural->fboWidth
178 || mural->height!=mural->height)
179 {
180 crServerRedirMuralFBO(mural, GL_FALSE);
181 crServerDeleteMuralFBO(mural);
182 crServerRedirMuralFBO(mural, GL_TRUE);
183 }
184 }
185 }
186#ifdef DEBUG_misha
187 else
188 {
189 Assert(!mural->bUseFBO);
190 }
191#endif
192
193 if (!mural->bUseFBO)
194 {
195 cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX, mural->hY);
196 }
197 }
198
199 if (mural->pvOutputRedirectInstance)
200 {
201 cr_server.outputRedirect.CRORGeometry(mural->pvOutputRedirectInstance,
202 mural->hX, mural->hY,
203 mural->width, mural->height);
204 }
205}
206
207GLboolean crServerSupportRedirMuralFBO(void)
208{
209 static GLboolean fInited = GL_FALSE;
210 static GLboolean fSupported = GL_FALSE;
211 if (!fInited)
212 {
213 const GLubyte* pExt = cr_server.head_spu->dispatch_table.GetString(GL_REAL_EXTENSIONS);
214
215 fSupported = ( NULL!=crStrstr((const char*)pExt, "GL_ARB_framebuffer_object")
216 || NULL!=crStrstr((const char*)pExt, "GL_EXT_framebuffer_object"))
217 && NULL!=crStrstr((const char*)pExt, "GL_ARB_texture_non_power_of_two");
218 fInited = GL_TRUE;
219 }
220 return fSupported;
221}
222
223void crServerRedirMuralFBO(CRMuralInfo *mural, GLboolean redir)
224{
225 if (redir)
226 {
227 if (!crServerSupportRedirMuralFBO())
228 {
229 crWarning("FBO not supported, can't redirect window output");
230 return;
231 }
232
233 cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, GL_FALSE);
234
235 if (mural->idFBO==0)
236 {
237 crServerCreateMuralFBO(mural);
238 }
239
240 if (!crStateGetCurrent()->framebufferobject.drawFB)
241 {
242 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, mural->idFBO);
243 }
244 if (!crStateGetCurrent()->framebufferobject.readFB)
245 {
246 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, mural->idFBO);
247 }
248
249 crStateGetCurrent()->buffer.width = 0;
250 crStateGetCurrent()->buffer.height = 0;
251 }
252 else
253 {
254 cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, mural->bVisible);
255
256 if (mural->bUseFBO && crServerSupportRedirMuralFBO())
257 {
258 if (!crStateGetCurrent()->framebufferobject.drawFB)
259 {
260 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
261 }
262 if (!crStateGetCurrent()->framebufferobject.readFB)
263 {
264 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
265 }
266 }
267
268 crStateGetCurrent()->buffer.width = mural->width;
269 crStateGetCurrent()->buffer.height = mural->height;
270 }
271
272 mural->bUseFBO = redir;
273}
274
275void crServerCreateMuralFBO(CRMuralInfo *mural)
276{
277 CRContext *ctx = crStateGetCurrent();
278 GLuint uid;
279 GLenum status;
280 SPUDispatchTable *gl = &cr_server.head_spu->dispatch_table;
281
282 CRASSERT(mural->idFBO==0);
283
284 /*Color texture*/
285 gl->GenTextures(1, &mural->idColorTex);
286 gl->BindTexture(GL_TEXTURE_2D, mural->idColorTex);
287 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
288 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
289 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
290 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
291 if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
292 {
293 gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
294 }
295 gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, mural->width, mural->height,
296 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
297
298 /*Depth&Stencil*/
299 gl->GenRenderbuffersEXT(1, &mural->idDepthStencilRB);
300 gl->BindRenderbufferEXT(GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
301 gl->RenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT,
302 mural->width, mural->height);
303
304 /*FBO*/
305 gl->GenFramebuffersEXT(1, &mural->idFBO);
306 gl->BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mural->idFBO);
307
308 gl->FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
309 GL_TEXTURE_2D, mural->idColorTex, 0);
310 gl->FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
311 GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
312 gl->FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
313 GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
314
315 status = gl->CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
316 if (status!=GL_FRAMEBUFFER_COMPLETE_EXT)
317 {
318 crWarning("FBO status(0x%x) isn't complete", status);
319 }
320
321 mural->fboWidth = mural->width;
322 mural->fboHeight = mural->height;
323
324 /*PBO*/
325 if (cr_server.bUsePBOForReadback)
326 {
327 gl->GenBuffersARB(1, &mural->idPBO);
328 gl->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, mural->idPBO);
329 gl->BufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, mural->width*mural->height*4, 0, GL_STREAM_READ_ARB);
330 gl->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, ctx->bufferobject.packBuffer->hwid);
331
332 if (!mural->idPBO)
333 {
334 crWarning("PBO create failed");
335 }
336 }
337
338 /*Restore gl state*/
339 uid = ctx->texture.unit[ctx->texture.curTextureUnit].currentTexture2D->hwid;
340 gl->BindTexture(GL_TEXTURE_2D, uid);
341
342 uid = ctx->framebufferobject.renderbuffer ? ctx->framebufferobject.renderbuffer->hwid:0;
343 gl->BindRenderbufferEXT(GL_RENDERBUFFER_EXT, uid);
344
345 uid = ctx->framebufferobject.drawFB ? ctx->framebufferobject.drawFB->hwid:0;
346 gl->BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, uid);
347
348 uid = ctx->framebufferobject.readFB ? ctx->framebufferobject.readFB->hwid:0;
349 gl->BindFramebufferEXT(GL_READ_FRAMEBUFFER, uid);
350
351 if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
352 {
353 gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, ctx->bufferobject.unpackBuffer->hwid);
354 }
355}
356
357void crServerDeleteMuralFBO(CRMuralInfo *mural)
358{
359 CRASSERT(!mural->bUseFBO);
360
361 if (mural->idFBO!=0)
362 {
363 cr_server.head_spu->dispatch_table.DeleteTextures(1, &mural->idColorTex);
364 cr_server.head_spu->dispatch_table.DeleteRenderbuffersEXT(1, &mural->idDepthStencilRB);
365 cr_server.head_spu->dispatch_table.DeleteFramebuffersEXT(1, &mural->idFBO);
366
367 mural->idFBO = 0;
368 mural->idColorTex = 0;
369 mural->idDepthStencilRB = 0;
370 }
371
372 if (mural->idPBO!=0)
373 {
374 CRASSERT(cr_server.bUsePBOForReadback);
375 cr_server.head_spu->dispatch_table.DeleteBuffersARB(1, &mural->idPBO);
376 mural->idPBO = 0;
377 }
378}
379
380#define MIN(a, b) ((a) < (b) ? (a) : (b))
381#define MAX(a, b) ((a) > (b) ? (a) : (b))
382
383static GLboolean crServerIntersectRect(CRrecti *a, CRrecti *b, CRrecti *rect)
384{
385 CRASSERT(a && b && rect);
386
387 rect->x1 = MAX(a->x1, b->x1);
388 rect->x2 = MIN(a->x2, b->x2);
389 rect->y1 = MAX(a->y1, b->y1);
390 rect->y2 = MIN(a->y2, b->y2);
391
392 return (rect->x2>rect->x1) && (rect->y2>rect->y1);
393}
394
395static GLboolean crServerIntersectScreen(CRMuralInfo *mural, int sId, CRrecti *rect)
396{
397 rect->x1 = MAX(mural->gX, cr_server.screen[sId].x);
398 rect->x2 = MIN(mural->gX+(int)mural->fboWidth, cr_server.screen[sId].x+(int)cr_server.screen[sId].w);
399 rect->y1 = MAX(mural->gY, cr_server.screen[sId].y);
400 rect->y2 = MIN(mural->gY+(int)mural->fboHeight, cr_server.screen[sId].y+(int)cr_server.screen[sId].h);
401
402 return (rect->x2>rect->x1) && (rect->y2>rect->y1);
403}
404
405static void crServerCopySubImage(char *pDst, char* pSrc, CRrecti *pRect, int srcWidth, int srcHeight)
406{
407 int i;
408 int dstrowsize = 4*(pRect->x2-pRect->x1);
409 int srcrowsize = 4*srcWidth;
410 int height = pRect->y2-pRect->y1;
411
412 pSrc += 4*pRect->x1 + srcrowsize*(srcHeight-1-pRect->y1);
413
414 for (i=0; i<height; ++i)
415 {
416 crMemcpy(pDst, pSrc, dstrowsize);
417
418 pSrc -= srcrowsize;
419 pDst += dstrowsize;
420 }
421}
422
423static void crServerTransformRect(CRrecti *pDst, CRrecti *pSrc, int dx, int dy)
424{
425 pDst->x1 = pSrc->x1+dx;
426 pDst->x2 = pSrc->x2+dx;
427 pDst->y1 = pSrc->y1+dy;
428 pDst->y2 = pSrc->y2+dy;
429}
430
431void crServerPresentFBO(CRMuralInfo *mural)
432{
433 char *pixels=NULL, *tmppixels;
434 GLuint uid;
435 int i, j;
436 CRrecti rect, rectwr, sectr;
437 GLboolean bUsePBO;
438 CRContext *ctx = crStateGetCurrent();
439
440 CRASSERT(cr_server.pfnPresentFBO);
441
442 if (!mural->bVisible)
443 {
444 return;
445 }
446
447 if (!mural->width || !mural->height)
448 {
449 return;
450 }
451
452 if (cr_server.bUsePBOForReadback && !mural->idPBO)
453 {
454 crWarning("Mural doesn't have PBO even though bUsePBOForReadback is set!");
455 }
456
457 bUsePBO = cr_server.bUsePBOForReadback && mural->idPBO;
458
459 cr_server.head_spu->dispatch_table.BindTexture(GL_TEXTURE_2D, mural->idColorTex);
460
461 if (bUsePBO)
462 {
463 CRASSERT(mural->idPBO);
464 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, mural->idPBO);
465 }
466 else
467 {
468 if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
469 {
470 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
471 }
472
473 pixels = crAlloc(4*mural->fboWidth*mural->fboHeight);
474 if (!pixels)
475 {
476 crWarning("Out of memory in crServerPresentFBO");
477 return;
478 }
479 }
480
481 /*read the texture, note pixels are NULL for PBO case as it's offset in the buffer*/
482 cr_server.head_spu->dispatch_table.GetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
483
484 /*restore gl state*/
485 uid = ctx->texture.unit[ctx->texture.curTextureUnit].currentTexture2D->hwid;
486 cr_server.head_spu->dispatch_table.BindTexture(GL_TEXTURE_2D, uid);
487
488 if (bUsePBO)
489 {
490 pixels = cr_server.head_spu->dispatch_table.MapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
491 if (!pixels)
492 {
493 crWarning("Failed to MapBuffer in crServerPresentFBO");
494 return;
495 }
496 }
497
498 for (i=0; i<cr_server.screenCount; ++i)
499 {
500 if (crServerIntersectScreen(mural, i, &rect))
501 {
502 /* rect in window relative coords */
503 crServerTransformRect(&rectwr, &rect, -mural->gX, -mural->gY);
504
505 if (!mural->pVisibleRects)
506 {
507 /*we don't get any rects info for guest compiz windows, so we treat windows as visible unless explicitly received 0 visible rects*/
508 if (!mural->bReceivedRects)
509 {
510 tmppixels = crAlloc(4*(rect.x2-rect.x1)*(rect.y2-rect.y1));
511 if (!tmppixels)
512 {
513 crWarning("Out of memory in crServerPresentFBO");
514 crFree(pixels);
515 return;
516 }
517
518 crServerCopySubImage(tmppixels, pixels, &rectwr, mural->fboWidth, mural->fboHeight);
519 /*Note: pfnPresentFBO would free tmppixels*/
520 cr_server.pfnPresentFBO(tmppixels, i, rect.x1-cr_server.screen[i].x, rect.y1-cr_server.screen[i].y, rect.x2-rect.x1, rect.y2-rect.y1);
521 }
522 }
523 else
524 {
525 for (j=0; j<mural->cVisibleRects; ++j)
526 {
527 if (crServerIntersectRect(&rectwr, (CRrecti*) &mural->pVisibleRects[4*j], &sectr))
528 {
529 tmppixels = crAlloc(4*(sectr.x2-sectr.x1)*(sectr.y2-sectr.y1));
530 if (!tmppixels)
531 {
532 crWarning("Out of memory in crServerPresentFBO");
533 crFree(pixels);
534 return;
535 }
536
537 crServerCopySubImage(tmppixels, pixels, &sectr, mural->fboWidth, mural->fboHeight);
538 /*Note: pfnPresentFBO would free tmppixels*/
539 cr_server.pfnPresentFBO(tmppixels, i,
540 sectr.x1+mural->gX-cr_server.screen[i].x,
541 sectr.y1+mural->gY-cr_server.screen[i].y,
542 sectr.x2-sectr.x1, sectr.y2-sectr.y1);
543 }
544 }
545 }
546 }
547 }
548
549 if (mural->pvOutputRedirectInstance)
550 {
551 /* @todo find out why presentfbo is not called but crorframe is called. */
552 cr_server.outputRedirect.CRORFrame(mural->pvOutputRedirectInstance,
553 pixels,
554 4 * mural->fboWidth * mural->fboHeight);
555 }
556
557 if (bUsePBO)
558 {
559 cr_server.head_spu->dispatch_table.UnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
560 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, ctx->bufferobject.packBuffer->hwid);
561 }
562 else
563 {
564 crFree(pixels);
565 if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
566 {
567 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, ctx->bufferobject.packBuffer->hwid);
568 }
569 }
570}
571
572GLboolean crServerIsRedirectedToFBO()
573{
574 return cr_server.curClient
575 && cr_server.curClient->currentMural
576 && cr_server.curClient->currentMural->bUseFBO;
577}
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