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