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