VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c@ 16684

Last change on this file since 16684 was 16477, checked in by vboxsync, 16 years ago

LGPL disclaimer by filemuncher

  • Property svn:eol-style set to native
File size: 18.9 KB
Line 
1/*
2 *IDirect3DSwapChain9 implementation
3 *
4 *Copyright 2002-2003 Jason Edmeades
5 *Copyright 2002-2003 Raphael Junqueira
6 *Copyright 2005 Oliver Stieber
7 *Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 *
9 *This library is free software; you can redistribute it and/or
10 *modify it under the terms of the GNU Lesser General Public
11 *License as published by the Free Software Foundation; either
12 *version 2.1 of the License, or (at your option) any later version.
13 *
14 *This library is distributed in the hope that it will be useful,
15 *but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 *Lesser General Public License for more details.
18 *
19 *You should have received a copy of the GNU Lesser General Public
20 *License along with this library; if not, write to the Free Software
21 *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24/*
25 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
26 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
27 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
28 * a choice of LGPL license versions is made available with the language indicating
29 * that LGPLv2 or any later version may be used, or where a choice of which version
30 * of the LGPL is applied is otherwise unspecified.
31 */
32
33#include "config.h"
34#include "wined3d_private.h"
35
36
37/*TODO: some of the additional parameters may be required to
38 set the gamma ramp (for some weird reason microsoft have left swap gammaramp in device
39 but it operates on a swapchain, it may be a good idea to move it to IWineD3DSwapChain for IWineD3D)*/
40
41
42WINE_DEFAULT_DEBUG_CHANNEL(d3d);
43WINE_DECLARE_DEBUG_CHANNEL(fps);
44
45#define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
46
47/*IWineD3DSwapChain parts follow: */
48static void WINAPI IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroyRenderTarget) {
49 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
50 WINED3DDISPLAYMODE mode;
51 unsigned int i;
52
53 TRACE("Destroying swapchain %p\n", iface);
54
55 IWineD3DSwapChain_SetGammaRamp(iface, 0, &This->orig_gamma);
56
57 /* release the ref to the front and back buffer parents */
58 if(This->frontBuffer) {
59 IWineD3DSurface_SetContainer(This->frontBuffer, 0);
60 if(D3DCB_DestroyRenderTarget(This->frontBuffer) > 0) {
61 FIXME("(%p) Something's still holding the front buffer\n",This);
62 }
63 }
64
65 if(This->backBuffer) {
66 UINT i;
67 for(i = 0; i < This->presentParms.BackBufferCount; i++) {
68 IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
69 if(D3DCB_DestroyRenderTarget(This->backBuffer[i]) > 0) {
70 FIXME("(%p) Something's still holding the back buffer\n",This);
71 }
72 }
73 HeapFree(GetProcessHeap(), 0, This->backBuffer);
74 }
75
76 for(i = 0; i < This->num_contexts; i++) {
77 DestroyContext(This->wineD3DDevice, This->context[i]);
78 }
79 /* Restore the screen resolution if we rendered in fullscreen
80 * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
81 * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
82 * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
83 */
84 if(This->presentParms.Windowed == FALSE && This->presentParms.AutoRestoreDisplayMode) {
85 mode.Width = This->orig_width;
86 mode.Height = This->orig_height;
87 mode.RefreshRate = 0;
88 mode.Format = This->orig_fmt;
89 IWineD3DDevice_SetDisplayMode((IWineD3DDevice *) This->wineD3DDevice, 0, &mode);
90 }
91 HeapFree(GetProcessHeap(), 0, This->context);
92
93 HeapFree(GetProcessHeap(), 0, This);
94}
95
96static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
97 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
98 unsigned int sync;
99 int retval;
100
101
102 ActivateContext(This->wineD3DDevice, This->backBuffer[0], CTXUSAGE_RESOURCELOAD);
103
104 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
105 if(This->wineD3DDevice->bCursorVisible && This->wineD3DDevice->cursorTexture) {
106 IWineD3DSurfaceImpl cursor;
107 RECT destRect = {This->wineD3DDevice->xScreenSpace - This->wineD3DDevice->xHotSpot,
108 This->wineD3DDevice->yScreenSpace - This->wineD3DDevice->yHotSpot,
109 This->wineD3DDevice->xScreenSpace + This->wineD3DDevice->cursorWidth - This->wineD3DDevice->xHotSpot,
110 This->wineD3DDevice->yScreenSpace + This->wineD3DDevice->cursorHeight - This->wineD3DDevice->yHotSpot};
111 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
112 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
113 * the application because we are only supposed to copy the information out. Using a fake surface
114 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
115 */
116 memset(&cursor, 0, sizeof(cursor));
117 cursor.lpVtbl = &IWineD3DSurface_Vtbl;
118 cursor.resource.ref = 1;
119 cursor.resource.wineD3DDevice = This->wineD3DDevice;
120 cursor.resource.pool = WINED3DPOOL_SCRATCH;
121 cursor.resource.format = WINED3DFMT_A8R8G8B8;
122 cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
123 cursor.glDescription.textureName = This->wineD3DDevice->cursorTexture;
124 cursor.glDescription.target = GL_TEXTURE_2D;
125 cursor.glDescription.level = 0;
126 cursor.currentDesc.Width = This->wineD3DDevice->cursorWidth;
127 cursor.currentDesc.Height = This->wineD3DDevice->cursorHeight;
128 cursor.glRect.left = 0;
129 cursor.glRect.top = 0;
130 cursor.glRect.right = cursor.currentDesc.Width;
131 cursor.glRect.bottom = cursor.currentDesc.Height;
132 /* The cursor must have pow2 sizes */
133 cursor.pow2Width = cursor.currentDesc.Width;
134 cursor.pow2Height = cursor.currentDesc.Height;
135 /* The surface is in the texture */
136 cursor.Flags |= SFLAG_INTEXTURE;
137 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
138 * which is exactly what we want :-)
139 */
140 if (This->presentParms.Windowed) {
141 MapWindowPoints(NULL, This->win_handle, (LPPOINT)&destRect, 2);
142 }
143 IWineD3DSurface_Blt(This->backBuffer[0], &destRect, (IWineD3DSurface *) &cursor, NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_NONE);
144 }
145 if(This->wineD3DDevice->logo_surface) {
146 /* Blit the logo into the upper left corner of the drawable */
147 IWineD3DSurface_BltFast(This->backBuffer[0], 0, 0, This->wineD3DDevice->logo_surface, NULL, WINEDDBLTFAST_SRCCOLORKEY);
148 }
149
150 if (pSourceRect || pDestRect) FIXME("Unhandled present rects %s/%s\n", wine_dbgstr_rect(pSourceRect), wine_dbgstr_rect(pDestRect));
151 /* TODO: If only source rect or dest rect are supplied then clip the window to match */
152 TRACE("presetting HDC %p\n", This->context[0]->hdc);
153
154 /* Don't call checkGLcall, as glGetError is not applicable here */
155 if (hDestWindowOverride && This->win_handle != hDestWindowOverride) {
156 IWineD3DSwapChain_SetDestWindowOverride(iface, hDestWindowOverride);
157 }
158
159 SwapBuffers(This->context[0]->hdc); /* TODO: cycle through the swapchain buffers */
160
161 TRACE("SwapBuffers called, Starting new frame\n");
162 /* FPS support */
163 if (TRACE_ON(fps))
164 {
165 DWORD time = GetTickCount();
166 This->frames++;
167 /* every 1.5 seconds */
168 if (time - This->prev_time > 1500) {
169 TRACE_(fps)("%p @ approx %.2ffps\n", This, 1000.0*This->frames/(time - This->prev_time));
170 This->prev_time = time;
171 This->frames = 0;
172 }
173 }
174
175#if defined(FRAME_DEBUGGING)
176{
177 if (GetFileAttributesA("C:\\D3DTRACE") != INVALID_FILE_ATTRIBUTES) {
178 if (!isOn) {
179 isOn = TRUE;
180 FIXME("Enabling D3D Trace\n");
181 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 1);
182#if defined(SHOW_FRAME_MAKEUP)
183 FIXME("Singe Frame snapshots Starting\n");
184 isDumpingFrames = TRUE;
185 ENTER_GL();
186 glClear(GL_COLOR_BUFFER_BIT);
187 LEAVE_GL();
188#endif
189
190#if defined(SINGLE_FRAME_DEBUGGING)
191 } else {
192#if defined(SHOW_FRAME_MAKEUP)
193 FIXME("Singe Frame snapshots Finishing\n");
194 isDumpingFrames = FALSE;
195#endif
196 FIXME("Singe Frame trace complete\n");
197 DeleteFileA("C:\\D3DTRACE");
198 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
199#endif
200 }
201 } else {
202 if (isOn) {
203 isOn = FALSE;
204#if defined(SHOW_FRAME_MAKEUP)
205 FIXME("Single Frame snapshots Finishing\n");
206 isDumpingFrames = FALSE;
207#endif
208 FIXME("Disabling D3D Trace\n");
209 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
210 }
211 }
212}
213#endif
214
215 /* This is disabled, but the code left in for debug purposes.
216 *
217 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
218 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
219 * The Debug runtime does the same on Windows. However, a few games do not redraw the
220 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
221 *
222 * Tests show that the content of the back buffer after a discard flip is indeed not
223 * reliable, so no game can depend on the exact content. However, it resembles the
224 * old contents in some way, for example by showing fragments at other locations. In
225 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
226 * gets a dark background image. If we clear it with a bright ugly color, the game's
227 * bug shows up much more than it does on Windows, and the players see single pixels
228 * with wrong colors.
229 * (The Max Payne bug has been confirmed on Windows with the debug runtime)
230 */
231 if (FALSE && This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
232 TRACE("Clearing the color buffer with cyan color\n");
233
234 IWineD3DDevice_Clear((IWineD3DDevice*)This->wineD3DDevice, 0, NULL,
235 WINED3DCLEAR_TARGET, 0xff00ffff, 1.0, 0);
236 }
237
238 if(((IWineD3DSurfaceImpl *) This->frontBuffer)->Flags & SFLAG_INSYSMEM ||
239 ((IWineD3DSurfaceImpl *) This->backBuffer[0])->Flags & SFLAG_INSYSMEM ) {
240 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying */
241 IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
242 IWineD3DSurfaceImpl *back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
243
244 if(front->resource.size == back->resource.size) {
245 DWORD fbflags;
246 flip_surface(front, back);
247
248 /* Tell the front buffer surface that is has been modified. However,
249 * the other locations were preserved during that, so keep the flags.
250 * This serves to update the emulated overlay, if any
251 */
252 fbflags = front->Flags;
253 IWineD3DSurface_ModifyLocation(This->frontBuffer, SFLAG_INDRAWABLE, TRUE);
254 front->Flags = fbflags;
255 } else {
256 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) front, SFLAG_INDRAWABLE, TRUE);
257 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) back, SFLAG_INDRAWABLE, TRUE);
258 }
259 } else {
260 IWineD3DSurface_ModifyLocation(This->frontBuffer, SFLAG_INDRAWABLE, TRUE);
261 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
262 * and INTEXTURE copies can keep their old content if they have any defined content.
263 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
264 * the texture / sysmem copy needs to be reloaded from the drawable
265 */
266 if(This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP) {
267 IWineD3DSurface_ModifyLocation(This->backBuffer[0], SFLAG_INDRAWABLE, TRUE);
268 }
269 }
270
271 if (This->wineD3DDevice->stencilBufferTarget) {
272 if (This->presentParms.Flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
273 || ((IWineD3DSurfaceImpl *)This->wineD3DDevice->stencilBufferTarget)->Flags & SFLAG_DISCARD) {
274 surface_modify_ds_location(This->wineD3DDevice->stencilBufferTarget, SFLAG_DS_DISCARDED);
275 }
276 }
277
278 if(This->presentParms.PresentationInterval != WINED3DPRESENT_INTERVAL_IMMEDIATE && GL_SUPPORT(SGI_VIDEO_SYNC)) {
279 retval = GL_EXTCALL(glXGetVideoSyncSGI(&sync));
280 if(retval != 0) {
281 ERR("glXGetVideoSyncSGI failed(retval = %d\n", retval);
282 }
283
284 switch(This->presentParms.PresentationInterval) {
285 case WINED3DPRESENT_INTERVAL_DEFAULT:
286 case WINED3DPRESENT_INTERVAL_ONE:
287 if(sync <= This->vSyncCounter) {
288 retval = GL_EXTCALL(glXWaitVideoSyncSGI(1, 0, &This->vSyncCounter));
289 } else {
290 This->vSyncCounter = sync;
291 }
292 break;
293 case WINED3DPRESENT_INTERVAL_TWO:
294 if(sync <= This->vSyncCounter + 1) {
295 retval = GL_EXTCALL(glXWaitVideoSyncSGI(2, This->vSyncCounter & 0x1, &This->vSyncCounter));
296 } else {
297 This->vSyncCounter = sync;
298 }
299 break;
300 case WINED3DPRESENT_INTERVAL_THREE:
301 if(sync <= This->vSyncCounter + 2) {
302 retval = GL_EXTCALL(glXWaitVideoSyncSGI(3, This->vSyncCounter % 0x3, &This->vSyncCounter));
303 } else {
304 This->vSyncCounter = sync;
305 }
306 break;
307 case WINED3DPRESENT_INTERVAL_FOUR:
308 if(sync <= This->vSyncCounter + 3) {
309 retval = GL_EXTCALL(glXWaitVideoSyncSGI(4, This->vSyncCounter & 0x3, &This->vSyncCounter));
310 } else {
311 This->vSyncCounter = sync;
312 }
313 break;
314 default:
315 FIXME("Unknown presentation interval %08x\n", This->presentParms.PresentationInterval);
316 }
317 }
318
319 TRACE("returning\n");
320 return WINED3D_OK;
321}
322
323static HRESULT WINAPI IWineD3DSwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain *iface, HWND window) {
324 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
325 WINED3DLOCKED_RECT r;
326 BYTE *mem;
327
328 if(window == This->win_handle) return WINED3D_OK;
329
330 TRACE("Performing dest override of swapchain %p from window %p to %p\n", This, This->win_handle, window);
331 if(This->context[0] == This->wineD3DDevice->contexts[0]) {
332 /* The primary context 'owns' all the opengl resources. Destroying and recreating that context requires downloading
333 * all opengl resources, deleting the gl resources, destroying all other contexts, then recreating all other contexts
334 * and reload the resources
335 */
336 delete_opengl_contexts((IWineD3DDevice *) This->wineD3DDevice, iface);
337 This->win_handle = window;
338 create_primary_opengl_context((IWineD3DDevice *) This->wineD3DDevice, iface);
339 } else {
340 This->win_handle = window;
341
342 /* The old back buffer has to be copied over to the new back buffer. A lockrect - switchcontext - unlockrect
343 * would suffice in theory, but it is rather nasty and may cause troubles with future changes of the locking code
344 * So lock read only, copy the surface out, then lock with the discard flag and write back
345 */
346 IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_READONLY);
347 mem = HeapAlloc(GetProcessHeap(), 0, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
348 memcpy(mem, r.pBits, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
349 IWineD3DSurface_UnlockRect(This->backBuffer[0]);
350
351 DestroyContext(This->wineD3DDevice, This->context[0]);
352 This->context[0] = CreateContext(This->wineD3DDevice, (IWineD3DSurfaceImpl *) This->frontBuffer, This->win_handle, FALSE /* pbuffer */, &This->presentParms);
353
354 IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_DISCARD);
355 memcpy(r.pBits, mem, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
356 HeapFree(GetProcessHeap(), 0, mem);
357 IWineD3DSurface_UnlockRect(This->backBuffer[0]);
358 }
359 return WINED3D_OK;
360}
361
362const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
363{
364 /* IUnknown */
365 IWineD3DBaseSwapChainImpl_QueryInterface,
366 IWineD3DBaseSwapChainImpl_AddRef,
367 IWineD3DBaseSwapChainImpl_Release,
368 /* IWineD3DSwapChain */
369 IWineD3DBaseSwapChainImpl_GetParent,
370 IWineD3DSwapChainImpl_Destroy,
371 IWineD3DBaseSwapChainImpl_GetDevice,
372 IWineD3DSwapChainImpl_Present,
373 IWineD3DSwapChainImpl_SetDestWindowOverride,
374 IWineD3DBaseSwapChainImpl_GetFrontBufferData,
375 IWineD3DBaseSwapChainImpl_GetBackBuffer,
376 IWineD3DBaseSwapChainImpl_GetRasterStatus,
377 IWineD3DBaseSwapChainImpl_GetDisplayMode,
378 IWineD3DBaseSwapChainImpl_GetPresentParameters,
379 IWineD3DBaseSwapChainImpl_SetGammaRamp,
380 IWineD3DBaseSwapChainImpl_GetGammaRamp
381};
382
383WineD3DContext *IWineD3DSwapChainImpl_CreateContextForThread(IWineD3DSwapChain *iface) {
384 WineD3DContext *ctx;
385 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
386 WineD3DContext **newArray;
387
388 TRACE("Creating a new context for swapchain %p, thread %d\n", This, GetCurrentThreadId());
389
390 ctx = CreateContext(This->wineD3DDevice, (IWineD3DSurfaceImpl *) This->frontBuffer,
391 This->context[0]->win_handle, FALSE /* pbuffer */, &This->presentParms);
392 if(!ctx) {
393 ERR("Failed to create a new context for the swapchain\n");
394 return NULL;
395 }
396
397 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * This->num_contexts + 1);
398 if(!newArray) {
399 ERR("Out of memory when trying to allocate a new context array\n");
400 DestroyContext(This->wineD3DDevice, ctx);
401 return NULL;
402 }
403 memcpy(newArray, This->context, sizeof(*newArray) * This->num_contexts);
404 HeapFree(GetProcessHeap(), 0, This->context);
405 newArray[This->num_contexts] = ctx;
406 This->context = newArray;
407 This->num_contexts++;
408
409 TRACE("Returning context %p\n", ctx);
410 return ctx;
411}
412
413void get_drawable_size_swapchain(IWineD3DSurfaceImpl *This, UINT *width, UINT *height) {
414 /* The drawable size of an onscreen drawable is the surface size.
415 * (Actually: The window size, but the surface is created in window size)
416 */
417 *width = This->currentDesc.Width;
418 *height = This->currentDesc.Height;
419}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette