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