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