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