wined3d: Add UpdatOverlayZOrder.
[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 "winnls.h"
40 #include "winerror.h"
41 #include "wingdi.h"
42 #include "wine/exception.h"
43 #include "excpt.h"
44
45 #include "ddraw.h"
46 #include "d3d.h"
47
48 #include "ddraw_private.h"
49 #include "wine/debug.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
52
53 /*****************************************************************************
54  * IUnknown parts follow
55  *****************************************************************************/
56
57 /*****************************************************************************
58  * IDirectDrawSurface7::QueryInterface
59  *
60  * A normal QueryInterface implementation. For QueryInterface rules
61  * see ddraw.c, IDirectDraw7::QueryInterface. This method
62  * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
63  * in all versions, the IDirectDrawGammaControl interface and it can
64  * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
65  *
66  * Params:
67  *  riid: The interface id queried for
68  *  obj: Address to write the pointer to
69  *
70  * Returns:
71  *  S_OK on success
72  *  E_NOINTERFACE if the requested interface wasn't found
73  *
74  *****************************************************************************/
75 static HRESULT WINAPI
76 IDirectDrawSurfaceImpl_QueryInterface(IDirectDrawSurface7 *iface,
77                                       REFIID riid,
78                                       void **obj)
79 {
80     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
81
82     /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
83     *obj = NULL;
84
85     if(!riid)
86         return DDERR_INVALIDPARAMS;
87
88     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),obj);
89     if (IsEqualGUID(riid, &IID_IUnknown)
90      || IsEqualGUID(riid, &IID_IDirectDrawSurface7)
91      || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
92     {
93         IUnknown_AddRef(iface);
94         *obj = ICOM_INTERFACE(This, IDirectDrawSurface7);
95         TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
96         return S_OK;
97     }
98     else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
99           || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
100           || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
101     {
102         IUnknown_AddRef(iface);
103         *obj = ICOM_INTERFACE(This, IDirectDrawSurface3);
104         TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
105         return S_OK;
106     }
107     else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
108     {
109         IUnknown_AddRef(iface);
110         *obj = ICOM_INTERFACE(This, IDirectDrawGammaControl);
111         TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
112         return S_OK;
113     }
114     else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
115              IsEqualGUID(riid, &IID_IDirect3DHALDevice) )
116     {
117         IDirect3DDevice7 *d3d;
118
119         /* Call into IDirect3D7 for creation */
120         IDirect3D7_CreateDevice(ICOM_INTERFACE(This->ddraw, IDirect3D7),
121                                 riid,
122                                 ICOM_INTERFACE(This, IDirectDrawSurface7),
123                                 &d3d);
124
125         *obj = COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice7, IDirect3DDevice, d3d);
126         TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
127
128         return S_OK;
129     }
130     else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
131              IsEqualGUID( &IID_IDirect3DTexture2, riid ))
132     {
133         if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
134         {
135             *obj = ICOM_INTERFACE(This, IDirect3DTexture);
136             TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
137         }
138         else
139         {
140             *obj = ICOM_INTERFACE(This, IDirect3DTexture2);
141             TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
142         }
143         IUnknown_AddRef( (IUnknown *) *obj);
144         return S_OK;
145     }
146
147     ERR("No interface\n");
148     return E_NOINTERFACE;
149 }
150
151 /*****************************************************************************
152  * IDirectDrawSurface7::AddRef
153  *
154  * A normal addref implementation
155  *
156  * Returns:
157  *  The new refcount
158  *
159  *****************************************************************************/
160 static ULONG WINAPI
161 IDirectDrawSurfaceImpl_AddRef(IDirectDrawSurface7 *iface)
162 {
163     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
164     ULONG refCount = InterlockedIncrement(&This->ref);
165
166     TRACE("(%p) : AddRef increasing from %ld\n", This, refCount - 1);
167     return refCount;
168 }
169
170 /*****************************************************************************
171  * IDirectDrawSurfaceImpl_Destroy
172  *
173  * A helper function for IDirectDrawSurface7::Release
174  *
175  * Frees the surface, regardless of its refcount.
176  *  See IDirectDrawSurface7::Release for more information
177  *
178  * Params:
179  *  This: Surface to free
180  *
181  *****************************************************************************/
182 static void IDirectDrawSurfaceImpl_Destroy(IDirectDrawSurfaceImpl *This)
183 {
184     TRACE("(%p)\n", This);
185
186     /* Check the refcount and give a warning */
187     if(This->ref > 1)
188     {
189         /* This can happen when a complex surface is destroyed,
190          * because the 2nd surface was addref()ed when the app
191          * called GetAttachedSurface
192          */
193         WARN("(%p): Destroying surface with refount %ld\n", This, This->ref);
194     }
195
196     /* Check for attached surfaces and detach them */
197     if(This->first_attached != This)
198     {
199         /* Well, this shouldn't happen: The surface being attached is addref()ed
200           * in AddAttachedSurface, so it shouldn't be released until DeleteAttachedSurface
201           * is called, because the refcount is held. It looks like the app released()
202           * it often enough to force this
203           */
204         IDirectDrawSurface7 *root = ICOM_INTERFACE(This->first_attached, IDirectDrawSurface7);
205         IDirectDrawSurface7 *detach = ICOM_INTERFACE(This, IDirectDrawSurface7);
206
207         FIXME("(%p) Freeing a surface that is attached to surface %p\n", This, This->first_attached);
208
209         /* The refcount will drop to -1 here */
210         if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
211         {
212             ERR("(%p) DeleteAttachedSurface failed!\n", This);
213         }
214     }
215
216     while(This->next_attached != NULL)
217     {
218         IDirectDrawSurface7 *root = ICOM_INTERFACE(This, IDirectDrawSurface7);
219         IDirectDrawSurface7 *detach = ICOM_INTERFACE(This->next_attached, IDirectDrawSurface7);
220
221         if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
222         {
223             ERR("(%p) DeleteAttachedSurface failed!\n", This);
224             assert(0);
225         }
226     }
227
228     /* Now destroy the surface. Wait: It could have been released if we are a texture */
229     if(This->WineD3DSurface)
230         IWineD3DSurface_Release(This->WineD3DSurface);
231
232     /* Having a texture handle set implies that the device still exists */
233     if(This->Handle)
234     {
235         This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
236         This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
237     }
238
239     /* Reduce the ddraw surface count */
240     InterlockedDecrement(&This->ddraw->surfaces);
241     if(This->prev)
242         This->prev->next = This->next;
243     else
244     {
245         assert(This->ddraw->surface_list == This);
246         This->ddraw->surface_list = This->next;
247     }
248     if(This->next)
249         This->next->prev = This->prev;
250
251     HeapFree(GetProcessHeap(), 0, This);
252 }
253
254 /*****************************************************************************
255  * IDirectDrawSurface7::Release
256  *
257  * Reduces the surface's refcount by 1. If the refcount falls to 0, the
258  * surface is destroyed.
259  *
260  * Destroying the surface is a bit tricky. For the connection between
261  * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
262  * It has a nice graph explaining the connection.
263  *
264  * What happens here is basically this:
265  * When a surface is destroyed, its WineD3DSurface is released,
266  * and the refcount of the DirectDraw interface is reduced by 1. If it has
267  * complex surfaces attached to it, then these surfaces are destroyed too,
268  * regardless of their refcount. If any surface being destroyed has another
269  * surface attached to it (with a "soft" attachment, not complex), then
270  * this surface is detached with DeleteAttachedSurface.
271  *
272  * When the surface is a texture, the WineD3DTexture is released.
273  * If the surface is the Direct3D render target, then the D3D
274  * capabilities of the WineD3DDevice are uninitialized, which causes the
275  * swapchain to be released.
276  *
277  * Returns:
278  *  The new refcount
279  *
280  *****************************************************************************/
281 static ULONG WINAPI
282 IDirectDrawSurfaceImpl_Release(IDirectDrawSurface7 *iface)
283 {
284     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
285     ULONG ref;
286     TRACE("(%p) : Releasing from %ld\n", This, This->ref);
287     ref = InterlockedDecrement(&This->ref);
288
289     if (ref == 0)
290     {
291
292         IDirectDrawSurfaceImpl *surf;
293         IDirectDrawImpl *ddraw;
294         IUnknown *ifaceToRelease = This->ifaceToRelease;
295
296         /* Complex attached surfaces are destroyed implicitely when the root is released */
297         if(This->first_complex != This)
298         {
299             WARN("(%p) Attempt to destroy a surface that is attached to a complex root %p\n", This, This->first_complex);
300             return ref;
301         }
302         ddraw = This->ddraw;
303
304         /* If it's a texture, destroy the WineD3DTexture.
305          * WineD3D will destroy the IParent interfaces
306          * of the sublevels, which destroys the WineD3DSurfaces.
307          * Set the surfaces to NULL to avoid destroying them again later
308          */
309         if(This->wineD3DTexture)
310         {
311             IWineD3DTexture_Release(This->wineD3DTexture);
312         }
313         /* If it's the RenderTarget, destroy the d3ddevice */
314         else if( (ddraw->d3d_initialized) && (This == ddraw->d3d_target))
315         {
316             TRACE("(%p) Destroying the render target, uninitializing D3D\n", This);
317
318             /* Unset any index buffer, just to be sure */
319             IWineD3DDevice_SetIndices(ddraw->wineD3DDevice, NULL, 0);
320
321             if(IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice) != D3D_OK)
322             {
323                 /* Not good */
324                 ERR("(%p) Failed to uninit 3D\n", This);
325             }
326             else
327             {
328                 /* Free the d3d window if one was created */
329                 if(ddraw->d3d_window != 0)
330                 {
331                     TRACE(" (%p) Destroying the hidden render window %p\n", This, ddraw->d3d_window);
332                     DestroyWindow(ddraw->d3d_window);
333                     ddraw->d3d_window = 0;
334                 }
335                 /* Unset the pointers */
336             }
337
338             ddraw->d3d_initialized = FALSE;
339             ddraw->d3d_target = NULL;
340
341             /* Write a trace because D3D unloading was the reason for many
342              * crashes during development.
343              */
344             TRACE("(%p) D3D unloaded\n", This);
345         }
346         else if(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
347                                                      DDSCAPS_3DDEVICE       |
348                                                      DDSCAPS_TEXTURE        ) )
349         {
350             /* It's a render target, but no swapchain was created.
351              * The IParent interfaces have to be released manually.
352              * The same applies for textures without an
353              * IWineD3DTexture object attached
354              */
355             surf = This;
356             while(surf)
357             {
358                 IParent *Parent;
359
360                 IWineD3DSurface_GetParent(surf->WineD3DSurface,
361                                           (IUnknown **) &Parent);
362                 IParent_Release(Parent);  /* For the getParent */
363                 IParent_Release(Parent);  /* To release it */
364                 surf = surf->next_complex;
365             }
366         }
367
368         /* The refcount test shows that the palette is detached when the surface is destroyed */
369         IDirectDrawSurface7_SetPalette(ICOM_INTERFACE(This, IDirectDrawSurface7),
370                                                       NULL);
371
372         /* Loop through all complex attached surfaces,
373          * and destroy them
374          */
375         while( (surf = This->next_complex) )
376         {
377             This->next_complex = surf->next_complex;  /* Unchain it from the complex listing */
378             IDirectDrawSurfaceImpl_Destroy(surf);     /* Destroy it */
379         }
380
381         /* Destroy the surface.
382          */
383         IDirectDrawSurfaceImpl_Destroy(This);
384
385         /* Reduce the ddraw refcount */
386         IUnknown_Release(ifaceToRelease);
387     }
388
389     return ref;
390 }
391
392 /*****************************************************************************
393  * IDirectDrawSurface7::GetAttachedSurface
394  *
395  * Returns an attached surface with the requested caps. Surface attachment
396  * and complex surfaces are not clearly described by the MSDN or sdk,
397  * so this method is tricky and likely to contain problems.
398  * This implementation searches the complex chain first, then the
399  * attachment chain, and then it checks if the caps match to itself.
400  *
401  * The chains are searched from This down to the last surface in the chain,
402  * not from the first element in the chain. The first surface found is
403  * returned. The MSDN says that this method fails if more than one surface
404  * matches the caps, but apparently this is incorrect.
405  *
406  * The found surface is AddRef-ed before it is returned.
407  *
408  * Params:
409  *  Caps: Pointer to a DDCAPS2 structure describing the caps asked for
410  *  Surface: Address to store the found surface
411  *
412  * Returns:
413  *  DD_OK on success
414  *  DDERR_INVALIDPARAMS if Caps or Surface is NULL
415  *  DDERR_NOTFOUND if no surface was found
416  *
417  *****************************************************************************/
418 static HRESULT WINAPI
419 IDirectDrawSurfaceImpl_GetAttachedSurface(IDirectDrawSurface7 *iface,
420                                           DDSCAPS2 *Caps,
421                                           IDirectDrawSurface7 **Surface)
422 {
423     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
424     IDirectDrawSurfaceImpl *surf;
425     DDSCAPS2 our_caps;
426
427     TRACE("(%p)->(%p,%p)\n", This, Caps, Surface);
428
429     our_caps = *Caps;
430
431     if(This->version < 7)
432     {
433         /* Earlier dx apps put garbage into these members, clear them */
434         our_caps.dwCaps2 = 0;
435         our_caps.dwCaps3 = 0;
436         our_caps.dwCaps4 = 0;
437     }
438
439     TRACE("(%p): Looking for caps: %lx,%lx,%lx,%lx\n", This, our_caps.dwCaps, our_caps.dwCaps2, our_caps.dwCaps3, our_caps.dwCaps4); /* FIXME: Better debugging */
440
441     /* First, look at the complex chain */
442     surf = This;
443
444     while( (surf = surf->next_complex) )
445     {
446         if (TRACE_ON(ddraw))
447         {
448             TRACE("Surface: (%p) caps: %lx,%lx,%lx,%lx\n", surf,
449                    surf->surface_desc.ddsCaps.dwCaps,
450                    surf->surface_desc.ddsCaps.dwCaps2,
451                    surf->surface_desc.ddsCaps.dwCaps3,
452                    surf->surface_desc.ddsCaps.dwCaps4);
453         }
454
455         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
456             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
457
458             /* MSDN: "This method fails if more than one surface is attached
459              * that matches the capabilities requested."
460              *
461              * The mipmap demo of the DirectX7 sdk shows what to do here:
462              * apparently apps expect the first found surface to be returned.
463              */
464
465             TRACE("(%p): Returning surface %p\n", This, surf);
466             TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
467             *Surface = ICOM_INTERFACE(surf, IDirectDrawSurface7);
468             IDirectDrawSurface7_AddRef(*Surface);
469             return DD_OK;
470         }
471     }
472
473     /* Next, look at the attachment chain */
474     surf = This;
475
476     while( (surf = surf->next_attached) )
477     {
478         if (TRACE_ON(ddraw))
479         {
480             TRACE("Surface: (%p) caps: %lx,%lx,%lx,%lx\n", surf,
481                    surf->surface_desc.ddsCaps.dwCaps,
482                    surf->surface_desc.ddsCaps.dwCaps2,
483                    surf->surface_desc.ddsCaps.dwCaps3,
484                    surf->surface_desc.ddsCaps.dwCaps4);
485         }
486
487         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
488             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
489
490             /* MSDN: "This method fails if more than one surface is attached
491              * that matches the capabilities requested."
492              *
493              * The mipmap demo of the DirectX7 sdk shows what to do here:
494              * apparently apps expect the first found surface to be returned.
495              */
496
497             TRACE("(%p): Returning surface %p\n", This, surf);
498             *Surface = ICOM_INTERFACE(surf, IDirectDrawSurface7);
499             IDirectDrawSurface7_AddRef(*Surface);
500             return DD_OK;
501         }
502     }
503
504     /* Is this valid? */
505 #if 0
506     if (((This->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
507         ((This->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2) && 
508         This == This->first_complex)
509     {
510
511         TRACE("(%p): Returning surface %p\n", This, This);
512         *Surface = ICOM_INTERFACE(This, IDirectDrawSurface7);
513         IDirectDrawSurface7_AddRef(*Surface);
514         return DD_OK;
515     }
516 #endif
517
518     /* What to do here? Continue with the surface root?? */
519
520     TRACE("(%p) Didn't find a valid surface\n", This);
521     return DDERR_NOTFOUND;
522 }
523
524 /*****************************************************************************
525  * IDirectDrawSurface7::Lock
526  *
527  * Locks the surface and returns a pointer to the surface's memory
528  *
529  * Params:
530  *  Rect: Rectangle to lock. If NULL, the whole surface is locked
531  *  DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
532  *  Flags: Locking flags, e.g Read only or write only
533  *  h: An event handle that's not used and must be NULL
534  *
535  * Returns:
536  *  DD_OK on success
537  *  DDERR_INVALIDPARAMS if DDSD is NULL
538  *  For more details, see IWineD3DSurface::LockRect
539  *
540  *****************************************************************************/
541 static HRESULT WINAPI
542 IDirectDrawSurfaceImpl_Lock(IDirectDrawSurface7 *iface,
543                             RECT *Rect,
544                             DDSURFACEDESC2 *DDSD,
545                             DWORD Flags,
546                             HANDLE h)
547 {
548     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
549     WINED3DLOCKED_RECT LockedRect;
550     HRESULT hr;
551     TRACE("(%p)->(%p,%p,%lx,%p)\n", This, Rect, DDSD, Flags, h);
552
553     if(!DDSD)
554         return DDERR_INVALIDPARAMS;
555
556     /* Should I check for the handle to be NULL?
557      *
558      * The DDLOCK flags and the D3DLOCK flags are equal
559      * for the supported values. The others are ignored by WineD3D
560      */
561
562     /* Hmm. Anarchy online passes an uninitialized surface descriptor,
563      * that means it doesn't have dwSize set. Init it to some sane
564      * value
565      */
566     if(DDSD->dwSize <= sizeof(DDSURFACEDESC))
567     {
568         DDSD->dwSize = sizeof(DDSURFACEDESC);
569     }
570     else
571     {
572         DDSD->dwSize = sizeof(DDSURFACEDESC2);
573     }
574
575     DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
576     hr = IWineD3DSurface_LockRect(This->WineD3DSurface,
577                                   &LockedRect,
578                                   Rect,
579                                   Flags);
580     if(hr != D3D_OK) return hr;
581
582     /* Override the memory area and the pitch */
583     DDSD->dwFlags |= DDSD_LPSURFACE;
584     DDSD->lpSurface = LockedRect.pBits;
585     DDSD->dwFlags |= DDSD_PITCH;
586     DDSD->u1.lPitch = LockedRect.Pitch;
587
588     TRACE("locked surface returning description :\n");
589     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
590
591     return DD_OK;
592 }
593
594 /*****************************************************************************
595  * IDirectDrawSurface7::Unlock
596  *
597  * Unlocks an locked surface
598  *
599  * Params:
600  *  Rect: Not used by this implementation
601  *
602  * Returns:
603  *  D3D_OK on success
604  *  For more details, see IWineD3DSurface::UnlockRect
605  *
606  *****************************************************************************/
607 static HRESULT WINAPI
608 IDirectDrawSurfaceImpl_Unlock(IDirectDrawSurface7 *iface,
609                               RECT *pRect)
610 {
611     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
612     TRACE("(%p)->(%p)\n", This, pRect);
613
614     return IWineD3DSurface_UnlockRect(This->WineD3DSurface);
615 }
616
617 /*****************************************************************************
618  * IDirectDrawSurface7::Flip
619  *
620  * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
621  * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
622  * the flip target is passed to WineD3D, even if the app didn't specify one
623  *
624  * Params:
625  *  DestOverride: Specifies the surface that will become the new front
626  *                buffer. If NULL, the current back buffer is used
627  *  Flags: some DirectDraw flags, see include/ddraw.h
628  *
629  * Returns:
630  *  DD_OK on success
631  *  DDERR_NOTFLIPPABLE if no flip target could be found
632  *  DDERR_INVALIDOBJECT if the surface isn't a front buffer
633  *  For more details, see IWineD3DSurface::Flip
634  *
635  *****************************************************************************/
636 static HRESULT WINAPI
637 IDirectDrawSurfaceImpl_Flip(IDirectDrawSurface7 *iface,
638                             IDirectDrawSurface7 *DestOverride,
639                             DWORD Flags)
640 {
641     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
642     IDirectDrawSurfaceImpl *Override = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DestOverride);
643     IDirectDrawSurface7 *Override7;
644     HRESULT hr;
645     TRACE("(%p)->(%p,%lx)\n", This, DestOverride, Flags);
646
647     /* Flip has to be called from a front buffer
648      * What about overlay surfaces, AFAIK they can flip too?
649      */
650     if( !(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER) )
651         return DDERR_INVALIDOBJECT; /* Unckecked */
652
653     /* WineD3D doesn't keep track of attached surface, so find the target */
654     if(!Override)
655     {
656         DDSCAPS2 Caps;
657
658         memset(&Caps, 0, sizeof(Caps));
659         Caps.dwCaps |= DDSCAPS_BACKBUFFER;
660         hr = IDirectDrawSurface7_GetAttachedSurface(iface, &Caps, &Override7);
661         if(hr != DD_OK)
662         {
663             ERR("Can't find a flip target\n");
664             return DDERR_NOTFLIPPABLE; /* Unchecked */
665         }
666         Override = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Override7);
667
668         /* For the GetAttachedSurface */
669         IDirectDrawSurface7_Release(Override7);
670     }
671
672     return  IWineD3DSurface_Flip(This->WineD3DSurface,
673                                  Override->WineD3DSurface,
674                                  Flags);
675 }
676
677 /*****************************************************************************
678  * IDirectDrawSurface7::Blt
679  *
680  * Performs a blit on the surface
681  *
682  * Params:
683  *  DestRect: Destination rectangle, can be NULL
684  *  SrcSurface: Source surface, can be NULL
685  *  SrcRect: Source rectange, can be NULL
686  *  Flags: Blt flags
687  *  DDBltFx: Some extended blt parameters, connected to the flags
688  *
689  * Returns:
690  *  D3D_OK on success
691  *  See IWineD3DSurface::Blt for more details
692  *
693  *****************************************************************************/
694 static HRESULT WINAPI
695 IDirectDrawSurfaceImpl_Blt(IDirectDrawSurface7 *iface,
696                            RECT *DestRect,
697                            IDirectDrawSurface7 *SrcSurface,
698                            RECT *SrcRect,
699                            DWORD Flags,
700                            DDBLTFX *DDBltFx)
701 {
702     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
703     IDirectDrawSurfaceImpl *Src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, SrcSurface);
704     TRACE("(%p)->(%p,%p,%p,%lx,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
705
706     return IWineD3DSurface_Blt(This->WineD3DSurface,
707                                DestRect,
708                                Src ? Src->WineD3DSurface : NULL,
709                                SrcRect,
710                                Flags,
711                                DDBltFx);
712 }
713
714 /*****************************************************************************
715  * IDirectDrawSurface7::AddAttachedSurface
716  *
717  * Attaches a surface to another surface. Surface attachments are
718  * incorrectly described in the SDK and the MSDN, and this method
719  * is prone to bugs. The surface that is attached is AddRef-ed.
720  *
721  * The attachment list consists of a first surface (first_attached) and
722  * for each surface a pointer to the next attached surface (next_attached).
723  * For the first surface, and a surface that has no attachments
724  * first_attached points to the surface itself. A surface that has
725  * no successors in the chain has next_attached set to NULL.
726  *
727  * Newly attached surfaces are attached right after the root surface. The
728  * complex chains are handled separately in a similar chain, with
729  * first_complex and next_complex. If a surface is attached to a complex
730  * surface compound, it's attached to the surface that the app requested,
731  * not the complex root. See GetAttachedSurface for a description
732  * how surfaces are found.
733  *
734  * This is how the current implementation works, and it was coded by looking
735  * at the needs of the applications.
736  *
737  * So far only Z-Buffer attachments are tested, but there's no code yet
738  * to activate them. Mipmaps could be tricky to activate in WineD3D.
739  * Back buffers should work in 2D mode, but they are not tested.
740  * Rendering to the primary surface and switching between that and
741  * double buffering is not yet implemented in WineD3D, so for 3D it might
742  * have unexpected results.
743  *
744  * Params:
745  *  Attach: Surface to attach to iface
746  *
747  * Returns:
748  *  DD_OK on success
749  *  DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
750  *
751  *****************************************************************************/
752 static HRESULT WINAPI
753 IDirectDrawSurfaceImpl_AddAttachedSurface(IDirectDrawSurface7 *iface,
754                                           IDirectDrawSurface7 *Attach)
755 {
756     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
757     IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Attach);
758     TRACE("(%p)->(%p)\n", This, Surf);
759
760     /* Should I make sure to add it to the first complex surface? */
761
762     if(Surf == This)
763         return DDERR_CANNOTATTACHSURFACE; /* unchecked */
764
765     /* TODO MSDN: "You can attach only z-buffer surfaces with this method."
766      * But apparently backbuffers and mipmaps can be attached too. */
767
768     /* Set MIPMAPSUBLEVEL if this seems to be one */
769     if (This->surface_desc.ddsCaps.dwCaps &
770         Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
771     {
772         Surf->surface_desc.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
773         /* FIXME: we should probably also add to dwMipMapCount of this
774           * and all parent surfaces (update create_texture if you do) */
775     }
776
777     /* Check if the surface is already attached somewhere */
778     if( (Surf->next_attached != NULL) ||
779         (Surf->first_attached != Surf) )
780     {
781          ERR("(%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);
782         return DDERR_CANNOTATTACHSURFACE;
783     }
784
785     /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
786     Surf->next_attached = This->next_attached;
787     Surf->first_attached = This->first_attached;
788     This->next_attached = Surf;
789
790     /* MSDN: 
791      * "This method increments the reference count of the surface being attached."
792      */
793     IDirectDrawSurface7_AddRef(Attach);
794     return DD_OK;
795 }
796
797 /*****************************************************************************
798  * IDirectDrawSurface7::DeleteAttachedSurface
799  *
800  * Removes a surface from the attachment chain. The surface's refcount
801  * is decreased by one after it has been removed
802  *
803  * Params:
804  *  Flags: Some flags, not used by this implementation
805  *  Attach: Surface to detach
806  *
807  * Returns:
808  *  DD_OK on success
809  *  DDERR_SURFACENOTATTACHED if the surface isn't attached to
810  *
811  *****************************************************************************/
812 static HRESULT WINAPI
813 IDirectDrawSurfaceImpl_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
814                                              DWORD Flags,
815                                              IDirectDrawSurface7 *Attach)
816 {
817     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
818     IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Attach);
819     IDirectDrawSurfaceImpl *Prev = This;
820     TRACE("(%p)->(%08lx,%p)\n", This, Flags, Surf);
821
822     if (!Surf || (Surf->first_attached != This) || (Surf == This) )
823         return DDERR_SURFACENOTATTACHED; /* unchecked */
824
825     /* Remove MIPMAPSUBLEVEL if this seemed to be one */
826     if (This->surface_desc.ddsCaps.dwCaps &
827         Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
828     {
829         Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
830         /* FIXME: we should probably also subtract from dwMipMapCount of this
831          * and all parent surfaces */
832     }
833
834     /* Find the predecessor of the detached surface */
835     while(Prev)
836     {
837         if(Prev->next_attached == Surf) break;
838         Prev = Prev->next_attached;
839     }
840
841     /* There must be a surface, otherwise there's a bug */
842     assert(Prev != NULL);
843
844     /* Unchain the surface */
845     Prev->next_attached = Surf->next_attached;
846     Surf->next_attached = NULL;
847     Surf->first_attached = Surf;
848
849     IDirectDrawSurface7_Release(Attach);
850     return DD_OK;
851 }
852
853 /*****************************************************************************
854  * IDirectDrawSurface7::AddOverlayDirtyRect
855  *
856  * "This method is not currently implemented"
857  *
858  * Params:
859  *  Rect: ?
860  *
861  * Returns:
862  *  DDERR_UNSUPPORTED
863  *
864  *****************************************************************************/
865 static HRESULT WINAPI
866 IDirectDrawSurfaceImpl_AddOverlayDirtyRect(IDirectDrawSurface7 *iface,
867                                            LPRECT Rect)
868 {
869     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
870     TRACE("(%p)->(%p)\n",This,Rect);
871
872     /* MSDN says it's not implemented. I could forward it to WineD3D, 
873      * then we'd implement it, but I don't think that's a good idea
874      * (Stefan Dösinger)
875      */
876 #if 0
877     return IWineD3DSurface_AddOverlayDirtyRect(This->WineD3DSurface, pRect);
878 #endif
879     return DDERR_UNSUPPORTED; /* unchecked */
880 }
881
882 /*****************************************************************************
883  * IDirectDrawSurface7::GetDC
884  *
885  * Returns a GDI device context for the surface
886  *
887  * Params:
888  *  hdc: Address of a HDC variable to store the dc to
889  *
890  * Returns:
891  *  DD_OK on success
892  *  DDERR_INVALIDPARAMS if hdc is NULL
893  *  For details, see IWineD3DSurface::GetDC
894  *
895  *****************************************************************************/
896 static HRESULT WINAPI
897 IDirectDrawSurfaceImpl_GetDC(IDirectDrawSurface7 *iface,
898                              HDC *hdc)
899 {
900     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
901     TRACE("(%p)->(%p): Relay\n", This, hdc);
902
903     if(!hdc)
904         return DDERR_INVALIDPARAMS;
905
906     return IWineD3DSurface_GetDC(This->WineD3DSurface,
907                                  hdc);
908 }
909
910 /*****************************************************************************
911  * IDirectDrawSurface7::ReleaseDC
912  *
913  * Releases the DC that was constructed with GetDC
914  *
915  * Params:
916  *  hdc: HDC to release
917  *
918  * Returns:
919  *  DD_OK on success
920  *  For more details, see IWineD3DSurface::ReleaseDC
921  *
922  *****************************************************************************/
923 static HRESULT WINAPI
924 IDirectDrawSurfaceImpl_ReleaseDC(IDirectDrawSurface7 *iface,
925                                  HDC hdc)
926 {
927     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
928     TRACE("(%p)->(%p): Relay\n", This, hdc);
929
930     return IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
931 }
932
933 /*****************************************************************************
934  * IDirectDrawSurface7::GetCaps
935  *
936  * Returns the surface's caps
937  *
938  * Params:
939  *  Caps: Address to write the caps to
940  *
941  * Returns:
942  *  DD_OK on success
943  *  DDERR_INVALIDPARAMS if Caps is NULL
944  *
945  *****************************************************************************/
946 static HRESULT WINAPI
947 IDirectDrawSurfaceImpl_GetCaps(IDirectDrawSurface7 *iface,
948                                DDSCAPS2 *Caps)
949 {
950     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
951     TRACE("(%p)->(%p)\n",This,Caps);
952
953     if(!Caps)
954         return DDERR_INVALIDPARAMS;
955
956     *Caps = This->surface_desc.ddsCaps;
957     return DD_OK;
958 }
959
960 /*****************************************************************************
961  * IDirectDrawSurface7::SetPriority
962  *
963  * Sets a texture priority for managed textures.
964  *
965  * Params:
966  *  Priority: The new priority
967  *
968  * Returns:
969  *  DD_OK on success
970  *  For more details, see IWineD3DSurface::SetPriority
971  *
972  *****************************************************************************/
973 static HRESULT WINAPI
974 IDirectDrawSurfaceImpl_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
975 {
976     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
977     TRACE("(%p)->(%ld): Relay!\n",This,Priority);
978
979     return IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
980 }
981
982 /*****************************************************************************
983  * IDirectDrawSurface7::GetPriority
984  *
985  * Returns the surface's priority
986  *
987  * Params:
988  *  Priority: Address of a variable to write the priority to
989  *
990  * Returns:
991  *  D3D_OK on success
992  *  DDERR_INVALIDPARAMS if Priority == NULL
993  *  For more details, see IWineD3DSurface::GetPriority
994  *
995  *****************************************************************************/
996 static HRESULT WINAPI
997 IDirectDrawSurfaceImpl_GetPriority(IDirectDrawSurface7 *iface,
998                                    DWORD *Priority)
999 {
1000     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1001     TRACE("(%p)->(%p): Relay\n",This,Priority);
1002
1003     if(!Priority)
1004         return DDERR_INVALIDPARAMS;
1005
1006     *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1007     return DD_OK;
1008 }
1009
1010 /*****************************************************************************
1011  * IDirectDrawSurface7::SetPrivateData
1012  *
1013  * Stores some data in the surface that is intended for the application's
1014  * use.
1015  *
1016  * Params:
1017  *  tag: GUID that identifies the data
1018  *  Data: Pointer to the private data
1019  *  Size: Size of the private data
1020  *  Flags: Some flags
1021  *
1022  * Returns:
1023  *  D3D_OK on success
1024  *  For more details, see IWineD3DSurface::SetPrivateData
1025  *
1026  *****************************************************************************/
1027 static HRESULT WINAPI
1028 IDirectDrawSurfaceImpl_SetPrivateData(IDirectDrawSurface7 *iface,
1029                                       REFGUID tag,
1030                                       void *Data,
1031                                       DWORD Size,
1032                                       DWORD Flags)
1033 {
1034     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1035     TRACE("(%p)->(%s,%p,%ld,%lx): Relay\n", This, debugstr_guid(tag), Data, Size, Flags);
1036
1037     return IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1038                                           tag,
1039                                           Data,
1040                                           Size,
1041                                           Flags);
1042 }
1043
1044 /*****************************************************************************
1045  * IDirectDrawSurface7::GetPrivateData
1046  *
1047  * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1048  *
1049  * Params:
1050  *  tag: GUID of the data to return
1051  *  Data: Address where to write the data to
1052  *  Size: Size of the buffer at Data
1053  *
1054  * Returns:
1055  *  DD_OK on success
1056  *  DDERR_INVALIDPARAMS if Data is NULL
1057  *  For more details, see IWineD3DSurface::GetPrivateData
1058  *
1059  *****************************************************************************/
1060 static HRESULT WINAPI
1061 IDirectDrawSurfaceImpl_GetPrivateData(IDirectDrawSurface7 *iface,
1062                                       REFGUID tag,
1063                                       void *Data,
1064                                       DWORD *Size)
1065 {
1066     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1067     TRACE("(%p)->(%s,%p,%p): Relay\n", This, debugstr_guid(tag), Data, Size);
1068
1069     if(!Data)
1070         return DDERR_INVALIDPARAMS;
1071
1072     return IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1073                                           tag,
1074                                           Data,
1075                                           Size);
1076 }
1077
1078 /*****************************************************************************
1079  * IDirectDrawSurface7::FreePrivateData
1080  *
1081  * Frees private data stored in the surface
1082  *
1083  * Params:
1084  *  tag: Tag of the data to free
1085  *
1086  * Returns:
1087  *  D3D_OK on success
1088  *  For more details, see IWineD3DSurface::FreePrivateData
1089  *
1090  *****************************************************************************/
1091 static HRESULT WINAPI
1092 IDirectDrawSurfaceImpl_FreePrivateData(IDirectDrawSurface7 *iface,
1093                                        REFGUID tag)
1094 {
1095     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1096     TRACE("(%p)->(%s): Relay\n", This, debugstr_guid(tag));
1097
1098     return IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1099 }
1100
1101 /*****************************************************************************
1102  * IDirectDrawSurface7::PageLock
1103  *
1104  * Prevents a sysmem surface from being paged out
1105  *
1106  * Params:
1107  *  Flags: Not used, must be 0(unchecked)
1108  *
1109  * Returns:
1110  *  DD_OK, because it's a stub
1111  *
1112  *****************************************************************************/
1113 static HRESULT WINAPI
1114 IDirectDrawSurfaceImpl_PageLock(IDirectDrawSurface7 *iface,
1115                                 DWORD Flags)
1116 {
1117     TRACE("(%p)->(%lx)\n", iface, Flags);
1118
1119     /* This is Windows memory management related - we don't need this */
1120     return DD_OK;
1121 }
1122
1123 /*****************************************************************************
1124  * IDirectDrawSurface7::PageUnlock
1125  *
1126  * Allows a sysmem surface to be paged out
1127  *
1128  * Params:
1129  *  Flags: Not used, must be 0(unckeched)
1130  *
1131  * Returns:
1132  *  DD_OK, because it's a stub
1133  *
1134  *****************************************************************************/
1135 static HRESULT WINAPI
1136 IDirectDrawSurfaceImpl_PageUnlock(IDirectDrawSurface7 *iface,
1137                                   DWORD Flags)
1138 {
1139     TRACE("(%p)->(%lx)\n", iface, Flags);
1140
1141     return DD_OK;
1142 }
1143
1144 /*****************************************************************************
1145  * IDirectDrawSurface7::BltBatch
1146  *
1147  * An unimplemented function
1148  *
1149  * Params:
1150  *  ?
1151  *
1152  * Returns:
1153  *  DDERR_UNSUPPORTED
1154  *
1155  *****************************************************************************/
1156 static HRESULT WINAPI IDirectDrawSurfaceImpl_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1157 {
1158     TRACE("(%p)->(%p,%ld,%08lx)\n",iface,Batch,Count,Flags);
1159
1160     /* MSDN: "not currently implemented" */
1161     return DDERR_UNSUPPORTED;
1162 }
1163
1164 /*****************************************************************************
1165  * IDirectDrawSurface7::EnumAttachedSurfaces
1166  *
1167  * Enumerates all surfaces attached to this surface
1168  *
1169  * Params:
1170  *  context: Pointer to pass unmodified to the callback
1171  *  cb: Callback function to call for each surface
1172  *
1173  * Returns:
1174  *  DD_OK on success
1175  *  DDERR_INVALIDPARAMS if cb is NULL
1176  *
1177  *****************************************************************************/
1178 static HRESULT WINAPI
1179 IDirectDrawSurfaceImpl_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1180                                             void *context,
1181                                             LPDDENUMSURFACESCALLBACK7 cb)
1182 {
1183     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1184     IDirectDrawSurfaceImpl *surf;
1185     DDSURFACEDESC2 desc;
1186
1187     /* Attached surfaces aren't handled in WineD3D */
1188     TRACE("(%p)->(%p,%p)\n",This,context,cb);
1189
1190     if(!cb)
1191         return DDERR_INVALIDPARAMS;
1192
1193     for (surf = This->next_complex; surf != NULL; surf = surf->next_complex)
1194     {
1195         IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
1196         desc = surf->surface_desc;
1197         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1198         if (cb(ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, context) == DDENUMRET_CANCEL)
1199             return DD_OK;
1200     }
1201
1202     for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1203     {
1204         IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
1205         desc = surf->surface_desc;
1206         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1207         if (cb( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, context) == DDENUMRET_CANCEL)
1208             return DD_OK;
1209     }
1210
1211     TRACE(" end of enumeration.\n");
1212
1213     return DD_OK;
1214 }
1215
1216 /*****************************************************************************
1217  * IDirectDrawSurface7::EnumOverlayZOrders
1218  *
1219  * "Enumerates the overlay surfaces on the specified destination"
1220  *
1221  * Params:
1222  *  Flags: DDENUMOVERLAYZ_BACKTOFRONT  or DDENUMOVERLAYZ_FRONTTOBACK
1223  *  context: context to pass back to the callback
1224  *  cb: callback function to call for each enumerated surface
1225  *
1226  * Returns:
1227  *  DD_OK, because it's a stub
1228  *
1229  *****************************************************************************/
1230 static HRESULT WINAPI
1231 IDirectDrawSurfaceImpl_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1232                                           DWORD Flags,
1233                                           void *context,
1234                                           LPDDENUMSURFACESCALLBACK7 cb)
1235 {
1236      FIXME("(%p)->(%lx,%p,%p): Stub!\n", iface, Flags, context, cb);
1237
1238     return DD_OK;
1239 }
1240
1241 /*****************************************************************************
1242  * IDirectDrawSurface7::GetBltStatus
1243  *
1244  * Returns the blitting status
1245  *
1246  * Params:
1247  *  Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1248  *
1249  * Returns:
1250  *  See IWineD3DSurface::Blt
1251  *
1252  *****************************************************************************/
1253 static HRESULT WINAPI
1254 IDirectDrawSurfaceImpl_GetBltStatus(IDirectDrawSurface7 *iface,
1255                                     DWORD Flags)
1256 {
1257     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1258     TRACE("(%p)->(%lx): Relay\n", This, Flags);
1259
1260     return IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1261 }
1262
1263 /*****************************************************************************
1264  * IDirectDrawSurface7::GetColorKey
1265  *
1266  * Returns the color key assigned to the surface
1267  *
1268  * Params:
1269  *  Flags: Some flags
1270  *  CKey: Address to store the key to
1271  *
1272  * Returns:
1273  *  DD_OK on success
1274  *  DDERR_INVALIDPARAMS if CKey is NULL
1275  *
1276  *****************************************************************************/
1277 static HRESULT WINAPI
1278 IDirectDrawSurfaceImpl_GetColorKey(IDirectDrawSurface7 *iface,
1279                                    DWORD Flags,
1280                                    DDCOLORKEY *CKey)
1281 {
1282     /* There is a DDERR_NOCOLORKEY error, but how do we know if a color key
1283      * isn't there? That's like saying that an int isn't there. (Which MS
1284      * has done in other docs.) */
1285
1286     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1287
1288     if(!CKey)
1289         return DDERR_INVALIDPARAMS;
1290
1291     switch (Flags)
1292     {
1293     case DDCKEY_DESTBLT:
1294         *CKey = This->surface_desc.ddckCKDestBlt;
1295         break;
1296
1297     case DDCKEY_DESTOVERLAY:
1298         *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1299         break;
1300
1301     case DDCKEY_SRCBLT:
1302         *CKey = This->surface_desc.ddckCKSrcBlt;
1303         break;
1304
1305     case DDCKEY_SRCOVERLAY:
1306         *CKey = This->surface_desc.ddckCKSrcOverlay;
1307         break;
1308
1309     default:
1310         return DDERR_INVALIDPARAMS;
1311     }
1312
1313     return DD_OK;
1314 }
1315
1316 /*****************************************************************************
1317  * IDirectDrawSurface7::GetFlipStatus
1318  *
1319  * Returns the flipping status of the surface
1320  *
1321  * Params:
1322  *  Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1323  *
1324  * Returns:
1325  *  See IWineD3DSurface::GetFlipStatus
1326  *
1327  *****************************************************************************/
1328 static HRESULT WINAPI
1329 IDirectDrawSurfaceImpl_GetFlipStatus(IDirectDrawSurface7 *iface,
1330                                      DWORD Flags)
1331 {
1332     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1333     TRACE("(%p)->(%lx): Relay\n", This, Flags);
1334
1335     return IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1336 }
1337
1338 /*****************************************************************************
1339  * IDirectDrawSurface7::GetOverlayPosition
1340  *
1341  * Returns the display coordinates of a visible and active overlay surface
1342  *
1343  * Params:
1344  *  X
1345  *  Y
1346  *
1347  * Returns:
1348  *  DDERR_NOTAOVERLAYSURFACE, because it's a stub
1349  *****************************************************************************/
1350 static HRESULT WINAPI
1351 IDirectDrawSurfaceImpl_GetOverlayPosition(IDirectDrawSurface7 *iface,
1352                                           LONG *X,
1353                                           LONG *Y) {
1354     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1355     TRACE("(%p)->(%p,%p): Relay\n", This, X, Y);
1356
1357     return IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1358                                               X,
1359                                               Y);
1360 }
1361
1362 /*****************************************************************************
1363  * IDirectDrawSurface7::GetPixelFormat
1364  *
1365  * Returns the pixel format of the Surface
1366  *
1367  * Params:
1368  *  PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1369  *               format should be written
1370  *
1371  * Returns:
1372  *  DD_OK on success
1373  *  DDERR_INVALIDPARAMS if PixelFormat is NULL
1374  *
1375  *****************************************************************************/
1376 static HRESULT WINAPI
1377 IDirectDrawSurfaceImpl_GetPixelFormat(IDirectDrawSurface7 *iface,
1378                                       DDPIXELFORMAT *PixelFormat)
1379 {
1380     /* What is DDERR_INVALIDSURFACETYPE for here? */
1381     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1382     TRACE("(%p)->(%p)\n",This,PixelFormat);
1383
1384     if(!PixelFormat)
1385         return DDERR_INVALIDPARAMS;
1386
1387     DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1388
1389     return DD_OK;
1390 }
1391
1392
1393 /*****************************************************************************
1394  * IDirectDrawSurface7::GetSurfaceDesc
1395  *
1396  * Returns the description of this surface
1397  *
1398  * Params:
1399  *  DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1400  *        surface desc
1401  *
1402  * Returns:
1403  *  DD_OK on success
1404  *  DDERR_INVALIDPARAMS if DDSD is NULL
1405  *
1406  *****************************************************************************/
1407 static HRESULT WINAPI
1408 IDirectDrawSurfaceImpl_GetSurfaceDesc(IDirectDrawSurface7 *iface,
1409                                       DDSURFACEDESC2 *DDSD)
1410 {
1411     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1412
1413     TRACE("(%p)->(%p)\n",This,DDSD);
1414
1415     if(!DDSD)
1416         return DDERR_INVALIDPARAMS;
1417
1418     if ((DDSD->dwSize < sizeof(DDSURFACEDESC)) ||
1419         (DDSD->dwSize > sizeof(DDSURFACEDESC2)))
1420     {
1421         ERR("Impossible/Strange struct size %ld.\n",DDSD->dwSize);
1422         return DDERR_GENERIC;
1423     }
1424
1425     DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1426     TRACE("Returning surface desc:\n");
1427     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1428
1429     return DD_OK;
1430 }
1431
1432 /*****************************************************************************
1433  * IDirectDrawSurface7::Initialize
1434  *
1435  * Initializes the surface. This is a no-op in Wine
1436  *
1437  * Params:
1438  *  DD: Pointer to an DirectDraw interface
1439  *  DDSD: Surface description for initialization
1440  *
1441  * Returns:
1442  *  DDERR_ALREADYINITIALIZED
1443  *
1444  *****************************************************************************/
1445 static HRESULT WINAPI
1446 IDirectDrawSurfaceImpl_Initialize(IDirectDrawSurface7 *iface,
1447                                   IDirectDraw *DD,
1448                                   DDSURFACEDESC2 *DDSD)
1449 {
1450     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1451     IDirectDrawImpl *ddimpl = ICOM_OBJECT(IDirectDrawImpl, IDirectDraw, DD);
1452     TRACE("(%p)->(%p,%p)\n",This,ddimpl,DDSD);
1453
1454     return DDERR_ALREADYINITIALIZED;
1455 }
1456
1457 /*****************************************************************************
1458  * IDirectDrawSurface7::IsLost
1459  *
1460  * Checks if the surface is lost
1461  *
1462  * Returns:
1463  *  DD_OK, if the surface is useable
1464  *  DDERR_ISLOST if the surface is lost
1465  *  See IWineD3DSurface::IsLost for more details
1466  *
1467  *****************************************************************************/
1468 static HRESULT WINAPI
1469 IDirectDrawSurfaceImpl_IsLost(IDirectDrawSurface7 *iface)
1470 {
1471     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1472     TRACE("(%p)\n", This);
1473
1474     /* We lose the surface if the implementation was changed */
1475     if(This->ImplType != This->ddraw->ImplType)
1476     {
1477         /* But this shouldn't happen. When we change the implementation,
1478          * all surfaces are re-created automatically, and their content
1479          * is copied
1480          */
1481         ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1482         return DDERR_SURFACELOST;
1483     }
1484
1485     return IWineD3DSurface_IsLost(This->WineD3DSurface);
1486 }
1487
1488 /*****************************************************************************
1489  * IDirectDrawSurface7::Restore
1490  *
1491  * Restores a lost surface. This makes the surface usable again, but
1492  * doesn't reload its old contents
1493  *
1494  * Returns:
1495  *  DD_OK on success
1496  *  See IWineD3DSurface::Restore for more details
1497  *
1498  *****************************************************************************/
1499 static HRESULT WINAPI
1500 IDirectDrawSurfaceImpl_Restore(IDirectDrawSurface7 *iface)
1501 {
1502     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1503     TRACE("(%p)\n", This);
1504
1505     if(This->ImplType != This->ddraw->ImplType)
1506     {
1507         /* Call the recreation callback. Make sure to AddRef first */
1508         IDirectDrawSurface_AddRef(iface);
1509         IDirectDrawImpl_RecreateSurfacesCallback(iface,
1510                                                  &This->surface_desc,
1511                                                  NULL /* Not needed */);
1512     }
1513     return IWineD3DSurface_Restore(This->WineD3DSurface);
1514 }
1515
1516 /*****************************************************************************
1517  * IDirectDrawSurface7::SetOverlayPosition
1518  *
1519  * Changes the display coordinates of an overlay surface
1520  *
1521  * Params:
1522  *  X:
1523  *  Y:
1524  *
1525  * Returns:
1526  *   DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
1527  *****************************************************************************/
1528 static HRESULT WINAPI
1529 IDirectDrawSurfaceImpl_SetOverlayPosition(IDirectDrawSurface7 *iface,
1530                                           LONG X,
1531                                           LONG Y)
1532 {
1533     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1534     TRACE("(%p)->(%ld,%ld): Relay\n", This, X, Y);
1535
1536     return IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
1537                                               X,
1538                                               Y);
1539 }
1540
1541 /*****************************************************************************
1542  * IDirectDrawSurface7::UpdateOverlay
1543  *
1544  * Modifies the attributes of an overlay surface.
1545  *
1546  * Params:
1547  *  SrcRect: The section of the source being used for the overlay
1548  *  DstSurface: Address of the surface that is overlaid
1549  *  DstRect: Place of the overlay
1550  *  Flags: some DDOVER_* flags
1551  *
1552  * Returns:
1553  *  DDERR_UNSUPPORTED, because we don't support overlays
1554  *
1555  *****************************************************************************/
1556 static HRESULT WINAPI
1557 IDirectDrawSurfaceImpl_UpdateOverlay(IDirectDrawSurface7 *iface,
1558                                      LPRECT SrcRect,
1559                                      IDirectDrawSurface7 *DstSurface,
1560                                      LPRECT DstRect,
1561                                      DWORD Flags,
1562                                      LPDDOVERLAYFX FX)
1563 {
1564     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1565     IDirectDrawSurfaceImpl *Dst = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DstSurface);
1566     FIXME("(%p)->(%p,%p,%p,%lx,%p): Stub!\n", This, SrcRect, Dst, DstRect, Flags, FX);
1567     return DDERR_UNSUPPORTED;
1568 }
1569
1570 /*****************************************************************************
1571  * IDirectDrawSurface7::UpdateOverlayDisplay
1572  *
1573  * The DX7 sdk says that it's not implemented
1574  *
1575  * Params:
1576  *  Flags: ?
1577  *
1578  * Returns: DDERR_UNSUPPORTED, because we don't support overlays
1579  *
1580  *****************************************************************************/
1581 static HRESULT WINAPI
1582 IDirectDrawSurfaceImpl_UpdateOverlayDisplay(IDirectDrawSurface7 *iface,
1583                                             DWORD Flags)
1584 {
1585     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1586     TRACE("(%p)->(%lx)\n", This, Flags);
1587     return DDERR_UNSUPPORTED;
1588 }
1589
1590 /*****************************************************************************
1591  * IDirectDrawSurface7::UpdateOverlayZOrder
1592  *
1593  * Sets an overlay's Z order
1594  *
1595  * Params:
1596  *  Flags: DDOVERZ_* flags
1597  *  DDSRef: Defines the relative position in the overlay chain
1598  *
1599  * Returns:
1600  *  DDERR_NOTOVERLAYSURFACE, because we don't support overlays
1601  *
1602  *****************************************************************************/
1603 static HRESULT WINAPI
1604 IDirectDrawSurfaceImpl_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
1605                                            DWORD Flags,
1606                                            IDirectDrawSurface7 *DDSRef)
1607 {
1608     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1609     IDirectDrawSurfaceImpl *Ref = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DDSRef);
1610
1611     TRACE("(%p)->(%lx,%p): Relay\n", This, Flags, Ref);
1612     return IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
1613                                                Flags,
1614                                                Ref ? Ref->WineD3DSurface : NULL);
1615 }
1616
1617 /*****************************************************************************
1618  * IDirectDrawSurface7::GetDDInterface
1619  *
1620  * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
1621  * surface belongs to
1622  *
1623  * Params:
1624  *  DD: Address to write the interface pointer to
1625  *
1626  * Returns:
1627  *  DD_OK on success
1628  *  DDERR_INVALIDPARAMS if DD is NULL
1629  *
1630  *****************************************************************************/
1631 static HRESULT WINAPI
1632 IDirectDrawSurfaceImpl_GetDDInterface(IDirectDrawSurface7 *iface,
1633                                       void **DD)
1634 {
1635     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1636
1637     /* It is not quite correct to use the same lpVtable for the different
1638      * IDirectDrawSurface versions because the GetDDInterface return different interfaces
1639      */
1640     FIXME("(%p)->(%p)\n",This,DD);
1641
1642     if(!DD)
1643         return DDERR_INVALIDPARAMS;
1644
1645     switch(This->version)
1646     {
1647         case 7:
1648             *((IDirectDraw7 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw7);
1649             IDirectDraw7_AddRef(*(IDirectDraw7 **) DD);
1650             break;
1651
1652         case 4:
1653             *((IDirectDraw4 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw4);
1654             IDirectDraw4_AddRef(*(IDirectDraw4 **) DD);
1655
1656         case 2:
1657         case 1:
1658             *((IDirectDraw **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw);
1659             IDirectDraw_AddRef( *(IDirectDraw **) DD);
1660             break;
1661
1662     }
1663
1664     return DD_OK;
1665 }
1666
1667 /* This seems also windows implementation specific - I don't think WineD3D needs this */
1668 static HRESULT WINAPI IDirectDrawSurfaceImpl_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
1669 {
1670     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1671     volatile IDirectDrawSurfaceImpl* vThis = This;
1672
1673     TRACE("(%p)\n",This);
1674     /* A uniqueness value of 0 is apparently special.
1675     * This needs to be checked. */
1676     while (1) {
1677         DWORD old_uniqueness_value = vThis->uniqueness_value;
1678         DWORD new_uniqueness_value = old_uniqueness_value+1;
1679
1680         if (old_uniqueness_value == 0) break;
1681         if (new_uniqueness_value == 0) new_uniqueness_value = 1;
1682
1683         if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
1684                                       old_uniqueness_value,
1685                                       new_uniqueness_value)
1686             == old_uniqueness_value)
1687             break;
1688     }
1689
1690     return DD_OK;
1691 }
1692
1693 static HRESULT WINAPI IDirectDrawSurfaceImpl_GetUniquenessValue(IDirectDrawSurface7 *iface, LPDWORD pValue)
1694 {
1695     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1696
1697     TRACE("(%p)->(%p)\n",This,pValue);
1698     *pValue = This->uniqueness_value;
1699     return DD_OK;
1700 }
1701
1702 /*****************************************************************************
1703  * IDirectDrawSurface7::SetLOD
1704  *
1705  * Sets the level of detail of a texture
1706  *
1707  * Params:
1708  *  MaxLOD: LOD to set
1709  *
1710  * Returns:
1711  *  DD_OK on success
1712  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
1713  *
1714  *****************************************************************************/
1715 static HRESULT WINAPI
1716 IDirectDrawSurfaceImpl_SetLOD(IDirectDrawSurface7 *iface,
1717                               DWORD MaxLOD)
1718 {
1719     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1720     TRACE("(%p)->(%ld)\n", This, MaxLOD);
1721
1722     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
1723         return DDERR_INVALIDOBJECT;
1724
1725     if(!This->wineD3DTexture)
1726     {
1727         ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
1728         return DDERR_INVALIDOBJECT;
1729     }
1730
1731     return IWineD3DTexture_SetLOD(This->wineD3DTexture,
1732                                   MaxLOD);
1733 }
1734
1735 /*****************************************************************************
1736  * IDirectDrawSurface7::GetLOD
1737  *
1738  * Returns the level of detail of a Direct3D texture
1739  *
1740  * Params:
1741  *  MaxLOD: Address to write the LOD to
1742  *
1743  * Returns:
1744  *  DD_OK on success
1745  *  DDERR_INVALIDPARAMS if MaxLOD is NULL
1746  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
1747  *
1748  *****************************************************************************/
1749 static HRESULT WINAPI
1750 IDirectDrawSurfaceImpl_GetLOD(IDirectDrawSurface7 *iface,
1751                               DWORD *MaxLOD)
1752 {
1753     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1754     TRACE("(%p)->(%p)\n", This, MaxLOD);
1755
1756     if(!MaxLOD)
1757         return DDERR_INVALIDPARAMS;
1758
1759     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
1760         return DDERR_INVALIDOBJECT;
1761
1762     *MaxLOD = IWineD3DTexture_GetLOD(This->wineD3DTexture);
1763     return DD_OK;
1764 }
1765
1766 /*****************************************************************************
1767  * IDirectDrawSurface7::BltFast
1768  *
1769  * Performs a fast Blit.
1770  *
1771  * Params:
1772  *  dstx: The x coordinate to blit to on the destination
1773  *  dsty: The y coordinate to blit to on the destination
1774  *  Source: The source surface
1775  *  rsrc: The source rectangle
1776  *  trans: Type of transfer. Some DDBLTFAST_* flags
1777  *
1778  * Returns:
1779  *  DD_OK on success
1780  *  For more details, see IWineD3DSurface::BltFast
1781  *
1782  *****************************************************************************/
1783 static HRESULT WINAPI
1784 IDirectDrawSurfaceImpl_BltFast(IDirectDrawSurface7 *iface,
1785                                DWORD dstx,
1786                                DWORD dsty,
1787                                IDirectDrawSurface7 *Source,
1788                                RECT *rsrc,
1789                                DWORD trans)
1790 {
1791     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1792     IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Source);
1793     TRACE("(%p)->(%ld,%ld,%p,%p,%ld): Relay\n", This, dstx, dsty, Source, rsrc, trans);
1794
1795     return IWineD3DSurface_BltFast(This->WineD3DSurface,
1796                                    dstx, dsty,
1797                                    src ? src->WineD3DSurface : NULL,
1798                                    rsrc,
1799                                    trans);
1800 }
1801
1802 /*****************************************************************************
1803  * IDirectDrawSurface7::GetClipper
1804  *
1805  * Returns the IDirectDrawClipper interface of the clipper assigned to this
1806  * surface
1807  *
1808  * Params:
1809  *  Clipper: Address to store the interface pointer at
1810  *
1811  * Returns:
1812  *  DD_OK on success
1813  *  DDERR_INVALIDPARAMS if Clipper is NULL
1814  *  DDERR_NOCLIPPERATTACHED if there's no clipper attached
1815  *
1816  *****************************************************************************/
1817 static HRESULT WINAPI
1818 IDirectDrawSurfaceImpl_GetClipper(IDirectDrawSurface7 *iface,
1819                                   IDirectDrawClipper **Clipper)
1820 {
1821     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1822     TRACE("(%p)->(%p)\n", This, Clipper);
1823
1824     if(!Clipper)
1825         return DDERR_INVALIDPARAMS;
1826
1827     if(This->clipper == NULL)
1828         return DDERR_NOCLIPPERATTACHED;
1829
1830     *Clipper = ICOM_INTERFACE(This->clipper, IDirectDrawClipper);
1831     IDirectDrawClipper_AddRef(*Clipper);
1832     return DD_OK;
1833 }
1834
1835 /*****************************************************************************
1836  * IDirectDrawSurface7::SetClipper
1837  *
1838  * Sets a clipper for the surface
1839  *
1840  * Params:
1841  *  Clipper: IDirectDrawClipper interface of the clipper to set
1842  *
1843  * Returns:
1844  *  DD_OK on success
1845  *
1846  *****************************************************************************/
1847 static HRESULT WINAPI
1848 IDirectDrawSurfaceImpl_SetClipper(IDirectDrawSurface7 *iface,
1849                                   IDirectDrawClipper *Clipper)
1850 {
1851     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1852
1853     TRACE("(%p)->(%p)\n",This,Clipper);
1854     if (ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper) == This->clipper)
1855         return DD_OK;
1856
1857     if (This->clipper != NULL)
1858         IDirectDrawClipper_Release(ICOM_INTERFACE(This->clipper, IDirectDrawClipper) );
1859
1860     This->clipper = ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper);
1861     if (Clipper != NULL)
1862         IDirectDrawClipper_AddRef(Clipper);
1863
1864     return DD_OK;
1865 }
1866
1867 /*****************************************************************************
1868  * IDirectDrawSurface7::SetSurfaceDesc
1869  *
1870  * Sets the surface description. It can override the pixel format, the surface
1871  * memory, ...
1872  * It's not really tested.
1873  *
1874  * Params:
1875  * DDSD: Pointer to the new surface description to set
1876  * Flags: Some flags
1877  *
1878  * Returns:
1879  *  DD_OK on success
1880  *  DDERR_INVALIDPARAMS if DDSD is NULL
1881  *
1882  *****************************************************************************/
1883 static HRESULT WINAPI
1884 IDirectDrawSurfaceImpl_SetSurfaceDesc(IDirectDrawSurface7 *iface,
1885                                       DDSURFACEDESC2 *DDSD,
1886                                       DWORD Flags)
1887 {
1888     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1889     WINED3DFORMAT newFormat = WINED3DFMT_UNKNOWN;
1890     HRESULT hr;
1891     TRACE("(%p)->(%p,%lx)\n", This, DDSD, Flags);
1892
1893     if(!DDSD)
1894         return DDERR_INVALIDPARAMS;
1895
1896     if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
1897     {
1898         ERR("Setting the surface memory isn't supported yet\n");
1899         return DDERR_INVALIDPARAMS;
1900
1901     }
1902     if (DDSD->dwFlags & DDSD_PIXELFORMAT)
1903     {
1904         newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1905
1906         if(newFormat == WINED3DFMT_UNKNOWN)
1907         {
1908             ERR("Requested to set an unknown pixelformat\n");
1909             return DDERR_INVALIDPARAMS;
1910         }
1911         if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
1912         {
1913             hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
1914                                            newFormat);
1915             if(hr != DD_OK) return hr;
1916         }
1917     }
1918     if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
1919     {
1920         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
1921                                     DDCKEY_DESTOVERLAY,
1922                                     &DDSD->u3.ddckCKDestOverlay);
1923     }
1924     if (DDSD->dwFlags & DDSD_CKDESTBLT)
1925     {
1926         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
1927                                     DDCKEY_DESTBLT,
1928                                     &DDSD->ddckCKDestBlt);
1929     }
1930     if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
1931     {
1932         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
1933                                     DDCKEY_SRCOVERLAY,
1934                                     &DDSD->ddckCKSrcOverlay);
1935     }
1936     if (DDSD->dwFlags & DDSD_CKSRCBLT)
1937     {
1938         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
1939                                     DDCKEY_SRCBLT,
1940                                     &DDSD->ddckCKSrcBlt);
1941     }
1942     if (DDSD->dwFlags & DDSD_LPSURFACE)
1943     {
1944         hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
1945         if(hr != WINED3D_OK)
1946         {
1947             /* No need for a trace here, wined3d does that for us */
1948             return hr;
1949         }
1950     }
1951
1952     This->surface_desc = *DDSD;
1953
1954     return DD_OK;
1955 }
1956
1957 /*****************************************************************************
1958  * IDirectDrawSurface7::GetPalette
1959  *
1960  * Returns the IDirectDrawPalette interface of the palette currently assigned
1961  * to the surface
1962  *
1963  * Params:
1964  *  Pal: Address to write the interface pointer to
1965  *
1966  * Returns:
1967  *  DD_OK on success
1968  *  DDERR_INVALIDPARAMS if Pal is NULL
1969  *
1970  *****************************************************************************/
1971 static HRESULT WINAPI
1972 IDirectDrawSurfaceImpl_GetPalette(IDirectDrawSurface7 *iface,
1973                                   IDirectDrawPalette **Pal)
1974 {
1975     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1976     IWineD3DPalette *wPal;
1977     HRESULT hr;
1978     TRACE("(%p)->(%p): Relay\n", This, Pal);
1979
1980     if(!Pal)
1981         return DDERR_INVALIDPARAMS;
1982
1983     hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
1984     if(hr != DD_OK) return hr;
1985
1986     if(wPal)
1987     {
1988         hr = IWineD3DPalette_GetParent(wPal, (IUnknown **) Pal);
1989     }
1990     else
1991     {
1992         *Pal = NULL;
1993         hr = DDERR_NOPALETTEATTACHED;
1994     }
1995
1996     return hr;
1997 }
1998
1999 /*****************************************************************************
2000  * IDirectDrawSurface7::SetColorKey
2001  *
2002  * Sets the color keying options for the surface. Observations showed that
2003  * in case of complex surfaces the color key has to be assigned to all
2004  * sublevels.
2005  *
2006  * Params:
2007  *  Flags: DDCKEY_*
2008  *  CKey: The new color key
2009  *
2010  * Returns:
2011  *  DD_OK on success
2012  *  See IWineD3DSurface::SetColorKey for details
2013  *
2014  *****************************************************************************/
2015 static HRESULT WINAPI
2016 IDirectDrawSurfaceImpl_SetColorKey(IDirectDrawSurface7 *iface,
2017                                    DWORD Flags,
2018                                    DDCOLORKEY *CKey)
2019 {
2020     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2021     IDirectDrawSurfaceImpl *surf;
2022     HRESULT hr;
2023     TRACE("(%p)->(%lx,%p)\n", This, Flags, CKey);
2024
2025     for(surf = This->first_complex; surf; surf = surf->next_complex)
2026     {
2027         hr = IWineD3DSurface_SetColorKey(surf->WineD3DSurface,
2028                                          Flags,
2029                                          CKey);
2030         if(FAILED(hr))
2031         {
2032             WARN("IWineD3DSurface::SetColorKey for surface %p failed with hr=%08lx\n",
2033                  surf->WineD3DSurface, hr);
2034             return hr;
2035         }
2036     }
2037     return DD_OK;
2038 }
2039
2040 /*****************************************************************************
2041  * IDirectDrawSurface7::SetPalette
2042  *
2043  * Assigns a DirectDrawPalette object to the surface
2044  *
2045  * Params:
2046  *  Pal: Interface to the palette to set
2047  *
2048  * Returns:
2049  *  DD_OK on success
2050  *
2051  *****************************************************************************/
2052 static HRESULT WINAPI
2053 IDirectDrawSurfaceImpl_SetPalette(IDirectDrawSurface7 *iface,
2054                                   IDirectDrawPalette *Pal)
2055 {
2056     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2057     IDirectDrawPalette *oldPal;
2058     IDirectDrawSurfaceImpl *surf;
2059     IDirectDrawPaletteImpl *PalImpl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, Pal);
2060     HRESULT hr;
2061     TRACE("(%p)->(%p)\n", This, Pal);
2062
2063     /* Find the old palette */
2064     hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2065     if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED) return hr;
2066     if(oldPal) IDirectDrawPalette_Release(oldPal);  /* For the GetPalette */
2067
2068     /* Set the new Palette */
2069     IWineD3DSurface_SetPalette(This->WineD3DSurface,
2070                                PalImpl ? PalImpl->wineD3DPalette : NULL);
2071     /* AddRef the Palette */
2072     if(Pal) IDirectDrawPalette_AddRef(Pal);
2073
2074     /* Release the old palette */
2075     if(oldPal) IDirectDrawPalette_Release(oldPal);
2076
2077     /* If this is a front buffer, also update the back buffers */
2078     if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2079     {
2080         for(surf = This->next_complex; surf != NULL; surf = surf->next_complex)
2081         {
2082             IDirectDrawSurface7_SetPalette(ICOM_INTERFACE(surf, IDirectDrawSurface7),
2083                                            Pal);
2084         }
2085     }
2086
2087     return DD_OK;
2088 }
2089
2090 /*****************************************************************************
2091  * The VTable
2092  *****************************************************************************/
2093
2094 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl =
2095 {
2096     /*** IUnknown ***/
2097     IDirectDrawSurfaceImpl_QueryInterface,
2098     IDirectDrawSurfaceImpl_AddRef,
2099     IDirectDrawSurfaceImpl_Release,
2100     /*** IDirectDrawSurface ***/
2101     IDirectDrawSurfaceImpl_AddAttachedSurface,
2102     IDirectDrawSurfaceImpl_AddOverlayDirtyRect,
2103     IDirectDrawSurfaceImpl_Blt,
2104     IDirectDrawSurfaceImpl_BltBatch,
2105     IDirectDrawSurfaceImpl_BltFast,
2106     IDirectDrawSurfaceImpl_DeleteAttachedSurface,
2107     IDirectDrawSurfaceImpl_EnumAttachedSurfaces,
2108     IDirectDrawSurfaceImpl_EnumOverlayZOrders,
2109     IDirectDrawSurfaceImpl_Flip,
2110     IDirectDrawSurfaceImpl_GetAttachedSurface,
2111     IDirectDrawSurfaceImpl_GetBltStatus,
2112     IDirectDrawSurfaceImpl_GetCaps,
2113     IDirectDrawSurfaceImpl_GetClipper,
2114     IDirectDrawSurfaceImpl_GetColorKey,
2115     IDirectDrawSurfaceImpl_GetDC,
2116     IDirectDrawSurfaceImpl_GetFlipStatus,
2117     IDirectDrawSurfaceImpl_GetOverlayPosition,
2118     IDirectDrawSurfaceImpl_GetPalette,
2119     IDirectDrawSurfaceImpl_GetPixelFormat,
2120     IDirectDrawSurfaceImpl_GetSurfaceDesc,
2121     IDirectDrawSurfaceImpl_Initialize,
2122     IDirectDrawSurfaceImpl_IsLost,
2123     IDirectDrawSurfaceImpl_Lock,
2124     IDirectDrawSurfaceImpl_ReleaseDC,
2125     IDirectDrawSurfaceImpl_Restore,
2126     IDirectDrawSurfaceImpl_SetClipper,
2127     IDirectDrawSurfaceImpl_SetColorKey,
2128     IDirectDrawSurfaceImpl_SetOverlayPosition,
2129     IDirectDrawSurfaceImpl_SetPalette,
2130     IDirectDrawSurfaceImpl_Unlock,
2131     IDirectDrawSurfaceImpl_UpdateOverlay,
2132     IDirectDrawSurfaceImpl_UpdateOverlayDisplay,
2133     IDirectDrawSurfaceImpl_UpdateOverlayZOrder,
2134     /*** IDirectDrawSurface2 ***/
2135     IDirectDrawSurfaceImpl_GetDDInterface,
2136     IDirectDrawSurfaceImpl_PageLock,
2137     IDirectDrawSurfaceImpl_PageUnlock,
2138     /*** IDirectDrawSurface3 ***/
2139     IDirectDrawSurfaceImpl_SetSurfaceDesc,
2140     /*** IDirectDrawSurface4 ***/
2141     IDirectDrawSurfaceImpl_SetPrivateData,
2142     IDirectDrawSurfaceImpl_GetPrivateData,
2143     IDirectDrawSurfaceImpl_FreePrivateData,
2144     IDirectDrawSurfaceImpl_GetUniquenessValue,
2145     IDirectDrawSurfaceImpl_ChangeUniquenessValue,
2146     /*** IDirectDrawSurface7 ***/
2147     IDirectDrawSurfaceImpl_SetPriority,
2148     IDirectDrawSurfaceImpl_GetPriority,
2149     IDirectDrawSurfaceImpl_SetLOD,
2150     IDirectDrawSurfaceImpl_GetLOD
2151 };