wined3d: Add a Geforce9 driver version (right now garbage is returned).
[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 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 /* IDirect3DSwapChain IUnknown parts follow: */
39 static ULONG WINAPI IWineD3DSwapChainImpl_AddRef(IWineD3DSwapChain *iface) {
40     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
41     DWORD refCount = InterlockedIncrement(&This->ref);
42     TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
43     return refCount;
44 }
45
46 static HRESULT WINAPI IWineD3DSwapChainImpl_QueryInterface(IWineD3DSwapChain *iface, REFIID riid, LPVOID *ppobj)
47 {
48     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
49     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
50     if (IsEqualGUID(riid, &IID_IUnknown)
51         || IsEqualGUID(riid, &IID_IWineD3DBase)
52         || IsEqualGUID(riid, &IID_IWineD3DSwapChain)){
53         IWineD3DSwapChainImpl_AddRef(iface);
54         if(ppobj == NULL){
55             ERR("Query interface called but now data allocated\n");
56             return E_NOINTERFACE;
57         }
58         *ppobj = This;
59         return WINED3D_OK;
60     }
61     *ppobj = NULL;
62     return E_NOINTERFACE;
63 }
64
65
66 static ULONG WINAPI IWineD3DSwapChainImpl_Release(IWineD3DSwapChain *iface) {
67     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
68     DWORD refCount;
69     refCount = InterlockedDecrement(&This->ref);
70     TRACE("(%p) : ReleaseRef to %d\n", This, refCount);
71     if (refCount == 0) {
72         IWineD3DSwapChain_Destroy(iface, D3DCB_DefaultDestroySurface);
73     }
74     return refCount;
75 }
76
77 static HRESULT WINAPI IWineD3DSwapChainImpl_GetParent(IWineD3DSwapChain *iface, IUnknown ** ppParent){
78     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
79     *ppParent = This->parent;
80     IUnknown_AddRef(*ppParent);
81     TRACE("(%p) returning %p\n", This , *ppParent);
82     return WINED3D_OK;
83 }
84
85 /*IWineD3DSwapChain parts follow: */
86 static void WINAPI IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroyRenderTarget) {
87     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
88     WINED3DDISPLAYMODE mode;
89     int i;
90
91     /* release the ref to the front and back buffer parents */
92     if(This->frontBuffer) {
93         IWineD3DSurface_SetContainer(This->frontBuffer, 0);
94         if(D3DCB_DestroyRenderTarget(This->frontBuffer) > 0) {
95             FIXME("(%p) Something's still holding the front buffer\n",This);
96         }
97     }
98
99     if(This->backBuffer) {
100         int i;
101         for(i = 0; i < This->presentParms.BackBufferCount; i++) {
102             IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
103             if(D3DCB_DestroyRenderTarget(This->backBuffer[i]) > 0) {
104                 FIXME("(%p) Something's still holding the back buffer\n",This);
105             }
106         }
107         HeapFree(GetProcessHeap(), 0, This->backBuffer);
108     }
109
110     for(i = 0; i < This->num_contexts; i++) {
111         DestroyContext(This->wineD3DDevice, This->context[i]);
112     }
113     /* Restore the screen resolution if we rendered in fullscreen
114      * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
115      * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
116      * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
117      */
118     if(This->presentParms.Windowed == FALSE) {
119         mode.Width = This->orig_width;
120         mode.Height = This->orig_height;
121         mode.RefreshRate = 0;
122         mode.Format = This->orig_fmt;
123         IWineD3DDevice_SetDisplayMode((IWineD3DDevice *) This->wineD3DDevice, 0, &mode);
124     }
125     HeapFree(GetProcessHeap(), 0, This->context);
126
127     HeapFree(GetProcessHeap(), 0, This);
128 }
129
130 static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
131     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
132     unsigned int sync;
133     int retval;
134
135
136     ActivateContext(This->wineD3DDevice, This->backBuffer[0], CTXUSAGE_RESOURCELOAD);
137
138     /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
139     if(This->wineD3DDevice->bCursorVisible && This->wineD3DDevice->cursorTexture) {
140         IWineD3DSurfaceImpl cursor;
141         RECT destRect = {This->wineD3DDevice->xScreenSpace - This->wineD3DDevice->xHotSpot,
142                          This->wineD3DDevice->yScreenSpace - This->wineD3DDevice->yHotSpot,
143                          This->wineD3DDevice->xScreenSpace + This->wineD3DDevice->cursorWidth - This->wineD3DDevice->xHotSpot,
144                          This->wineD3DDevice->yScreenSpace + This->wineD3DDevice->cursorHeight - This->wineD3DDevice->yHotSpot};
145         TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
146         /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
147          * the application because we are only supposed to copy the information out. Using a fake surface
148          * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
149          */
150         memset(&cursor, 0, sizeof(cursor));
151         cursor.lpVtbl = &IWineD3DSurface_Vtbl;
152         cursor.resource.ref = 1;
153         cursor.resource.wineD3DDevice = This->wineD3DDevice;
154         cursor.resource.pool = WINED3DPOOL_SCRATCH;
155         cursor.resource.format = WINED3DFMT_A8R8G8B8;
156         cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
157         cursor.glDescription.textureName = This->wineD3DDevice->cursorTexture;
158         cursor.glDescription.target = GL_TEXTURE_2D;
159         cursor.glDescription.level = 0;
160         cursor.currentDesc.Width = This->wineD3DDevice->cursorWidth;
161         cursor.currentDesc.Height = This->wineD3DDevice->cursorHeight;
162         cursor.glRect.left = 0;
163         cursor.glRect.top = 0;
164         cursor.glRect.right = cursor.currentDesc.Width;
165         cursor.glRect.bottom = cursor.currentDesc.Height;
166         /* The cursor must have pow2 sizes */
167         cursor.pow2Width = cursor.currentDesc.Width;
168         cursor.pow2Height = cursor.currentDesc.Height;
169         /* The surface is in the texture */
170         cursor.Flags |= SFLAG_INTEXTURE;
171         /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
172          * which is exactly what we want :-)
173          */
174         if (This->presentParms.Windowed) {
175             MapWindowPoints(NULL, This->win_handle, (LPPOINT)&destRect, 2);
176         }
177         IWineD3DSurface_Blt(This->backBuffer[0], &destRect, (IWineD3DSurface *) &cursor, NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_NONE);
178     }
179     if(This->wineD3DDevice->logo_surface) {
180         /* Blit the logo into the upper left corner of the drawable */
181         IWineD3DSurface_BltFast(This->backBuffer[0], 0, 0, This->wineD3DDevice->logo_surface, NULL, WINEDDBLTFAST_SRCCOLORKEY);
182     }
183
184     if (pSourceRect || pDestRect) FIXME("Unhandled present options %p/%p\n", pSourceRect, pDestRect);
185     /* TODO: If only source rect or dest rect are supplied then clip the window to match */
186     TRACE("presetting HDC %p\n", This->context[0]->hdc);
187
188     /* Don't call checkGLcall, as glGetError is not applicable here */
189     if (hDestWindowOverride && This->win_handle != hDestWindowOverride) {
190         WINED3DLOCKED_RECT r;
191         BYTE *mem;
192
193         TRACE("Performing dest override of swapchain %p from window %p to %p\n", This, This->win_handle, hDestWindowOverride);
194         if(This->context[0] == This->wineD3DDevice->contexts[0]) {
195             /* The primary context 'owns' all the opengl resources. Destroying and recreating that context would require downloading
196              * all opengl resources, deleting the gl resources, destroying all other contexts, then recreating all other contexts
197              * and reload the resources
198              */
199             ERR("Cannot change the destination window of the owner of the primary context\n");
200         } else {
201             This->win_handle             = hDestWindowOverride;
202
203             /* The old back buffer has to be copied over to the new back buffer. A lockrect - switchcontext - unlockrect
204              * would suffice in theory, but it is rather nasty and may cause troubles with future changes of the locking code
205              * So lock read only, copy the surface out, then lock with the discard flag and write back
206              */
207             IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_READONLY);
208             mem = HeapAlloc(GetProcessHeap(), 0, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
209             memcpy(mem, r.pBits, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
210             IWineD3DSurface_UnlockRect(This->backBuffer[0]);
211
212             DestroyContext(This->wineD3DDevice, This->context[0]);
213             This->context[0] = CreateContext(This->wineD3DDevice, (IWineD3DSurfaceImpl *) This->frontBuffer, This->win_handle, FALSE /* pbuffer */, &This->presentParms);
214
215             IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_DISCARD);
216             memcpy(r.pBits, mem, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
217             HeapFree(GetProcessHeap(), 0, mem);
218             IWineD3DSurface_UnlockRect(This->backBuffer[0]);
219         }
220     }
221
222     SwapBuffers(This->context[0]->hdc); /* TODO: cycle through the swapchain buffers */
223
224     TRACE("SwapBuffers called, Starting new frame\n");
225     /* FPS support */
226     if (TRACE_ON(fps))
227     {
228         DWORD time = GetTickCount();
229         This->frames++;
230         /* every 1.5 seconds */
231         if (time - This->prev_time > 1500) {
232             TRACE_(fps)("%p @ approx %.2ffps\n", This, 1000.0*This->frames/(time - This->prev_time));
233             This->prev_time = time;
234             This->frames = 0;
235         }
236     }
237
238 #if defined(FRAME_DEBUGGING)
239 {
240     if (GetFileAttributesA("C:\\D3DTRACE") != INVALID_FILE_ATTRIBUTES) {
241         if (!isOn) {
242             isOn = TRUE;
243             FIXME("Enabling D3D Trace\n");
244             __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 1);
245 #if defined(SHOW_FRAME_MAKEUP)
246             FIXME("Singe Frame snapshots Starting\n");
247             isDumpingFrames = TRUE;
248             ENTER_GL();
249             glClear(GL_COLOR_BUFFER_BIT);
250             LEAVE_GL();
251 #endif
252
253 #if defined(SINGLE_FRAME_DEBUGGING)
254         } else {
255 #if defined(SHOW_FRAME_MAKEUP)
256             FIXME("Singe Frame snapshots Finishing\n");
257             isDumpingFrames = FALSE;
258 #endif
259             FIXME("Singe Frame trace complete\n");
260             DeleteFileA("C:\\D3DTRACE");
261             __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
262 #endif
263         }
264     } else {
265         if (isOn) {
266             isOn = FALSE;
267 #if defined(SHOW_FRAME_MAKEUP)
268             FIXME("Single Frame snapshots Finishing\n");
269             isDumpingFrames = FALSE;
270 #endif
271             FIXME("Disabling D3D Trace\n");
272             __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
273         }
274     }
275 }
276 #endif
277
278     /* This is disabled, but the code left in for debug purposes.
279      *
280      * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
281      * we can clear it with some ugly color to make bad drawing visible and ease debugging.
282      * The Debug runtime does the same on Windows. However, a few games do not redraw the
283      * screen properly, like Max Payne 2, which leaves a few pixels undefined.
284      *
285      * Tests show that the content of the back buffer after a discard flip is indeed not
286      * reliable, so no game can depend on the exact content. However, it resembles the
287      * old contents in some way, for example by showing fragments at other locations. In
288      * general, the color theme is still intact. So Max payne, which draws rather dark scenes
289      * gets a dark background image. If we clear it with a bright ugly color, the game's
290      * bug shows up much more than it does on Windows, and the players see single pixels
291      * with wrong colors.
292      * (The Max Payne bug has been confirmed on Windows with the debug runtime)
293      */
294     if (FALSE && This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
295         TRACE("Clearing the color buffer with cyan color\n");
296
297         IWineD3DDevice_Clear((IWineD3DDevice*)This->wineD3DDevice, 0, NULL,
298                               WINED3DCLEAR_TARGET, 0xff00ffff, 1.0, 0);
299     }
300
301     if(((IWineD3DSurfaceImpl *) This->frontBuffer)->Flags   & SFLAG_INSYSMEM ||
302        ((IWineD3DSurfaceImpl *) This->backBuffer[0])->Flags & SFLAG_INSYSMEM ) {
303         /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying */
304         IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
305         IWineD3DSurfaceImpl *back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
306         BOOL frontuptodate = front->Flags & SFLAG_INSYSMEM;
307         BOOL backuptodate = back->Flags & SFLAG_INSYSMEM;
308
309         if(front->resource.size == back->resource.size) {
310             /* Flip the DC */
311             {
312                 HDC tmp;
313                 tmp = front->hDC;
314                 front->hDC = back->hDC;
315                 back->hDC = tmp;
316             }
317
318             /* Flip the DIBsection */
319             {
320                 HBITMAP tmp;
321                 BOOL hasDib = front->Flags & SFLAG_DIBSECTION;
322                 tmp = front->dib.DIBsection;
323                 front->dib.DIBsection = back->dib.DIBsection;
324                 back->dib.DIBsection = tmp;
325
326                 if(back->Flags & SFLAG_DIBSECTION) front->Flags |= SFLAG_DIBSECTION;
327                 else front->Flags &= ~SFLAG_DIBSECTION;
328                 if(hasDib) back->Flags |= SFLAG_DIBSECTION;
329                 else back->Flags &= ~SFLAG_DIBSECTION;
330             }
331
332             /* Flip the surface data */
333             {
334                 void* tmp;
335
336                 tmp = front->dib.bitmap_data;
337                 front->dib.bitmap_data = back->dib.bitmap_data;
338                 back->dib.bitmap_data = tmp;
339
340                 tmp = front->resource.allocatedMemory;
341                 front->resource.allocatedMemory = back->resource.allocatedMemory;
342                 back->resource.allocatedMemory = tmp;
343
344                 tmp = front->resource.heapMemory;
345                 front->resource.heapMemory = back->resource.heapMemory;
346                 back->resource.heapMemory = tmp;
347             }
348
349             /* Flip the PBO */
350             {
351                 DWORD tmp_flags = front->Flags;
352
353                 GLuint tmp_pbo = front->pbo;
354                 front->pbo = back->pbo;
355                 back->pbo = tmp_pbo;
356
357                 if(back->Flags & SFLAG_PBO)
358                     front->Flags |= SFLAG_PBO;
359                 else
360                     front->Flags &= ~SFLAG_PBO;
361
362                 if(tmp_flags & SFLAG_PBO)
363                     back->Flags |= SFLAG_PBO;
364                 else
365                     back->Flags &= ~SFLAG_PBO;
366             }
367
368             /* client_memory should not be different, but just in case */
369             {
370                 BOOL tmp;
371                 tmp = front->dib.client_memory;
372                 front->dib.client_memory = back->dib.client_memory;
373                 back->dib.client_memory = tmp;
374             }
375             if(frontuptodate) back->Flags |= SFLAG_INSYSMEM;
376             else back->Flags &= ~SFLAG_INSYSMEM;
377             if(backuptodate) front->Flags |= SFLAG_INSYSMEM;
378             else front->Flags &= ~SFLAG_INSYSMEM;
379         } else {
380             IWineD3DSurface_ModifyLocation((IWineD3DSurface *) front, SFLAG_INDRAWABLE, TRUE);
381             IWineD3DSurface_ModifyLocation((IWineD3DSurface *) back, SFLAG_INDRAWABLE, TRUE);
382         }
383     }
384
385     if(This->presentParms.PresentationInterval != WINED3DPRESENT_INTERVAL_IMMEDIATE && GL_SUPPORT(SGI_VIDEO_SYNC)) {
386         retval = GL_EXTCALL(glXGetVideoSyncSGI(&sync));
387         if(retval != 0) {
388             ERR("glXGetVideoSyncSGI failed(retval = %d\n", retval);
389         }
390
391         switch(This->presentParms.PresentationInterval) {
392             case WINED3DPRESENT_INTERVAL_DEFAULT:
393             case WINED3DPRESENT_INTERVAL_ONE:
394                 if(sync <= This->vSyncCounter) {
395                     retval = GL_EXTCALL(glXWaitVideoSyncSGI(1, 0, &This->vSyncCounter));
396                 } else {
397                     This->vSyncCounter = sync;
398                 }
399                 break;
400             case WINED3DPRESENT_INTERVAL_TWO:
401                 if(sync <= This->vSyncCounter + 1) {
402                     retval = GL_EXTCALL(glXWaitVideoSyncSGI(2, This->vSyncCounter & 0x1, &This->vSyncCounter));
403                 } else {
404                     This->vSyncCounter = sync;
405                 }
406                 break;
407             case WINED3DPRESENT_INTERVAL_THREE:
408                 if(sync <= This->vSyncCounter + 2) {
409                     retval = GL_EXTCALL(glXWaitVideoSyncSGI(3, This->vSyncCounter % 0x3, &This->vSyncCounter));
410                 } else {
411                     This->vSyncCounter = sync;
412                 }
413                 break;
414             case WINED3DPRESENT_INTERVAL_FOUR:
415                 if(sync <= This->vSyncCounter + 3) {
416                     retval = GL_EXTCALL(glXWaitVideoSyncSGI(4, This->vSyncCounter & 0x3, &This->vSyncCounter));
417                 } else {
418                     This->vSyncCounter = sync;
419                 }
420                 break;
421             default:
422                 FIXME("Unknown presentation interval %08x\n", This->presentParms.PresentationInterval);
423         }
424     }
425
426     TRACE("returning\n");
427     return WINED3D_OK;
428 }
429
430 static HRESULT WINAPI IWineD3DSwapChainImpl_GetFrontBufferData(IWineD3DSwapChain *iface, IWineD3DSurface *pDestSurface) {
431     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
432     POINT start;
433
434     TRACE("(%p) : iface(%p) pDestSurface(%p)\n", This, iface, pDestSurface);
435
436     start.x = 0;
437     start.y = 0;
438
439     if (This->presentParms.Windowed) {
440         MapWindowPoints(This->win_handle, NULL, &start, 1);
441     }
442
443     IWineD3DSurface_BltFast(pDestSurface, start.x, start.y, This->frontBuffer, NULL, 0);
444     return WINED3D_OK;
445 }
446
447 static HRESULT WINAPI IWineD3DSwapChainImpl_GetBackBuffer(IWineD3DSwapChain *iface, UINT iBackBuffer, WINED3DBACKBUFFER_TYPE Type, IWineD3DSurface **ppBackBuffer) {
448
449     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
450
451     if (iBackBuffer > This->presentParms.BackBufferCount - 1) {
452         TRACE("Back buffer count out of range\n");
453         /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it here
454          * in wined3d to avoid problems in other libs
455          */
456         *ppBackBuffer = NULL;
457         return WINED3DERR_INVALIDCALL;
458     }
459
460     *ppBackBuffer = This->backBuffer[iBackBuffer];
461     TRACE("(%p) : BackBuf %d Type %d  returning %p\n", This, iBackBuffer, Type, *ppBackBuffer);
462
463     /* Note inc ref on returned surface */
464     if(*ppBackBuffer) IWineD3DSurface_AddRef(*ppBackBuffer);
465     return WINED3D_OK;
466
467 }
468
469 static HRESULT WINAPI IWineD3DSwapChainImpl_GetRasterStatus(IWineD3DSwapChain *iface, WINED3DRASTER_STATUS *pRasterStatus) {
470     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
471     static BOOL showFixmes = TRUE;
472     pRasterStatus->InVBlank = TRUE;
473     pRasterStatus->ScanLine = 0;
474     /* No openGL equivalent */
475     if(showFixmes) {
476         FIXME("(%p) : stub (once)\n", This);
477         showFixmes = FALSE;
478     }
479     return WINED3D_OK;
480 }
481
482 static HRESULT WINAPI IWineD3DSwapChainImpl_GetDisplayMode(IWineD3DSwapChain *iface, WINED3DDISPLAYMODE*pMode) {
483     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
484     HRESULT hr;
485
486     TRACE("(%p)->(%p): Calling GetAdapterDisplayMode\n", This, pMode);
487     hr = IWineD3D_GetAdapterDisplayMode(This->wineD3DDevice->wineD3D, This->wineD3DDevice->adapter->num, pMode);
488
489     TRACE("(%p) : returning w(%d) h(%d) rr(%d) fmt(%u,%s)\n", This, pMode->Width, pMode->Height, pMode->RefreshRate,
490     pMode->Format, debug_d3dformat(pMode->Format));
491     return hr;
492 }
493
494 static HRESULT WINAPI IWineD3DSwapChainImpl_GetDevice(IWineD3DSwapChain *iface, IWineD3DDevice**ppDevice) {
495     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
496
497     *ppDevice = (IWineD3DDevice *) This->wineD3DDevice;
498
499     /* Note  Calling this method will increase the internal reference count
500        on the IDirect3DDevice9 interface. */
501     IWineD3DDevice_AddRef(*ppDevice);
502     TRACE("(%p) : returning %p\n", This, *ppDevice);
503     return WINED3D_OK;
504 }
505
506 static HRESULT WINAPI IWineD3DSwapChainImpl_GetPresentParameters(IWineD3DSwapChain *iface, WINED3DPRESENT_PARAMETERS *pPresentationParameters) {
507     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
508     TRACE("(%p)\n", This);
509
510     *pPresentationParameters = This->presentParms;
511
512     return WINED3D_OK;
513 }
514
515 static HRESULT WINAPI IWineD3DSwapChainImpl_SetGammaRamp(IWineD3DSwapChain *iface, DWORD Flags, CONST WINED3DGAMMARAMP *pRamp){
516
517     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
518     HDC hDC;
519     TRACE("(%p) : pRamp@%p flags(%d)\n", This, pRamp, Flags);
520     hDC = GetDC(This->win_handle);
521     SetDeviceGammaRamp(hDC, (LPVOID)pRamp);
522     ReleaseDC(This->win_handle, hDC);
523     return WINED3D_OK;
524
525 }
526
527 static HRESULT WINAPI IWineD3DSwapChainImpl_GetGammaRamp(IWineD3DSwapChain *iface, WINED3DGAMMARAMP *pRamp){
528
529     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
530     HDC hDC;
531     TRACE("(%p) : pRamp@%p\n", This, pRamp);
532     hDC = GetDC(This->win_handle);
533     GetDeviceGammaRamp(hDC, pRamp);
534     ReleaseDC(This->win_handle, hDC);
535     return WINED3D_OK;
536
537 }
538
539
540 const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
541 {
542     /* IUnknown */
543     IWineD3DSwapChainImpl_QueryInterface,
544     IWineD3DSwapChainImpl_AddRef,
545     IWineD3DSwapChainImpl_Release,
546     /* IWineD3DSwapChain */
547     IWineD3DSwapChainImpl_GetParent,
548     IWineD3DSwapChainImpl_Destroy,
549     IWineD3DSwapChainImpl_GetDevice,
550     IWineD3DSwapChainImpl_Present,
551     IWineD3DSwapChainImpl_GetFrontBufferData,
552     IWineD3DSwapChainImpl_GetBackBuffer,
553     IWineD3DSwapChainImpl_GetRasterStatus,
554     IWineD3DSwapChainImpl_GetDisplayMode,
555     IWineD3DSwapChainImpl_GetPresentParameters,
556     IWineD3DSwapChainImpl_SetGammaRamp,
557     IWineD3DSwapChainImpl_GetGammaRamp
558 };
559
560 WineD3DContext *IWineD3DSwapChainImpl_CreateContextForThread(IWineD3DSwapChain *iface) {
561     WineD3DContext *ctx;
562     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
563     WineD3DContext **newArray;
564
565     TRACE("Creating a new context for swapchain %p, thread %d\n", This, GetCurrentThreadId());
566
567     ctx = CreateContext(This->wineD3DDevice, (IWineD3DSurfaceImpl *) This->frontBuffer,
568                         This->context[0]->win_handle, FALSE /* pbuffer */, &This->presentParms);
569     if(!ctx) {
570         ERR("Failed to create a new context for the swapchain\n");
571         return NULL;
572     }
573
574     newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * This->num_contexts + 1);
575     if(!newArray) {
576         ERR("Out of memory when trying to allocate a new context array\n");
577         DestroyContext(This->wineD3DDevice, ctx);
578         return NULL;
579     }
580     memcpy(newArray, This->context, sizeof(*newArray) * This->num_contexts);
581     HeapFree(GetProcessHeap(), 0, This->context);
582     newArray[This->num_contexts] = ctx;
583     This->context = newArray;
584     This->num_contexts++;
585
586     TRACE("Returning context %p\n", ctx);
587     return ctx;
588 }
589
590 void get_drawable_size_swapchain(IWineD3DSurfaceImpl *This, UINT *width, UINT *height) {
591     /* The drawable size of an onscreen drawable is the surface size.
592      * (Actually: The window size, but the surface is created in window size)
593      */
594     *width = This->currentDesc.Width;
595     *height = This->currentDesc.Height;
596 }