ddraw: Removed superflous NULL check (Coverity).
[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  * Copyright (c) 2011 Ričardas Barkauskas for CodeWeavers
8  *
9  * This file contains the (internal) driver registration functions,
10  * driver enumeration APIs and DirectDraw creation functions.
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26
27 #include "config.h"
28 #include "wine/port.h"
29
30 #include "ddraw_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
33
34 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface);
35 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface);
36
37 static inline IDirectDrawSurfaceImpl *impl_from_IDirectDrawGammaControl(IDirectDrawGammaControl *iface)
38 {
39     return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawGammaControl_iface);
40 }
41
42 /*****************************************************************************
43  * IUnknown parts follow
44  *****************************************************************************/
45
46 /*****************************************************************************
47  * IDirectDrawSurface7::QueryInterface
48  *
49  * A normal QueryInterface implementation. For QueryInterface rules
50  * see ddraw.c, IDirectDraw7::QueryInterface. This method
51  * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
52  * in all versions, the IDirectDrawGammaControl interface and it can
53  * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
54  *
55  * Params:
56  *  riid: The interface id queried for
57  *  obj: Address to write the pointer to
58  *
59  * Returns:
60  *  S_OK on success
61  *  E_NOINTERFACE if the requested interface wasn't found
62  *
63  *****************************************************************************/
64 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
65 {
66     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
67
68     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
69
70     /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
71     *obj = NULL;
72
73     if(!riid)
74         return DDERR_INVALIDPARAMS;
75
76     if (IsEqualGUID(riid, &IID_IUnknown)
77      || IsEqualGUID(riid, &IID_IDirectDrawSurface7) )
78     {
79         IUnknown_AddRef(iface);
80         *obj = iface;
81         TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
82         return S_OK;
83     }
84     else if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
85     {
86         IUnknown_AddRef(iface);
87         *obj = &This->IDirectDrawSurface4_iface;
88         TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
89         return S_OK;
90     }
91     else if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
92     {
93         IUnknown_AddRef(iface);
94         *obj = &This->IDirectDrawSurface3_iface;
95         TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
96         return S_OK;
97     }
98     else if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
99     {
100         IUnknown_AddRef(iface);
101         *obj = &This->IDirectDrawSurface2_iface;
102         TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
103         return S_OK;
104     }
105     else if (IsEqualGUID(riid, &IID_IDirectDrawSurface))
106     {
107         IUnknown_AddRef(iface);
108         *obj = &This->IDirectDrawSurface_iface;
109         TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
110         return S_OK;
111     }
112     else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
113     {
114         IUnknown_AddRef(iface);
115         *obj = &This->IDirectDrawGammaControl_iface;
116         TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
117         return S_OK;
118     }
119     else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
120              IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
121              IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
122     {
123         IDirect3DDevice7 *d3d;
124
125         /* Call into IDirect3D7 for creation */
126         IDirect3D7_CreateDevice(&This->ddraw->IDirect3D7_iface, riid, &This->IDirectDrawSurface7_iface,
127                 &d3d);
128
129         if (d3d)
130         {
131             *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
132             TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
133             return S_OK;
134         }
135
136         WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
137         return E_NOINTERFACE;
138     }
139     else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
140              IsEqualGUID( &IID_IDirect3DTexture2, riid ))
141     {
142         if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
143         {
144             *obj = &This->IDirect3DTexture_vtbl;
145             TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
146         }
147         else
148         {
149             *obj = &This->IDirect3DTexture2_vtbl;
150             TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
151         }
152         IUnknown_AddRef( (IUnknown *) *obj);
153         return S_OK;
154     }
155
156     ERR("No interface\n");
157     return E_NOINTERFACE;
158 }
159
160 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
161 {
162     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
163     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
164
165     return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
166 }
167
168 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
169 {
170     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
171     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
172
173     return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
174 }
175
176 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
177 {
178     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
179     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
180
181     return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
182 }
183
184 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
185 {
186     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
187     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
188
189     return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
190 }
191
192 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
193         REFIID riid, void **object)
194 {
195     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
196
197     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
198
199     return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
200 }
201
202 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
203 {
204     IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
205     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
206
207     return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
208 }
209
210 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
211 {
212     IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
213     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
214
215     return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
216 }
217
218 /*****************************************************************************
219  * IDirectDrawSurface7::AddRef
220  *
221  * A normal addref implementation
222  *
223  * Returns:
224  *  The new refcount
225  *
226  *****************************************************************************/
227 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
228 {
229     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
230     ULONG refCount = InterlockedIncrement(&This->ref);
231
232     TRACE("%p increasing refcount to %u.\n", This, refCount);
233
234     if (refCount == 1)
235     {
236         EnterCriticalSection(&ddraw_cs);
237         if (This->wined3d_surface)
238             wined3d_surface_incref(This->wined3d_surface);
239         if (This->wined3d_texture)
240             wined3d_texture_incref(This->wined3d_texture);
241         LeaveCriticalSection(&ddraw_cs);
242     }
243
244     return refCount;
245 }
246
247 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
248 {
249     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
250     TRACE("iface %p.\n", iface);
251
252     return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
253 }
254
255 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
256 {
257     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
258     TRACE("iface %p.\n", iface);
259
260     return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
261 }
262
263 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
264 {
265     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
266     TRACE("iface %p.\n", iface);
267
268     return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
269 }
270
271 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
272 {
273     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
274     TRACE("iface %p.\n", iface);
275
276     return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
277 }
278
279 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
280 {
281     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
282
283     TRACE("iface %p.\n", iface);
284
285     return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
286 }
287
288 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
289 {
290     IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
291     TRACE("iface %p.\n", iface);
292
293     return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
294 }
295
296 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
297 {
298     IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
299     TRACE("iface %p.\n", iface);
300
301     return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
302 }
303
304 /*****************************************************************************
305  * ddraw_surface_destroy
306  *
307  * A helper function for IDirectDrawSurface7::Release
308  *
309  * Frees the surface, regardless of its refcount.
310  *  See IDirectDrawSurface7::Release for more information
311  *
312  * Params:
313  *  This: Surface to free
314  *
315  *****************************************************************************/
316 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
317 {
318     TRACE("surface %p.\n", This);
319
320     /* Check the refcount and give a warning */
321     if(This->ref > 1)
322     {
323         /* This can happen when a complex surface is destroyed,
324          * because the 2nd surface was addref()ed when the app
325          * called GetAttachedSurface
326          */
327         WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
328     }
329
330     if (This->wined3d_surface)
331         wined3d_surface_decref(This->wined3d_surface);
332 }
333
334 static void ddraw_surface_cleanup(IDirectDrawSurfaceImpl *surface)
335 {
336     IDirectDrawSurfaceImpl *surf;
337     IUnknown *ifaceToRelease;
338     UINT i;
339
340     TRACE("surface %p.\n", surface);
341
342     if (surface->wined3d_swapchain)
343     {
344         IDirectDrawImpl *ddraw = surface->ddraw;
345
346         /* If it's the render target, destroy the D3D device. */
347         if (ddraw->d3d_initialized && surface == ddraw->d3d_target)
348         {
349             TRACE("Destroying the render target, uninitializing D3D.\n");
350
351             for (i = 0; i < ddraw->numConvertedDecls; ++i)
352             {
353                 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
354             }
355             HeapFree(GetProcessHeap(), 0, ddraw->decls);
356             ddraw->numConvertedDecls = 0;
357
358             if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
359             {
360                 ERR("Failed to uninit 3D.\n");
361             }
362             else
363             {
364                 /* Free the d3d window if one was created. */
365                 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
366                 {
367                     TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
368                     DestroyWindow(ddraw->d3d_window);
369                     ddraw->d3d_window = 0;
370                 }
371             }
372
373             ddraw->d3d_initialized = FALSE;
374             ddraw->d3d_target = NULL;
375         }
376         else
377         {
378             wined3d_device_uninit_gdi(ddraw->wined3d_device);
379         }
380
381         surface->wined3d_swapchain = NULL;
382
383         /* Reset to the default surface implementation type. This is needed
384          * if applications use non render target surfaces and expect blits to
385          * work after destroying the render target.
386          *
387          * TODO: Recreate existing offscreen surfaces. */
388         ddraw->ImplType = DefaultSurfaceType;
389
390         TRACE("D3D unloaded.\n");
391     }
392
393     /* The refcount test shows that the palette is detached when the surface
394      * is destroyed. */
395     IDirectDrawSurface7_SetPalette(&surface->IDirectDrawSurface7_iface, NULL);
396
397     /* Loop through all complex attached surfaces and destroy them.
398      *
399      * Yet again, only the root can have more than one complexly attached
400      * surface, all the others have a total of one. */
401     for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
402     {
403         if (!surface->complex_array[i])
404             break;
405
406         surf = surface->complex_array[i];
407         surface->complex_array[i] = NULL;
408         while (surf)
409         {
410             IDirectDrawSurfaceImpl *destroy = surf;
411             surf = surf->complex_array[0];              /* Iterate through the "tree" */
412             ddraw_surface_destroy(destroy);             /* Destroy it */
413         }
414     }
415
416     ifaceToRelease = surface->ifaceToRelease;
417
418     /* Destroy the root surface. */
419     ddraw_surface_destroy(surface);
420
421     /* Reduce the ddraw refcount */
422     if (ifaceToRelease)
423         IUnknown_Release(ifaceToRelease);
424 }
425
426 /*****************************************************************************
427  * IDirectDrawSurface7::Release
428  *
429  * Reduces the surface's refcount by 1. If the refcount falls to 0, the
430  * surface is destroyed.
431  *
432  * Destroying the surface is a bit tricky. For the connection between
433  * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
434  * It has a nice graph explaining the connection.
435  *
436  * What happens here is basically this:
437  * When a surface is destroyed, its WineD3DSurface is released,
438  * and the refcount of the DirectDraw interface is reduced by 1. If it has
439  * complex surfaces attached to it, then these surfaces are destroyed too,
440  * regardless of their refcount. If any surface being destroyed has another
441  * surface attached to it (with a "soft" attachment, not complex), then
442  * this surface is detached with DeleteAttachedSurface.
443  *
444  * When the surface is a texture, the WineD3DTexture is released.
445  * If the surface is the Direct3D render target, then the D3D
446  * capabilities of the WineD3DDevice are uninitialized, which causes the
447  * swapchain to be released.
448  *
449  * When a complex sublevel falls to ref zero, then this is ignored.
450  *
451  * Returns:
452  *  The new refcount
453  *
454  *****************************************************************************/
455 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
456 {
457     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
458     ULONG ref = InterlockedDecrement(&This->ref);
459
460     TRACE("%p decreasing refcount to %u.\n", This, ref);
461
462     if (ref == 0)
463     {
464         /* Complex attached surfaces are destroyed implicitly when the root is released */
465         EnterCriticalSection(&ddraw_cs);
466         if(!This->is_complex_root)
467         {
468             WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
469             LeaveCriticalSection(&ddraw_cs);
470             return ref;
471         }
472         if (This->wined3d_texture) /* If it's a texture, destroy the wined3d texture. */
473             wined3d_texture_decref(This->wined3d_texture);
474         else
475             ddraw_surface_cleanup(This);
476         LeaveCriticalSection(&ddraw_cs);
477     }
478
479     return ref;
480 }
481
482 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
483 {
484     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
485     TRACE("iface %p.\n", iface);
486
487     return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
488 }
489
490 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
491 {
492     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
493     TRACE("iface %p.\n", iface);
494
495     return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
496 }
497
498 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
499 {
500     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
501     TRACE("iface %p.\n", iface);
502
503     return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
504 }
505
506 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
507 {
508     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
509     TRACE("iface %p.\n", iface);
510
511     return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
512 }
513
514 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
515 {
516     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
517
518     TRACE("iface %p.\n", iface);
519
520     return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
521 }
522
523 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
524 {
525     IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
526     TRACE("iface %p.\n", iface);
527
528     return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
529 }
530
531 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
532 {
533     IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
534     TRACE("iface %p.\n", iface);
535
536     return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
537 }
538
539 /*****************************************************************************
540  * IDirectDrawSurface7::GetAttachedSurface
541  *
542  * Returns an attached surface with the requested caps. Surface attachment
543  * and complex surfaces are not clearly described by the MSDN or sdk,
544  * so this method is tricky and likely to contain problems.
545  * This implementation searches the complex list first, then the
546  * attachment chain.
547  *
548  * The chains are searched from This down to the last surface in the chain,
549  * not from the first element in the chain. The first surface found is
550  * returned. The MSDN says that this method fails if more than one surface
551  * matches the caps, but it is not sure if that is right. The attachment
552  * structure may not even allow two matching surfaces.
553  *
554  * The found surface is AddRef-ed before it is returned.
555  *
556  * Params:
557  *  Caps: Pointer to a DDCAPS2 structure describing the caps asked for
558  *  Surface: Address to store the found surface
559  *
560  * Returns:
561  *  DD_OK on success
562  *  DDERR_INVALIDPARAMS if Caps or Surface is NULL
563  *  DDERR_NOTFOUND if no surface was found
564  *
565  *****************************************************************************/
566 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
567         DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
568 {
569     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
570     IDirectDrawSurfaceImpl *surf;
571     DDSCAPS2 our_caps;
572     int i;
573
574     TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
575
576     EnterCriticalSection(&ddraw_cs);
577
578     if(This->version < 7)
579     {
580         /* Earlier dx apps put garbage into these members, clear them */
581         our_caps.dwCaps = Caps->dwCaps;
582         our_caps.dwCaps2 = 0;
583         our_caps.dwCaps3 = 0;
584         our_caps.dwCaps4 = 0;
585     }
586     else
587     {
588         our_caps = *Caps;
589     }
590
591     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 */
592
593     for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
594     {
595         surf = This->complex_array[i];
596         if(!surf) break;
597
598         if (TRACE_ON(ddraw))
599         {
600             TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
601                    surf->surface_desc.ddsCaps.dwCaps,
602                    surf->surface_desc.ddsCaps.dwCaps2,
603                    surf->surface_desc.ddsCaps.dwCaps3,
604                    surf->surface_desc.ddsCaps.dwCaps4);
605         }
606
607         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
608             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
609
610             /* MSDN: "This method fails if more than one surface is attached
611              * that matches the capabilities requested."
612              *
613              * Not sure how to test this.
614              */
615
616             TRACE("(%p): Returning surface %p\n", This, surf);
617             TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
618             *Surface = &surf->IDirectDrawSurface7_iface;
619             ddraw_surface7_AddRef(*Surface);
620             LeaveCriticalSection(&ddraw_cs);
621             return DD_OK;
622         }
623     }
624
625     /* Next, look at the attachment chain */
626     surf = This;
627
628     while( (surf = surf->next_attached) )
629     {
630         if (TRACE_ON(ddraw))
631         {
632             TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
633                    surf->surface_desc.ddsCaps.dwCaps,
634                    surf->surface_desc.ddsCaps.dwCaps2,
635                    surf->surface_desc.ddsCaps.dwCaps3,
636                    surf->surface_desc.ddsCaps.dwCaps4);
637         }
638
639         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
640             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
641
642             TRACE("(%p): Returning surface %p\n", This, surf);
643             *Surface = &surf->IDirectDrawSurface7_iface;
644             ddraw_surface7_AddRef(*Surface);
645             LeaveCriticalSection(&ddraw_cs);
646             return DD_OK;
647         }
648     }
649
650     TRACE("(%p) Didn't find a valid surface\n", This);
651     LeaveCriticalSection(&ddraw_cs);
652
653     *Surface = NULL;
654     return DDERR_NOTFOUND;
655 }
656
657 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
658         DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
659 {
660     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
661     IDirectDrawSurface7 *attachment7;
662     IDirectDrawSurfaceImpl *attachment_impl;
663     HRESULT hr;
664
665     TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
666
667     hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
668             caps, &attachment7);
669     if (FAILED(hr))
670     {
671         *attachment = NULL;
672         return hr;
673     }
674     attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
675     *attachment = &attachment_impl->IDirectDrawSurface4_iface;
676
677     return hr;
678 }
679
680 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
681         DDSCAPS *caps, IDirectDrawSurface3 **attachment)
682 {
683     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
684     IDirectDrawSurface7 *attachment7;
685     IDirectDrawSurfaceImpl *attachment_impl;
686     DDSCAPS2 caps2;
687     HRESULT hr;
688
689     TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
690
691     caps2.dwCaps  = caps->dwCaps;
692     caps2.dwCaps2 = 0;
693     caps2.dwCaps3 = 0;
694     caps2.dwCaps4 = 0;
695
696     hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
697             &caps2, &attachment7);
698     if (FAILED(hr))
699     {
700         *attachment = NULL;
701         return hr;
702     }
703     attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
704     *attachment = &attachment_impl->IDirectDrawSurface3_iface;
705
706     return hr;
707 }
708
709 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
710         DDSCAPS *caps, IDirectDrawSurface2 **attachment)
711 {
712     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
713     IDirectDrawSurface7 *attachment7;
714     IDirectDrawSurfaceImpl *attachment_impl;
715     DDSCAPS2 caps2;
716     HRESULT hr;
717
718     TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
719
720     caps2.dwCaps  = caps->dwCaps;
721     caps2.dwCaps2 = 0;
722     caps2.dwCaps3 = 0;
723     caps2.dwCaps4 = 0;
724
725     hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
726             &caps2, &attachment7);
727     if (FAILED(hr))
728     {
729         *attachment = NULL;
730         return hr;
731     }
732     attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
733     *attachment = &attachment_impl->IDirectDrawSurface2_iface;
734
735     return hr;
736 }
737
738 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
739         DDSCAPS *caps, IDirectDrawSurface **attachment)
740 {
741     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
742     IDirectDrawSurface7 *attachment7;
743     IDirectDrawSurfaceImpl *attachment_impl;
744     DDSCAPS2 caps2;
745     HRESULT hr;
746
747     TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
748
749     caps2.dwCaps  = caps->dwCaps;
750     caps2.dwCaps2 = 0;
751     caps2.dwCaps3 = 0;
752     caps2.dwCaps4 = 0;
753
754     hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
755             &caps2, &attachment7);
756     if (FAILED(hr))
757     {
758         *attachment = NULL;
759         return hr;
760     }
761     attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
762     *attachment = &attachment_impl->IDirectDrawSurface_iface;
763
764     return hr;
765 }
766
767 /*****************************************************************************
768  * IDirectDrawSurface7::Lock
769  *
770  * Locks the surface and returns a pointer to the surface's memory
771  *
772  * Params:
773  *  Rect: Rectangle to lock. If NULL, the whole surface is locked
774  *  DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
775  *  Flags: Locking flags, e.g Read only or write only
776  *  h: An event handle that's not used and must be NULL
777  *
778  * Returns:
779  *  DD_OK on success
780  *  DDERR_INVALIDPARAMS if DDSD is NULL
781  *  For more details, see IWineD3DSurface::LockRect
782  *
783  *****************************************************************************/
784 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
785         RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
786 {
787     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
788     WINED3DLOCKED_RECT LockedRect;
789     HRESULT hr;
790
791     TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
792             iface, wine_dbgstr_rect(Rect), DDSD, Flags, h);
793
794     if(!DDSD)
795         return DDERR_INVALIDPARAMS;
796
797     /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
798     EnterCriticalSection(&ddraw_cs);
799
800     /* Should I check for the handle to be NULL?
801      *
802      * The DDLOCK flags and the D3DLOCK flags are equal
803      * for the supported values. The others are ignored by WineD3D
804      */
805
806     if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
807        DDSD->dwSize != sizeof(DDSURFACEDESC2))
808     {
809         WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
810         LeaveCriticalSection(&ddraw_cs);
811         return DDERR_INVALIDPARAMS;
812     }
813
814     /* Windows zeroes this if the rect is invalid */
815     DDSD->lpSurface = 0;
816
817     if (Rect)
818     {
819         if ((Rect->left < 0)
820                 || (Rect->top < 0)
821                 || (Rect->left > Rect->right)
822                 || (Rect->top > Rect->bottom)
823                 || (Rect->right > This->surface_desc.dwWidth)
824                 || (Rect->bottom > This->surface_desc.dwHeight))
825         {
826             WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
827             LeaveCriticalSection(&ddraw_cs);
828             return DDERR_INVALIDPARAMS;
829         }
830     }
831
832     hr = wined3d_surface_map(This->wined3d_surface, &LockedRect, Rect, Flags);
833     if (FAILED(hr))
834     {
835         LeaveCriticalSection(&ddraw_cs);
836         switch(hr)
837         {
838             /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
839              * specific error. But since IWineD3DSurface::LockRect returns that error in this
840              * only occasion, keep d3d8 and d3d9 free from the return value override. There are
841              * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
842              * is much easier to do it in one place in ddraw
843              */
844             case WINED3DERR_INVALIDCALL:    return DDERR_SURFACEBUSY;
845             default:                        return hr;
846         }
847     }
848
849     /* Override the memory area. The pitch should be set already. Strangely windows
850      * does not set the LPSURFACE flag on locked surfaces !?!.
851      * DDSD->dwFlags |= DDSD_LPSURFACE;
852      */
853     This->surface_desc.lpSurface = LockedRect.pBits;
854     DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
855
856     TRACE("locked surface returning description :\n");
857     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
858
859     LeaveCriticalSection(&ddraw_cs);
860     return DD_OK;
861 }
862
863 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
864         DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
865 {
866     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
867     TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
868             iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
869
870     return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
871             rect, surface_desc, flags, h);
872 }
873
874 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
875         DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
876 {
877     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
878     TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
879             iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
880
881     return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
882             rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
883 }
884
885 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
886         DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
887 {
888     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
889     TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
890             iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
891
892     return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
893             rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
894 }
895
896 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
897         DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
898 {
899     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
900     TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
901             iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
902
903     return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
904             rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
905 }
906
907 /*****************************************************************************
908  * IDirectDrawSurface7::Unlock
909  *
910  * Unlocks an locked surface
911  *
912  * Params:
913  *  Rect: Not used by this implementation
914  *
915  * Returns:
916  *  D3D_OK on success
917  *  For more details, see IWineD3DSurface::UnlockRect
918  *
919  *****************************************************************************/
920 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
921 {
922     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
923     HRESULT hr;
924
925     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
926
927     EnterCriticalSection(&ddraw_cs);
928     hr = wined3d_surface_unmap(This->wined3d_surface);
929     if (SUCCEEDED(hr))
930     {
931         This->surface_desc.lpSurface = NULL;
932     }
933     LeaveCriticalSection(&ddraw_cs);
934     return hr;
935 }
936
937 static HRESULT WINAPI ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
938 {
939     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
940     TRACE("iface %p, rect %p.\n", iface, pRect);
941
942     return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, pRect);
943 }
944
945 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
946 {
947     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
948     TRACE("iface %p, data %p.\n", iface, data);
949
950     /* data might not be the LPRECT of later versions, so drop it. */
951     return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
952 }
953
954 static HRESULT WINAPI ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
955 {
956     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
957     TRACE("iface %p, data %p.\n", iface, data);
958
959     /* data might not be the LPRECT of later versions, so drop it. */
960     return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
961 }
962
963 static HRESULT WINAPI ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
964 {
965     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
966     TRACE("iface %p, data %p.\n", iface, data);
967
968     /* data might not be the LPRECT of later versions, so drop it. */
969     return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
970 }
971
972 /*****************************************************************************
973  * IDirectDrawSurface7::Flip
974  *
975  * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
976  * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
977  * the flip target is passed to WineD3D, even if the app didn't specify one
978  *
979  * Params:
980  *  DestOverride: Specifies the surface that will become the new front
981  *                buffer. If NULL, the current back buffer is used
982  *  Flags: some DirectDraw flags, see include/ddraw.h
983  *
984  * Returns:
985  *  DD_OK on success
986  *  DDERR_NOTFLIPPABLE if no flip target could be found
987  *  DDERR_INVALIDOBJECT if the surface isn't a front buffer
988  *  For more details, see IWineD3DSurface::Flip
989  *
990  *****************************************************************************/
991 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
992 {
993     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
994     IDirectDrawSurfaceImpl *Override = unsafe_impl_from_IDirectDrawSurface7(DestOverride);
995     IDirectDrawSurface7 *Override7;
996     HRESULT hr;
997
998     TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
999
1000     /* Flip has to be called from a front buffer
1001      * What about overlay surfaces, AFAIK they can flip too?
1002      */
1003     if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
1004         return DDERR_INVALIDOBJECT; /* Unchecked */
1005
1006     EnterCriticalSection(&ddraw_cs);
1007
1008     /* WineD3D doesn't keep track of attached surface, so find the target */
1009     if(!Override)
1010     {
1011         DDSCAPS2 Caps;
1012
1013         memset(&Caps, 0, sizeof(Caps));
1014         Caps.dwCaps |= DDSCAPS_BACKBUFFER;
1015         hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
1016         if(hr != DD_OK)
1017         {
1018             ERR("Can't find a flip target\n");
1019             LeaveCriticalSection(&ddraw_cs);
1020             return DDERR_NOTFLIPPABLE; /* Unchecked */
1021         }
1022         Override = impl_from_IDirectDrawSurface7(Override7);
1023
1024         /* For the GetAttachedSurface */
1025         ddraw_surface7_Release(Override7);
1026     }
1027
1028     hr = wined3d_surface_flip(This->wined3d_surface, Override->wined3d_surface, Flags);
1029     LeaveCriticalSection(&ddraw_cs);
1030     return hr;
1031 }
1032
1033 static HRESULT WINAPI ddraw_surface4_Flip(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *dst, DWORD flags)
1034 {
1035     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1036     IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1037     TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1038
1039     return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1040             dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1041 }
1042
1043 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
1044 {
1045     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1046     IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1047     TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1048
1049     return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1050             dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1051 }
1052
1053 static HRESULT WINAPI ddraw_surface2_Flip(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *dst, DWORD flags)
1054 {
1055     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1056     IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1057     TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1058
1059     return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1060             dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1061 }
1062
1063 static HRESULT WINAPI ddraw_surface1_Flip(IDirectDrawSurface *iface, IDirectDrawSurface *dst, DWORD flags)
1064 {
1065     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1066     IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1067     TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1068
1069     return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1070             dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1071 }
1072
1073 /*****************************************************************************
1074  * IDirectDrawSurface7::Blt
1075  *
1076  * Performs a blit on the surface
1077  *
1078  * Params:
1079  *  DestRect: Destination rectangle, can be NULL
1080  *  SrcSurface: Source surface, can be NULL
1081  *  SrcRect: Source rectangle, can be NULL
1082  *  Flags: Blt flags
1083  *  DDBltFx: Some extended blt parameters, connected to the flags
1084  *
1085  * Returns:
1086  *  D3D_OK on success
1087  *  See IWineD3DSurface::Blt for more details
1088  *
1089  *****************************************************************************/
1090 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1091         IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1092 {
1093     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1094     IDirectDrawSurfaceImpl *Src = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1095     HRESULT hr;
1096
1097     TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1098             iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1099
1100     /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1101      * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1102      */
1103     if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1104         WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1105         return DDERR_INVALIDPARAMS;
1106     }
1107
1108     if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1109         WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1110         return DDERR_INVALIDPARAMS;
1111     }
1112
1113     /* Sizes can change, therefore hold the lock when testing the rectangles */
1114     EnterCriticalSection(&ddraw_cs);
1115     if(DestRect)
1116     {
1117         if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
1118            DestRect->right > This->surface_desc.dwWidth ||
1119            DestRect->bottom > This->surface_desc.dwHeight)
1120         {
1121             WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
1122             LeaveCriticalSection(&ddraw_cs);
1123             return DDERR_INVALIDRECT;
1124         }
1125     }
1126     if(Src && SrcRect)
1127     {
1128         if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
1129            SrcRect->right > Src->surface_desc.dwWidth ||
1130            SrcRect->bottom > Src->surface_desc.dwHeight)
1131         {
1132             WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
1133             LeaveCriticalSection(&ddraw_cs);
1134             return DDERR_INVALIDRECT;
1135         }
1136     }
1137
1138     if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
1139         WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1140         LeaveCriticalSection(&ddraw_cs);
1141         return DDERR_INVALIDPARAMS;
1142     }
1143
1144     /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1145      * does, copy the struct, and replace the ddraw surfaces with the wined3d
1146      * surfaces. So far no blitting operations using surfaces in the bltfx
1147      * struct are supported anyway. */
1148     hr = wined3d_surface_blt(This->wined3d_surface, DestRect, Src ? Src->wined3d_surface : NULL,
1149             SrcRect, Flags, (WINEDDBLTFX *)DDBltFx, WINED3DTEXF_LINEAR);
1150
1151     LeaveCriticalSection(&ddraw_cs);
1152     switch(hr)
1153     {
1154         case WINED3DERR_NOTAVAILABLE:       return DDERR_UNSUPPORTED;
1155         case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
1156         default:                            return hr;
1157     }
1158 }
1159
1160 static HRESULT WINAPI ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1161         IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1162 {
1163     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1164     IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1165     TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1166             iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1167
1168     return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1169             src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1170 }
1171
1172 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1173         IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1174 {
1175     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1176     IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1177     TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1178             iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1179
1180     return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1181             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1182 }
1183
1184 static HRESULT WINAPI ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1185         IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1186 {
1187     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1188     IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1189     TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1190             iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1191
1192     return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1193             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1194 }
1195
1196 static HRESULT WINAPI ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1197         IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1198 {
1199     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1200     IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1201     TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1202             iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1203
1204     return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1205             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1206 }
1207
1208 /*****************************************************************************
1209  * IDirectDrawSurface7::AddAttachedSurface
1210  *
1211  * Attaches a surface to another surface. How the surface attachments work
1212  * is not totally understood yet, and this method is prone to problems.
1213  * he surface that is attached is AddRef-ed.
1214  *
1215  * Tests with complex surfaces suggest that the surface attachments form a
1216  * tree, but no method to test this has been found yet.
1217  *
1218  * The attachment list consists of a first surface (first_attached) and
1219  * for each surface a pointer to the next attached surface (next_attached).
1220  * For the first surface, and a surface that has no attachments
1221  * first_attached points to the surface itself. A surface that has
1222  * no successors in the chain has next_attached set to NULL.
1223  *
1224  * Newly attached surfaces are attached right after the root surface.
1225  * If a surface is attached to a complex surface compound, it's attached to
1226  * the surface that the app requested, not the complex root. See
1227  * GetAttachedSurface for a description how surfaces are found.
1228  *
1229  * This is how the current implementation works, and it was coded by looking
1230  * at the needs of the applications.
1231  *
1232  * So far only Z-Buffer attachments are tested, and they are activated in
1233  * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1234  * Back buffers should work in 2D mode, but they are not tested(They can be
1235  * attached in older iface versions). Rendering to the front buffer and
1236  * switching between that and double buffering is not yet implemented in
1237  * WineD3D, so for 3D it might have unexpected results.
1238  *
1239  * ddraw_surface_attach_surface is the real thing,
1240  * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1241  * performs additional checks. Version 7 of this interface is much more restrictive
1242  * than its predecessors.
1243  *
1244  * Params:
1245  *  Attach: Surface to attach to iface
1246  *
1247  * Returns:
1248  *  DD_OK on success
1249  *  DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1250  *
1251  *****************************************************************************/
1252 static HRESULT ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
1253 {
1254     TRACE("surface %p, attachment %p.\n", This, Surf);
1255
1256     if(Surf == This)
1257         return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1258
1259     EnterCriticalSection(&ddraw_cs);
1260
1261     /* Check if the surface is already attached somewhere */
1262     if (Surf->next_attached || Surf->first_attached != Surf)
1263     {
1264         /* TODO: Test for the structure of the manual attachment. Is it a
1265          * chain or a list? What happens if one surface is attached to 2
1266          * different surfaces? */
1267         WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1268                 Surf, Surf->next_attached, Surf->first_attached);
1269
1270         LeaveCriticalSection(&ddraw_cs);
1271         return DDERR_SURFACEALREADYATTACHED;
1272     }
1273
1274     /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1275     Surf->next_attached = This->next_attached;
1276     Surf->first_attached = This->first_attached;
1277     This->next_attached = Surf;
1278
1279     /* Check if the WineD3D depth stencil needs updating */
1280     if(This->ddraw->d3ddevice)
1281     {
1282         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1283     }
1284
1285     ddraw_surface7_AddRef(&Surf->IDirectDrawSurface7_iface);
1286     LeaveCriticalSection(&ddraw_cs);
1287     return DD_OK;
1288 }
1289
1290 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
1291 {
1292     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1293     IDirectDrawSurfaceImpl *Surf = unsafe_impl_from_IDirectDrawSurface7(Attach);
1294
1295     TRACE("iface %p, attachment %p.\n", iface, Attach);
1296
1297     /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1298     if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1299     {
1300
1301         WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1302               Surf->surface_desc.ddsCaps.dwCaps);
1303         return DDERR_CANNOTATTACHSURFACE;
1304     }
1305
1306     return ddraw_surface_attach_surface(This, Surf);
1307 }
1308
1309 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1310 {
1311     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1312     IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1313
1314     TRACE("iface %p, attachment %p.\n", iface, attachment);
1315
1316     return ddraw_surface7_AddAttachedSurface(&This->IDirectDrawSurface7_iface,
1317             attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1318 }
1319 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1320 {
1321     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1322     IDirectDrawSurfaceImpl *attach_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1323
1324     TRACE("iface %p, attachment %p.\n", iface, attachment);
1325
1326     /* Tests suggest that
1327      * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1328      * -> offscreen plain surfaces can be attached to primaries
1329      * -> primaries can be attached to offscreen plain surfaces
1330      * -> z buffers can be attached to primaries */
1331     if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1332             && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1333     {
1334         /* Sizes have to match */
1335         if (attach_impl->surface_desc.dwWidth != This->surface_desc.dwWidth
1336                 || attach_impl->surface_desc.dwHeight != This->surface_desc.dwHeight)
1337         {
1338             WARN("Surface sizes do not match.\n");
1339             return DDERR_CANNOTATTACHSURFACE;
1340         }
1341         /* OK */
1342     }
1343     else if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1344             && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1345     {
1346         /* OK */
1347     }
1348     else
1349     {
1350         WARN("Invalid attachment combination.\n");
1351         return DDERR_CANNOTATTACHSURFACE;
1352     }
1353
1354     return ddraw_surface_attach_surface(This, attach_impl);
1355 }
1356
1357 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1358 {
1359     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1360     IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1361
1362     TRACE("iface %p, attachment %p.\n", iface, attachment);
1363
1364     return ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1365             attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1366 }
1367
1368 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1369 {
1370     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1371     IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1372
1373     TRACE("iface %p, attachment %p.\n", iface, attachment);
1374
1375     return ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1376             attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1377 }
1378
1379 /*****************************************************************************
1380  * IDirectDrawSurface7::DeleteAttachedSurface
1381  *
1382  * Removes a surface from the attachment chain. The surface's refcount
1383  * is decreased by one after it has been removed
1384  *
1385  * Params:
1386  *  Flags: Some flags, not used by this implementation
1387  *  Attach: Surface to detach
1388  *
1389  * Returns:
1390  *  DD_OK on success
1391  *  DDERR_SURFACENOTATTACHED if the surface isn't attached to
1392  *
1393  *****************************************************************************/
1394 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1395         DWORD Flags, IDirectDrawSurface7 *Attach)
1396 {
1397     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1398     IDirectDrawSurfaceImpl *Surf = unsafe_impl_from_IDirectDrawSurface7(Attach);
1399     IDirectDrawSurfaceImpl *Prev = This;
1400
1401     TRACE("iface %p, flags %#x, attachment %p.\n", iface, Flags, Attach);
1402
1403     EnterCriticalSection(&ddraw_cs);
1404     if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1405     {
1406         LeaveCriticalSection(&ddraw_cs);
1407         return DDERR_CANNOTDETACHSURFACE;
1408     }
1409
1410     /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1411     if (This->surface_desc.ddsCaps.dwCaps &
1412         Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1413     {
1414         Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1415         /* FIXME: we should probably also subtract from dwMipMapCount of this
1416          * and all parent surfaces */
1417     }
1418
1419     /* Find the predecessor of the detached surface */
1420     while(Prev)
1421     {
1422         if(Prev->next_attached == Surf) break;
1423         Prev = Prev->next_attached;
1424     }
1425
1426     /* There must be a surface, otherwise there's a bug */
1427     assert(Prev != NULL);
1428
1429     /* Unchain the surface */
1430     Prev->next_attached = Surf->next_attached;
1431     Surf->next_attached = NULL;
1432     Surf->first_attached = Surf;
1433
1434     /* Check if the WineD3D depth stencil needs updating */
1435     if(This->ddraw->d3ddevice)
1436     {
1437         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1438     }
1439
1440     ddraw_surface7_Release(Attach);
1441     LeaveCriticalSection(&ddraw_cs);
1442     return DD_OK;
1443 }
1444
1445 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1446         DWORD flags, IDirectDrawSurface4 *attachment)
1447 {
1448     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1449     IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1450     TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1451
1452     return ddraw_surface7_DeleteAttachedSurface(&This->IDirectDrawSurface7_iface, flags,
1453             attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1454 }
1455
1456 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1457         DWORD flags, IDirectDrawSurface3 *attachment)
1458 {
1459     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1460     IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1461     TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1462
1463     return ddraw_surface7_DeleteAttachedSurface(&This->IDirectDrawSurface7_iface, flags,
1464             attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1465 }
1466
1467 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
1468         DWORD flags, IDirectDrawSurface2 *attachment)
1469 {
1470     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1471     IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1472     TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1473
1474     return ddraw_surface7_DeleteAttachedSurface(&This->IDirectDrawSurface7_iface, flags,
1475             attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1476 }
1477
1478 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
1479         DWORD flags, IDirectDrawSurface *attachment)
1480 {
1481     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1482     IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1483     TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1484
1485     return ddraw_surface7_DeleteAttachedSurface(&This->IDirectDrawSurface7_iface, flags,
1486             attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1487 }
1488
1489 /*****************************************************************************
1490  * IDirectDrawSurface7::AddOverlayDirtyRect
1491  *
1492  * "This method is not currently implemented"
1493  *
1494  * Params:
1495  *  Rect: ?
1496  *
1497  * Returns:
1498  *  DDERR_UNSUPPORTED
1499  *
1500  *****************************************************************************/
1501 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1502 {
1503     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1504
1505     return DDERR_UNSUPPORTED; /* unchecked */
1506 }
1507
1508 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
1509 {
1510     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1511     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1512
1513     return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1514 }
1515
1516 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1517 {
1518     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1519     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1520
1521     return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1522 }
1523
1524 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
1525 {
1526     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1527     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1528
1529     return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1530 }
1531
1532 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
1533 {
1534     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1535     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1536
1537     return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1538 }
1539
1540 /*****************************************************************************
1541  * IDirectDrawSurface7::GetDC
1542  *
1543  * Returns a GDI device context for the surface
1544  *
1545  * Params:
1546  *  hdc: Address of a HDC variable to store the dc to
1547  *
1548  * Returns:
1549  *  DD_OK on success
1550  *  DDERR_INVALIDPARAMS if hdc is NULL
1551  *  For details, see IWineD3DSurface::GetDC
1552  *
1553  *****************************************************************************/
1554 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1555 {
1556     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1557     HRESULT hr;
1558
1559     TRACE("iface %p, dc %p.\n", iface, hdc);
1560
1561     if(!hdc)
1562         return DDERR_INVALIDPARAMS;
1563
1564     EnterCriticalSection(&ddraw_cs);
1565     hr = wined3d_surface_getdc(This->wined3d_surface, hdc);
1566     LeaveCriticalSection(&ddraw_cs);
1567     switch(hr)
1568     {
1569         /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1570          * touch *hdc
1571          */
1572         case WINED3DERR_INVALIDCALL:
1573             if(hdc) *hdc = NULL;
1574             return DDERR_INVALIDPARAMS;
1575
1576         default: return hr;
1577     }
1578 }
1579
1580 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
1581 {
1582     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1583     TRACE("iface %p, dc %p.\n", iface, dc);
1584
1585     return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1586 }
1587
1588 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1589 {
1590     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1591     TRACE("iface %p, dc %p.\n", iface, dc);
1592
1593     return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1594 }
1595
1596 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
1597 {
1598     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1599     TRACE("iface %p, dc %p.\n", iface, dc);
1600
1601     return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1602 }
1603
1604 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
1605 {
1606     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1607     TRACE("iface %p, dc %p.\n", iface, dc);
1608
1609     return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1610 }
1611
1612 /*****************************************************************************
1613  * IDirectDrawSurface7::ReleaseDC
1614  *
1615  * Releases the DC that was constructed with GetDC
1616  *
1617  * Params:
1618  *  hdc: HDC to release
1619  *
1620  * Returns:
1621  *  DD_OK on success
1622  *  For more details, see IWineD3DSurface::ReleaseDC
1623  *
1624  *****************************************************************************/
1625 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1626 {
1627     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1628     HRESULT hr;
1629
1630     TRACE("iface %p, dc %p.\n", iface, hdc);
1631
1632     EnterCriticalSection(&ddraw_cs);
1633     hr = wined3d_surface_releasedc(This->wined3d_surface, hdc);
1634     LeaveCriticalSection(&ddraw_cs);
1635     return hr;
1636 }
1637
1638 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
1639 {
1640     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1641     TRACE("iface %p, dc %p.\n", iface, dc);
1642
1643     return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1644 }
1645
1646 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1647 {
1648     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1649     TRACE("iface %p, dc %p.\n", iface, dc);
1650
1651     return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1652 }
1653
1654 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
1655 {
1656     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1657     TRACE("iface %p, dc %p.\n", iface, dc);
1658
1659     return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1660 }
1661
1662 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
1663 {
1664     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1665     TRACE("iface %p, dc %p.\n", iface, dc);
1666
1667     return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1668 }
1669
1670 /*****************************************************************************
1671  * IDirectDrawSurface7::GetCaps
1672  *
1673  * Returns the surface's caps
1674  *
1675  * Params:
1676  *  Caps: Address to write the caps to
1677  *
1678  * Returns:
1679  *  DD_OK on success
1680  *  DDERR_INVALIDPARAMS if Caps is NULL
1681  *
1682  *****************************************************************************/
1683 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1684 {
1685     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1686
1687     TRACE("iface %p, caps %p.\n", iface, Caps);
1688
1689     if(!Caps)
1690         return DDERR_INVALIDPARAMS;
1691
1692     *Caps = This->surface_desc.ddsCaps;
1693     return DD_OK;
1694 }
1695
1696 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
1697 {
1698     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1699     TRACE("iface %p, caps %p.\n", iface, caps);
1700
1701     return ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, caps);
1702 }
1703
1704 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1705 {
1706     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1707     DDSCAPS2 caps2;
1708     HRESULT hr;
1709
1710     TRACE("iface %p, caps %p.\n", iface, caps);
1711
1712     hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1713     if (FAILED(hr)) return hr;
1714
1715     caps->dwCaps = caps2.dwCaps;
1716     return hr;
1717 }
1718
1719 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
1720 {
1721     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1722     DDSCAPS2 caps2;
1723     HRESULT hr;
1724
1725     TRACE("iface %p, caps %p.\n", iface, caps);
1726
1727     hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1728     if (FAILED(hr)) return hr;
1729
1730     caps->dwCaps = caps2.dwCaps;
1731     return hr;
1732 }
1733
1734 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
1735 {
1736     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1737     DDSCAPS2 caps2;
1738     HRESULT hr;
1739
1740     TRACE("iface %p, caps %p.\n", iface, caps);
1741
1742     hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1743     if (FAILED(hr)) return hr;
1744
1745     caps->dwCaps = caps2.dwCaps;
1746     return hr;
1747 }
1748
1749 /*****************************************************************************
1750  * IDirectDrawSurface7::SetPriority
1751  *
1752  * Sets a texture priority for managed textures.
1753  *
1754  * Params:
1755  *  Priority: The new priority
1756  *
1757  * Returns:
1758  *  DD_OK on success
1759  *  For more details, see IWineD3DSurface::SetPriority
1760  *
1761  *****************************************************************************/
1762 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1763 {
1764     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1765     HRESULT hr;
1766
1767     TRACE("iface %p, priority %u.\n", iface, Priority);
1768
1769     EnterCriticalSection(&ddraw_cs);
1770     hr = wined3d_surface_set_priority(This->wined3d_surface, Priority);
1771     LeaveCriticalSection(&ddraw_cs);
1772     return hr;
1773 }
1774
1775 /*****************************************************************************
1776  * IDirectDrawSurface7::GetPriority
1777  *
1778  * Returns the surface's priority
1779  *
1780  * Params:
1781  *  Priority: Address of a variable to write the priority to
1782  *
1783  * Returns:
1784  *  D3D_OK on success
1785  *  DDERR_INVALIDPARAMS if Priority == NULL
1786  *  For more details, see IWineD3DSurface::GetPriority
1787  *
1788  *****************************************************************************/
1789 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
1790 {
1791     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1792
1793     TRACE("iface %p, priority %p.\n", iface, Priority);
1794
1795     if(!Priority)
1796     {
1797         return DDERR_INVALIDPARAMS;
1798     }
1799
1800     EnterCriticalSection(&ddraw_cs);
1801     *Priority = wined3d_surface_get_priority(This->wined3d_surface);
1802     LeaveCriticalSection(&ddraw_cs);
1803     return DD_OK;
1804 }
1805
1806 /*****************************************************************************
1807  * IDirectDrawSurface7::SetPrivateData
1808  *
1809  * Stores some data in the surface that is intended for the application's
1810  * use.
1811  *
1812  * Params:
1813  *  tag: GUID that identifies the data
1814  *  Data: Pointer to the private data
1815  *  Size: Size of the private data
1816  *  Flags: Some flags
1817  *
1818  * Returns:
1819  *  D3D_OK on success
1820  *  For more details, see IWineD3DSurface::SetPrivateData
1821  *
1822  *****************************************************************************/
1823 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
1824         REFGUID tag, void *Data, DWORD Size, DWORD Flags)
1825 {
1826     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1827     HRESULT hr;
1828
1829     TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
1830             iface, debugstr_guid(tag), Data, Size, Flags);
1831
1832     EnterCriticalSection(&ddraw_cs);
1833     hr = wined3d_surface_set_private_data(This->wined3d_surface, tag, Data, Size, Flags);
1834     LeaveCriticalSection(&ddraw_cs);
1835     switch(hr)
1836     {
1837         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1838         default:                            return hr;
1839     }
1840 }
1841
1842 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
1843         REFGUID tag, void *data, DWORD size, DWORD flags)
1844 {
1845     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1846     TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
1847                 iface, debugstr_guid(tag), data, size, flags);
1848
1849     return ddraw_surface7_SetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size, flags);
1850 }
1851
1852 /*****************************************************************************
1853  * IDirectDrawSurface7::GetPrivateData
1854  *
1855  * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1856  *
1857  * Params:
1858  *  tag: GUID of the data to return
1859  *  Data: Address where to write the data to
1860  *  Size: Size of the buffer at Data
1861  *
1862  * Returns:
1863  *  DD_OK on success
1864  *  DDERR_INVALIDPARAMS if Data is NULL
1865  *  For more details, see IWineD3DSurface::GetPrivateData
1866  *
1867  *****************************************************************************/
1868 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
1869 {
1870     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1871     HRESULT hr;
1872
1873     TRACE("iface %p, tag %s, data %p, data_size %p.\n",
1874             iface, debugstr_guid(tag), Data, Size);
1875
1876     if(!Data)
1877         return DDERR_INVALIDPARAMS;
1878
1879     EnterCriticalSection(&ddraw_cs);
1880     hr = wined3d_surface_get_private_data(This->wined3d_surface, tag, Data, Size);
1881     LeaveCriticalSection(&ddraw_cs);
1882     return hr;
1883 }
1884
1885 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
1886 {
1887     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1888     TRACE("iface %p, tag %s, data %p, data_size %p.\n",
1889                 iface, debugstr_guid(tag), data, size);
1890
1891     return ddraw_surface7_GetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size);
1892 }
1893
1894 /*****************************************************************************
1895  * IDirectDrawSurface7::FreePrivateData
1896  *
1897  * Frees private data stored in the surface
1898  *
1899  * Params:
1900  *  tag: Tag of the data to free
1901  *
1902  * Returns:
1903  *  D3D_OK on success
1904  *  For more details, see IWineD3DSurface::FreePrivateData
1905  *
1906  *****************************************************************************/
1907 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
1908 {
1909     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1910     HRESULT hr;
1911
1912     TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
1913
1914     EnterCriticalSection(&ddraw_cs);
1915     hr = wined3d_surface_free_private_data(This->wined3d_surface, tag);
1916     LeaveCriticalSection(&ddraw_cs);
1917     return hr;
1918 }
1919
1920 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
1921 {
1922     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1923     TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
1924
1925     return ddraw_surface7_FreePrivateData(&This->IDirectDrawSurface7_iface, tag);
1926 }
1927
1928 /*****************************************************************************
1929  * IDirectDrawSurface7::PageLock
1930  *
1931  * Prevents a sysmem surface from being paged out
1932  *
1933  * Params:
1934  *  Flags: Not used, must be 0(unchecked)
1935  *
1936  * Returns:
1937  *  DD_OK, because it's a stub
1938  *
1939  *****************************************************************************/
1940 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
1941 {
1942     TRACE("iface %p, flags %#x.\n", iface, Flags);
1943
1944     /* This is Windows memory management related - we don't need this */
1945     return DD_OK;
1946 }
1947
1948 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
1949 {
1950     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1951     TRACE("iface %p, flags %#x.\n", iface, flags);
1952
1953     return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
1954 }
1955
1956 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
1957 {
1958     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1959     TRACE("iface %p, flags %#x.\n", iface, flags);
1960
1961     return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
1962 }
1963
1964 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
1965 {
1966     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1967     TRACE("iface %p, flags %#x.\n", iface, flags);
1968
1969     return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
1970 }
1971
1972 /*****************************************************************************
1973  * IDirectDrawSurface7::PageUnlock
1974  *
1975  * Allows a sysmem surface to be paged out
1976  *
1977  * Params:
1978  *  Flags: Not used, must be 0(unchecked)
1979  *
1980  * Returns:
1981  *  DD_OK, because it's a stub
1982  *
1983  *****************************************************************************/
1984 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
1985 {
1986     TRACE("iface %p, flags %#x.\n", iface, Flags);
1987
1988     return DD_OK;
1989 }
1990
1991 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
1992 {
1993     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1994     TRACE("iface %p, flags %#x.\n", iface, flags);
1995
1996     return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
1997 }
1998
1999 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2000 {
2001     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2002     TRACE("iface %p, flags %#x.\n", iface, flags);
2003
2004     return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2005 }
2006
2007 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2008 {
2009     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2010     TRACE("iface %p, flags %#x.\n", iface, flags);
2011
2012     return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2013 }
2014
2015 /*****************************************************************************
2016  * IDirectDrawSurface7::BltBatch
2017  *
2018  * An unimplemented function
2019  *
2020  * Params:
2021  *  ?
2022  *
2023  * Returns:
2024  *  DDERR_UNSUPPORTED
2025  *
2026  *****************************************************************************/
2027 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2028 {
2029     TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2030
2031     /* MSDN: "not currently implemented" */
2032     return DDERR_UNSUPPORTED;
2033 }
2034
2035 static HRESULT WINAPI ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2036 {
2037     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2038     TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2039
2040     return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2041 }
2042
2043 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2044 {
2045     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2046     TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2047
2048     return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2049 }
2050
2051 static HRESULT WINAPI ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2052 {
2053     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2054     TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2055
2056     return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2057 }
2058
2059 static HRESULT WINAPI ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2060 {
2061     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2062     TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2063
2064     return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2065 }
2066
2067 /*****************************************************************************
2068  * IDirectDrawSurface7::EnumAttachedSurfaces
2069  *
2070  * Enumerates all surfaces attached to this surface
2071  *
2072  * Params:
2073  *  context: Pointer to pass unmodified to the callback
2074  *  cb: Callback function to call for each surface
2075  *
2076  * Returns:
2077  *  DD_OK on success
2078  *  DDERR_INVALIDPARAMS if cb is NULL
2079  *
2080  *****************************************************************************/
2081 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2082         void *context, LPDDENUMSURFACESCALLBACK7 cb)
2083 {
2084     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2085     IDirectDrawSurfaceImpl *surf;
2086     DDSURFACEDESC2 desc;
2087     int i;
2088
2089     /* Attached surfaces aren't handled in WineD3D */
2090     TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2091
2092     if(!cb)
2093         return DDERR_INVALIDPARAMS;
2094
2095     EnterCriticalSection(&ddraw_cs);
2096     for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2097     {
2098         surf = This->complex_array[i];
2099         if(!surf) break;
2100
2101         ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2102         desc = surf->surface_desc;
2103         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2104         if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2105         {
2106             LeaveCriticalSection(&ddraw_cs);
2107             return DD_OK;
2108         }
2109     }
2110
2111     for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
2112     {
2113         ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2114         desc = surf->surface_desc;
2115         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2116         if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2117         {
2118             LeaveCriticalSection(&ddraw_cs);
2119             return DD_OK;
2120         }
2121     }
2122
2123     TRACE(" end of enumeration.\n");
2124
2125     LeaveCriticalSection(&ddraw_cs);
2126     return DD_OK;
2127 }
2128
2129 struct callback_info2
2130 {
2131     LPDDENUMSURFACESCALLBACK2 callback;
2132     void *context;
2133 };
2134
2135 struct callback_info
2136 {
2137     LPDDENUMSURFACESCALLBACK callback;
2138     void *context;
2139 };
2140
2141 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2142 {
2143     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
2144     const struct callback_info2 *info = context;
2145
2146     return info->callback(&This->IDirectDrawSurface4_iface, surface_desc, info->context);
2147 }
2148
2149 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2150 {
2151     IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
2152     const struct callback_info *info = context;
2153
2154     return info->callback(&surface_impl->IDirectDrawSurface_iface,
2155             (DDSURFACEDESC *)surface_desc, info->context);
2156 }
2157
2158 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2159         void *context, LPDDENUMSURFACESCALLBACK2 callback)
2160 {
2161     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2162     struct callback_info2 info;
2163
2164     TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2165
2166     info.callback = callback;
2167     info.context  = context;
2168
2169     return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2170             &info, EnumCallback2);
2171 }
2172
2173 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2174         void *context, LPDDENUMSURFACESCALLBACK callback)
2175 {
2176     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2177     struct callback_info info;
2178
2179     TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2180
2181     info.callback = callback;
2182     info.context  = context;
2183
2184     return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2185             &info, EnumCallback);
2186 }
2187
2188 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2189         void *context, LPDDENUMSURFACESCALLBACK callback)
2190 {
2191     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2192     struct callback_info info;
2193
2194     TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2195
2196     info.callback = callback;
2197     info.context  = context;
2198
2199     return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2200             &info, EnumCallback);
2201 }
2202
2203 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2204         void *context, LPDDENUMSURFACESCALLBACK callback)
2205 {
2206     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2207     struct callback_info info;
2208
2209     TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2210
2211     info.callback = callback;
2212     info.context  = context;
2213
2214     return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2215             &info, EnumCallback);
2216 }
2217
2218 /*****************************************************************************
2219  * IDirectDrawSurface7::EnumOverlayZOrders
2220  *
2221  * "Enumerates the overlay surfaces on the specified destination"
2222  *
2223  * Params:
2224  *  Flags: DDENUMOVERLAYZ_BACKTOFRONT  or DDENUMOVERLAYZ_FRONTTOBACK
2225  *  context: context to pass back to the callback
2226  *  cb: callback function to call for each enumerated surface
2227  *
2228  * Returns:
2229  *  DD_OK, because it's a stub
2230  *
2231  *****************************************************************************/
2232 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2233         DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2234 {
2235     FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2236
2237     return DD_OK;
2238 }
2239
2240 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2241         DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2242 {
2243     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2244     struct callback_info2 info;
2245
2246     TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2247
2248     info.callback = callback;
2249     info.context  = context;
2250
2251     return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2252             flags, &info, EnumCallback2);
2253 }
2254
2255 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2256         DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2257 {
2258     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2259     struct callback_info info;
2260
2261     TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2262
2263     info.callback = callback;
2264     info.context  = context;
2265
2266     return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2267             flags, &info, EnumCallback);
2268 }
2269
2270 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2271         DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2272 {
2273     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2274     struct callback_info info;
2275
2276     TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2277
2278     info.callback = callback;
2279     info.context  = context;
2280
2281     return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2282             flags, &info, EnumCallback);
2283 }
2284
2285 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2286         DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2287 {
2288     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2289     struct callback_info info;
2290
2291     TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2292
2293     info.callback = callback;
2294     info.context  = context;
2295
2296     return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2297             flags, &info, EnumCallback);
2298 }
2299
2300 /*****************************************************************************
2301  * IDirectDrawSurface7::GetBltStatus
2302  *
2303  * Returns the blitting status
2304  *
2305  * Params:
2306  *  Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2307  *
2308  * Returns:
2309  *  See IWineD3DSurface::Blt
2310  *
2311  *****************************************************************************/
2312 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2313 {
2314     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2315     HRESULT hr;
2316
2317     TRACE("iface %p, flags %#x.\n", iface, Flags);
2318
2319     EnterCriticalSection(&ddraw_cs);
2320     hr = wined3d_surface_get_blt_status(This->wined3d_surface, Flags);
2321     LeaveCriticalSection(&ddraw_cs);
2322     switch(hr)
2323     {
2324         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2325         default:                            return hr;
2326     }
2327 }
2328
2329 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2330 {
2331     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2332     TRACE("iface %p, flags %#x.\n", iface, flags);
2333
2334     return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2335 }
2336
2337 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2338 {
2339     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2340     TRACE("iface %p, flags %#x.\n", iface, flags);
2341
2342     return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2343 }
2344
2345 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2346 {
2347     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2348     TRACE("iface %p, flags %#x.\n", iface, flags);
2349
2350     return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2351 }
2352
2353 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
2354 {
2355     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2356     TRACE("iface %p, flags %#x.\n", iface, flags);
2357
2358     return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2359 }
2360
2361 /*****************************************************************************
2362  * IDirectDrawSurface7::GetColorKey
2363  *
2364  * Returns the color key assigned to the surface
2365  *
2366  * Params:
2367  *  Flags: Some flags
2368  *  CKey: Address to store the key to
2369  *
2370  * Returns:
2371  *  DD_OK on success
2372  *  DDERR_INVALIDPARAMS if CKey is NULL
2373  *
2374  *****************************************************************************/
2375 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2376 {
2377     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2378
2379     TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2380
2381     if(!CKey)
2382         return DDERR_INVALIDPARAMS;
2383
2384     EnterCriticalSection(&ddraw_cs);
2385
2386     switch (Flags)
2387     {
2388     case DDCKEY_DESTBLT:
2389         if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
2390         {
2391             LeaveCriticalSection(&ddraw_cs);
2392             return DDERR_NOCOLORKEY;
2393         }
2394         *CKey = This->surface_desc.ddckCKDestBlt;
2395         break;
2396
2397     case DDCKEY_DESTOVERLAY:
2398         if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
2399             {
2400             LeaveCriticalSection(&ddraw_cs);
2401             return DDERR_NOCOLORKEY;
2402             }
2403         *CKey = This->surface_desc.u3.ddckCKDestOverlay;
2404         break;
2405
2406     case DDCKEY_SRCBLT:
2407         if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
2408         {
2409             LeaveCriticalSection(&ddraw_cs);
2410             return DDERR_NOCOLORKEY;
2411         }
2412         *CKey = This->surface_desc.ddckCKSrcBlt;
2413         break;
2414
2415     case DDCKEY_SRCOVERLAY:
2416         if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
2417         {
2418             LeaveCriticalSection(&ddraw_cs);
2419             return DDERR_NOCOLORKEY;
2420         }
2421         *CKey = This->surface_desc.ddckCKSrcOverlay;
2422         break;
2423
2424     default:
2425         LeaveCriticalSection(&ddraw_cs);
2426         return DDERR_INVALIDPARAMS;
2427     }
2428
2429     LeaveCriticalSection(&ddraw_cs);
2430     return DD_OK;
2431 }
2432
2433 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
2434 {
2435     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2436     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2437
2438     return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2439 }
2440
2441 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2442 {
2443     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2444     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2445
2446     return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2447 }
2448
2449 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
2450 {
2451     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2452     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2453
2454     return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2455 }
2456
2457 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
2458 {
2459     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2460     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2461
2462     return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2463 }
2464
2465 /*****************************************************************************
2466  * IDirectDrawSurface7::GetFlipStatus
2467  *
2468  * Returns the flipping status of the surface
2469  *
2470  * Params:
2471  *  Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
2472  *
2473  * Returns:
2474  *  See IWineD3DSurface::GetFlipStatus
2475  *
2476  *****************************************************************************/
2477 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2478 {
2479     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2480     HRESULT hr;
2481
2482     TRACE("iface %p, flags %#x.\n", iface, Flags);
2483
2484     EnterCriticalSection(&ddraw_cs);
2485     hr = wined3d_surface_get_flip_status(This->wined3d_surface, Flags);
2486     LeaveCriticalSection(&ddraw_cs);
2487     switch(hr)
2488     {
2489         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2490         default:                            return hr;
2491     }
2492 }
2493
2494 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
2495 {
2496     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2497     TRACE("iface %p, flags %#x.\n", iface, flags);
2498
2499     return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2500 }
2501
2502 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
2503 {
2504     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2505     TRACE("iface %p, flags %#x.\n", iface, flags);
2506
2507     return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2508 }
2509
2510 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
2511 {
2512     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2513     TRACE("iface %p, flags %#x.\n", iface, flags);
2514
2515     return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2516 }
2517
2518 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
2519 {
2520     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2521     TRACE("iface %p, flags %#x.\n", iface, flags);
2522
2523     return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2524 }
2525
2526 /*****************************************************************************
2527  * IDirectDrawSurface7::GetOverlayPosition
2528  *
2529  * Returns the display coordinates of a visible and active overlay surface
2530  *
2531  * Params:
2532  *  X
2533  *  Y
2534  *
2535  * Returns:
2536  *  DDERR_NOTAOVERLAYSURFACE, because it's a stub
2537  *****************************************************************************/
2538 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
2539 {
2540     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2541     HRESULT hr;
2542
2543     TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
2544
2545     EnterCriticalSection(&ddraw_cs);
2546     hr = wined3d_surface_get_overlay_position(This->wined3d_surface, X, Y);
2547     LeaveCriticalSection(&ddraw_cs);
2548     return hr;
2549 }
2550
2551 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
2552 {
2553     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2554     TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2555
2556     return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2557 }
2558
2559 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
2560 {
2561     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2562     TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2563
2564     return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2565 }
2566
2567 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
2568 {
2569     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2570     TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2571
2572     return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2573 }
2574
2575 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
2576 {
2577     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2578     TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2579
2580     return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2581 }
2582
2583 /*****************************************************************************
2584  * IDirectDrawSurface7::GetPixelFormat
2585  *
2586  * Returns the pixel format of the Surface
2587  *
2588  * Params:
2589  *  PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
2590  *               format should be written
2591  *
2592  * Returns:
2593  *  DD_OK on success
2594  *  DDERR_INVALIDPARAMS if PixelFormat is NULL
2595  *
2596  *****************************************************************************/
2597 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
2598 {
2599     /* What is DDERR_INVALIDSURFACETYPE for here? */
2600     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2601
2602     TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
2603
2604     if(!PixelFormat)
2605         return DDERR_INVALIDPARAMS;
2606
2607     EnterCriticalSection(&ddraw_cs);
2608     DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
2609     LeaveCriticalSection(&ddraw_cs);
2610
2611     return DD_OK;
2612 }
2613
2614 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
2615 {
2616     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2617     TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2618
2619     return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2620 }
2621
2622 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
2623 {
2624     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2625     TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2626
2627     return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2628 }
2629
2630 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
2631 {
2632     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2633     TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2634
2635     return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2636 }
2637
2638 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
2639 {
2640     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2641     TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2642
2643     return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2644 }
2645
2646 /*****************************************************************************
2647  * IDirectDrawSurface7::GetSurfaceDesc
2648  *
2649  * Returns the description of this surface
2650  *
2651  * Params:
2652  *  DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
2653  *        surface desc
2654  *
2655  * Returns:
2656  *  DD_OK on success
2657  *  DDERR_INVALIDPARAMS if DDSD is NULL
2658  *
2659  *****************************************************************************/
2660 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
2661 {
2662     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2663
2664     TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2665
2666     if(!DDSD)
2667         return DDERR_INVALIDPARAMS;
2668
2669     if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
2670     {
2671         WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
2672         return DDERR_INVALIDPARAMS;
2673     }
2674
2675     EnterCriticalSection(&ddraw_cs);
2676     DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
2677     TRACE("Returning surface desc:\n");
2678     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
2679
2680     LeaveCriticalSection(&ddraw_cs);
2681     return DD_OK;
2682 }
2683
2684 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
2685 {
2686     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2687     TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2688
2689     return ddraw_surface7_GetSurfaceDesc(&This->IDirectDrawSurface7_iface, DDSD);
2690 }
2691
2692 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
2693 {
2694     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2695
2696     TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
2697
2698     if (!surface_desc) return DDERR_INVALIDPARAMS;
2699
2700     if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
2701     {
2702         WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
2703         return DDERR_INVALIDPARAMS;
2704     }
2705
2706     EnterCriticalSection(&ddraw_cs);
2707     DD_STRUCT_COPY_BYSIZE(surface_desc, (DDSURFACEDESC *)&This->surface_desc);
2708     TRACE("Returning surface desc:\n");
2709     if (TRACE_ON(ddraw))
2710     {
2711         /* DDRAW_dump_surface_desc handles the smaller size */
2712         DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2713     }
2714
2715     LeaveCriticalSection(&ddraw_cs);
2716     return DD_OK;
2717 }
2718
2719 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
2720 {
2721     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2722     TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2723
2724     return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
2725 }
2726
2727 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
2728 {
2729     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2730     TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2731
2732     return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
2733 }
2734
2735 /*****************************************************************************
2736  * IDirectDrawSurface7::Initialize
2737  *
2738  * Initializes the surface. This is a no-op in Wine
2739  *
2740  * Params:
2741  *  DD: Pointer to an DirectDraw interface
2742  *  DDSD: Surface description for initialization
2743  *
2744  * Returns:
2745  *  DDERR_ALREADYINITIALIZED
2746  *
2747  *****************************************************************************/
2748 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
2749         IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
2750 {
2751     TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2752
2753     return DDERR_ALREADYINITIALIZED;
2754 }
2755
2756 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
2757         IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
2758 {
2759     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2760     TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2761
2762     return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2763             ddraw, surface_desc);
2764 }
2765
2766 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
2767         IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2768 {
2769     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2770     TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2771
2772     return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2773             ddraw, (DDSURFACEDESC2 *)surface_desc);
2774 }
2775
2776 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
2777         IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2778 {
2779     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2780     TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2781
2782     return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2783             ddraw, (DDSURFACEDESC2 *)surface_desc);
2784 }
2785
2786 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
2787         IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2788 {
2789     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2790     TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2791
2792     return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2793             ddraw, (DDSURFACEDESC2 *)surface_desc);
2794 }
2795
2796 /*****************************************************************************
2797  * IDirect3DTexture1::Initialize
2798  *
2799  * The sdk says it's not implemented
2800  *
2801  * Params:
2802  *  ?
2803  *
2804  * Returns
2805  *  DDERR_UNSUPPORTED
2806  *
2807  *****************************************************************************/
2808 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
2809         IDirect3DDevice *device, IDirectDrawSurface *surface)
2810 {
2811     TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
2812
2813     return DDERR_UNSUPPORTED; /* Unchecked */
2814 }
2815
2816 /*****************************************************************************
2817  * IDirectDrawSurface7::IsLost
2818  *
2819  * Checks if the surface is lost
2820  *
2821  * Returns:
2822  *  DD_OK, if the surface is usable
2823  *  DDERR_ISLOST if the surface is lost
2824  *  See IWineD3DSurface::IsLost for more details
2825  *
2826  *****************************************************************************/
2827 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
2828 {
2829     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2830     HRESULT hr;
2831
2832     TRACE("iface %p.\n", iface);
2833
2834     EnterCriticalSection(&ddraw_cs);
2835     /* We lose the surface if the implementation was changed */
2836     if(This->ImplType != This->ddraw->ImplType)
2837     {
2838         /* But this shouldn't happen. When we change the implementation,
2839          * all surfaces are re-created automatically, and their content
2840          * is copied
2841          */
2842         ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
2843         LeaveCriticalSection(&ddraw_cs);
2844         return DDERR_SURFACELOST;
2845     }
2846
2847     hr = wined3d_surface_is_lost(This->wined3d_surface);
2848     LeaveCriticalSection(&ddraw_cs);
2849     switch(hr)
2850     {
2851         /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
2852          * WineD3D uses the same error for surfaces
2853          */
2854         case WINED3DERR_DEVICELOST:         return DDERR_SURFACELOST;
2855         default:                            return hr;
2856     }
2857 }
2858
2859 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
2860 {
2861     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2862     TRACE("iface %p.\n", iface);
2863
2864     return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
2865 }
2866
2867 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
2868 {
2869     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2870     TRACE("iface %p.\n", iface);
2871
2872     return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
2873 }
2874
2875 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
2876 {
2877     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2878     TRACE("iface %p.\n", iface);
2879
2880     return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
2881 }
2882
2883 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
2884 {
2885     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2886     TRACE("iface %p.\n", iface);
2887
2888     return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
2889 }
2890
2891 /*****************************************************************************
2892  * IDirectDrawSurface7::Restore
2893  *
2894  * Restores a lost surface. This makes the surface usable again, but
2895  * doesn't reload its old contents
2896  *
2897  * Returns:
2898  *  DD_OK on success
2899  *  See IWineD3DSurface::Restore for more details
2900  *
2901  *****************************************************************************/
2902 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
2903 {
2904     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2905     HRESULT hr;
2906
2907     TRACE("iface %p.\n", iface);
2908
2909     EnterCriticalSection(&ddraw_cs);
2910     if(This->ImplType != This->ddraw->ImplType)
2911     {
2912         /* Call the recreation callback. Make sure to AddRef first */
2913         IDirectDrawSurface_AddRef(iface);
2914         ddraw_recreate_surfaces_cb(iface, &This->surface_desc, NULL /* Not needed */);
2915     }
2916     hr = wined3d_surface_restore(This->wined3d_surface);
2917     LeaveCriticalSection(&ddraw_cs);
2918     return hr;
2919 }
2920
2921 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
2922 {
2923     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2924     TRACE("iface %p.\n", iface);
2925
2926     return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
2927 }
2928
2929 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
2930 {
2931     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2932     TRACE("iface %p.\n", iface);
2933
2934     return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
2935 }
2936
2937 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
2938 {
2939     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2940     TRACE("iface %p.\n", iface);
2941
2942     return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
2943 }
2944
2945 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
2946 {
2947     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2948     TRACE("iface %p.\n", iface);
2949
2950     return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
2951 }
2952
2953 /*****************************************************************************
2954  * IDirectDrawSurface7::SetOverlayPosition
2955  *
2956  * Changes the display coordinates of an overlay surface
2957  *
2958  * Params:
2959  *  X:
2960  *  Y:
2961  *
2962  * Returns:
2963  *   DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
2964  *****************************************************************************/
2965 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
2966 {
2967     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2968     HRESULT hr;
2969
2970     TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
2971
2972     EnterCriticalSection(&ddraw_cs);
2973     hr = wined3d_surface_set_overlay_position(This->wined3d_surface, X, Y);
2974     LeaveCriticalSection(&ddraw_cs);
2975     return hr;
2976 }
2977
2978 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
2979 {
2980     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2981     TRACE("iface %p, x %d, y %d.\n", iface, x, y);
2982
2983     return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2984 }
2985
2986 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
2987 {
2988     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2989     TRACE("iface %p, x %d, y %d.\n", iface, x, y);
2990
2991     return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2992 }
2993
2994 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
2995 {
2996     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2997     TRACE("iface %p, x %d, y %d.\n", iface, x, y);
2998
2999     return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3000 }
3001
3002 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3003 {
3004     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3005     TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3006
3007     return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3008 }
3009
3010 /*****************************************************************************
3011  * IDirectDrawSurface7::UpdateOverlay
3012  *
3013  * Modifies the attributes of an overlay surface.
3014  *
3015  * Params:
3016  *  SrcRect: The section of the source being used for the overlay
3017  *  DstSurface: Address of the surface that is overlaid
3018  *  DstRect: Place of the overlay
3019  *  Flags: some DDOVER_* flags
3020  *
3021  * Returns:
3022  *  DDERR_UNSUPPORTED, because we don't support overlays
3023  *
3024  *****************************************************************************/
3025 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
3026         IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
3027 {
3028     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3029     IDirectDrawSurfaceImpl *Dst = unsafe_impl_from_IDirectDrawSurface7(DstSurface);
3030     HRESULT hr;
3031
3032     TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3033             iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
3034
3035     EnterCriticalSection(&ddraw_cs);
3036     hr = wined3d_surface_update_overlay(This->wined3d_surface, SrcRect,
3037             Dst ? Dst->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
3038     LeaveCriticalSection(&ddraw_cs);
3039     switch(hr) {
3040         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
3041         case WINEDDERR_NOTAOVERLAYSURFACE:  return DDERR_NOTAOVERLAYSURFACE;
3042         case WINEDDERR_OVERLAYNOTVISIBLE:   return DDERR_OVERLAYNOTVISIBLE;
3043         default:
3044             return hr;
3045     }
3046 }
3047
3048 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3049         IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3050 {
3051     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3052     IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3053     TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3054             iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3055
3056     return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3057             dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3058 }
3059
3060 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3061         IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3062 {
3063     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3064     IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3065     TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3066             iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3067
3068     return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3069             dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3070 }
3071
3072 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3073         IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3074 {
3075     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3076     IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3077     TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3078             iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3079
3080     return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3081             dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3082 }
3083
3084 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3085         IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3086 {
3087     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3088     IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3089     TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3090             iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3091
3092     return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3093             dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3094 }
3095
3096 /*****************************************************************************
3097  * IDirectDrawSurface7::UpdateOverlayDisplay
3098  *
3099  * The DX7 sdk says that it's not implemented
3100  *
3101  * Params:
3102  *  Flags: ?
3103  *
3104  * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3105  *
3106  *****************************************************************************/
3107 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3108 {
3109     TRACE("iface %p, flags %#x.\n", iface, Flags);
3110
3111     return DDERR_UNSUPPORTED;
3112 }
3113
3114 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3115 {
3116     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3117     TRACE("iface %p, flags %#x.\n", iface, flags);
3118
3119     return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3120 }
3121
3122 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3123 {
3124     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3125     TRACE("iface %p, flags %#x.\n", iface, flags);
3126
3127     return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3128 }
3129
3130 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3131 {
3132     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3133     TRACE("iface %p, flags %#x.\n", iface, flags);
3134
3135     return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3136 }
3137
3138 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3139 {
3140     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3141     TRACE("iface %p, flags %#x.\n", iface, flags);
3142
3143     return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3144 }
3145
3146 /*****************************************************************************
3147  * IDirectDrawSurface7::UpdateOverlayZOrder
3148  *
3149  * Sets an overlay's Z order
3150  *
3151  * Params:
3152  *  Flags: DDOVERZ_* flags
3153  *  DDSRef: Defines the relative position in the overlay chain
3154  *
3155  * Returns:
3156  *  DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3157  *
3158  *****************************************************************************/
3159 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3160         DWORD Flags, IDirectDrawSurface7 *DDSRef)
3161 {
3162     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3163     IDirectDrawSurfaceImpl *Ref = unsafe_impl_from_IDirectDrawSurface7(DDSRef);
3164     HRESULT hr;
3165
3166     TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
3167
3168     EnterCriticalSection(&ddraw_cs);
3169     hr = wined3d_surface_update_overlay_z_order(This->wined3d_surface,
3170             Flags, Ref ? Ref->wined3d_surface : NULL);
3171     LeaveCriticalSection(&ddraw_cs);
3172     return hr;
3173 }
3174
3175 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3176         DWORD flags, IDirectDrawSurface4 *reference)
3177 {
3178     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3179     IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3180     TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3181
3182     return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3183             reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3184 }
3185
3186 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3187         DWORD flags, IDirectDrawSurface3 *reference)
3188 {
3189     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3190     IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3191     TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3192
3193     return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3194             reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3195 }
3196
3197 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3198         DWORD flags, IDirectDrawSurface2 *reference)
3199 {
3200     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3201     IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3202     TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3203
3204     return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3205             reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3206 }
3207
3208 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3209         DWORD flags, IDirectDrawSurface *reference)
3210 {
3211     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3212     IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3213     TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3214
3215     return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3216             reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3217 }
3218
3219 /*****************************************************************************
3220  * IDirectDrawSurface7::GetDDInterface
3221  *
3222  * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3223  * surface belongs to
3224  *
3225  * Params:
3226  *  DD: Address to write the interface pointer to
3227  *
3228  * Returns:
3229  *  DD_OK on success
3230  *  DDERR_INVALIDPARAMS if DD is NULL
3231  *
3232  *****************************************************************************/
3233 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3234 {
3235     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3236
3237     TRACE("iface %p, ddraw %p.\n", iface, DD);
3238
3239     if(!DD)
3240         return DDERR_INVALIDPARAMS;
3241
3242     switch(This->version)
3243     {
3244         case 7:
3245             *DD = &This->ddraw->IDirectDraw7_iface;
3246             break;
3247
3248         case 4:
3249             *DD = &This->ddraw->IDirectDraw4_iface;
3250             break;
3251
3252         case 2:
3253             *DD = &This->ddraw->IDirectDraw2_iface;
3254             break;
3255
3256         case 1:
3257             *DD = &This->ddraw->IDirectDraw_iface;
3258             break;
3259
3260     }
3261     IUnknown_AddRef((IUnknown *)*DD);
3262
3263     return DD_OK;
3264 }
3265
3266 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3267 {
3268     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3269     TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3270
3271     return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3272 }
3273
3274 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3275 {
3276     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3277     TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3278
3279     return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3280 }
3281
3282 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3283 {
3284     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3285     TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3286
3287     return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3288 }
3289
3290 /* This seems also windows implementation specific - I don't think WineD3D needs this */
3291 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3292 {
3293     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3294     volatile IDirectDrawSurfaceImpl* vThis = This;
3295
3296     TRACE("iface %p.\n", iface);
3297
3298     EnterCriticalSection(&ddraw_cs);
3299     /* A uniqueness value of 0 is apparently special.
3300      * This needs to be checked.
3301      * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
3302      */
3303     while (1) {
3304         DWORD old_uniqueness_value = vThis->uniqueness_value;
3305         DWORD new_uniqueness_value = old_uniqueness_value+1;
3306
3307         if (old_uniqueness_value == 0) break;
3308         if (new_uniqueness_value == 0) new_uniqueness_value = 1;
3309
3310         if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
3311                                       old_uniqueness_value,
3312                                       new_uniqueness_value)
3313             == old_uniqueness_value)
3314             break;
3315     }
3316
3317     LeaveCriticalSection(&ddraw_cs);
3318     return DD_OK;
3319 }
3320
3321 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
3322 {
3323     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3324     TRACE("iface %p.\n", iface);
3325
3326     return ddraw_surface7_ChangeUniquenessValue(&This->IDirectDrawSurface7_iface);
3327 }
3328
3329 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
3330 {
3331     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3332
3333     TRACE("iface %p, value %p.\n", iface, pValue);
3334
3335     EnterCriticalSection(&ddraw_cs);
3336     *pValue = This->uniqueness_value;
3337     LeaveCriticalSection(&ddraw_cs);
3338     return DD_OK;
3339 }
3340
3341 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
3342 {
3343     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3344     TRACE("iface %p, value %p.\n", iface, pValue);
3345
3346     return ddraw_surface7_GetUniquenessValue(&This->IDirectDrawSurface7_iface, pValue);
3347 }
3348
3349 /*****************************************************************************
3350  * IDirectDrawSurface7::SetLOD
3351  *
3352  * Sets the level of detail of a texture
3353  *
3354  * Params:
3355  *  MaxLOD: LOD to set
3356  *
3357  * Returns:
3358  *  DD_OK on success
3359  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
3360  *
3361  *****************************************************************************/
3362 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
3363 {
3364     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3365     HRESULT hr;
3366
3367     TRACE("iface %p, lod %u.\n", iface, MaxLOD);
3368
3369     EnterCriticalSection(&ddraw_cs);
3370     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3371     {
3372         LeaveCriticalSection(&ddraw_cs);
3373         return DDERR_INVALIDOBJECT;
3374     }
3375
3376     if (!This->wined3d_texture)
3377     {
3378         ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
3379         LeaveCriticalSection(&ddraw_cs);
3380         return DDERR_INVALIDOBJECT;
3381     }
3382
3383     hr = wined3d_texture_set_lod(This->wined3d_texture, MaxLOD);
3384     LeaveCriticalSection(&ddraw_cs);
3385     return hr;
3386 }
3387
3388 /*****************************************************************************
3389  * IDirectDrawSurface7::GetLOD
3390  *
3391  * Returns the level of detail of a Direct3D texture
3392  *
3393  * Params:
3394  *  MaxLOD: Address to write the LOD to
3395  *
3396  * Returns:
3397  *  DD_OK on success
3398  *  DDERR_INVALIDPARAMS if MaxLOD is NULL
3399  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
3400  *
3401  *****************************************************************************/
3402 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
3403 {
3404     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3405
3406     TRACE("iface %p, lod %p.\n", iface, MaxLOD);
3407
3408     if(!MaxLOD)
3409         return DDERR_INVALIDPARAMS;
3410
3411     EnterCriticalSection(&ddraw_cs);
3412     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3413     {
3414         LeaveCriticalSection(&ddraw_cs);
3415         return DDERR_INVALIDOBJECT;
3416     }
3417
3418     *MaxLOD = wined3d_texture_get_lod(This->wined3d_texture);
3419     LeaveCriticalSection(&ddraw_cs);
3420     return DD_OK;
3421 }
3422
3423 /*****************************************************************************
3424  * IDirectDrawSurface7::BltFast
3425  *
3426  * Performs a fast Blit.
3427  *
3428  * Params:
3429  *  dstx: The x coordinate to blit to on the destination
3430  *  dsty: The y coordinate to blit to on the destination
3431  *  Source: The source surface
3432  *  rsrc: The source rectangle
3433  *  trans: Type of transfer. Some DDBLTFAST_* flags
3434  *
3435  * Returns:
3436  *  DD_OK on success
3437  *  For more details, see IWineD3DSurface::BltFast
3438  *
3439  *****************************************************************************/
3440 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
3441         IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
3442 {
3443     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3444     IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface7(Source);
3445     DWORD src_w, src_h, dst_w, dst_h;
3446     HRESULT hr;
3447
3448     TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3449             iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
3450
3451     dst_w = This->surface_desc.dwWidth;
3452     dst_h = This->surface_desc.dwHeight;
3453
3454     /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
3455      * in that case
3456      */
3457     if(rsrc)
3458     {
3459         if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
3460            rsrc->right > src->surface_desc.dwWidth ||
3461            rsrc->bottom > src->surface_desc.dwHeight)
3462         {
3463             WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
3464             return DDERR_INVALIDRECT;
3465         }
3466
3467         src_w = rsrc->right - rsrc->left;
3468         src_h = rsrc->bottom - rsrc->top;
3469     }
3470     else
3471     {
3472         src_w = src->surface_desc.dwWidth;
3473         src_h = src->surface_desc.dwHeight;
3474     }
3475
3476     if (src_w > dst_w || dstx > dst_w - src_w
3477             || src_h > dst_h || dsty > dst_h - src_h)
3478     {
3479         WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
3480         return DDERR_INVALIDRECT;
3481     }
3482
3483     EnterCriticalSection(&ddraw_cs);
3484     hr = wined3d_surface_bltfast(This->wined3d_surface, dstx, dsty,
3485             src->wined3d_surface, rsrc, trans);
3486     LeaveCriticalSection(&ddraw_cs);
3487     switch(hr)
3488     {
3489         case WINED3DERR_NOTAVAILABLE:           return DDERR_UNSUPPORTED;
3490         case WINED3DERR_WRONGTEXTUREFORMAT:     return DDERR_INVALIDPIXELFORMAT;
3491         default:                                return hr;
3492     }
3493 }
3494
3495 static HRESULT WINAPI ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
3496         IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
3497 {
3498     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3499     IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
3500     TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3501             iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3502
3503     return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3504             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3505 }
3506
3507 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
3508         IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
3509 {
3510     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3511     IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
3512     TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3513             iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3514
3515     return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3516             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3517 }
3518
3519 static HRESULT WINAPI ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
3520         IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
3521 {
3522     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3523     IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
3524     TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3525             iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3526
3527     return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3528             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3529 }
3530
3531 static HRESULT WINAPI ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
3532         IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
3533 {
3534     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3535     IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
3536     TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3537             iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3538
3539     return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3540             src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3541 }
3542
3543 /*****************************************************************************
3544  * IDirectDrawSurface7::GetClipper
3545  *
3546  * Returns the IDirectDrawClipper interface of the clipper assigned to this
3547  * surface
3548  *
3549  * Params:
3550  *  Clipper: Address to store the interface pointer at
3551  *
3552  * Returns:
3553  *  DD_OK on success
3554  *  DDERR_INVALIDPARAMS if Clipper is NULL
3555  *  DDERR_NOCLIPPERATTACHED if there's no clipper attached
3556  *
3557  *****************************************************************************/
3558 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
3559 {
3560     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3561
3562     TRACE("iface %p, clipper %p.\n", iface, Clipper);
3563
3564     if(!Clipper)
3565     {
3566         LeaveCriticalSection(&ddraw_cs);
3567         return DDERR_INVALIDPARAMS;
3568     }
3569
3570     EnterCriticalSection(&ddraw_cs);
3571     if(This->clipper == NULL)
3572     {
3573         LeaveCriticalSection(&ddraw_cs);
3574         return DDERR_NOCLIPPERATTACHED;
3575     }
3576
3577     *Clipper = (IDirectDrawClipper *)This->clipper;
3578     IDirectDrawClipper_AddRef(*Clipper);
3579     LeaveCriticalSection(&ddraw_cs);
3580     return DD_OK;
3581 }
3582
3583 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
3584 {
3585     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3586     TRACE("iface %p, clipper %p.\n", iface, clipper);
3587
3588     return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3589 }
3590
3591 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
3592 {
3593     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3594     TRACE("iface %p, clipper %p.\n", iface, clipper);
3595
3596     return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3597 }
3598
3599 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
3600 {
3601     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3602     TRACE("iface %p, clipper %p.\n", iface, clipper);
3603
3604     return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3605 }
3606
3607 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
3608 {
3609     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3610     TRACE("iface %p, clipper %p.\n", iface, clipper);
3611
3612     return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3613 }
3614
3615 /*****************************************************************************
3616  * IDirectDrawSurface7::SetClipper
3617  *
3618  * Sets a clipper for the surface
3619  *
3620  * Params:
3621  *  Clipper: IDirectDrawClipper interface of the clipper to set
3622  *
3623  * Returns:
3624  *  DD_OK on success
3625  *
3626  *****************************************************************************/
3627 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper *Clipper)
3628 {
3629     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3630     IDirectDrawClipperImpl *oldClipper = This->clipper;
3631     HWND clipWindow;
3632     HRESULT hr;
3633
3634     TRACE("iface %p, clipper %p.\n", iface, Clipper);
3635
3636     EnterCriticalSection(&ddraw_cs);
3637     if ((IDirectDrawClipperImpl *)Clipper == This->clipper)
3638     {
3639         LeaveCriticalSection(&ddraw_cs);
3640         return DD_OK;
3641     }
3642
3643     This->clipper = (IDirectDrawClipperImpl *)Clipper;
3644
3645     if (Clipper != NULL)
3646         IDirectDrawClipper_AddRef(Clipper);
3647     if(oldClipper)
3648         IDirectDrawClipper_Release((IDirectDrawClipper *)oldClipper);
3649
3650     hr = wined3d_surface_set_clipper(This->wined3d_surface,
3651             This->clipper ? This->clipper->wineD3DClipper : NULL);
3652
3653     if (This->wined3d_swapchain)
3654     {
3655         clipWindow = NULL;
3656         if(Clipper) {
3657             IDirectDrawClipper_GetHWnd(Clipper, &clipWindow);
3658         }
3659
3660         if (clipWindow)
3661             wined3d_swapchain_set_window(This->wined3d_swapchain, clipWindow);
3662         else
3663             wined3d_swapchain_set_window(This->wined3d_swapchain, This->ddraw->d3d_window);
3664     }
3665
3666     LeaveCriticalSection(&ddraw_cs);
3667     return hr;
3668 }
3669
3670 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
3671 {
3672     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3673     TRACE("iface %p, clipper %p.\n", iface, clipper);
3674
3675     return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3676 }
3677
3678 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
3679 {
3680     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3681     TRACE("iface %p, clipper %p.\n", iface, clipper);
3682
3683     return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3684 }
3685
3686 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
3687 {
3688     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3689     TRACE("iface %p, clipper %p.\n", iface, clipper);
3690
3691     return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3692 }
3693
3694 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
3695 {
3696     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3697     TRACE("iface %p, clipper %p.\n", iface, clipper);
3698
3699     return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3700 }
3701
3702 /*****************************************************************************
3703  * IDirectDrawSurface7::SetSurfaceDesc
3704  *
3705  * Sets the surface description. It can override the pixel format, the surface
3706  * memory, ...
3707  * It's not really tested.
3708  *
3709  * Params:
3710  * DDSD: Pointer to the new surface description to set
3711  * Flags: Some flags
3712  *
3713  * Returns:
3714  *  DD_OK on success
3715  *  DDERR_INVALIDPARAMS if DDSD is NULL
3716  *
3717  *****************************************************************************/
3718 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
3719 {
3720     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3721     enum wined3d_format_id newFormat = WINED3DFMT_UNKNOWN;
3722     HRESULT hr;
3723
3724     TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
3725
3726     if(!DDSD)
3727         return DDERR_INVALIDPARAMS;
3728
3729     EnterCriticalSection(&ddraw_cs);
3730     if (DDSD->dwFlags & DDSD_PIXELFORMAT)
3731     {
3732         newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
3733
3734         if(newFormat == WINED3DFMT_UNKNOWN)
3735         {
3736             ERR("Requested to set an unknown pixelformat\n");
3737             LeaveCriticalSection(&ddraw_cs);
3738             return DDERR_INVALIDPARAMS;
3739         }
3740         if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
3741         {
3742             hr = wined3d_surface_set_format(This->wined3d_surface, newFormat);
3743             if (FAILED(hr))
3744             {
3745                 LeaveCriticalSection(&ddraw_cs);
3746                 return hr;
3747             }
3748         }
3749     }
3750     if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
3751     {
3752         wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTOVERLAY,
3753                 (WINEDDCOLORKEY *)&DDSD->u3.ddckCKDestOverlay);
3754     }
3755     if (DDSD->dwFlags & DDSD_CKDESTBLT)
3756     {
3757         wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTBLT,
3758                 (WINEDDCOLORKEY *)&DDSD->ddckCKDestBlt);
3759     }
3760     if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
3761     {
3762         wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCOVERLAY,
3763                 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcOverlay);
3764     }
3765     if (DDSD->dwFlags & DDSD_CKSRCBLT)
3766     {
3767         wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCBLT,
3768                 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcBlt);
3769     }
3770     if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
3771     {
3772         hr = wined3d_surface_set_mem(This->wined3d_surface, DDSD->lpSurface);
3773         if (FAILED(hr))
3774         {
3775             /* No need for a trace here, wined3d does that for us */
3776             switch(hr)
3777             {
3778                 case WINED3DERR_INVALIDCALL:
3779                     LeaveCriticalSection(&ddraw_cs);
3780                     return DDERR_INVALIDPARAMS;
3781                 default:
3782                     break; /* Go on */
3783             }
3784         }
3785     }
3786
3787     This->surface_desc = *DDSD;
3788
3789     LeaveCriticalSection(&ddraw_cs);
3790     return DD_OK;
3791 }
3792
3793 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
3794         DDSURFACEDESC2 *surface_desc, DWORD flags)
3795 {
3796     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3797     TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
3798
3799     return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
3800             surface_desc, flags);
3801 }
3802
3803 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
3804         DDSURFACEDESC *surface_desc, DWORD flags)
3805 {
3806     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3807     TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
3808
3809     return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
3810             (DDSURFACEDESC2 *)surface_desc, flags);
3811 }
3812
3813 /*****************************************************************************
3814  * IDirectDrawSurface7::GetPalette
3815  *
3816  * Returns the IDirectDrawPalette interface of the palette currently assigned
3817  * to the surface
3818  *
3819  * Params:
3820  *  Pal: Address to write the interface pointer to
3821  *
3822  * Returns:
3823  *  DD_OK on success
3824  *  DDERR_INVALIDPARAMS if Pal is NULL
3825  *
3826  *****************************************************************************/
3827 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
3828 {
3829     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3830     struct wined3d_palette *wined3d_palette;
3831     HRESULT hr = DD_OK;
3832
3833     TRACE("iface %p, palette %p.\n", iface, Pal);
3834
3835     if(!Pal)
3836         return DDERR_INVALIDPARAMS;
3837
3838     EnterCriticalSection(&ddraw_cs);
3839     wined3d_palette = wined3d_surface_get_palette(This->wined3d_surface);
3840     if (wined3d_palette)
3841     {
3842         *Pal = wined3d_palette_get_parent(wined3d_palette);
3843         IDirectDrawPalette_AddRef(*Pal);
3844     }
3845     else
3846     {
3847         *Pal = NULL;
3848         hr = DDERR_NOPALETTEATTACHED;
3849     }
3850
3851     LeaveCriticalSection(&ddraw_cs);
3852     return hr;
3853 }
3854
3855 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
3856 {
3857     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3858     TRACE("iface %p, palette %p.\n", iface, palette);
3859
3860     return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
3861 }
3862
3863 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
3864 {
3865     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3866     TRACE("iface %p, palette %p.\n", iface, palette);
3867
3868     return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
3869 }
3870
3871 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
3872 {
3873     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3874     TRACE("iface %p, palette %p.\n", iface, palette);
3875
3876     return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
3877 }
3878
3879 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
3880 {
3881     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3882     TRACE("iface %p, palette %p.\n", iface, palette);
3883
3884     return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
3885 }
3886
3887 /*****************************************************************************
3888  * SetColorKeyEnum
3889  *
3890  * EnumAttachedSurface callback for SetColorKey. Used to set color keys
3891  * recursively in the surface tree
3892  *
3893  *****************************************************************************/
3894 struct SCKContext
3895 {
3896     HRESULT ret;
3897     WINEDDCOLORKEY *CKey;
3898     DWORD Flags;
3899 };
3900
3901 static HRESULT WINAPI
3902 SetColorKeyEnum(IDirectDrawSurface7 *surface,
3903                 DDSURFACEDESC2 *desc,
3904                 void *context)
3905 {
3906     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
3907     struct SCKContext *ctx = context;
3908     HRESULT hr;
3909
3910     hr = wined3d_surface_set_color_key(This->wined3d_surface, ctx->Flags, ctx->CKey);
3911     if (FAILED(hr))
3912     {
3913         WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
3914         ctx->ret = hr;
3915     }
3916
3917     ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
3918     ddraw_surface7_Release(surface);
3919
3920     return DDENUMRET_OK;
3921 }
3922
3923 /*****************************************************************************
3924  * IDirectDrawSurface7::SetColorKey
3925  *
3926  * Sets the color keying options for the surface. Observations showed that
3927  * in case of complex surfaces the color key has to be assigned to all
3928  * sublevels.
3929  *
3930  * Params:
3931  *  Flags: DDCKEY_*
3932  *  CKey: The new color key
3933  *
3934  * Returns:
3935  *  DD_OK on success
3936  *  See IWineD3DSurface::SetColorKey for details
3937  *
3938  *****************************************************************************/
3939 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
3940 {
3941     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3942     DDCOLORKEY FixedCKey;
3943     struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
3944
3945     TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
3946
3947     EnterCriticalSection(&ddraw_cs);
3948     if (CKey)
3949     {
3950         FixedCKey = *CKey;
3951         /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
3952         if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
3953             FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
3954
3955         switch (Flags & ~DDCKEY_COLORSPACE)
3956         {
3957         case DDCKEY_DESTBLT:
3958             This->surface_desc.ddckCKDestBlt = FixedCKey;
3959             This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
3960             break;
3961
3962         case DDCKEY_DESTOVERLAY:
3963             This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
3964             This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
3965             break;
3966
3967         case DDCKEY_SRCOVERLAY:
3968             This->surface_desc.ddckCKSrcOverlay = FixedCKey;
3969             This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
3970             break;
3971
3972         case DDCKEY_SRCBLT:
3973             This->surface_desc.ddckCKSrcBlt = FixedCKey;
3974             This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
3975             break;
3976
3977         default:
3978             LeaveCriticalSection(&ddraw_cs);
3979             return DDERR_INVALIDPARAMS;
3980         }
3981     }
3982     else
3983     {
3984         switch (Flags & ~DDCKEY_COLORSPACE)
3985         {
3986         case DDCKEY_DESTBLT:
3987             This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
3988             break;
3989
3990         case DDCKEY_DESTOVERLAY:
3991             This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
3992             break;
3993
3994         case DDCKEY_SRCOVERLAY:
3995             This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
3996             break;
3997
3998         case DDCKEY_SRCBLT:
3999             This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4000             break;
4001
4002         default:
4003             LeaveCriticalSection(&ddraw_cs);
4004             return DDERR_INVALIDPARAMS;
4005         }
4006     }
4007     ctx.ret = wined3d_surface_set_color_key(This->wined3d_surface, Flags, ctx.CKey);
4008     ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
4009     LeaveCriticalSection(&ddraw_cs);
4010     switch(ctx.ret)
4011     {
4012         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
4013         default:                            return ctx.ret;
4014     }
4015 }
4016
4017 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4018 {
4019     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4020     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4021
4022     return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4023 }
4024
4025 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4026 {
4027     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4028     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4029
4030     return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4031 }
4032
4033 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4034 {
4035     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4036     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4037
4038     return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4039 }
4040
4041 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4042 {
4043     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4044     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4045
4046     return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4047 }
4048
4049 /*****************************************************************************
4050  * IDirectDrawSurface7::SetPalette
4051  *
4052  * Assigns a DirectDrawPalette object to the surface
4053  *
4054  * Params:
4055  *  Pal: Interface to the palette to set
4056  *
4057  * Returns:
4058  *  DD_OK on success
4059  *
4060  *****************************************************************************/
4061 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
4062 {
4063     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4064     IDirectDrawPalette *oldPal;
4065     IDirectDrawSurfaceImpl *surf;
4066     IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
4067     HRESULT hr;
4068
4069     TRACE("iface %p, palette %p.\n", iface, Pal);
4070
4071     if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
4072             DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
4073         return DDERR_INVALIDPIXELFORMAT;
4074     }
4075
4076     /* Find the old palette */
4077     EnterCriticalSection(&ddraw_cs);
4078     hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
4079     if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
4080     {
4081         LeaveCriticalSection(&ddraw_cs);
4082         return hr;
4083     }
4084     if(oldPal) IDirectDrawPalette_Release(oldPal);  /* For the GetPalette */
4085
4086     /* Set the new Palette */
4087     wined3d_surface_set_palette(This->wined3d_surface, PalImpl ? PalImpl->wineD3DPalette : NULL);
4088     /* AddRef the Palette */
4089     if(Pal) IDirectDrawPalette_AddRef(Pal);
4090
4091     /* Release the old palette */
4092     if(oldPal) IDirectDrawPalette_Release(oldPal);
4093
4094     /* If this is a front buffer, also update the back buffers
4095      * TODO: How do things work for palettized cube textures?
4096      */
4097     if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
4098     {
4099         /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
4100         DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
4101
4102         surf = This;
4103         while(1)
4104         {
4105             IDirectDrawSurface7 *attach;
4106             HRESULT hr;
4107             hr = ddraw_surface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &caps2, &attach);
4108             if(hr != DD_OK)
4109             {
4110                 break;
4111             }
4112
4113             TRACE("Setting palette on %p\n", attach);
4114             ddraw_surface7_SetPalette(attach, Pal);
4115             surf = impl_from_IDirectDrawSurface7(attach);
4116             ddraw_surface7_Release(attach);
4117         }
4118     }
4119
4120     LeaveCriticalSection(&ddraw_cs);
4121     return DD_OK;
4122 }
4123
4124 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4125 {
4126     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4127     TRACE("iface %p, palette %p.\n", iface, palette);
4128
4129     return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4130 }
4131
4132 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4133 {
4134     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4135     TRACE("iface %p, palette %p.\n", iface, palette);
4136
4137     return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4138 }
4139
4140 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4141 {
4142     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4143     TRACE("iface %p, palette %p.\n", iface, palette);
4144
4145     return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4146 }
4147
4148 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4149 {
4150     IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4151     TRACE("iface %p, palette %p.\n", iface, palette);
4152
4153     return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4154 }
4155
4156 /**********************************************************
4157  * IDirectDrawGammaControl::GetGammaRamp
4158  *
4159  * Returns the current gamma ramp for a surface
4160  *
4161  * Params:
4162  *  flags: Ignored
4163  *  gamma_ramp: Address to write the ramp to
4164  *
4165  * Returns:
4166  *  DD_OK on success
4167  *  DDERR_INVALIDPARAMS if gamma_ramp is NULL
4168  *
4169  **********************************************************/
4170 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4171         DWORD flags, DDGAMMARAMP *gamma_ramp)
4172 {
4173     IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4174
4175     TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4176
4177     if (!gamma_ramp)
4178     {
4179         WARN("Invalid gamma_ramp passed.\n");
4180         return DDERR_INVALIDPARAMS;
4181     }
4182
4183     EnterCriticalSection(&ddraw_cs);
4184     if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4185     {
4186         /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP. */
4187         wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (WINED3DGAMMARAMP *)gamma_ramp);
4188     }
4189     else
4190     {
4191         ERR("Not implemented for non-primary surfaces.\n");
4192     }
4193     LeaveCriticalSection(&ddraw_cs);
4194
4195     return DD_OK;
4196 }
4197
4198 /**********************************************************
4199  * IDirectDrawGammaControl::SetGammaRamp
4200  *
4201  * Sets the red, green and blue gamma ramps for
4202  *
4203  * Params:
4204  *  flags: Can be DDSGR_CALIBRATE to request calibration
4205  *  gamma_ramp: Structure containing the new gamma ramp
4206  *
4207  * Returns:
4208  *  DD_OK on success
4209  *  DDERR_INVALIDPARAMS if gamma_ramp is NULL
4210  *
4211  **********************************************************/
4212 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4213         DWORD flags, DDGAMMARAMP *gamma_ramp)
4214 {
4215     IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4216
4217     TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4218
4219     if (!gamma_ramp)
4220     {
4221         WARN("Invalid gamma_ramp passed.\n");
4222         return DDERR_INVALIDPARAMS;
4223     }
4224
4225     EnterCriticalSection(&ddraw_cs);
4226     if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4227     {
4228         /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP */
4229         wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device, 0, flags, (WINED3DGAMMARAMP *)gamma_ramp);
4230     }
4231     else
4232     {
4233         ERR("Not implemented for non-primary surfaces.\n");
4234     }
4235     LeaveCriticalSection(&ddraw_cs);
4236
4237     return DD_OK;
4238 }
4239
4240 /*****************************************************************************
4241  * IDirect3DTexture2::PaletteChanged
4242  *
4243  * Informs the texture about a palette change
4244  *
4245  * Params:
4246  *  start: Start index of the change
4247  *  count: The number of changed entries
4248  *
4249  * Returns
4250  *  D3D_OK, because it's a stub
4251  *
4252  *****************************************************************************/
4253 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4254 {
4255     FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4256
4257     return D3D_OK;
4258 }
4259
4260 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4261 {
4262     IDirectDrawSurfaceImpl *surface = surface_from_texture1(iface);
4263
4264     TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4265
4266     return d3d_texture2_PaletteChanged((IDirect3DTexture2 *)&surface->IDirect3DTexture2_vtbl, start, count);
4267 }
4268
4269 /*****************************************************************************
4270  * IDirect3DTexture::Unload
4271  *
4272  * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4273  *
4274  *
4275  * Returns:
4276  *  DDERR_UNSUPPORTED
4277  *
4278  *****************************************************************************/
4279 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4280 {
4281     WARN("iface %p. Not implemented.\n", iface);
4282
4283     return DDERR_UNSUPPORTED;
4284 }
4285
4286 /*****************************************************************************
4287  * IDirect3DTexture2::GetHandle
4288  *
4289  * Returns handle for the texture. At the moment, the interface
4290  * to the IWineD3DTexture is used.
4291  *
4292  * Params:
4293  *  device: Device this handle is assigned to
4294  *  handle: Address to store the handle at.
4295  *
4296  * Returns:
4297  *  D3D_OK
4298  *
4299  *****************************************************************************/
4300 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4301         IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4302 {
4303     IDirectDrawSurfaceImpl *surface = surface_from_texture2(iface);
4304
4305     TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4306
4307     EnterCriticalSection(&ddraw_cs);
4308
4309     if (!surface->Handle)
4310     {
4311         DWORD h = ddraw_allocate_handle(&device_from_device2(device)->handle_table, surface, DDRAW_HANDLE_SURFACE);
4312         if (h == DDRAW_INVALID_HANDLE)
4313         {
4314             ERR("Failed to allocate a texture handle.\n");
4315             LeaveCriticalSection(&ddraw_cs);
4316             return DDERR_OUTOFMEMORY;
4317         }
4318
4319         surface->Handle = h + 1;
4320     }
4321
4322     TRACE("Returning handle %08x.\n", surface->Handle);
4323     *handle = surface->Handle;
4324
4325     LeaveCriticalSection(&ddraw_cs);
4326
4327     return D3D_OK;
4328 }
4329
4330 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
4331         IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
4332 {
4333     IDirect3DTexture2 *texture2 = (IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl;
4334     IDirect3DDevice2 *device2 = (IDirect3DDevice2 *)&device_from_device1(device)->IDirect3DDevice2_vtbl;
4335
4336     TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4337
4338     return d3d_texture2_GetHandle(texture2, device2, handle);
4339 }
4340
4341 /*****************************************************************************
4342  * get_sub_mimaplevel
4343  *
4344  * Helper function that returns the next mipmap level
4345  *
4346  * tex_ptr: Surface of which to return the next level
4347  *
4348  *****************************************************************************/
4349 static IDirectDrawSurfaceImpl *get_sub_mimaplevel(IDirectDrawSurfaceImpl *surface)
4350 {
4351     /* Now go down the mipmap chain to the next surface */
4352     static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
4353     IDirectDrawSurface7 *next_level;
4354     HRESULT hr;
4355
4356     hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
4357     if (FAILED(hr)) return NULL;
4358
4359     ddraw_surface7_Release(next_level);
4360
4361     return impl_from_IDirectDrawSurface7(next_level);
4362 }
4363
4364 /*****************************************************************************
4365  * IDirect3DTexture2::Load
4366  *
4367  * Loads a texture created with the DDSCAPS_ALLOCONLOAD
4368  *
4369  * This function isn't relayed to WineD3D because the whole interface is
4370  * implemented in DDraw only. For speed improvements a implementation which
4371  * takes OpenGL more into account could be placed into WineD3D.
4372  *
4373  * Params:
4374  *  src_texture: Address of the texture to load
4375  *
4376  * Returns:
4377  *  D3D_OK on success
4378  *  D3DERR_TEXTURE_LOAD_FAILED.
4379  *
4380  *****************************************************************************/
4381 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
4382 {
4383     IDirectDrawSurfaceImpl *dst_surface = surface_from_texture2(iface);
4384     IDirectDrawSurfaceImpl *src_surface = surface_from_texture2(src_texture);
4385     HRESULT hr;
4386
4387     TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4388
4389     if (src_surface == dst_surface)
4390     {
4391         TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
4392         return D3D_OK;
4393     }
4394
4395     EnterCriticalSection(&ddraw_cs);
4396
4397     if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4398             != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
4399             || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
4400     {
4401         ERR("Trying to load surfaces with different mip-map counts.\n");
4402     }
4403
4404     for (;;)
4405     {
4406         struct wined3d_palette *wined3d_dst_pal, *wined3d_src_pal;
4407         IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
4408         DDSURFACEDESC *src_desc, *dst_desc;
4409
4410         TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
4411                 src_surface, dst_surface, src_surface->mipmap_level);
4412
4413         /* Suppress the ALLOCONLOAD flag */
4414         dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
4415
4416         /* Get the palettes */
4417         wined3d_dst_pal = wined3d_surface_get_palette(dst_surface->wined3d_surface);
4418         if (wined3d_dst_pal)
4419             dst_pal = wined3d_palette_get_parent(wined3d_dst_pal);
4420
4421         wined3d_src_pal = wined3d_surface_get_palette(src_surface->wined3d_surface);
4422         if (wined3d_src_pal)
4423             src_pal = wined3d_palette_get_parent(wined3d_src_pal);
4424
4425         if (src_pal)
4426         {
4427             PALETTEENTRY palent[256];
4428
4429             if (!dst_pal)
4430             {
4431                 LeaveCriticalSection(&ddraw_cs);
4432                 return DDERR_NOPALETTEATTACHED;
4433             }
4434             IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
4435             IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
4436         }
4437
4438         /* Copy one surface on the other */
4439         dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
4440         src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
4441
4442         if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
4443         {
4444             /* Should also check for same pixel format, u1.lPitch, ... */
4445             ERR("Error in surface sizes.\n");
4446             LeaveCriticalSection(&ddraw_cs);
4447             return D3DERR_TEXTURE_LOAD_FAILED;
4448         }
4449         else
4450         {
4451             WINED3DLOCKED_RECT src_rect, dst_rect;
4452
4453             /* Copy also the ColorKeying stuff */
4454             if (src_desc->dwFlags & DDSD_CKSRCBLT)
4455             {
4456                 dst_desc->dwFlags |= DDSD_CKSRCBLT;
4457                 dst_desc->ddckCKSrcBlt.dwColorSpaceLowValue = src_desc->ddckCKSrcBlt.dwColorSpaceLowValue;
4458                 dst_desc->ddckCKSrcBlt.dwColorSpaceHighValue = src_desc->ddckCKSrcBlt.dwColorSpaceHighValue;
4459             }
4460
4461             /* Copy the main memory texture into the surface that corresponds
4462              * to the OpenGL texture object. */
4463
4464             hr = wined3d_surface_map(src_surface->wined3d_surface, &src_rect, NULL, 0);
4465             if (FAILED(hr))
4466             {
4467                 ERR("Failed to lock source surface, hr %#x.\n", hr);
4468                 LeaveCriticalSection(&ddraw_cs);
4469                 return D3DERR_TEXTURE_LOAD_FAILED;
4470             }
4471
4472             hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_rect, NULL, 0);
4473             if (FAILED(hr))
4474             {
4475                 ERR("Failed to lock destination surface, hr %#x.\n", hr);
4476                 wined3d_surface_unmap(src_surface->wined3d_surface);
4477                 LeaveCriticalSection(&ddraw_cs);
4478                 return D3DERR_TEXTURE_LOAD_FAILED;
4479             }
4480
4481             if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
4482                 memcpy(dst_rect.pBits, src_rect.pBits, src_surface->surface_desc.u1.dwLinearSize);
4483             else
4484                 memcpy(dst_rect.pBits, src_rect.pBits, src_rect.Pitch * src_desc->dwHeight);
4485
4486             wined3d_surface_unmap(src_surface->wined3d_surface);
4487             wined3d_surface_unmap(dst_surface->wined3d_surface);
4488         }
4489
4490         if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4491             src_surface = get_sub_mimaplevel(src_surface);
4492         else
4493             src_surface = NULL;
4494
4495         if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4496             dst_surface = get_sub_mimaplevel(dst_surface);
4497         else
4498             dst_surface = NULL;
4499
4500         if (!src_surface || !dst_surface)
4501         {
4502             if (src_surface != dst_surface)
4503                 ERR("Loading surface with different mipmap structure.\n");
4504             break;
4505         }
4506     }
4507
4508     LeaveCriticalSection(&ddraw_cs);
4509
4510     return hr;
4511 }
4512
4513 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
4514 {
4515     TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4516
4517     return d3d_texture2_Load((IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl,
4518             src_texture ? (IDirect3DTexture2 *)&surface_from_texture1(src_texture)->IDirect3DTexture2_vtbl : NULL);
4519 }
4520
4521 /*****************************************************************************
4522  * The VTable
4523  *****************************************************************************/
4524
4525 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
4526 {
4527     /* IUnknown */
4528     ddraw_surface7_QueryInterface,
4529     ddraw_surface7_AddRef,
4530     ddraw_surface7_Release,
4531     /* IDirectDrawSurface */
4532     ddraw_surface7_AddAttachedSurface,
4533     ddraw_surface7_AddOverlayDirtyRect,
4534     ddraw_surface7_Blt,
4535     ddraw_surface7_BltBatch,
4536     ddraw_surface7_BltFast,
4537     ddraw_surface7_DeleteAttachedSurface,
4538     ddraw_surface7_EnumAttachedSurfaces,
4539     ddraw_surface7_EnumOverlayZOrders,
4540     ddraw_surface7_Flip,
4541     ddraw_surface7_GetAttachedSurface,
4542     ddraw_surface7_GetBltStatus,
4543     ddraw_surface7_GetCaps,
4544     ddraw_surface7_GetClipper,
4545     ddraw_surface7_GetColorKey,
4546     ddraw_surface7_GetDC,
4547     ddraw_surface7_GetFlipStatus,
4548     ddraw_surface7_GetOverlayPosition,
4549     ddraw_surface7_GetPalette,
4550     ddraw_surface7_GetPixelFormat,
4551     ddraw_surface7_GetSurfaceDesc,
4552     ddraw_surface7_Initialize,
4553     ddraw_surface7_IsLost,
4554     ddraw_surface7_Lock,
4555     ddraw_surface7_ReleaseDC,
4556     ddraw_surface7_Restore,
4557     ddraw_surface7_SetClipper,
4558     ddraw_surface7_SetColorKey,
4559     ddraw_surface7_SetOverlayPosition,
4560     ddraw_surface7_SetPalette,
4561     ddraw_surface7_Unlock,
4562     ddraw_surface7_UpdateOverlay,
4563     ddraw_surface7_UpdateOverlayDisplay,
4564     ddraw_surface7_UpdateOverlayZOrder,
4565     /* IDirectDrawSurface2 */
4566     ddraw_surface7_GetDDInterface,
4567     ddraw_surface7_PageLock,
4568     ddraw_surface7_PageUnlock,
4569     /* IDirectDrawSurface3 */
4570     ddraw_surface7_SetSurfaceDesc,
4571     /* IDirectDrawSurface4 */
4572     ddraw_surface7_SetPrivateData,
4573     ddraw_surface7_GetPrivateData,
4574     ddraw_surface7_FreePrivateData,
4575     ddraw_surface7_GetUniquenessValue,
4576     ddraw_surface7_ChangeUniquenessValue,
4577     /* IDirectDrawSurface7 */
4578     ddraw_surface7_SetPriority,
4579     ddraw_surface7_GetPriority,
4580     ddraw_surface7_SetLOD,
4581     ddraw_surface7_GetLOD,
4582 };
4583
4584 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
4585 {
4586     /* IUnknown */
4587     ddraw_surface4_QueryInterface,
4588     ddraw_surface4_AddRef,
4589     ddraw_surface4_Release,
4590     /* IDirectDrawSurface */
4591     ddraw_surface4_AddAttachedSurface,
4592     ddraw_surface4_AddOverlayDirtyRect,
4593     ddraw_surface4_Blt,
4594     ddraw_surface4_BltBatch,
4595     ddraw_surface4_BltFast,
4596     ddraw_surface4_DeleteAttachedSurface,
4597     ddraw_surface4_EnumAttachedSurfaces,
4598     ddraw_surface4_EnumOverlayZOrders,
4599     ddraw_surface4_Flip,
4600     ddraw_surface4_GetAttachedSurface,
4601     ddraw_surface4_GetBltStatus,
4602     ddraw_surface4_GetCaps,
4603     ddraw_surface4_GetClipper,
4604     ddraw_surface4_GetColorKey,
4605     ddraw_surface4_GetDC,
4606     ddraw_surface4_GetFlipStatus,
4607     ddraw_surface4_GetOverlayPosition,
4608     ddraw_surface4_GetPalette,
4609     ddraw_surface4_GetPixelFormat,
4610     ddraw_surface4_GetSurfaceDesc,
4611     ddraw_surface4_Initialize,
4612     ddraw_surface4_IsLost,
4613     ddraw_surface4_Lock,
4614     ddraw_surface4_ReleaseDC,
4615     ddraw_surface4_Restore,
4616     ddraw_surface4_SetClipper,
4617     ddraw_surface4_SetColorKey,
4618     ddraw_surface4_SetOverlayPosition,
4619     ddraw_surface4_SetPalette,
4620     ddraw_surface4_Unlock,
4621     ddraw_surface4_UpdateOverlay,
4622     ddraw_surface4_UpdateOverlayDisplay,
4623     ddraw_surface4_UpdateOverlayZOrder,
4624     /* IDirectDrawSurface2 */
4625     ddraw_surface4_GetDDInterface,
4626     ddraw_surface4_PageLock,
4627     ddraw_surface4_PageUnlock,
4628     /* IDirectDrawSurface3 */
4629     ddraw_surface4_SetSurfaceDesc,
4630     /* IDirectDrawSurface4 */
4631     ddraw_surface4_SetPrivateData,
4632     ddraw_surface4_GetPrivateData,
4633     ddraw_surface4_FreePrivateData,
4634     ddraw_surface4_GetUniquenessValue,
4635     ddraw_surface4_ChangeUniquenessValue,
4636 };
4637
4638 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
4639 {
4640     /* IUnknown */
4641     ddraw_surface3_QueryInterface,
4642     ddraw_surface3_AddRef,
4643     ddraw_surface3_Release,
4644     /* IDirectDrawSurface */
4645     ddraw_surface3_AddAttachedSurface,
4646     ddraw_surface3_AddOverlayDirtyRect,
4647     ddraw_surface3_Blt,
4648     ddraw_surface3_BltBatch,
4649     ddraw_surface3_BltFast,
4650     ddraw_surface3_DeleteAttachedSurface,
4651     ddraw_surface3_EnumAttachedSurfaces,
4652     ddraw_surface3_EnumOverlayZOrders,
4653     ddraw_surface3_Flip,
4654     ddraw_surface3_GetAttachedSurface,
4655     ddraw_surface3_GetBltStatus,
4656     ddraw_surface3_GetCaps,
4657     ddraw_surface3_GetClipper,
4658     ddraw_surface3_GetColorKey,
4659     ddraw_surface3_GetDC,
4660     ddraw_surface3_GetFlipStatus,
4661     ddraw_surface3_GetOverlayPosition,
4662     ddraw_surface3_GetPalette,
4663     ddraw_surface3_GetPixelFormat,
4664     ddraw_surface3_GetSurfaceDesc,
4665     ddraw_surface3_Initialize,
4666     ddraw_surface3_IsLost,
4667     ddraw_surface3_Lock,
4668     ddraw_surface3_ReleaseDC,
4669     ddraw_surface3_Restore,
4670     ddraw_surface3_SetClipper,
4671     ddraw_surface3_SetColorKey,
4672     ddraw_surface3_SetOverlayPosition,
4673     ddraw_surface3_SetPalette,
4674     ddraw_surface3_Unlock,
4675     ddraw_surface3_UpdateOverlay,
4676     ddraw_surface3_UpdateOverlayDisplay,
4677     ddraw_surface3_UpdateOverlayZOrder,
4678     /* IDirectDrawSurface2 */
4679     ddraw_surface3_GetDDInterface,
4680     ddraw_surface3_PageLock,
4681     ddraw_surface3_PageUnlock,
4682     /* IDirectDrawSurface3 */
4683     ddraw_surface3_SetSurfaceDesc,
4684 };
4685
4686 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
4687 {
4688     /* IUnknown */
4689     ddraw_surface2_QueryInterface,
4690     ddraw_surface2_AddRef,
4691     ddraw_surface2_Release,
4692     /* IDirectDrawSurface */
4693     ddraw_surface2_AddAttachedSurface,
4694     ddraw_surface2_AddOverlayDirtyRect,
4695     ddraw_surface2_Blt,
4696     ddraw_surface2_BltBatch,
4697     ddraw_surface2_BltFast,
4698     ddraw_surface2_DeleteAttachedSurface,
4699     ddraw_surface2_EnumAttachedSurfaces,
4700     ddraw_surface2_EnumOverlayZOrders,
4701     ddraw_surface2_Flip,
4702     ddraw_surface2_GetAttachedSurface,
4703     ddraw_surface2_GetBltStatus,
4704     ddraw_surface2_GetCaps,
4705     ddraw_surface2_GetClipper,
4706     ddraw_surface2_GetColorKey,
4707     ddraw_surface2_GetDC,
4708     ddraw_surface2_GetFlipStatus,
4709     ddraw_surface2_GetOverlayPosition,
4710     ddraw_surface2_GetPalette,
4711     ddraw_surface2_GetPixelFormat,
4712     ddraw_surface2_GetSurfaceDesc,
4713     ddraw_surface2_Initialize,
4714     ddraw_surface2_IsLost,
4715     ddraw_surface2_Lock,
4716     ddraw_surface2_ReleaseDC,
4717     ddraw_surface2_Restore,
4718     ddraw_surface2_SetClipper,
4719     ddraw_surface2_SetColorKey,
4720     ddraw_surface2_SetOverlayPosition,
4721     ddraw_surface2_SetPalette,
4722     ddraw_surface2_Unlock,
4723     ddraw_surface2_UpdateOverlay,
4724     ddraw_surface2_UpdateOverlayDisplay,
4725     ddraw_surface2_UpdateOverlayZOrder,
4726     /* IDirectDrawSurface2 */
4727     ddraw_surface2_GetDDInterface,
4728     ddraw_surface2_PageLock,
4729     ddraw_surface2_PageUnlock,
4730 };
4731
4732 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
4733 {
4734     /* IUnknown */
4735     ddraw_surface1_QueryInterface,
4736     ddraw_surface1_AddRef,
4737     ddraw_surface1_Release,
4738     /* IDirectDrawSurface */
4739     ddraw_surface1_AddAttachedSurface,
4740     ddraw_surface1_AddOverlayDirtyRect,
4741     ddraw_surface1_Blt,
4742     ddraw_surface1_BltBatch,
4743     ddraw_surface1_BltFast,
4744     ddraw_surface1_DeleteAttachedSurface,
4745     ddraw_surface1_EnumAttachedSurfaces,
4746     ddraw_surface1_EnumOverlayZOrders,
4747     ddraw_surface1_Flip,
4748     ddraw_surface1_GetAttachedSurface,
4749     ddraw_surface1_GetBltStatus,
4750     ddraw_surface1_GetCaps,
4751     ddraw_surface1_GetClipper,
4752     ddraw_surface1_GetColorKey,
4753     ddraw_surface1_GetDC,
4754     ddraw_surface1_GetFlipStatus,
4755     ddraw_surface1_GetOverlayPosition,
4756     ddraw_surface1_GetPalette,
4757     ddraw_surface1_GetPixelFormat,
4758     ddraw_surface1_GetSurfaceDesc,
4759     ddraw_surface1_Initialize,
4760     ddraw_surface1_IsLost,
4761     ddraw_surface1_Lock,
4762     ddraw_surface1_ReleaseDC,
4763     ddraw_surface1_Restore,
4764     ddraw_surface1_SetClipper,
4765     ddraw_surface1_SetColorKey,
4766     ddraw_surface1_SetOverlayPosition,
4767     ddraw_surface1_SetPalette,
4768     ddraw_surface1_Unlock,
4769     ddraw_surface1_UpdateOverlay,
4770     ddraw_surface1_UpdateOverlayDisplay,
4771     ddraw_surface1_UpdateOverlayZOrder,
4772 };
4773
4774 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
4775 {
4776     ddraw_gamma_control_QueryInterface,
4777     ddraw_gamma_control_AddRef,
4778     ddraw_gamma_control_Release,
4779     ddraw_gamma_control_GetGammaRamp,
4780     ddraw_gamma_control_SetGammaRamp,
4781 };
4782
4783 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
4784 {
4785     d3d_texture2_QueryInterface,
4786     d3d_texture2_AddRef,
4787     d3d_texture2_Release,
4788     d3d_texture2_GetHandle,
4789     d3d_texture2_PaletteChanged,
4790     d3d_texture2_Load,
4791 };
4792
4793 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
4794 {
4795     d3d_texture1_QueryInterface,
4796     d3d_texture1_AddRef,
4797     d3d_texture1_Release,
4798     d3d_texture1_Initialize,
4799     d3d_texture1_GetHandle,
4800     d3d_texture1_PaletteChanged,
4801     d3d_texture1_Load,
4802     d3d_texture1_Unload,
4803 };
4804
4805 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
4806 {
4807     if (!iface) return NULL;
4808     assert(iface->lpVtbl == &ddraw_surface7_vtbl);
4809     return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface7_iface);
4810 }
4811
4812 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
4813 {
4814     if (!iface) return NULL;
4815     assert(iface->lpVtbl == &ddraw_surface4_vtbl);
4816     return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface4_iface);
4817 }
4818
4819 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
4820 {
4821     if (!iface) return NULL;
4822     assert(iface->lpVtbl == &ddraw_surface3_vtbl);
4823     return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface3_iface);
4824 }
4825
4826 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
4827 {
4828     if (!iface) return NULL;
4829     assert(iface->lpVtbl == &ddraw_surface2_vtbl);
4830     return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface2_iface);
4831 }
4832
4833 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
4834 {
4835     if (!iface) return NULL;
4836     assert(iface->lpVtbl == &ddraw_surface1_vtbl);
4837     return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface_iface);
4838 }
4839
4840 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
4841 {
4842     IDirectDrawSurfaceImpl *surface = parent;
4843
4844     TRACE("surface %p.\n", surface);
4845
4846     /* Check for attached surfaces and detach them. */
4847     if (surface->first_attached != surface)
4848     {
4849         IDirectDrawSurface7 *root = &surface->first_attached->IDirectDrawSurface7_iface;
4850         IDirectDrawSurface7 *detach = &surface->IDirectDrawSurface7_iface;
4851
4852         /* Well, this shouldn't happen: The surface being attached is
4853          * referenced in AddAttachedSurface(), so it shouldn't be released
4854          * until DeleteAttachedSurface() is called, because the refcount is
4855          * held. It looks like the application released it often enough to
4856          * force this. */
4857         WARN("Surface is still attached to surface %p.\n", surface->first_attached);
4858
4859         /* The refcount will drop to -1 here */
4860         if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
4861             ERR("DeleteAttachedSurface failed.\n");
4862     }
4863
4864     while (surface->next_attached)
4865     {
4866         IDirectDrawSurface7 *root = &surface->IDirectDrawSurface7_iface;
4867         IDirectDrawSurface7 *detach = &surface->next_attached->IDirectDrawSurface7_iface;
4868
4869         if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
4870             ERR("DeleteAttachedSurface failed.\n");
4871     }
4872
4873     /* Having a texture handle set implies that the device still exists. */
4874     if (surface->Handle)
4875         ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
4876
4877     /* Reduce the ddraw surface count. */
4878     InterlockedDecrement(&surface->ddraw->surfaces);
4879     list_remove(&surface->surface_list_entry);
4880
4881     HeapFree(GetProcessHeap(), 0, surface);
4882 }
4883
4884 const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
4885 {
4886     ddraw_surface_wined3d_object_destroyed,
4887 };
4888
4889 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
4890 {
4891     IDirectDrawSurfaceImpl *surface = parent;
4892
4893     TRACE("surface %p.\n", surface);
4894
4895     ddraw_surface_cleanup(surface);
4896 }
4897
4898 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
4899 {
4900     ddraw_texture_wined3d_object_destroyed,
4901 };
4902
4903 HRESULT ddraw_surface_create_texture(IDirectDrawSurfaceImpl *surface)
4904 {
4905     const DDSURFACEDESC2 *desc = &surface->surface_desc;
4906     enum wined3d_format_id format;
4907     WINED3DPOOL pool;
4908     UINT levels;
4909
4910     if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4911         levels = desc->u2.dwMipMapCount;
4912     else
4913         levels = 1;
4914
4915     /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM.
4916      * Should I forward the MANAGED cap to the managed pool? */
4917     if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4918         pool = WINED3DPOOL_SYSTEMMEM;
4919     else
4920         pool = WINED3DPOOL_DEFAULT;
4921
4922     format = PixelFormat_DD2WineD3D(&surface->surface_desc.u4.ddpfPixelFormat);
4923     if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
4924         return wined3d_texture_create_cube(surface->ddraw->wined3d_device, desc->dwWidth,
4925                 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
4926     else
4927         return wined3d_texture_create_2d(surface->ddraw->wined3d_device, desc->dwWidth, desc->dwHeight,
4928                 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
4929 }
4930
4931 HRESULT ddraw_surface_init(IDirectDrawSurfaceImpl *surface, IDirectDrawImpl *ddraw,
4932         DDSURFACEDESC2 *desc, UINT mip_level, WINED3DSURFTYPE surface_type)
4933 {
4934     struct wined3d_resource_desc wined3d_desc;
4935     struct wined3d_resource *wined3d_resource;
4936     WINED3DPOOL pool = WINED3DPOOL_DEFAULT;
4937     enum wined3d_format_id format;
4938     DWORD usage = 0;
4939     HRESULT hr;
4940
4941     if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
4942             && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
4943             && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
4944     {
4945         /* Tests show surfaces without memory flags get these flags added
4946          * right after creation. */
4947         desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
4948     }
4949
4950     if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4951     {
4952         usage |= WINED3DUSAGE_RENDERTARGET;
4953         desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
4954     }
4955
4956     if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && !(desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
4957     {
4958         usage |= WINED3DUSAGE_RENDERTARGET;
4959     }
4960
4961     if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
4962     {
4963         usage |= WINED3DUSAGE_OVERLAY;
4964     }
4965
4966     if (ddraw->depthstencil || (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
4967     {
4968         /* The depth stencil creation callback sets this flag. Set the
4969          * wined3d usage to let it know it's a depth/stencil surface. */
4970         usage |= WINED3DUSAGE_DEPTHSTENCIL;
4971     }
4972
4973     if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4974     {
4975         pool = WINED3DPOOL_SYSTEMMEM;
4976     }
4977     else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
4978     {
4979         pool = WINED3DPOOL_MANAGED;
4980         /* Managed textures have the system memory flag set. */
4981         desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
4982     }
4983     else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
4984     {
4985         /* Videomemory adds localvidmem. This is mutually exclusive with
4986          * systemmemory and texturemanage. */
4987         desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
4988     }
4989
4990     format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
4991     if (format == WINED3DFMT_UNKNOWN)
4992     {
4993         WARN("Unsupported / unknown pixelformat.\n");
4994         return DDERR_INVALIDPIXELFORMAT;
4995     }
4996
4997     surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
4998     surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
4999     surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
5000     surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
5001     surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
5002     surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
5003     surface->IDirect3DTexture2_vtbl = &d3d_texture2_vtbl;
5004     surface->IDirect3DTexture_vtbl = &d3d_texture1_vtbl;
5005     surface->ref = 1;
5006     surface->version = 7;
5007     surface->ddraw = ddraw;
5008
5009     copy_to_surfacedesc2(&surface->surface_desc, desc);
5010
5011     surface->first_attached = surface;
5012     surface->ImplType = surface_type;
5013
5014     hr = wined3d_surface_create(ddraw->wined3d_device, desc->dwWidth, desc->dwHeight, format,
5015             TRUE /* Lockable */, FALSE /* Discard */, mip_level, usage, pool,
5016             WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, surface_type, surface,
5017             &ddraw_surface_wined3d_parent_ops, &surface->wined3d_surface);
5018     if (FAILED(hr))
5019     {
5020         WARN("Failed to create wined3d surface, hr %#x.\n", hr);
5021         return hr;
5022     }
5023
5024     surface->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
5025     wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
5026     wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
5027
5028     format = wined3d_desc.format;
5029     if (format == WINED3DFMT_UNKNOWN)
5030     {
5031         FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN.\n");
5032     }
5033     PixelFormat_WineD3DtoDD(&surface->surface_desc.u4.ddpfPixelFormat, format);
5034
5035     /* Anno 1602 stores the pitch right after surface creation, so make sure
5036      * it's there. TODO: Test other fourcc formats. */
5037     if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
5038             || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
5039     {
5040         surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
5041         if (format == WINED3DFMT_DXT1)
5042         {
5043             surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height) / 2;
5044         }
5045         else
5046         {
5047             surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height);
5048         }
5049     }
5050     else
5051     {
5052         surface->surface_desc.dwFlags |= DDSD_PITCH;
5053         surface->surface_desc.u1.lPitch = wined3d_surface_get_pitch(surface->wined3d_surface);
5054     }
5055
5056     if (desc->dwFlags & DDSD_CKDESTOVERLAY)
5057     {
5058         wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTOVERLAY,
5059                 (WINEDDCOLORKEY *)&desc->u3.ddckCKDestOverlay);
5060     }
5061     if (desc->dwFlags & DDSD_CKDESTBLT)
5062     {
5063         wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTBLT,
5064                 (WINEDDCOLORKEY *)&desc->ddckCKDestBlt);
5065     }
5066     if (desc->dwFlags & DDSD_CKSRCOVERLAY)
5067     {
5068         wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCOVERLAY,
5069                 (WINEDDCOLORKEY *)&desc->ddckCKSrcOverlay);
5070     }
5071     if (desc->dwFlags & DDSD_CKSRCBLT)
5072     {
5073         wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCBLT,
5074                 (WINEDDCOLORKEY *)&desc->ddckCKSrcBlt);
5075     }
5076     if (desc->dwFlags & DDSD_LPSURFACE)
5077     {
5078         hr = wined3d_surface_set_mem(surface->wined3d_surface, desc->lpSurface);
5079         if (FAILED(hr))
5080         {
5081             ERR("Failed to set surface memory, hr %#x.\n", hr);
5082             wined3d_surface_decref(surface->wined3d_surface);
5083             return hr;
5084         }
5085     }
5086
5087     return DD_OK;
5088 }