ddraw: Do not let the a surface dimension fall to 0.
[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 %d\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 %d\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 %d\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: %x,%x,%x,%x\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: %x,%x,%x,%x\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: %x,%x,%x,%x\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,%x,%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,%x)\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,%x,%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)->(%08x,%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)->(%d): 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,%d,%x): 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)->(%x)\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)->(%x)\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,%d,%08x)\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)->(%x,%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)->(%x): 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)->(%x): 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 %d.\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)->(%d,%d): 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     TRACE("(%p)->(%p,%p,%p,%x,%p): Relay\n", This, SrcRect, Dst, DstRect, Flags, FX);
1567
1568     return IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
1569                                          SrcRect,
1570                                          Dst ? Dst->WineD3DSurface : NULL,
1571                                          DstRect,
1572                                          Flags,
1573                                          (WINEDDOVERLAYFX *) FX);
1574 }
1575
1576 /*****************************************************************************
1577  * IDirectDrawSurface7::UpdateOverlayDisplay
1578  *
1579  * The DX7 sdk says that it's not implemented
1580  *
1581  * Params:
1582  *  Flags: ?
1583  *
1584  * Returns: DDERR_UNSUPPORTED, because we don't support overlays
1585  *
1586  *****************************************************************************/
1587 static HRESULT WINAPI
1588 IDirectDrawSurfaceImpl_UpdateOverlayDisplay(IDirectDrawSurface7 *iface,
1589                                             DWORD Flags)
1590 {
1591     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1592     TRACE("(%p)->(%x)\n", This, Flags);
1593     return DDERR_UNSUPPORTED;
1594 }
1595
1596 /*****************************************************************************
1597  * IDirectDrawSurface7::UpdateOverlayZOrder
1598  *
1599  * Sets an overlay's Z order
1600  *
1601  * Params:
1602  *  Flags: DDOVERZ_* flags
1603  *  DDSRef: Defines the relative position in the overlay chain
1604  *
1605  * Returns:
1606  *  DDERR_NOTOVERLAYSURFACE, because we don't support overlays
1607  *
1608  *****************************************************************************/
1609 static HRESULT WINAPI
1610 IDirectDrawSurfaceImpl_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
1611                                            DWORD Flags,
1612                                            IDirectDrawSurface7 *DDSRef)
1613 {
1614     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1615     IDirectDrawSurfaceImpl *Ref = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DDSRef);
1616
1617     TRACE("(%p)->(%x,%p): Relay\n", This, Flags, Ref);
1618     return IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
1619                                                Flags,
1620                                                Ref ? Ref->WineD3DSurface : NULL);
1621 }
1622
1623 /*****************************************************************************
1624  * IDirectDrawSurface7::GetDDInterface
1625  *
1626  * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
1627  * surface belongs to
1628  *
1629  * Params:
1630  *  DD: Address to write the interface pointer to
1631  *
1632  * Returns:
1633  *  DD_OK on success
1634  *  DDERR_INVALIDPARAMS if DD is NULL
1635  *
1636  *****************************************************************************/
1637 static HRESULT WINAPI
1638 IDirectDrawSurfaceImpl_GetDDInterface(IDirectDrawSurface7 *iface,
1639                                       void **DD)
1640 {
1641     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1642
1643     /* It is not quite correct to use the same lpVtable for the different
1644      * IDirectDrawSurface versions because the GetDDInterface return different interfaces
1645      */
1646     FIXME("(%p)->(%p)\n",This,DD);
1647
1648     if(!DD)
1649         return DDERR_INVALIDPARAMS;
1650
1651     switch(This->version)
1652     {
1653         case 7:
1654             *((IDirectDraw7 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw7);
1655             IDirectDraw7_AddRef(*(IDirectDraw7 **) DD);
1656             break;
1657
1658         case 4:
1659             *((IDirectDraw4 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw4);
1660             IDirectDraw4_AddRef(*(IDirectDraw4 **) DD);
1661
1662         case 2:
1663         case 1:
1664             *((IDirectDraw **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw);
1665             IDirectDraw_AddRef( *(IDirectDraw **) DD);
1666             break;
1667
1668     }
1669
1670     return DD_OK;
1671 }
1672
1673 /* This seems also windows implementation specific - I don't think WineD3D needs this */
1674 static HRESULT WINAPI IDirectDrawSurfaceImpl_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
1675 {
1676     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1677     volatile IDirectDrawSurfaceImpl* vThis = This;
1678
1679     TRACE("(%p)\n",This);
1680     /* A uniqueness value of 0 is apparently special.
1681     * This needs to be checked. */
1682     while (1) {
1683         DWORD old_uniqueness_value = vThis->uniqueness_value;
1684         DWORD new_uniqueness_value = old_uniqueness_value+1;
1685
1686         if (old_uniqueness_value == 0) break;
1687         if (new_uniqueness_value == 0) new_uniqueness_value = 1;
1688
1689         if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
1690                                       old_uniqueness_value,
1691                                       new_uniqueness_value)
1692             == old_uniqueness_value)
1693             break;
1694     }
1695
1696     return DD_OK;
1697 }
1698
1699 static HRESULT WINAPI IDirectDrawSurfaceImpl_GetUniquenessValue(IDirectDrawSurface7 *iface, LPDWORD pValue)
1700 {
1701     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1702
1703     TRACE("(%p)->(%p)\n",This,pValue);
1704     *pValue = This->uniqueness_value;
1705     return DD_OK;
1706 }
1707
1708 /*****************************************************************************
1709  * IDirectDrawSurface7::SetLOD
1710  *
1711  * Sets the level of detail of a texture
1712  *
1713  * Params:
1714  *  MaxLOD: LOD to set
1715  *
1716  * Returns:
1717  *  DD_OK on success
1718  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
1719  *
1720  *****************************************************************************/
1721 static HRESULT WINAPI
1722 IDirectDrawSurfaceImpl_SetLOD(IDirectDrawSurface7 *iface,
1723                               DWORD MaxLOD)
1724 {
1725     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1726     TRACE("(%p)->(%d)\n", This, MaxLOD);
1727
1728     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
1729         return DDERR_INVALIDOBJECT;
1730
1731     if(!This->wineD3DTexture)
1732     {
1733         ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
1734         return DDERR_INVALIDOBJECT;
1735     }
1736
1737     return IWineD3DTexture_SetLOD(This->wineD3DTexture,
1738                                   MaxLOD);
1739 }
1740
1741 /*****************************************************************************
1742  * IDirectDrawSurface7::GetLOD
1743  *
1744  * Returns the level of detail of a Direct3D texture
1745  *
1746  * Params:
1747  *  MaxLOD: Address to write the LOD to
1748  *
1749  * Returns:
1750  *  DD_OK on success
1751  *  DDERR_INVALIDPARAMS if MaxLOD is NULL
1752  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
1753  *
1754  *****************************************************************************/
1755 static HRESULT WINAPI
1756 IDirectDrawSurfaceImpl_GetLOD(IDirectDrawSurface7 *iface,
1757                               DWORD *MaxLOD)
1758 {
1759     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1760     TRACE("(%p)->(%p)\n", This, MaxLOD);
1761
1762     if(!MaxLOD)
1763         return DDERR_INVALIDPARAMS;
1764
1765     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
1766         return DDERR_INVALIDOBJECT;
1767
1768     *MaxLOD = IWineD3DTexture_GetLOD(This->wineD3DTexture);
1769     return DD_OK;
1770 }
1771
1772 /*****************************************************************************
1773  * IDirectDrawSurface7::BltFast
1774  *
1775  * Performs a fast Blit.
1776  *
1777  * Params:
1778  *  dstx: The x coordinate to blit to on the destination
1779  *  dsty: The y coordinate to blit to on the destination
1780  *  Source: The source surface
1781  *  rsrc: The source rectangle
1782  *  trans: Type of transfer. Some DDBLTFAST_* flags
1783  *
1784  * Returns:
1785  *  DD_OK on success
1786  *  For more details, see IWineD3DSurface::BltFast
1787  *
1788  *****************************************************************************/
1789 static HRESULT WINAPI
1790 IDirectDrawSurfaceImpl_BltFast(IDirectDrawSurface7 *iface,
1791                                DWORD dstx,
1792                                DWORD dsty,
1793                                IDirectDrawSurface7 *Source,
1794                                RECT *rsrc,
1795                                DWORD trans)
1796 {
1797     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1798     IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Source);
1799     TRACE("(%p)->(%d,%d,%p,%p,%d): Relay\n", This, dstx, dsty, Source, rsrc, trans);
1800
1801     return IWineD3DSurface_BltFast(This->WineD3DSurface,
1802                                    dstx, dsty,
1803                                    src ? src->WineD3DSurface : NULL,
1804                                    rsrc,
1805                                    trans);
1806 }
1807
1808 /*****************************************************************************
1809  * IDirectDrawSurface7::GetClipper
1810  *
1811  * Returns the IDirectDrawClipper interface of the clipper assigned to this
1812  * surface
1813  *
1814  * Params:
1815  *  Clipper: Address to store the interface pointer at
1816  *
1817  * Returns:
1818  *  DD_OK on success
1819  *  DDERR_INVALIDPARAMS if Clipper is NULL
1820  *  DDERR_NOCLIPPERATTACHED if there's no clipper attached
1821  *
1822  *****************************************************************************/
1823 static HRESULT WINAPI
1824 IDirectDrawSurfaceImpl_GetClipper(IDirectDrawSurface7 *iface,
1825                                   IDirectDrawClipper **Clipper)
1826 {
1827     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1828     TRACE("(%p)->(%p)\n", This, Clipper);
1829
1830     if(!Clipper)
1831         return DDERR_INVALIDPARAMS;
1832
1833     if(This->clipper == NULL)
1834         return DDERR_NOCLIPPERATTACHED;
1835
1836     *Clipper = ICOM_INTERFACE(This->clipper, IDirectDrawClipper);
1837     IDirectDrawClipper_AddRef(*Clipper);
1838     return DD_OK;
1839 }
1840
1841 /*****************************************************************************
1842  * IDirectDrawSurface7::SetClipper
1843  *
1844  * Sets a clipper for the surface
1845  *
1846  * Params:
1847  *  Clipper: IDirectDrawClipper interface of the clipper to set
1848  *
1849  * Returns:
1850  *  DD_OK on success
1851  *
1852  *****************************************************************************/
1853 static HRESULT WINAPI
1854 IDirectDrawSurfaceImpl_SetClipper(IDirectDrawSurface7 *iface,
1855                                   IDirectDrawClipper *Clipper)
1856 {
1857     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1858
1859     TRACE("(%p)->(%p)\n",This,Clipper);
1860     if (ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper) == This->clipper)
1861         return DD_OK;
1862
1863     if (This->clipper != NULL)
1864         IDirectDrawClipper_Release(ICOM_INTERFACE(This->clipper, IDirectDrawClipper) );
1865
1866     This->clipper = ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper);
1867     if (Clipper != NULL)
1868         IDirectDrawClipper_AddRef(Clipper);
1869
1870     return DD_OK;
1871 }
1872
1873 /*****************************************************************************
1874  * IDirectDrawSurface7::SetSurfaceDesc
1875  *
1876  * Sets the surface description. It can override the pixel format, the surface
1877  * memory, ...
1878  * It's not really tested.
1879  *
1880  * Params:
1881  * DDSD: Pointer to the new surface description to set
1882  * Flags: Some flags
1883  *
1884  * Returns:
1885  *  DD_OK on success
1886  *  DDERR_INVALIDPARAMS if DDSD is NULL
1887  *
1888  *****************************************************************************/
1889 static HRESULT WINAPI
1890 IDirectDrawSurfaceImpl_SetSurfaceDesc(IDirectDrawSurface7 *iface,
1891                                       DDSURFACEDESC2 *DDSD,
1892                                       DWORD Flags)
1893 {
1894     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1895     WINED3DFORMAT newFormat = WINED3DFMT_UNKNOWN;
1896     HRESULT hr;
1897     TRACE("(%p)->(%p,%x)\n", This, DDSD, Flags);
1898
1899     if(!DDSD)
1900         return DDERR_INVALIDPARAMS;
1901
1902     if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
1903     {
1904         ERR("Setting the surface memory isn't supported yet\n");
1905         return DDERR_INVALIDPARAMS;
1906
1907     }
1908     if (DDSD->dwFlags & DDSD_PIXELFORMAT)
1909     {
1910         newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1911
1912         if(newFormat == WINED3DFMT_UNKNOWN)
1913         {
1914             ERR("Requested to set an unknown pixelformat\n");
1915             return DDERR_INVALIDPARAMS;
1916         }
1917         if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
1918         {
1919             hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
1920                                            newFormat);
1921             if(hr != DD_OK) return hr;
1922         }
1923     }
1924     if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
1925     {
1926         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
1927                                     DDCKEY_DESTOVERLAY,
1928                                     &DDSD->u3.ddckCKDestOverlay);
1929     }
1930     if (DDSD->dwFlags & DDSD_CKDESTBLT)
1931     {
1932         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
1933                                     DDCKEY_DESTBLT,
1934                                     &DDSD->ddckCKDestBlt);
1935     }
1936     if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
1937     {
1938         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
1939                                     DDCKEY_SRCOVERLAY,
1940                                     &DDSD->ddckCKSrcOverlay);
1941     }
1942     if (DDSD->dwFlags & DDSD_CKSRCBLT)
1943     {
1944         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
1945                                     DDCKEY_SRCBLT,
1946                                     &DDSD->ddckCKSrcBlt);
1947     }
1948     if (DDSD->dwFlags & DDSD_LPSURFACE)
1949     {
1950         hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
1951         if(hr != WINED3D_OK)
1952         {
1953             /* No need for a trace here, wined3d does that for us */
1954             return hr;
1955         }
1956     }
1957
1958     This->surface_desc = *DDSD;
1959
1960     return DD_OK;
1961 }
1962
1963 /*****************************************************************************
1964  * IDirectDrawSurface7::GetPalette
1965  *
1966  * Returns the IDirectDrawPalette interface of the palette currently assigned
1967  * to the surface
1968  *
1969  * Params:
1970  *  Pal: Address to write the interface pointer to
1971  *
1972  * Returns:
1973  *  DD_OK on success
1974  *  DDERR_INVALIDPARAMS if Pal is NULL
1975  *
1976  *****************************************************************************/
1977 static HRESULT WINAPI
1978 IDirectDrawSurfaceImpl_GetPalette(IDirectDrawSurface7 *iface,
1979                                   IDirectDrawPalette **Pal)
1980 {
1981     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1982     IWineD3DPalette *wPal;
1983     HRESULT hr;
1984     TRACE("(%p)->(%p): Relay\n", This, Pal);
1985
1986     if(!Pal)
1987         return DDERR_INVALIDPARAMS;
1988
1989     hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
1990     if(hr != DD_OK) return hr;
1991
1992     if(wPal)
1993     {
1994         hr = IWineD3DPalette_GetParent(wPal, (IUnknown **) Pal);
1995     }
1996     else
1997     {
1998         *Pal = NULL;
1999         hr = DDERR_NOPALETTEATTACHED;
2000     }
2001
2002     return hr;
2003 }
2004
2005 /*****************************************************************************
2006  * IDirectDrawSurface7::SetColorKey
2007  *
2008  * Sets the color keying options for the surface. Observations showed that
2009  * in case of complex surfaces the color key has to be assigned to all
2010  * sublevels.
2011  *
2012  * Params:
2013  *  Flags: DDCKEY_*
2014  *  CKey: The new color key
2015  *
2016  * Returns:
2017  *  DD_OK on success
2018  *  See IWineD3DSurface::SetColorKey for details
2019  *
2020  *****************************************************************************/
2021 static HRESULT WINAPI
2022 IDirectDrawSurfaceImpl_SetColorKey(IDirectDrawSurface7 *iface,
2023                                    DWORD Flags,
2024                                    DDCOLORKEY *CKey)
2025 {
2026     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2027     IDirectDrawSurfaceImpl *surf;
2028     HRESULT hr;
2029     TRACE("(%p)->(%x,%p)\n", This, Flags, CKey);
2030
2031     for(surf = This->first_complex; surf; surf = surf->next_complex)
2032     {
2033         hr = IWineD3DSurface_SetColorKey(surf->WineD3DSurface,
2034                                          Flags,
2035                                          CKey);
2036         if(FAILED(hr))
2037         {
2038             WARN("IWineD3DSurface::SetColorKey for surface %p failed with hr=%08x\n",
2039                  surf->WineD3DSurface, hr);
2040             return hr;
2041         }
2042     }
2043     return DD_OK;
2044 }
2045
2046 /*****************************************************************************
2047  * IDirectDrawSurface7::SetPalette
2048  *
2049  * Assigns a DirectDrawPalette object to the surface
2050  *
2051  * Params:
2052  *  Pal: Interface to the palette to set
2053  *
2054  * Returns:
2055  *  DD_OK on success
2056  *
2057  *****************************************************************************/
2058 static HRESULT WINAPI
2059 IDirectDrawSurfaceImpl_SetPalette(IDirectDrawSurface7 *iface,
2060                                   IDirectDrawPalette *Pal)
2061 {
2062     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2063     IDirectDrawPalette *oldPal;
2064     IDirectDrawSurfaceImpl *surf;
2065     IDirectDrawPaletteImpl *PalImpl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, Pal);
2066     HRESULT hr;
2067     TRACE("(%p)->(%p)\n", This, Pal);
2068
2069     /* Find the old palette */
2070     hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2071     if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED) return hr;
2072     if(oldPal) IDirectDrawPalette_Release(oldPal);  /* For the GetPalette */
2073
2074     /* Set the new Palette */
2075     IWineD3DSurface_SetPalette(This->WineD3DSurface,
2076                                PalImpl ? PalImpl->wineD3DPalette : NULL);
2077     /* AddRef the Palette */
2078     if(Pal) IDirectDrawPalette_AddRef(Pal);
2079
2080     /* Release the old palette */
2081     if(oldPal) IDirectDrawPalette_Release(oldPal);
2082
2083     /* If this is a front buffer, also update the back buffers */
2084     if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2085     {
2086         for(surf = This->next_complex; surf != NULL; surf = surf->next_complex)
2087         {
2088             IDirectDrawSurface7_SetPalette(ICOM_INTERFACE(surf, IDirectDrawSurface7),
2089                                            Pal);
2090         }
2091     }
2092
2093     return DD_OK;
2094 }
2095
2096 /*****************************************************************************
2097  * The VTable
2098  *****************************************************************************/
2099
2100 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl =
2101 {
2102     /*** IUnknown ***/
2103     IDirectDrawSurfaceImpl_QueryInterface,
2104     IDirectDrawSurfaceImpl_AddRef,
2105     IDirectDrawSurfaceImpl_Release,
2106     /*** IDirectDrawSurface ***/
2107     IDirectDrawSurfaceImpl_AddAttachedSurface,
2108     IDirectDrawSurfaceImpl_AddOverlayDirtyRect,
2109     IDirectDrawSurfaceImpl_Blt,
2110     IDirectDrawSurfaceImpl_BltBatch,
2111     IDirectDrawSurfaceImpl_BltFast,
2112     IDirectDrawSurfaceImpl_DeleteAttachedSurface,
2113     IDirectDrawSurfaceImpl_EnumAttachedSurfaces,
2114     IDirectDrawSurfaceImpl_EnumOverlayZOrders,
2115     IDirectDrawSurfaceImpl_Flip,
2116     IDirectDrawSurfaceImpl_GetAttachedSurface,
2117     IDirectDrawSurfaceImpl_GetBltStatus,
2118     IDirectDrawSurfaceImpl_GetCaps,
2119     IDirectDrawSurfaceImpl_GetClipper,
2120     IDirectDrawSurfaceImpl_GetColorKey,
2121     IDirectDrawSurfaceImpl_GetDC,
2122     IDirectDrawSurfaceImpl_GetFlipStatus,
2123     IDirectDrawSurfaceImpl_GetOverlayPosition,
2124     IDirectDrawSurfaceImpl_GetPalette,
2125     IDirectDrawSurfaceImpl_GetPixelFormat,
2126     IDirectDrawSurfaceImpl_GetSurfaceDesc,
2127     IDirectDrawSurfaceImpl_Initialize,
2128     IDirectDrawSurfaceImpl_IsLost,
2129     IDirectDrawSurfaceImpl_Lock,
2130     IDirectDrawSurfaceImpl_ReleaseDC,
2131     IDirectDrawSurfaceImpl_Restore,
2132     IDirectDrawSurfaceImpl_SetClipper,
2133     IDirectDrawSurfaceImpl_SetColorKey,
2134     IDirectDrawSurfaceImpl_SetOverlayPosition,
2135     IDirectDrawSurfaceImpl_SetPalette,
2136     IDirectDrawSurfaceImpl_Unlock,
2137     IDirectDrawSurfaceImpl_UpdateOverlay,
2138     IDirectDrawSurfaceImpl_UpdateOverlayDisplay,
2139     IDirectDrawSurfaceImpl_UpdateOverlayZOrder,
2140     /*** IDirectDrawSurface2 ***/
2141     IDirectDrawSurfaceImpl_GetDDInterface,
2142     IDirectDrawSurfaceImpl_PageLock,
2143     IDirectDrawSurfaceImpl_PageUnlock,
2144     /*** IDirectDrawSurface3 ***/
2145     IDirectDrawSurfaceImpl_SetSurfaceDesc,
2146     /*** IDirectDrawSurface4 ***/
2147     IDirectDrawSurfaceImpl_SetPrivateData,
2148     IDirectDrawSurfaceImpl_GetPrivateData,
2149     IDirectDrawSurfaceImpl_FreePrivateData,
2150     IDirectDrawSurfaceImpl_GetUniquenessValue,
2151     IDirectDrawSurfaceImpl_ChangeUniquenessValue,
2152     /*** IDirectDrawSurface7 ***/
2153     IDirectDrawSurfaceImpl_SetPriority,
2154     IDirectDrawSurfaceImpl_GetPriority,
2155     IDirectDrawSurfaceImpl_SetLOD,
2156     IDirectDrawSurfaceImpl_GetLOD
2157 };