imm32: Implement ImmConfigureIME using the loaded IME.
[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     /* There is a DDERR_NOCOLORKEY error, but how do we know if a color key
1494      * isn't there? That's like saying that an int isn't there. (Which MS
1495      * has done in other docs.) */
1496     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1497     TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
1498
1499     if(!CKey)
1500         return DDERR_INVALIDPARAMS;
1501
1502     EnterCriticalSection(&ddraw_cs);
1503     switch (Flags)
1504     {
1505     case DDCKEY_DESTBLT:
1506         *CKey = This->surface_desc.ddckCKDestBlt;
1507         break;
1508
1509     case DDCKEY_DESTOVERLAY:
1510         *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1511         break;
1512
1513     case DDCKEY_SRCBLT:
1514         *CKey = This->surface_desc.ddckCKSrcBlt;
1515         break;
1516
1517     case DDCKEY_SRCOVERLAY:
1518         *CKey = This->surface_desc.ddckCKSrcOverlay;
1519         break;
1520
1521     default:
1522         LeaveCriticalSection(&ddraw_cs);
1523         return DDERR_INVALIDPARAMS;
1524     }
1525
1526     LeaveCriticalSection(&ddraw_cs);
1527     return DD_OK;
1528 }
1529
1530 /*****************************************************************************
1531  * IDirectDrawSurface7::GetFlipStatus
1532  *
1533  * Returns the flipping status of the surface
1534  *
1535  * Params:
1536  *  Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1537  *
1538  * Returns:
1539  *  See IWineD3DSurface::GetFlipStatus
1540  *
1541  *****************************************************************************/
1542 static HRESULT WINAPI
1543 IDirectDrawSurfaceImpl_GetFlipStatus(IDirectDrawSurface7 *iface,
1544                                      DWORD Flags)
1545 {
1546     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1547     HRESULT hr;
1548     TRACE("(%p)->(%x): Relay\n", This, Flags);
1549
1550     EnterCriticalSection(&ddraw_cs);
1551     hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1552     LeaveCriticalSection(&ddraw_cs);
1553     switch(hr)
1554     {
1555         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1556         default:                            return hr;
1557     }
1558 }
1559
1560 /*****************************************************************************
1561  * IDirectDrawSurface7::GetOverlayPosition
1562  *
1563  * Returns the display coordinates of a visible and active overlay surface
1564  *
1565  * Params:
1566  *  X
1567  *  Y
1568  *
1569  * Returns:
1570  *  DDERR_NOTAOVERLAYSURFACE, because it's a stub
1571  *****************************************************************************/
1572 static HRESULT WINAPI
1573 IDirectDrawSurfaceImpl_GetOverlayPosition(IDirectDrawSurface7 *iface,
1574                                           LONG *X,
1575                                           LONG *Y) {
1576     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1577     HRESULT hr;
1578     TRACE("(%p)->(%p,%p): Relay\n", This, X, Y);
1579
1580     EnterCriticalSection(&ddraw_cs);
1581     hr = IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1582                                             X,
1583                                             Y);
1584     LeaveCriticalSection(&ddraw_cs);
1585     return hr;
1586 }
1587
1588 /*****************************************************************************
1589  * IDirectDrawSurface7::GetPixelFormat
1590  *
1591  * Returns the pixel format of the Surface
1592  *
1593  * Params:
1594  *  PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1595  *               format should be written
1596  *
1597  * Returns:
1598  *  DD_OK on success
1599  *  DDERR_INVALIDPARAMS if PixelFormat is NULL
1600  *
1601  *****************************************************************************/
1602 static HRESULT WINAPI
1603 IDirectDrawSurfaceImpl_GetPixelFormat(IDirectDrawSurface7 *iface,
1604                                       DDPIXELFORMAT *PixelFormat)
1605 {
1606     /* What is DDERR_INVALIDSURFACETYPE for here? */
1607     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1608     TRACE("(%p)->(%p)\n",This,PixelFormat);
1609
1610     if(!PixelFormat)
1611         return DDERR_INVALIDPARAMS;
1612
1613     EnterCriticalSection(&ddraw_cs);
1614     DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1615     LeaveCriticalSection(&ddraw_cs);
1616
1617     return DD_OK;
1618 }
1619
1620
1621 /*****************************************************************************
1622  * IDirectDrawSurface7::GetSurfaceDesc
1623  *
1624  * Returns the description of this surface
1625  *
1626  * Params:
1627  *  DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1628  *        surface desc
1629  *
1630  * Returns:
1631  *  DD_OK on success
1632  *  DDERR_INVALIDPARAMS if DDSD is NULL
1633  *
1634  *****************************************************************************/
1635 static HRESULT WINAPI
1636 IDirectDrawSurfaceImpl_GetSurfaceDesc(IDirectDrawSurface7 *iface,
1637                                       DDSURFACEDESC2 *DDSD)
1638 {
1639     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1640
1641     TRACE("(%p)->(%p)\n",This,DDSD);
1642
1643     if(!DDSD)
1644         return DDERR_INVALIDPARAMS;
1645
1646     if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1647     {
1648         WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1649         return DDERR_INVALIDPARAMS;
1650     }
1651
1652     EnterCriticalSection(&ddraw_cs);
1653     DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1654     TRACE("Returning surface desc:\n");
1655     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1656
1657     LeaveCriticalSection(&ddraw_cs);
1658     return DD_OK;
1659 }
1660
1661 /*****************************************************************************
1662  * IDirectDrawSurface7::Initialize
1663  *
1664  * Initializes the surface. This is a no-op in Wine
1665  *
1666  * Params:
1667  *  DD: Pointer to an DirectDraw interface
1668  *  DDSD: Surface description for initialization
1669  *
1670  * Returns:
1671  *  DDERR_ALREADYINITIALIZED
1672  *
1673  *****************************************************************************/
1674 static HRESULT WINAPI
1675 IDirectDrawSurfaceImpl_Initialize(IDirectDrawSurface7 *iface,
1676                                   IDirectDraw *DD,
1677                                   DDSURFACEDESC2 *DDSD)
1678 {
1679     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1680     IDirectDrawImpl *ddimpl = ICOM_OBJECT(IDirectDrawImpl, IDirectDraw, DD);
1681     TRACE("(%p)->(%p,%p)\n",This,ddimpl,DDSD);
1682
1683     return DDERR_ALREADYINITIALIZED;
1684 }
1685
1686 /*****************************************************************************
1687  * IDirectDrawSurface7::IsLost
1688  *
1689  * Checks if the surface is lost
1690  *
1691  * Returns:
1692  *  DD_OK, if the surface is useable
1693  *  DDERR_ISLOST if the surface is lost
1694  *  See IWineD3DSurface::IsLost for more details
1695  *
1696  *****************************************************************************/
1697 static HRESULT WINAPI
1698 IDirectDrawSurfaceImpl_IsLost(IDirectDrawSurface7 *iface)
1699 {
1700     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1701     HRESULT hr;
1702     TRACE("(%p)\n", This);
1703
1704     EnterCriticalSection(&ddraw_cs);
1705     /* We lose the surface if the implementation was changed */
1706     if(This->ImplType != This->ddraw->ImplType)
1707     {
1708         /* But this shouldn't happen. When we change the implementation,
1709          * all surfaces are re-created automatically, and their content
1710          * is copied
1711          */
1712         ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1713         LeaveCriticalSection(&ddraw_cs);
1714         return DDERR_SURFACELOST;
1715     }
1716
1717     hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
1718     LeaveCriticalSection(&ddraw_cs);
1719     switch(hr)
1720     {
1721         /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1722          * WineD3D uses the same error for surfaces
1723          */
1724         case WINED3DERR_DEVICELOST:         return DDERR_SURFACELOST;
1725         default:                            return hr;
1726     }
1727 }
1728
1729 /*****************************************************************************
1730  * IDirectDrawSurface7::Restore
1731  *
1732  * Restores a lost surface. This makes the surface usable again, but
1733  * doesn't reload its old contents
1734  *
1735  * Returns:
1736  *  DD_OK on success
1737  *  See IWineD3DSurface::Restore for more details
1738  *
1739  *****************************************************************************/
1740 static HRESULT WINAPI
1741 IDirectDrawSurfaceImpl_Restore(IDirectDrawSurface7 *iface)
1742 {
1743     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1744     HRESULT hr;
1745     TRACE("(%p)\n", This);
1746
1747     EnterCriticalSection(&ddraw_cs);
1748     if(This->ImplType != This->ddraw->ImplType)
1749     {
1750         /* Call the recreation callback. Make sure to AddRef first */
1751         IDirectDrawSurface_AddRef(iface);
1752         IDirectDrawImpl_RecreateSurfacesCallback(iface,
1753                                                  &This->surface_desc,
1754                                                  NULL /* Not needed */);
1755     }
1756     hr = IWineD3DSurface_Restore(This->WineD3DSurface);
1757     LeaveCriticalSection(&ddraw_cs);
1758     return hr;
1759 }
1760
1761 /*****************************************************************************
1762  * IDirectDrawSurface7::SetOverlayPosition
1763  *
1764  * Changes the display coordinates of an overlay surface
1765  *
1766  * Params:
1767  *  X:
1768  *  Y:
1769  *
1770  * Returns:
1771  *   DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
1772  *****************************************************************************/
1773 static HRESULT WINAPI
1774 IDirectDrawSurfaceImpl_SetOverlayPosition(IDirectDrawSurface7 *iface,
1775                                           LONG X,
1776                                           LONG Y)
1777 {
1778     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1779     HRESULT hr;
1780     TRACE("(%p)->(%d,%d): Relay\n", This, X, Y);
1781
1782     EnterCriticalSection(&ddraw_cs);
1783     hr = IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
1784                                             X,
1785                                             Y);
1786     LeaveCriticalSection(&ddraw_cs);
1787     return hr;
1788 }
1789
1790 /*****************************************************************************
1791  * IDirectDrawSurface7::UpdateOverlay
1792  *
1793  * Modifies the attributes of an overlay surface.
1794  *
1795  * Params:
1796  *  SrcRect: The section of the source being used for the overlay
1797  *  DstSurface: Address of the surface that is overlaid
1798  *  DstRect: Place of the overlay
1799  *  Flags: some DDOVER_* flags
1800  *
1801  * Returns:
1802  *  DDERR_UNSUPPORTED, because we don't support overlays
1803  *
1804  *****************************************************************************/
1805 static HRESULT WINAPI
1806 IDirectDrawSurfaceImpl_UpdateOverlay(IDirectDrawSurface7 *iface,
1807                                      LPRECT SrcRect,
1808                                      IDirectDrawSurface7 *DstSurface,
1809                                      LPRECT DstRect,
1810                                      DWORD Flags,
1811                                      LPDDOVERLAYFX FX)
1812 {
1813     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1814     IDirectDrawSurfaceImpl *Dst = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DstSurface);
1815     HRESULT hr;
1816     TRACE("(%p)->(%p,%p,%p,%x,%p): Relay\n", This, SrcRect, Dst, DstRect, Flags, FX);
1817
1818     EnterCriticalSection(&ddraw_cs);
1819     hr = IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
1820                                        SrcRect,
1821                                        Dst ? Dst->WineD3DSurface : NULL,
1822                                        DstRect,
1823                                        Flags,
1824                                        (WINEDDOVERLAYFX *) FX);
1825     LeaveCriticalSection(&ddraw_cs);
1826     return hr;
1827 }
1828
1829 /*****************************************************************************
1830  * IDirectDrawSurface7::UpdateOverlayDisplay
1831  *
1832  * The DX7 sdk says that it's not implemented
1833  *
1834  * Params:
1835  *  Flags: ?
1836  *
1837  * Returns: DDERR_UNSUPPORTED, because we don't support overlays
1838  *
1839  *****************************************************************************/
1840 static HRESULT WINAPI
1841 IDirectDrawSurfaceImpl_UpdateOverlayDisplay(IDirectDrawSurface7 *iface,
1842                                             DWORD Flags)
1843 {
1844     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1845     TRACE("(%p)->(%x)\n", This, Flags);
1846     return DDERR_UNSUPPORTED;
1847 }
1848
1849 /*****************************************************************************
1850  * IDirectDrawSurface7::UpdateOverlayZOrder
1851  *
1852  * Sets an overlay's Z order
1853  *
1854  * Params:
1855  *  Flags: DDOVERZ_* flags
1856  *  DDSRef: Defines the relative position in the overlay chain
1857  *
1858  * Returns:
1859  *  DDERR_NOTOVERLAYSURFACE, because we don't support overlays
1860  *
1861  *****************************************************************************/
1862 static HRESULT WINAPI
1863 IDirectDrawSurfaceImpl_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
1864                                            DWORD Flags,
1865                                            IDirectDrawSurface7 *DDSRef)
1866 {
1867     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1868     HRESULT hr;
1869     IDirectDrawSurfaceImpl *Ref = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DDSRef);
1870
1871     TRACE("(%p)->(%x,%p): Relay\n", This, Flags, Ref);
1872     EnterCriticalSection(&ddraw_cs);
1873     hr =  IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
1874                                               Flags,
1875                                               Ref ? Ref->WineD3DSurface : NULL);
1876     LeaveCriticalSection(&ddraw_cs);
1877     return hr;
1878 }
1879
1880 /*****************************************************************************
1881  * IDirectDrawSurface7::GetDDInterface
1882  *
1883  * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
1884  * surface belongs to
1885  *
1886  * Params:
1887  *  DD: Address to write the interface pointer to
1888  *
1889  * Returns:
1890  *  DD_OK on success
1891  *  DDERR_INVALIDPARAMS if DD is NULL
1892  *
1893  *****************************************************************************/
1894 static HRESULT WINAPI
1895 IDirectDrawSurfaceImpl_GetDDInterface(IDirectDrawSurface7 *iface,
1896                                       void **DD)
1897 {
1898     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1899
1900     TRACE("(%p)->(%p)\n",This,DD);
1901
1902     if(!DD)
1903         return DDERR_INVALIDPARAMS;
1904
1905     switch(This->version)
1906     {
1907         case 7:
1908             *((IDirectDraw7 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw7);
1909             IDirectDraw7_AddRef(*(IDirectDraw7 **) DD);
1910             break;
1911
1912         case 4:
1913             *((IDirectDraw4 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw4);
1914             IDirectDraw4_AddRef(*(IDirectDraw4 **) DD);
1915             break;
1916
1917         case 2:
1918             *((IDirectDraw2 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw2);
1919             IDirectDraw_AddRef( *(IDirectDraw2 **) DD);
1920             break;
1921
1922         case 1:
1923             *((IDirectDraw **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw);
1924             IDirectDraw_AddRef( *(IDirectDraw **) DD);
1925             break;
1926
1927     }
1928
1929     return DD_OK;
1930 }
1931
1932 /* This seems also windows implementation specific - I don't think WineD3D needs this */
1933 static HRESULT WINAPI IDirectDrawSurfaceImpl_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
1934 {
1935     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1936     volatile IDirectDrawSurfaceImpl* vThis = This;
1937
1938     TRACE("(%p)\n",This);
1939     EnterCriticalSection(&ddraw_cs);
1940     /* A uniqueness value of 0 is apparently special.
1941      * This needs to be checked.
1942      * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
1943      */
1944     while (1) {
1945         DWORD old_uniqueness_value = vThis->uniqueness_value;
1946         DWORD new_uniqueness_value = old_uniqueness_value+1;
1947
1948         if (old_uniqueness_value == 0) break;
1949         if (new_uniqueness_value == 0) new_uniqueness_value = 1;
1950
1951         if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
1952                                       old_uniqueness_value,
1953                                       new_uniqueness_value)
1954             == old_uniqueness_value)
1955             break;
1956     }
1957
1958     LeaveCriticalSection(&ddraw_cs);
1959     return DD_OK;
1960 }
1961
1962 static HRESULT WINAPI IDirectDrawSurfaceImpl_GetUniquenessValue(IDirectDrawSurface7 *iface, LPDWORD pValue)
1963 {
1964     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1965
1966     TRACE("(%p)->(%p)\n",This,pValue);
1967     EnterCriticalSection(&ddraw_cs);
1968     *pValue = This->uniqueness_value;
1969     LeaveCriticalSection(&ddraw_cs);
1970     return DD_OK;
1971 }
1972
1973 /*****************************************************************************
1974  * IDirectDrawSurface7::SetLOD
1975  *
1976  * Sets the level of detail of a texture
1977  *
1978  * Params:
1979  *  MaxLOD: LOD to set
1980  *
1981  * Returns:
1982  *  DD_OK on success
1983  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
1984  *
1985  *****************************************************************************/
1986 static HRESULT WINAPI
1987 IDirectDrawSurfaceImpl_SetLOD(IDirectDrawSurface7 *iface,
1988                               DWORD MaxLOD)
1989 {
1990     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1991     HRESULT hr;
1992     TRACE("(%p)->(%d)\n", This, MaxLOD);
1993
1994     EnterCriticalSection(&ddraw_cs);
1995     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
1996     {
1997         LeaveCriticalSection(&ddraw_cs);
1998         return DDERR_INVALIDOBJECT;
1999     }
2000
2001     if(!This->wineD3DTexture)
2002     {
2003         ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2004         LeaveCriticalSection(&ddraw_cs);
2005         return DDERR_INVALIDOBJECT;
2006     }
2007
2008     hr = IWineD3DBaseTexture_SetLOD(This->wineD3DTexture,
2009                                     MaxLOD);
2010     LeaveCriticalSection(&ddraw_cs);
2011     return hr;
2012 }
2013
2014 /*****************************************************************************
2015  * IDirectDrawSurface7::GetLOD
2016  *
2017  * Returns the level of detail of a Direct3D texture
2018  *
2019  * Params:
2020  *  MaxLOD: Address to write the LOD to
2021  *
2022  * Returns:
2023  *  DD_OK on success
2024  *  DDERR_INVALIDPARAMS if MaxLOD is NULL
2025  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
2026  *
2027  *****************************************************************************/
2028 static HRESULT WINAPI
2029 IDirectDrawSurfaceImpl_GetLOD(IDirectDrawSurface7 *iface,
2030                               DWORD *MaxLOD)
2031 {
2032     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2033     TRACE("(%p)->(%p)\n", This, MaxLOD);
2034
2035     if(!MaxLOD)
2036         return DDERR_INVALIDPARAMS;
2037
2038     EnterCriticalSection(&ddraw_cs);
2039     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2040     {
2041         LeaveCriticalSection(&ddraw_cs);
2042         return DDERR_INVALIDOBJECT;
2043     }
2044
2045     *MaxLOD = IWineD3DBaseTexture_GetLOD(This->wineD3DTexture);
2046     LeaveCriticalSection(&ddraw_cs);
2047     return DD_OK;
2048 }
2049
2050 /*****************************************************************************
2051  * IDirectDrawSurface7::BltFast
2052  *
2053  * Performs a fast Blit.
2054  *
2055  * Params:
2056  *  dstx: The x coordinate to blit to on the destination
2057  *  dsty: The y coordinate to blit to on the destination
2058  *  Source: The source surface
2059  *  rsrc: The source rectangle
2060  *  trans: Type of transfer. Some DDBLTFAST_* flags
2061  *
2062  * Returns:
2063  *  DD_OK on success
2064  *  For more details, see IWineD3DSurface::BltFast
2065  *
2066  *****************************************************************************/
2067 static HRESULT WINAPI
2068 IDirectDrawSurfaceImpl_BltFast(IDirectDrawSurface7 *iface,
2069                                DWORD dstx,
2070                                DWORD dsty,
2071                                IDirectDrawSurface7 *Source,
2072                                RECT *rsrc,
2073                                DWORD trans)
2074 {
2075     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2076     HRESULT hr;
2077     IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Source);
2078     TRACE("(%p)->(%d,%d,%p,%p,%d): Relay\n", This, dstx, dsty, Source, rsrc, trans);
2079
2080     /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2081      * in that case
2082      */
2083     if(rsrc)
2084     {
2085         if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2086            rsrc->right > src->surface_desc.dwWidth ||
2087            rsrc->bottom > src->surface_desc.dwHeight)
2088         {
2089             WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2090             return DDERR_INVALIDRECT;
2091         }
2092         if(dstx + rsrc->right - rsrc->left > This->surface_desc.dwWidth ||
2093            dsty + rsrc->bottom - rsrc->top > This->surface_desc.dwHeight)
2094         {
2095             WARN("Destination area out of bounds, returning DDERR_INVALIDRECT\n");
2096             return DDERR_INVALIDRECT;
2097         }
2098     }
2099     else
2100     {
2101         if(dstx + src->surface_desc.dwWidth > This->surface_desc.dwWidth ||
2102            dsty + src->surface_desc.dwHeight > This->surface_desc.dwHeight)
2103         {
2104             WARN("Destination area out of bounds, returning DDERR_INVALIDRECT\n");
2105             return DDERR_INVALIDRECT;
2106         }
2107     }
2108
2109     EnterCriticalSection(&ddraw_cs);
2110     hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
2111                                  dstx, dsty,
2112                                  src ? src->WineD3DSurface : NULL,
2113                                  rsrc,
2114                                  trans);
2115     LeaveCriticalSection(&ddraw_cs);
2116     switch(hr)
2117     {
2118         case WINED3DERR_NOTAVAILABLE:           return DDERR_UNSUPPORTED;
2119         case WINED3DERR_WRONGTEXTUREFORMAT:     return DDERR_INVALIDPIXELFORMAT;
2120         default:                                return hr;
2121     }
2122 }
2123
2124 /*****************************************************************************
2125  * IDirectDrawSurface7::GetClipper
2126  *
2127  * Returns the IDirectDrawClipper interface of the clipper assigned to this
2128  * surface
2129  *
2130  * Params:
2131  *  Clipper: Address to store the interface pointer at
2132  *
2133  * Returns:
2134  *  DD_OK on success
2135  *  DDERR_INVALIDPARAMS if Clipper is NULL
2136  *  DDERR_NOCLIPPERATTACHED if there's no clipper attached
2137  *
2138  *****************************************************************************/
2139 static HRESULT WINAPI
2140 IDirectDrawSurfaceImpl_GetClipper(IDirectDrawSurface7 *iface,
2141                                   IDirectDrawClipper **Clipper)
2142 {
2143     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2144     TRACE("(%p)->(%p)\n", This, Clipper);
2145
2146     if(!Clipper)
2147     {
2148         LeaveCriticalSection(&ddraw_cs);
2149         return DDERR_INVALIDPARAMS;
2150     }
2151
2152     EnterCriticalSection(&ddraw_cs);
2153     if(This->clipper == NULL)
2154     {
2155         LeaveCriticalSection(&ddraw_cs);
2156         return DDERR_NOCLIPPERATTACHED;
2157     }
2158
2159     *Clipper = ICOM_INTERFACE(This->clipper, IDirectDrawClipper);
2160     IDirectDrawClipper_AddRef(*Clipper);
2161     LeaveCriticalSection(&ddraw_cs);
2162     return DD_OK;
2163 }
2164
2165 /*****************************************************************************
2166  * IDirectDrawSurface7::SetClipper
2167  *
2168  * Sets a clipper for the surface
2169  *
2170  * Params:
2171  *  Clipper: IDirectDrawClipper interface of the clipper to set
2172  *
2173  * Returns:
2174  *  DD_OK on success
2175  *
2176  *****************************************************************************/
2177 static HRESULT WINAPI
2178 IDirectDrawSurfaceImpl_SetClipper(IDirectDrawSurface7 *iface,
2179                                   IDirectDrawClipper *Clipper)
2180 {
2181     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2182     IDirectDrawClipperImpl *oldClipper = This->clipper;
2183     HRESULT hr;
2184     TRACE("(%p)->(%p)\n",This,Clipper);
2185
2186     EnterCriticalSection(&ddraw_cs);
2187     if (ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper) == This->clipper)
2188     {
2189         LeaveCriticalSection(&ddraw_cs);
2190         return DD_OK;
2191     }
2192
2193     This->clipper = ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper);
2194
2195     if (Clipper != NULL)
2196         IDirectDrawClipper_AddRef(Clipper);
2197     if(oldClipper)
2198         IDirectDrawClipper_Release(ICOM_INTERFACE(oldClipper, IDirectDrawClipper));
2199
2200     hr = IWineD3DSurface_SetClipper(This->WineD3DSurface, This->clipper ? This->clipper->wineD3DClipper : NULL);
2201     LeaveCriticalSection(&ddraw_cs);
2202     return hr;
2203 }
2204
2205 /*****************************************************************************
2206  * IDirectDrawSurface7::SetSurfaceDesc
2207  *
2208  * Sets the surface description. It can override the pixel format, the surface
2209  * memory, ...
2210  * It's not really tested.
2211  *
2212  * Params:
2213  * DDSD: Pointer to the new surface description to set
2214  * Flags: Some flags
2215  *
2216  * Returns:
2217  *  DD_OK on success
2218  *  DDERR_INVALIDPARAMS if DDSD is NULL
2219  *
2220  *****************************************************************************/
2221 static HRESULT WINAPI
2222 IDirectDrawSurfaceImpl_SetSurfaceDesc(IDirectDrawSurface7 *iface,
2223                                       DDSURFACEDESC2 *DDSD,
2224                                       DWORD Flags)
2225 {
2226     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2227     WINED3DFORMAT newFormat = WINED3DFMT_UNKNOWN;
2228     HRESULT hr;
2229     TRACE("(%p)->(%p,%x)\n", This, DDSD, Flags);
2230
2231     if(!DDSD)
2232         return DDERR_INVALIDPARAMS;
2233
2234     EnterCriticalSection(&ddraw_cs);
2235     if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2236     {
2237         newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2238
2239         if(newFormat == WINED3DFMT_UNKNOWN)
2240         {
2241             ERR("Requested to set an unknown pixelformat\n");
2242             LeaveCriticalSection(&ddraw_cs);
2243             return DDERR_INVALIDPARAMS;
2244         }
2245         if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2246         {
2247             hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2248                                            newFormat);
2249             if(hr != DD_OK)
2250             {
2251                 LeaveCriticalSection(&ddraw_cs);
2252                 return hr;
2253             }
2254         }
2255     }
2256     if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2257     {
2258         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2259                                     DDCKEY_DESTOVERLAY,
2260                                     (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2261     }
2262     if (DDSD->dwFlags & DDSD_CKDESTBLT)
2263     {
2264         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2265                                     DDCKEY_DESTBLT,
2266                                     (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2267     }
2268     if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2269     {
2270         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2271                                     DDCKEY_SRCOVERLAY,
2272                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2273     }
2274     if (DDSD->dwFlags & DDSD_CKSRCBLT)
2275     {
2276         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2277                                     DDCKEY_SRCBLT,
2278                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2279     }
2280     if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2281     {
2282         hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2283         if(hr != WINED3D_OK)
2284         {
2285             /* No need for a trace here, wined3d does that for us */
2286             LeaveCriticalSection(&ddraw_cs);
2287             switch(hr)
2288             {
2289                 case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2290                 default:                            break; /* Go on */
2291             }
2292         }
2293
2294     }
2295
2296     This->surface_desc = *DDSD;
2297
2298     LeaveCriticalSection(&ddraw_cs);
2299     return DD_OK;
2300 }
2301
2302 /*****************************************************************************
2303  * IDirectDrawSurface7::GetPalette
2304  *
2305  * Returns the IDirectDrawPalette interface of the palette currently assigned
2306  * to the surface
2307  *
2308  * Params:
2309  *  Pal: Address to write the interface pointer to
2310  *
2311  * Returns:
2312  *  DD_OK on success
2313  *  DDERR_INVALIDPARAMS if Pal is NULL
2314  *
2315  *****************************************************************************/
2316 static HRESULT WINAPI
2317 IDirectDrawSurfaceImpl_GetPalette(IDirectDrawSurface7 *iface,
2318                                   IDirectDrawPalette **Pal)
2319 {
2320     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2321     IWineD3DPalette *wPal;
2322     HRESULT hr;
2323     TRACE("(%p)->(%p): Relay\n", This, Pal);
2324
2325     if(!Pal)
2326         return DDERR_INVALIDPARAMS;
2327
2328     EnterCriticalSection(&ddraw_cs);
2329     hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
2330     if(hr != DD_OK)
2331     {
2332         LeaveCriticalSection(&ddraw_cs);
2333         return hr;
2334     }
2335
2336     if(wPal)
2337     {
2338         hr = IWineD3DPalette_GetParent(wPal, (IUnknown **) Pal);
2339     }
2340     else
2341     {
2342         *Pal = NULL;
2343         hr = DDERR_NOPALETTEATTACHED;
2344     }
2345
2346     LeaveCriticalSection(&ddraw_cs);
2347     return hr;
2348 }
2349
2350 /*****************************************************************************
2351  * SetColorKeyEnum
2352  *
2353  * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2354  * recursively in the surface tree
2355  *
2356  *****************************************************************************/
2357 struct SCKContext
2358 {
2359     HRESULT ret;
2360     WINEDDCOLORKEY *CKey;
2361     DWORD Flags;
2362 };
2363
2364 static HRESULT WINAPI
2365 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2366                 DDSURFACEDESC2 *desc,
2367                 void *context)
2368 {
2369     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2370     struct SCKContext *ctx = context;
2371     HRESULT hr;
2372
2373     hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2374                                      ctx->Flags,
2375                                      ctx->CKey);
2376     if(hr != DD_OK)
2377     {
2378         WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2379         ctx->ret = hr;
2380     }
2381
2382     IDirectDrawSurface7_EnumAttachedSurfaces(surface,
2383                                              context,
2384                                              SetColorKeyEnum);
2385     IDirectDrawSurface7_Release(surface);
2386     return DDENUMRET_OK;
2387 }
2388
2389 /*****************************************************************************
2390  * IDirectDrawSurface7::SetColorKey
2391  *
2392  * Sets the color keying options for the surface. Observations showed that
2393  * in case of complex surfaces the color key has to be assigned to all
2394  * sublevels.
2395  *
2396  * Params:
2397  *  Flags: DDCKEY_*
2398  *  CKey: The new color key
2399  *
2400  * Returns:
2401  *  DD_OK on success
2402  *  See IWineD3DSurface::SetColorKey for details
2403  *
2404  *****************************************************************************/
2405 static HRESULT WINAPI
2406 IDirectDrawSurfaceImpl_SetColorKey(IDirectDrawSurface7 *iface,
2407                                    DWORD Flags,
2408                                    DDCOLORKEY *CKey)
2409 {
2410     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2411     struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) CKey, Flags };
2412     TRACE("(%p)->(%x,%p)\n", This, Flags, CKey);
2413
2414     EnterCriticalSection(&ddraw_cs);
2415     if (CKey)
2416     {
2417         switch (Flags & ~DDCKEY_COLORSPACE)
2418         {
2419         case DDCKEY_DESTBLT:
2420             This->surface_desc.ddckCKDestBlt = *CKey;
2421             This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2422             break;
2423
2424         case DDCKEY_DESTOVERLAY:
2425             This->surface_desc.u3.ddckCKDestOverlay = *CKey;
2426             This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2427             break;
2428
2429         case DDCKEY_SRCOVERLAY:
2430             This->surface_desc.ddckCKSrcOverlay = *CKey;
2431             This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2432             break;
2433
2434         case DDCKEY_SRCBLT:
2435             This->surface_desc.ddckCKSrcBlt = *CKey;
2436             This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2437             break;
2438
2439         default:
2440             LeaveCriticalSection(&ddraw_cs);
2441             return DDERR_INVALIDPARAMS;
2442         }
2443     }
2444     else
2445     {
2446         switch (Flags & ~DDCKEY_COLORSPACE)
2447         {
2448         case DDCKEY_DESTBLT:
2449             This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2450             break;
2451
2452         case DDCKEY_DESTOVERLAY:
2453             This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2454             break;
2455
2456         case DDCKEY_SRCOVERLAY:
2457             This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2458             break;
2459
2460         case DDCKEY_SRCBLT:
2461             This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2462             break;
2463
2464         default:
2465             LeaveCriticalSection(&ddraw_cs);
2466             return DDERR_INVALIDPARAMS;
2467         }
2468     }
2469     ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2470                                           Flags,
2471                                           ctx.CKey);
2472     IDirectDrawSurface7_EnumAttachedSurfaces(iface,
2473                                              (void *) &ctx,
2474                                              SetColorKeyEnum);
2475     LeaveCriticalSection(&ddraw_cs);
2476     switch(ctx.ret)
2477     {
2478         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2479         default:                            return ctx.ret;
2480     }
2481 }
2482
2483 /*****************************************************************************
2484  * IDirectDrawSurface7::SetPalette
2485  *
2486  * Assigns a DirectDrawPalette object to the surface
2487  *
2488  * Params:
2489  *  Pal: Interface to the palette to set
2490  *
2491  * Returns:
2492  *  DD_OK on success
2493  *
2494  *****************************************************************************/
2495 static HRESULT WINAPI
2496 IDirectDrawSurfaceImpl_SetPalette(IDirectDrawSurface7 *iface,
2497                                   IDirectDrawPalette *Pal)
2498 {
2499     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2500     IDirectDrawPalette *oldPal;
2501     IDirectDrawSurfaceImpl *surf;
2502     IDirectDrawPaletteImpl *PalImpl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, Pal);
2503     HRESULT hr;
2504     TRACE("(%p)->(%p)\n", This, Pal);
2505
2506     if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
2507             DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
2508         return DDERR_INVALIDPIXELFORMAT;
2509     }
2510
2511     /* Find the old palette */
2512     EnterCriticalSection(&ddraw_cs);
2513     hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2514     if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2515     {
2516         LeaveCriticalSection(&ddraw_cs);
2517         return hr;
2518     }
2519     if(oldPal) IDirectDrawPalette_Release(oldPal);  /* For the GetPalette */
2520
2521     /* Set the new Palette */
2522     IWineD3DSurface_SetPalette(This->WineD3DSurface,
2523                                PalImpl ? PalImpl->wineD3DPalette : NULL);
2524     /* AddRef the Palette */
2525     if(Pal) IDirectDrawPalette_AddRef(Pal);
2526
2527     /* Release the old palette */
2528     if(oldPal) IDirectDrawPalette_Release(oldPal);
2529
2530     /* If this is a front buffer, also update the back buffers
2531      * TODO: How do things work for palettized cube textures?
2532      */
2533     if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2534     {
2535         /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2536         DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2537
2538         surf = This;
2539         while(1)
2540         {
2541             IDirectDrawSurface7 *attach;
2542             HRESULT hr;
2543             hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
2544                                                         &caps2, &attach);
2545             if(hr != DD_OK)
2546             {
2547                 break;
2548             }
2549
2550             TRACE("Setting palette on %p\n", attach);
2551             IDirectDrawSurface7_SetPalette(attach,
2552                                            Pal);
2553             surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attach);
2554             IDirectDrawSurface7_Release(attach);
2555         }
2556     }
2557
2558     LeaveCriticalSection(&ddraw_cs);
2559     return DD_OK;
2560 }
2561
2562 /*****************************************************************************
2563  * The VTable
2564  *****************************************************************************/
2565
2566 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl =
2567 {
2568     /*** IUnknown ***/
2569     IDirectDrawSurfaceImpl_QueryInterface,
2570     IDirectDrawSurfaceImpl_AddRef,
2571     IDirectDrawSurfaceImpl_Release,
2572     /*** IDirectDrawSurface ***/
2573     IDirectDrawSurface7Impl_AddAttachedSurface,
2574     IDirectDrawSurfaceImpl_AddOverlayDirtyRect,
2575     IDirectDrawSurfaceImpl_Blt,
2576     IDirectDrawSurfaceImpl_BltBatch,
2577     IDirectDrawSurfaceImpl_BltFast,
2578     IDirectDrawSurfaceImpl_DeleteAttachedSurface,
2579     IDirectDrawSurfaceImpl_EnumAttachedSurfaces,
2580     IDirectDrawSurfaceImpl_EnumOverlayZOrders,
2581     IDirectDrawSurfaceImpl_Flip,
2582     IDirectDrawSurfaceImpl_GetAttachedSurface,
2583     IDirectDrawSurfaceImpl_GetBltStatus,
2584     IDirectDrawSurfaceImpl_GetCaps,
2585     IDirectDrawSurfaceImpl_GetClipper,
2586     IDirectDrawSurfaceImpl_GetColorKey,
2587     IDirectDrawSurfaceImpl_GetDC,
2588     IDirectDrawSurfaceImpl_GetFlipStatus,
2589     IDirectDrawSurfaceImpl_GetOverlayPosition,
2590     IDirectDrawSurfaceImpl_GetPalette,
2591     IDirectDrawSurfaceImpl_GetPixelFormat,
2592     IDirectDrawSurfaceImpl_GetSurfaceDesc,
2593     IDirectDrawSurfaceImpl_Initialize,
2594     IDirectDrawSurfaceImpl_IsLost,
2595     IDirectDrawSurfaceImpl_Lock,
2596     IDirectDrawSurfaceImpl_ReleaseDC,
2597     IDirectDrawSurfaceImpl_Restore,
2598     IDirectDrawSurfaceImpl_SetClipper,
2599     IDirectDrawSurfaceImpl_SetColorKey,
2600     IDirectDrawSurfaceImpl_SetOverlayPosition,
2601     IDirectDrawSurfaceImpl_SetPalette,
2602     IDirectDrawSurfaceImpl_Unlock,
2603     IDirectDrawSurfaceImpl_UpdateOverlay,
2604     IDirectDrawSurfaceImpl_UpdateOverlayDisplay,
2605     IDirectDrawSurfaceImpl_UpdateOverlayZOrder,
2606     /*** IDirectDrawSurface2 ***/
2607     IDirectDrawSurfaceImpl_GetDDInterface,
2608     IDirectDrawSurfaceImpl_PageLock,
2609     IDirectDrawSurfaceImpl_PageUnlock,
2610     /*** IDirectDrawSurface3 ***/
2611     IDirectDrawSurfaceImpl_SetSurfaceDesc,
2612     /*** IDirectDrawSurface4 ***/
2613     IDirectDrawSurfaceImpl_SetPrivateData,
2614     IDirectDrawSurfaceImpl_GetPrivateData,
2615     IDirectDrawSurfaceImpl_FreePrivateData,
2616     IDirectDrawSurfaceImpl_GetUniquenessValue,
2617     IDirectDrawSurfaceImpl_ChangeUniquenessValue,
2618     /*** IDirectDrawSurface7 ***/
2619     IDirectDrawSurfaceImpl_SetPriority,
2620     IDirectDrawSurfaceImpl_GetPriority,
2621     IDirectDrawSurfaceImpl_SetLOD,
2622     IDirectDrawSurfaceImpl_GetLOD
2623 };