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