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