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