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