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