1 | /* $Id: server_muralfbo.c 28242 2010-04-13 10:47:48Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | * VBox crOpenGL: Window to FBO redirect support.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2010 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "server.h"
|
---|
24 | #include "cr_string.h"
|
---|
25 | #include "cr_mem.h"
|
---|
26 | #include "render/renderspu.h"
|
---|
27 |
|
---|
28 | static int crServerGetPointScreen(GLint x, GLint y)
|
---|
29 | {
|
---|
30 | int i;
|
---|
31 |
|
---|
32 | for (i=0; i<cr_server.screenCount; ++i)
|
---|
33 | {
|
---|
34 | if ((x>=cr_server.screen[i].x && x<=cr_server.screen[i].x+(int)cr_server.screen[i].w)
|
---|
35 | && (y>=cr_server.screen[i].y && y<=cr_server.screen[i].y+(int)cr_server.screen[i].h))
|
---|
36 | {
|
---|
37 | return i;
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | return -1;
|
---|
42 | }
|
---|
43 |
|
---|
44 | static GLboolean crServerMuralCoverScreen(CRMuralInfo *mural, int sId)
|
---|
45 | {
|
---|
46 | return mural->gX<=cr_server.screen[sId].x
|
---|
47 | && mural->gX>=cr_server.screen[sId].x+(int)cr_server.screen[sId].w
|
---|
48 | && mural->gY<=cr_server.screen[sId].y
|
---|
49 | && mural->gY>=cr_server.screen[sId].y+(int)cr_server.screen[sId].h;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void crServerCheckMuralGeometry(CRMuralInfo *mural)
|
---|
53 | {
|
---|
54 | int tlS, brS, trS, blS;
|
---|
55 | int overlappingScreenCount, primaryS, i;
|
---|
56 |
|
---|
57 | if (cr_server.screenCount<2)
|
---|
58 | {
|
---|
59 | CRASSERT(cr_server.screenCount>0);
|
---|
60 |
|
---|
61 | mural->hX = mural->gX-cr_server.screen[0].x;
|
---|
62 | mural->hY = mural->gY-cr_server.screen[0].y;
|
---|
63 |
|
---|
64 | cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX, mural->hY);
|
---|
65 |
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | tlS = crServerGetPointScreen(mural->gX, mural->gY);
|
---|
70 | brS = crServerGetPointScreen(mural->gX+mural->width, mural->gY+mural->height);
|
---|
71 |
|
---|
72 | if (tlS==brS && tlS>=0)
|
---|
73 | {
|
---|
74 | overlappingScreenCount = 1;
|
---|
75 | primaryS = tlS;
|
---|
76 | }
|
---|
77 | else
|
---|
78 | {
|
---|
79 | trS = crServerGetPointScreen(mural->gX+mural->width, mural->gY);
|
---|
80 | blS = crServerGetPointScreen(mural->gX, mural->gY+mural->height);
|
---|
81 |
|
---|
82 | primaryS = -1; overlappingScreenCount = 0;
|
---|
83 | for (i=0; i<cr_server.screenCount; ++i)
|
---|
84 | {
|
---|
85 | if ((i==tlS) || (i==brS) || (i==trS) || (i==blS)
|
---|
86 | || crServerMuralCoverScreen(mural, i))
|
---|
87 | {
|
---|
88 | overlappingScreenCount++;
|
---|
89 | primaryS = primaryS<0 ? i:primaryS;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (!overlappingScreenCount)
|
---|
94 | {
|
---|
95 | primaryS = 0;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (primaryS!=mural->screenId)
|
---|
100 | {
|
---|
101 | mural->screenId = primaryS;
|
---|
102 |
|
---|
103 | renderspuSetWindowId(cr_server.screen[primaryS].winID);
|
---|
104 | renderspuReparentWindow(mural->spuWindow);
|
---|
105 | renderspuSetWindowId(cr_server.screen[0].winID);
|
---|
106 | }
|
---|
107 |
|
---|
108 | mural->hX = mural->gX-cr_server.screen[primaryS].x;
|
---|
109 | mural->hY = mural->gY-cr_server.screen[primaryS].y;
|
---|
110 |
|
---|
111 | if (overlappingScreenCount<2)
|
---|
112 | {
|
---|
113 | if (mural->bUseFBO)
|
---|
114 | {
|
---|
115 | crServerRedirMuralFBO(mural, GL_FALSE);
|
---|
116 | crServerDeleteMuralFBO(mural);
|
---|
117 | }
|
---|
118 |
|
---|
119 | cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX, mural->hY);
|
---|
120 | }
|
---|
121 | else
|
---|
122 | {
|
---|
123 | if (!mural->bUseFBO)
|
---|
124 | {
|
---|
125 | crServerRedirMuralFBO(mural, GL_TRUE);
|
---|
126 | }
|
---|
127 | else
|
---|
128 | {
|
---|
129 | if (mural->width!=mural->fboWidth
|
---|
130 | || mural->height!=mural->height)
|
---|
131 | {
|
---|
132 | crServerRedirMuralFBO(mural, GL_FALSE);
|
---|
133 | crServerDeleteMuralFBO(mural);
|
---|
134 | crServerRedirMuralFBO(mural, GL_TRUE);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (!mural->bUseFBO)
|
---|
139 | {
|
---|
140 | cr_server.head_spu->dispatch_table.WindowPosition(mural->spuWindow, mural->hX, mural->hY);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | GLboolean crServerSupportRedirMuralFBO(void)
|
---|
146 | {
|
---|
147 | const GLubyte* pExt = cr_server.head_spu->dispatch_table.GetString(GL_REAL_EXTENSIONS);
|
---|
148 |
|
---|
149 | return NULL!=crStrstr((const char*)pExt, "GL_ARB_framebuffer_object")
|
---|
150 | && NULL!=crStrstr((const char*)pExt, "GL_ARB_texture_non_power_of_two");
|
---|
151 | }
|
---|
152 |
|
---|
153 | void crServerRedirMuralFBO(CRMuralInfo *mural, GLboolean redir)
|
---|
154 | {
|
---|
155 | if (redir)
|
---|
156 | {
|
---|
157 | if (!crServerSupportRedirMuralFBO())
|
---|
158 | {
|
---|
159 | crWarning("FBO not supported, can't redirect window output");
|
---|
160 | return;
|
---|
161 | }
|
---|
162 |
|
---|
163 | cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, GL_FALSE);
|
---|
164 |
|
---|
165 | if (mural->idFBO==0)
|
---|
166 | {
|
---|
167 | crServerCreateMuralFBO(mural);
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (!crStateGetCurrent()->framebufferobject.drawFB)
|
---|
171 | {
|
---|
172 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mural->idFBO);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | else
|
---|
176 | {
|
---|
177 | cr_server.head_spu->dispatch_table.WindowShow(mural->spuWindow, mural->bVisible);
|
---|
178 |
|
---|
179 | if (mural->bUseFBO && crServerSupportRedirMuralFBO()
|
---|
180 | && !crStateGetCurrent()->framebufferobject.drawFB)
|
---|
181 | {
|
---|
182 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | mural->bUseFBO = redir;
|
---|
187 | }
|
---|
188 |
|
---|
189 | void crServerCreateMuralFBO(CRMuralInfo *mural)
|
---|
190 | {
|
---|
191 | CRContext *ctx = crStateGetCurrent();
|
---|
192 | GLuint uid;
|
---|
193 | GLenum status;
|
---|
194 |
|
---|
195 | CRASSERT(mural->idFBO==0);
|
---|
196 |
|
---|
197 | /*Color texture*/
|
---|
198 | cr_server.head_spu->dispatch_table.GenTextures(1, &mural->idColorTex);
|
---|
199 | cr_server.head_spu->dispatch_table.BindTexture(GL_TEXTURE_2D, mural->idColorTex);
|
---|
200 | cr_server.head_spu->dispatch_table.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
201 | cr_server.head_spu->dispatch_table.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
202 | cr_server.head_spu->dispatch_table.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
---|
203 | cr_server.head_spu->dispatch_table.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
---|
204 | cr_server.head_spu->dispatch_table.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, mural->width, mural->height,
|
---|
205 | 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
|
---|
206 |
|
---|
207 | /*Depth&Stencil*/
|
---|
208 | cr_server.head_spu->dispatch_table.GenRenderbuffersEXT(1, &mural->idDepthStencilRB);
|
---|
209 | cr_server.head_spu->dispatch_table.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
210 | cr_server.head_spu->dispatch_table.RenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT,
|
---|
211 | mural->width, mural->height);
|
---|
212 |
|
---|
213 | /*FBO*/
|
---|
214 | cr_server.head_spu->dispatch_table.GenFramebuffersEXT(1, &mural->idFBO);
|
---|
215 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mural->idFBO);
|
---|
216 |
|
---|
217 | cr_server.head_spu->dispatch_table.FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
|
---|
218 | GL_TEXTURE_2D, mural->idColorTex, 0);
|
---|
219 | cr_server.head_spu->dispatch_table.FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
|
---|
220 | GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
221 | cr_server.head_spu->dispatch_table.FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
|
---|
222 | GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
223 |
|
---|
224 | status = cr_server.head_spu->dispatch_table.CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
---|
225 | if (status!=GL_FRAMEBUFFER_COMPLETE_EXT)
|
---|
226 | {
|
---|
227 | crWarning("FBO status(0x%x) isn't complete", status);
|
---|
228 | }
|
---|
229 |
|
---|
230 | mural->fboWidth = mural->width;
|
---|
231 | mural->fboHeight = mural->height;
|
---|
232 |
|
---|
233 | /*Restore gl state*/
|
---|
234 | uid = ctx->texture.unit[ctx->texture.curTextureUnit].currentTexture2D->name;
|
---|
235 | cr_server.head_spu->dispatch_table.BindTexture(GL_TEXTURE_2D, uid);
|
---|
236 |
|
---|
237 | uid = ctx->framebufferobject.renderbuffer ? ctx->framebufferobject.renderbuffer->hwid:0;
|
---|
238 | cr_server.head_spu->dispatch_table.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, mural->idDepthStencilRB);
|
---|
239 |
|
---|
240 | uid = ctx->framebufferobject.drawFB ? ctx->framebufferobject.drawFB->hwid:0;
|
---|
241 | cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, uid);
|
---|
242 | }
|
---|
243 |
|
---|
244 | void crServerDeleteMuralFBO(CRMuralInfo *mural)
|
---|
245 | {
|
---|
246 | CRASSERT(!mural->bUseFBO);
|
---|
247 |
|
---|
248 | if (mural->idFBO!=0)
|
---|
249 | {
|
---|
250 | cr_server.head_spu->dispatch_table.DeleteTextures(1, &mural->idColorTex);
|
---|
251 | cr_server.head_spu->dispatch_table.DeleteRenderbuffersEXT(1, &mural->idDepthStencilRB);
|
---|
252 | cr_server.head_spu->dispatch_table.DeleteFramebuffersEXT(1, &mural->idFBO);
|
---|
253 |
|
---|
254 | mural->idFBO = 0;
|
---|
255 | mural->idColorTex = 0;
|
---|
256 | mural->idDepthStencilRB = 0;
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | #define MIN(a, b) ((a) < (b) ? (a) : (b))
|
---|
261 | #define MAX(a, b) ((a) > (b) ? (a) : (b))
|
---|
262 |
|
---|
263 | static GLboolean crServerIntersectScreen(CRMuralInfo *mural, int sId, CRrecti *rect)
|
---|
264 | {
|
---|
265 | rect->x1 = MAX(mural->gX, cr_server.screen[sId].x);
|
---|
266 | rect->x2 = MIN(mural->gX+(int)mural->fboWidth, cr_server.screen[sId].x+(int)cr_server.screen[sId].w);
|
---|
267 | rect->y1 = MAX(mural->gY, cr_server.screen[sId].y);
|
---|
268 | rect->y2 = MIN(mural->gY+(int)mural->fboHeight, cr_server.screen[sId].y+(int)cr_server.screen[sId].h);
|
---|
269 |
|
---|
270 | return (rect->x2>rect->x1) && (rect->y2>rect->y1);
|
---|
271 | }
|
---|
272 |
|
---|
273 | void crServerPresentFBO(CRMuralInfo *mural)
|
---|
274 | {
|
---|
275 | char *pixels, *tmppixels, *pSrc, *pDst;
|
---|
276 | GLuint uid;
|
---|
277 | int i, j, rowsize, rowstride, height;
|
---|
278 | CRrecti rect;
|
---|
279 | CRContext *ctx = crStateGetCurrent();
|
---|
280 |
|
---|
281 | CRASSERT(cr_server.pfnPresentFBO);
|
---|
282 |
|
---|
283 | pixels = crAlloc(4*mural->fboWidth*mural->fboHeight);
|
---|
284 | if (!pixels)
|
---|
285 | {
|
---|
286 | crWarning("Out of memory in crServerPresentFBO");
|
---|
287 | return;
|
---|
288 | }
|
---|
289 |
|
---|
290 | cr_server.head_spu->dispatch_table.BindTexture(GL_TEXTURE_2D, mural->idColorTex);
|
---|
291 | cr_server.head_spu->dispatch_table.GetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
|
---|
292 | uid = ctx->texture.unit[ctx->texture.curTextureUnit].currentTexture2D->name;
|
---|
293 | cr_server.head_spu->dispatch_table.BindTexture(GL_TEXTURE_2D, uid);
|
---|
294 |
|
---|
295 | for (i=0; i<cr_server.screenCount; ++i)
|
---|
296 | {
|
---|
297 | if (crServerIntersectScreen(mural, i, &rect))
|
---|
298 | {
|
---|
299 | rowsize = 4*(rect.x2-rect.x1);
|
---|
300 | tmppixels = crAlloc(rowsize*(rect.y2-rect.y1));
|
---|
301 |
|
---|
302 | if (!tmppixels)
|
---|
303 | {
|
---|
304 | crWarning("Out of memory in crServerPresentFBO");
|
---|
305 | crFree(pixels);
|
---|
306 | return;
|
---|
307 | }
|
---|
308 | rowstride = 4*mural->fboWidth;
|
---|
309 | height = rect.y2-rect.y1;
|
---|
310 |
|
---|
311 | pSrc = pixels + 4*(rect.x1-mural->gX) + rowstride*(rect.y2-mural->gY-1);
|
---|
312 | pDst = tmppixels;
|
---|
313 |
|
---|
314 | for (j=0; j<height; ++j)
|
---|
315 | {
|
---|
316 | crMemcpy(pDst, pSrc, rowsize);
|
---|
317 |
|
---|
318 | pSrc -= rowstride;
|
---|
319 | pDst += rowsize;
|
---|
320 | }
|
---|
321 |
|
---|
322 | cr_server.pfnPresentFBO(tmppixels, i, rect.x1-cr_server.screen[i].x, rect.y1-cr_server.screen[i].y, rect.x2-rect.x1, height);
|
---|
323 |
|
---|
324 | crFree(tmppixels);
|
---|
325 | }
|
---|
326 | }
|
---|
327 | crFree(pixels);
|
---|
328 | }
|
---|
329 |
|
---|
330 | GLboolean crServerIsRedirectedToFBO()
|
---|
331 | {
|
---|
332 | return cr_server.curClient
|
---|
333 | && cr_server.curClient->currentMural
|
---|
334 | && cr_server.curClient->currentMural->bUseFBO;
|
---|
335 | }
|
---|