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