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