1 | /* $Id: server_muralfbo.c 45179 2013-03-25 17:31:01Z 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.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 "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 | uint32_t cRects;
|
---|
90 | const RTRECT *pRects;
|
---|
91 |
|
---|
92 | int rc = CrVrScrCompositorEntryRegionsGet(&mural->Compositor, &mural->CEntry, &cRects, NULL, &pRects);
|
---|
93 | if (!RT_SUCCESS(rc))
|
---|
94 | {
|
---|
95 | crWarning("CrVrScrCompositorEntryRegionsGet failed, rc %d", rc);
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | cr_server.outputRedirect.CRORGeometry(mural->pvOutputRedirectInstance,
|
---|
100 | mural->hX, mural->hY,
|
---|
101 | mural->width, mural->height);
|
---|
102 | // @todo the code assumes that RTRECT == four of GLInts
|
---|
103 | cr_server.outputRedirect.CRORVisibleRegion(mural->pvOutputRedirectInstance,
|
---|
104 | mural->cVisibleRects, (RTRECT *)mural->pVisibleRects);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | void crServerCheckMuralGeometry(CRMuralInfo *mural)
|
---|
110 | {
|
---|
111 | int tlS, brS, trS, blS;
|
---|
112 | int overlappingScreenCount, primaryS, i;
|
---|
113 |
|
---|
114 | if (!mural->CreateInfo.externalID)
|
---|
115 | return;
|
---|
116 |
|
---|
117 | if (!mural->width || !mural->height)
|
---|
118 | {
|
---|
119 | crServerRedirMuralFBO(mural, CR_SERVER_REDIR_NONE);
|
---|
120 | crServerDeleteMuralFBO(mural);
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | crServerVBoxCompositionDisableEnter(mural);
|
---|
125 |
|
---|
126 | if (cr_server.screenCount<2 && !cr_server.bForceOffscreenRendering)
|
---|
127 | {
|
---|
128 | CRScreenViewportInfo *pVieport = &cr_server.screenVieport[mural->screenId];
|
---|
129 | CRASSERT(cr_server.screenCount>0);
|
---|
130 | CRASSERT(mural->fUseFBO == CR_SERVER_REDIR_NONE);
|
---|
131 |
|
---|
132 | mural->hX = mural->gX-cr_server.screen[0].x;
|
---|
133 | mural->hY = mural->gY-cr_server.screen[0].y;
|
---|
134 |
|
---|
135 | cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX - pVieport->x, mural->hY - pVieport->y);
|
---|
136 |
|
---|
137 | crServerVBoxCompositionDisableLeave(mural, GL_FALSE);
|
---|
138 |
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | tlS = crServerGetPointScreen(mural->gX, mural->gY);
|
---|
143 | brS = crServerGetPointScreen(mural->gX+mural->width-1, mural->gY+mural->height-1);
|
---|
144 |
|
---|
145 | if (tlS==brS && tlS>=0)
|
---|
146 | {
|
---|
147 | overlappingScreenCount = 1;
|
---|
148 | primaryS = tlS;
|
---|
149 | }
|
---|
150 | else
|
---|
151 | {
|
---|
152 | trS = crServerGetPointScreen(mural->gX+mural->width-1, mural->gY);
|
---|
153 | blS = crServerGetPointScreen(mural->gX, mural->gY+mural->height-1);
|
---|
154 |
|
---|
155 | primaryS = -1; overlappingScreenCount = 0;
|
---|
156 | for (i=0; i<cr_server.screenCount; ++i)
|
---|
157 | {
|
---|
158 | if ((i==tlS) || (i==brS) || (i==trS) || (i==blS)
|
---|
159 | || crServerMuralCoverScreen(mural, i))
|
---|
160 | {
|
---|
161 | overlappingScreenCount++;
|
---|
162 | primaryS = primaryS<0 ? i:primaryS;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (!overlappingScreenCount)
|
---|
167 | {
|
---|
168 | primaryS = 0;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (primaryS!=mural->screenId)
|
---|
173 | {
|
---|
174 | mural->screenId = primaryS;
|
---|
175 |
|
---|
176 | renderspuSetWindowId(cr_server.screen[primaryS].winID);
|
---|
177 | renderspuReparentWindow(mural->spuWindow);
|
---|
178 | renderspuSetWindowId(cr_server.screen[0].winID);
|
---|
179 | }
|
---|
180 |
|
---|
181 | mural->hX = mural->gX-cr_server.screen[primaryS].x;
|
---|
182 | mural->hY = mural->gY-cr_server.screen[primaryS].y;
|
---|
183 |
|
---|
184 | if (overlappingScreenCount<2 && !cr_server.bForceOffscreenRendering)
|
---|
185 | {
|
---|
186 | CRScreenViewportInfo *pVieport = &cr_server.screenVieport[mural->screenId];
|
---|
187 |
|
---|
188 | if (mural->fUseFBO)
|
---|
189 | {
|
---|
190 | crServerRedirMuralFBO(mural, CR_SERVER_REDIR_NONE);
|
---|
191 | crServerDeleteMuralFBO(mural);
|
---|
192 | }
|
---|
193 |
|
---|
194 | cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX - pVieport->x, mural->hY - pVieport->y);
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | GLubyte redir = overlappingScreenCount >= 2 ? CR_SERVER_REDIR_FBO_RAM : cr_server.bForceOffscreenRendering;
|
---|
199 |
|
---|
200 | if (mural->spuWindow)
|
---|
201 | {
|
---|
202 | if (mural->fUseFBO)
|
---|
203 | {
|
---|
204 | if (mural->width!=mural->fboWidth
|
---|
205 | || mural->height!=mural->fboHeight)
|
---|
206 | {
|
---|
207 | crServerRedirMuralFBO(mural, CR_SERVER_REDIR_NONE);
|
---|
208 | crServerDeleteMuralFBO(mural);
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (mural->fUseFBO != redir)
|
---|
213 | {
|
---|
214 | crServerRedirMuralFBO(mural, redir);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | #ifdef DEBUG_misha
|
---|
218 | else
|
---|
219 | {
|
---|
220 | Assert(!mural->fUseFBO);
|
---|
221 | }
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | if (mural->fUseFBO != CR_SERVER_REDIR_FBO_RAM)
|
---|
225 | {
|
---|
226 | CRScreenViewportInfo *pVieport = &cr_server.screenVieport[mural->screenId];
|
---|
227 |
|
---|
228 | cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX - pVieport->x, mural->hY - pVieport->y);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (mural->pvOutputRedirectInstance)
|
---|
233 | {
|
---|
234 | cr_server.outputRedirect.CRORGeometry(mural->pvOutputRedirectInstance,
|
---|
235 | mural->hX, mural->hY,
|
---|
236 | mural->width, mural->height);
|
---|
237 | }
|
---|
238 |
|
---|
239 | crServerVBoxCompositionDisableLeave(mural, GL_FALSE);
|
---|
240 | }
|
---|
241 |
|
---|
242 | GLboolean crServerSupportRedirMuralFBO(void)
|
---|
243 | {
|
---|
244 | static GLboolean fInited = GL_FALSE;
|
---|
245 | static GLboolean fSupported = GL_FALSE;
|
---|
246 | if (!fInited)
|
---|
247 | {
|
---|
248 | const GLubyte* pExt = cr_server.head_spu->dispatch_table.GetString(GL_REAL_EXTENSIONS);
|
---|
249 |
|
---|
250 | fSupported = ( NULL!=crStrstr((const char*)pExt, "GL_ARB_framebuffer_object")
|
---|
251 | || NULL!=crStrstr((const char*)pExt, "GL_EXT_framebuffer_object"))
|
---|
252 | && NULL!=crStrstr((const char*)pExt, "GL_ARB_texture_non_power_of_two");
|
---|
253 | fInited = GL_TRUE;
|
---|
254 | }
|
---|
255 | return fSupported;
|
---|
256 | }
|
---|
257 |
|
---|
258 | static void crServerCreateMuralFBO(CRMuralInfo *mural);
|
---|
259 |
|
---|
260 | void crServerRedirMuralFBO(CRMuralInfo *mural, GLubyte redir)
|
---|
261 | {
|
---|
262 | if (mural->fUseFBO == redir)
|
---|
263 | {
|
---|
264 | if (redir)
|
---|
265 | crWarning("crServerRedirMuralFBO called with the same redir status %d", redir);
|
---|
266 | return;
|
---|
267 | }
|
---|
268 |
|
---|
269 | crServerVBoxCompositionDisableEnter(mural);
|
---|
270 |
|
---|
271 | if (redir)
|
---|
272 | {
|
---|
273 | if (!crServerSupportRedirMuralFBO())
|
---|
274 | {
|
---|
275 | crWarning("FBO not supported, can't redirect window output");
|
---|
276 | crServerVBoxCompositionDisableLeave(mural, GL_FALSE);
|
---|
277 | return;
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (redir == CR_SERVER_REDIR_FBO_RAM)
|
---|
281 | cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, GL_FALSE);
|
---|
282 | else
|
---|
283 | cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, mural->bVisible);
|
---|
284 |
|
---|
285 | if (mural->aidFBOs[0]==0)
|
---|
286 | {
|
---|
287 | crServerCreateMuralFBO(mural);
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (cr_server.currentMural == mural)
|
---|
291 | {
|
---|
292 | if (!crStateGetCurrent()->framebufferobject.drawFB)
|
---|
293 | {
|
---|
294 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, mural->aidFBOs[mural->iCurDrawBuffer]);
|
---|
295 | }
|
---|
296 | if (!crStateGetCurrent()->framebufferobject.readFB)
|
---|
297 | {
|
---|
298 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, mural->aidFBOs[mural->iCurReadBuffer]);
|
---|
299 | }
|
---|
300 |
|
---|
301 | crStateGetCurrent()->buffer.width = 0;
|
---|
302 | crStateGetCurrent()->buffer.height = 0;
|
---|
303 | }
|
---|
304 | }
|
---|
305 | else
|
---|
306 | {
|
---|
307 | if (mural->fUseFBO == CR_SERVER_REDIR_FBO_RAM)
|
---|
308 | cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, mural->bVisible);
|
---|
309 |
|
---|
310 | if (cr_server.currentMural == mural)
|
---|
311 | {
|
---|
312 | if (!crStateGetCurrent()->framebufferobject.drawFB)
|
---|
313 | {
|
---|
314 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
|
---|
315 | }
|
---|
316 | if (!crStateGetCurrent()->framebufferobject.readFB)
|
---|
317 | {
|
---|
318 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
|
---|
319 | }
|
---|
320 |
|
---|
321 | crStateGetCurrent()->buffer.width = mural->width;
|
---|
322 | crStateGetCurrent()->buffer.height = mural->height;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | mural->fUseFBO = redir;
|
---|
327 | crServerVBoxCompositionDisableLeave(mural, GL_FALSE);
|
---|
328 | }
|
---|
329 |
|
---|
330 | static void crServerCreateMuralFBO(CRMuralInfo *mural)
|
---|
331 | {
|
---|
332 | CRContext *ctx = crStateGetCurrent();
|
---|
333 | GLuint uid, i;
|
---|
334 | GLenum status;
|
---|
335 | SPUDispatchTable *gl = &cr_server.head_spu->dispatch_table;
|
---|
336 | CRContextInfo *pMuralContextInfo;
|
---|
337 |
|
---|
338 | CRASSERT(mural->aidFBOs[0]==0);
|
---|
339 | CRASSERT(mural->aidFBOs[1]==0);
|
---|
340 | CRASSERT(mural->width == mural->CEntry.Tex.width);
|
---|
341 | CRASSERT(mural->height == mural->CEntry.Tex.height);
|
---|
342 |
|
---|
343 | pMuralContextInfo = cr_server.currentCtxInfo;
|
---|
344 | if (!pMuralContextInfo)
|
---|
345 | {
|
---|
346 | /* happens on saved state load */
|
---|
347 | CRASSERT(cr_server.MainContextInfo.SpuContext);
|
---|
348 | pMuralContextInfo = &cr_server.MainContextInfo;
|
---|
349 | cr_server.head_spu->dispatch_table.MakeCurrent(mural->spuWindow, 0, cr_server.MainContextInfo.SpuContext);
|
---|
350 | }
|
---|
351 |
|
---|
352 | if (pMuralContextInfo->CreateInfo.visualBits != mural->CreateInfo.visualBits)
|
---|
353 | {
|
---|
354 | crWarning("mural visual bits do not match with current context visual bits!");
|
---|
355 | }
|
---|
356 |
|
---|
357 | mural->cBuffers = 2;
|
---|
358 | mural->iBbBuffer = 0;
|
---|
359 | /*Color texture*/
|
---|
360 | for (i = 0; i < mural->cBuffers; ++i)
|
---|
361 | {
|
---|
362 | gl->GenTextures(1, &mural->aidColorTexs[i]);
|
---|
363 | gl->BindTexture(GL_TEXTURE_2D, mural->aidColorTexs[i]);
|
---|
364 | gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
365 | gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
366 | gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
---|
367 | gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
---|
368 | if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
|
---|
369 | {
|
---|
370 | gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
|
---|
371 | }
|
---|
372 | gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, mural->width, mural->height,
|
---|
373 | 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
|
---|
374 | }
|
---|
375 |
|
---|
376 | /*Depth&Stencil*/
|
---|
377 | gl->GenRenderbuffersEXT(1, &mural->idDepthStencilRB);
|
---|
378 | gl->BindRenderbufferEXT(GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
379 | gl->RenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT,
|
---|
380 | mural->width, mural->height);
|
---|
381 |
|
---|
382 | /*FBO*/
|
---|
383 | for (i = 0; i < mural->cBuffers; ++i)
|
---|
384 | {
|
---|
385 | gl->GenFramebuffersEXT(1, &mural->aidFBOs[i]);
|
---|
386 | gl->BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mural->aidFBOs[i]);
|
---|
387 |
|
---|
388 | gl->FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
|
---|
389 | GL_TEXTURE_2D, mural->aidColorTexs[i], 0);
|
---|
390 | gl->FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
|
---|
391 | GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
392 | gl->FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
|
---|
393 | GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
394 |
|
---|
395 | status = gl->CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
---|
396 | if (status!=GL_FRAMEBUFFER_COMPLETE_EXT)
|
---|
397 | {
|
---|
398 | crWarning("FBO status(0x%x) isn't complete", status);
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | mural->fboWidth = mural->width;
|
---|
403 | mural->fboHeight = mural->height;
|
---|
404 |
|
---|
405 | mural->iCurDrawBuffer = crServerMuralFBOIdxFromBufferName(mural, ctx->buffer.drawBuffer);
|
---|
406 | mural->iCurReadBuffer = crServerMuralFBOIdxFromBufferName(mural, ctx->buffer.readBuffer);
|
---|
407 |
|
---|
408 | /*PBO*/
|
---|
409 | if (cr_server.bUsePBOForReadback)
|
---|
410 | {
|
---|
411 | gl->GenBuffersARB(1, &mural->idPBO);
|
---|
412 | gl->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, mural->idPBO);
|
---|
413 | gl->BufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, mural->width*mural->height*4, 0, GL_STREAM_READ_ARB);
|
---|
414 | gl->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, ctx->bufferobject.packBuffer->hwid);
|
---|
415 |
|
---|
416 | if (!mural->idPBO)
|
---|
417 | {
|
---|
418 | crWarning("PBO create failed");
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | /*Restore gl state*/
|
---|
423 | uid = ctx->texture.unit[ctx->texture.curTextureUnit].currentTexture2D->hwid;
|
---|
424 | gl->BindTexture(GL_TEXTURE_2D, uid);
|
---|
425 |
|
---|
426 | uid = ctx->framebufferobject.renderbuffer ? ctx->framebufferobject.renderbuffer->hwid:0;
|
---|
427 | gl->BindRenderbufferEXT(GL_RENDERBUFFER_EXT, uid);
|
---|
428 |
|
---|
429 | uid = ctx->framebufferobject.drawFB ? ctx->framebufferobject.drawFB->hwid:0;
|
---|
430 | gl->BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, uid);
|
---|
431 |
|
---|
432 | uid = ctx->framebufferobject.readFB ? ctx->framebufferobject.readFB->hwid:0;
|
---|
433 | gl->BindFramebufferEXT(GL_READ_FRAMEBUFFER, uid);
|
---|
434 |
|
---|
435 | if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
|
---|
436 | {
|
---|
437 | gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, ctx->bufferobject.unpackBuffer->hwid);
|
---|
438 | }
|
---|
439 |
|
---|
440 | CRASSERT(mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
441 |
|
---|
442 | CrVrScrCompositorEntryTexNameUpdate(&mural->CEntry, mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
443 |
|
---|
444 | if (mural->fRootVrOn)
|
---|
445 | CrVrScrCompositorEntryTexNameUpdate(&mural->RootVrCEntry, mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
446 | }
|
---|
447 |
|
---|
448 | void crServerDeleteMuralFBO(CRMuralInfo *mural)
|
---|
449 | {
|
---|
450 | CRASSERT(!mural->fUseFBO);
|
---|
451 |
|
---|
452 | if (mural->aidFBOs[0]!=0)
|
---|
453 | {
|
---|
454 | GLuint i;
|
---|
455 | for (i = 0; i < mural->cBuffers; ++i)
|
---|
456 | {
|
---|
457 | cr_server.head_spu->dispatch_table.DeleteTextures(1, &mural->aidColorTexs[i]);
|
---|
458 | mural->aidColorTexs[i] = 0;
|
---|
459 | }
|
---|
460 |
|
---|
461 | cr_server.head_spu->dispatch_table.DeleteRenderbuffersEXT(1, &mural->idDepthStencilRB);
|
---|
462 | mural->idDepthStencilRB = 0;
|
---|
463 |
|
---|
464 | for (i = 0; i < mural->cBuffers; ++i)
|
---|
465 | {
|
---|
466 | cr_server.head_spu->dispatch_table.DeleteFramebuffersEXT(1, &mural->aidFBOs[i]);
|
---|
467 | mural->aidFBOs[i] = 0;
|
---|
468 | }
|
---|
469 | }
|
---|
470 |
|
---|
471 | if (mural->idPBO!=0)
|
---|
472 | {
|
---|
473 | CRASSERT(cr_server.bUsePBOForReadback);
|
---|
474 | cr_server.head_spu->dispatch_table.DeleteBuffersARB(1, &mural->idPBO);
|
---|
475 | mural->idPBO = 0;
|
---|
476 | }
|
---|
477 |
|
---|
478 | mural->cBuffers = 0;
|
---|
479 | }
|
---|
480 |
|
---|
481 | #define MIN(a, b) ((a) < (b) ? (a) : (b))
|
---|
482 | #define MAX(a, b) ((a) > (b) ? (a) : (b))
|
---|
483 |
|
---|
484 | static GLboolean crServerIntersectRect(CRrecti *a, CRrecti *b, CRrecti *rect)
|
---|
485 | {
|
---|
486 | CRASSERT(a && b && rect);
|
---|
487 |
|
---|
488 | rect->x1 = MAX(a->x1, b->x1);
|
---|
489 | rect->x2 = MIN(a->x2, b->x2);
|
---|
490 | rect->y1 = MAX(a->y1, b->y1);
|
---|
491 | rect->y2 = MIN(a->y2, b->y2);
|
---|
492 |
|
---|
493 | return (rect->x2>rect->x1) && (rect->y2>rect->y1);
|
---|
494 | }
|
---|
495 |
|
---|
496 | static GLboolean crServerIntersectScreen(CRMuralInfo *mural, int sId, CRrecti *rect)
|
---|
497 | {
|
---|
498 | rect->x1 = MAX(mural->gX, cr_server.screen[sId].x);
|
---|
499 | rect->x2 = MIN(mural->gX+(int)mural->fboWidth, cr_server.screen[sId].x+(int)cr_server.screen[sId].w);
|
---|
500 | rect->y1 = MAX(mural->gY, cr_server.screen[sId].y);
|
---|
501 | rect->y2 = MIN(mural->gY+(int)mural->fboHeight, cr_server.screen[sId].y+(int)cr_server.screen[sId].h);
|
---|
502 |
|
---|
503 | return (rect->x2>rect->x1) && (rect->y2>rect->y1);
|
---|
504 | }
|
---|
505 |
|
---|
506 | static void crServerCopySubImage(char *pDst, char* pSrc, CRrecti *pRect, int srcWidth, int srcHeight)
|
---|
507 | {
|
---|
508 | int i;
|
---|
509 | int dstrowsize = 4*(pRect->x2-pRect->x1);
|
---|
510 | int srcrowsize = 4*srcWidth;
|
---|
511 | int height = pRect->y2-pRect->y1;
|
---|
512 |
|
---|
513 | pSrc += 4*pRect->x1 + srcrowsize*(srcHeight-1-pRect->y1);
|
---|
514 |
|
---|
515 | for (i=0; i<height; ++i)
|
---|
516 | {
|
---|
517 | crMemcpy(pDst, pSrc, dstrowsize);
|
---|
518 |
|
---|
519 | pSrc -= srcrowsize;
|
---|
520 | pDst += dstrowsize;
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | static void crServerTransformRect(CRrecti *pDst, CRrecti *pSrc, int dx, int dy)
|
---|
525 | {
|
---|
526 | pDst->x1 = pSrc->x1+dx;
|
---|
527 | pDst->x2 = pSrc->x2+dx;
|
---|
528 | pDst->y1 = pSrc->y1+dy;
|
---|
529 | pDst->y2 = pSrc->y2+dy;
|
---|
530 | }
|
---|
531 |
|
---|
532 | static void crServerVBoxCompositionPresentPerform(CRMuralInfo *mural)
|
---|
533 | {
|
---|
534 | CRMuralInfo *currentMural = cr_server.currentMural;
|
---|
535 | CRContextInfo *curCtxInfo = cr_server.currentCtxInfo;
|
---|
536 | GLuint idDrawFBO, idReadFBO;
|
---|
537 | CRContext *curCtx = curCtxInfo->pContext;
|
---|
538 |
|
---|
539 | CRASSERT(curCtx == crStateGetCurrent());
|
---|
540 |
|
---|
541 | mural->fDataPresented = GL_TRUE;
|
---|
542 |
|
---|
543 | if (currentMural)
|
---|
544 | {
|
---|
545 | idDrawFBO = currentMural->aidFBOs[currentMural->iCurDrawBuffer];
|
---|
546 | idReadFBO = currentMural->aidFBOs[currentMural->iCurReadBuffer];
|
---|
547 | }
|
---|
548 | else
|
---|
549 | {
|
---|
550 | idDrawFBO = 0;
|
---|
551 | idReadFBO = 0;
|
---|
552 | }
|
---|
553 |
|
---|
554 | crStateSwitchPrepare(NULL, curCtx, idDrawFBO, idReadFBO);
|
---|
555 |
|
---|
556 | if (!mural->fRootVrOn)
|
---|
557 | cr_server.head_spu->dispatch_table.VBoxPresentComposition(mural->spuWindow, &mural->Compositor, &mural->CEntry);
|
---|
558 | else
|
---|
559 | cr_server.head_spu->dispatch_table.VBoxPresentComposition(mural->spuWindow, &mural->RootVrCompositor, &mural->RootVrCEntry);
|
---|
560 |
|
---|
561 | crStateSwitchPostprocess(curCtx, NULL, idDrawFBO, idReadFBO);
|
---|
562 | }
|
---|
563 |
|
---|
564 | void crServerVBoxCompositionPresent(CRMuralInfo *mural)
|
---|
565 | {
|
---|
566 | if (!crServerVBoxCompositionPresentNeeded(mural))
|
---|
567 | return;
|
---|
568 | crServerVBoxCompositionPresentPerform(mural);
|
---|
569 | }
|
---|
570 |
|
---|
571 | static void crServerVBoxCompositionReenable(CRMuralInfo *mural, GLboolean fForcePresent)
|
---|
572 | {
|
---|
573 | if (mural->fUseFBO != CR_SERVER_REDIR_FBO_BLT
|
---|
574 | || !mural->fDataPresented
|
---|
575 | || (!fForcePresent
|
---|
576 | && !crServerVBoxCompositionPresentNeeded(mural)))
|
---|
577 | return;
|
---|
578 |
|
---|
579 | crServerVBoxCompositionPresentPerform(mural);
|
---|
580 | }
|
---|
581 |
|
---|
582 | static void crServerVBoxCompositionDisable(CRMuralInfo *mural)
|
---|
583 | {
|
---|
584 | if (mural->fUseFBO != CR_SERVER_REDIR_FBO_BLT
|
---|
585 | || !mural->fDataPresented)
|
---|
586 | return;
|
---|
587 | cr_server.head_spu->dispatch_table.VBoxPresentComposition(mural->spuWindow, NULL, NULL);
|
---|
588 | }
|
---|
589 |
|
---|
590 | void crServerVBoxCompositionDisableEnter(CRMuralInfo *mural)
|
---|
591 | {
|
---|
592 | ++mural->cDisabled;
|
---|
593 | Assert(mural->cDisabled);
|
---|
594 | if (mural->cDisabled == 1)
|
---|
595 | {
|
---|
596 | crServerVBoxCompositionDisable(mural);
|
---|
597 | }
|
---|
598 | }
|
---|
599 |
|
---|
600 | void crServerVBoxCompositionDisableLeave(CRMuralInfo *mural, GLboolean fForcePresentOnEnabled)
|
---|
601 | {
|
---|
602 | mural->fForcePresentState = fForcePresentOnEnabled;
|
---|
603 | --mural->cDisabled;
|
---|
604 | Assert(mural->cDisabled < UINT32_MAX/2);
|
---|
605 | if (!mural->cDisabled)
|
---|
606 | {
|
---|
607 | crServerVBoxCompositionReenable(mural, mural->fForcePresentState);
|
---|
608 | mural->fForcePresentState = GL_FALSE;
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | static void crServerVBoxCompositionSetEnableStateGlobalCB(unsigned long key, void *data1, void *data2)
|
---|
613 | {
|
---|
614 | CRMuralInfo *mural = (CRMuralInfo *)data1;
|
---|
615 |
|
---|
616 | if (data2)
|
---|
617 | crServerVBoxCompositionDisableLeave(mural, GL_FALSE);
|
---|
618 | else
|
---|
619 | crServerVBoxCompositionDisableEnter(mural);
|
---|
620 | }
|
---|
621 |
|
---|
622 | DECLEXPORT(void) crServerVBoxCompositionSetEnableStateGlobal(GLboolean fEnable)
|
---|
623 | {
|
---|
624 | crHashtableWalk(cr_server.muralTable, crServerVBoxCompositionSetEnableStateGlobalCB, (void*)fEnable);
|
---|
625 |
|
---|
626 | crHashtableWalk(cr_server.dummyMuralTable, crServerVBoxCompositionSetEnableStateGlobalCB, (void*)fEnable);
|
---|
627 | }
|
---|
628 |
|
---|
629 | void crServerPresentFBO(CRMuralInfo *mural)
|
---|
630 | {
|
---|
631 | char *pixels=NULL, *tmppixels;
|
---|
632 | int i, j;
|
---|
633 | CRrecti rect, rectwr, sectr;
|
---|
634 | GLuint idPBO;
|
---|
635 | CRContext *ctx = crStateGetCurrent();
|
---|
636 | VBOXVR_TEXTURE Tex;
|
---|
637 |
|
---|
638 | CRASSERT(mural->fUseFBO);
|
---|
639 | CRASSERT(cr_server.pfnPresentFBO || mural->fUseFBO == CR_SERVER_REDIR_FBO_BLT);
|
---|
640 |
|
---|
641 | if (mural->fUseFBO == CR_SERVER_REDIR_FBO_BLT)
|
---|
642 | {
|
---|
643 | crServerVBoxCompositionPresent(mural);
|
---|
644 | return;
|
---|
645 | }
|
---|
646 |
|
---|
647 | if (!crServerVBoxCompositionPresentNeeded(mural))
|
---|
648 | return;
|
---|
649 |
|
---|
650 | mural->fDataPresented = GL_TRUE;
|
---|
651 |
|
---|
652 | Tex.width = mural->width;
|
---|
653 | Tex.height = mural->height;
|
---|
654 | Tex.target = GL_TEXTURE_2D;
|
---|
655 | Tex.hwid = mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)];
|
---|
656 | CRASSERT(Tex.hwid);
|
---|
657 |
|
---|
658 | if (cr_server.bUsePBOForReadback && !mural->idPBO)
|
---|
659 | {
|
---|
660 | crWarning("Mural doesn't have PBO even though bUsePBOForReadback is set!");
|
---|
661 | }
|
---|
662 |
|
---|
663 | idPBO = cr_server.bUsePBOForReadback ? mural->idPBO : 0;
|
---|
664 | if (idPBO)
|
---|
665 | {
|
---|
666 | CRASSERT(mural->fboWidth == mural->width);
|
---|
667 | CRASSERT(mural->fboHeight == mural->height);
|
---|
668 | }
|
---|
669 |
|
---|
670 | pixels = CrHlpGetTexImage(ctx, &Tex, idPBO, GL_BGRA);
|
---|
671 | if (!pixels)
|
---|
672 | {
|
---|
673 | crWarning("CrHlpGetTexImage failed in crServerPresentFBO");
|
---|
674 | return;
|
---|
675 | }
|
---|
676 |
|
---|
677 | for (i=0; i<cr_server.screenCount; ++i)
|
---|
678 | {
|
---|
679 | if (crServerIntersectScreen(mural, i, &rect))
|
---|
680 | {
|
---|
681 | /* rect in window relative coords */
|
---|
682 | crServerTransformRect(&rectwr, &rect, -mural->gX, -mural->gY);
|
---|
683 |
|
---|
684 | if (!mural->pVisibleRects)
|
---|
685 | {
|
---|
686 | /*we don't get any rects info for guest compiz windows, so we treat windows as visible unless explicitly received 0 visible rects*/
|
---|
687 | if (!mural->bReceivedRects)
|
---|
688 | {
|
---|
689 | tmppixels = crAlloc(4*(rect.x2-rect.x1)*(rect.y2-rect.y1));
|
---|
690 | if (!tmppixels)
|
---|
691 | {
|
---|
692 | crWarning("Out of memory in crServerPresentFBO");
|
---|
693 | crFree(pixels);
|
---|
694 | return;
|
---|
695 | }
|
---|
696 |
|
---|
697 | crServerCopySubImage(tmppixels, pixels, &rectwr, mural->fboWidth, mural->fboHeight);
|
---|
698 | /*Note: pfnPresentFBO would free tmppixels*/
|
---|
699 | 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);
|
---|
700 | }
|
---|
701 | }
|
---|
702 | else
|
---|
703 | {
|
---|
704 | for (j=0; j<mural->cVisibleRects; ++j)
|
---|
705 | {
|
---|
706 | if (crServerIntersectRect(&rectwr, (CRrecti*) &mural->pVisibleRects[4*j], §r))
|
---|
707 | {
|
---|
708 | tmppixels = crAlloc(4*(sectr.x2-sectr.x1)*(sectr.y2-sectr.y1));
|
---|
709 | if (!tmppixels)
|
---|
710 | {
|
---|
711 | crWarning("Out of memory in crServerPresentFBO");
|
---|
712 | crFree(pixels);
|
---|
713 | return;
|
---|
714 | }
|
---|
715 |
|
---|
716 | crServerCopySubImage(tmppixels, pixels, §r, mural->fboWidth, mural->fboHeight);
|
---|
717 | /*Note: pfnPresentFBO would free tmppixels*/
|
---|
718 | cr_server.pfnPresentFBO(tmppixels, i,
|
---|
719 | sectr.x1+mural->gX-cr_server.screen[i].x,
|
---|
720 | sectr.y1+mural->gY-cr_server.screen[i].y,
|
---|
721 | sectr.x2-sectr.x1, sectr.y2-sectr.y1);
|
---|
722 | }
|
---|
723 | }
|
---|
724 | }
|
---|
725 | }
|
---|
726 | }
|
---|
727 |
|
---|
728 | if (mural->pvOutputRedirectInstance)
|
---|
729 | {
|
---|
730 | /* @todo find out why presentfbo is not called but crorframe is called. */
|
---|
731 | cr_server.outputRedirect.CRORFrame(mural->pvOutputRedirectInstance,
|
---|
732 | pixels,
|
---|
733 | 4 * mural->fboWidth * mural->fboHeight);
|
---|
734 | }
|
---|
735 |
|
---|
736 | CrHlpFreeTexImage(ctx, idPBO, pixels);
|
---|
737 | }
|
---|
738 |
|
---|
739 | GLboolean crServerIsRedirectedToFBO()
|
---|
740 | {
|
---|
741 | #ifdef DEBUG_misha
|
---|
742 | Assert(cr_server.curClient);
|
---|
743 | if (cr_server.curClient)
|
---|
744 | {
|
---|
745 | Assert(cr_server.curClient->currentMural == cr_server.currentMural);
|
---|
746 | Assert(cr_server.curClient->currentCtxInfo == cr_server.currentCtxInfo);
|
---|
747 | }
|
---|
748 | #endif
|
---|
749 | return cr_server.curClient
|
---|
750 | && cr_server.curClient->currentMural
|
---|
751 | && cr_server.curClient->currentMural->fUseFBO;
|
---|
752 | }
|
---|
753 |
|
---|
754 | GLuint crServerMuralFBOIdxFromBufferName(CRMuralInfo *mural, GLenum buffer)
|
---|
755 | {
|
---|
756 | if (buffer == GL_FRONT
|
---|
757 | || buffer == GL_FRONT_LEFT
|
---|
758 | || buffer == GL_FRONT_RIGHT)
|
---|
759 | return CR_SERVER_FBO_FB_IDX(mural);
|
---|
760 | return CR_SERVER_FBO_BB_IDX(mural);
|
---|
761 | }
|
---|
762 |
|
---|
763 | void crServerMuralFBOSwapBuffers(CRMuralInfo *mural)
|
---|
764 | {
|
---|
765 | CRContext *ctx = crStateGetCurrent();
|
---|
766 | GLuint iOldCurDrawBuffer = mural->iCurDrawBuffer;
|
---|
767 | GLuint iOldCurReadBuffer = mural->iCurReadBuffer;
|
---|
768 | mural->iBbBuffer = ((mural->iBbBuffer + 1) % (mural->cBuffers));
|
---|
769 | mural->iCurDrawBuffer = ((mural->iCurDrawBuffer + 1) % (mural->cBuffers));
|
---|
770 | mural->iCurReadBuffer = ((mural->iCurReadBuffer + 1) % (mural->cBuffers));
|
---|
771 | Assert(iOldCurDrawBuffer != mural->iCurDrawBuffer || mural->cBuffers == 1);
|
---|
772 | Assert(iOldCurReadBuffer != mural->iCurReadBuffer || mural->cBuffers == 1);
|
---|
773 | if (!ctx->framebufferobject.drawFB && iOldCurDrawBuffer != mural->iCurDrawBuffer)
|
---|
774 | {
|
---|
775 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, mural->aidFBOs[mural->iCurDrawBuffer]);
|
---|
776 | }
|
---|
777 | if (!ctx->framebufferobject.readFB && iOldCurReadBuffer != mural->iCurReadBuffer)
|
---|
778 | {
|
---|
779 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, mural->aidFBOs[mural->iCurReadBuffer]);
|
---|
780 | }
|
---|
781 | Assert(mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
782 | CrVrScrCompositorEntryTexNameUpdate(&mural->CEntry, mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
783 | if (mural->fRootVrOn)
|
---|
784 | CrVrScrCompositorEntryTexNameUpdate(&mural->RootVrCEntry, mural->aidColorTexs[CR_SERVER_FBO_FB_IDX(mural)]);
|
---|
785 | }
|
---|