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