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