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