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