user32: Reimplement UserYield using PeekMessageW.
[wine/multimedia.git] / dlls / wined3d / swapchain.c
bloba23166a22197c3a1af25f3c2a4853afd7d4e49fb
1 /*
2 *IDirect3DSwapChain9 implementation
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
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.
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.
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
24 #include "config.h"
25 #include "wined3d_private.h"
28 /*TODO: some of the additional parameters may be required to
29 set the gamma ramp (for some weird reason microsoft have left swap gammaramp in device
30 but it operates on a swapchain, it may be a good idea to move it to IWineD3DSwapChain for IWineD3D)*/
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
34 WINE_DECLARE_DEBUG_CHANNEL(fps);
36 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
38 /*IWineD3DSwapChain parts follow: */
39 static void WINAPI IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain *iface)
41 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
42 WINED3DDISPLAYMODE mode;
43 unsigned int i;
45 TRACE("Destroying swapchain %p\n", iface);
47 IWineD3DSwapChain_SetGammaRamp(iface, 0, &This->orig_gamma);
49 /* Release the swapchain's draw buffers. Make sure This->backBuffer[0] is
50 * the last buffer to be destroyed, FindContext() depends on that. */
51 if (This->frontBuffer)
53 IWineD3DSurface_SetContainer(This->frontBuffer, 0);
54 if (IWineD3DSurface_Release(This->frontBuffer))
56 WARN("(%p) Something's still holding the front buffer (%p).\n",
57 This, This->frontBuffer);
59 This->frontBuffer = NULL;
62 if (This->backBuffer)
64 UINT i = This->presentParms.BackBufferCount;
66 while (i--)
68 IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
69 if (IWineD3DSurface_Release(This->backBuffer[i]))
70 WARN("(%p) Something's still holding back buffer %u (%p).\n",
71 This, i, This->backBuffer[i]);
73 HeapFree(GetProcessHeap(), 0, This->backBuffer);
74 This->backBuffer = NULL;
77 for (i = 0; i < This->num_contexts; ++i)
79 context_destroy(This->wineD3DDevice, This->context[i]);
81 /* Restore the screen resolution if we rendered in fullscreen
82 * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
83 * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
84 * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
86 if(This->presentParms.Windowed == FALSE && This->presentParms.AutoRestoreDisplayMode) {
87 mode.Width = This->orig_width;
88 mode.Height = This->orig_height;
89 mode.RefreshRate = 0;
90 mode.Format = This->orig_fmt;
91 IWineD3DDevice_SetDisplayMode((IWineD3DDevice *) This->wineD3DDevice, 0, &mode);
93 HeapFree(GetProcessHeap(), 0, This->context);
95 HeapFree(GetProcessHeap(), 0, This);
98 static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
99 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
100 struct wined3d_context *context;
101 unsigned int sync;
102 int retval;
104 context = context_acquire(This->wineD3DDevice, This->backBuffer[0], CTXUSAGE_RESOURCELOAD);
106 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
107 if(This->wineD3DDevice->bCursorVisible && This->wineD3DDevice->cursorTexture) {
108 IWineD3DSurfaceImpl cursor;
109 RECT destRect = {This->wineD3DDevice->xScreenSpace - This->wineD3DDevice->xHotSpot,
110 This->wineD3DDevice->yScreenSpace - This->wineD3DDevice->yHotSpot,
111 This->wineD3DDevice->xScreenSpace + This->wineD3DDevice->cursorWidth - This->wineD3DDevice->xHotSpot,
112 This->wineD3DDevice->yScreenSpace + This->wineD3DDevice->cursorHeight - This->wineD3DDevice->yHotSpot};
113 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
114 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
115 * the application because we are only supposed to copy the information out. Using a fake surface
116 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
118 memset(&cursor, 0, sizeof(cursor));
119 cursor.lpVtbl = &IWineD3DSurface_Vtbl;
120 cursor.resource.ref = 1;
121 cursor.resource.wineD3DDevice = This->wineD3DDevice;
122 cursor.resource.pool = WINED3DPOOL_SCRATCH;
123 cursor.resource.format_desc =
124 getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, &This->wineD3DDevice->adapter->gl_info);
125 cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
126 cursor.texture_name = This->wineD3DDevice->cursorTexture;
127 cursor.texture_target = GL_TEXTURE_2D;
128 cursor.texture_level = 0;
129 cursor.currentDesc.Width = This->wineD3DDevice->cursorWidth;
130 cursor.currentDesc.Height = This->wineD3DDevice->cursorHeight;
131 cursor.glRect.left = 0;
132 cursor.glRect.top = 0;
133 cursor.glRect.right = cursor.currentDesc.Width;
134 cursor.glRect.bottom = cursor.currentDesc.Height;
135 /* The cursor must have pow2 sizes */
136 cursor.pow2Width = cursor.currentDesc.Width;
137 cursor.pow2Height = cursor.currentDesc.Height;
138 /* The surface is in the texture */
139 cursor.Flags |= SFLAG_INTEXTURE;
140 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
141 * which is exactly what we want :-)
143 if (This->presentParms.Windowed) {
144 MapWindowPoints(NULL, This->win_handle, (LPPOINT)&destRect, 2);
146 IWineD3DSurface_Blt(This->backBuffer[0], &destRect, (IWineD3DSurface *)&cursor,
147 NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_POINT);
149 if(This->wineD3DDevice->logo_surface) {
150 /* Blit the logo into the upper left corner of the drawable */
151 IWineD3DSurface_BltFast(This->backBuffer[0], 0, 0, This->wineD3DDevice->logo_surface, NULL, WINEDDBLTFAST_SRCCOLORKEY);
154 if (pSourceRect || pDestRect) FIXME("Unhandled present rects %s/%s\n", wine_dbgstr_rect(pSourceRect), wine_dbgstr_rect(pDestRect));
155 /* TODO: If only source rect or dest rect are supplied then clip the window to match */
156 TRACE("presetting HDC %p\n", This->context[0]->hdc);
158 /* Don't call checkGLcall, as glGetError is not applicable here */
159 if (hDestWindowOverride && This->win_handle != hDestWindowOverride) {
160 IWineD3DSwapChain_SetDestWindowOverride(iface, hDestWindowOverride);
163 SwapBuffers(This->context[0]->hdc); /* TODO: cycle through the swapchain buffers */
165 TRACE("SwapBuffers called, Starting new frame\n");
166 /* FPS support */
167 if (TRACE_ON(fps))
169 DWORD time = GetTickCount();
170 This->frames++;
171 /* every 1.5 seconds */
172 if (time - This->prev_time > 1500) {
173 TRACE_(fps)("%p @ approx %.2ffps\n", This, 1000.0*This->frames/(time - This->prev_time));
174 This->prev_time = time;
175 This->frames = 0;
179 #if defined(FRAME_DEBUGGING)
181 if (GetFileAttributesA("C:\\D3DTRACE") != INVALID_FILE_ATTRIBUTES) {
182 if (!isOn) {
183 isOn = TRUE;
184 FIXME("Enabling D3D Trace\n");
185 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 1);
186 #if defined(SHOW_FRAME_MAKEUP)
187 FIXME("Singe Frame snapshots Starting\n");
188 isDumpingFrames = TRUE;
189 ENTER_GL();
190 glClear(GL_COLOR_BUFFER_BIT);
191 LEAVE_GL();
192 #endif
194 #if defined(SINGLE_FRAME_DEBUGGING)
195 } else {
196 #if defined(SHOW_FRAME_MAKEUP)
197 FIXME("Singe Frame snapshots Finishing\n");
198 isDumpingFrames = FALSE;
199 #endif
200 FIXME("Singe Frame trace complete\n");
201 DeleteFileA("C:\\D3DTRACE");
202 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
203 #endif
205 } else {
206 if (isOn) {
207 isOn = FALSE;
208 #if defined(SHOW_FRAME_MAKEUP)
209 FIXME("Single Frame snapshots Finishing\n");
210 isDumpingFrames = FALSE;
211 #endif
212 FIXME("Disabling D3D Trace\n");
213 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
217 #endif
219 /* This is disabled, but the code left in for debug purposes.
221 * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
222 * we can clear it with some ugly color to make bad drawing visible and ease debugging.
223 * The Debug runtime does the same on Windows. However, a few games do not redraw the
224 * screen properly, like Max Payne 2, which leaves a few pixels undefined.
226 * Tests show that the content of the back buffer after a discard flip is indeed not
227 * reliable, so no game can depend on the exact content. However, it resembles the
228 * old contents in some way, for example by showing fragments at other locations. In
229 * general, the color theme is still intact. So Max payne, which draws rather dark scenes
230 * gets a dark background image. If we clear it with a bright ugly color, the game's
231 * bug shows up much more than it does on Windows, and the players see single pixels
232 * with wrong colors.
233 * (The Max Payne bug has been confirmed on Windows with the debug runtime)
235 if (FALSE && This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
236 TRACE("Clearing the color buffer with cyan color\n");
238 IWineD3DDevice_Clear((IWineD3DDevice*)This->wineD3DDevice, 0, NULL,
239 WINED3DCLEAR_TARGET, 0xff00ffff, 1.0f, 0);
242 if(((IWineD3DSurfaceImpl *) This->frontBuffer)->Flags & SFLAG_INSYSMEM ||
243 ((IWineD3DSurfaceImpl *) This->backBuffer[0])->Flags & SFLAG_INSYSMEM ) {
244 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying */
245 IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
246 IWineD3DSurfaceImpl *back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
248 if(front->resource.size == back->resource.size) {
249 DWORD fbflags;
250 flip_surface(front, back);
252 /* Tell the front buffer surface that is has been modified. However,
253 * the other locations were preserved during that, so keep the flags.
254 * This serves to update the emulated overlay, if any
256 fbflags = front->Flags;
257 IWineD3DSurface_ModifyLocation(This->frontBuffer, SFLAG_INDRAWABLE, TRUE);
258 front->Flags = fbflags;
259 } else {
260 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) front, SFLAG_INDRAWABLE, TRUE);
261 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) back, SFLAG_INDRAWABLE, TRUE);
263 } else {
264 IWineD3DSurface_ModifyLocation(This->frontBuffer, SFLAG_INDRAWABLE, TRUE);
265 /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
266 * and INTEXTURE copies can keep their old content if they have any defined content.
267 * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
268 * the texture / sysmem copy needs to be reloaded from the drawable
270 if(This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP) {
271 IWineD3DSurface_ModifyLocation(This->backBuffer[0], SFLAG_INDRAWABLE, TRUE);
275 if (This->wineD3DDevice->stencilBufferTarget) {
276 if (This->presentParms.Flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
277 || ((IWineD3DSurfaceImpl *)This->wineD3DDevice->stencilBufferTarget)->Flags & SFLAG_DISCARD) {
278 surface_modify_ds_location(This->wineD3DDevice->stencilBufferTarget, SFLAG_DS_DISCARDED);
282 if (This->presentParms.PresentationInterval != WINED3DPRESENT_INTERVAL_IMMEDIATE
283 && context->gl_info->supported[SGI_VIDEO_SYNC])
285 retval = GL_EXTCALL(glXGetVideoSyncSGI(&sync));
286 if(retval != 0) {
287 ERR("glXGetVideoSyncSGI failed(retval = %d\n", retval);
290 switch(This->presentParms.PresentationInterval) {
291 case WINED3DPRESENT_INTERVAL_DEFAULT:
292 case WINED3DPRESENT_INTERVAL_ONE:
293 if(sync <= This->vSyncCounter) {
294 retval = GL_EXTCALL(glXWaitVideoSyncSGI(1, 0, &This->vSyncCounter));
295 } else {
296 This->vSyncCounter = sync;
298 break;
299 case WINED3DPRESENT_INTERVAL_TWO:
300 if(sync <= This->vSyncCounter + 1) {
301 retval = GL_EXTCALL(glXWaitVideoSyncSGI(2, This->vSyncCounter & 0x1, &This->vSyncCounter));
302 } else {
303 This->vSyncCounter = sync;
305 break;
306 case WINED3DPRESENT_INTERVAL_THREE:
307 if(sync <= This->vSyncCounter + 2) {
308 retval = GL_EXTCALL(glXWaitVideoSyncSGI(3, This->vSyncCounter % 0x3, &This->vSyncCounter));
309 } else {
310 This->vSyncCounter = sync;
312 break;
313 case WINED3DPRESENT_INTERVAL_FOUR:
314 if(sync <= This->vSyncCounter + 3) {
315 retval = GL_EXTCALL(glXWaitVideoSyncSGI(4, This->vSyncCounter & 0x3, &This->vSyncCounter));
316 } else {
317 This->vSyncCounter = sync;
319 break;
320 default:
321 FIXME("Unknown presentation interval %08x\n", This->presentParms.PresentationInterval);
325 context_release(context);
327 TRACE("returning\n");
328 return WINED3D_OK;
331 static HRESULT WINAPI IWineD3DSwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain *iface, HWND window) {
332 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
333 WINED3DLOCKED_RECT r;
334 BYTE *mem;
336 if(window == This->win_handle) return WINED3D_OK;
338 TRACE("Performing dest override of swapchain %p from window %p to %p\n", This, This->win_handle, window);
339 if(This->context[0] == This->wineD3DDevice->contexts[0]) {
340 /* The primary context 'owns' all the opengl resources. Destroying and recreating that context requires downloading
341 * all opengl resources, deleting the gl resources, destroying all other contexts, then recreating all other contexts
342 * and reload the resources
344 delete_opengl_contexts((IWineD3DDevice *) This->wineD3DDevice, iface);
345 This->win_handle = window;
346 create_primary_opengl_context((IWineD3DDevice *) This->wineD3DDevice, iface);
347 } else {
348 This->win_handle = window;
350 /* The old back buffer has to be copied over to the new back buffer. A lockrect - switchcontext - unlockrect
351 * would suffice in theory, but it is rather nasty and may cause troubles with future changes of the locking code
352 * So lock read only, copy the surface out, then lock with the discard flag and write back
354 IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_READONLY);
355 mem = HeapAlloc(GetProcessHeap(), 0, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
356 memcpy(mem, r.pBits, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
357 IWineD3DSurface_UnlockRect(This->backBuffer[0]);
359 context_destroy(This->wineD3DDevice, This->context[0]);
360 This->context[0] = context_create(This->wineD3DDevice, (IWineD3DSurfaceImpl *)This->frontBuffer,
361 This->win_handle, FALSE /* pbuffer */, &This->presentParms);
362 context_release(This->context[0]);
364 IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_DISCARD);
365 memcpy(r.pBits, mem, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
366 HeapFree(GetProcessHeap(), 0, mem);
367 IWineD3DSurface_UnlockRect(This->backBuffer[0]);
369 return WINED3D_OK;
372 const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
374 /* IUnknown */
375 IWineD3DBaseSwapChainImpl_QueryInterface,
376 IWineD3DBaseSwapChainImpl_AddRef,
377 IWineD3DBaseSwapChainImpl_Release,
378 /* IWineD3DSwapChain */
379 IWineD3DBaseSwapChainImpl_GetParent,
380 IWineD3DSwapChainImpl_Destroy,
381 IWineD3DBaseSwapChainImpl_GetDevice,
382 IWineD3DSwapChainImpl_Present,
383 IWineD3DSwapChainImpl_SetDestWindowOverride,
384 IWineD3DBaseSwapChainImpl_GetFrontBufferData,
385 IWineD3DBaseSwapChainImpl_GetBackBuffer,
386 IWineD3DBaseSwapChainImpl_GetRasterStatus,
387 IWineD3DBaseSwapChainImpl_GetDisplayMode,
388 IWineD3DBaseSwapChainImpl_GetPresentParameters,
389 IWineD3DBaseSwapChainImpl_SetGammaRamp,
390 IWineD3DBaseSwapChainImpl_GetGammaRamp
393 struct wined3d_context *swapchain_create_context_for_thread(IWineD3DSwapChain *iface)
395 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
396 struct wined3d_context **newArray;
397 struct wined3d_context *ctx;
399 TRACE("Creating a new context for swapchain %p, thread %d\n", This, GetCurrentThreadId());
401 ctx = context_create(This->wineD3DDevice, (IWineD3DSurfaceImpl *) This->frontBuffer,
402 This->context[0]->win_handle, FALSE /* pbuffer */, &This->presentParms);
403 if (!ctx)
405 ERR("Failed to create a new context for the swapchain\n");
406 return NULL;
408 context_release(ctx);
410 newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * This->num_contexts + 1);
411 if(!newArray) {
412 ERR("Out of memory when trying to allocate a new context array\n");
413 context_destroy(This->wineD3DDevice, ctx);
414 return NULL;
416 memcpy(newArray, This->context, sizeof(*newArray) * This->num_contexts);
417 HeapFree(GetProcessHeap(), 0, This->context);
418 newArray[This->num_contexts] = ctx;
419 This->context = newArray;
420 This->num_contexts++;
422 TRACE("Returning context %p\n", ctx);
423 return ctx;
426 void get_drawable_size_swapchain(struct wined3d_context *context, UINT *width, UINT *height)
428 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)context->current_rt;
429 /* The drawable size of an onscreen drawable is the surface size.
430 * (Actually: The window size, but the surface is created in window size) */
431 *width = surface->currentDesc.Width;
432 *height = surface->currentDesc.Height;