ddraw: Fix reported caps on compressed surfaces.
[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 (Rect)
553     {
554         if ((Rect->left < 0)
555                 || (Rect->top < 0)
556                 || (Rect->left > Rect->right)
557                 || (Rect->top > Rect->bottom)
558                 || (Rect->right > This->surface_desc.dwWidth)
559                 || (Rect->bottom > This->surface_desc.dwHeight))
560         {
561             WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
562             return DDERR_INVALIDPARAMS;
563         }
564     }
565
566     if(!DDSD)
567         return DDERR_INVALIDPARAMS;
568
569     /* Should I check for the handle to be NULL?
570      *
571      * The DDLOCK flags and the D3DLOCK flags are equal
572      * for the supported values. The others are ignored by WineD3D
573      */
574
575     /* Hmm. Anarchy online passes an uninitialized surface descriptor,
576      * that means it doesn't have dwSize set. Init it to some sane
577      * value
578      */
579     if(DDSD->dwSize <= sizeof(DDSURFACEDESC))
580     {
581         DDSD->dwSize = sizeof(DDSURFACEDESC);
582     }
583     else
584     {
585         DDSD->dwSize = sizeof(DDSURFACEDESC2);
586     }
587
588     DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
589     hr = IWineD3DSurface_LockRect(This->WineD3DSurface,
590                                   &LockedRect,
591                                   Rect,
592                                   Flags);
593     if(hr != D3D_OK) return hr;
594
595     /* Override the memory area. The pitch should be set already. Strangely windows
596      * does not set the LPSURFACE flag on locked surfaces !?!.
597      * DDSD->dwFlags |= DDSD_LPSURFACE;
598      */
599     DDSD->lpSurface = LockedRect.pBits;
600
601     TRACE("locked surface returning description :\n");
602     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
603
604     return DD_OK;
605 }
606
607 /*****************************************************************************
608  * IDirectDrawSurface7::Unlock
609  *
610  * Unlocks an locked surface
611  *
612  * Params:
613  *  Rect: Not used by this implementation
614  *
615  * Returns:
616  *  D3D_OK on success
617  *  For more details, see IWineD3DSurface::UnlockRect
618  *
619  *****************************************************************************/
620 static HRESULT WINAPI
621 IDirectDrawSurfaceImpl_Unlock(IDirectDrawSurface7 *iface,
622                               RECT *pRect)
623 {
624     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
625     TRACE("(%p)->(%p)\n", This, pRect);
626
627     return IWineD3DSurface_UnlockRect(This->WineD3DSurface);
628 }
629
630 /*****************************************************************************
631  * IDirectDrawSurface7::Flip
632  *
633  * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
634  * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
635  * the flip target is passed to WineD3D, even if the app didn't specify one
636  *
637  * Params:
638  *  DestOverride: Specifies the surface that will become the new front
639  *                buffer. If NULL, the current back buffer is used
640  *  Flags: some DirectDraw flags, see include/ddraw.h
641  *
642  * Returns:
643  *  DD_OK on success
644  *  DDERR_NOTFLIPPABLE if no flip target could be found
645  *  DDERR_INVALIDOBJECT if the surface isn't a front buffer
646  *  For more details, see IWineD3DSurface::Flip
647  *
648  *****************************************************************************/
649 static HRESULT WINAPI
650 IDirectDrawSurfaceImpl_Flip(IDirectDrawSurface7 *iface,
651                             IDirectDrawSurface7 *DestOverride,
652                             DWORD Flags)
653 {
654     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
655     IDirectDrawSurfaceImpl *Override = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DestOverride);
656     IDirectDrawSurface7 *Override7;
657     HRESULT hr;
658     TRACE("(%p)->(%p,%x)\n", This, DestOverride, Flags);
659
660     /* Flip has to be called from a front buffer
661      * What about overlay surfaces, AFAIK they can flip too?
662      */
663     if( !(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER) )
664         return DDERR_INVALIDOBJECT; /* Unckecked */
665
666     /* WineD3D doesn't keep track of attached surface, so find the target */
667     if(!Override)
668     {
669         DDSCAPS2 Caps;
670
671         memset(&Caps, 0, sizeof(Caps));
672         Caps.dwCaps |= DDSCAPS_BACKBUFFER;
673         hr = IDirectDrawSurface7_GetAttachedSurface(iface, &Caps, &Override7);
674         if(hr != DD_OK)
675         {
676             ERR("Can't find a flip target\n");
677             return DDERR_NOTFLIPPABLE; /* Unchecked */
678         }
679         Override = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Override7);
680
681         /* For the GetAttachedSurface */
682         IDirectDrawSurface7_Release(Override7);
683     }
684
685     return  IWineD3DSurface_Flip(This->WineD3DSurface,
686                                  Override->WineD3DSurface,
687                                  Flags);
688 }
689
690 /*****************************************************************************
691  * IDirectDrawSurface7::Blt
692  *
693  * Performs a blit on the surface
694  *
695  * Params:
696  *  DestRect: Destination rectangle, can be NULL
697  *  SrcSurface: Source surface, can be NULL
698  *  SrcRect: Source rectange, can be NULL
699  *  Flags: Blt flags
700  *  DDBltFx: Some extended blt parameters, connected to the flags
701  *
702  * Returns:
703  *  D3D_OK on success
704  *  See IWineD3DSurface::Blt for more details
705  *
706  *****************************************************************************/
707 static HRESULT WINAPI
708 IDirectDrawSurfaceImpl_Blt(IDirectDrawSurface7 *iface,
709                            RECT *DestRect,
710                            IDirectDrawSurface7 *SrcSurface,
711                            RECT *SrcRect,
712                            DWORD Flags,
713                            DDBLTFX *DDBltFx)
714 {
715     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
716     HRESULT hr;
717     IDirectDrawSurfaceImpl *Src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, SrcSurface);
718     TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
719
720     /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
721      * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
722      */
723     if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
724         WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
725         return DDERR_INVALIDPARAMS;
726     }
727
728     if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
729         WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
730         return DDERR_INVALIDPARAMS;
731     }
732
733     if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
734         WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
735         return DDERR_INVALIDPARAMS;
736     }
737
738     /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it does, copy the struct,
739      * and replace the ddraw surfaces with the wined3d surfaces
740      * So far no blitting operations using surfaces in the bltfx struct are supported anyway.
741      */
742     hr = IWineD3DSurface_Blt(This->WineD3DSurface,
743                              DestRect,
744                              Src ? Src->WineD3DSurface : NULL,
745                              SrcRect,
746                              Flags,
747                              (WINEDDBLTFX *) DDBltFx,
748                              WINED3DTEXF_NONE);
749     switch(hr)
750     {
751         case WINED3DERR_NOTAVAILABLE:       return DDERR_UNSUPPORTED;
752         case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
753         default:                            return hr;
754     }
755 }
756
757 /*****************************************************************************
758  * IDirectDrawSurface7::AddAttachedSurface
759  *
760  * Attaches a surface to another surface. How the surface attachments work
761  * is not totally understood yet, and this method is prone to problems.
762  * he surface that is attached is AddRef-ed.
763  *
764  * Tests with complex surfaces suggest that the surface attachments form a
765  * tree, but no method to test this has been found yet.
766  *
767  * The attachment list consists of a first surface (first_attached) and
768  * for each surface a pointer to the next attached surface (next_attached).
769  * For the first surface, and a surface that has no attachments
770  * first_attached points to the surface itself. A surface that has
771  * no successors in the chain has next_attached set to NULL.
772  *
773  * Newly attached surfaces are attached right after the root surface.
774  * If a surface is attached to a complex surface compound, it's attached to
775  * the surface that the app requested, not the complex root. See
776  * GetAttachedSurface for a description how surfaces are found.
777  *
778  * This is how the current implementation works, and it was coded by looking
779  * at the needs of the applications.
780  *
781  * So far only Z-Buffer attachments are tested, and they are activated in
782  * WineD3D. Mipmaps could be tricky to activate in WineD3D.
783  * Back buffers should work in 2D mode, but they are not tested(Not sure if
784  * they can be attached at all). Rendering to the primary surface and
785  * switching between that and double buffering is not yet implemented in
786  * WineD3D, so for 3D it might have unexpected results.
787  *
788  * Params:
789  *  Attach: Surface to attach to iface
790  *
791  * Returns:
792  *  DD_OK on success
793  *  DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
794  *
795  *****************************************************************************/
796 static HRESULT WINAPI
797 IDirectDrawSurfaceImpl_AddAttachedSurface(IDirectDrawSurface7 *iface,
798                                           IDirectDrawSurface7 *Attach)
799 {
800     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
801     IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Attach);
802     TRACE("(%p)->(%p)\n", This, Surf);
803
804     /* Should I make sure to add it to the first complex surface? */
805
806     if(Surf == This)
807         return DDERR_CANNOTATTACHSURFACE; /* unchecked */
808
809     /* MSDN: Only Z buffer surfaces can be attached. An old comment said that apparently
810      * mipmaps and back buffers can be attached too, although our tests say no.
811      */
812     if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
813     {
814         /* Write a fixme until we know for sure what is going on */
815         FIXME("Application tries to attach a non Z buffer surface. caps %08x\n",
816               Surf->surface_desc.ddsCaps.dwCaps);
817         return DDERR_CANNOTATTACHSURFACE;
818     }
819
820     /* Set MIPMAPSUBLEVEL if this seems to be one */
821     if (This->surface_desc.ddsCaps.dwCaps &
822         Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
823     {
824         Surf->surface_desc.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
825         /* FIXME: we should probably also add to dwMipMapCount of this
826           * and all parent surfaces (update create_texture if you do) */
827     }
828
829     /* Check if the surface is already attached somewhere */
830     if( (Surf->next_attached != NULL) ||
831         (Surf->first_attached != Surf) )
832     {
833          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);
834         return DDERR_CANNOTATTACHSURFACE;
835     }
836
837     /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
838     Surf->next_attached = This->next_attached;
839     Surf->first_attached = This->first_attached;
840     This->next_attached = Surf;
841
842     /* Check if the WineD3D depth stencil needs updating */
843     if(This->ddraw->d3ddevice)
844     {
845         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
846     }
847
848     /* MSDN: 
849      * "This method increments the reference count of the surface being attached."
850      */
851     IDirectDrawSurface7_AddRef(Attach);
852     return DD_OK;
853 }
854
855 /*****************************************************************************
856  * IDirectDrawSurface7::DeleteAttachedSurface
857  *
858  * Removes a surface from the attachment chain. The surface's refcount
859  * is decreased by one after it has been removed
860  *
861  * Params:
862  *  Flags: Some flags, not used by this implementation
863  *  Attach: Surface to detach
864  *
865  * Returns:
866  *  DD_OK on success
867  *  DDERR_SURFACENOTATTACHED if the surface isn't attached to
868  *
869  *****************************************************************************/
870 static HRESULT WINAPI
871 IDirectDrawSurfaceImpl_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
872                                              DWORD Flags,
873                                              IDirectDrawSurface7 *Attach)
874 {
875     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
876     IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Attach);
877     IDirectDrawSurfaceImpl *Prev = This;
878     TRACE("(%p)->(%08x,%p)\n", This, Flags, Surf);
879
880     if (!Surf || (Surf->first_attached != This) || (Surf == This) )
881         return DDERR_SURFACENOTATTACHED; /* unchecked */
882
883     /* Remove MIPMAPSUBLEVEL if this seemed to be one */
884     if (This->surface_desc.ddsCaps.dwCaps &
885         Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
886     {
887         Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
888         /* FIXME: we should probably also subtract from dwMipMapCount of this
889          * and all parent surfaces */
890     }
891
892     /* Find the predecessor of the detached surface */
893     while(Prev)
894     {
895         if(Prev->next_attached == Surf) break;
896         Prev = Prev->next_attached;
897     }
898
899     /* There must be a surface, otherwise there's a bug */
900     assert(Prev != NULL);
901
902     /* Unchain the surface */
903     Prev->next_attached = Surf->next_attached;
904     Surf->next_attached = NULL;
905     Surf->first_attached = Surf;
906
907     /* Check if the WineD3D depth stencil needs updating */
908     if(This->ddraw->d3ddevice)
909     {
910         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
911     }
912
913     IDirectDrawSurface7_Release(Attach);
914     return DD_OK;
915 }
916
917 /*****************************************************************************
918  * IDirectDrawSurface7::AddOverlayDirtyRect
919  *
920  * "This method is not currently implemented"
921  *
922  * Params:
923  *  Rect: ?
924  *
925  * Returns:
926  *  DDERR_UNSUPPORTED
927  *
928  *****************************************************************************/
929 static HRESULT WINAPI
930 IDirectDrawSurfaceImpl_AddOverlayDirtyRect(IDirectDrawSurface7 *iface,
931                                            LPRECT Rect)
932 {
933     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
934     TRACE("(%p)->(%p)\n",This,Rect);
935
936     /* MSDN says it's not implemented. I could forward it to WineD3D, 
937      * then we'd implement it, but I don't think that's a good idea
938      * (Stefan Dösinger)
939      */
940 #if 0
941     return IWineD3DSurface_AddOverlayDirtyRect(This->WineD3DSurface, pRect);
942 #endif
943     return DDERR_UNSUPPORTED; /* unchecked */
944 }
945
946 /*****************************************************************************
947  * IDirectDrawSurface7::GetDC
948  *
949  * Returns a GDI device context for the surface
950  *
951  * Params:
952  *  hdc: Address of a HDC variable to store the dc to
953  *
954  * Returns:
955  *  DD_OK on success
956  *  DDERR_INVALIDPARAMS if hdc is NULL
957  *  For details, see IWineD3DSurface::GetDC
958  *
959  *****************************************************************************/
960 static HRESULT WINAPI
961 IDirectDrawSurfaceImpl_GetDC(IDirectDrawSurface7 *iface,
962                              HDC *hdc)
963 {
964     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
965     TRACE("(%p)->(%p): Relay\n", This, hdc);
966
967     if(!hdc)
968         return DDERR_INVALIDPARAMS;
969
970     return IWineD3DSurface_GetDC(This->WineD3DSurface,
971                                  hdc);
972 }
973
974 /*****************************************************************************
975  * IDirectDrawSurface7::ReleaseDC
976  *
977  * Releases the DC that was constructed with GetDC
978  *
979  * Params:
980  *  hdc: HDC to release
981  *
982  * Returns:
983  *  DD_OK on success
984  *  For more details, see IWineD3DSurface::ReleaseDC
985  *
986  *****************************************************************************/
987 static HRESULT WINAPI
988 IDirectDrawSurfaceImpl_ReleaseDC(IDirectDrawSurface7 *iface,
989                                  HDC hdc)
990 {
991     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
992     TRACE("(%p)->(%p): Relay\n", This, hdc);
993
994     return IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
995 }
996
997 /*****************************************************************************
998  * IDirectDrawSurface7::GetCaps
999  *
1000  * Returns the surface's caps
1001  *
1002  * Params:
1003  *  Caps: Address to write the caps to
1004  *
1005  * Returns:
1006  *  DD_OK on success
1007  *  DDERR_INVALIDPARAMS if Caps is NULL
1008  *
1009  *****************************************************************************/
1010 static HRESULT WINAPI
1011 IDirectDrawSurfaceImpl_GetCaps(IDirectDrawSurface7 *iface,
1012                                DDSCAPS2 *Caps)
1013 {
1014     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1015     TRACE("(%p)->(%p)\n",This,Caps);
1016
1017     if(!Caps)
1018         return DDERR_INVALIDPARAMS;
1019
1020     *Caps = This->surface_desc.ddsCaps;
1021     return DD_OK;
1022 }
1023
1024 /*****************************************************************************
1025  * IDirectDrawSurface7::SetPriority
1026  *
1027  * Sets a texture priority for managed textures.
1028  *
1029  * Params:
1030  *  Priority: The new priority
1031  *
1032  * Returns:
1033  *  DD_OK on success
1034  *  For more details, see IWineD3DSurface::SetPriority
1035  *
1036  *****************************************************************************/
1037 static HRESULT WINAPI
1038 IDirectDrawSurfaceImpl_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1039 {
1040     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1041     TRACE("(%p)->(%d): Relay!\n",This,Priority);
1042
1043     return IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
1044 }
1045
1046 /*****************************************************************************
1047  * IDirectDrawSurface7::GetPriority
1048  *
1049  * Returns the surface's priority
1050  *
1051  * Params:
1052  *  Priority: Address of a variable to write the priority to
1053  *
1054  * Returns:
1055  *  D3D_OK on success
1056  *  DDERR_INVALIDPARAMS if Priority == NULL
1057  *  For more details, see IWineD3DSurface::GetPriority
1058  *
1059  *****************************************************************************/
1060 static HRESULT WINAPI
1061 IDirectDrawSurfaceImpl_GetPriority(IDirectDrawSurface7 *iface,
1062                                    DWORD *Priority)
1063 {
1064     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1065     TRACE("(%p)->(%p): Relay\n",This,Priority);
1066
1067     if(!Priority)
1068         return DDERR_INVALIDPARAMS;
1069
1070     *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1071     return DD_OK;
1072 }
1073
1074 /*****************************************************************************
1075  * IDirectDrawSurface7::SetPrivateData
1076  *
1077  * Stores some data in the surface that is intended for the application's
1078  * use.
1079  *
1080  * Params:
1081  *  tag: GUID that identifies the data
1082  *  Data: Pointer to the private data
1083  *  Size: Size of the private data
1084  *  Flags: Some flags
1085  *
1086  * Returns:
1087  *  D3D_OK on success
1088  *  For more details, see IWineD3DSurface::SetPrivateData
1089  *
1090  *****************************************************************************/
1091 static HRESULT WINAPI
1092 IDirectDrawSurfaceImpl_SetPrivateData(IDirectDrawSurface7 *iface,
1093                                       REFGUID tag,
1094                                       void *Data,
1095                                       DWORD Size,
1096                                       DWORD Flags)
1097 {
1098     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1099     TRACE("(%p)->(%s,%p,%d,%x): Relay\n", This, debugstr_guid(tag), Data, Size, Flags);
1100
1101     return IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1102                                           tag,
1103                                           Data,
1104                                           Size,
1105                                           Flags);
1106 }
1107
1108 /*****************************************************************************
1109  * IDirectDrawSurface7::GetPrivateData
1110  *
1111  * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1112  *
1113  * Params:
1114  *  tag: GUID of the data to return
1115  *  Data: Address where to write the data to
1116  *  Size: Size of the buffer at Data
1117  *
1118  * Returns:
1119  *  DD_OK on success
1120  *  DDERR_INVALIDPARAMS if Data is NULL
1121  *  For more details, see IWineD3DSurface::GetPrivateData
1122  *
1123  *****************************************************************************/
1124 static HRESULT WINAPI
1125 IDirectDrawSurfaceImpl_GetPrivateData(IDirectDrawSurface7 *iface,
1126                                       REFGUID tag,
1127                                       void *Data,
1128                                       DWORD *Size)
1129 {
1130     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1131     TRACE("(%p)->(%s,%p,%p): Relay\n", This, debugstr_guid(tag), Data, Size);
1132
1133     if(!Data)
1134         return DDERR_INVALIDPARAMS;
1135
1136     return IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1137                                           tag,
1138                                           Data,
1139                                           Size);
1140 }
1141
1142 /*****************************************************************************
1143  * IDirectDrawSurface7::FreePrivateData
1144  *
1145  * Frees private data stored in the surface
1146  *
1147  * Params:
1148  *  tag: Tag of the data to free
1149  *
1150  * Returns:
1151  *  D3D_OK on success
1152  *  For more details, see IWineD3DSurface::FreePrivateData
1153  *
1154  *****************************************************************************/
1155 static HRESULT WINAPI
1156 IDirectDrawSurfaceImpl_FreePrivateData(IDirectDrawSurface7 *iface,
1157                                        REFGUID tag)
1158 {
1159     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1160     TRACE("(%p)->(%s): Relay\n", This, debugstr_guid(tag));
1161
1162     return IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1163 }
1164
1165 /*****************************************************************************
1166  * IDirectDrawSurface7::PageLock
1167  *
1168  * Prevents a sysmem surface from being paged out
1169  *
1170  * Params:
1171  *  Flags: Not used, must be 0(unchecked)
1172  *
1173  * Returns:
1174  *  DD_OK, because it's a stub
1175  *
1176  *****************************************************************************/
1177 static HRESULT WINAPI
1178 IDirectDrawSurfaceImpl_PageLock(IDirectDrawSurface7 *iface,
1179                                 DWORD Flags)
1180 {
1181     TRACE("(%p)->(%x)\n", iface, Flags);
1182
1183     /* This is Windows memory management related - we don't need this */
1184     return DD_OK;
1185 }
1186
1187 /*****************************************************************************
1188  * IDirectDrawSurface7::PageUnlock
1189  *
1190  * Allows a sysmem surface to be paged out
1191  *
1192  * Params:
1193  *  Flags: Not used, must be 0(unckeched)
1194  *
1195  * Returns:
1196  *  DD_OK, because it's a stub
1197  *
1198  *****************************************************************************/
1199 static HRESULT WINAPI
1200 IDirectDrawSurfaceImpl_PageUnlock(IDirectDrawSurface7 *iface,
1201                                   DWORD Flags)
1202 {
1203     TRACE("(%p)->(%x)\n", iface, Flags);
1204
1205     return DD_OK;
1206 }
1207
1208 /*****************************************************************************
1209  * IDirectDrawSurface7::BltBatch
1210  *
1211  * An unimplemented function
1212  *
1213  * Params:
1214  *  ?
1215  *
1216  * Returns:
1217  *  DDERR_UNSUPPORTED
1218  *
1219  *****************************************************************************/
1220 static HRESULT WINAPI IDirectDrawSurfaceImpl_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1221 {
1222     TRACE("(%p)->(%p,%d,%08x)\n",iface,Batch,Count,Flags);
1223
1224     /* MSDN: "not currently implemented" */
1225     return DDERR_UNSUPPORTED;
1226 }
1227
1228 /*****************************************************************************
1229  * IDirectDrawSurface7::EnumAttachedSurfaces
1230  *
1231  * Enumerates all surfaces attached to this surface
1232  *
1233  * Params:
1234  *  context: Pointer to pass unmodified to the callback
1235  *  cb: Callback function to call for each surface
1236  *
1237  * Returns:
1238  *  DD_OK on success
1239  *  DDERR_INVALIDPARAMS if cb is NULL
1240  *
1241  *****************************************************************************/
1242 static HRESULT WINAPI
1243 IDirectDrawSurfaceImpl_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1244                                             void *context,
1245                                             LPDDENUMSURFACESCALLBACK7 cb)
1246 {
1247     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1248     IDirectDrawSurfaceImpl *surf;
1249     DDSURFACEDESC2 desc;
1250     int i;
1251
1252     /* Attached surfaces aren't handled in WineD3D */
1253     TRACE("(%p)->(%p,%p)\n",This,context,cb);
1254
1255     if(!cb)
1256         return DDERR_INVALIDPARAMS;
1257
1258     for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1259     {
1260         surf = This->complex_array[i];
1261         if(!surf) break;
1262
1263         IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
1264         desc = surf->surface_desc;
1265         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1266         if (cb(ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, context) == DDENUMRET_CANCEL)
1267             return DD_OK;
1268     }
1269
1270     for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1271     {
1272         IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
1273         desc = surf->surface_desc;
1274         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1275         if (cb( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, context) == DDENUMRET_CANCEL)
1276             return DD_OK;
1277     }
1278
1279     TRACE(" end of enumeration.\n");
1280
1281     return DD_OK;
1282 }
1283
1284 /*****************************************************************************
1285  * IDirectDrawSurface7::EnumOverlayZOrders
1286  *
1287  * "Enumerates the overlay surfaces on the specified destination"
1288  *
1289  * Params:
1290  *  Flags: DDENUMOVERLAYZ_BACKTOFRONT  or DDENUMOVERLAYZ_FRONTTOBACK
1291  *  context: context to pass back to the callback
1292  *  cb: callback function to call for each enumerated surface
1293  *
1294  * Returns:
1295  *  DD_OK, because it's a stub
1296  *
1297  *****************************************************************************/
1298 static HRESULT WINAPI
1299 IDirectDrawSurfaceImpl_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1300                                           DWORD Flags,
1301                                           void *context,
1302                                           LPDDENUMSURFACESCALLBACK7 cb)
1303 {
1304      FIXME("(%p)->(%x,%p,%p): Stub!\n", iface, Flags, context, cb);
1305
1306     return DD_OK;
1307 }
1308
1309 /*****************************************************************************
1310  * IDirectDrawSurface7::GetBltStatus
1311  *
1312  * Returns the blitting status
1313  *
1314  * Params:
1315  *  Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1316  *
1317  * Returns:
1318  *  See IWineD3DSurface::Blt
1319  *
1320  *****************************************************************************/
1321 static HRESULT WINAPI
1322 IDirectDrawSurfaceImpl_GetBltStatus(IDirectDrawSurface7 *iface,
1323                                     DWORD Flags)
1324 {
1325     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1326     HRESULT hr;
1327     TRACE("(%p)->(%x): Relay\n", This, Flags);
1328
1329     hr = IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1330     switch(hr)
1331     {
1332         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1333         default:                            return hr;
1334     }
1335 }
1336
1337 /*****************************************************************************
1338  * IDirectDrawSurface7::GetColorKey
1339  *
1340  * Returns the color key assigned to the surface
1341  *
1342  * Params:
1343  *  Flags: Some flags
1344  *  CKey: Address to store the key to
1345  *
1346  * Returns:
1347  *  DD_OK on success
1348  *  DDERR_INVALIDPARAMS if CKey is NULL
1349  *
1350  *****************************************************************************/
1351 static HRESULT WINAPI
1352 IDirectDrawSurfaceImpl_GetColorKey(IDirectDrawSurface7 *iface,
1353                                    DWORD Flags,
1354                                    DDCOLORKEY *CKey)
1355 {
1356     /* There is a DDERR_NOCOLORKEY error, but how do we know if a color key
1357      * isn't there? That's like saying that an int isn't there. (Which MS
1358      * has done in other docs.) */
1359     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1360     TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
1361
1362     if(!CKey)
1363         return DDERR_INVALIDPARAMS;
1364
1365     switch (Flags)
1366     {
1367     case DDCKEY_DESTBLT:
1368         *CKey = This->surface_desc.ddckCKDestBlt;
1369         break;
1370
1371     case DDCKEY_DESTOVERLAY:
1372         *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1373         break;
1374
1375     case DDCKEY_SRCBLT:
1376         *CKey = This->surface_desc.ddckCKSrcBlt;
1377         break;
1378
1379     case DDCKEY_SRCOVERLAY:
1380         *CKey = This->surface_desc.ddckCKSrcOverlay;
1381         break;
1382
1383     default:
1384         return DDERR_INVALIDPARAMS;
1385     }
1386
1387     return DD_OK;
1388 }
1389
1390 /*****************************************************************************
1391  * IDirectDrawSurface7::GetFlipStatus
1392  *
1393  * Returns the flipping status of the surface
1394  *
1395  * Params:
1396  *  Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1397  *
1398  * Returns:
1399  *  See IWineD3DSurface::GetFlipStatus
1400  *
1401  *****************************************************************************/
1402 static HRESULT WINAPI
1403 IDirectDrawSurfaceImpl_GetFlipStatus(IDirectDrawSurface7 *iface,
1404                                      DWORD Flags)
1405 {
1406     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1407     HRESULT hr;
1408     TRACE("(%p)->(%x): Relay\n", This, Flags);
1409
1410     hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1411     switch(hr)
1412     {
1413         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1414         default:                            return hr;
1415     }
1416 }
1417
1418 /*****************************************************************************
1419  * IDirectDrawSurface7::GetOverlayPosition
1420  *
1421  * Returns the display coordinates of a visible and active overlay surface
1422  *
1423  * Params:
1424  *  X
1425  *  Y
1426  *
1427  * Returns:
1428  *  DDERR_NOTAOVERLAYSURFACE, because it's a stub
1429  *****************************************************************************/
1430 static HRESULT WINAPI
1431 IDirectDrawSurfaceImpl_GetOverlayPosition(IDirectDrawSurface7 *iface,
1432                                           LONG *X,
1433                                           LONG *Y) {
1434     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1435     TRACE("(%p)->(%p,%p): Relay\n", This, X, Y);
1436
1437     return IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1438                                               X,
1439                                               Y);
1440 }
1441
1442 /*****************************************************************************
1443  * IDirectDrawSurface7::GetPixelFormat
1444  *
1445  * Returns the pixel format of the Surface
1446  *
1447  * Params:
1448  *  PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1449  *               format should be written
1450  *
1451  * Returns:
1452  *  DD_OK on success
1453  *  DDERR_INVALIDPARAMS if PixelFormat is NULL
1454  *
1455  *****************************************************************************/
1456 static HRESULT WINAPI
1457 IDirectDrawSurfaceImpl_GetPixelFormat(IDirectDrawSurface7 *iface,
1458                                       DDPIXELFORMAT *PixelFormat)
1459 {
1460     /* What is DDERR_INVALIDSURFACETYPE for here? */
1461     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1462     TRACE("(%p)->(%p)\n",This,PixelFormat);
1463
1464     if(!PixelFormat)
1465         return DDERR_INVALIDPARAMS;
1466
1467     DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1468
1469     return DD_OK;
1470 }
1471
1472
1473 /*****************************************************************************
1474  * IDirectDrawSurface7::GetSurfaceDesc
1475  *
1476  * Returns the description of this surface
1477  *
1478  * Params:
1479  *  DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1480  *        surface desc
1481  *
1482  * Returns:
1483  *  DD_OK on success
1484  *  DDERR_INVALIDPARAMS if DDSD is NULL
1485  *
1486  *****************************************************************************/
1487 static HRESULT WINAPI
1488 IDirectDrawSurfaceImpl_GetSurfaceDesc(IDirectDrawSurface7 *iface,
1489                                       DDSURFACEDESC2 *DDSD)
1490 {
1491     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1492
1493     TRACE("(%p)->(%p)\n",This,DDSD);
1494
1495     if(!DDSD)
1496         return DDERR_INVALIDPARAMS;
1497
1498     if ((DDSD->dwSize < sizeof(DDSURFACEDESC)) ||
1499         (DDSD->dwSize > sizeof(DDSURFACEDESC2)))
1500     {
1501         ERR("Impossible/Strange struct size %d.\n",DDSD->dwSize);
1502         return DDERR_GENERIC;
1503     }
1504
1505     DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1506     TRACE("Returning surface desc:\n");
1507     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1508
1509     return DD_OK;
1510 }
1511
1512 /*****************************************************************************
1513  * IDirectDrawSurface7::Initialize
1514  *
1515  * Initializes the surface. This is a no-op in Wine
1516  *
1517  * Params:
1518  *  DD: Pointer to an DirectDraw interface
1519  *  DDSD: Surface description for initialization
1520  *
1521  * Returns:
1522  *  DDERR_ALREADYINITIALIZED
1523  *
1524  *****************************************************************************/
1525 static HRESULT WINAPI
1526 IDirectDrawSurfaceImpl_Initialize(IDirectDrawSurface7 *iface,
1527                                   IDirectDraw *DD,
1528                                   DDSURFACEDESC2 *DDSD)
1529 {
1530     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1531     IDirectDrawImpl *ddimpl = ICOM_OBJECT(IDirectDrawImpl, IDirectDraw, DD);
1532     TRACE("(%p)->(%p,%p)\n",This,ddimpl,DDSD);
1533
1534     return DDERR_ALREADYINITIALIZED;
1535 }
1536
1537 /*****************************************************************************
1538  * IDirectDrawSurface7::IsLost
1539  *
1540  * Checks if the surface is lost
1541  *
1542  * Returns:
1543  *  DD_OK, if the surface is useable
1544  *  DDERR_ISLOST if the surface is lost
1545  *  See IWineD3DSurface::IsLost for more details
1546  *
1547  *****************************************************************************/
1548 static HRESULT WINAPI
1549 IDirectDrawSurfaceImpl_IsLost(IDirectDrawSurface7 *iface)
1550 {
1551     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1552     HRESULT hr;
1553     TRACE("(%p)\n", This);
1554
1555     /* We lose the surface if the implementation was changed */
1556     if(This->ImplType != This->ddraw->ImplType)
1557     {
1558         /* But this shouldn't happen. When we change the implementation,
1559          * all surfaces are re-created automatically, and their content
1560          * is copied
1561          */
1562         ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1563         return DDERR_SURFACELOST;
1564     }
1565
1566     hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
1567     switch(hr)
1568     {
1569         /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1570          * WineD3D uses the same error for surfaces
1571          */
1572         case WINED3DERR_DEVICELOST:         return DDERR_SURFACELOST;
1573         default:                            return hr;
1574     }
1575 }
1576
1577 /*****************************************************************************
1578  * IDirectDrawSurface7::Restore
1579  *
1580  * Restores a lost surface. This makes the surface usable again, but
1581  * doesn't reload its old contents
1582  *
1583  * Returns:
1584  *  DD_OK on success
1585  *  See IWineD3DSurface::Restore for more details
1586  *
1587  *****************************************************************************/
1588 static HRESULT WINAPI
1589 IDirectDrawSurfaceImpl_Restore(IDirectDrawSurface7 *iface)
1590 {
1591     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1592     TRACE("(%p)\n", This);
1593
1594     if(This->ImplType != This->ddraw->ImplType)
1595     {
1596         /* Call the recreation callback. Make sure to AddRef first */
1597         IDirectDrawSurface_AddRef(iface);
1598         IDirectDrawImpl_RecreateSurfacesCallback(iface,
1599                                                  &This->surface_desc,
1600                                                  NULL /* Not needed */);
1601     }
1602     return IWineD3DSurface_Restore(This->WineD3DSurface);
1603 }
1604
1605 /*****************************************************************************
1606  * IDirectDrawSurface7::SetOverlayPosition
1607  *
1608  * Changes the display coordinates of an overlay surface
1609  *
1610  * Params:
1611  *  X:
1612  *  Y:
1613  *
1614  * Returns:
1615  *   DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
1616  *****************************************************************************/
1617 static HRESULT WINAPI
1618 IDirectDrawSurfaceImpl_SetOverlayPosition(IDirectDrawSurface7 *iface,
1619                                           LONG X,
1620                                           LONG Y)
1621 {
1622     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1623     TRACE("(%p)->(%d,%d): Relay\n", This, X, Y);
1624
1625     return IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
1626                                               X,
1627                                               Y);
1628 }
1629
1630 /*****************************************************************************
1631  * IDirectDrawSurface7::UpdateOverlay
1632  *
1633  * Modifies the attributes of an overlay surface.
1634  *
1635  * Params:
1636  *  SrcRect: The section of the source being used for the overlay
1637  *  DstSurface: Address of the surface that is overlaid
1638  *  DstRect: Place of the overlay
1639  *  Flags: some DDOVER_* flags
1640  *
1641  * Returns:
1642  *  DDERR_UNSUPPORTED, because we don't support overlays
1643  *
1644  *****************************************************************************/
1645 static HRESULT WINAPI
1646 IDirectDrawSurfaceImpl_UpdateOverlay(IDirectDrawSurface7 *iface,
1647                                      LPRECT SrcRect,
1648                                      IDirectDrawSurface7 *DstSurface,
1649                                      LPRECT DstRect,
1650                                      DWORD Flags,
1651                                      LPDDOVERLAYFX FX)
1652 {
1653     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1654     IDirectDrawSurfaceImpl *Dst = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DstSurface);
1655     TRACE("(%p)->(%p,%p,%p,%x,%p): Relay\n", This, SrcRect, Dst, DstRect, Flags, FX);
1656
1657     return IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
1658                                          SrcRect,
1659                                          Dst ? Dst->WineD3DSurface : NULL,
1660                                          DstRect,
1661                                          Flags,
1662                                          (WINEDDOVERLAYFX *) FX);
1663 }
1664
1665 /*****************************************************************************
1666  * IDirectDrawSurface7::UpdateOverlayDisplay
1667  *
1668  * The DX7 sdk says that it's not implemented
1669  *
1670  * Params:
1671  *  Flags: ?
1672  *
1673  * Returns: DDERR_UNSUPPORTED, because we don't support overlays
1674  *
1675  *****************************************************************************/
1676 static HRESULT WINAPI
1677 IDirectDrawSurfaceImpl_UpdateOverlayDisplay(IDirectDrawSurface7 *iface,
1678                                             DWORD Flags)
1679 {
1680     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1681     TRACE("(%p)->(%x)\n", This, Flags);
1682     return DDERR_UNSUPPORTED;
1683 }
1684
1685 /*****************************************************************************
1686  * IDirectDrawSurface7::UpdateOverlayZOrder
1687  *
1688  * Sets an overlay's Z order
1689  *
1690  * Params:
1691  *  Flags: DDOVERZ_* flags
1692  *  DDSRef: Defines the relative position in the overlay chain
1693  *
1694  * Returns:
1695  *  DDERR_NOTOVERLAYSURFACE, because we don't support overlays
1696  *
1697  *****************************************************************************/
1698 static HRESULT WINAPI
1699 IDirectDrawSurfaceImpl_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
1700                                            DWORD Flags,
1701                                            IDirectDrawSurface7 *DDSRef)
1702 {
1703     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1704     IDirectDrawSurfaceImpl *Ref = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DDSRef);
1705
1706     TRACE("(%p)->(%x,%p): Relay\n", This, Flags, Ref);
1707     return IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
1708                                                Flags,
1709                                                Ref ? Ref->WineD3DSurface : NULL);
1710 }
1711
1712 /*****************************************************************************
1713  * IDirectDrawSurface7::GetDDInterface
1714  *
1715  * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
1716  * surface belongs to
1717  *
1718  * Params:
1719  *  DD: Address to write the interface pointer to
1720  *
1721  * Returns:
1722  *  DD_OK on success
1723  *  DDERR_INVALIDPARAMS if DD is NULL
1724  *
1725  *****************************************************************************/
1726 static HRESULT WINAPI
1727 IDirectDrawSurfaceImpl_GetDDInterface(IDirectDrawSurface7 *iface,
1728                                       void **DD)
1729 {
1730     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1731
1732     TRACE("(%p)->(%p)\n",This,DD);
1733
1734     if(!DD)
1735         return DDERR_INVALIDPARAMS;
1736
1737     switch(This->version)
1738     {
1739         case 7:
1740             *((IDirectDraw7 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw7);
1741             IDirectDraw7_AddRef(*(IDirectDraw7 **) DD);
1742             break;
1743
1744         case 4:
1745             *((IDirectDraw4 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw4);
1746             IDirectDraw4_AddRef(*(IDirectDraw4 **) DD);
1747             break;
1748
1749         case 2:
1750             *((IDirectDraw2 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw2);
1751             IDirectDraw_AddRef( *(IDirectDraw2 **) DD);
1752             break;
1753
1754         case 1:
1755             *((IDirectDraw **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw);
1756             IDirectDraw_AddRef( *(IDirectDraw **) DD);
1757             break;
1758
1759     }
1760
1761     return DD_OK;
1762 }
1763
1764 /* This seems also windows implementation specific - I don't think WineD3D needs this */
1765 static HRESULT WINAPI IDirectDrawSurfaceImpl_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
1766 {
1767     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1768     volatile IDirectDrawSurfaceImpl* vThis = This;
1769
1770     TRACE("(%p)\n",This);
1771     /* A uniqueness value of 0 is apparently special.
1772     * This needs to be checked. */
1773     while (1) {
1774         DWORD old_uniqueness_value = vThis->uniqueness_value;
1775         DWORD new_uniqueness_value = old_uniqueness_value+1;
1776
1777         if (old_uniqueness_value == 0) break;
1778         if (new_uniqueness_value == 0) new_uniqueness_value = 1;
1779
1780         if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
1781                                       old_uniqueness_value,
1782                                       new_uniqueness_value)
1783             == old_uniqueness_value)
1784             break;
1785     }
1786
1787     return DD_OK;
1788 }
1789
1790 static HRESULT WINAPI IDirectDrawSurfaceImpl_GetUniquenessValue(IDirectDrawSurface7 *iface, LPDWORD pValue)
1791 {
1792     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1793
1794     TRACE("(%p)->(%p)\n",This,pValue);
1795     *pValue = This->uniqueness_value;
1796     return DD_OK;
1797 }
1798
1799 /*****************************************************************************
1800  * IDirectDrawSurface7::SetLOD
1801  *
1802  * Sets the level of detail of a texture
1803  *
1804  * Params:
1805  *  MaxLOD: LOD to set
1806  *
1807  * Returns:
1808  *  DD_OK on success
1809  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
1810  *
1811  *****************************************************************************/
1812 static HRESULT WINAPI
1813 IDirectDrawSurfaceImpl_SetLOD(IDirectDrawSurface7 *iface,
1814                               DWORD MaxLOD)
1815 {
1816     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1817     TRACE("(%p)->(%d)\n", This, MaxLOD);
1818
1819     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
1820         return DDERR_INVALIDOBJECT;
1821
1822     if(!This->wineD3DTexture)
1823     {
1824         ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
1825         return DDERR_INVALIDOBJECT;
1826     }
1827
1828     return IWineD3DBaseTexture_SetLOD(This->wineD3DTexture,
1829                                       MaxLOD);
1830 }
1831
1832 /*****************************************************************************
1833  * IDirectDrawSurface7::GetLOD
1834  *
1835  * Returns the level of detail of a Direct3D texture
1836  *
1837  * Params:
1838  *  MaxLOD: Address to write the LOD to
1839  *
1840  * Returns:
1841  *  DD_OK on success
1842  *  DDERR_INVALIDPARAMS if MaxLOD is NULL
1843  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
1844  *
1845  *****************************************************************************/
1846 static HRESULT WINAPI
1847 IDirectDrawSurfaceImpl_GetLOD(IDirectDrawSurface7 *iface,
1848                               DWORD *MaxLOD)
1849 {
1850     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1851     TRACE("(%p)->(%p)\n", This, MaxLOD);
1852
1853     if(!MaxLOD)
1854         return DDERR_INVALIDPARAMS;
1855
1856     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
1857         return DDERR_INVALIDOBJECT;
1858
1859     *MaxLOD = IWineD3DBaseTexture_GetLOD(This->wineD3DTexture);
1860     return DD_OK;
1861 }
1862
1863 /*****************************************************************************
1864  * IDirectDrawSurface7::BltFast
1865  *
1866  * Performs a fast Blit.
1867  *
1868  * Params:
1869  *  dstx: The x coordinate to blit to on the destination
1870  *  dsty: The y coordinate to blit to on the destination
1871  *  Source: The source surface
1872  *  rsrc: The source rectangle
1873  *  trans: Type of transfer. Some DDBLTFAST_* flags
1874  *
1875  * Returns:
1876  *  DD_OK on success
1877  *  For more details, see IWineD3DSurface::BltFast
1878  *
1879  *****************************************************************************/
1880 static HRESULT WINAPI
1881 IDirectDrawSurfaceImpl_BltFast(IDirectDrawSurface7 *iface,
1882                                DWORD dstx,
1883                                DWORD dsty,
1884                                IDirectDrawSurface7 *Source,
1885                                RECT *rsrc,
1886                                DWORD trans)
1887 {
1888     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1889     HRESULT hr;
1890     IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Source);
1891     TRACE("(%p)->(%d,%d,%p,%p,%d): Relay\n", This, dstx, dsty, Source, rsrc, trans);
1892
1893     hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
1894                                  dstx, dsty,
1895                                  src ? src->WineD3DSurface : NULL,
1896                                  rsrc,
1897                                  trans);
1898     switch(hr)
1899     {
1900         case WINED3DERR_NOTAVAILABLE:           return DDERR_UNSUPPORTED;
1901         case WINED3DERR_WRONGTEXTUREFORMAT:     return DDERR_INVALIDPIXELFORMAT;
1902         default:                                return hr;
1903     }
1904 }
1905
1906 /*****************************************************************************
1907  * IDirectDrawSurface7::GetClipper
1908  *
1909  * Returns the IDirectDrawClipper interface of the clipper assigned to this
1910  * surface
1911  *
1912  * Params:
1913  *  Clipper: Address to store the interface pointer at
1914  *
1915  * Returns:
1916  *  DD_OK on success
1917  *  DDERR_INVALIDPARAMS if Clipper is NULL
1918  *  DDERR_NOCLIPPERATTACHED if there's no clipper attached
1919  *
1920  *****************************************************************************/
1921 static HRESULT WINAPI
1922 IDirectDrawSurfaceImpl_GetClipper(IDirectDrawSurface7 *iface,
1923                                   IDirectDrawClipper **Clipper)
1924 {
1925     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1926     TRACE("(%p)->(%p)\n", This, Clipper);
1927
1928     if(!Clipper)
1929         return DDERR_INVALIDPARAMS;
1930
1931     if(This->clipper == NULL)
1932         return DDERR_NOCLIPPERATTACHED;
1933
1934     *Clipper = ICOM_INTERFACE(This->clipper, IDirectDrawClipper);
1935     IDirectDrawClipper_AddRef(*Clipper);
1936     return DD_OK;
1937 }
1938
1939 /*****************************************************************************
1940  * IDirectDrawSurface7::SetClipper
1941  *
1942  * Sets a clipper for the surface
1943  *
1944  * Params:
1945  *  Clipper: IDirectDrawClipper interface of the clipper to set
1946  *
1947  * Returns:
1948  *  DD_OK on success
1949  *
1950  *****************************************************************************/
1951 static HRESULT WINAPI
1952 IDirectDrawSurfaceImpl_SetClipper(IDirectDrawSurface7 *iface,
1953                                   IDirectDrawClipper *Clipper)
1954 {
1955     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1956     IDirectDrawClipperImpl *oldClipper = This->clipper;
1957
1958     TRACE("(%p)->(%p)\n",This,Clipper);
1959     if (ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper) == This->clipper)
1960         return DD_OK;
1961
1962     This->clipper = ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper);
1963
1964     if (Clipper != NULL)
1965         IDirectDrawClipper_AddRef(Clipper);
1966     if(oldClipper)
1967         IDirectDrawClipper_Release(ICOM_INTERFACE(oldClipper, IDirectDrawClipper));
1968
1969     return DD_OK;
1970 }
1971
1972 /*****************************************************************************
1973  * IDirectDrawSurface7::SetSurfaceDesc
1974  *
1975  * Sets the surface description. It can override the pixel format, the surface
1976  * memory, ...
1977  * It's not really tested.
1978  *
1979  * Params:
1980  * DDSD: Pointer to the new surface description to set
1981  * Flags: Some flags
1982  *
1983  * Returns:
1984  *  DD_OK on success
1985  *  DDERR_INVALIDPARAMS if DDSD is NULL
1986  *
1987  *****************************************************************************/
1988 static HRESULT WINAPI
1989 IDirectDrawSurfaceImpl_SetSurfaceDesc(IDirectDrawSurface7 *iface,
1990                                       DDSURFACEDESC2 *DDSD,
1991                                       DWORD Flags)
1992 {
1993     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1994     WINED3DFORMAT newFormat = WINED3DFMT_UNKNOWN;
1995     HRESULT hr;
1996     TRACE("(%p)->(%p,%x)\n", This, DDSD, Flags);
1997
1998     if(!DDSD)
1999         return DDERR_INVALIDPARAMS;
2000
2001     if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2002     {
2003         ERR("Setting the surface memory isn't supported yet\n");
2004         return DDERR_INVALIDPARAMS;
2005
2006     }
2007     if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2008     {
2009         newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2010
2011         if(newFormat == WINED3DFMT_UNKNOWN)
2012         {
2013             ERR("Requested to set an unknown pixelformat\n");
2014             return DDERR_INVALIDPARAMS;
2015         }
2016         if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2017         {
2018             hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2019                                            newFormat);
2020             if(hr != DD_OK) return hr;
2021         }
2022     }
2023     if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2024     {
2025         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2026                                     DDCKEY_DESTOVERLAY,
2027                                     (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2028     }
2029     if (DDSD->dwFlags & DDSD_CKDESTBLT)
2030     {
2031         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2032                                     DDCKEY_DESTBLT,
2033                                     (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2034     }
2035     if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2036     {
2037         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2038                                     DDCKEY_SRCOVERLAY,
2039                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2040     }
2041     if (DDSD->dwFlags & DDSD_CKSRCBLT)
2042     {
2043         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2044                                     DDCKEY_SRCBLT,
2045                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2046     }
2047     if (DDSD->dwFlags & DDSD_LPSURFACE)
2048     {
2049         hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2050         if(hr != WINED3D_OK)
2051         {
2052             /* No need for a trace here, wined3d does that for us */
2053             return hr;
2054         }
2055     }
2056
2057     This->surface_desc = *DDSD;
2058
2059     return DD_OK;
2060 }
2061
2062 /*****************************************************************************
2063  * IDirectDrawSurface7::GetPalette
2064  *
2065  * Returns the IDirectDrawPalette interface of the palette currently assigned
2066  * to the surface
2067  *
2068  * Params:
2069  *  Pal: Address to write the interface pointer to
2070  *
2071  * Returns:
2072  *  DD_OK on success
2073  *  DDERR_INVALIDPARAMS if Pal is NULL
2074  *
2075  *****************************************************************************/
2076 static HRESULT WINAPI
2077 IDirectDrawSurfaceImpl_GetPalette(IDirectDrawSurface7 *iface,
2078                                   IDirectDrawPalette **Pal)
2079 {
2080     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2081     IWineD3DPalette *wPal;
2082     HRESULT hr;
2083     TRACE("(%p)->(%p): Relay\n", This, Pal);
2084
2085     if(!Pal)
2086         return DDERR_INVALIDPARAMS;
2087
2088     hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
2089     if(hr != DD_OK) return hr;
2090
2091     if(wPal)
2092     {
2093         hr = IWineD3DPalette_GetParent(wPal, (IUnknown **) Pal);
2094     }
2095     else
2096     {
2097         *Pal = NULL;
2098         hr = DDERR_NOPALETTEATTACHED;
2099     }
2100
2101     return hr;
2102 }
2103
2104 /*****************************************************************************
2105  * SetColorKeyEnum
2106  *
2107  * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2108  * recursively in the surface tree
2109  *
2110  *****************************************************************************/
2111 struct SCKContext
2112 {
2113     HRESULT ret;
2114     WINEDDCOLORKEY *CKey;
2115     DWORD Flags;
2116 };
2117
2118 static HRESULT WINAPI
2119 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2120                 DDSURFACEDESC2 *desc,
2121                 void *context)
2122 {
2123     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2124     struct SCKContext *ctx = context;
2125     HRESULT hr;
2126
2127     hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2128                                      ctx->Flags,
2129                                      ctx->CKey);
2130     if(hr != DD_OK)
2131     {
2132         WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2133         ctx->ret = hr;
2134     }
2135
2136     IDirectDrawSurface7_EnumAttachedSurfaces(surface,
2137                                              context,
2138                                              SetColorKeyEnum);
2139     IDirectDrawSurface7_Release(surface);
2140     return DDENUMRET_OK;
2141 }
2142
2143 /*****************************************************************************
2144  * IDirectDrawSurface7::SetColorKey
2145  *
2146  * Sets the color keying options for the surface. Observations showed that
2147  * in case of complex surfaces the color key has to be assigned to all
2148  * sublevels.
2149  *
2150  * Params:
2151  *  Flags: DDCKEY_*
2152  *  CKey: The new color key
2153  *
2154  * Returns:
2155  *  DD_OK on success
2156  *  See IWineD3DSurface::SetColorKey for details
2157  *
2158  *****************************************************************************/
2159 static HRESULT WINAPI
2160 IDirectDrawSurfaceImpl_SetColorKey(IDirectDrawSurface7 *iface,
2161                                    DWORD Flags,
2162                                    DDCOLORKEY *CKey)
2163 {
2164     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2165     struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) CKey, Flags };
2166     TRACE("(%p)->(%x,%p)\n", This, Flags, CKey);
2167
2168     if (CKey)
2169     {
2170         switch (Flags & ~DDCKEY_COLORSPACE)
2171         {
2172         case DDCKEY_DESTBLT:
2173             This->surface_desc.ddckCKDestBlt = *CKey;
2174             This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2175             break;
2176
2177         case DDCKEY_DESTOVERLAY:
2178             This->surface_desc.u3.ddckCKDestOverlay = *CKey;
2179             This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2180             break;
2181
2182         case DDCKEY_SRCOVERLAY:
2183             This->surface_desc.ddckCKSrcOverlay = *CKey;
2184             This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2185             break;
2186
2187         case DDCKEY_SRCBLT:
2188             This->surface_desc.ddckCKSrcBlt = *CKey;
2189             This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2190             break;
2191
2192         default:
2193             return DDERR_INVALIDPARAMS;
2194         }
2195     }
2196     else
2197     {
2198         switch (Flags & ~DDCKEY_COLORSPACE)
2199         {
2200         case DDCKEY_DESTBLT:
2201             This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2202             break;
2203
2204         case DDCKEY_DESTOVERLAY:
2205             This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2206             break;
2207
2208         case DDCKEY_SRCOVERLAY:
2209             This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2210             break;
2211
2212         case DDCKEY_SRCBLT:
2213             This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2214             break;
2215
2216         default:
2217             return DDERR_INVALIDPARAMS;
2218         }
2219     }
2220     ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2221                                           Flags,
2222                                           ctx.CKey);
2223     IDirectDrawSurface7_EnumAttachedSurfaces(iface,
2224                                              (void *) &ctx,
2225                                              SetColorKeyEnum);
2226     switch(ctx.ret)
2227     {
2228         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2229         default:                            return ctx.ret;
2230     }
2231 }
2232
2233 /*****************************************************************************
2234  * IDirectDrawSurface7::SetPalette
2235  *
2236  * Assigns a DirectDrawPalette object to the surface
2237  *
2238  * Params:
2239  *  Pal: Interface to the palette to set
2240  *
2241  * Returns:
2242  *  DD_OK on success
2243  *
2244  *****************************************************************************/
2245 static HRESULT WINAPI
2246 IDirectDrawSurfaceImpl_SetPalette(IDirectDrawSurface7 *iface,
2247                                   IDirectDrawPalette *Pal)
2248 {
2249     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2250     IDirectDrawPalette *oldPal;
2251     IDirectDrawSurfaceImpl *surf;
2252     IDirectDrawPaletteImpl *PalImpl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, Pal);
2253     HRESULT hr;
2254     TRACE("(%p)->(%p)\n", This, Pal);
2255
2256     /* Find the old palette */
2257     hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2258     if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED) return hr;
2259     if(oldPal) IDirectDrawPalette_Release(oldPal);  /* For the GetPalette */
2260
2261     /* Set the new Palette */
2262     IWineD3DSurface_SetPalette(This->WineD3DSurface,
2263                                PalImpl ? PalImpl->wineD3DPalette : NULL);
2264     /* AddRef the Palette */
2265     if(Pal) IDirectDrawPalette_AddRef(Pal);
2266
2267     /* Release the old palette */
2268     if(oldPal) IDirectDrawPalette_Release(oldPal);
2269
2270     /* If this is a front buffer, also update the back buffers
2271      * TODO: How do things work for palettized cube textures?
2272      */
2273     if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2274     {
2275         /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2276         DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2277
2278         surf = This;
2279         while(1)
2280         {
2281             IDirectDrawSurface7 *attach;
2282             HRESULT hr;
2283             hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
2284                                                         &caps2, &attach);
2285             if(hr != DD_OK)
2286             {
2287                 break;
2288             }
2289
2290             TRACE("Setting palette on %p\n", attach);
2291             IDirectDrawSurface7_SetPalette(attach,
2292                                            Pal);
2293             surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attach);
2294             IDirectDrawSurface7_Release(attach);
2295         }
2296     }
2297
2298     return DD_OK;
2299 }
2300
2301 /*****************************************************************************
2302  * The VTable
2303  *****************************************************************************/
2304
2305 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl =
2306 {
2307     /*** IUnknown ***/
2308     IDirectDrawSurfaceImpl_QueryInterface,
2309     IDirectDrawSurfaceImpl_AddRef,
2310     IDirectDrawSurfaceImpl_Release,
2311     /*** IDirectDrawSurface ***/
2312     IDirectDrawSurfaceImpl_AddAttachedSurface,
2313     IDirectDrawSurfaceImpl_AddOverlayDirtyRect,
2314     IDirectDrawSurfaceImpl_Blt,
2315     IDirectDrawSurfaceImpl_BltBatch,
2316     IDirectDrawSurfaceImpl_BltFast,
2317     IDirectDrawSurfaceImpl_DeleteAttachedSurface,
2318     IDirectDrawSurfaceImpl_EnumAttachedSurfaces,
2319     IDirectDrawSurfaceImpl_EnumOverlayZOrders,
2320     IDirectDrawSurfaceImpl_Flip,
2321     IDirectDrawSurfaceImpl_GetAttachedSurface,
2322     IDirectDrawSurfaceImpl_GetBltStatus,
2323     IDirectDrawSurfaceImpl_GetCaps,
2324     IDirectDrawSurfaceImpl_GetClipper,
2325     IDirectDrawSurfaceImpl_GetColorKey,
2326     IDirectDrawSurfaceImpl_GetDC,
2327     IDirectDrawSurfaceImpl_GetFlipStatus,
2328     IDirectDrawSurfaceImpl_GetOverlayPosition,
2329     IDirectDrawSurfaceImpl_GetPalette,
2330     IDirectDrawSurfaceImpl_GetPixelFormat,
2331     IDirectDrawSurfaceImpl_GetSurfaceDesc,
2332     IDirectDrawSurfaceImpl_Initialize,
2333     IDirectDrawSurfaceImpl_IsLost,
2334     IDirectDrawSurfaceImpl_Lock,
2335     IDirectDrawSurfaceImpl_ReleaseDC,
2336     IDirectDrawSurfaceImpl_Restore,
2337     IDirectDrawSurfaceImpl_SetClipper,
2338     IDirectDrawSurfaceImpl_SetColorKey,
2339     IDirectDrawSurfaceImpl_SetOverlayPosition,
2340     IDirectDrawSurfaceImpl_SetPalette,
2341     IDirectDrawSurfaceImpl_Unlock,
2342     IDirectDrawSurfaceImpl_UpdateOverlay,
2343     IDirectDrawSurfaceImpl_UpdateOverlayDisplay,
2344     IDirectDrawSurfaceImpl_UpdateOverlayZOrder,
2345     /*** IDirectDrawSurface2 ***/
2346     IDirectDrawSurfaceImpl_GetDDInterface,
2347     IDirectDrawSurfaceImpl_PageLock,
2348     IDirectDrawSurfaceImpl_PageUnlock,
2349     /*** IDirectDrawSurface3 ***/
2350     IDirectDrawSurfaceImpl_SetSurfaceDesc,
2351     /*** IDirectDrawSurface4 ***/
2352     IDirectDrawSurfaceImpl_SetPrivateData,
2353     IDirectDrawSurfaceImpl_GetPrivateData,
2354     IDirectDrawSurfaceImpl_FreePrivateData,
2355     IDirectDrawSurfaceImpl_GetUniquenessValue,
2356     IDirectDrawSurfaceImpl_ChangeUniquenessValue,
2357     /*** IDirectDrawSurface7 ***/
2358     IDirectDrawSurfaceImpl_SetPriority,
2359     IDirectDrawSurfaceImpl_GetPriority,
2360     IDirectDrawSurfaceImpl_SetLOD,
2361     IDirectDrawSurfaceImpl_GetLOD
2362 };