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