VirtualBox

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

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

crOpenGL: render to fbo fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 KB
Line 
1/* $Id: server_muralfbo.c 41160 2012-05-04 09:55:53Z 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->bUseFBO)
170 {
171 crServerRedirMuralFBO(mural, GL_TRUE);
172 }
173 else
174 {
175 if (mural->width!=mural->fboWidth
176 || mural->height!=mural->height)
177 {
178 crServerRedirMuralFBO(mural, GL_FALSE);
179 crServerDeleteMuralFBO(mural);
180 crServerRedirMuralFBO(mural, GL_TRUE);
181 }
182 }
183
184 if (!mural->bUseFBO)
185 {
186 cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX, mural->hY);
187 }
188 }
189
190 if (mural->pvOutputRedirectInstance)
191 {
192 cr_server.outputRedirect.CRORGeometry(mural->pvOutputRedirectInstance,
193 mural->hX, mural->hY,
194 mural->width, mural->height);
195 }
196}
197
198GLboolean crServerSupportRedirMuralFBO(void)
199{
200 static GLboolean fInited = GL_FALSE;
201 static GLboolean fSupported = GL_FALSE;
202 if (!fInited)
203 {
204 const GLubyte* pExt = cr_server.head_spu->dispatch_table.GetString(GL_REAL_EXTENSIONS);
205
206 fSupported = ( NULL!=crStrstr((const char*)pExt, "GL_ARB_framebuffer_object")
207 || NULL!=crStrstr((const char*)pExt, "GL_EXT_framebuffer_object"))
208 && NULL!=crStrstr((const char*)pExt, "GL_ARB_texture_non_power_of_two");
209 fInited = GL_TRUE;
210 }
211 return fSupported;
212}
213
214void crServerRedirMuralFBO(CRMuralInfo *mural, GLboolean redir)
215{
216 if (redir)
217 {
218 if (!crServerSupportRedirMuralFBO())
219 {
220 crWarning("FBO not supported, can't redirect window output");
221 return;
222 }
223
224 cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, GL_FALSE);
225
226 if (mural->idFBO==0)
227 {
228 crServerCreateMuralFBO(mural);
229 }
230
231 if (!crStateGetCurrent()->framebufferobject.drawFB)
232 {
233 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, mural->idFBO);
234 }
235 if (!crStateGetCurrent()->framebufferobject.readFB)
236 {
237 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, mural->idFBO);
238 }
239
240 crStateGetCurrent()->buffer.width = 0;
241 crStateGetCurrent()->buffer.height = 0;
242 }
243 else
244 {
245 cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, mural->bVisible);
246
247 if (mural->bUseFBO && crServerSupportRedirMuralFBO())
248 {
249 if (!crStateGetCurrent()->framebufferobject.drawFB)
250 {
251 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
252 }
253 if (!crStateGetCurrent()->framebufferobject.readFB)
254 {
255 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
256 }
257 }
258
259 crStateGetCurrent()->buffer.width = mural->width;
260 crStateGetCurrent()->buffer.height = mural->height;
261 }
262
263 mural->bUseFBO = redir;
264}
265
266void crServerCreateMuralFBO(CRMuralInfo *mural)
267{
268 CRContext *ctx = crStateGetCurrent();
269 GLuint uid;
270 GLenum status;
271 SPUDispatchTable *gl = &cr_server.head_spu->dispatch_table;
272
273 CRASSERT(mural->idFBO==0);
274
275 /*Color texture*/
276 gl->GenTextures(1, &mural->idColorTex);
277 gl->BindTexture(GL_TEXTURE_2D, mural->idColorTex);
278 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
279 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
280 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
281 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
282 if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
283 {
284 gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
285 }
286 gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, mural->width, mural->height,
287 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
288
289 /*Depth&Stencil*/
290 gl->GenRenderbuffersEXT(1, &mural->idDepthStencilRB);
291 gl->BindRenderbufferEXT(GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
292 gl->RenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT,
293 mural->width, mural->height);
294
295 /*FBO*/
296 gl->GenFramebuffersEXT(1, &mural->idFBO);
297 gl->BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mural->idFBO);
298
299 gl->FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
300 GL_TEXTURE_2D, mural->idColorTex, 0);
301 gl->FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
302 GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
303 gl->FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
304 GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
305
306 status = gl->CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
307 if (status!=GL_FRAMEBUFFER_COMPLETE_EXT)
308 {
309 crWarning("FBO status(0x%x) isn't complete", status);
310 }
311
312 mural->fboWidth = mural->width;
313 mural->fboHeight = mural->height;
314
315 /*PBO*/
316 if (cr_server.bUsePBOForReadback)
317 {
318 gl->GenBuffersARB(1, &mural->idPBO);
319 gl->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, mural->idPBO);
320 gl->BufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, mural->width*mural->height*4, 0, GL_STREAM_READ_ARB);
321 gl->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, ctx->bufferobject.packBuffer->hwid);
322
323 if (!mural->idPBO)
324 {
325 crWarning("PBO create failed");
326 }
327 }
328
329 /*Restore gl state*/
330 uid = ctx->texture.unit[ctx->texture.curTextureUnit].currentTexture2D->hwid;
331 gl->BindTexture(GL_TEXTURE_2D, uid);
332
333 uid = ctx->framebufferobject.renderbuffer ? ctx->framebufferobject.renderbuffer->hwid:0;
334 gl->BindRenderbufferEXT(GL_RENDERBUFFER_EXT, uid);
335
336 uid = ctx->framebufferobject.drawFB ? ctx->framebufferobject.drawFB->hwid:0;
337 gl->BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, uid);
338
339 uid = ctx->framebufferobject.readFB ? ctx->framebufferobject.readFB->hwid:0;
340 gl->BindFramebufferEXT(GL_READ_FRAMEBUFFER, uid);
341
342 if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
343 {
344 gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, ctx->bufferobject.unpackBuffer->hwid);
345 }
346}
347
348void crServerDeleteMuralFBO(CRMuralInfo *mural)
349{
350 CRASSERT(!mural->bUseFBO);
351
352 if (mural->idFBO!=0)
353 {
354 cr_server.head_spu->dispatch_table.DeleteTextures(1, &mural->idColorTex);
355 cr_server.head_spu->dispatch_table.DeleteRenderbuffersEXT(1, &mural->idDepthStencilRB);
356 cr_server.head_spu->dispatch_table.DeleteFramebuffersEXT(1, &mural->idFBO);
357
358 mural->idFBO = 0;
359 mural->idColorTex = 0;
360 mural->idDepthStencilRB = 0;
361 }
362
363 if (mural->idPBO!=0)
364 {
365 CRASSERT(cr_server.bUsePBOForReadback);
366 cr_server.head_spu->dispatch_table.DeleteBuffersARB(1, &mural->idPBO);
367 mural->idPBO = 0;
368 }
369}
370
371#define MIN(a, b) ((a) < (b) ? (a) : (b))
372#define MAX(a, b) ((a) > (b) ? (a) : (b))
373
374static GLboolean crServerIntersectRect(CRrecti *a, CRrecti *b, CRrecti *rect)
375{
376 CRASSERT(a && b && rect);
377
378 rect->x1 = MAX(a->x1, b->x1);
379 rect->x2 = MIN(a->x2, b->x2);
380 rect->y1 = MAX(a->y1, b->y1);
381 rect->y2 = MIN(a->y2, b->y2);
382
383 return (rect->x2>rect->x1) && (rect->y2>rect->y1);
384}
385
386static GLboolean crServerIntersectScreen(CRMuralInfo *mural, int sId, CRrecti *rect)
387{
388 rect->x1 = MAX(mural->gX, cr_server.screen[sId].x);
389 rect->x2 = MIN(mural->gX+(int)mural->fboWidth, cr_server.screen[sId].x+(int)cr_server.screen[sId].w);
390 rect->y1 = MAX(mural->gY, cr_server.screen[sId].y);
391 rect->y2 = MIN(mural->gY+(int)mural->fboHeight, cr_server.screen[sId].y+(int)cr_server.screen[sId].h);
392
393 return (rect->x2>rect->x1) && (rect->y2>rect->y1);
394}
395
396static void crServerCopySubImage(char *pDst, char* pSrc, CRrecti *pRect, int srcWidth, int srcHeight)
397{
398 int i;
399 int dstrowsize = 4*(pRect->x2-pRect->x1);
400 int srcrowsize = 4*srcWidth;
401 int height = pRect->y2-pRect->y1;
402
403 pSrc += 4*pRect->x1 + srcrowsize*(srcHeight-1-pRect->y1);
404
405 for (i=0; i<height; ++i)
406 {
407 crMemcpy(pDst, pSrc, dstrowsize);
408
409 pSrc -= srcrowsize;
410 pDst += dstrowsize;
411 }
412}
413
414static void crServerTransformRect(CRrecti *pDst, CRrecti *pSrc, int dx, int dy)
415{
416 pDst->x1 = pSrc->x1+dx;
417 pDst->x2 = pSrc->x2+dx;
418 pDst->y1 = pSrc->y1+dy;
419 pDst->y2 = pSrc->y2+dy;
420}
421
422void crServerPresentFBO(CRMuralInfo *mural)
423{
424 char *pixels=NULL, *tmppixels;
425 GLuint uid;
426 int i, j;
427 CRrecti rect, rectwr, sectr;
428 GLboolean bUsePBO;
429 CRContext *ctx = crStateGetCurrent();
430
431 CRASSERT(cr_server.pfnPresentFBO);
432
433 if (!mural->bVisible)
434 {
435 return;
436 }
437
438 if (!mural->width || !mural->height)
439 {
440 return;
441 }
442
443 if (cr_server.bUsePBOForReadback && !mural->idPBO)
444 {
445 crWarning("Mural doesn't have PBO even though bUsePBOForReadback is set!");
446 }
447
448 bUsePBO = cr_server.bUsePBOForReadback && mural->idPBO;
449
450 cr_server.head_spu->dispatch_table.BindTexture(GL_TEXTURE_2D, mural->idColorTex);
451
452 if (bUsePBO)
453 {
454 CRASSERT(mural->idPBO);
455 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, mural->idPBO);
456 }
457 else
458 {
459 if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
460 {
461 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
462 }
463
464 pixels = crAlloc(4*mural->fboWidth*mural->fboHeight);
465 if (!pixels)
466 {
467 crWarning("Out of memory in crServerPresentFBO");
468 return;
469 }
470 }
471
472 /*read the texture, note pixels are NULL for PBO case as it's offset in the buffer*/
473 cr_server.head_spu->dispatch_table.GetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
474
475 /*restore gl state*/
476 uid = ctx->texture.unit[ctx->texture.curTextureUnit].currentTexture2D->hwid;
477 cr_server.head_spu->dispatch_table.BindTexture(GL_TEXTURE_2D, uid);
478
479 if (bUsePBO)
480 {
481 pixels = cr_server.head_spu->dispatch_table.MapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
482 if (!pixels)
483 {
484 crWarning("Failed to MapBuffer in crServerPresentFBO");
485 return;
486 }
487 }
488
489 for (i=0; i<cr_server.screenCount; ++i)
490 {
491 if (crServerIntersectScreen(mural, i, &rect))
492 {
493 /* rect in window relative coords */
494 crServerTransformRect(&rectwr, &rect, -mural->gX, -mural->gY);
495
496 if (!mural->pVisibleRects)
497 {
498 /*we don't get any rects info for guest compiz windows, so we treat windows as visible unless explicitly received 0 visible rects*/
499 if (!mural->bReceivedRects)
500 {
501 tmppixels = crAlloc(4*(rect.x2-rect.x1)*(rect.y2-rect.y1));
502 if (!tmppixels)
503 {
504 crWarning("Out of memory in crServerPresentFBO");
505 crFree(pixels);
506 return;
507 }
508
509 crServerCopySubImage(tmppixels, pixels, &rectwr, mural->fboWidth, mural->fboHeight);
510 /*Note: pfnPresentFBO would free tmppixels*/
511 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);
512 }
513 }
514 else
515 {
516 for (j=0; j<mural->cVisibleRects; ++j)
517 {
518 if (crServerIntersectRect(&rectwr, (CRrecti*) &mural->pVisibleRects[4*j], &sectr))
519 {
520 tmppixels = crAlloc(4*(sectr.x2-sectr.x1)*(sectr.y2-sectr.y1));
521 if (!tmppixels)
522 {
523 crWarning("Out of memory in crServerPresentFBO");
524 crFree(pixels);
525 return;
526 }
527
528 crServerCopySubImage(tmppixels, pixels, &sectr, mural->fboWidth, mural->fboHeight);
529 /*Note: pfnPresentFBO would free tmppixels*/
530 cr_server.pfnPresentFBO(tmppixels, i,
531 sectr.x1+mural->gX-cr_server.screen[i].x,
532 sectr.y1+mural->gY-cr_server.screen[i].y,
533 sectr.x2-sectr.x1, sectr.y2-sectr.y1);
534 }
535 }
536 }
537 }
538 }
539
540 if (mural->pvOutputRedirectInstance)
541 {
542 /* @todo find out why presentfbo is not called but crorframe is called. */
543 cr_server.outputRedirect.CRORFrame(mural->pvOutputRedirectInstance,
544 pixels,
545 4 * mural->fboWidth * mural->fboHeight);
546 }
547
548 if (bUsePBO)
549 {
550 cr_server.head_spu->dispatch_table.UnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
551 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, ctx->bufferobject.packBuffer->hwid);
552 }
553 else
554 {
555 crFree(pixels);
556 if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
557 {
558 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, ctx->bufferobject.packBuffer->hwid);
559 }
560 }
561}
562
563GLboolean crServerIsRedirectedToFBO()
564{
565 return cr_server.curClient
566 && cr_server.curClient->currentMural
567 && cr_server.curClient->currentMural->bUseFBO;
568}
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