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