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