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