VirtualBox

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

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

3D for VRDP: initial commit, build fix (xTracker 5565).

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