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