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