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