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