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