ddraw: Merge the surface thunks into surface.c.
[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 ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
74 {
75     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
76
77     /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
78     *obj = NULL;
79
80     if(!riid)
81         return DDERR_INVALIDPARAMS;
82
83     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),obj);
84     if (IsEqualGUID(riid, &IID_IUnknown)
85      || IsEqualGUID(riid, &IID_IDirectDrawSurface7)
86      || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
87     {
88         IUnknown_AddRef(iface);
89         *obj = iface;
90         TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
91         return S_OK;
92     }
93     else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
94           || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
95           || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
96     {
97         IUnknown_AddRef(iface);
98         *obj = &This->IDirectDrawSurface3_vtbl;
99         TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
100         return S_OK;
101     }
102     else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
103     {
104         IUnknown_AddRef(iface);
105         *obj = &This->IDirectDrawGammaControl_vtbl;
106         TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
107         return S_OK;
108     }
109     else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
110              IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
111              IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
112     {
113         IDirect3DDevice7 *d3d;
114
115         /* Call into IDirect3D7 for creation */
116         IDirect3D7_CreateDevice((IDirect3D7 *)&This->ddraw->IDirect3D7_vtbl, riid, (IDirectDrawSurface7 *)This, &d3d);
117
118         if (d3d)
119         {
120             *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
121             TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
122             return S_OK;
123         }
124
125         WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
126         return E_NOINTERFACE;
127     }
128     else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
129              IsEqualGUID( &IID_IDirect3DTexture2, riid ))
130     {
131         if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
132         {
133             *obj = &This->IDirect3DTexture_vtbl;
134             TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
135         }
136         else
137         {
138             *obj = &This->IDirect3DTexture2_vtbl;
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 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
150 {
151     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
152
153     return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), riid, object);
154 }
155
156 /*****************************************************************************
157  * IDirectDrawSurface7::AddRef
158  *
159  * A normal addref implementation
160  *
161  * Returns:
162  *  The new refcount
163  *
164  *****************************************************************************/
165 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
166 {
167     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
168     ULONG refCount = InterlockedIncrement(&This->ref);
169
170     if (refCount == 1 && This->WineD3DSurface)
171     {
172         EnterCriticalSection(&ddraw_cs);
173         IWineD3DSurface_AddRef(This->WineD3DSurface);
174         LeaveCriticalSection(&ddraw_cs);
175     }
176
177     TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
178     return refCount;
179 }
180
181 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
182 {
183     TRACE("iface %p.\n", iface);
184
185     return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_surface3(iface));
186 }
187
188 /*****************************************************************************
189  * ddraw_surface_destroy
190  *
191  * A helper function for IDirectDrawSurface7::Release
192  *
193  * Frees the surface, regardless of its refcount.
194  *  See IDirectDrawSurface7::Release for more information
195  *
196  * Params:
197  *  This: Surface to free
198  *
199  *****************************************************************************/
200 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
201 {
202     TRACE("(%p)\n", This);
203
204     /* Check the refcount and give a warning */
205     if(This->ref > 1)
206     {
207         /* This can happen when a complex surface is destroyed,
208          * because the 2nd surface was addref()ed when the app
209          * called GetAttachedSurface
210          */
211         WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
212     }
213
214     /* Check for attached surfaces and detach them */
215     if(This->first_attached != This)
216     {
217         /* Well, this shouldn't happen: The surface being attached is addref()ed
218           * in AddAttachedSurface, so it shouldn't be released until DeleteAttachedSurface
219           * is called, because the refcount is held. It looks like the app released()
220           * it often enough to force this
221           */
222         IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)This->first_attached;
223         IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)This;
224
225         FIXME("(%p) Freeing a surface that is attached to surface %p\n", This, This->first_attached);
226
227         /* The refcount will drop to -1 here */
228         if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
229         {
230             ERR("(%p) DeleteAttachedSurface failed!\n", This);
231         }
232     }
233
234     while(This->next_attached != NULL)
235     {
236         IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)This;
237         IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)This->next_attached;
238
239         if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
240         {
241             ERR("(%p) DeleteAttachedSurface failed!\n", This);
242             assert(0);
243         }
244     }
245
246     /* Now destroy the surface. Wait: It could have been released if we are a texture */
247     if(This->WineD3DSurface)
248         IWineD3DSurface_Release(This->WineD3DSurface);
249
250     /* Having a texture handle set implies that the device still exists */
251     if(This->Handle)
252     {
253         This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
254         This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
255     }
256
257     /* Reduce the ddraw surface count */
258     InterlockedDecrement(&This->ddraw->surfaces);
259     list_remove(&This->surface_list_entry);
260
261     HeapFree(GetProcessHeap(), 0, This);
262 }
263
264 /*****************************************************************************
265  * IDirectDrawSurface7::Release
266  *
267  * Reduces the surface's refcount by 1. If the refcount falls to 0, the
268  * surface is destroyed.
269  *
270  * Destroying the surface is a bit tricky. For the connection between
271  * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
272  * It has a nice graph explaining the connection.
273  *
274  * What happens here is basically this:
275  * When a surface is destroyed, its WineD3DSurface is released,
276  * and the refcount of the DirectDraw interface is reduced by 1. If it has
277  * complex surfaces attached to it, then these surfaces are destroyed too,
278  * regardless of their refcount. If any surface being destroyed has another
279  * surface attached to it (with a "soft" attachment, not complex), then
280  * this surface is detached with DeleteAttachedSurface.
281  *
282  * When the surface is a texture, the WineD3DTexture is released.
283  * If the surface is the Direct3D render target, then the D3D
284  * capabilities of the WineD3DDevice are uninitialized, which causes the
285  * swapchain to be released.
286  *
287  * When a complex sublevel falls to ref zero, then this is ignored.
288  *
289  * Returns:
290  *  The new refcount
291  *
292  *****************************************************************************/
293 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
294 {
295     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
296     ULONG ref;
297     TRACE("(%p) : Releasing from %d\n", This, This->ref);
298     ref = InterlockedDecrement(&This->ref);
299
300     if (ref == 0)
301     {
302
303         IDirectDrawSurfaceImpl *surf;
304         IDirectDrawImpl *ddraw;
305         IUnknown *ifaceToRelease = This->ifaceToRelease;
306         UINT i;
307
308         /* Complex attached surfaces are destroyed implicitly when the root is released */
309         EnterCriticalSection(&ddraw_cs);
310         if(!This->is_complex_root)
311         {
312             WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
313             LeaveCriticalSection(&ddraw_cs);
314             return ref;
315         }
316         ddraw = This->ddraw;
317
318         /* If it's a texture, destroy the WineD3DTexture.
319          * WineD3D will destroy the IParent interfaces
320          * of the sublevels, which destroys the WineD3DSurfaces.
321          * Set the surfaces to NULL to avoid destroying them again later
322          */
323         if(This->wineD3DTexture)
324         {
325             IWineD3DBaseTexture_Release(This->wineD3DTexture);
326         }
327         /* If it's the RenderTarget, destroy the d3ddevice */
328         else if(This->wineD3DSwapChain)
329         {
330             if((ddraw->d3d_initialized) && (This == ddraw->d3d_target)) {
331                 TRACE("(%p) Destroying the render target, uninitializing D3D\n", This);
332
333                 /* Unset any index buffer, just to be sure */
334                 IWineD3DDevice_SetIndexBuffer(ddraw->wineD3DDevice, NULL, WINED3DFMT_UNKNOWN);
335                 IWineD3DDevice_SetDepthStencilSurface(ddraw->wineD3DDevice, NULL);
336                 IWineD3DDevice_SetVertexDeclaration(ddraw->wineD3DDevice, NULL);
337                 for(i = 0; i < ddraw->numConvertedDecls; i++)
338                 {
339                     IWineD3DVertexDeclaration_Release(ddraw->decls[i].decl);
340                 }
341                 HeapFree(GetProcessHeap(), 0, ddraw->decls);
342                 ddraw->numConvertedDecls = 0;
343
344                 if (FAILED(IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain)))
345                 {
346                     /* Not good */
347                     ERR("(%p) Failed to uninit 3D\n", This);
348                 }
349                 else
350                 {
351                     /* Free the d3d window if one was created */
352                     if(ddraw->d3d_window != 0 && ddraw->d3d_window != ddraw->dest_window)
353                     {
354                         TRACE(" (%p) Destroying the hidden render window %p\n", This, ddraw->d3d_window);
355                         DestroyWindow(ddraw->d3d_window);
356                         ddraw->d3d_window = 0;
357                     }
358                     /* Unset the pointers */
359                 }
360
361                 This->wineD3DSwapChain = NULL; /* Uninit3D releases the swapchain */
362                 ddraw->d3d_initialized = FALSE;
363                 ddraw->d3d_target = NULL;
364             } else {
365                 IWineD3DDevice_UninitGDI(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain);
366                 This->wineD3DSwapChain = NULL;
367             }
368
369             /* Reset to the default surface implementation type. This is needed if apps use
370              * non render target surfaces and expect blits to work after destroying the render
371              * target.
372              *
373              * TODO: Recreate existing offscreen surfaces
374              */
375             ddraw->ImplType = DefaultSurfaceType;
376
377             /* Write a trace because D3D unloading was the reason for many
378              * crashes during development.
379              */
380             TRACE("(%p) D3D unloaded\n", This);
381         }
382
383         /* The refcount test shows that the palette is detached when the surface is destroyed */
384         IDirectDrawSurface7_SetPalette((IDirectDrawSurface7 *)This, NULL);
385
386         /* Loop through all complex attached surfaces,
387          * and destroy them.
388          *
389          * Yet again, only the root can have more than one complexly attached surface, all the others
390          * have a total of one;
391          */
392         for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
393         {
394             if(!This->complex_array[i]) break;
395
396             surf = This->complex_array[i];
397             This->complex_array[i] = NULL;
398             while(surf)
399             {
400                 IDirectDrawSurfaceImpl *destroy = surf;
401                 surf = surf->complex_array[0];              /* Iterate through the "tree" */
402                 ddraw_surface_destroy(destroy);             /* Destroy it */
403             }
404         }
405
406         /* Destroy the root surface. */
407         ddraw_surface_destroy(This);
408
409         /* Reduce the ddraw refcount */
410         if(ifaceToRelease) IUnknown_Release(ifaceToRelease);
411         LeaveCriticalSection(&ddraw_cs);
412     }
413
414     return ref;
415 }
416
417 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
418 {
419     TRACE("iface %p.\n", iface);
420
421     return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_surface3(iface));
422 }
423
424 /*****************************************************************************
425  * IDirectDrawSurface7::GetAttachedSurface
426  *
427  * Returns an attached surface with the requested caps. Surface attachment
428  * and complex surfaces are not clearly described by the MSDN or sdk,
429  * so this method is tricky and likely to contain problems.
430  * This implementation searches the complex list first, then the
431  * attachment chain.
432  *
433  * The chains are searched from This down to the last surface in the chain,
434  * not from the first element in the chain. The first surface found is
435  * returned. The MSDN says that this method fails if more than one surface
436  * matches the caps, but it is not sure if that is right. The attachment
437  * structure may not even allow two matching surfaces.
438  *
439  * The found surface is AddRef-ed before it is returned.
440  *
441  * Params:
442  *  Caps: Pointer to a DDCAPS2 structure describing the caps asked for
443  *  Surface: Address to store the found surface
444  *
445  * Returns:
446  *  DD_OK on success
447  *  DDERR_INVALIDPARAMS if Caps or Surface is NULL
448  *  DDERR_NOTFOUND if no surface was found
449  *
450  *****************************************************************************/
451 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
452         DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
453 {
454     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
455     IDirectDrawSurfaceImpl *surf;
456     DDSCAPS2 our_caps;
457     int i;
458
459     TRACE("(%p)->(%p,%p)\n", This, Caps, Surface);
460     EnterCriticalSection(&ddraw_cs);
461
462     if(This->version < 7)
463     {
464         /* Earlier dx apps put garbage into these members, clear them */
465         our_caps.dwCaps = Caps->dwCaps;
466         our_caps.dwCaps2 = 0;
467         our_caps.dwCaps3 = 0;
468         our_caps.dwCaps4 = 0;
469     }
470     else
471     {
472         our_caps = *Caps;
473     }
474
475     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 */
476
477     for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
478     {
479         surf = This->complex_array[i];
480         if(!surf) break;
481
482         if (TRACE_ON(ddraw))
483         {
484             TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
485                    surf->surface_desc.ddsCaps.dwCaps,
486                    surf->surface_desc.ddsCaps.dwCaps2,
487                    surf->surface_desc.ddsCaps.dwCaps3,
488                    surf->surface_desc.ddsCaps.dwCaps4);
489         }
490
491         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
492             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
493
494             /* MSDN: "This method fails if more than one surface is attached
495              * that matches the capabilities requested."
496              *
497              * Not sure how to test this.
498              */
499
500             TRACE("(%p): Returning surface %p\n", This, surf);
501             TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
502             *Surface = (IDirectDrawSurface7 *)surf;
503             ddraw_surface7_AddRef(*Surface);
504             LeaveCriticalSection(&ddraw_cs);
505             return DD_OK;
506         }
507     }
508
509     /* Next, look at the attachment chain */
510     surf = This;
511
512     while( (surf = surf->next_attached) )
513     {
514         if (TRACE_ON(ddraw))
515         {
516             TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
517                    surf->surface_desc.ddsCaps.dwCaps,
518                    surf->surface_desc.ddsCaps.dwCaps2,
519                    surf->surface_desc.ddsCaps.dwCaps3,
520                    surf->surface_desc.ddsCaps.dwCaps4);
521         }
522
523         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
524             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
525
526             TRACE("(%p): Returning surface %p\n", This, surf);
527             *Surface = (IDirectDrawSurface7 *)surf;
528             ddraw_surface7_AddRef(*Surface);
529             LeaveCriticalSection(&ddraw_cs);
530             return DD_OK;
531         }
532     }
533
534     TRACE("(%p) Didn't find a valid surface\n", This);
535     LeaveCriticalSection(&ddraw_cs);
536
537     *Surface = NULL;
538     return DDERR_NOTFOUND;
539 }
540
541 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
542         DDSCAPS *caps, IDirectDrawSurface3 **attachment)
543 {
544     IDirectDrawSurface7 *attachment7;
545     DDSCAPS2 caps2;
546     HRESULT hr;
547
548     TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
549
550     caps2.dwCaps  = caps->dwCaps;
551     caps2.dwCaps2 = 0;
552     caps2.dwCaps3 = 0;
553     caps2.dwCaps4 = 0;
554
555     hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface),
556             &caps2, &attachment7);
557     if (FAILED(hr)) *attachment = NULL;
558     else *attachment = attachment7 ?
559             (IDirectDrawSurface3 *)&((IDirectDrawSurfaceImpl *)attachment7)->IDirectDrawSurface3_vtbl : NULL;
560
561     return hr;
562 }
563
564 /*****************************************************************************
565  * IDirectDrawSurface7::Lock
566  *
567  * Locks the surface and returns a pointer to the surface's memory
568  *
569  * Params:
570  *  Rect: Rectangle to lock. If NULL, the whole surface is locked
571  *  DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
572  *  Flags: Locking flags, e.g Read only or write only
573  *  h: An event handle that's not used and must be NULL
574  *
575  * Returns:
576  *  DD_OK on success
577  *  DDERR_INVALIDPARAMS if DDSD is NULL
578  *  For more details, see IWineD3DSurface::LockRect
579  *
580  *****************************************************************************/
581 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
582         RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
583 {
584     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
585     WINED3DLOCKED_RECT LockedRect;
586     HRESULT hr;
587     TRACE("(%p)->(%p,%p,%x,%p)\n", This, Rect, DDSD, Flags, h);
588
589     if(!DDSD)
590         return DDERR_INVALIDPARAMS;
591
592     /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
593     EnterCriticalSection(&ddraw_cs);
594
595     /* Should I check for the handle to be NULL?
596      *
597      * The DDLOCK flags and the D3DLOCK flags are equal
598      * for the supported values. The others are ignored by WineD3D
599      */
600
601     if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
602        DDSD->dwSize != sizeof(DDSURFACEDESC2))
603     {
604         WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
605         LeaveCriticalSection(&ddraw_cs);
606         return DDERR_INVALIDPARAMS;
607     }
608
609     /* Windows zeroes this if the rect is invalid */
610     DDSD->lpSurface = 0;
611
612     if (Rect)
613     {
614         if ((Rect->left < 0)
615                 || (Rect->top < 0)
616                 || (Rect->left > Rect->right)
617                 || (Rect->top > Rect->bottom)
618                 || (Rect->right > This->surface_desc.dwWidth)
619                 || (Rect->bottom > This->surface_desc.dwHeight))
620         {
621             WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
622             LeaveCriticalSection(&ddraw_cs);
623             return DDERR_INVALIDPARAMS;
624         }
625     }
626
627     hr = IWineD3DSurface_LockRect(This->WineD3DSurface,
628                                   &LockedRect,
629                                   Rect,
630                                   Flags);
631     if(hr != D3D_OK)
632     {
633         LeaveCriticalSection(&ddraw_cs);
634         switch(hr)
635         {
636             /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
637              * specific error. But since IWineD3DSurface::LockRect returns that error in this
638              * only occasion, keep d3d8 and d3d9 free from the return value override. There are
639              * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
640              * is much easier to do it in one place in ddraw
641              */
642             case WINED3DERR_INVALIDCALL:    return DDERR_SURFACEBUSY;
643             default:                        return hr;
644         }
645     }
646
647     /* Override the memory area. The pitch should be set already. Strangely windows
648      * does not set the LPSURFACE flag on locked surfaces !?!.
649      * DDSD->dwFlags |= DDSD_LPSURFACE;
650      */
651     This->surface_desc.lpSurface = LockedRect.pBits;
652     DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
653
654     TRACE("locked surface returning description :\n");
655     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
656
657     LeaveCriticalSection(&ddraw_cs);
658     return DD_OK;
659 }
660
661 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
662         DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
663 {
664     TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
665             iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
666
667     return ddraw_surface7_Lock((IDirectDrawSurface7 *)surface_from_surface3(iface),
668             rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
669 }
670
671 /*****************************************************************************
672  * IDirectDrawSurface7::Unlock
673  *
674  * Unlocks an locked surface
675  *
676  * Params:
677  *  Rect: Not used by this implementation
678  *
679  * Returns:
680  *  D3D_OK on success
681  *  For more details, see IWineD3DSurface::UnlockRect
682  *
683  *****************************************************************************/
684 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
685 {
686     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
687     HRESULT hr;
688     TRACE("(%p)->(%p)\n", This, pRect);
689
690     EnterCriticalSection(&ddraw_cs);
691     hr = IWineD3DSurface_UnlockRect(This->WineD3DSurface);
692     if(SUCCEEDED(hr))
693     {
694         This->surface_desc.lpSurface = NULL;
695     }
696     LeaveCriticalSection(&ddraw_cs);
697     return hr;
698 }
699
700 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
701 {
702     TRACE("iface %p, data %p.\n", iface, data);
703
704     /* data might not be the LPRECT of later versions, so drop it. */
705     return ddraw_surface7_Unlock((IDirectDrawSurface7 *)surface_from_surface3(iface), NULL);
706 }
707
708 /*****************************************************************************
709  * IDirectDrawSurface7::Flip
710  *
711  * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
712  * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
713  * the flip target is passed to WineD3D, even if the app didn't specify one
714  *
715  * Params:
716  *  DestOverride: Specifies the surface that will become the new front
717  *                buffer. If NULL, the current back buffer is used
718  *  Flags: some DirectDraw flags, see include/ddraw.h
719  *
720  * Returns:
721  *  DD_OK on success
722  *  DDERR_NOTFLIPPABLE if no flip target could be found
723  *  DDERR_INVALIDOBJECT if the surface isn't a front buffer
724  *  For more details, see IWineD3DSurface::Flip
725  *
726  *****************************************************************************/
727 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
728 {
729     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
730     IDirectDrawSurfaceImpl *Override = (IDirectDrawSurfaceImpl *)DestOverride;
731     IDirectDrawSurface7 *Override7;
732     HRESULT hr;
733     TRACE("(%p)->(%p,%x)\n", This, DestOverride, Flags);
734
735     /* Flip has to be called from a front buffer
736      * What about overlay surfaces, AFAIK they can flip too?
737      */
738     if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
739         return DDERR_INVALIDOBJECT; /* Unchecked */
740
741     EnterCriticalSection(&ddraw_cs);
742
743     /* WineD3D doesn't keep track of attached surface, so find the target */
744     if(!Override)
745     {
746         DDSCAPS2 Caps;
747
748         memset(&Caps, 0, sizeof(Caps));
749         Caps.dwCaps |= DDSCAPS_BACKBUFFER;
750         hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
751         if(hr != DD_OK)
752         {
753             ERR("Can't find a flip target\n");
754             LeaveCriticalSection(&ddraw_cs);
755             return DDERR_NOTFLIPPABLE; /* Unchecked */
756         }
757         Override = (IDirectDrawSurfaceImpl *)Override7;
758
759         /* For the GetAttachedSurface */
760         ddraw_surface7_Release(Override7);
761     }
762
763     hr = IWineD3DSurface_Flip(This->WineD3DSurface,
764                               Override->WineD3DSurface,
765                               Flags);
766     LeaveCriticalSection(&ddraw_cs);
767     return hr;
768 }
769
770 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
771 {
772     TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
773
774     return ddraw_surface7_Flip((IDirectDrawSurface7 *)surface_from_surface3(iface),
775             dst ? (IDirectDrawSurface7 *)surface_from_surface3(dst) : NULL, flags);
776 }
777
778 /*****************************************************************************
779  * IDirectDrawSurface7::Blt
780  *
781  * Performs a blit on the surface
782  *
783  * Params:
784  *  DestRect: Destination rectangle, can be NULL
785  *  SrcSurface: Source surface, can be NULL
786  *  SrcRect: Source rectangle, can be NULL
787  *  Flags: Blt flags
788  *  DDBltFx: Some extended blt parameters, connected to the flags
789  *
790  * Returns:
791  *  D3D_OK on success
792  *  See IWineD3DSurface::Blt for more details
793  *
794  *****************************************************************************/
795 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
796         IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
797 {
798     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
799     IDirectDrawSurfaceImpl *Src = (IDirectDrawSurfaceImpl *)SrcSurface;
800     HRESULT hr;
801     TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
802
803     /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
804      * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
805      */
806     if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
807         WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
808         return DDERR_INVALIDPARAMS;
809     }
810
811     if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
812         WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
813         return DDERR_INVALIDPARAMS;
814     }
815
816     /* Sizes can change, therefore hold the lock when testing the rectangles */
817     EnterCriticalSection(&ddraw_cs);
818     if(DestRect)
819     {
820         if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
821            DestRect->right > This->surface_desc.dwWidth ||
822            DestRect->bottom > This->surface_desc.dwHeight)
823         {
824             WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
825             LeaveCriticalSection(&ddraw_cs);
826             return DDERR_INVALIDRECT;
827         }
828     }
829     if(Src && SrcRect)
830     {
831         if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
832            SrcRect->right > Src->surface_desc.dwWidth ||
833            SrcRect->bottom > Src->surface_desc.dwHeight)
834         {
835             WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
836             LeaveCriticalSection(&ddraw_cs);
837             return DDERR_INVALIDRECT;
838         }
839     }
840
841     if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
842         WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
843         LeaveCriticalSection(&ddraw_cs);
844         return DDERR_INVALIDPARAMS;
845     }
846
847     /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it does, copy the struct,
848      * and replace the ddraw surfaces with the wined3d surfaces
849      * So far no blitting operations using surfaces in the bltfx struct are supported anyway.
850      */
851     hr = IWineD3DSurface_Blt(This->WineD3DSurface,
852                              DestRect,
853                              Src ? Src->WineD3DSurface : NULL,
854                              SrcRect,
855                              Flags,
856                              (WINEDDBLTFX *) DDBltFx,
857                              WINED3DTEXF_POINT);
858
859     LeaveCriticalSection(&ddraw_cs);
860     switch(hr)
861     {
862         case WINED3DERR_NOTAVAILABLE:       return DDERR_UNSUPPORTED;
863         case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
864         default:                            return hr;
865     }
866 }
867
868 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
869         IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
870 {
871     TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %p, flags %#x, fx %p.\n",
872             iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
873
874     return ddraw_surface7_Blt((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_rect,
875             src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags, fx);
876 }
877
878 /*****************************************************************************
879  * IDirectDrawSurface7::AddAttachedSurface
880  *
881  * Attaches a surface to another surface. How the surface attachments work
882  * is not totally understood yet, and this method is prone to problems.
883  * he surface that is attached is AddRef-ed.
884  *
885  * Tests with complex surfaces suggest that the surface attachments form a
886  * tree, but no method to test this has been found yet.
887  *
888  * The attachment list consists of a first surface (first_attached) and
889  * for each surface a pointer to the next attached surface (next_attached).
890  * For the first surface, and a surface that has no attachments
891  * first_attached points to the surface itself. A surface that has
892  * no successors in the chain has next_attached set to NULL.
893  *
894  * Newly attached surfaces are attached right after the root surface.
895  * If a surface is attached to a complex surface compound, it's attached to
896  * the surface that the app requested, not the complex root. See
897  * GetAttachedSurface for a description how surfaces are found.
898  *
899  * This is how the current implementation works, and it was coded by looking
900  * at the needs of the applications.
901  *
902  * So far only Z-Buffer attachments are tested, and they are activated in
903  * WineD3D. Mipmaps could be tricky to activate in WineD3D.
904  * Back buffers should work in 2D mode, but they are not tested(They can be
905  * attached in older iface versions). Rendering to the front buffer and
906  * switching between that and double buffering is not yet implemented in
907  * WineD3D, so for 3D it might have unexpected results.
908  *
909  * ddraw_surface_attach_surface is the real thing,
910  * ddraw_surface7_AddAttachedSurface is a wrapper around it that
911  * performs additional checks. Version 7 of this interface is much more restrictive
912  * than its predecessors.
913  *
914  * Params:
915  *  Attach: Surface to attach to iface
916  *
917  * Returns:
918  *  DD_OK on success
919  *  DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
920  *
921  *****************************************************************************/
922 static HRESULT WINAPI ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
923 {
924     TRACE("(%p)->(%p)\n", This, Surf);
925
926     if(Surf == This)
927         return DDERR_CANNOTATTACHSURFACE; /* unchecked */
928
929     EnterCriticalSection(&ddraw_cs);
930
931     /* Check if the surface is already attached somewhere */
932     if( (Surf->next_attached != NULL) ||
933         (Surf->first_attached != Surf) )
934     {
935         /* TODO: Test for the structure of the manual attachment. Is it a chain or a list?
936          * What happens if one surface is attached to 2 different surfaces?
937          */
938         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);
939         LeaveCriticalSection(&ddraw_cs);
940         return DDERR_SURFACEALREADYATTACHED;
941     }
942
943     /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
944     Surf->next_attached = This->next_attached;
945     Surf->first_attached = This->first_attached;
946     This->next_attached = Surf;
947
948     /* Check if the WineD3D depth stencil needs updating */
949     if(This->ddraw->d3ddevice)
950     {
951         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
952     }
953
954     ddraw_surface7_AddRef((IDirectDrawSurface7 *)Surf);
955     LeaveCriticalSection(&ddraw_cs);
956     return DD_OK;
957 }
958
959 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
960 {
961     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
962     IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
963
964     /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
965     if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
966     {
967
968         WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
969               Surf->surface_desc.ddsCaps.dwCaps);
970         return DDERR_CANNOTATTACHSURFACE;
971     }
972
973     return ddraw_surface_attach_surface(This, Surf);
974 }
975
976 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
977 {
978     IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
979     IDirectDrawSurfaceImpl *attach_impl = surface_from_surface3(attachment);
980
981     TRACE("iface %p, attachment %p.\n", iface, attachment);
982
983     /* Tests suggest that
984      * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
985      * -> offscreen plain surfaces can be attached to primaries
986      * -> primaries can be attached to offscreen plain surfaces
987      * -> z buffers can be attached to primaries */
988     if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
989             && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
990     {
991         /* Sizes have to match */
992         if (attach_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
993                 || attach_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
994         {
995             WARN("Surface sizes do not match.\n");
996             return DDERR_CANNOTATTACHSURFACE;
997         }
998         /* OK */
999     }
1000     else if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1001             && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1002     {
1003         /* OK */
1004     }
1005     else
1006     {
1007         WARN("Invalid attachment combination.\n");
1008         return DDERR_CANNOTATTACHSURFACE;
1009     }
1010
1011     return ddraw_surface_attach_surface(surface, attach_impl);
1012 }
1013
1014 /*****************************************************************************
1015  * IDirectDrawSurface7::DeleteAttachedSurface
1016  *
1017  * Removes a surface from the attachment chain. The surface's refcount
1018  * is decreased by one after it has been removed
1019  *
1020  * Params:
1021  *  Flags: Some flags, not used by this implementation
1022  *  Attach: Surface to detach
1023  *
1024  * Returns:
1025  *  DD_OK on success
1026  *  DDERR_SURFACENOTATTACHED if the surface isn't attached to
1027  *
1028  *****************************************************************************/
1029 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1030         DWORD Flags, IDirectDrawSurface7 *Attach)
1031 {
1032     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1033     IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
1034     IDirectDrawSurfaceImpl *Prev = This;
1035     TRACE("(%p)->(%08x,%p)\n", This, Flags, Surf);
1036
1037     EnterCriticalSection(&ddraw_cs);
1038     if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1039     {
1040         LeaveCriticalSection(&ddraw_cs);
1041         return DDERR_CANNOTDETACHSURFACE;
1042     }
1043
1044     /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1045     if (This->surface_desc.ddsCaps.dwCaps &
1046         Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1047     {
1048         Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1049         /* FIXME: we should probably also subtract from dwMipMapCount of this
1050          * and all parent surfaces */
1051     }
1052
1053     /* Find the predecessor of the detached surface */
1054     while(Prev)
1055     {
1056         if(Prev->next_attached == Surf) break;
1057         Prev = Prev->next_attached;
1058     }
1059
1060     /* There must be a surface, otherwise there's a bug */
1061     assert(Prev != NULL);
1062
1063     /* Unchain the surface */
1064     Prev->next_attached = Surf->next_attached;
1065     Surf->next_attached = NULL;
1066     Surf->first_attached = Surf;
1067
1068     /* Check if the WineD3D depth stencil needs updating */
1069     if(This->ddraw->d3ddevice)
1070     {
1071         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1072     }
1073
1074     ddraw_surface7_Release(Attach);
1075     LeaveCriticalSection(&ddraw_cs);
1076     return DD_OK;
1077 }
1078
1079 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1080         DWORD flags, IDirectDrawSurface3 *attachment)
1081 {
1082     TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1083
1084     return ddraw_surface7_DeleteAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
1085             attachment ? (IDirectDrawSurface7 *)surface_from_surface3(attachment) : NULL);
1086 }
1087
1088 /*****************************************************************************
1089  * IDirectDrawSurface7::AddOverlayDirtyRect
1090  *
1091  * "This method is not currently implemented"
1092  *
1093  * Params:
1094  *  Rect: ?
1095  *
1096  * Returns:
1097  *  DDERR_UNSUPPORTED
1098  *
1099  *****************************************************************************/
1100 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1101 {
1102     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1103     TRACE("(%p)->(%p)\n",This,Rect);
1104
1105     /* MSDN says it's not implemented. I could forward it to WineD3D,
1106      * then we'd implement it, but I don't think that's a good idea
1107      * (Stefan Dösinger)
1108      */
1109 #if 0
1110     return IWineD3DSurface_AddOverlayDirtyRect(This->WineD3DSurface, pRect);
1111 #endif
1112     return DDERR_UNSUPPORTED; /* unchecked */
1113 }
1114
1115 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1116 {
1117     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1118
1119     return ddraw_surface7_AddOverlayDirtyRect((IDirectDrawSurface7 *)surface_from_surface3(iface), rect);
1120 }
1121
1122 /*****************************************************************************
1123  * IDirectDrawSurface7::GetDC
1124  *
1125  * Returns a GDI device context for the surface
1126  *
1127  * Params:
1128  *  hdc: Address of a HDC variable to store the dc to
1129  *
1130  * Returns:
1131  *  DD_OK on success
1132  *  DDERR_INVALIDPARAMS if hdc is NULL
1133  *  For details, see IWineD3DSurface::GetDC
1134  *
1135  *****************************************************************************/
1136 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1137 {
1138     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1139     HRESULT hr;
1140     TRACE("(%p)->(%p): Relay\n", This, hdc);
1141
1142     if(!hdc)
1143         return DDERR_INVALIDPARAMS;
1144
1145     EnterCriticalSection(&ddraw_cs);
1146     hr = IWineD3DSurface_GetDC(This->WineD3DSurface,
1147                                hdc);
1148     LeaveCriticalSection(&ddraw_cs);
1149     switch(hr)
1150     {
1151         /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1152          * touch *hdc
1153          */
1154         case WINED3DERR_INVALIDCALL:
1155             if(hdc) *hdc = NULL;
1156             return DDERR_INVALIDPARAMS;
1157
1158         default: return hr;
1159     }
1160 }
1161
1162 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1163 {
1164     TRACE("iface %p, dc %p.\n", iface, dc);
1165
1166     return ddraw_surface7_GetDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1167 }
1168
1169 /*****************************************************************************
1170  * IDirectDrawSurface7::ReleaseDC
1171  *
1172  * Releases the DC that was constructed with GetDC
1173  *
1174  * Params:
1175  *  hdc: HDC to release
1176  *
1177  * Returns:
1178  *  DD_OK on success
1179  *  For more details, see IWineD3DSurface::ReleaseDC
1180  *
1181  *****************************************************************************/
1182 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1183 {
1184     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1185     HRESULT hr;
1186     TRACE("(%p)->(%p): Relay\n", This, hdc);
1187
1188     EnterCriticalSection(&ddraw_cs);
1189     hr = IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
1190     LeaveCriticalSection(&ddraw_cs);
1191     return hr;
1192 }
1193
1194 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1195 {
1196     TRACE("iface %p, dc %p.\n", iface, dc);
1197
1198     return ddraw_surface7_ReleaseDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1199 }
1200
1201 /*****************************************************************************
1202  * IDirectDrawSurface7::GetCaps
1203  *
1204  * Returns the surface's caps
1205  *
1206  * Params:
1207  *  Caps: Address to write the caps to
1208  *
1209  * Returns:
1210  *  DD_OK on success
1211  *  DDERR_INVALIDPARAMS if Caps is NULL
1212  *
1213  *****************************************************************************/
1214 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1215 {
1216     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1217     TRACE("(%p)->(%p)\n",This,Caps);
1218
1219     if(!Caps)
1220         return DDERR_INVALIDPARAMS;
1221
1222     *Caps = This->surface_desc.ddsCaps;
1223     return DD_OK;
1224 }
1225
1226 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1227 {
1228     DDSCAPS2 caps2;
1229     HRESULT hr;
1230
1231     TRACE("iface %p, caps %p.\n", iface, caps);
1232
1233     hr = ddraw_surface7_GetCaps((IDirectDrawSurface7 *)surface_from_surface3(iface), &caps2);
1234     if (FAILED(hr)) return hr;
1235
1236     caps->dwCaps = caps2.dwCaps;
1237     return hr;
1238 }
1239
1240 /*****************************************************************************
1241  * IDirectDrawSurface7::SetPriority
1242  *
1243  * Sets a texture priority for managed textures.
1244  *
1245  * Params:
1246  *  Priority: The new priority
1247  *
1248  * Returns:
1249  *  DD_OK on success
1250  *  For more details, see IWineD3DSurface::SetPriority
1251  *
1252  *****************************************************************************/
1253 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1254 {
1255     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1256     HRESULT hr;
1257     TRACE("(%p)->(%d): Relay!\n",This,Priority);
1258
1259     EnterCriticalSection(&ddraw_cs);
1260     hr = IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
1261     LeaveCriticalSection(&ddraw_cs);
1262     return hr;
1263 }
1264
1265 /*****************************************************************************
1266  * IDirectDrawSurface7::GetPriority
1267  *
1268  * Returns the surface's priority
1269  *
1270  * Params:
1271  *  Priority: Address of a variable to write the priority to
1272  *
1273  * Returns:
1274  *  D3D_OK on success
1275  *  DDERR_INVALIDPARAMS if Priority == NULL
1276  *  For more details, see IWineD3DSurface::GetPriority
1277  *
1278  *****************************************************************************/
1279 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
1280 {
1281     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1282     TRACE("(%p)->(%p): Relay\n",This,Priority);
1283
1284     if(!Priority)
1285     {
1286         return DDERR_INVALIDPARAMS;
1287     }
1288
1289     EnterCriticalSection(&ddraw_cs);
1290     *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1291     LeaveCriticalSection(&ddraw_cs);
1292     return DD_OK;
1293 }
1294
1295 /*****************************************************************************
1296  * IDirectDrawSurface7::SetPrivateData
1297  *
1298  * Stores some data in the surface that is intended for the application's
1299  * use.
1300  *
1301  * Params:
1302  *  tag: GUID that identifies the data
1303  *  Data: Pointer to the private data
1304  *  Size: Size of the private data
1305  *  Flags: Some flags
1306  *
1307  * Returns:
1308  *  D3D_OK on success
1309  *  For more details, see IWineD3DSurface::SetPrivateData
1310  *
1311  *****************************************************************************/
1312 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
1313         REFGUID tag, void *Data, DWORD Size, DWORD Flags)
1314 {
1315     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1316     HRESULT hr;
1317     TRACE("(%p)->(%s,%p,%d,%x): Relay\n", This, debugstr_guid(tag), Data, Size, Flags);
1318
1319     EnterCriticalSection(&ddraw_cs);
1320     hr = IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1321                                         tag,
1322                                         Data,
1323                                         Size,
1324                                         Flags);
1325     LeaveCriticalSection(&ddraw_cs);
1326     switch(hr)
1327     {
1328         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1329         default:                            return hr;
1330     }
1331 }
1332
1333 /*****************************************************************************
1334  * IDirectDrawSurface7::GetPrivateData
1335  *
1336  * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1337  *
1338  * Params:
1339  *  tag: GUID of the data to return
1340  *  Data: Address where to write the data to
1341  *  Size: Size of the buffer at Data
1342  *
1343  * Returns:
1344  *  DD_OK on success
1345  *  DDERR_INVALIDPARAMS if Data is NULL
1346  *  For more details, see IWineD3DSurface::GetPrivateData
1347  *
1348  *****************************************************************************/
1349 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
1350 {
1351     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1352     HRESULT hr;
1353     TRACE("(%p)->(%s,%p,%p): Relay\n", This, debugstr_guid(tag), Data, Size);
1354
1355     if(!Data)
1356         return DDERR_INVALIDPARAMS;
1357
1358     EnterCriticalSection(&ddraw_cs);
1359     hr = IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1360                                         tag,
1361                                         Data,
1362                                         Size);
1363     LeaveCriticalSection(&ddraw_cs);
1364     return hr;
1365 }
1366
1367 /*****************************************************************************
1368  * IDirectDrawSurface7::FreePrivateData
1369  *
1370  * Frees private data stored in the surface
1371  *
1372  * Params:
1373  *  tag: Tag of the data to free
1374  *
1375  * Returns:
1376  *  D3D_OK on success
1377  *  For more details, see IWineD3DSurface::FreePrivateData
1378  *
1379  *****************************************************************************/
1380 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
1381 {
1382     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1383     HRESULT hr;
1384     TRACE("(%p)->(%s): Relay\n", This, debugstr_guid(tag));
1385
1386     EnterCriticalSection(&ddraw_cs);
1387     hr = IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1388     LeaveCriticalSection(&ddraw_cs);
1389     return hr;
1390 }
1391
1392 /*****************************************************************************
1393  * IDirectDrawSurface7::PageLock
1394  *
1395  * Prevents a sysmem surface from being paged out
1396  *
1397  * Params:
1398  *  Flags: Not used, must be 0(unchecked)
1399  *
1400  * Returns:
1401  *  DD_OK, because it's a stub
1402  *
1403  *****************************************************************************/
1404 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
1405 {
1406     TRACE("(%p)->(%x)\n", iface, Flags);
1407
1408     /* This is Windows memory management related - we don't need this */
1409     return DD_OK;
1410 }
1411
1412 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
1413 {
1414     TRACE("iface %p, flags %#x.\n", iface, flags);
1415
1416     return ddraw_surface7_PageLock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1417 }
1418
1419 /*****************************************************************************
1420  * IDirectDrawSurface7::PageUnlock
1421  *
1422  * Allows a sysmem surface to be paged out
1423  *
1424  * Params:
1425  *  Flags: Not used, must be 0(unchecked)
1426  *
1427  * Returns:
1428  *  DD_OK, because it's a stub
1429  *
1430  *****************************************************************************/
1431 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
1432 {
1433     TRACE("(%p)->(%x)\n", iface, Flags);
1434
1435     return DD_OK;
1436 }
1437
1438 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
1439 {
1440     TRACE("iface %p, flags %#x.\n", iface, flags);
1441
1442     return ddraw_surface7_PageUnlock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1443 }
1444
1445 /*****************************************************************************
1446  * IDirectDrawSurface7::BltBatch
1447  *
1448  * An unimplemented function
1449  *
1450  * Params:
1451  *  ?
1452  *
1453  * Returns:
1454  *  DDERR_UNSUPPORTED
1455  *
1456  *****************************************************************************/
1457 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1458 {
1459     TRACE("(%p)->(%p,%d,%08x)\n",iface,Batch,Count,Flags);
1460
1461     /* MSDN: "not currently implemented" */
1462     return DDERR_UNSUPPORTED;
1463 }
1464
1465 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
1466 {
1467     TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
1468
1469     return ddraw_surface7_BltBatch((IDirectDrawSurface7 *)surface_from_surface3(iface), batch, count, flags);
1470 }
1471
1472 /*****************************************************************************
1473  * IDirectDrawSurface7::EnumAttachedSurfaces
1474  *
1475  * Enumerates all surfaces attached to this surface
1476  *
1477  * Params:
1478  *  context: Pointer to pass unmodified to the callback
1479  *  cb: Callback function to call for each surface
1480  *
1481  * Returns:
1482  *  DD_OK on success
1483  *  DDERR_INVALIDPARAMS if cb is NULL
1484  *
1485  *****************************************************************************/
1486 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1487         void *context, LPDDENUMSURFACESCALLBACK7 cb)
1488 {
1489     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1490     IDirectDrawSurfaceImpl *surf;
1491     DDSURFACEDESC2 desc;
1492     int i;
1493
1494     /* Attached surfaces aren't handled in WineD3D */
1495     TRACE("(%p)->(%p,%p)\n",This,context,cb);
1496
1497     if(!cb)
1498         return DDERR_INVALIDPARAMS;
1499
1500     EnterCriticalSection(&ddraw_cs);
1501     for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1502     {
1503         surf = This->complex_array[i];
1504         if(!surf) break;
1505
1506         ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1507         desc = surf->surface_desc;
1508         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1509         if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1510         {
1511             LeaveCriticalSection(&ddraw_cs);
1512             return DD_OK;
1513         }
1514     }
1515
1516     for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1517     {
1518         ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1519         desc = surf->surface_desc;
1520         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1521         if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1522         {
1523             LeaveCriticalSection(&ddraw_cs);
1524             return DD_OK;
1525         }
1526     }
1527
1528     TRACE(" end of enumeration.\n");
1529
1530     LeaveCriticalSection(&ddraw_cs);
1531     return DD_OK;
1532 }
1533
1534 struct callback_info
1535 {
1536     LPDDENUMSURFACESCALLBACK callback;
1537     void *context;
1538 };
1539
1540 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
1541 {
1542     const struct callback_info *info = context;
1543
1544     return info->callback((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_vtbl,
1545             (DDSURFACEDESC *)surface_desc, info->context);
1546 }
1547
1548 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
1549         void *context, LPDDENUMSURFACESCALLBACK callback)
1550 {
1551     struct callback_info info;
1552
1553     TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
1554
1555     info.callback = callback;
1556     info.context  = context;
1557
1558     return ddraw_surface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)surface_from_surface3(iface),
1559             &info, EnumCallback);
1560 }
1561
1562 /*****************************************************************************
1563  * IDirectDrawSurface7::EnumOverlayZOrders
1564  *
1565  * "Enumerates the overlay surfaces on the specified destination"
1566  *
1567  * Params:
1568  *  Flags: DDENUMOVERLAYZ_BACKTOFRONT  or DDENUMOVERLAYZ_FRONTTOBACK
1569  *  context: context to pass back to the callback
1570  *  cb: callback function to call for each enumerated surface
1571  *
1572  * Returns:
1573  *  DD_OK, because it's a stub
1574  *
1575  *****************************************************************************/
1576 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1577         DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
1578 {
1579      FIXME("(%p)->(%x,%p,%p): Stub!\n", iface, Flags, context, cb);
1580
1581     return DD_OK;
1582 }
1583
1584 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
1585         DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
1586 {
1587     struct callback_info info;
1588
1589     TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
1590
1591     info.callback = callback;
1592     info.context  = context;
1593
1594     return ddraw_surface7_EnumOverlayZOrders((IDirectDrawSurface7 *)surface_from_surface3(iface),
1595             flags, &info, EnumCallback);
1596 }
1597
1598 /*****************************************************************************
1599  * IDirectDrawSurface7::GetBltStatus
1600  *
1601  * Returns the blitting status
1602  *
1603  * Params:
1604  *  Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1605  *
1606  * Returns:
1607  *  See IWineD3DSurface::Blt
1608  *
1609  *****************************************************************************/
1610 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1611 {
1612     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1613     HRESULT hr;
1614     TRACE("(%p)->(%x): Relay\n", This, Flags);
1615
1616     EnterCriticalSection(&ddraw_cs);
1617     hr = IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1618     LeaveCriticalSection(&ddraw_cs);
1619     switch(hr)
1620     {
1621         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1622         default:                            return hr;
1623     }
1624 }
1625
1626 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
1627 {
1628     TRACE("iface %p, flags %#x.\n", iface, flags);
1629
1630     return ddraw_surface7_GetBltStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1631 }
1632
1633 /*****************************************************************************
1634  * IDirectDrawSurface7::GetColorKey
1635  *
1636  * Returns the color key assigned to the surface
1637  *
1638  * Params:
1639  *  Flags: Some flags
1640  *  CKey: Address to store the key to
1641  *
1642  * Returns:
1643  *  DD_OK on success
1644  *  DDERR_INVALIDPARAMS if CKey is NULL
1645  *
1646  *****************************************************************************/
1647 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
1648 {
1649     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1650     TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
1651
1652     if(!CKey)
1653         return DDERR_INVALIDPARAMS;
1654
1655     EnterCriticalSection(&ddraw_cs);
1656
1657     switch (Flags)
1658     {
1659     case DDCKEY_DESTBLT:
1660         if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
1661         {
1662             LeaveCriticalSection(&ddraw_cs);
1663             return DDERR_NOCOLORKEY;
1664         }
1665         *CKey = This->surface_desc.ddckCKDestBlt;
1666         break;
1667
1668     case DDCKEY_DESTOVERLAY:
1669         if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
1670             {
1671             LeaveCriticalSection(&ddraw_cs);
1672             return DDERR_NOCOLORKEY;
1673             }
1674         *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1675         break;
1676
1677     case DDCKEY_SRCBLT:
1678         if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
1679         {
1680             LeaveCriticalSection(&ddraw_cs);
1681             return DDERR_NOCOLORKEY;
1682         }
1683         *CKey = This->surface_desc.ddckCKSrcBlt;
1684         break;
1685
1686     case DDCKEY_SRCOVERLAY:
1687         if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
1688         {
1689             LeaveCriticalSection(&ddraw_cs);
1690             return DDERR_NOCOLORKEY;
1691         }
1692         *CKey = This->surface_desc.ddckCKSrcOverlay;
1693         break;
1694
1695     default:
1696         LeaveCriticalSection(&ddraw_cs);
1697         return DDERR_INVALIDPARAMS;
1698     }
1699
1700     LeaveCriticalSection(&ddraw_cs);
1701     return DD_OK;
1702 }
1703
1704 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
1705 {
1706     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
1707
1708     return ddraw_surface7_GetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
1709 }
1710
1711 /*****************************************************************************
1712  * IDirectDrawSurface7::GetFlipStatus
1713  *
1714  * Returns the flipping status of the surface
1715  *
1716  * Params:
1717  *  Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1718  *
1719  * Returns:
1720  *  See IWineD3DSurface::GetFlipStatus
1721  *
1722  *****************************************************************************/
1723 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1724 {
1725     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1726     HRESULT hr;
1727     TRACE("(%p)->(%x): Relay\n", This, Flags);
1728
1729     EnterCriticalSection(&ddraw_cs);
1730     hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1731     LeaveCriticalSection(&ddraw_cs);
1732     switch(hr)
1733     {
1734         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1735         default:                            return hr;
1736     }
1737 }
1738
1739 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
1740 {
1741     TRACE("iface %p, flags %#x.\n", iface, flags);
1742
1743     return ddraw_surface7_GetFlipStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1744 }
1745
1746 /*****************************************************************************
1747  * IDirectDrawSurface7::GetOverlayPosition
1748  *
1749  * Returns the display coordinates of a visible and active overlay surface
1750  *
1751  * Params:
1752  *  X
1753  *  Y
1754  *
1755  * Returns:
1756  *  DDERR_NOTAOVERLAYSURFACE, because it's a stub
1757  *****************************************************************************/
1758 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
1759 {
1760     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1761     HRESULT hr;
1762     TRACE("(%p)->(%p,%p): Relay\n", This, X, Y);
1763
1764     EnterCriticalSection(&ddraw_cs);
1765     hr = IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1766                                             X,
1767                                             Y);
1768     LeaveCriticalSection(&ddraw_cs);
1769     return hr;
1770 }
1771
1772 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
1773 {
1774     TRACE("iface %p, x %p, y %p.\n", iface, x, y);
1775
1776     return ddraw_surface7_GetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
1777 }
1778
1779 /*****************************************************************************
1780  * IDirectDrawSurface7::GetPixelFormat
1781  *
1782  * Returns the pixel format of the Surface
1783  *
1784  * Params:
1785  *  PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1786  *               format should be written
1787  *
1788  * Returns:
1789  *  DD_OK on success
1790  *  DDERR_INVALIDPARAMS if PixelFormat is NULL
1791  *
1792  *****************************************************************************/
1793 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
1794 {
1795     /* What is DDERR_INVALIDSURFACETYPE for here? */
1796     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1797     TRACE("(%p)->(%p)\n",This,PixelFormat);
1798
1799     if(!PixelFormat)
1800         return DDERR_INVALIDPARAMS;
1801
1802     EnterCriticalSection(&ddraw_cs);
1803     DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1804     LeaveCriticalSection(&ddraw_cs);
1805
1806     return DD_OK;
1807 }
1808
1809 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
1810 {
1811     TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
1812
1813     return ddraw_surface7_GetPixelFormat((IDirectDrawSurface7 *)surface_from_surface3(iface), pixel_format);
1814 }
1815
1816 /*****************************************************************************
1817  * IDirectDrawSurface7::GetSurfaceDesc
1818  *
1819  * Returns the description of this surface
1820  *
1821  * Params:
1822  *  DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1823  *        surface desc
1824  *
1825  * Returns:
1826  *  DD_OK on success
1827  *  DDERR_INVALIDPARAMS if DDSD is NULL
1828  *
1829  *****************************************************************************/
1830 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
1831 {
1832     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1833
1834     TRACE("(%p)->(%p)\n",This,DDSD);
1835
1836     if(!DDSD)
1837         return DDERR_INVALIDPARAMS;
1838
1839     if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1840     {
1841         WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1842         return DDERR_INVALIDPARAMS;
1843     }
1844
1845     EnterCriticalSection(&ddraw_cs);
1846     DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1847     TRACE("Returning surface desc:\n");
1848     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1849
1850     LeaveCriticalSection(&ddraw_cs);
1851     return DD_OK;
1852 }
1853
1854 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
1855 {
1856     IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
1857
1858     TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1859
1860     if (!surface_desc) return DDERR_INVALIDPARAMS;
1861
1862     if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
1863     {
1864         WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
1865         return DDERR_INVALIDPARAMS;
1866     }
1867
1868     EnterCriticalSection(&ddraw_cs);
1869     DD_STRUCT_COPY_BYSIZE(surface_desc, (DDSURFACEDESC *)&surface->surface_desc);
1870     TRACE("Returning surface desc:\n");
1871     if (TRACE_ON(ddraw))
1872     {
1873         /* DDRAW_dump_surface_desc handles the smaller size */
1874         DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
1875     }
1876
1877     LeaveCriticalSection(&ddraw_cs);
1878     return DD_OK;
1879 }
1880
1881 /*****************************************************************************
1882  * IDirectDrawSurface7::Initialize
1883  *
1884  * Initializes the surface. This is a no-op in Wine
1885  *
1886  * Params:
1887  *  DD: Pointer to an DirectDraw interface
1888  *  DDSD: Surface description for initialization
1889  *
1890  * Returns:
1891  *  DDERR_ALREADYINITIALIZED
1892  *
1893  *****************************************************************************/
1894 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
1895         IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
1896 {
1897     TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1898
1899     return DDERR_ALREADYINITIALIZED;
1900 }
1901
1902 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
1903         IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
1904 {
1905     TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1906
1907     return ddraw_surface7_Initialize((IDirectDrawSurface7 *)surface_from_surface3(iface),
1908             ddraw, (DDSURFACEDESC2 *)surface_desc);
1909 }
1910
1911 /*****************************************************************************
1912  * IDirectDrawSurface7::IsLost
1913  *
1914  * Checks if the surface is lost
1915  *
1916  * Returns:
1917  *  DD_OK, if the surface is usable
1918  *  DDERR_ISLOST if the surface is lost
1919  *  See IWineD3DSurface::IsLost for more details
1920  *
1921  *****************************************************************************/
1922 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
1923 {
1924     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1925     HRESULT hr;
1926     TRACE("(%p)\n", This);
1927
1928     EnterCriticalSection(&ddraw_cs);
1929     /* We lose the surface if the implementation was changed */
1930     if(This->ImplType != This->ddraw->ImplType)
1931     {
1932         /* But this shouldn't happen. When we change the implementation,
1933          * all surfaces are re-created automatically, and their content
1934          * is copied
1935          */
1936         ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1937         LeaveCriticalSection(&ddraw_cs);
1938         return DDERR_SURFACELOST;
1939     }
1940
1941     hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
1942     LeaveCriticalSection(&ddraw_cs);
1943     switch(hr)
1944     {
1945         /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1946          * WineD3D uses the same error for surfaces
1947          */
1948         case WINED3DERR_DEVICELOST:         return DDERR_SURFACELOST;
1949         default:                            return hr;
1950     }
1951 }
1952
1953 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
1954 {
1955     TRACE("iface %p.\n", iface);
1956
1957     return ddraw_surface7_IsLost((IDirectDrawSurface7 *)surface_from_surface3(iface));
1958 }
1959
1960 /*****************************************************************************
1961  * IDirectDrawSurface7::Restore
1962  *
1963  * Restores a lost surface. This makes the surface usable again, but
1964  * doesn't reload its old contents
1965  *
1966  * Returns:
1967  *  DD_OK on success
1968  *  See IWineD3DSurface::Restore for more details
1969  *
1970  *****************************************************************************/
1971 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
1972 {
1973     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1974     HRESULT hr;
1975     TRACE("(%p)\n", This);
1976
1977     EnterCriticalSection(&ddraw_cs);
1978     if(This->ImplType != This->ddraw->ImplType)
1979     {
1980         /* Call the recreation callback. Make sure to AddRef first */
1981         IDirectDrawSurface_AddRef(iface);
1982         ddraw_recreate_surfaces_cb(iface, &This->surface_desc, NULL /* Not needed */);
1983     }
1984     hr = IWineD3DSurface_Restore(This->WineD3DSurface);
1985     LeaveCriticalSection(&ddraw_cs);
1986     return hr;
1987 }
1988
1989 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
1990 {
1991     TRACE("iface %p.\n", iface);
1992
1993     return ddraw_surface7_Restore((IDirectDrawSurface7 *)surface_from_surface3(iface));
1994 }
1995
1996 /*****************************************************************************
1997  * IDirectDrawSurface7::SetOverlayPosition
1998  *
1999  * Changes the display coordinates of an overlay surface
2000  *
2001  * Params:
2002  *  X:
2003  *  Y:
2004  *
2005  * Returns:
2006  *   DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
2007  *****************************************************************************/
2008 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
2009 {
2010     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2011     HRESULT hr;
2012     TRACE("(%p)->(%d,%d): Relay\n", This, X, Y);
2013
2014     EnterCriticalSection(&ddraw_cs);
2015     hr = IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
2016                                             X,
2017                                             Y);
2018     LeaveCriticalSection(&ddraw_cs);
2019     return hr;
2020 }
2021
2022 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
2023 {
2024     TRACE("iface %p, x %d, y %d.\n", iface, x, y);
2025
2026     return ddraw_surface7_SetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
2027 }
2028
2029 /*****************************************************************************
2030  * IDirectDrawSurface7::UpdateOverlay
2031  *
2032  * Modifies the attributes of an overlay surface.
2033  *
2034  * Params:
2035  *  SrcRect: The section of the source being used for the overlay
2036  *  DstSurface: Address of the surface that is overlaid
2037  *  DstRect: Place of the overlay
2038  *  Flags: some DDOVER_* flags
2039  *
2040  * Returns:
2041  *  DDERR_UNSUPPORTED, because we don't support overlays
2042  *
2043  *****************************************************************************/
2044 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
2045         IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
2046 {
2047     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2048     IDirectDrawSurfaceImpl *Dst = (IDirectDrawSurfaceImpl *)DstSurface;
2049     HRESULT hr;
2050     TRACE("(%p)->(%p,%p,%p,%x,%p): Relay\n", This, SrcRect, Dst, DstRect, Flags, FX);
2051
2052     EnterCriticalSection(&ddraw_cs);
2053     hr = IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
2054                                        SrcRect,
2055                                        Dst ? Dst->WineD3DSurface : NULL,
2056                                        DstRect,
2057                                        Flags,
2058                                        (WINEDDOVERLAYFX *) FX);
2059     LeaveCriticalSection(&ddraw_cs);
2060     switch(hr) {
2061         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2062         case WINEDDERR_NOTAOVERLAYSURFACE:  return DDERR_NOTAOVERLAYSURFACE;
2063         case WINEDDERR_OVERLAYNOTVISIBLE:   return DDERR_OVERLAYNOTVISIBLE;
2064         default:
2065             return hr;
2066     }
2067 }
2068
2069 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
2070         IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
2071 {
2072     TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2073             iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
2074
2075     return ddraw_surface7_UpdateOverlay((IDirectDrawSurface7 *)surface_from_surface3(iface), src_rect,
2076             dst_surface ? (IDirectDrawSurface7 *)surface_from_surface3(dst_surface) : NULL, dst_rect, flags, fx);
2077 }
2078
2079 /*****************************************************************************
2080  * IDirectDrawSurface7::UpdateOverlayDisplay
2081  *
2082  * The DX7 sdk says that it's not implemented
2083  *
2084  * Params:
2085  *  Flags: ?
2086  *
2087  * Returns: DDERR_UNSUPPORTED, because we don't support overlays
2088  *
2089  *****************************************************************************/
2090 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
2091 {
2092     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2093     TRACE("(%p)->(%x)\n", This, Flags);
2094     return DDERR_UNSUPPORTED;
2095 }
2096
2097 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
2098 {
2099     TRACE("iface %p, flags %#x.\n", iface, flags);
2100
2101     return ddraw_surface7_UpdateOverlayDisplay((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
2102 }
2103
2104 /*****************************************************************************
2105  * IDirectDrawSurface7::UpdateOverlayZOrder
2106  *
2107  * Sets an overlay's Z order
2108  *
2109  * Params:
2110  *  Flags: DDOVERZ_* flags
2111  *  DDSRef: Defines the relative position in the overlay chain
2112  *
2113  * Returns:
2114  *  DDERR_NOTOVERLAYSURFACE, because we don't support overlays
2115  *
2116  *****************************************************************************/
2117 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
2118         DWORD Flags, IDirectDrawSurface7 *DDSRef)
2119 {
2120     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2121     IDirectDrawSurfaceImpl *Ref = (IDirectDrawSurfaceImpl *)DDSRef;
2122     HRESULT hr;
2123
2124     TRACE("(%p)->(%x,%p): Relay\n", This, Flags, Ref);
2125     EnterCriticalSection(&ddraw_cs);
2126     hr =  IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
2127                                               Flags,
2128                                               Ref ? Ref->WineD3DSurface : NULL);
2129     LeaveCriticalSection(&ddraw_cs);
2130     return hr;
2131 }
2132
2133 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
2134         DWORD flags, IDirectDrawSurface3 *reference)
2135 {
2136     TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
2137
2138     return ddraw_surface7_UpdateOverlayZOrder((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
2139             reference ? (IDirectDrawSurface7 *)surface_from_surface3(reference) : NULL);
2140 }
2141
2142 /*****************************************************************************
2143  * IDirectDrawSurface7::GetDDInterface
2144  *
2145  * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
2146  * surface belongs to
2147  *
2148  * Params:
2149  *  DD: Address to write the interface pointer to
2150  *
2151  * Returns:
2152  *  DD_OK on success
2153  *  DDERR_INVALIDPARAMS if DD is NULL
2154  *
2155  *****************************************************************************/
2156 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
2157 {
2158     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2159
2160     TRACE("(%p)->(%p)\n",This,DD);
2161
2162     if(!DD)
2163         return DDERR_INVALIDPARAMS;
2164
2165     switch(This->version)
2166     {
2167         case 7:
2168             *DD = This->ddraw;
2169             break;
2170
2171         case 4:
2172             *DD = &This->ddraw->IDirectDraw4_vtbl;
2173             break;
2174
2175         case 2:
2176             *DD = &This->ddraw->IDirectDraw2_vtbl;
2177             break;
2178
2179         case 1:
2180             *DD = &This->ddraw->IDirectDraw_vtbl;
2181             break;
2182
2183     }
2184     IUnknown_AddRef((IUnknown *)*DD);
2185
2186     return DD_OK;
2187 }
2188
2189 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
2190 {
2191     TRACE("iface %p, ddraw %p.\n", iface, ddraw);
2192
2193     return ddraw_surface7_GetDDInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), ddraw);
2194 }
2195
2196 /* This seems also windows implementation specific - I don't think WineD3D needs this */
2197 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
2198 {
2199     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2200     volatile IDirectDrawSurfaceImpl* vThis = This;
2201
2202     TRACE("(%p)\n",This);
2203     EnterCriticalSection(&ddraw_cs);
2204     /* A uniqueness value of 0 is apparently special.
2205      * This needs to be checked.
2206      * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
2207      */
2208     while (1) {
2209         DWORD old_uniqueness_value = vThis->uniqueness_value;
2210         DWORD new_uniqueness_value = old_uniqueness_value+1;
2211
2212         if (old_uniqueness_value == 0) break;
2213         if (new_uniqueness_value == 0) new_uniqueness_value = 1;
2214
2215         if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
2216                                       old_uniqueness_value,
2217                                       new_uniqueness_value)
2218             == old_uniqueness_value)
2219             break;
2220     }
2221
2222     LeaveCriticalSection(&ddraw_cs);
2223     return DD_OK;
2224 }
2225
2226 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
2227 {
2228     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2229
2230     TRACE("(%p)->(%p)\n",This,pValue);
2231     EnterCriticalSection(&ddraw_cs);
2232     *pValue = This->uniqueness_value;
2233     LeaveCriticalSection(&ddraw_cs);
2234     return DD_OK;
2235 }
2236
2237 /*****************************************************************************
2238  * IDirectDrawSurface7::SetLOD
2239  *
2240  * Sets the level of detail of a texture
2241  *
2242  * Params:
2243  *  MaxLOD: LOD to set
2244  *
2245  * Returns:
2246  *  DD_OK on success
2247  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
2248  *
2249  *****************************************************************************/
2250 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
2251 {
2252     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2253     HRESULT hr;
2254     TRACE("(%p)->(%d)\n", This, MaxLOD);
2255
2256     EnterCriticalSection(&ddraw_cs);
2257     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2258     {
2259         LeaveCriticalSection(&ddraw_cs);
2260         return DDERR_INVALIDOBJECT;
2261     }
2262
2263     if(!This->wineD3DTexture)
2264     {
2265         ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2266         LeaveCriticalSection(&ddraw_cs);
2267         return DDERR_INVALIDOBJECT;
2268     }
2269
2270     hr = IWineD3DBaseTexture_SetLOD(This->wineD3DTexture,
2271                                     MaxLOD);
2272     LeaveCriticalSection(&ddraw_cs);
2273     return hr;
2274 }
2275
2276 /*****************************************************************************
2277  * IDirectDrawSurface7::GetLOD
2278  *
2279  * Returns the level of detail of a Direct3D texture
2280  *
2281  * Params:
2282  *  MaxLOD: Address to write the LOD to
2283  *
2284  * Returns:
2285  *  DD_OK on success
2286  *  DDERR_INVALIDPARAMS if MaxLOD is NULL
2287  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
2288  *
2289  *****************************************************************************/
2290 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
2291 {
2292     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2293     TRACE("(%p)->(%p)\n", This, MaxLOD);
2294
2295     if(!MaxLOD)
2296         return DDERR_INVALIDPARAMS;
2297
2298     EnterCriticalSection(&ddraw_cs);
2299     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2300     {
2301         LeaveCriticalSection(&ddraw_cs);
2302         return DDERR_INVALIDOBJECT;
2303     }
2304
2305     *MaxLOD = IWineD3DBaseTexture_GetLOD(This->wineD3DTexture);
2306     LeaveCriticalSection(&ddraw_cs);
2307     return DD_OK;
2308 }
2309
2310 /*****************************************************************************
2311  * IDirectDrawSurface7::BltFast
2312  *
2313  * Performs a fast Blit.
2314  *
2315  * Params:
2316  *  dstx: The x coordinate to blit to on the destination
2317  *  dsty: The y coordinate to blit to on the destination
2318  *  Source: The source surface
2319  *  rsrc: The source rectangle
2320  *  trans: Type of transfer. Some DDBLTFAST_* flags
2321  *
2322  * Returns:
2323  *  DD_OK on success
2324  *  For more details, see IWineD3DSurface::BltFast
2325  *
2326  *****************************************************************************/
2327 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
2328         IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
2329 {
2330     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2331     IDirectDrawSurfaceImpl *src = (IDirectDrawSurfaceImpl *)Source;
2332     DWORD src_w, src_h, dst_w, dst_h;
2333     HRESULT hr;
2334     TRACE("(%p)->(%d,%d,%p,%p,%d): Relay\n", This, dstx, dsty, Source, rsrc, trans);
2335
2336     dst_w = This->surface_desc.dwWidth;
2337     dst_h = This->surface_desc.dwHeight;
2338
2339     /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2340      * in that case
2341      */
2342     if(rsrc)
2343     {
2344         if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2345            rsrc->right > src->surface_desc.dwWidth ||
2346            rsrc->bottom > src->surface_desc.dwHeight)
2347         {
2348             WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2349             return DDERR_INVALIDRECT;
2350         }
2351
2352         src_w = rsrc->right - rsrc->left;
2353         src_h = rsrc->bottom - rsrc->top;
2354     }
2355     else
2356     {
2357         src_w = src->surface_desc.dwWidth;
2358         src_h = src->surface_desc.dwHeight;
2359     }
2360
2361     if (src_w > dst_w || dstx > dst_w - src_w
2362             || src_h > dst_h || dsty > dst_h - src_h)
2363     {
2364         WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
2365         return DDERR_INVALIDRECT;
2366     }
2367
2368     EnterCriticalSection(&ddraw_cs);
2369     hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
2370                                  dstx, dsty,
2371                                  src ? src->WineD3DSurface : NULL,
2372                                  rsrc,
2373                                  trans);
2374     LeaveCriticalSection(&ddraw_cs);
2375     switch(hr)
2376     {
2377         case WINED3DERR_NOTAVAILABLE:           return DDERR_UNSUPPORTED;
2378         case WINED3DERR_WRONGTEXTUREFORMAT:     return DDERR_INVALIDPIXELFORMAT;
2379         default:                                return hr;
2380     }
2381 }
2382
2383 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
2384         IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
2385 {
2386     TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2387             iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
2388
2389     return ddraw_surface7_BltFast((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_x, dst_y,
2390             src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags);
2391 }
2392
2393 /*****************************************************************************
2394  * IDirectDrawSurface7::GetClipper
2395  *
2396  * Returns the IDirectDrawClipper interface of the clipper assigned to this
2397  * surface
2398  *
2399  * Params:
2400  *  Clipper: Address to store the interface pointer at
2401  *
2402  * Returns:
2403  *  DD_OK on success
2404  *  DDERR_INVALIDPARAMS if Clipper is NULL
2405  *  DDERR_NOCLIPPERATTACHED if there's no clipper attached
2406  *
2407  *****************************************************************************/
2408 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
2409 {
2410     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2411     TRACE("(%p)->(%p)\n", This, Clipper);
2412
2413     if(!Clipper)
2414     {
2415         LeaveCriticalSection(&ddraw_cs);
2416         return DDERR_INVALIDPARAMS;
2417     }
2418
2419     EnterCriticalSection(&ddraw_cs);
2420     if(This->clipper == NULL)
2421     {
2422         LeaveCriticalSection(&ddraw_cs);
2423         return DDERR_NOCLIPPERATTACHED;
2424     }
2425
2426     *Clipper = (IDirectDrawClipper *)This->clipper;
2427     IDirectDrawClipper_AddRef(*Clipper);
2428     LeaveCriticalSection(&ddraw_cs);
2429     return DD_OK;
2430 }
2431
2432 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
2433 {
2434     TRACE("iface %p, clipper %p.\n", iface, clipper);
2435
2436     return ddraw_surface7_GetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2437 }
2438
2439 /*****************************************************************************
2440  * IDirectDrawSurface7::SetClipper
2441  *
2442  * Sets a clipper for the surface
2443  *
2444  * Params:
2445  *  Clipper: IDirectDrawClipper interface of the clipper to set
2446  *
2447  * Returns:
2448  *  DD_OK on success
2449  *
2450  *****************************************************************************/
2451 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper *Clipper)
2452 {
2453     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2454     IDirectDrawClipperImpl *oldClipper = This->clipper;
2455     HWND clipWindow;
2456     HRESULT hr;
2457     TRACE("(%p)->(%p)\n",This,Clipper);
2458
2459     EnterCriticalSection(&ddraw_cs);
2460     if ((IDirectDrawClipperImpl *)Clipper == This->clipper)
2461     {
2462         LeaveCriticalSection(&ddraw_cs);
2463         return DD_OK;
2464     }
2465
2466     This->clipper = (IDirectDrawClipperImpl *)Clipper;
2467
2468     if (Clipper != NULL)
2469         IDirectDrawClipper_AddRef(Clipper);
2470     if(oldClipper)
2471         IDirectDrawClipper_Release((IDirectDrawClipper *)oldClipper);
2472
2473     hr = IWineD3DSurface_SetClipper(This->WineD3DSurface, This->clipper ? This->clipper->wineD3DClipper : NULL);
2474
2475     if(This->wineD3DSwapChain) {
2476         clipWindow = NULL;
2477         if(Clipper) {
2478             IDirectDrawClipper_GetHWnd(Clipper, &clipWindow);
2479         }
2480
2481         if(clipWindow) {
2482             IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2483                                                     clipWindow);
2484         } else {
2485             IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2486                                                     This->ddraw->d3d_window);
2487         }
2488     }
2489
2490     LeaveCriticalSection(&ddraw_cs);
2491     return hr;
2492 }
2493
2494 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
2495 {
2496     TRACE("iface %p, clipper %p.\n", iface, clipper);
2497
2498     return ddraw_surface7_SetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2499 }
2500
2501 /*****************************************************************************
2502  * IDirectDrawSurface7::SetSurfaceDesc
2503  *
2504  * Sets the surface description. It can override the pixel format, the surface
2505  * memory, ...
2506  * It's not really tested.
2507  *
2508  * Params:
2509  * DDSD: Pointer to the new surface description to set
2510  * Flags: Some flags
2511  *
2512  * Returns:
2513  *  DD_OK on success
2514  *  DDERR_INVALIDPARAMS if DDSD is NULL
2515  *
2516  *****************************************************************************/
2517 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
2518 {
2519     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2520     WINED3DFORMAT newFormat = WINED3DFMT_UNKNOWN;
2521     HRESULT hr;
2522     TRACE("(%p)->(%p,%x)\n", This, DDSD, Flags);
2523
2524     if(!DDSD)
2525         return DDERR_INVALIDPARAMS;
2526
2527     EnterCriticalSection(&ddraw_cs);
2528     if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2529     {
2530         newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2531
2532         if(newFormat == WINED3DFMT_UNKNOWN)
2533         {
2534             ERR("Requested to set an unknown pixelformat\n");
2535             LeaveCriticalSection(&ddraw_cs);
2536             return DDERR_INVALIDPARAMS;
2537         }
2538         if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2539         {
2540             hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2541                                            newFormat);
2542             if(hr != DD_OK)
2543             {
2544                 LeaveCriticalSection(&ddraw_cs);
2545                 return hr;
2546             }
2547         }
2548     }
2549     if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2550     {
2551         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2552                                     DDCKEY_DESTOVERLAY,
2553                                     (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2554     }
2555     if (DDSD->dwFlags & DDSD_CKDESTBLT)
2556     {
2557         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2558                                     DDCKEY_DESTBLT,
2559                                     (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2560     }
2561     if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2562     {
2563         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2564                                     DDCKEY_SRCOVERLAY,
2565                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2566     }
2567     if (DDSD->dwFlags & DDSD_CKSRCBLT)
2568     {
2569         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2570                                     DDCKEY_SRCBLT,
2571                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2572     }
2573     if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2574     {
2575         hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2576         if(hr != WINED3D_OK)
2577         {
2578             /* No need for a trace here, wined3d does that for us */
2579             switch(hr)
2580             {
2581                 case WINED3DERR_INVALIDCALL:
2582                     LeaveCriticalSection(&ddraw_cs);
2583                     return DDERR_INVALIDPARAMS;
2584                 default:
2585                     break; /* Go on */
2586             }
2587         }
2588     }
2589
2590     This->surface_desc = *DDSD;
2591
2592     LeaveCriticalSection(&ddraw_cs);
2593     return DD_OK;
2594 }
2595
2596 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
2597         DDSURFACEDESC *surface_desc, DWORD flags)
2598 {
2599     TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
2600
2601     return ddraw_surface7_SetSurfaceDesc((IDirectDrawSurface7 *)surface_from_surface3(iface),
2602             (DDSURFACEDESC2 *)surface_desc, flags);
2603 }
2604
2605 /*****************************************************************************
2606  * IDirectDrawSurface7::GetPalette
2607  *
2608  * Returns the IDirectDrawPalette interface of the palette currently assigned
2609  * to the surface
2610  *
2611  * Params:
2612  *  Pal: Address to write the interface pointer to
2613  *
2614  * Returns:
2615  *  DD_OK on success
2616  *  DDERR_INVALIDPARAMS if Pal is NULL
2617  *
2618  *****************************************************************************/
2619 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
2620 {
2621     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2622     IWineD3DPalette *wPal;
2623     HRESULT hr;
2624     TRACE("(%p)->(%p): Relay\n", This, Pal);
2625
2626     if(!Pal)
2627         return DDERR_INVALIDPARAMS;
2628
2629     EnterCriticalSection(&ddraw_cs);
2630     hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
2631     if(hr != DD_OK)
2632     {
2633         LeaveCriticalSection(&ddraw_cs);
2634         return hr;
2635     }
2636
2637     if(wPal)
2638     {
2639         hr = IWineD3DPalette_GetParent(wPal, (IUnknown **) Pal);
2640     }
2641     else
2642     {
2643         *Pal = NULL;
2644         hr = DDERR_NOPALETTEATTACHED;
2645     }
2646
2647     LeaveCriticalSection(&ddraw_cs);
2648     return hr;
2649 }
2650
2651 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
2652 {
2653     TRACE("iface %p, palette %p.\n", iface, palette);
2654
2655     return ddraw_surface7_GetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2656 }
2657
2658 /*****************************************************************************
2659  * SetColorKeyEnum
2660  *
2661  * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2662  * recursively in the surface tree
2663  *
2664  *****************************************************************************/
2665 struct SCKContext
2666 {
2667     HRESULT ret;
2668     WINEDDCOLORKEY *CKey;
2669     DWORD Flags;
2670 };
2671
2672 static HRESULT WINAPI
2673 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2674                 DDSURFACEDESC2 *desc,
2675                 void *context)
2676 {
2677     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)surface;
2678     struct SCKContext *ctx = context;
2679     HRESULT hr;
2680
2681     hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2682                                      ctx->Flags,
2683                                      ctx->CKey);
2684     if(hr != DD_OK)
2685     {
2686         WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2687         ctx->ret = hr;
2688     }
2689
2690     ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
2691     ddraw_surface7_Release(surface);
2692
2693     return DDENUMRET_OK;
2694 }
2695
2696 /*****************************************************************************
2697  * IDirectDrawSurface7::SetColorKey
2698  *
2699  * Sets the color keying options for the surface. Observations showed that
2700  * in case of complex surfaces the color key has to be assigned to all
2701  * sublevels.
2702  *
2703  * Params:
2704  *  Flags: DDCKEY_*
2705  *  CKey: The new color key
2706  *
2707  * Returns:
2708  *  DD_OK on success
2709  *  See IWineD3DSurface::SetColorKey for details
2710  *
2711  *****************************************************************************/
2712 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2713 {
2714     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2715     DDCOLORKEY FixedCKey;
2716     struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
2717     TRACE("(%p)->(%x,%p)\n", This, Flags, CKey);
2718
2719     EnterCriticalSection(&ddraw_cs);
2720     if (CKey)
2721     {
2722         FixedCKey = *CKey;
2723         /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
2724         if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
2725             FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
2726
2727         switch (Flags & ~DDCKEY_COLORSPACE)
2728         {
2729         case DDCKEY_DESTBLT:
2730             This->surface_desc.ddckCKDestBlt = FixedCKey;
2731             This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2732             break;
2733
2734         case DDCKEY_DESTOVERLAY:
2735             This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
2736             This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2737             break;
2738
2739         case DDCKEY_SRCOVERLAY:
2740             This->surface_desc.ddckCKSrcOverlay = FixedCKey;
2741             This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2742             break;
2743
2744         case DDCKEY_SRCBLT:
2745             This->surface_desc.ddckCKSrcBlt = FixedCKey;
2746             This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2747             break;
2748
2749         default:
2750             LeaveCriticalSection(&ddraw_cs);
2751             return DDERR_INVALIDPARAMS;
2752         }
2753     }
2754     else
2755     {
2756         switch (Flags & ~DDCKEY_COLORSPACE)
2757         {
2758         case DDCKEY_DESTBLT:
2759             This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2760             break;
2761
2762         case DDCKEY_DESTOVERLAY:
2763             This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2764             break;
2765
2766         case DDCKEY_SRCOVERLAY:
2767             This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2768             break;
2769
2770         case DDCKEY_SRCBLT:
2771             This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2772             break;
2773
2774         default:
2775             LeaveCriticalSection(&ddraw_cs);
2776             return DDERR_INVALIDPARAMS;
2777         }
2778     }
2779     ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface, Flags, ctx.CKey);
2780     ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
2781     LeaveCriticalSection(&ddraw_cs);
2782     switch(ctx.ret)
2783     {
2784         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2785         default:                            return ctx.ret;
2786     }
2787 }
2788
2789 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2790 {
2791     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2792
2793     return ddraw_surface7_SetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
2794 }
2795
2796 /*****************************************************************************
2797  * IDirectDrawSurface7::SetPalette
2798  *
2799  * Assigns a DirectDrawPalette object to the surface
2800  *
2801  * Params:
2802  *  Pal: Interface to the palette to set
2803  *
2804  * Returns:
2805  *  DD_OK on success
2806  *
2807  *****************************************************************************/
2808 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
2809 {
2810     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2811     IDirectDrawPalette *oldPal;
2812     IDirectDrawSurfaceImpl *surf;
2813     IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
2814     HRESULT hr;
2815     TRACE("(%p)->(%p)\n", This, Pal);
2816
2817     if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
2818             DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
2819         return DDERR_INVALIDPIXELFORMAT;
2820     }
2821
2822     /* Find the old palette */
2823     EnterCriticalSection(&ddraw_cs);
2824     hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2825     if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2826     {
2827         LeaveCriticalSection(&ddraw_cs);
2828         return hr;
2829     }
2830     if(oldPal) IDirectDrawPalette_Release(oldPal);  /* For the GetPalette */
2831
2832     /* Set the new Palette */
2833     IWineD3DSurface_SetPalette(This->WineD3DSurface,
2834                                PalImpl ? PalImpl->wineD3DPalette : NULL);
2835     /* AddRef the Palette */
2836     if(Pal) IDirectDrawPalette_AddRef(Pal);
2837
2838     /* Release the old palette */
2839     if(oldPal) IDirectDrawPalette_Release(oldPal);
2840
2841     /* If this is a front buffer, also update the back buffers
2842      * TODO: How do things work for palettized cube textures?
2843      */
2844     if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2845     {
2846         /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2847         DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2848
2849         surf = This;
2850         while(1)
2851         {
2852             IDirectDrawSurface7 *attach;
2853             HRESULT hr;
2854             hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &caps2, &attach);
2855             if(hr != DD_OK)
2856             {
2857                 break;
2858             }
2859
2860             TRACE("Setting palette on %p\n", attach);
2861             ddraw_surface7_SetPalette(attach, Pal);
2862             surf = (IDirectDrawSurfaceImpl *)attach;
2863             ddraw_surface7_Release(attach);
2864         }
2865     }
2866
2867     LeaveCriticalSection(&ddraw_cs);
2868     return DD_OK;
2869 }
2870
2871 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
2872 {
2873     TRACE("iface %p, palette %p.\n", iface, palette);
2874
2875     return ddraw_surface7_SetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2876 }
2877
2878 /*****************************************************************************
2879  * The VTable
2880  *****************************************************************************/
2881
2882 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl =
2883 {
2884     /* IUnknown */
2885     ddraw_surface7_QueryInterface,
2886     ddraw_surface7_AddRef,
2887     ddraw_surface7_Release,
2888     /* IDirectDrawSurface */
2889     ddraw_surface7_AddAttachedSurface,
2890     ddraw_surface7_AddOverlayDirtyRect,
2891     ddraw_surface7_Blt,
2892     ddraw_surface7_BltBatch,
2893     ddraw_surface7_BltFast,
2894     ddraw_surface7_DeleteAttachedSurface,
2895     ddraw_surface7_EnumAttachedSurfaces,
2896     ddraw_surface7_EnumOverlayZOrders,
2897     ddraw_surface7_Flip,
2898     ddraw_surface7_GetAttachedSurface,
2899     ddraw_surface7_GetBltStatus,
2900     ddraw_surface7_GetCaps,
2901     ddraw_surface7_GetClipper,
2902     ddraw_surface7_GetColorKey,
2903     ddraw_surface7_GetDC,
2904     ddraw_surface7_GetFlipStatus,
2905     ddraw_surface7_GetOverlayPosition,
2906     ddraw_surface7_GetPalette,
2907     ddraw_surface7_GetPixelFormat,
2908     ddraw_surface7_GetSurfaceDesc,
2909     ddraw_surface7_Initialize,
2910     ddraw_surface7_IsLost,
2911     ddraw_surface7_Lock,
2912     ddraw_surface7_ReleaseDC,
2913     ddraw_surface7_Restore,
2914     ddraw_surface7_SetClipper,
2915     ddraw_surface7_SetColorKey,
2916     ddraw_surface7_SetOverlayPosition,
2917     ddraw_surface7_SetPalette,
2918     ddraw_surface7_Unlock,
2919     ddraw_surface7_UpdateOverlay,
2920     ddraw_surface7_UpdateOverlayDisplay,
2921     ddraw_surface7_UpdateOverlayZOrder,
2922     /* IDirectDrawSurface2 */
2923     ddraw_surface7_GetDDInterface,
2924     ddraw_surface7_PageLock,
2925     ddraw_surface7_PageUnlock,
2926     /* IDirectDrawSurface3 */
2927     ddraw_surface7_SetSurfaceDesc,
2928     /* IDirectDrawSurface4 */
2929     ddraw_surface7_SetPrivateData,
2930     ddraw_surface7_GetPrivateData,
2931     ddraw_surface7_FreePrivateData,
2932     ddraw_surface7_GetUniquenessValue,
2933     ddraw_surface7_ChangeUniquenessValue,
2934     /* IDirectDrawSurface7 */
2935     ddraw_surface7_SetPriority,
2936     ddraw_surface7_GetPriority,
2937     ddraw_surface7_SetLOD,
2938     ddraw_surface7_GetLOD,
2939 };
2940
2941 const IDirectDrawSurface3Vtbl IDirectDrawSurface3_Vtbl =
2942 {
2943     /* IUnknown */
2944     ddraw_surface3_QueryInterface,
2945     ddraw_surface3_AddRef,
2946     ddraw_surface3_Release,
2947     /* IDirectDrawSurface */
2948     ddraw_surface3_AddAttachedSurface,
2949     ddraw_surface3_AddOverlayDirtyRect,
2950     ddraw_surface3_Blt,
2951     ddraw_surface3_BltBatch,
2952     ddraw_surface3_BltFast,
2953     ddraw_surface3_DeleteAttachedSurface,
2954     ddraw_surface3_EnumAttachedSurfaces,
2955     ddraw_surface3_EnumOverlayZOrders,
2956     ddraw_surface3_Flip,
2957     ddraw_surface3_GetAttachedSurface,
2958     ddraw_surface3_GetBltStatus,
2959     ddraw_surface3_GetCaps,
2960     ddraw_surface3_GetClipper,
2961     ddraw_surface3_GetColorKey,
2962     ddraw_surface3_GetDC,
2963     ddraw_surface3_GetFlipStatus,
2964     ddraw_surface3_GetOverlayPosition,
2965     ddraw_surface3_GetPalette,
2966     ddraw_surface3_GetPixelFormat,
2967     ddraw_surface3_GetSurfaceDesc,
2968     ddraw_surface3_Initialize,
2969     ddraw_surface3_IsLost,
2970     ddraw_surface3_Lock,
2971     ddraw_surface3_ReleaseDC,
2972     ddraw_surface3_Restore,
2973     ddraw_surface3_SetClipper,
2974     ddraw_surface3_SetColorKey,
2975     ddraw_surface3_SetOverlayPosition,
2976     ddraw_surface3_SetPalette,
2977     ddraw_surface3_Unlock,
2978     ddraw_surface3_UpdateOverlay,
2979     ddraw_surface3_UpdateOverlayDisplay,
2980     ddraw_surface3_UpdateOverlayZOrder,
2981     /* IDirectDrawSurface2 */
2982     ddraw_surface3_GetDDInterface,
2983     ddraw_surface3_PageLock,
2984     ddraw_surface3_PageUnlock,
2985     /* IDirectDrawSurface3 */
2986     ddraw_surface3_SetSurfaceDesc,
2987 };