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