1 | /* $Id: server_muralfbo.c 44951 2013-03-07 12:33:54Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | * VBox crOpenGL: Window to FBO redirect support.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2010-2013 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.215389.xyz. 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 "cr_vreg.h"
|
---|
23 | #include "render/renderspu.h"
|
---|
24 |
|
---|
25 | static int crServerGetPointScreen(GLint x, GLint y)
|
---|
26 | {
|
---|
27 | int i;
|
---|
28 |
|
---|
29 | for (i=0; i<cr_server.screenCount; ++i)
|
---|
30 | {
|
---|
31 | if ((x>=cr_server.screen[i].x && x<cr_server.screen[i].x+(int)cr_server.screen[i].w)
|
---|
32 | && (y>=cr_server.screen[i].y && y<cr_server.screen[i].y+(int)cr_server.screen[i].h))
|
---|
33 | {
|
---|
34 | return i;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | return -1;
|
---|
39 | }
|
---|
40 |
|
---|
41 | static GLboolean crServerMuralCoverScreen(CRMuralInfo *mural, int sId)
|
---|
42 | {
|
---|
43 | return mural->gX < cr_server.screen[sId].x
|
---|
44 | && mural->gX+(int)mural->width > cr_server.screen[sId].x+(int)cr_server.screen[sId].w
|
---|
45 | && mural->gY < cr_server.screen[sId].y
|
---|
46 | && mural->gY+(int)mural->height > cr_server.screen[sId].y+(int)cr_server.screen[sId].h;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /* Called when a new CRMuralInfo is created
|
---|
50 | * or when OutputRedirect status is changed.
|
---|
51 | */
|
---|
52 | void crServerSetupOutputRedirect(CRMuralInfo *mural)
|
---|
53 | {
|
---|
54 | /* Unset the previous redirect. */
|
---|
55 | if (mural->pvOutputRedirectInstance)
|
---|
56 | {
|
---|
57 | cr_server.outputRedirect.CROREnd(mural->pvOutputRedirectInstance);
|
---|
58 | mural->pvOutputRedirectInstance = NULL;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* Setup a new redirect. */
|
---|
62 | if (cr_server.bUseOutputRedirect)
|
---|
63 | {
|
---|
64 | /* Query supported formats. */
|
---|
65 | uint32_t cbFormats = 4096;
|
---|
66 | char *pachFormats = (char *)crAlloc(cbFormats);
|
---|
67 |
|
---|
68 | if (pachFormats)
|
---|
69 | {
|
---|
70 | int rc = cr_server.outputRedirect.CRORContextProperty(cr_server.outputRedirect.pvContext,
|
---|
71 | 0 /* H3DOR_PROP_FORMATS */, // @todo from a header
|
---|
72 | pachFormats, cbFormats, &cbFormats);
|
---|
73 | if (RT_SUCCESS(rc))
|
---|
74 | {
|
---|
75 | if (RTStrStr(pachFormats, "H3DOR_FMT_RGBA_TOPDOWN"))
|
---|
76 | {
|
---|
77 | cr_server.outputRedirect.CRORBegin(cr_server.outputRedirect.pvContext,
|
---|
78 | &mural->pvOutputRedirectInstance,
|
---|
79 | "H3DOR_FMT_RGBA_TOPDOWN"); // @todo from a header
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | crFree(pachFormats);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* If this is not NULL then there was a supported format. */
|
---|
87 | if (mural->pvOutputRedirectInstance)
|
---|
88 | {
|
---|
89 | cr_server.outputRedirect.CRORGeometry(mural->pvOutputRedirectInstance,
|
---|
90 | mural->hX, mural->hY,
|
---|
91 | mural->width, mural->height);
|
---|
92 | // @todo the code assumes that RTRECT == four of GLInts
|
---|
93 | cr_server.outputRedirect.CRORVisibleRegion(mural->pvOutputRedirectInstance,
|
---|
94 | mural->cVisibleRects, (RTRECT *)mural->pVisibleRects);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | void crServerCheckMuralGeometry(CRMuralInfo *mural)
|
---|
100 | {
|
---|
101 | int tlS, brS, trS, blS;
|
---|
102 | int overlappingScreenCount, primaryS, i;
|
---|
103 |
|
---|
104 | if (!mural->CreateInfo.externalID)
|
---|
105 | return;
|
---|
106 |
|
---|
107 | if (!mural->width || !mural->height)
|
---|
108 | return;
|
---|
109 |
|
---|
110 | if (cr_server.screenCount<2 && !cr_server.bForceOffscreenRendering)
|
---|
111 | {
|
---|
112 | CRScreenViewportInfo *pVieport = &cr_server.screenVieport[mural->screenId];
|
---|
113 | CRASSERT(cr_server.screenCount>0);
|
---|
114 | CRASSERT(mural->fUseFBO == CR_SERVER_REDIR_NONE);
|
---|
115 |
|
---|
116 | mural->hX = mural->gX-cr_server.screen[0].x;
|
---|
117 | mural->hY = mural->gY-cr_server.screen[0].y;
|
---|
118 |
|
---|
119 | cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX - pVieport->x, mural->hY - pVieport->y);
|
---|
120 |
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | tlS = crServerGetPointScreen(mural->gX, mural->gY);
|
---|
125 | brS = crServerGetPointScreen(mural->gX+mural->width-1, mural->gY+mural->height-1);
|
---|
126 |
|
---|
127 | if (tlS==brS && tlS>=0)
|
---|
128 | {
|
---|
129 | overlappingScreenCount = 1;
|
---|
130 | primaryS = tlS;
|
---|
131 | }
|
---|
132 | else
|
---|
133 | {
|
---|
134 | trS = crServerGetPointScreen(mural->gX+mural->width-1, mural->gY);
|
---|
135 | blS = crServerGetPointScreen(mural->gX, mural->gY+mural->height-1);
|
---|
136 |
|
---|
137 | primaryS = -1; overlappingScreenCount = 0;
|
---|
138 | for (i=0; i<cr_server.screenCount; ++i)
|
---|
139 | {
|
---|
140 | if ((i==tlS) || (i==brS) || (i==trS) || (i==blS)
|
---|
141 | || crServerMuralCoverScreen(mural, i))
|
---|
142 | {
|
---|
143 | overlappingScreenCount++;
|
---|
144 | primaryS = primaryS<0 ? i:primaryS;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (!overlappingScreenCount)
|
---|
149 | {
|
---|
150 | primaryS = 0;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (primaryS!=mural->screenId)
|
---|
155 | {
|
---|
156 | mural->screenId = primaryS;
|
---|
157 |
|
---|
158 | renderspuSetWindowId(cr_server.screen[primaryS].winID);
|
---|
159 | renderspuReparentWindow(mural->spuWindow);
|
---|
160 | renderspuSetWindowId(cr_server.screen[0].winID);
|
---|
161 | }
|
---|
162 |
|
---|
163 | mural->hX = mural->gX-cr_server.screen[primaryS].x;
|
---|
164 | mural->hY = mural->gY-cr_server.screen[primaryS].y;
|
---|
165 |
|
---|
166 | if (overlappingScreenCount<2 && !cr_server.bForceOffscreenRendering)
|
---|
167 | {
|
---|
168 | CRScreenViewportInfo *pVieport = &cr_server.screenVieport[mural->screenId];
|
---|
169 |
|
---|
170 | if (mural->fUseFBO)
|
---|
171 | {
|
---|
172 | crServerRedirMuralFBO(mural, CR_SERVER_REDIR_NONE);
|
---|
173 | crServerDeleteMuralFBO(mural);
|
---|
174 | }
|
---|
175 |
|
---|
176 | cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX - pVieport->x, mural->hY - pVieport->y);
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | GLubyte redir = overlappingScreenCount >= 2 ? CR_SERVER_REDIR_FBO_RAM : cr_server.bForceOffscreenRendering;
|
---|
181 |
|
---|
182 | if (mural->spuWindow)
|
---|
183 | {
|
---|
184 | if (mural->fUseFBO)
|
---|
185 | {
|
---|
186 | if (mural->width!=mural->fboWidth
|
---|
187 | || mural->height!=mural->fboHeight)
|
---|
188 | {
|
---|
189 | crServerRedirMuralFBO(mural, CR_SERVER_REDIR_NONE);
|
---|
190 | crServerDeleteMuralFBO(mural);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | if (mural->fUseFBO != redir)
|
---|
195 | {
|
---|
196 | crServerRedirMuralFBO(mural, redir);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | #ifdef DEBUG_misha
|
---|
200 | else
|
---|
201 | {
|
---|
202 | Assert(!mural->fUseFBO);
|
---|
203 | }
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | if (mural->fUseFBO != CR_SERVER_REDIR_FBO_RAM)
|
---|
207 | {
|
---|
208 | CRScreenViewportInfo *pVieport = &cr_server.screenVieport[mural->screenId];
|
---|
209 |
|
---|
210 | cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX - pVieport->x, mural->hY - pVieport->y);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (mural->pvOutputRedirectInstance)
|
---|
215 | {
|
---|
216 | cr_server.outputRedirect.CRORGeometry(mural->pvOutputRedirectInstance,
|
---|
217 | mural->hX, mural->hY,
|
---|
218 | mural->width, mural->height);
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | GLboolean crServerSupportRedirMuralFBO(void)
|
---|
223 | {
|
---|
224 | static GLboolean fInited = GL_FALSE;
|
---|
225 | static GLboolean fSupported = GL_FALSE;
|
---|
226 | if (!fInited)
|
---|
227 | {
|
---|
228 | const GLubyte* pExt = cr_server.head_spu->dispatch_table.GetString(GL_REAL_EXTENSIONS);
|
---|
229 |
|
---|
230 | fSupported = ( NULL!=crStrstr((const char*)pExt, "GL_ARB_framebuffer_object")
|
---|
231 | || NULL!=crStrstr((const char*)pExt, "GL_EXT_framebuffer_object"))
|
---|
232 | && NULL!=crStrstr((const char*)pExt, "GL_ARB_texture_non_power_of_two");
|
---|
233 | fInited = GL_TRUE;
|
---|
234 | }
|
---|
235 | return fSupported;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static void crServerCreateMuralFBO(CRMuralInfo *mural);
|
---|
239 |
|
---|
240 | void crServerRedirMuralFBO(CRMuralInfo *mural, GLubyte redir)
|
---|
241 | {
|
---|
242 | if (mural->fUseFBO == redir)
|
---|
243 | {
|
---|
244 | if (redir)
|
---|
245 | crWarning("crServerRedirMuralFBO called with the same redir status %d", redir);
|
---|
246 | return;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if (redir)
|
---|
250 | {
|
---|
251 | if (!crServerSupportRedirMuralFBO())
|
---|
252 | {
|
---|
253 | crWarning("FBO not supported, can't redirect window output");
|
---|
254 | return;
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (redir == CR_SERVER_REDIR_FBO_RAM)
|
---|
258 | cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, GL_FALSE);
|
---|
259 | else
|
---|
260 | cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, mural->bVisible);
|
---|
261 |
|
---|
262 | if (mural->aidFBOs[0]==0)
|
---|
263 | {
|
---|
264 | crServerCreateMuralFBO(mural);
|
---|
265 | }
|
---|
266 |
|
---|
267 | if (!crStateGetCurrent()->framebufferobject.drawFB)
|
---|
268 | {
|
---|
269 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, mural->aidFBOs[mural->iCurDrawBuffer]);
|
---|
270 | }
|
---|
271 | if (!crStateGetCurrent()->framebufferobject.readFB)
|
---|
272 | {
|
---|
273 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, mural->aidFBOs[mural->iCurReadBuffer]);
|
---|
274 | }
|
---|
275 |
|
---|
276 | if (cr_server.curClient && cr_server.curClient->currentMural == mural)
|
---|
277 | {
|
---|
278 | crStateGetCurrent()->buffer.width = 0;
|
---|
279 | crStateGetCurrent()->buffer.height = 0;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | else
|
---|
283 | {
|
---|
284 | if (redir == CR_SERVER_REDIR_NONE)
|
---|
285 | {
|
---|
286 | /* tell renderspu we do not want compositor presentation anymore
|
---|
287 | * renderspu will ensure its redraw thread is done with using the compositor, etc. */
|
---|
288 | cr_server.head_spu->dispatch_table.VBoxPresentComposition(mural->spuWindow, NULL, NULL);
|
---|
289 | }
|
---|
290 |
|
---|
291 | if (mural->fUseFBO == CR_SERVER_REDIR_FBO_RAM)
|
---|
292 | cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, mural->bVisible);
|
---|
293 |
|
---|
294 | if (mural->fUseFBO && crServerSupportRedirMuralFBO())
|
---|
295 | {
|
---|
296 | if (!crStateGetCurrent()->framebufferobject.drawFB)
|
---|
297 | {
|
---|
298 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
|
---|
299 | }
|
---|
300 | if (!crStateGetCurrent()->framebufferobject.readFB)
|
---|
301 | {
|
---|
302 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | if (cr_server.curClient && cr_server.curClient->currentMural == mural)
|
---|
307 | {
|
---|
308 | crStateGetCurrent()->buffer.width = mural->width;
|
---|
309 | crStateGetCurrent()->buffer.height = mural->height;
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | mural->fUseFBO = redir;
|
---|
314 | }
|
---|
315 |
|
---|
316 | static void crServerCreateMuralFBO(CRMuralInfo *mural)
|
---|
317 | {
|
---|
318 | CRContext *ctx = crStateGetCurrent();
|
---|
319 | GLuint uid, i;
|
---|
320 | GLenum status;
|
---|
321 | SPUDispatchTable *gl = &cr_server.head_spu->dispatch_table;
|
---|
322 | CRContextInfo *pMuralContextInfo;
|
---|
323 |
|
---|
324 | CRASSERT(mural->aidFBOs[0]==0);
|
---|
325 | CRASSERT(mural->aidFBOs[1]==0);
|
---|
326 | CRASSERT(mural->width == mural->CEntry.Tex.width);
|
---|
327 | CRASSERT(mural->height == mural->CEntry.Tex.height);
|
---|
328 |
|
---|
329 | pMuralContextInfo = cr_server.currentCtxInfo;
|
---|
330 | if (!pMuralContextInfo)
|
---|
331 | {
|
---|
332 | /* happens on saved state load */
|
---|
333 | CRASSERT(cr_server.MainContextInfo.SpuContext);
|
---|
334 | pMuralContextInfo = &cr_server.MainContextInfo;
|
---|
335 | cr_server.head_spu->dispatch_table.MakeCurrent(mural->spuWindow, 0, cr_server.MainContextInfo.SpuContext);
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (pMuralContextInfo->CreateInfo.visualBits != mural->CreateInfo.visualBits)
|
---|
339 | {
|
---|
340 | crWarning("mural visual bits do not match with current context visual bits!");
|
---|
341 | }
|
---|
342 |
|
---|
343 | mural->cBuffers = 2;
|
---|
344 | mural->iBbBuffer = 0;
|
---|
345 | /*Color texture*/
|
---|
346 | for (i = 0; i < mural->cBuffers; ++i)
|
---|
347 | {
|
---|
348 | gl->GenTextures(1, &mural->aidColorTexs[i]);
|
---|
349 | gl->BindTexture(GL_TEXTURE_2D, mural->aidColorTexs[i]);
|
---|
350 | gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
351 | gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
352 | gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
---|
353 | gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
---|
354 | if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
|
---|
355 | {
|
---|
356 | gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
|
---|
357 | }
|
---|
358 | gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, mural->width, mural->height,
|
---|
359 | 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
|
---|
360 | }
|
---|
361 |
|
---|
362 | /*Depth&Stencil*/
|
---|
363 | gl->GenRenderbuffersEXT(1, &mural->idDepthStencilRB);
|
---|
364 | gl->BindRenderbufferEXT(GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
365 | gl->RenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT,
|
---|
366 | mural->width, mural->height);
|
---|
367 |
|
---|
368 | /*FBO*/
|
---|
369 | for (i = 0; i < mural->cBuffers; ++i)
|
---|
370 | {
|
---|
371 | gl->GenFramebuffersEXT(1, &mural->aidFBOs[i]);
|
---|
372 | gl->BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mural->aidFBOs[i]);
|
---|
373 |
|
---|
374 | gl->FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
|
---|
375 | GL_TEXTURE_2D, mural->aidColorTexs[i], 0);
|
---|
376 | gl->FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
|
---|
377 | GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
378 | gl->FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
|
---|
379 | GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
380 |
|
---|
381 | status = gl->CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
---|
382 | if (status!=GL_FRAMEBUFFER_COMPLETE_EXT)
|
---|
383 | {
|
---|
384 | crWarning("FBO status(0x%x) isn't complete", status);
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | mural->fboWidth = mural->width;
|
---|
389 | mural->fboHeight = mural->height;
|
---|
390 |
|
---|
391 | mural->iCurDrawBuffer = crServerMuralFBOIdxFromBufferName(mural, ctx->buffer.drawBuffer);
|
---|
392 | mural->iCurReadBuffer = crServerMuralFBOIdxFromBufferName(mural, ctx->buffer.readBuffer);
|
---|
393 |
|
---|
394 | /*PBO*/
|
---|
395 | if (cr_server.bUsePBOForReadback)
|
---|
396 | {
|
---|
397 | gl->GenBuffersARB(1, &mural->idPBO);
|
---|
398 | gl->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, mural->idPBO);
|
---|
399 | gl->BufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, mural->width*mural->height*4, 0, GL_STREAM_READ_ARB);
|
---|
400 | gl->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, ctx->bufferobject.packBuffer->hwid);
|
---|
401 |
|
---|
402 | if (!mural->idPBO)
|
---|
403 | {
|
---|
404 | crWarning("PBO create failed");
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | /*Restore gl state*/
|
---|
409 | uid = ctx->texture.unit[ctx->texture.curTextureUnit].currentTexture2D->hwid;
|
---|
410 | gl->BindTexture(GL_TEXTURE_2D, uid);
|
---|
411 |
|
---|
412 | uid = ctx->framebufferobject.renderbuffer ? ctx->framebufferobject.renderbuffer->hwid:0;
|
---|
413 | gl->BindRenderbufferEXT(GL_RENDERBUFFER_EXT, uid);
|
---|
414 |
|
---|
415 | uid = ctx->framebufferobject.drawFB ? ctx->framebufferobject.drawFB->hwid:0;
|
---|
416 | gl->BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, uid);
|
---|
417 |
|
---|
418 | uid = ctx->framebufferobject.readFB ? ctx->framebufferobject.readFB->hwid:0;
|
---|
419 | gl->BindFramebufferEXT(GL_READ_FRAMEBUFFER, uid);
|
---|
420 |
|
---|
421 | if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
|
---|
422 | {
|
---|
423 | gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, ctx->bufferobject.unpackBuffer->hwid);
|
---|
424 | }
|
---|
425 |
|
---|
426 | CRASSERT(mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
427 | CrVrScrCompositorEntryTexNameUpdate(&mural->CEntry, mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
428 | }
|
---|
429 |
|
---|
430 | void crServerDeleteMuralFBO(CRMuralInfo *mural)
|
---|
431 | {
|
---|
432 | CRASSERT(!mural->fUseFBO);
|
---|
433 |
|
---|
434 | if (mural->aidFBOs[0]!=0)
|
---|
435 | {
|
---|
436 | GLuint i;
|
---|
437 | for (i = 0; i < mural->cBuffers; ++i)
|
---|
438 | {
|
---|
439 | cr_server.head_spu->dispatch_table.DeleteTextures(1, &mural->aidColorTexs[i]);
|
---|
440 | mural->aidColorTexs[i] = 0;
|
---|
441 | }
|
---|
442 |
|
---|
443 | cr_server.head_spu->dispatch_table.DeleteRenderbuffersEXT(1, &mural->idDepthStencilRB);
|
---|
444 | mural->idDepthStencilRB = 0;
|
---|
445 |
|
---|
446 | for (i = 0; i < mural->cBuffers; ++i)
|
---|
447 | {
|
---|
448 | cr_server.head_spu->dispatch_table.DeleteFramebuffersEXT(1, &mural->aidFBOs[i]);
|
---|
449 | mural->aidFBOs[i] = 0;
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | if (mural->idPBO!=0)
|
---|
454 | {
|
---|
455 | CRASSERT(cr_server.bUsePBOForReadback);
|
---|
456 | cr_server.head_spu->dispatch_table.DeleteBuffersARB(1, &mural->idPBO);
|
---|
457 | mural->idPBO = 0;
|
---|
458 | }
|
---|
459 |
|
---|
460 | mural->cBuffers = 0;
|
---|
461 | }
|
---|
462 |
|
---|
463 | #define MIN(a, b) ((a) < (b) ? (a) : (b))
|
---|
464 | #define MAX(a, b) ((a) > (b) ? (a) : (b))
|
---|
465 |
|
---|
466 | static GLboolean crServerIntersectRect(CRrecti *a, CRrecti *b, CRrecti *rect)
|
---|
467 | {
|
---|
468 | CRASSERT(a && b && rect);
|
---|
469 |
|
---|
470 | rect->x1 = MAX(a->x1, b->x1);
|
---|
471 | rect->x2 = MIN(a->x2, b->x2);
|
---|
472 | rect->y1 = MAX(a->y1, b->y1);
|
---|
473 | rect->y2 = MIN(a->y2, b->y2);
|
---|
474 |
|
---|
475 | return (rect->x2>rect->x1) && (rect->y2>rect->y1);
|
---|
476 | }
|
---|
477 |
|
---|
478 | static GLboolean crServerIntersectScreen(CRMuralInfo *mural, int sId, CRrecti *rect)
|
---|
479 | {
|
---|
480 | rect->x1 = MAX(mural->gX, cr_server.screen[sId].x);
|
---|
481 | rect->x2 = MIN(mural->gX+(int)mural->fboWidth, cr_server.screen[sId].x+(int)cr_server.screen[sId].w);
|
---|
482 | rect->y1 = MAX(mural->gY, cr_server.screen[sId].y);
|
---|
483 | rect->y2 = MIN(mural->gY+(int)mural->fboHeight, cr_server.screen[sId].y+(int)cr_server.screen[sId].h);
|
---|
484 |
|
---|
485 | return (rect->x2>rect->x1) && (rect->y2>rect->y1);
|
---|
486 | }
|
---|
487 |
|
---|
488 | static void crServerCopySubImage(char *pDst, char* pSrc, CRrecti *pRect, int srcWidth, int srcHeight)
|
---|
489 | {
|
---|
490 | int i;
|
---|
491 | int dstrowsize = 4*(pRect->x2-pRect->x1);
|
---|
492 | int srcrowsize = 4*srcWidth;
|
---|
493 | int height = pRect->y2-pRect->y1;
|
---|
494 |
|
---|
495 | pSrc += 4*pRect->x1 + srcrowsize*(srcHeight-1-pRect->y1);
|
---|
496 |
|
---|
497 | for (i=0; i<height; ++i)
|
---|
498 | {
|
---|
499 | crMemcpy(pDst, pSrc, dstrowsize);
|
---|
500 |
|
---|
501 | pSrc -= srcrowsize;
|
---|
502 | pDst += dstrowsize;
|
---|
503 | }
|
---|
504 | }
|
---|
505 |
|
---|
506 | static void crServerTransformRect(CRrecti *pDst, CRrecti *pSrc, int dx, int dy)
|
---|
507 | {
|
---|
508 | pDst->x1 = pSrc->x1+dx;
|
---|
509 | pDst->x2 = pSrc->x2+dx;
|
---|
510 | pDst->y1 = pSrc->y1+dy;
|
---|
511 | pDst->y2 = pSrc->y2+dy;
|
---|
512 | }
|
---|
513 |
|
---|
514 | void crServerPresentFBO(CRMuralInfo *mural)
|
---|
515 | {
|
---|
516 | char *pixels=NULL, *tmppixels;
|
---|
517 | int i, j;
|
---|
518 | CRrecti rect, rectwr, sectr;
|
---|
519 | GLuint idPBO;
|
---|
520 | CRContext *ctx = crStateGetCurrent();
|
---|
521 | VBOXVR_TEXTURE Tex;
|
---|
522 |
|
---|
523 | CRASSERT(mural->fUseFBO);
|
---|
524 | CRASSERT(cr_server.pfnPresentFBO || mural->fUseFBO == CR_SERVER_REDIR_FBO_BLT);
|
---|
525 |
|
---|
526 | if (!mural->bVisible)
|
---|
527 | {
|
---|
528 | return;
|
---|
529 | }
|
---|
530 |
|
---|
531 | if (!mural->width || !mural->height)
|
---|
532 | {
|
---|
533 | return;
|
---|
534 | }
|
---|
535 |
|
---|
536 | if (!CrVrScrCompositorEntryIsInList(&mural->CEntry))
|
---|
537 | return;
|
---|
538 |
|
---|
539 | if (mural->fUseFBO == CR_SERVER_REDIR_FBO_BLT)
|
---|
540 | {
|
---|
541 | GLuint idDrawFBO, idReadFBO;
|
---|
542 |
|
---|
543 | idDrawFBO = mural->aidFBOs[mural->iCurDrawBuffer];
|
---|
544 | idReadFBO = mural->aidFBOs[mural->iCurReadBuffer];
|
---|
545 |
|
---|
546 | crStateSwitchPrepare(NULL, ctx, idDrawFBO, idReadFBO);
|
---|
547 |
|
---|
548 | cr_server.head_spu->dispatch_table.VBoxPresentComposition(mural->spuWindow, &mural->Compositor, &mural->CEntry);
|
---|
549 |
|
---|
550 | crStateSwitchPostprocess(ctx, NULL, idDrawFBO, idReadFBO);
|
---|
551 |
|
---|
552 | return;
|
---|
553 | }
|
---|
554 |
|
---|
555 | Tex.width = mural->width;
|
---|
556 | Tex.height = mural->height;
|
---|
557 | Tex.target = GL_TEXTURE_2D;
|
---|
558 | Tex.hwid = mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)];
|
---|
559 | CRASSERT(Tex.hwid);
|
---|
560 |
|
---|
561 | if (cr_server.bUsePBOForReadback && !mural->idPBO)
|
---|
562 | {
|
---|
563 | crWarning("Mural doesn't have PBO even though bUsePBOForReadback is set!");
|
---|
564 | }
|
---|
565 |
|
---|
566 | idPBO = cr_server.bUsePBOForReadback ? mural->idPBO : 0;
|
---|
567 | if (idPBO)
|
---|
568 | {
|
---|
569 | CRASSERT(mural->fboWidth == mural->width);
|
---|
570 | CRASSERT(mural->fboHeight == mural->height);
|
---|
571 | }
|
---|
572 |
|
---|
573 | pixels = CrHlpGetTexImage(ctx, &Tex, idPBO, GL_BGRA);
|
---|
574 | if (!pixels)
|
---|
575 | {
|
---|
576 | crWarning("CrHlpGetTexImage failed in crServerPresentFBO");
|
---|
577 | return;
|
---|
578 | }
|
---|
579 |
|
---|
580 | for (i=0; i<cr_server.screenCount; ++i)
|
---|
581 | {
|
---|
582 | if (crServerIntersectScreen(mural, i, &rect))
|
---|
583 | {
|
---|
584 | /* rect in window relative coords */
|
---|
585 | crServerTransformRect(&rectwr, &rect, -mural->gX, -mural->gY);
|
---|
586 |
|
---|
587 | if (!mural->pVisibleRects)
|
---|
588 | {
|
---|
589 | /*we don't get any rects info for guest compiz windows, so we treat windows as visible unless explicitly received 0 visible rects*/
|
---|
590 | if (!mural->bReceivedRects)
|
---|
591 | {
|
---|
592 | tmppixels = crAlloc(4*(rect.x2-rect.x1)*(rect.y2-rect.y1));
|
---|
593 | if (!tmppixels)
|
---|
594 | {
|
---|
595 | crWarning("Out of memory in crServerPresentFBO");
|
---|
596 | crFree(pixels);
|
---|
597 | return;
|
---|
598 | }
|
---|
599 |
|
---|
600 | crServerCopySubImage(tmppixels, pixels, &rectwr, mural->fboWidth, mural->fboHeight);
|
---|
601 | /*Note: pfnPresentFBO would free tmppixels*/
|
---|
602 | 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);
|
---|
603 | }
|
---|
604 | }
|
---|
605 | else
|
---|
606 | {
|
---|
607 | for (j=0; j<mural->cVisibleRects; ++j)
|
---|
608 | {
|
---|
609 | if (crServerIntersectRect(&rectwr, (CRrecti*) &mural->pVisibleRects[4*j], §r))
|
---|
610 | {
|
---|
611 | tmppixels = crAlloc(4*(sectr.x2-sectr.x1)*(sectr.y2-sectr.y1));
|
---|
612 | if (!tmppixels)
|
---|
613 | {
|
---|
614 | crWarning("Out of memory in crServerPresentFBO");
|
---|
615 | crFree(pixels);
|
---|
616 | return;
|
---|
617 | }
|
---|
618 |
|
---|
619 | crServerCopySubImage(tmppixels, pixels, §r, mural->fboWidth, mural->fboHeight);
|
---|
620 | /*Note: pfnPresentFBO would free tmppixels*/
|
---|
621 | cr_server.pfnPresentFBO(tmppixels, i,
|
---|
622 | sectr.x1+mural->gX-cr_server.screen[i].x,
|
---|
623 | sectr.y1+mural->gY-cr_server.screen[i].y,
|
---|
624 | sectr.x2-sectr.x1, sectr.y2-sectr.y1);
|
---|
625 | }
|
---|
626 | }
|
---|
627 | }
|
---|
628 | }
|
---|
629 | }
|
---|
630 |
|
---|
631 | if (mural->pvOutputRedirectInstance)
|
---|
632 | {
|
---|
633 | /* @todo find out why presentfbo is not called but crorframe is called. */
|
---|
634 | cr_server.outputRedirect.CRORFrame(mural->pvOutputRedirectInstance,
|
---|
635 | pixels,
|
---|
636 | 4 * mural->fboWidth * mural->fboHeight);
|
---|
637 | }
|
---|
638 |
|
---|
639 | CrHlpFreeTexImage(ctx, idPBO, pixels);
|
---|
640 | }
|
---|
641 |
|
---|
642 | GLboolean crServerIsRedirectedToFBO()
|
---|
643 | {
|
---|
644 | return cr_server.curClient
|
---|
645 | && cr_server.curClient->currentMural
|
---|
646 | && cr_server.curClient->currentMural->fUseFBO;
|
---|
647 | }
|
---|
648 |
|
---|
649 | GLuint crServerMuralFBOIdxFromBufferName(CRMuralInfo *mural, GLenum buffer)
|
---|
650 | {
|
---|
651 | if (buffer == GL_FRONT
|
---|
652 | || buffer == GL_FRONT_LEFT
|
---|
653 | || buffer == GL_FRONT_RIGHT)
|
---|
654 | return CR_SERVER_FBO_FB_IDX(mural);
|
---|
655 | return CR_SERVER_FBO_BB_IDX(mural);
|
---|
656 | }
|
---|
657 |
|
---|
658 | void crServerMuralFBOSwapBuffers(CRMuralInfo *mural)
|
---|
659 | {
|
---|
660 | CRContext *ctx = crStateGetCurrent();
|
---|
661 | GLuint iOldCurDrawBuffer = mural->iCurDrawBuffer;
|
---|
662 | GLuint iOldCurReadBuffer = mural->iCurReadBuffer;
|
---|
663 | mural->iBbBuffer = ((mural->iBbBuffer + 1) % (mural->cBuffers));
|
---|
664 | mural->iCurDrawBuffer = ((mural->iCurDrawBuffer + 1) % (mural->cBuffers));
|
---|
665 | mural->iCurReadBuffer = ((mural->iCurReadBuffer + 1) % (mural->cBuffers));
|
---|
666 | Assert(iOldCurDrawBuffer != mural->iCurDrawBuffer || mural->cBuffers == 1);
|
---|
667 | Assert(iOldCurReadBuffer != mural->iCurReadBuffer || mural->cBuffers == 1);
|
---|
668 | if (!ctx->framebufferobject.drawFB && iOldCurDrawBuffer != mural->iCurDrawBuffer)
|
---|
669 | {
|
---|
670 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, mural->aidFBOs[mural->iCurDrawBuffer]);
|
---|
671 | }
|
---|
672 | if (!ctx->framebufferobject.readFB && iOldCurReadBuffer != mural->iCurReadBuffer)
|
---|
673 | {
|
---|
674 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, mural->aidFBOs[mural->iCurReadBuffer]);
|
---|
675 | }
|
---|
676 | Assert(mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
677 | CrVrScrCompositorEntryTexNameUpdate(&mural->CEntry, mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
678 | }
|
---|