wined3d: Rename CreateAdditionalSwapChain to CreateSwapChain.
[wine] / dlls / ddraw / surface.c
1 /* DirectDraw Surface Implementation
2  *
3  * Copyright (c) 1997-2000 Marcus Meissner
4  * Copyright (c) 1998-2000 Lionel Ulmer
5  * Copyright (c) 2000-2001 TransGaming Technologies Inc.
6  * Copyright (c) 2006 Stefan Dösinger
7  *
8  * This file contains the (internal) driver registration functions,
9  * driver enumeration APIs and DirectDraw creation functions.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include "config.h"
27 #include "wine/port.h"
28
29 #include <assert.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #define COBJMACROS
35 #define NONAMELESSUNION
36
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "wingdi.h"
41 #include "wine/exception.h"
42
43 #include "ddraw.h"
44 #include "d3d.h"
45
46 #include "ddraw_private.h"
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
50
51 /*****************************************************************************
52  * IUnknown parts follow
53  *****************************************************************************/
54
55 /*****************************************************************************
56  * IDirectDrawSurface7::QueryInterface
57  *
58  * A normal QueryInterface implementation. For QueryInterface rules
59  * see ddraw.c, IDirectDraw7::QueryInterface. This method
60  * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
61  * in all versions, the IDirectDrawGammaControl interface and it can
62  * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
63  *
64  * Params:
65  *  riid: The interface id queried for
66  *  obj: Address to write the pointer to
67  *
68  * Returns:
69  *  S_OK on success
70  *  E_NOINTERFACE if the requested interface wasn't found
71  *
72  *****************************************************************************/
73 static HRESULT WINAPI
74 IDirectDrawSurfaceImpl_QueryInterface(IDirectDrawSurface7 *iface,
75                                       REFIID riid,
76                                       void **obj)
77 {
78     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
79
80     /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
81     *obj = NULL;
82
83     if(!riid)
84         return DDERR_INVALIDPARAMS;
85
86     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),obj);
87     if (IsEqualGUID(riid, &IID_IUnknown)
88      || IsEqualGUID(riid, &IID_IDirectDrawSurface7)
89      || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
90     {
91         IUnknown_AddRef(iface);
92         *obj = ICOM_INTERFACE(This, IDirectDrawSurface7);
93         TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
94         return S_OK;
95     }
96     else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
97           || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
98           || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
99     {
100         IUnknown_AddRef(iface);
101         *obj = ICOM_INTERFACE(This, IDirectDrawSurface3);
102         TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
103         return S_OK;
104     }
105     else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
106     {
107         IUnknown_AddRef(iface);
108         *obj = ICOM_INTERFACE(This, IDirectDrawGammaControl);
109         TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
110         return S_OK;
111     }
112     else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
113              IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
114              IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
115     {
116         IDirect3DDevice7 *d3d;
117
118         /* Call into IDirect3D7 for creation */
119         IDirect3D7_CreateDevice(ICOM_INTERFACE(This->ddraw, IDirect3D7),
120                                 riid,
121                                 ICOM_INTERFACE(This, IDirectDrawSurface7),
122                                 &d3d);
123
124         *obj = COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice7, IDirect3DDevice, d3d);
125         TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
126
127         return S_OK;
128     }
129     else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
130              IsEqualGUID( &IID_IDirect3DTexture2, riid ))
131     {
132         if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
133         {
134             *obj = ICOM_INTERFACE(This, IDirect3DTexture);
135             TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
136         }
137         else
138         {
139             *obj = ICOM_INTERFACE(This, IDirect3DTexture2);
140             TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
141         }
142         IUnknown_AddRef( (IUnknown *) *obj);
143         return S_OK;
144     }
145
146     ERR("No interface\n");
147     return E_NOINTERFACE;
148 }
149
150 /*****************************************************************************
151  * IDirectDrawSurface7::AddRef
152  *
153  * A normal addref implementation
154  *
155  * Returns:
156  *  The new refcount
157  *
158  *****************************************************************************/
159 static ULONG WINAPI
160 IDirectDrawSurfaceImpl_AddRef(IDirectDrawSurface7 *iface)
161 {
162     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
163     ULONG refCount = InterlockedIncrement(&This->ref);
164
165     TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
166     return refCount;
167 }
168
169 /*****************************************************************************
170  * IDirectDrawSurfaceImpl_Destroy
171  *
172  * A helper function for IDirectDrawSurface7::Release
173  *
174  * Frees the surface, regardless of its refcount.
175  *  See IDirectDrawSurface7::Release for more information
176  *
177  * Params:
178  *  This: Surface to free
179  *
180  *****************************************************************************/
181 void IDirectDrawSurfaceImpl_Destroy(IDirectDrawSurfaceImpl *This)
182 {
183     TRACE("(%p)\n", This);
184
185     /* Check the refcount and give a warning */
186     if(This->ref > 1)
187     {
188         /* This can happen when a complex surface is destroyed,
189          * because the 2nd surface was addref()ed when the app
190          * called GetAttachedSurface
191          */
192         WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
193     }
194
195     /* Check for attached surfaces and detach them */
196     if(This->first_attached != This)
197     {
198         /* Well, this shouldn't happen: The surface being attached is addref()ed
199           * in AddAttachedSurface, so it shouldn't be released until DeleteAttachedSurface
200           * is called, because the refcount is held. It looks like the app released()
201           * it often enough to force this
202           */
203         IDirectDrawSurface7 *root = ICOM_INTERFACE(This->first_attached, IDirectDrawSurface7);
204         IDirectDrawSurface7 *detach = ICOM_INTERFACE(This, IDirectDrawSurface7);
205
206         FIXME("(%p) Freeing a surface that is attached to surface %p\n", This, This->first_attached);
207
208         /* The refcount will drop to -1 here */
209         if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
210         {
211             ERR("(%p) DeleteAttachedSurface failed!\n", This);
212         }
213     }
214
215     while(This->next_attached != NULL)
216     {
217         IDirectDrawSurface7 *root = ICOM_INTERFACE(This, IDirectDrawSurface7);
218         IDirectDrawSurface7 *detach = ICOM_INTERFACE(This->next_attached, IDirectDrawSurface7);
219
220         if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
221         {
222             ERR("(%p) DeleteAttachedSurface failed!\n", This);
223             assert(0);
224         }
225     }
226
227     /* Now destroy the surface. Wait: It could have been released if we are a texture */
228     if(This->WineD3DSurface)
229         IWineD3DSurface_Release(This->WineD3DSurface);
230
231     /* Having a texture handle set implies that the device still exists */
232     if(This->Handle)
233     {
234         This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
235         This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
236     }
237
238     /* Reduce the ddraw surface count */
239     InterlockedDecrement(&This->ddraw->surfaces);
240     list_remove(&This->surface_list_entry);
241
242     HeapFree(GetProcessHeap(), 0, This);
243 }
244
245 /*****************************************************************************
246  * IDirectDrawSurface7::Release
247  *
248  * Reduces the surface's refcount by 1. If the refcount falls to 0, the
249  * surface is destroyed.
250  *
251  * Destroying the surface is a bit tricky. For the connection between
252  * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
253  * It has a nice graph explaining the connection.
254  *
255  * What happens here is basically this:
256  * When a surface is destroyed, its WineD3DSurface is released,
257  * and the refcount of the DirectDraw interface is reduced by 1. If it has
258  * complex surfaces attached to it, then these surfaces are destroyed too,
259  * regardless of their refcount. If any surface being destroyed has another
260  * surface attached to it (with a "soft" attachment, not complex), then
261  * this surface is detached with DeleteAttachedSurface.
262  *
263  * When the surface is a texture, the WineD3DTexture is released.
264  * If the surface is the Direct3D render target, then the D3D
265  * capabilities of the WineD3DDevice are uninitialized, which causes the
266  * swapchain to be released.
267  *
268  * When a complex sublevel falls to ref zero, then this is ignored.
269  *
270  * Returns:
271  *  The new refcount
272  *
273  *****************************************************************************/
274 static ULONG WINAPI
275 IDirectDrawSurfaceImpl_Release(IDirectDrawSurface7 *iface)
276 {
277     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
278     ULONG ref;
279     TRACE("(%p) : Releasing from %d\n", This, This->ref);
280     ref = InterlockedDecrement(&This->ref);
281
282     if (ref == 0)
283     {
284
285         IDirectDrawSurfaceImpl *surf;
286         IDirectDrawImpl *ddraw;
287         IUnknown *ifaceToRelease = This->ifaceToRelease;
288         UINT i;
289
290         /* Complex attached surfaces are destroyed implicitly when the root is released */
291         EnterCriticalSection(&ddraw_cs);
292         if(!This->is_complex_root)
293         {
294             WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
295             LeaveCriticalSection(&ddraw_cs);
296             return ref;
297         }
298         ddraw = This->ddraw;
299
300         /* If it's a texture, destroy the WineD3DTexture.
301          * WineD3D will destroy the IParent interfaces
302          * of the sublevels, which destroys the WineD3DSurfaces.
303          * Set the surfaces to NULL to avoid destroying them again later
304          */
305         if(This->wineD3DTexture)
306         {
307             IWineD3DBaseTexture_Release(This->wineD3DTexture);
308         }
309         /* If it's the RenderTarget, destroy the d3ddevice */
310         else if(This->wineD3DSwapChain)
311         {
312             if((ddraw->d3d_initialized) && (This == ddraw->d3d_target)) {
313                 TRACE("(%p) Destroying the render target, uninitializing D3D\n", This);
314
315                 /* Unset any index buffer, just to be sure */
316                 IWineD3DDevice_SetIndices(ddraw->wineD3DDevice, NULL);
317                 IWineD3DDevice_SetDepthStencilSurface(ddraw->wineD3DDevice, NULL);
318                 IWineD3DDevice_SetVertexDeclaration(ddraw->wineD3DDevice, NULL);
319                 for(i = 0; i < ddraw->numConvertedDecls; i++)
320                 {
321                     IWineD3DVertexDeclaration_Release(ddraw->decls[i].decl);
322                 }
323                 HeapFree(GetProcessHeap(), 0, ddraw->decls);
324                 ddraw->numConvertedDecls = 0;
325
326                 if(IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain) != D3D_OK)
327                 {
328                     /* Not good */
329                     ERR("(%p) Failed to uninit 3D\n", This);
330                 }
331                 else
332                 {
333                     /* Free the d3d window if one was created */
334                     if(ddraw->d3d_window != 0 && ddraw->d3d_window != ddraw->dest_window)
335                     {
336                         TRACE(" (%p) Destroying the hidden render window %p\n", This, ddraw->d3d_window);
337                         DestroyWindow(ddraw->d3d_window);
338                         ddraw->d3d_window = 0;
339                     }
340                     /* Unset the pointers */
341                 }
342
343                 This->wineD3DSwapChain = NULL; /* Uninit3D releases the swapchain */
344                 ddraw->d3d_initialized = FALSE;
345                 ddraw->d3d_target = NULL;
346             } else {
347                 IWineD3DDevice_UninitGDI(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain);
348                 This->wineD3DSwapChain = NULL;
349             }
350
351             /* Reset to the default surface implementation type. This is needed if apps use
352              * non render target surfaces and expect blits to work after destroying the render
353              * target.
354              *
355              * TODO: Recreate existing offscreen surfaces
356              */
357             ddraw->ImplType = DefaultSurfaceType;
358
359             /* Write a trace because D3D unloading was the reason for many
360              * crashes during development.
361              */
362             TRACE("(%p) D3D unloaded\n", This);
363         }
364         else if(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
365                                                      DDSCAPS_3DDEVICE       |
366                                                      DDSCAPS_TEXTURE        ) )
367         {
368             /* It's a render target, but no swapchain was created.
369              * The IParent interfaces have to be released manually.
370              * The same applies for textures without an
371              * IWineD3DTexture object attached
372              */
373             IParent *Parent;
374
375             for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
376             {
377                 if(This->complex_array[i])
378                 {
379                     /* Only the topmost level can have more than 1 surfaces in the complex
380                      * attachment array(Cube texture roots), for all others there is only
381                      * one
382                      */
383                     surf = This->complex_array[i];
384                     while(surf)
385                     {
386                         IWineD3DSurface_GetParent(surf->WineD3DSurface,
387                                                   (IUnknown **) &Parent);
388                         IParent_Release(Parent);  /* For the getParent */
389                         IParent_Release(Parent);  /* To release it */
390                         surf = surf->complex_array[0];
391                     }
392                 }
393             }
394
395             /* Now the top-level surface */
396             IWineD3DSurface_GetParent(This->WineD3DSurface,
397                                       (IUnknown **) &Parent);
398             IParent_Release(Parent);  /* For the getParent */
399             IParent_Release(Parent);  /* To release it */
400         }
401
402         /* The refcount test shows that the palette is detached when the surface is destroyed */
403         IDirectDrawSurface7_SetPalette(ICOM_INTERFACE(This, IDirectDrawSurface7),
404                                                       NULL);
405
406         /* Loop through all complex attached surfaces,
407          * and destroy them.
408          *
409          * Yet again, only the root can have more than one complexly attached surface, all the others
410          * have a total of one;
411          */
412         for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
413         {
414             if(!This->complex_array[i]) break;
415
416             surf = This->complex_array[i];
417             This->complex_array[i] = NULL;
418             while(surf)
419             {
420                 IDirectDrawSurfaceImpl *destroy = surf;
421                 surf = surf->complex_array[0];              /* Iterate through the "tree" */
422                 IDirectDrawSurfaceImpl_Destroy(destroy);    /* Destroy it */
423             }
424         }
425
426         /* Destroy the root surface.
427          */
428         IDirectDrawSurfaceImpl_Destroy(This);
429
430         /* Reduce the ddraw refcount */
431         if(ifaceToRelease) IUnknown_Release(ifaceToRelease);
432         LeaveCriticalSection(&ddraw_cs);
433     }
434
435     return ref;
436 }
437
438 /*****************************************************************************
439  * IDirectDrawSurface7::GetAttachedSurface
440  *
441  * Returns an attached surface with the requested caps. Surface attachment
442  * and complex surfaces are not clearly described by the MSDN or sdk,
443  * so this method is tricky and likely to contain problems.
444  * This implementation searches the complex list first, then the
445  * attachment chain.
446  *
447  * The chains are searched from This down to the last surface in the chain,
448  * not from the first element in the chain. The first surface found is
449  * returned. The MSDN says that this method fails if more than one surface
450  * matches the caps, but it is not sure if that is right. The attachment
451  * structure may not even allow two matching surfaces.
452  *
453  * The found surface is AddRef-ed before it is returned.
454  *
455  * Params:
456  *  Caps: Pointer to a DDCAPS2 structure describing the caps asked for
457  *  Surface: Address to store the found surface
458  *
459  * Returns:
460  *  DD_OK on success
461  *  DDERR_INVALIDPARAMS if Caps or Surface is NULL
462  *  DDERR_NOTFOUND if no surface was found
463  *
464  *****************************************************************************/
465 static HRESULT WINAPI
466 IDirectDrawSurfaceImpl_GetAttachedSurface(IDirectDrawSurface7 *iface,
467                                           DDSCAPS2 *Caps,
468                                           IDirectDrawSurface7 **Surface)
469 {
470     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
471     IDirectDrawSurfaceImpl *surf;
472     DDSCAPS2 our_caps;
473     int i;
474
475     TRACE("(%p)->(%p,%p)\n", This, Caps, Surface);
476     EnterCriticalSection(&ddraw_cs);
477
478     our_caps = *Caps;
479
480     if(This->version < 7)
481     {
482         /* Earlier dx apps put garbage into these members, clear them */
483         our_caps.dwCaps2 = 0;
484         our_caps.dwCaps3 = 0;
485         our_caps.dwCaps4 = 0;
486     }
487
488     TRACE("(%p): Looking for caps: %x,%x,%x,%x\n", This, our_caps.dwCaps, our_caps.dwCaps2, our_caps.dwCaps3, our_caps.dwCaps4); /* FIXME: Better debugging */
489
490     for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
491     {
492         surf = This->complex_array[i];
493         if(!surf) break;
494
495         if (TRACE_ON(ddraw))
496         {
497             TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
498                    surf->surface_desc.ddsCaps.dwCaps,
499                    surf->surface_desc.ddsCaps.dwCaps2,
500                    surf->surface_desc.ddsCaps.dwCaps3,
501                    surf->surface_desc.ddsCaps.dwCaps4);
502         }
503
504         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
505             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
506
507             /* MSDN: "This method fails if more than one surface is attached
508              * that matches the capabilities requested."
509              *
510              * Not sure how to test this.
511              */
512
513             TRACE("(%p): Returning surface %p\n", This, surf);
514             TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
515             *Surface = ICOM_INTERFACE(surf, IDirectDrawSurface7);
516             IDirectDrawSurface7_AddRef(*Surface);
517             LeaveCriticalSection(&ddraw_cs);
518             return DD_OK;
519         }
520     }
521
522     /* Next, look at the attachment chain */
523     surf = This;
524
525     while( (surf = surf->next_attached) )
526     {
527         if (TRACE_ON(ddraw))
528         {
529             TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
530                    surf->surface_desc.ddsCaps.dwCaps,
531                    surf->surface_desc.ddsCaps.dwCaps2,
532                    surf->surface_desc.ddsCaps.dwCaps3,
533                    surf->surface_desc.ddsCaps.dwCaps4);
534         }
535
536         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
537             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
538
539             TRACE("(%p): Returning surface %p\n", This, surf);
540             *Surface = ICOM_INTERFACE(surf, IDirectDrawSurface7);
541             IDirectDrawSurface7_AddRef(*Surface);
542             LeaveCriticalSection(&ddraw_cs);
543             return DD_OK;
544         }
545     }
546
547     TRACE("(%p) Didn't find a valid surface\n", This);
548     LeaveCriticalSection(&ddraw_cs);
549
550     *Surface = NULL;
551     return DDERR_NOTFOUND;
552 }
553
554 /*****************************************************************************
555  * IDirectDrawSurface7::Lock
556  *
557  * Locks the surface and returns a pointer to the surface's memory
558  *
559  * Params:
560  *  Rect: Rectangle to lock. If NULL, the whole surface is locked
561  *  DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
562  *  Flags: Locking flags, e.g Read only or write only
563  *  h: An event handle that's not used and must be NULL
564  *
565  * Returns:
566  *  DD_OK on success
567  *  DDERR_INVALIDPARAMS if DDSD is NULL
568  *  For more details, see IWineD3DSurface::LockRect
569  *
570  *****************************************************************************/
571 static HRESULT WINAPI
572 IDirectDrawSurfaceImpl_Lock(IDirectDrawSurface7 *iface,
573                             RECT *Rect,
574                             DDSURFACEDESC2 *DDSD,
575                             DWORD Flags,
576                             HANDLE h)
577 {
578     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
579     WINED3DLOCKED_RECT LockedRect;
580     HRESULT hr;
581     TRACE("(%p)->(%p,%p,%x,%p)\n", This, Rect, DDSD, Flags, h);
582
583     if(!DDSD)
584         return DDERR_INVALIDPARAMS;
585
586     /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
587     EnterCriticalSection(&ddraw_cs);
588
589     /* Should I check for the handle to be NULL?
590      *
591      * The DDLOCK flags and the D3DLOCK flags are equal
592      * for the supported values. The others are ignored by WineD3D
593      */
594
595     if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
596        DDSD->dwSize != sizeof(DDSURFACEDESC2))
597     {
598         WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
599         LeaveCriticalSection(&ddraw_cs);
600         return DDERR_INVALIDPARAMS;
601     }
602
603     /* Windows zeroes this if the rect is invalid */
604     DDSD->lpSurface = 0;
605
606     if (Rect)
607     {
608         if ((Rect->left < 0)
609                 || (Rect->top < 0)
610                 || (Rect->left > Rect->right)
611                 || (Rect->top > Rect->bottom)
612                 || (Rect->right > This->surface_desc.dwWidth)
613                 || (Rect->bottom > This->surface_desc.dwHeight))
614         {
615             WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
616             LeaveCriticalSection(&ddraw_cs);
617             return DDERR_INVALIDPARAMS;
618         }
619     }
620
621     hr = IWineD3DSurface_LockRect(This->WineD3DSurface,
622                                   &LockedRect,
623                                   Rect,
624                                   Flags);
625     if(hr != D3D_OK)
626     {
627         LeaveCriticalSection(&ddraw_cs);
628         switch(hr)
629         {
630             /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
631              * specific error. But since IWineD3DSurface::LockRect returns that error in this
632              * only occasion, keep d3d8 and d3d9 free from the return value override. There are
633              * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
634              * is much easier to do it in one place in ddraw
635              */
636             case WINED3DERR_INVALIDCALL:    return DDERR_SURFACEBUSY;
637             default:                        return hr;
638         }
639     }
640
641     /* Override the memory area. The pitch should be set already. Strangely windows
642      * does not set the LPSURFACE flag on locked surfaces !?!.
643      * DDSD->dwFlags |= DDSD_LPSURFACE;
644      */
645     This->surface_desc.lpSurface = LockedRect.pBits;
646     DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
647
648     TRACE("locked surface returning description :\n");
649     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
650
651     LeaveCriticalSection(&ddraw_cs);
652     return DD_OK;
653 }
654
655 /*****************************************************************************
656  * IDirectDrawSurface7::Unlock
657  *
658  * Unlocks an locked surface
659  *
660  * Params:
661  *  Rect: Not used by this implementation
662  *
663  * Returns:
664  *  D3D_OK on success
665  *  For more details, see IWineD3DSurface::UnlockRect
666  *
667  *****************************************************************************/
668 static HRESULT WINAPI
669 IDirectDrawSurfaceImpl_Unlock(IDirectDrawSurface7 *iface,
670                               RECT *pRect)
671 {
672     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
673     HRESULT hr;
674     TRACE("(%p)->(%p)\n", This, pRect);
675
676     EnterCriticalSection(&ddraw_cs);
677     hr = IWineD3DSurface_UnlockRect(This->WineD3DSurface);
678     if(SUCCEEDED(hr))
679     {
680         This->surface_desc.lpSurface = NULL;
681     }
682     LeaveCriticalSection(&ddraw_cs);
683     return hr;
684 }
685
686 /*****************************************************************************
687  * IDirectDrawSurface7::Flip
688  *
689  * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
690  * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
691  * the flip target is passed to WineD3D, even if the app didn't specify one
692  *
693  * Params:
694  *  DestOverride: Specifies the surface that will become the new front
695  *                buffer. If NULL, the current back buffer is used
696  *  Flags: some DirectDraw flags, see include/ddraw.h
697  *
698  * Returns:
699  *  DD_OK on success
700  *  DDERR_NOTFLIPPABLE if no flip target could be found
701  *  DDERR_INVALIDOBJECT if the surface isn't a front buffer
702  *  For more details, see IWineD3DSurface::Flip
703  *
704  *****************************************************************************/
705 static HRESULT WINAPI
706 IDirectDrawSurfaceImpl_Flip(IDirectDrawSurface7 *iface,
707                             IDirectDrawSurface7 *DestOverride,
708                             DWORD Flags)
709 {
710     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
711     IDirectDrawSurfaceImpl *Override = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DestOverride);
712     IDirectDrawSurface7 *Override7;
713     HRESULT hr;
714     TRACE("(%p)->(%p,%x)\n", This, DestOverride, Flags);
715
716     /* Flip has to be called from a front buffer
717      * What about overlay surfaces, AFAIK they can flip too?
718      */
719     if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
720         return DDERR_INVALIDOBJECT; /* Unchecked */
721
722     EnterCriticalSection(&ddraw_cs);
723
724     /* WineD3D doesn't keep track of attached surface, so find the target */
725     if(!Override)
726     {
727         DDSCAPS2 Caps;
728
729         memset(&Caps, 0, sizeof(Caps));
730         Caps.dwCaps |= DDSCAPS_BACKBUFFER;
731         hr = IDirectDrawSurface7_GetAttachedSurface(iface, &Caps, &Override7);
732         if(hr != DD_OK)
733         {
734             ERR("Can't find a flip target\n");
735             LeaveCriticalSection(&ddraw_cs);
736             return DDERR_NOTFLIPPABLE; /* Unchecked */
737         }
738         Override = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Override7);
739
740         /* For the GetAttachedSurface */
741         IDirectDrawSurface7_Release(Override7);
742     }
743
744     hr = IWineD3DSurface_Flip(This->WineD3DSurface,
745                               Override->WineD3DSurface,
746                               Flags);
747     LeaveCriticalSection(&ddraw_cs);
748     return hr;
749 }
750
751 /*****************************************************************************
752  * IDirectDrawSurface7::Blt
753  *
754  * Performs a blit on the surface
755  *
756  * Params:
757  *  DestRect: Destination rectangle, can be NULL
758  *  SrcSurface: Source surface, can be NULL
759  *  SrcRect: Source rectangle, can be NULL
760  *  Flags: Blt flags
761  *  DDBltFx: Some extended blt parameters, connected to the flags
762  *
763  * Returns:
764  *  D3D_OK on success
765  *  See IWineD3DSurface::Blt for more details
766  *
767  *****************************************************************************/
768 static HRESULT WINAPI
769 IDirectDrawSurfaceImpl_Blt(IDirectDrawSurface7 *iface,
770                            RECT *DestRect,
771                            IDirectDrawSurface7 *SrcSurface,
772                            RECT *SrcRect,
773                            DWORD Flags,
774                            DDBLTFX *DDBltFx)
775 {
776     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
777     HRESULT hr;
778     IDirectDrawSurfaceImpl *Src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, SrcSurface);
779     TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
780
781     /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
782      * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
783      */
784     if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
785         WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
786         return DDERR_INVALIDPARAMS;
787     }
788
789     if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
790         WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
791         return DDERR_INVALIDPARAMS;
792     }
793
794     /* Sizes can change, therefore hold the lock when testing the rectangles */
795     EnterCriticalSection(&ddraw_cs);
796     if(DestRect)
797     {
798         if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
799            DestRect->right > This->surface_desc.dwWidth ||
800            DestRect->bottom > This->surface_desc.dwHeight)
801         {
802             WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
803             LeaveCriticalSection(&ddraw_cs);
804             return DDERR_INVALIDRECT;
805         }
806     }
807     if(Src && SrcRect)
808     {
809         if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
810            SrcRect->right > Src->surface_desc.dwWidth ||
811            SrcRect->bottom > Src->surface_desc.dwHeight)
812         {
813             WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
814             LeaveCriticalSection(&ddraw_cs);
815             return DDERR_INVALIDRECT;
816         }
817     }
818
819     if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
820         WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
821         LeaveCriticalSection(&ddraw_cs);
822         return DDERR_INVALIDPARAMS;
823     }
824
825     /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it does, copy the struct,
826      * and replace the ddraw surfaces with the wined3d surfaces
827      * So far no blitting operations using surfaces in the bltfx struct are supported anyway.
828      */
829     hr = IWineD3DSurface_Blt(This->WineD3DSurface,
830                              DestRect,
831                              Src ? Src->WineD3DSurface : NULL,
832                              SrcRect,
833                              Flags,
834                              (WINEDDBLTFX *) DDBltFx,
835                              WINED3DTEXF_POINT);
836
837     LeaveCriticalSection(&ddraw_cs);
838     switch(hr)
839     {
840         case WINED3DERR_NOTAVAILABLE:       return DDERR_UNSUPPORTED;
841         case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
842         default:                            return hr;
843     }
844 }
845
846 /*****************************************************************************
847  * IDirectDrawSurface7::AddAttachedSurface
848  *
849  * Attaches a surface to another surface. How the surface attachments work
850  * is not totally understood yet, and this method is prone to problems.
851  * he surface that is attached is AddRef-ed.
852  *
853  * Tests with complex surfaces suggest that the surface attachments form a
854  * tree, but no method to test this has been found yet.
855  *
856  * The attachment list consists of a first surface (first_attached) and
857  * for each surface a pointer to the next attached surface (next_attached).
858  * For the first surface, and a surface that has no attachments
859  * first_attached points to the surface itself. A surface that has
860  * no successors in the chain has next_attached set to NULL.
861  *
862  * Newly attached surfaces are attached right after the root surface.
863  * If a surface is attached to a complex surface compound, it's attached to
864  * the surface that the app requested, not the complex root. See
865  * GetAttachedSurface for a description how surfaces are found.
866  *
867  * This is how the current implementation works, and it was coded by looking
868  * at the needs of the applications.
869  *
870  * So far only Z-Buffer attachments are tested, and they are activated in
871  * WineD3D. Mipmaps could be tricky to activate in WineD3D.
872  * Back buffers should work in 2D mode, but they are not tested(They can be
873  * attached in older iface versions). Rendering to the front buffer and
874  * switching between that and double buffering is not yet implemented in
875  * WineD3D, so for 3D it might have unexpected results.
876  *
877  * IDirectDrawSurfaceImpl_AddAttachedSurface is the real thing,
878  * IDirectDrawSurface7Impl_AddAttachedSurface is a wrapper around it that
879  * performs additional checks. Version 7 of this interface is much more restrictive
880  * than its predecessors.
881  *
882  * Params:
883  *  Attach: Surface to attach to iface
884  *
885  * Returns:
886  *  DD_OK on success
887  *  DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
888  *
889  *****************************************************************************/
890 HRESULT WINAPI
891 IDirectDrawSurfaceImpl_AddAttachedSurface(IDirectDrawSurfaceImpl *This,
892                                           IDirectDrawSurfaceImpl *Surf)
893 {
894     TRACE("(%p)->(%p)\n", This, Surf);
895
896     if(Surf == This)
897         return DDERR_CANNOTATTACHSURFACE; /* unchecked */
898
899     EnterCriticalSection(&ddraw_cs);
900
901     /* Check if the surface is already attached somewhere */
902     if( (Surf->next_attached != NULL) ||
903         (Surf->first_attached != Surf) )
904     {
905         /* TODO: Test for the structure of the manual attachment. Is it a chain or a list?
906          * What happens if one surface is attached to 2 different surfaces?
907          */
908         FIXME("(%p) The Surface %p is already attached somewhere else: next_attached = %p, first_attached = %p, can't handle by now\n", This, Surf, Surf->next_attached, Surf->first_attached);
909         LeaveCriticalSection(&ddraw_cs);
910         return DDERR_SURFACEALREADYATTACHED;
911     }
912
913     /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
914     Surf->next_attached = This->next_attached;
915     Surf->first_attached = This->first_attached;
916     This->next_attached = Surf;
917
918     /* Check if the WineD3D depth stencil needs updating */
919     if(This->ddraw->d3ddevice)
920     {
921         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
922     }
923
924     /* MSDN: 
925      * "This method increments the reference count of the surface being attached."
926      */
927     IDirectDrawSurface7_AddRef(ICOM_INTERFACE(Surf, IDirectDrawSurface7));
928     LeaveCriticalSection(&ddraw_cs);
929     return DD_OK;
930 }
931
932 static HRESULT WINAPI
933 IDirectDrawSurface7Impl_AddAttachedSurface(IDirectDrawSurface7 *iface,
934                                            IDirectDrawSurface7 *Attach)
935 {
936     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
937     IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Attach);
938
939     /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
940     if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
941     {
942
943         WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
944               Surf->surface_desc.ddsCaps.dwCaps);
945         return DDERR_CANNOTATTACHSURFACE;
946     }
947
948     return IDirectDrawSurfaceImpl_AddAttachedSurface(This,
949                                                      Surf);
950 }
951 /*****************************************************************************
952  * IDirectDrawSurface7::DeleteAttachedSurface
953  *
954  * Removes a surface from the attachment chain. The surface's refcount
955  * is decreased by one after it has been removed
956  *
957  * Params:
958  *  Flags: Some flags, not used by this implementation
959  *  Attach: Surface to detach
960  *
961  * Returns:
962  *  DD_OK on success
963  *  DDERR_SURFACENOTATTACHED if the surface isn't attached to
964  *
965  *****************************************************************************/
966 static HRESULT WINAPI
967 IDirectDrawSurfaceImpl_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
968                                              DWORD Flags,
969                                              IDirectDrawSurface7 *Attach)
970 {
971     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
972     IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Attach);
973     IDirectDrawSurfaceImpl *Prev = This;
974     TRACE("(%p)->(%08x,%p)\n", This, Flags, Surf);
975
976     EnterCriticalSection(&ddraw_cs);
977     if (!Surf || (Surf->first_attached != This) || (Surf == This) )
978     {
979         LeaveCriticalSection(&ddraw_cs);
980         return DDERR_CANNOTDETACHSURFACE;
981     }
982
983     /* Remove MIPMAPSUBLEVEL if this seemed to be one */
984     if (This->surface_desc.ddsCaps.dwCaps &
985         Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
986     {
987         Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
988         /* FIXME: we should probably also subtract from dwMipMapCount of this
989          * and all parent surfaces */
990     }
991
992     /* Find the predecessor of the detached surface */
993     while(Prev)
994     {
995         if(Prev->next_attached == Surf) break;
996         Prev = Prev->next_attached;
997     }
998
999     /* There must be a surface, otherwise there's a bug */
1000     assert(Prev != NULL);
1001
1002     /* Unchain the surface */
1003     Prev->next_attached = Surf->next_attached;
1004     Surf->next_attached = NULL;
1005     Surf->first_attached = Surf;
1006
1007     /* Check if the WineD3D depth stencil needs updating */
1008     if(This->ddraw->d3ddevice)
1009     {
1010         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1011     }
1012
1013     IDirectDrawSurface7_Release(Attach);
1014     LeaveCriticalSection(&ddraw_cs);
1015     return DD_OK;
1016 }
1017
1018 /*****************************************************************************
1019  * IDirectDrawSurface7::AddOverlayDirtyRect
1020  *
1021  * "This method is not currently implemented"
1022  *
1023  * Params:
1024  *  Rect: ?
1025  *
1026  * Returns:
1027  *  DDERR_UNSUPPORTED
1028  *
1029  *****************************************************************************/
1030 static HRESULT WINAPI
1031 IDirectDrawSurfaceImpl_AddOverlayDirtyRect(IDirectDrawSurface7 *iface,
1032                                            LPRECT Rect)
1033 {
1034     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1035     TRACE("(%p)->(%p)\n",This,Rect);
1036
1037     /* MSDN says it's not implemented. I could forward it to WineD3D, 
1038      * then we'd implement it, but I don't think that's a good idea
1039      * (Stefan Dösinger)
1040      */
1041 #if 0
1042     return IWineD3DSurface_AddOverlayDirtyRect(This->WineD3DSurface, pRect);
1043 #endif
1044     return DDERR_UNSUPPORTED; /* unchecked */
1045 }
1046
1047 /*****************************************************************************
1048  * IDirectDrawSurface7::GetDC
1049  *
1050  * Returns a GDI device context for the surface
1051  *
1052  * Params:
1053  *  hdc: Address of a HDC variable to store the dc to
1054  *
1055  * Returns:
1056  *  DD_OK on success
1057  *  DDERR_INVALIDPARAMS if hdc is NULL
1058  *  For details, see IWineD3DSurface::GetDC
1059  *
1060  *****************************************************************************/
1061 static HRESULT WINAPI
1062 IDirectDrawSurfaceImpl_GetDC(IDirectDrawSurface7 *iface,
1063                              HDC *hdc)
1064 {
1065     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1066     HRESULT hr;
1067     TRACE("(%p)->(%p): Relay\n", This, hdc);
1068
1069     if(!hdc)
1070         return DDERR_INVALIDPARAMS;
1071
1072     EnterCriticalSection(&ddraw_cs);
1073     hr = IWineD3DSurface_GetDC(This->WineD3DSurface,
1074                                hdc);
1075     LeaveCriticalSection(&ddraw_cs);
1076     return hr;
1077 }
1078
1079 /*****************************************************************************
1080  * IDirectDrawSurface7::ReleaseDC
1081  *
1082  * Releases the DC that was constructed with GetDC
1083  *
1084  * Params:
1085  *  hdc: HDC to release
1086  *
1087  * Returns:
1088  *  DD_OK on success
1089  *  For more details, see IWineD3DSurface::ReleaseDC
1090  *
1091  *****************************************************************************/
1092 static HRESULT WINAPI
1093 IDirectDrawSurfaceImpl_ReleaseDC(IDirectDrawSurface7 *iface,
1094                                  HDC hdc)
1095 {
1096     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1097     HRESULT hr;
1098     TRACE("(%p)->(%p): Relay\n", This, hdc);
1099
1100     EnterCriticalSection(&ddraw_cs);
1101     hr = IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
1102     LeaveCriticalSection(&ddraw_cs);
1103     return hr;
1104 }
1105
1106 /*****************************************************************************
1107  * IDirectDrawSurface7::GetCaps
1108  *
1109  * Returns the surface's caps
1110  *
1111  * Params:
1112  *  Caps: Address to write the caps to
1113  *
1114  * Returns:
1115  *  DD_OK on success
1116  *  DDERR_INVALIDPARAMS if Caps is NULL
1117  *
1118  *****************************************************************************/
1119 static HRESULT WINAPI
1120 IDirectDrawSurfaceImpl_GetCaps(IDirectDrawSurface7 *iface,
1121                                DDSCAPS2 *Caps)
1122 {
1123     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1124     TRACE("(%p)->(%p)\n",This,Caps);
1125
1126     if(!Caps)
1127         return DDERR_INVALIDPARAMS;
1128
1129     *Caps = This->surface_desc.ddsCaps;
1130     return DD_OK;
1131 }
1132
1133 /*****************************************************************************
1134  * IDirectDrawSurface7::SetPriority
1135  *
1136  * Sets a texture priority for managed textures.
1137  *
1138  * Params:
1139  *  Priority: The new priority
1140  *
1141  * Returns:
1142  *  DD_OK on success
1143  *  For more details, see IWineD3DSurface::SetPriority
1144  *
1145  *****************************************************************************/
1146 static HRESULT WINAPI
1147 IDirectDrawSurfaceImpl_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1148 {
1149     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1150     HRESULT hr;
1151     TRACE("(%p)->(%d): Relay!\n",This,Priority);
1152
1153     EnterCriticalSection(&ddraw_cs);
1154     hr = IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
1155     LeaveCriticalSection(&ddraw_cs);
1156     return hr;
1157 }
1158
1159 /*****************************************************************************
1160  * IDirectDrawSurface7::GetPriority
1161  *
1162  * Returns the surface's priority
1163  *
1164  * Params:
1165  *  Priority: Address of a variable to write the priority to
1166  *
1167  * Returns:
1168  *  D3D_OK on success
1169  *  DDERR_INVALIDPARAMS if Priority == NULL
1170  *  For more details, see IWineD3DSurface::GetPriority
1171  *
1172  *****************************************************************************/
1173 static HRESULT WINAPI
1174 IDirectDrawSurfaceImpl_GetPriority(IDirectDrawSurface7 *iface,
1175                                    DWORD *Priority)
1176 {
1177     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1178     TRACE("(%p)->(%p): Relay\n",This,Priority);
1179
1180     if(!Priority)
1181     {
1182         return DDERR_INVALIDPARAMS;
1183     }
1184
1185     EnterCriticalSection(&ddraw_cs);
1186     *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1187     LeaveCriticalSection(&ddraw_cs);
1188     return DD_OK;
1189 }
1190
1191 /*****************************************************************************
1192  * IDirectDrawSurface7::SetPrivateData
1193  *
1194  * Stores some data in the surface that is intended for the application's
1195  * use.
1196  *
1197  * Params:
1198  *  tag: GUID that identifies the data
1199  *  Data: Pointer to the private data
1200  *  Size: Size of the private data
1201  *  Flags: Some flags
1202  *
1203  * Returns:
1204  *  D3D_OK on success
1205  *  For more details, see IWineD3DSurface::SetPrivateData
1206  *
1207  *****************************************************************************/
1208 static HRESULT WINAPI
1209 IDirectDrawSurfaceImpl_SetPrivateData(IDirectDrawSurface7 *iface,
1210                                       REFGUID tag,
1211                                       void *Data,
1212                                       DWORD Size,
1213                                       DWORD Flags)
1214 {
1215     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1216     HRESULT hr;
1217     TRACE("(%p)->(%s,%p,%d,%x): Relay\n", This, debugstr_guid(tag), Data, Size, Flags);
1218
1219     EnterCriticalSection(&ddraw_cs);
1220     hr = IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1221                                         tag,
1222                                         Data,
1223                                         Size,
1224                                         Flags);
1225     LeaveCriticalSection(&ddraw_cs);
1226     switch(hr)
1227     {
1228         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1229         default:                            return hr;
1230     }
1231 }
1232
1233 /*****************************************************************************
1234  * IDirectDrawSurface7::GetPrivateData
1235  *
1236  * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1237  *
1238  * Params:
1239  *  tag: GUID of the data to return
1240  *  Data: Address where to write the data to
1241  *  Size: Size of the buffer at Data
1242  *
1243  * Returns:
1244  *  DD_OK on success
1245  *  DDERR_INVALIDPARAMS if Data is NULL
1246  *  For more details, see IWineD3DSurface::GetPrivateData
1247  *
1248  *****************************************************************************/
1249 static HRESULT WINAPI
1250 IDirectDrawSurfaceImpl_GetPrivateData(IDirectDrawSurface7 *iface,
1251                                       REFGUID tag,
1252                                       void *Data,
1253                                       DWORD *Size)
1254 {
1255     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1256     HRESULT hr;
1257     TRACE("(%p)->(%s,%p,%p): Relay\n", This, debugstr_guid(tag), Data, Size);
1258
1259     if(!Data)
1260         return DDERR_INVALIDPARAMS;
1261
1262     EnterCriticalSection(&ddraw_cs);
1263     hr = IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1264                                         tag,
1265                                         Data,
1266                                         Size);
1267     LeaveCriticalSection(&ddraw_cs);
1268     return hr;
1269 }
1270
1271 /*****************************************************************************
1272  * IDirectDrawSurface7::FreePrivateData
1273  *
1274  * Frees private data stored in the surface
1275  *
1276  * Params:
1277  *  tag: Tag of the data to free
1278  *
1279  * Returns:
1280  *  D3D_OK on success
1281  *  For more details, see IWineD3DSurface::FreePrivateData
1282  *
1283  *****************************************************************************/
1284 static HRESULT WINAPI
1285 IDirectDrawSurfaceImpl_FreePrivateData(IDirectDrawSurface7 *iface,
1286                                        REFGUID tag)
1287 {
1288     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1289     HRESULT hr;
1290     TRACE("(%p)->(%s): Relay\n", This, debugstr_guid(tag));
1291
1292     EnterCriticalSection(&ddraw_cs);
1293     hr = IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1294     LeaveCriticalSection(&ddraw_cs);
1295     return hr;
1296 }
1297
1298 /*****************************************************************************
1299  * IDirectDrawSurface7::PageLock
1300  *
1301  * Prevents a sysmem surface from being paged out
1302  *
1303  * Params:
1304  *  Flags: Not used, must be 0(unchecked)
1305  *
1306  * Returns:
1307  *  DD_OK, because it's a stub
1308  *
1309  *****************************************************************************/
1310 static HRESULT WINAPI
1311 IDirectDrawSurfaceImpl_PageLock(IDirectDrawSurface7 *iface,
1312                                 DWORD Flags)
1313 {
1314     TRACE("(%p)->(%x)\n", iface, Flags);
1315
1316     /* This is Windows memory management related - we don't need this */
1317     return DD_OK;
1318 }
1319
1320 /*****************************************************************************
1321  * IDirectDrawSurface7::PageUnlock
1322  *
1323  * Allows a sysmem surface to be paged out
1324  *
1325  * Params:
1326  *  Flags: Not used, must be 0(unchecked)
1327  *
1328  * Returns:
1329  *  DD_OK, because it's a stub
1330  *
1331  *****************************************************************************/
1332 static HRESULT WINAPI
1333 IDirectDrawSurfaceImpl_PageUnlock(IDirectDrawSurface7 *iface,
1334                                   DWORD Flags)
1335 {
1336     TRACE("(%p)->(%x)\n", iface, Flags);
1337
1338     return DD_OK;
1339 }
1340
1341 /*****************************************************************************
1342  * IDirectDrawSurface7::BltBatch
1343  *
1344  * An unimplemented function
1345  *
1346  * Params:
1347  *  ?
1348  *
1349  * Returns:
1350  *  DDERR_UNSUPPORTED
1351  *
1352  *****************************************************************************/
1353 static HRESULT WINAPI IDirectDrawSurfaceImpl_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1354 {
1355     TRACE("(%p)->(%p,%d,%08x)\n",iface,Batch,Count,Flags);
1356
1357     /* MSDN: "not currently implemented" */
1358     return DDERR_UNSUPPORTED;
1359 }
1360
1361 /*****************************************************************************
1362  * IDirectDrawSurface7::EnumAttachedSurfaces
1363  *
1364  * Enumerates all surfaces attached to this surface
1365  *
1366  * Params:
1367  *  context: Pointer to pass unmodified to the callback
1368  *  cb: Callback function to call for each surface
1369  *
1370  * Returns:
1371  *  DD_OK on success
1372  *  DDERR_INVALIDPARAMS if cb is NULL
1373  *
1374  *****************************************************************************/
1375 static HRESULT WINAPI
1376 IDirectDrawSurfaceImpl_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1377                                             void *context,
1378                                             LPDDENUMSURFACESCALLBACK7 cb)
1379 {
1380     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1381     IDirectDrawSurfaceImpl *surf;
1382     DDSURFACEDESC2 desc;
1383     int i;
1384
1385     /* Attached surfaces aren't handled in WineD3D */
1386     TRACE("(%p)->(%p,%p)\n",This,context,cb);
1387
1388     if(!cb)
1389         return DDERR_INVALIDPARAMS;
1390
1391     EnterCriticalSection(&ddraw_cs);
1392     for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1393     {
1394         surf = This->complex_array[i];
1395         if(!surf) break;
1396
1397         IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
1398         desc = surf->surface_desc;
1399         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1400         if (cb(ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, context) == DDENUMRET_CANCEL)
1401         {
1402             LeaveCriticalSection(&ddraw_cs);
1403             return DD_OK;
1404         }
1405     }
1406
1407     for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1408     {
1409         IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
1410         desc = surf->surface_desc;
1411         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1412         if (cb( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, context) == DDENUMRET_CANCEL)
1413         {
1414             LeaveCriticalSection(&ddraw_cs);
1415             return DD_OK;
1416         }
1417     }
1418
1419     TRACE(" end of enumeration.\n");
1420
1421     LeaveCriticalSection(&ddraw_cs);
1422     return DD_OK;
1423 }
1424
1425 /*****************************************************************************
1426  * IDirectDrawSurface7::EnumOverlayZOrders
1427  *
1428  * "Enumerates the overlay surfaces on the specified destination"
1429  *
1430  * Params:
1431  *  Flags: DDENUMOVERLAYZ_BACKTOFRONT  or DDENUMOVERLAYZ_FRONTTOBACK
1432  *  context: context to pass back to the callback
1433  *  cb: callback function to call for each enumerated surface
1434  *
1435  * Returns:
1436  *  DD_OK, because it's a stub
1437  *
1438  *****************************************************************************/
1439 static HRESULT WINAPI
1440 IDirectDrawSurfaceImpl_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1441                                           DWORD Flags,
1442                                           void *context,
1443                                           LPDDENUMSURFACESCALLBACK7 cb)
1444 {
1445      FIXME("(%p)->(%x,%p,%p): Stub!\n", iface, Flags, context, cb);
1446
1447     return DD_OK;
1448 }
1449
1450 /*****************************************************************************
1451  * IDirectDrawSurface7::GetBltStatus
1452  *
1453  * Returns the blitting status
1454  *
1455  * Params:
1456  *  Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1457  *
1458  * Returns:
1459  *  See IWineD3DSurface::Blt
1460  *
1461  *****************************************************************************/
1462 static HRESULT WINAPI
1463 IDirectDrawSurfaceImpl_GetBltStatus(IDirectDrawSurface7 *iface,
1464                                     DWORD Flags)
1465 {
1466     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1467     HRESULT hr;
1468     TRACE("(%p)->(%x): Relay\n", This, Flags);
1469
1470     EnterCriticalSection(&ddraw_cs);
1471     hr = IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1472     LeaveCriticalSection(&ddraw_cs);
1473     switch(hr)
1474     {
1475         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1476         default:                            return hr;
1477     }
1478 }
1479
1480 /*****************************************************************************
1481  * IDirectDrawSurface7::GetColorKey
1482  *
1483  * Returns the color key assigned to the surface
1484  *
1485  * Params:
1486  *  Flags: Some flags
1487  *  CKey: Address to store the key to
1488  *
1489  * Returns:
1490  *  DD_OK on success
1491  *  DDERR_INVALIDPARAMS if CKey is NULL
1492  *
1493  *****************************************************************************/
1494 static HRESULT WINAPI
1495 IDirectDrawSurfaceImpl_GetColorKey(IDirectDrawSurface7 *iface,
1496                                    DWORD Flags,
1497                                    DDCOLORKEY *CKey)
1498 {
1499     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1500     TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
1501
1502     if(!CKey)
1503         return DDERR_INVALIDPARAMS;
1504
1505     EnterCriticalSection(&ddraw_cs);
1506
1507     switch (Flags)
1508     {
1509     case DDCKEY_DESTBLT:
1510         if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
1511         {
1512             LeaveCriticalSection(&ddraw_cs);
1513             return DDERR_NOCOLORKEY;
1514         }
1515         *CKey = This->surface_desc.ddckCKDestBlt;
1516         break;
1517
1518     case DDCKEY_DESTOVERLAY:
1519         if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
1520             {
1521             LeaveCriticalSection(&ddraw_cs);
1522             return DDERR_NOCOLORKEY;
1523             }
1524         *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1525         break;
1526
1527     case DDCKEY_SRCBLT:
1528         if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
1529         {
1530             LeaveCriticalSection(&ddraw_cs);
1531             return DDERR_NOCOLORKEY;
1532         }
1533         *CKey = This->surface_desc.ddckCKSrcBlt;
1534         break;
1535
1536     case DDCKEY_SRCOVERLAY:
1537         if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
1538         {
1539             LeaveCriticalSection(&ddraw_cs);
1540             return DDERR_NOCOLORKEY;
1541         }
1542         *CKey = This->surface_desc.ddckCKSrcOverlay;
1543         break;
1544
1545     default:
1546         LeaveCriticalSection(&ddraw_cs);
1547         return DDERR_INVALIDPARAMS;
1548     }
1549
1550     LeaveCriticalSection(&ddraw_cs);
1551     return DD_OK;
1552 }
1553
1554 /*****************************************************************************
1555  * IDirectDrawSurface7::GetFlipStatus
1556  *
1557  * Returns the flipping status of the surface
1558  *
1559  * Params:
1560  *  Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1561  *
1562  * Returns:
1563  *  See IWineD3DSurface::GetFlipStatus
1564  *
1565  *****************************************************************************/
1566 static HRESULT WINAPI
1567 IDirectDrawSurfaceImpl_GetFlipStatus(IDirectDrawSurface7 *iface,
1568                                      DWORD Flags)
1569 {
1570     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1571     HRESULT hr;
1572     TRACE("(%p)->(%x): Relay\n", This, Flags);
1573
1574     EnterCriticalSection(&ddraw_cs);
1575     hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1576     LeaveCriticalSection(&ddraw_cs);
1577     switch(hr)
1578     {
1579         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1580         default:                            return hr;
1581     }
1582 }
1583
1584 /*****************************************************************************
1585  * IDirectDrawSurface7::GetOverlayPosition
1586  *
1587  * Returns the display coordinates of a visible and active overlay surface
1588  *
1589  * Params:
1590  *  X
1591  *  Y
1592  *
1593  * Returns:
1594  *  DDERR_NOTAOVERLAYSURFACE, because it's a stub
1595  *****************************************************************************/
1596 static HRESULT WINAPI
1597 IDirectDrawSurfaceImpl_GetOverlayPosition(IDirectDrawSurface7 *iface,
1598                                           LONG *X,
1599                                           LONG *Y) {
1600     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1601     HRESULT hr;
1602     TRACE("(%p)->(%p,%p): Relay\n", This, X, Y);
1603
1604     EnterCriticalSection(&ddraw_cs);
1605     hr = IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1606                                             X,
1607                                             Y);
1608     LeaveCriticalSection(&ddraw_cs);
1609     return hr;
1610 }
1611
1612 /*****************************************************************************
1613  * IDirectDrawSurface7::GetPixelFormat
1614  *
1615  * Returns the pixel format of the Surface
1616  *
1617  * Params:
1618  *  PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1619  *               format should be written
1620  *
1621  * Returns:
1622  *  DD_OK on success
1623  *  DDERR_INVALIDPARAMS if PixelFormat is NULL
1624  *
1625  *****************************************************************************/
1626 static HRESULT WINAPI
1627 IDirectDrawSurfaceImpl_GetPixelFormat(IDirectDrawSurface7 *iface,
1628                                       DDPIXELFORMAT *PixelFormat)
1629 {
1630     /* What is DDERR_INVALIDSURFACETYPE for here? */
1631     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1632     TRACE("(%p)->(%p)\n",This,PixelFormat);
1633
1634     if(!PixelFormat)
1635         return DDERR_INVALIDPARAMS;
1636
1637     EnterCriticalSection(&ddraw_cs);
1638     DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1639     LeaveCriticalSection(&ddraw_cs);
1640
1641     return DD_OK;
1642 }
1643
1644
1645 /*****************************************************************************
1646  * IDirectDrawSurface7::GetSurfaceDesc
1647  *
1648  * Returns the description of this surface
1649  *
1650  * Params:
1651  *  DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1652  *        surface desc
1653  *
1654  * Returns:
1655  *  DD_OK on success
1656  *  DDERR_INVALIDPARAMS if DDSD is NULL
1657  *
1658  *****************************************************************************/
1659 static HRESULT WINAPI
1660 IDirectDrawSurfaceImpl_GetSurfaceDesc(IDirectDrawSurface7 *iface,
1661                                       DDSURFACEDESC2 *DDSD)
1662 {
1663     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1664
1665     TRACE("(%p)->(%p)\n",This,DDSD);
1666
1667     if(!DDSD)
1668         return DDERR_INVALIDPARAMS;
1669
1670     if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1671     {
1672         WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1673         return DDERR_INVALIDPARAMS;
1674     }
1675
1676     EnterCriticalSection(&ddraw_cs);
1677     DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1678     TRACE("Returning surface desc:\n");
1679     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1680
1681     LeaveCriticalSection(&ddraw_cs);
1682     return DD_OK;
1683 }
1684
1685 /*****************************************************************************
1686  * IDirectDrawSurface7::Initialize
1687  *
1688  * Initializes the surface. This is a no-op in Wine
1689  *
1690  * Params:
1691  *  DD: Pointer to an DirectDraw interface
1692  *  DDSD: Surface description for initialization
1693  *
1694  * Returns:
1695  *  DDERR_ALREADYINITIALIZED
1696  *
1697  *****************************************************************************/
1698 static HRESULT WINAPI
1699 IDirectDrawSurfaceImpl_Initialize(IDirectDrawSurface7 *iface,
1700                                   IDirectDraw *DD,
1701                                   DDSURFACEDESC2 *DDSD)
1702 {
1703     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1704     IDirectDrawImpl *ddimpl = ICOM_OBJECT(IDirectDrawImpl, IDirectDraw, DD);
1705     TRACE("(%p)->(%p,%p)\n",This,ddimpl,DDSD);
1706
1707     return DDERR_ALREADYINITIALIZED;
1708 }
1709
1710 /*****************************************************************************
1711  * IDirectDrawSurface7::IsLost
1712  *
1713  * Checks if the surface is lost
1714  *
1715  * Returns:
1716  *  DD_OK, if the surface is usable
1717  *  DDERR_ISLOST if the surface is lost
1718  *  See IWineD3DSurface::IsLost for more details
1719  *
1720  *****************************************************************************/
1721 static HRESULT WINAPI
1722 IDirectDrawSurfaceImpl_IsLost(IDirectDrawSurface7 *iface)
1723 {
1724     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1725     HRESULT hr;
1726     TRACE("(%p)\n", This);
1727
1728     EnterCriticalSection(&ddraw_cs);
1729     /* We lose the surface if the implementation was changed */
1730     if(This->ImplType != This->ddraw->ImplType)
1731     {
1732         /* But this shouldn't happen. When we change the implementation,
1733          * all surfaces are re-created automatically, and their content
1734          * is copied
1735          */
1736         ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1737         LeaveCriticalSection(&ddraw_cs);
1738         return DDERR_SURFACELOST;
1739     }
1740
1741     hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
1742     LeaveCriticalSection(&ddraw_cs);
1743     switch(hr)
1744     {
1745         /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1746          * WineD3D uses the same error for surfaces
1747          */
1748         case WINED3DERR_DEVICELOST:         return DDERR_SURFACELOST;
1749         default:                            return hr;
1750     }
1751 }
1752
1753 /*****************************************************************************
1754  * IDirectDrawSurface7::Restore
1755  *
1756  * Restores a lost surface. This makes the surface usable again, but
1757  * doesn't reload its old contents
1758  *
1759  * Returns:
1760  *  DD_OK on success
1761  *  See IWineD3DSurface::Restore for more details
1762  *
1763  *****************************************************************************/
1764 static HRESULT WINAPI
1765 IDirectDrawSurfaceImpl_Restore(IDirectDrawSurface7 *iface)
1766 {
1767     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1768     HRESULT hr;
1769     TRACE("(%p)\n", This);
1770
1771     EnterCriticalSection(&ddraw_cs);
1772     if(This->ImplType != This->ddraw->ImplType)
1773     {
1774         /* Call the recreation callback. Make sure to AddRef first */
1775         IDirectDrawSurface_AddRef(iface);
1776         IDirectDrawImpl_RecreateSurfacesCallback(iface,
1777                                                  &This->surface_desc,
1778                                                  NULL /* Not needed */);
1779     }
1780     hr = IWineD3DSurface_Restore(This->WineD3DSurface);
1781     LeaveCriticalSection(&ddraw_cs);
1782     return hr;
1783 }
1784
1785 /*****************************************************************************
1786  * IDirectDrawSurface7::SetOverlayPosition
1787  *
1788  * Changes the display coordinates of an overlay surface
1789  *
1790  * Params:
1791  *  X:
1792  *  Y:
1793  *
1794  * Returns:
1795  *   DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
1796  *****************************************************************************/
1797 static HRESULT WINAPI
1798 IDirectDrawSurfaceImpl_SetOverlayPosition(IDirectDrawSurface7 *iface,
1799                                           LONG X,
1800                                           LONG Y)
1801 {
1802     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1803     HRESULT hr;
1804     TRACE("(%p)->(%d,%d): Relay\n", This, X, Y);
1805
1806     EnterCriticalSection(&ddraw_cs);
1807     hr = IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
1808                                             X,
1809                                             Y);
1810     LeaveCriticalSection(&ddraw_cs);
1811     return hr;
1812 }
1813
1814 /*****************************************************************************
1815  * IDirectDrawSurface7::UpdateOverlay
1816  *
1817  * Modifies the attributes of an overlay surface.
1818  *
1819  * Params:
1820  *  SrcRect: The section of the source being used for the overlay
1821  *  DstSurface: Address of the surface that is overlaid
1822  *  DstRect: Place of the overlay
1823  *  Flags: some DDOVER_* flags
1824  *
1825  * Returns:
1826  *  DDERR_UNSUPPORTED, because we don't support overlays
1827  *
1828  *****************************************************************************/
1829 static HRESULT WINAPI
1830 IDirectDrawSurfaceImpl_UpdateOverlay(IDirectDrawSurface7 *iface,
1831                                      LPRECT SrcRect,
1832                                      IDirectDrawSurface7 *DstSurface,
1833                                      LPRECT DstRect,
1834                                      DWORD Flags,
1835                                      LPDDOVERLAYFX FX)
1836 {
1837     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1838     IDirectDrawSurfaceImpl *Dst = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DstSurface);
1839     HRESULT hr;
1840     TRACE("(%p)->(%p,%p,%p,%x,%p): Relay\n", This, SrcRect, Dst, DstRect, Flags, FX);
1841
1842     EnterCriticalSection(&ddraw_cs);
1843     hr = IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
1844                                        SrcRect,
1845                                        Dst ? Dst->WineD3DSurface : NULL,
1846                                        DstRect,
1847                                        Flags,
1848                                        (WINEDDOVERLAYFX *) FX);
1849     LeaveCriticalSection(&ddraw_cs);
1850     switch(hr) {
1851         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1852         case WINEDDERR_NOTAOVERLAYSURFACE:  return DDERR_NOTAOVERLAYSURFACE;
1853         case WINEDDERR_OVERLAYNOTVISIBLE:   return DDERR_OVERLAYNOTVISIBLE;
1854         default:
1855             return hr;
1856     }
1857 }
1858
1859 /*****************************************************************************
1860  * IDirectDrawSurface7::UpdateOverlayDisplay
1861  *
1862  * The DX7 sdk says that it's not implemented
1863  *
1864  * Params:
1865  *  Flags: ?
1866  *
1867  * Returns: DDERR_UNSUPPORTED, because we don't support overlays
1868  *
1869  *****************************************************************************/
1870 static HRESULT WINAPI
1871 IDirectDrawSurfaceImpl_UpdateOverlayDisplay(IDirectDrawSurface7 *iface,
1872                                             DWORD Flags)
1873 {
1874     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1875     TRACE("(%p)->(%x)\n", This, Flags);
1876     return DDERR_UNSUPPORTED;
1877 }
1878
1879 /*****************************************************************************
1880  * IDirectDrawSurface7::UpdateOverlayZOrder
1881  *
1882  * Sets an overlay's Z order
1883  *
1884  * Params:
1885  *  Flags: DDOVERZ_* flags
1886  *  DDSRef: Defines the relative position in the overlay chain
1887  *
1888  * Returns:
1889  *  DDERR_NOTOVERLAYSURFACE, because we don't support overlays
1890  *
1891  *****************************************************************************/
1892 static HRESULT WINAPI
1893 IDirectDrawSurfaceImpl_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
1894                                            DWORD Flags,
1895                                            IDirectDrawSurface7 *DDSRef)
1896 {
1897     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1898     HRESULT hr;
1899     IDirectDrawSurfaceImpl *Ref = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DDSRef);
1900
1901     TRACE("(%p)->(%x,%p): Relay\n", This, Flags, Ref);
1902     EnterCriticalSection(&ddraw_cs);
1903     hr =  IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
1904                                               Flags,
1905                                               Ref ? Ref->WineD3DSurface : NULL);
1906     LeaveCriticalSection(&ddraw_cs);
1907     return hr;
1908 }
1909
1910 /*****************************************************************************
1911  * IDirectDrawSurface7::GetDDInterface
1912  *
1913  * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
1914  * surface belongs to
1915  *
1916  * Params:
1917  *  DD: Address to write the interface pointer to
1918  *
1919  * Returns:
1920  *  DD_OK on success
1921  *  DDERR_INVALIDPARAMS if DD is NULL
1922  *
1923  *****************************************************************************/
1924 static HRESULT WINAPI
1925 IDirectDrawSurfaceImpl_GetDDInterface(IDirectDrawSurface7 *iface,
1926                                       void **DD)
1927 {
1928     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1929
1930     TRACE("(%p)->(%p)\n",This,DD);
1931
1932     if(!DD)
1933         return DDERR_INVALIDPARAMS;
1934
1935     switch(This->version)
1936     {
1937         case 7:
1938             *((IDirectDraw7 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw7);
1939             IDirectDraw7_AddRef(*(IDirectDraw7 **) DD);
1940             break;
1941
1942         case 4:
1943             *((IDirectDraw4 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw4);
1944             IDirectDraw4_AddRef(*(IDirectDraw4 **) DD);
1945             break;
1946
1947         case 2:
1948             *((IDirectDraw2 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw2);
1949             IDirectDraw_AddRef( *(IDirectDraw2 **) DD);
1950             break;
1951
1952         case 1:
1953             *((IDirectDraw **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw);
1954             IDirectDraw_AddRef( *(IDirectDraw **) DD);
1955             break;
1956
1957     }
1958
1959     return DD_OK;
1960 }
1961
1962 /* This seems also windows implementation specific - I don't think WineD3D needs this */
1963 static HRESULT WINAPI IDirectDrawSurfaceImpl_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
1964 {
1965     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1966     volatile IDirectDrawSurfaceImpl* vThis = This;
1967
1968     TRACE("(%p)\n",This);
1969     EnterCriticalSection(&ddraw_cs);
1970     /* A uniqueness value of 0 is apparently special.
1971      * This needs to be checked.
1972      * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
1973      */
1974     while (1) {
1975         DWORD old_uniqueness_value = vThis->uniqueness_value;
1976         DWORD new_uniqueness_value = old_uniqueness_value+1;
1977
1978         if (old_uniqueness_value == 0) break;
1979         if (new_uniqueness_value == 0) new_uniqueness_value = 1;
1980
1981         if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
1982                                       old_uniqueness_value,
1983                                       new_uniqueness_value)
1984             == old_uniqueness_value)
1985             break;
1986     }
1987
1988     LeaveCriticalSection(&ddraw_cs);
1989     return DD_OK;
1990 }
1991
1992 static HRESULT WINAPI IDirectDrawSurfaceImpl_GetUniquenessValue(IDirectDrawSurface7 *iface, LPDWORD pValue)
1993 {
1994     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1995
1996     TRACE("(%p)->(%p)\n",This,pValue);
1997     EnterCriticalSection(&ddraw_cs);
1998     *pValue = This->uniqueness_value;
1999     LeaveCriticalSection(&ddraw_cs);
2000     return DD_OK;
2001 }
2002
2003 /*****************************************************************************
2004  * IDirectDrawSurface7::SetLOD
2005  *
2006  * Sets the level of detail of a texture
2007  *
2008  * Params:
2009  *  MaxLOD: LOD to set
2010  *
2011  * Returns:
2012  *  DD_OK on success
2013  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
2014  *
2015  *****************************************************************************/
2016 static HRESULT WINAPI
2017 IDirectDrawSurfaceImpl_SetLOD(IDirectDrawSurface7 *iface,
2018                               DWORD MaxLOD)
2019 {
2020     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2021     HRESULT hr;
2022     TRACE("(%p)->(%d)\n", This, MaxLOD);
2023
2024     EnterCriticalSection(&ddraw_cs);
2025     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2026     {
2027         LeaveCriticalSection(&ddraw_cs);
2028         return DDERR_INVALIDOBJECT;
2029     }
2030
2031     if(!This->wineD3DTexture)
2032     {
2033         ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2034         LeaveCriticalSection(&ddraw_cs);
2035         return DDERR_INVALIDOBJECT;
2036     }
2037
2038     hr = IWineD3DBaseTexture_SetLOD(This->wineD3DTexture,
2039                                     MaxLOD);
2040     LeaveCriticalSection(&ddraw_cs);
2041     return hr;
2042 }
2043
2044 /*****************************************************************************
2045  * IDirectDrawSurface7::GetLOD
2046  *
2047  * Returns the level of detail of a Direct3D texture
2048  *
2049  * Params:
2050  *  MaxLOD: Address to write the LOD to
2051  *
2052  * Returns:
2053  *  DD_OK on success
2054  *  DDERR_INVALIDPARAMS if MaxLOD is NULL
2055  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
2056  *
2057  *****************************************************************************/
2058 static HRESULT WINAPI
2059 IDirectDrawSurfaceImpl_GetLOD(IDirectDrawSurface7 *iface,
2060                               DWORD *MaxLOD)
2061 {
2062     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2063     TRACE("(%p)->(%p)\n", This, MaxLOD);
2064
2065     if(!MaxLOD)
2066         return DDERR_INVALIDPARAMS;
2067
2068     EnterCriticalSection(&ddraw_cs);
2069     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2070     {
2071         LeaveCriticalSection(&ddraw_cs);
2072         return DDERR_INVALIDOBJECT;
2073     }
2074
2075     *MaxLOD = IWineD3DBaseTexture_GetLOD(This->wineD3DTexture);
2076     LeaveCriticalSection(&ddraw_cs);
2077     return DD_OK;
2078 }
2079
2080 /*****************************************************************************
2081  * IDirectDrawSurface7::BltFast
2082  *
2083  * Performs a fast Blit.
2084  *
2085  * Params:
2086  *  dstx: The x coordinate to blit to on the destination
2087  *  dsty: The y coordinate to blit to on the destination
2088  *  Source: The source surface
2089  *  rsrc: The source rectangle
2090  *  trans: Type of transfer. Some DDBLTFAST_* flags
2091  *
2092  * Returns:
2093  *  DD_OK on success
2094  *  For more details, see IWineD3DSurface::BltFast
2095  *
2096  *****************************************************************************/
2097 static HRESULT WINAPI
2098 IDirectDrawSurfaceImpl_BltFast(IDirectDrawSurface7 *iface,
2099                                DWORD dstx,
2100                                DWORD dsty,
2101                                IDirectDrawSurface7 *Source,
2102                                RECT *rsrc,
2103                                DWORD trans)
2104 {
2105     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2106     HRESULT hr;
2107     IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Source);
2108     TRACE("(%p)->(%d,%d,%p,%p,%d): Relay\n", This, dstx, dsty, Source, rsrc, trans);
2109
2110     /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2111      * in that case
2112      */
2113     if(rsrc)
2114     {
2115         if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2116            rsrc->right > src->surface_desc.dwWidth ||
2117            rsrc->bottom > src->surface_desc.dwHeight)
2118         {
2119             WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2120             return DDERR_INVALIDRECT;
2121         }
2122         if(dstx + rsrc->right - rsrc->left > This->surface_desc.dwWidth ||
2123            dsty + rsrc->bottom - rsrc->top > This->surface_desc.dwHeight)
2124         {
2125             WARN("Destination area out of bounds, returning DDERR_INVALIDRECT\n");
2126             return DDERR_INVALIDRECT;
2127         }
2128     }
2129     else
2130     {
2131         if(dstx + src->surface_desc.dwWidth > This->surface_desc.dwWidth ||
2132            dsty + src->surface_desc.dwHeight > This->surface_desc.dwHeight)
2133         {
2134             WARN("Destination area out of bounds, returning DDERR_INVALIDRECT\n");
2135             return DDERR_INVALIDRECT;
2136         }
2137     }
2138
2139     EnterCriticalSection(&ddraw_cs);
2140     hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
2141                                  dstx, dsty,
2142                                  src ? src->WineD3DSurface : NULL,
2143                                  rsrc,
2144                                  trans);
2145     LeaveCriticalSection(&ddraw_cs);
2146     switch(hr)
2147     {
2148         case WINED3DERR_NOTAVAILABLE:           return DDERR_UNSUPPORTED;
2149         case WINED3DERR_WRONGTEXTUREFORMAT:     return DDERR_INVALIDPIXELFORMAT;
2150         default:                                return hr;
2151     }
2152 }
2153
2154 /*****************************************************************************
2155  * IDirectDrawSurface7::GetClipper
2156  *
2157  * Returns the IDirectDrawClipper interface of the clipper assigned to this
2158  * surface
2159  *
2160  * Params:
2161  *  Clipper: Address to store the interface pointer at
2162  *
2163  * Returns:
2164  *  DD_OK on success
2165  *  DDERR_INVALIDPARAMS if Clipper is NULL
2166  *  DDERR_NOCLIPPERATTACHED if there's no clipper attached
2167  *
2168  *****************************************************************************/
2169 static HRESULT WINAPI
2170 IDirectDrawSurfaceImpl_GetClipper(IDirectDrawSurface7 *iface,
2171                                   IDirectDrawClipper **Clipper)
2172 {
2173     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2174     TRACE("(%p)->(%p)\n", This, Clipper);
2175
2176     if(!Clipper)
2177     {
2178         LeaveCriticalSection(&ddraw_cs);
2179         return DDERR_INVALIDPARAMS;
2180     }
2181
2182     EnterCriticalSection(&ddraw_cs);
2183     if(This->clipper == NULL)
2184     {
2185         LeaveCriticalSection(&ddraw_cs);
2186         return DDERR_NOCLIPPERATTACHED;
2187     }
2188
2189     *Clipper = ICOM_INTERFACE(This->clipper, IDirectDrawClipper);
2190     IDirectDrawClipper_AddRef(*Clipper);
2191     LeaveCriticalSection(&ddraw_cs);
2192     return DD_OK;
2193 }
2194
2195 /*****************************************************************************
2196  * IDirectDrawSurface7::SetClipper
2197  *
2198  * Sets a clipper for the surface
2199  *
2200  * Params:
2201  *  Clipper: IDirectDrawClipper interface of the clipper to set
2202  *
2203  * Returns:
2204  *  DD_OK on success
2205  *
2206  *****************************************************************************/
2207 static HRESULT WINAPI
2208 IDirectDrawSurfaceImpl_SetClipper(IDirectDrawSurface7 *iface,
2209                                   IDirectDrawClipper *Clipper)
2210 {
2211     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2212     IDirectDrawClipperImpl *oldClipper = This->clipper;
2213     HWND clipWindow;
2214     HRESULT hr;
2215     TRACE("(%p)->(%p)\n",This,Clipper);
2216
2217     EnterCriticalSection(&ddraw_cs);
2218     if (ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper) == This->clipper)
2219     {
2220         LeaveCriticalSection(&ddraw_cs);
2221         return DD_OK;
2222     }
2223
2224     This->clipper = ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper);
2225
2226     if (Clipper != NULL)
2227         IDirectDrawClipper_AddRef(Clipper);
2228     if(oldClipper)
2229         IDirectDrawClipper_Release(ICOM_INTERFACE(oldClipper, IDirectDrawClipper));
2230
2231     hr = IWineD3DSurface_SetClipper(This->WineD3DSurface, This->clipper ? This->clipper->wineD3DClipper : NULL);
2232
2233     if(This->wineD3DSwapChain) {
2234         clipWindow = NULL;
2235         if(Clipper) {
2236             IDirectDrawClipper_GetHWnd(Clipper, &clipWindow);
2237         }
2238
2239         if(clipWindow) {
2240             IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2241                                                     clipWindow);
2242         } else {
2243             IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2244                                                     This->ddraw->d3d_window);
2245         }
2246     }
2247
2248     LeaveCriticalSection(&ddraw_cs);
2249     return hr;
2250 }
2251
2252 /*****************************************************************************
2253  * IDirectDrawSurface7::SetSurfaceDesc
2254  *
2255  * Sets the surface description. It can override the pixel format, the surface
2256  * memory, ...
2257  * It's not really tested.
2258  *
2259  * Params:
2260  * DDSD: Pointer to the new surface description to set
2261  * Flags: Some flags
2262  *
2263  * Returns:
2264  *  DD_OK on success
2265  *  DDERR_INVALIDPARAMS if DDSD is NULL
2266  *
2267  *****************************************************************************/
2268 static HRESULT WINAPI
2269 IDirectDrawSurfaceImpl_SetSurfaceDesc(IDirectDrawSurface7 *iface,
2270                                       DDSURFACEDESC2 *DDSD,
2271                                       DWORD Flags)
2272 {
2273     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2274     WINED3DFORMAT newFormat = WINED3DFMT_UNKNOWN;
2275     HRESULT hr;
2276     TRACE("(%p)->(%p,%x)\n", This, DDSD, Flags);
2277
2278     if(!DDSD)
2279         return DDERR_INVALIDPARAMS;
2280
2281     EnterCriticalSection(&ddraw_cs);
2282     if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2283     {
2284         newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2285
2286         if(newFormat == WINED3DFMT_UNKNOWN)
2287         {
2288             ERR("Requested to set an unknown pixelformat\n");
2289             LeaveCriticalSection(&ddraw_cs);
2290             return DDERR_INVALIDPARAMS;
2291         }
2292         if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2293         {
2294             hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2295                                            newFormat);
2296             if(hr != DD_OK)
2297             {
2298                 LeaveCriticalSection(&ddraw_cs);
2299                 return hr;
2300             }
2301         }
2302     }
2303     if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2304     {
2305         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2306                                     DDCKEY_DESTOVERLAY,
2307                                     (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2308     }
2309     if (DDSD->dwFlags & DDSD_CKDESTBLT)
2310     {
2311         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2312                                     DDCKEY_DESTBLT,
2313                                     (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2314     }
2315     if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2316     {
2317         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2318                                     DDCKEY_SRCOVERLAY,
2319                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2320     }
2321     if (DDSD->dwFlags & DDSD_CKSRCBLT)
2322     {
2323         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2324                                     DDCKEY_SRCBLT,
2325                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2326     }
2327     if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2328     {
2329         hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2330         if(hr != WINED3D_OK)
2331         {
2332             /* No need for a trace here, wined3d does that for us */
2333             LeaveCriticalSection(&ddraw_cs);
2334             switch(hr)
2335             {
2336                 case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2337                 default:                            break; /* Go on */
2338             }
2339         }
2340
2341     }
2342
2343     This->surface_desc = *DDSD;
2344
2345     LeaveCriticalSection(&ddraw_cs);
2346     return DD_OK;
2347 }
2348
2349 /*****************************************************************************
2350  * IDirectDrawSurface7::GetPalette
2351  *
2352  * Returns the IDirectDrawPalette interface of the palette currently assigned
2353  * to the surface
2354  *
2355  * Params:
2356  *  Pal: Address to write the interface pointer to
2357  *
2358  * Returns:
2359  *  DD_OK on success
2360  *  DDERR_INVALIDPARAMS if Pal is NULL
2361  *
2362  *****************************************************************************/
2363 static HRESULT WINAPI
2364 IDirectDrawSurfaceImpl_GetPalette(IDirectDrawSurface7 *iface,
2365                                   IDirectDrawPalette **Pal)
2366 {
2367     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2368     IWineD3DPalette *wPal;
2369     HRESULT hr;
2370     TRACE("(%p)->(%p): Relay\n", This, Pal);
2371
2372     if(!Pal)
2373         return DDERR_INVALIDPARAMS;
2374
2375     EnterCriticalSection(&ddraw_cs);
2376     hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
2377     if(hr != DD_OK)
2378     {
2379         LeaveCriticalSection(&ddraw_cs);
2380         return hr;
2381     }
2382
2383     if(wPal)
2384     {
2385         hr = IWineD3DPalette_GetParent(wPal, (IUnknown **) Pal);
2386     }
2387     else
2388     {
2389         *Pal = NULL;
2390         hr = DDERR_NOPALETTEATTACHED;
2391     }
2392
2393     LeaveCriticalSection(&ddraw_cs);
2394     return hr;
2395 }
2396
2397 /*****************************************************************************
2398  * SetColorKeyEnum
2399  *
2400  * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2401  * recursively in the surface tree
2402  *
2403  *****************************************************************************/
2404 struct SCKContext
2405 {
2406     HRESULT ret;
2407     WINEDDCOLORKEY *CKey;
2408     DWORD Flags;
2409 };
2410
2411 static HRESULT WINAPI
2412 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2413                 DDSURFACEDESC2 *desc,
2414                 void *context)
2415 {
2416     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2417     struct SCKContext *ctx = context;
2418     HRESULT hr;
2419
2420     hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2421                                      ctx->Flags,
2422                                      ctx->CKey);
2423     if(hr != DD_OK)
2424     {
2425         WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2426         ctx->ret = hr;
2427     }
2428
2429     IDirectDrawSurface7_EnumAttachedSurfaces(surface,
2430                                              context,
2431                                              SetColorKeyEnum);
2432     IDirectDrawSurface7_Release(surface);
2433     return DDENUMRET_OK;
2434 }
2435
2436 /*****************************************************************************
2437  * IDirectDrawSurface7::SetColorKey
2438  *
2439  * Sets the color keying options for the surface. Observations showed that
2440  * in case of complex surfaces the color key has to be assigned to all
2441  * sublevels.
2442  *
2443  * Params:
2444  *  Flags: DDCKEY_*
2445  *  CKey: The new color key
2446  *
2447  * Returns:
2448  *  DD_OK on success
2449  *  See IWineD3DSurface::SetColorKey for details
2450  *
2451  *****************************************************************************/
2452 static HRESULT WINAPI
2453 IDirectDrawSurfaceImpl_SetColorKey(IDirectDrawSurface7 *iface,
2454                                    DWORD Flags,
2455                                    DDCOLORKEY *CKey)
2456 {
2457     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2458     struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) CKey, Flags };
2459     TRACE("(%p)->(%x,%p)\n", This, Flags, CKey);
2460
2461     EnterCriticalSection(&ddraw_cs);
2462     if (CKey)
2463     {
2464         switch (Flags & ~DDCKEY_COLORSPACE)
2465         {
2466         case DDCKEY_DESTBLT:
2467             This->surface_desc.ddckCKDestBlt = *CKey;
2468             This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2469             break;
2470
2471         case DDCKEY_DESTOVERLAY:
2472             This->surface_desc.u3.ddckCKDestOverlay = *CKey;
2473             This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2474             break;
2475
2476         case DDCKEY_SRCOVERLAY:
2477             This->surface_desc.ddckCKSrcOverlay = *CKey;
2478             This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2479             break;
2480
2481         case DDCKEY_SRCBLT:
2482             This->surface_desc.ddckCKSrcBlt = *CKey;
2483             This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2484             break;
2485
2486         default:
2487             LeaveCriticalSection(&ddraw_cs);
2488             return DDERR_INVALIDPARAMS;
2489         }
2490     }
2491     else
2492     {
2493         switch (Flags & ~DDCKEY_COLORSPACE)
2494         {
2495         case DDCKEY_DESTBLT:
2496             This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2497             break;
2498
2499         case DDCKEY_DESTOVERLAY:
2500             This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2501             break;
2502
2503         case DDCKEY_SRCOVERLAY:
2504             This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2505             break;
2506
2507         case DDCKEY_SRCBLT:
2508             This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2509             break;
2510
2511         default:
2512             LeaveCriticalSection(&ddraw_cs);
2513             return DDERR_INVALIDPARAMS;
2514         }
2515     }
2516     ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2517                                           Flags,
2518                                           ctx.CKey);
2519     IDirectDrawSurface7_EnumAttachedSurfaces(iface,
2520                                              (void *) &ctx,
2521                                              SetColorKeyEnum);
2522     LeaveCriticalSection(&ddraw_cs);
2523     switch(ctx.ret)
2524     {
2525         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2526         default:                            return ctx.ret;
2527     }
2528 }
2529
2530 /*****************************************************************************
2531  * IDirectDrawSurface7::SetPalette
2532  *
2533  * Assigns a DirectDrawPalette object to the surface
2534  *
2535  * Params:
2536  *  Pal: Interface to the palette to set
2537  *
2538  * Returns:
2539  *  DD_OK on success
2540  *
2541  *****************************************************************************/
2542 static HRESULT WINAPI
2543 IDirectDrawSurfaceImpl_SetPalette(IDirectDrawSurface7 *iface,
2544                                   IDirectDrawPalette *Pal)
2545 {
2546     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2547     IDirectDrawPalette *oldPal;
2548     IDirectDrawSurfaceImpl *surf;
2549     IDirectDrawPaletteImpl *PalImpl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, Pal);
2550     HRESULT hr;
2551     TRACE("(%p)->(%p)\n", This, Pal);
2552
2553     if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
2554             DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
2555         return DDERR_INVALIDPIXELFORMAT;
2556     }
2557
2558     /* Find the old palette */
2559     EnterCriticalSection(&ddraw_cs);
2560     hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2561     if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2562     {
2563         LeaveCriticalSection(&ddraw_cs);
2564         return hr;
2565     }
2566     if(oldPal) IDirectDrawPalette_Release(oldPal);  /* For the GetPalette */
2567
2568     /* Set the new Palette */
2569     IWineD3DSurface_SetPalette(This->WineD3DSurface,
2570                                PalImpl ? PalImpl->wineD3DPalette : NULL);
2571     /* AddRef the Palette */
2572     if(Pal) IDirectDrawPalette_AddRef(Pal);
2573
2574     /* Release the old palette */
2575     if(oldPal) IDirectDrawPalette_Release(oldPal);
2576
2577     /* If this is a front buffer, also update the back buffers
2578      * TODO: How do things work for palettized cube textures?
2579      */
2580     if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2581     {
2582         /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2583         DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2584
2585         surf = This;
2586         while(1)
2587         {
2588             IDirectDrawSurface7 *attach;
2589             HRESULT hr;
2590             hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
2591                                                         &caps2, &attach);
2592             if(hr != DD_OK)
2593             {
2594                 break;
2595             }
2596
2597             TRACE("Setting palette on %p\n", attach);
2598             IDirectDrawSurface7_SetPalette(attach,
2599                                            Pal);
2600             surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attach);
2601             IDirectDrawSurface7_Release(attach);
2602         }
2603     }
2604
2605     LeaveCriticalSection(&ddraw_cs);
2606     return DD_OK;
2607 }
2608
2609 /*****************************************************************************
2610  * The VTable
2611  *****************************************************************************/
2612
2613 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl =
2614 {
2615     /*** IUnknown ***/
2616     IDirectDrawSurfaceImpl_QueryInterface,
2617     IDirectDrawSurfaceImpl_AddRef,
2618     IDirectDrawSurfaceImpl_Release,
2619     /*** IDirectDrawSurface ***/
2620     IDirectDrawSurface7Impl_AddAttachedSurface,
2621     IDirectDrawSurfaceImpl_AddOverlayDirtyRect,
2622     IDirectDrawSurfaceImpl_Blt,
2623     IDirectDrawSurfaceImpl_BltBatch,
2624     IDirectDrawSurfaceImpl_BltFast,
2625     IDirectDrawSurfaceImpl_DeleteAttachedSurface,
2626     IDirectDrawSurfaceImpl_EnumAttachedSurfaces,
2627     IDirectDrawSurfaceImpl_EnumOverlayZOrders,
2628     IDirectDrawSurfaceImpl_Flip,
2629     IDirectDrawSurfaceImpl_GetAttachedSurface,
2630     IDirectDrawSurfaceImpl_GetBltStatus,
2631     IDirectDrawSurfaceImpl_GetCaps,
2632     IDirectDrawSurfaceImpl_GetClipper,
2633     IDirectDrawSurfaceImpl_GetColorKey,
2634     IDirectDrawSurfaceImpl_GetDC,
2635     IDirectDrawSurfaceImpl_GetFlipStatus,
2636     IDirectDrawSurfaceImpl_GetOverlayPosition,
2637     IDirectDrawSurfaceImpl_GetPalette,
2638     IDirectDrawSurfaceImpl_GetPixelFormat,
2639     IDirectDrawSurfaceImpl_GetSurfaceDesc,
2640     IDirectDrawSurfaceImpl_Initialize,
2641     IDirectDrawSurfaceImpl_IsLost,
2642     IDirectDrawSurfaceImpl_Lock,
2643     IDirectDrawSurfaceImpl_ReleaseDC,
2644     IDirectDrawSurfaceImpl_Restore,
2645     IDirectDrawSurfaceImpl_SetClipper,
2646     IDirectDrawSurfaceImpl_SetColorKey,
2647     IDirectDrawSurfaceImpl_SetOverlayPosition,
2648     IDirectDrawSurfaceImpl_SetPalette,
2649     IDirectDrawSurfaceImpl_Unlock,
2650     IDirectDrawSurfaceImpl_UpdateOverlay,
2651     IDirectDrawSurfaceImpl_UpdateOverlayDisplay,
2652     IDirectDrawSurfaceImpl_UpdateOverlayZOrder,
2653     /*** IDirectDrawSurface2 ***/
2654     IDirectDrawSurfaceImpl_GetDDInterface,
2655     IDirectDrawSurfaceImpl_PageLock,
2656     IDirectDrawSurfaceImpl_PageUnlock,
2657     /*** IDirectDrawSurface3 ***/
2658     IDirectDrawSurfaceImpl_SetSurfaceDesc,
2659     /*** IDirectDrawSurface4 ***/
2660     IDirectDrawSurfaceImpl_SetPrivateData,
2661     IDirectDrawSurfaceImpl_GetPrivateData,
2662     IDirectDrawSurfaceImpl_FreePrivateData,
2663     IDirectDrawSurfaceImpl_GetUniquenessValue,
2664     IDirectDrawSurfaceImpl_ChangeUniquenessValue,
2665     /*** IDirectDrawSurface7 ***/
2666     IDirectDrawSurfaceImpl_SetPriority,
2667     IDirectDrawSurfaceImpl_GetPriority,
2668     IDirectDrawSurfaceImpl_SetLOD,
2669     IDirectDrawSurfaceImpl_GetLOD
2670 };