user32: Mark internal functions with hidden visibility.
[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  *
8  * This file contains the (internal) driver registration functions,
9  * driver enumeration APIs and DirectDraw creation functions.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include "config.h"
27 #include "wine/port.h"
28
29 #include "ddraw_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
32
33 static inline IDirectDrawSurfaceImpl *surface_from_gamma_control(IDirectDrawGammaControl *iface)
34 {
35     return (IDirectDrawSurfaceImpl *)((char*)iface
36             - FIELD_OFFSET(IDirectDrawSurfaceImpl, IDirectDrawGammaControl_vtbl));
37 }
38
39 /*****************************************************************************
40  * IUnknown parts follow
41  *****************************************************************************/
42
43 /*****************************************************************************
44  * IDirectDrawSurface7::QueryInterface
45  *
46  * A normal QueryInterface implementation. For QueryInterface rules
47  * see ddraw.c, IDirectDraw7::QueryInterface. This method
48  * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
49  * in all versions, the IDirectDrawGammaControl interface and it can
50  * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
51  *
52  * Params:
53  *  riid: The interface id queried for
54  *  obj: Address to write the pointer to
55  *
56  * Returns:
57  *  S_OK on success
58  *  E_NOINTERFACE if the requested interface wasn't found
59  *
60  *****************************************************************************/
61 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
62 {
63     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
64
65     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
66
67     /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
68     *obj = NULL;
69
70     if(!riid)
71         return DDERR_INVALIDPARAMS;
72
73     if (IsEqualGUID(riid, &IID_IUnknown)
74      || IsEqualGUID(riid, &IID_IDirectDrawSurface7)
75      || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
76     {
77         IUnknown_AddRef(iface);
78         *obj = iface;
79         TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
80         return S_OK;
81     }
82     else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
83           || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
84           || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
85     {
86         IUnknown_AddRef(iface);
87         *obj = &This->IDirectDrawSurface3_vtbl;
88         TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
89         return S_OK;
90     }
91     else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
92     {
93         IUnknown_AddRef(iface);
94         *obj = &This->IDirectDrawGammaControl_vtbl;
95         TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
96         return S_OK;
97     }
98     else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
99              IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
100              IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
101     {
102         IDirect3DDevice7 *d3d;
103
104         /* Call into IDirect3D7 for creation */
105         IDirect3D7_CreateDevice(&This->ddraw->IDirect3D7_iface, riid, (IDirectDrawSurface7 *)This,
106                 &d3d);
107
108         if (d3d)
109         {
110             *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
111             TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
112             return S_OK;
113         }
114
115         WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
116         return E_NOINTERFACE;
117     }
118     else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
119              IsEqualGUID( &IID_IDirect3DTexture2, riid ))
120     {
121         if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
122         {
123             *obj = &This->IDirect3DTexture_vtbl;
124             TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
125         }
126         else
127         {
128             *obj = &This->IDirect3DTexture2_vtbl;
129             TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
130         }
131         IUnknown_AddRef( (IUnknown *) *obj);
132         return S_OK;
133     }
134
135     ERR("No interface\n");
136     return E_NOINTERFACE;
137 }
138
139 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
140 {
141     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
142
143     return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), riid, object);
144 }
145
146 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface, REFIID riid, void **object)
147 {
148     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
149
150     return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_gamma_control(iface), riid, object);
151 }
152
153 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
154 {
155     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
156
157     return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_texture2(iface), riid, object);
158 }
159
160 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
161 {
162     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
163
164     return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_texture1(iface), riid, object);
165 }
166
167 /*****************************************************************************
168  * IDirectDrawSurface7::AddRef
169  *
170  * A normal addref implementation
171  *
172  * Returns:
173  *  The new refcount
174  *
175  *****************************************************************************/
176 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
177 {
178     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
179     ULONG refCount = InterlockedIncrement(&This->ref);
180
181     TRACE("%p increasing refcount to %u.\n", This, refCount);
182
183     if (refCount == 1)
184     {
185         EnterCriticalSection(&ddraw_cs);
186         if (This->WineD3DSurface)
187             IWineD3DSurface_AddRef(This->WineD3DSurface);
188         if (This->wined3d_texture)
189             wined3d_texture_incref(This->wined3d_texture);
190         LeaveCriticalSection(&ddraw_cs);
191     }
192
193     return refCount;
194 }
195
196 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
197 {
198     TRACE("iface %p.\n", iface);
199
200     return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_surface3(iface));
201 }
202
203 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
204 {
205     TRACE("iface %p.\n", iface);
206
207     return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_gamma_control(iface));
208 }
209
210 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
211 {
212     TRACE("iface %p.\n", iface);
213
214     return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_texture2(iface));
215 }
216
217 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
218 {
219     TRACE("iface %p.\n", iface);
220
221     return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_texture1(iface));
222 }
223
224 /*****************************************************************************
225  * ddraw_surface_destroy
226  *
227  * A helper function for IDirectDrawSurface7::Release
228  *
229  * Frees the surface, regardless of its refcount.
230  *  See IDirectDrawSurface7::Release for more information
231  *
232  * Params:
233  *  This: Surface to free
234  *
235  *****************************************************************************/
236 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
237 {
238     TRACE("surface %p.\n", This);
239
240     /* Check the refcount and give a warning */
241     if(This->ref > 1)
242     {
243         /* This can happen when a complex surface is destroyed,
244          * because the 2nd surface was addref()ed when the app
245          * called GetAttachedSurface
246          */
247         WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
248     }
249
250     if (This->WineD3DSurface)
251         IWineD3DSurface_Release(This->WineD3DSurface);
252 }
253
254 static void ddraw_surface_cleanup(IDirectDrawSurfaceImpl *surface)
255 {
256     IDirectDrawSurfaceImpl *surf;
257     IUnknown *ifaceToRelease;
258     UINT i;
259
260     TRACE("surface %p.\n", surface);
261
262     if (surface->wined3d_swapchain)
263     {
264         IDirectDrawImpl *ddraw = surface->ddraw;
265
266         /* If it's the render target, destroy the D3D device. */
267         if (ddraw->d3d_initialized && surface == ddraw->d3d_target)
268         {
269             TRACE("Destroying the render target, uninitializing D3D.\n");
270
271             for (i = 0; i < ddraw->numConvertedDecls; ++i)
272             {
273                 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
274             }
275             HeapFree(GetProcessHeap(), 0, ddraw->decls);
276             ddraw->numConvertedDecls = 0;
277
278             if (FAILED(IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice)))
279             {
280                 ERR("Failed to uninit 3D.\n");
281             }
282             else
283             {
284                 /* Free the d3d window if one was created. */
285                 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
286                 {
287                     TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
288                     DestroyWindow(ddraw->d3d_window);
289                     ddraw->d3d_window = 0;
290                 }
291             }
292
293             ddraw->d3d_initialized = FALSE;
294             ddraw->d3d_target = NULL;
295         }
296         else
297         {
298             IWineD3DDevice_UninitGDI(ddraw->wineD3DDevice);
299         }
300
301         surface->wined3d_swapchain = NULL;
302
303         /* Reset to the default surface implementation type. This is needed
304          * if applications use non render target surfaces and expect blits to
305          * work after destroying the render target.
306          *
307          * TODO: Recreate existing offscreen surfaces. */
308         ddraw->ImplType = DefaultSurfaceType;
309
310         TRACE("D3D unloaded.\n");
311     }
312
313     /* The refcount test shows that the palette is detached when the surface
314      * is destroyed. */
315     IDirectDrawSurface7_SetPalette((IDirectDrawSurface7 *)surface, NULL);
316
317     /* Loop through all complex attached surfaces and destroy them.
318      *
319      * Yet again, only the root can have more than one complexly attached
320      * surface, all the others have a total of one. */
321     for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
322     {
323         if (!surface->complex_array[i])
324             break;
325
326         surf = surface->complex_array[i];
327         surface->complex_array[i] = NULL;
328         while (surf)
329         {
330             IDirectDrawSurfaceImpl *destroy = surf;
331             surf = surf->complex_array[0];              /* Iterate through the "tree" */
332             ddraw_surface_destroy(destroy);             /* Destroy it */
333         }
334     }
335
336     ifaceToRelease = surface->ifaceToRelease;
337
338     /* Destroy the root surface. */
339     ddraw_surface_destroy(surface);
340
341     /* Reduce the ddraw refcount */
342     if (ifaceToRelease)
343         IUnknown_Release(ifaceToRelease);
344 }
345
346 /*****************************************************************************
347  * IDirectDrawSurface7::Release
348  *
349  * Reduces the surface's refcount by 1. If the refcount falls to 0, the
350  * surface is destroyed.
351  *
352  * Destroying the surface is a bit tricky. For the connection between
353  * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
354  * It has a nice graph explaining the connection.
355  *
356  * What happens here is basically this:
357  * When a surface is destroyed, its WineD3DSurface is released,
358  * and the refcount of the DirectDraw interface is reduced by 1. If it has
359  * complex surfaces attached to it, then these surfaces are destroyed too,
360  * regardless of their refcount. If any surface being destroyed has another
361  * surface attached to it (with a "soft" attachment, not complex), then
362  * this surface is detached with DeleteAttachedSurface.
363  *
364  * When the surface is a texture, the WineD3DTexture is released.
365  * If the surface is the Direct3D render target, then the D3D
366  * capabilities of the WineD3DDevice are uninitialized, which causes the
367  * swapchain to be released.
368  *
369  * When a complex sublevel falls to ref zero, then this is ignored.
370  *
371  * Returns:
372  *  The new refcount
373  *
374  *****************************************************************************/
375 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
376 {
377     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
378     ULONG ref = InterlockedDecrement(&This->ref);
379
380     TRACE("%p decreasing refcount to %u.\n", This, ref);
381
382     if (ref == 0)
383     {
384         /* Complex attached surfaces are destroyed implicitly when the root is released */
385         EnterCriticalSection(&ddraw_cs);
386         if(!This->is_complex_root)
387         {
388             WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
389             LeaveCriticalSection(&ddraw_cs);
390             return ref;
391         }
392         if (This->wined3d_texture) /* If it's a texture, destroy the wined3d texture. */
393             wined3d_texture_decref(This->wined3d_texture);
394         else
395             ddraw_surface_cleanup(This);
396         LeaveCriticalSection(&ddraw_cs);
397     }
398
399     return ref;
400 }
401
402 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
403 {
404     TRACE("iface %p.\n", iface);
405
406     return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_surface3(iface));
407 }
408
409 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
410 {
411     TRACE("iface %p.\n", iface);
412
413     return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_gamma_control(iface));
414 }
415
416 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
417 {
418     TRACE("iface %p.\n", iface);
419
420     return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_texture2(iface));
421 }
422
423 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
424 {
425     TRACE("iface %p.\n", iface);
426
427     return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_texture1(iface));
428 }
429
430 /*****************************************************************************
431  * IDirectDrawSurface7::GetAttachedSurface
432  *
433  * Returns an attached surface with the requested caps. Surface attachment
434  * and complex surfaces are not clearly described by the MSDN or sdk,
435  * so this method is tricky and likely to contain problems.
436  * This implementation searches the complex list first, then the
437  * attachment chain.
438  *
439  * The chains are searched from This down to the last surface in the chain,
440  * not from the first element in the chain. The first surface found is
441  * returned. The MSDN says that this method fails if more than one surface
442  * matches the caps, but it is not sure if that is right. The attachment
443  * structure may not even allow two matching surfaces.
444  *
445  * The found surface is AddRef-ed before it is returned.
446  *
447  * Params:
448  *  Caps: Pointer to a DDCAPS2 structure describing the caps asked for
449  *  Surface: Address to store the found surface
450  *
451  * Returns:
452  *  DD_OK on success
453  *  DDERR_INVALIDPARAMS if Caps or Surface is NULL
454  *  DDERR_NOTFOUND if no surface was found
455  *
456  *****************************************************************************/
457 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
458         DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
459 {
460     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
461     IDirectDrawSurfaceImpl *surf;
462     DDSCAPS2 our_caps;
463     int i;
464
465     TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
466
467     EnterCriticalSection(&ddraw_cs);
468
469     if(This->version < 7)
470     {
471         /* Earlier dx apps put garbage into these members, clear them */
472         our_caps.dwCaps = Caps->dwCaps;
473         our_caps.dwCaps2 = 0;
474         our_caps.dwCaps3 = 0;
475         our_caps.dwCaps4 = 0;
476     }
477     else
478     {
479         our_caps = *Caps;
480     }
481
482     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 */
483
484     for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
485     {
486         surf = This->complex_array[i];
487         if(!surf) break;
488
489         if (TRACE_ON(ddraw))
490         {
491             TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
492                    surf->surface_desc.ddsCaps.dwCaps,
493                    surf->surface_desc.ddsCaps.dwCaps2,
494                    surf->surface_desc.ddsCaps.dwCaps3,
495                    surf->surface_desc.ddsCaps.dwCaps4);
496         }
497
498         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
499             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
500
501             /* MSDN: "This method fails if more than one surface is attached
502              * that matches the capabilities requested."
503              *
504              * Not sure how to test this.
505              */
506
507             TRACE("(%p): Returning surface %p\n", This, surf);
508             TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
509             *Surface = (IDirectDrawSurface7 *)surf;
510             ddraw_surface7_AddRef(*Surface);
511             LeaveCriticalSection(&ddraw_cs);
512             return DD_OK;
513         }
514     }
515
516     /* Next, look at the attachment chain */
517     surf = This;
518
519     while( (surf = surf->next_attached) )
520     {
521         if (TRACE_ON(ddraw))
522         {
523             TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
524                    surf->surface_desc.ddsCaps.dwCaps,
525                    surf->surface_desc.ddsCaps.dwCaps2,
526                    surf->surface_desc.ddsCaps.dwCaps3,
527                    surf->surface_desc.ddsCaps.dwCaps4);
528         }
529
530         if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
531             ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
532
533             TRACE("(%p): Returning surface %p\n", This, surf);
534             *Surface = (IDirectDrawSurface7 *)surf;
535             ddraw_surface7_AddRef(*Surface);
536             LeaveCriticalSection(&ddraw_cs);
537             return DD_OK;
538         }
539     }
540
541     TRACE("(%p) Didn't find a valid surface\n", This);
542     LeaveCriticalSection(&ddraw_cs);
543
544     *Surface = NULL;
545     return DDERR_NOTFOUND;
546 }
547
548 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
549         DDSCAPS *caps, IDirectDrawSurface3 **attachment)
550 {
551     IDirectDrawSurface7 *attachment7;
552     DDSCAPS2 caps2;
553     HRESULT hr;
554
555     TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
556
557     caps2.dwCaps  = caps->dwCaps;
558     caps2.dwCaps2 = 0;
559     caps2.dwCaps3 = 0;
560     caps2.dwCaps4 = 0;
561
562     hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface),
563             &caps2, &attachment7);
564     if (FAILED(hr)) *attachment = NULL;
565     else *attachment = attachment7 ?
566             (IDirectDrawSurface3 *)&((IDirectDrawSurfaceImpl *)attachment7)->IDirectDrawSurface3_vtbl : NULL;
567
568     return hr;
569 }
570
571 /*****************************************************************************
572  * IDirectDrawSurface7::Lock
573  *
574  * Locks the surface and returns a pointer to the surface's memory
575  *
576  * Params:
577  *  Rect: Rectangle to lock. If NULL, the whole surface is locked
578  *  DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
579  *  Flags: Locking flags, e.g Read only or write only
580  *  h: An event handle that's not used and must be NULL
581  *
582  * Returns:
583  *  DD_OK on success
584  *  DDERR_INVALIDPARAMS if DDSD is NULL
585  *  For more details, see IWineD3DSurface::LockRect
586  *
587  *****************************************************************************/
588 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
589         RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
590 {
591     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
592     WINED3DLOCKED_RECT LockedRect;
593     HRESULT hr;
594
595     TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
596             iface, wine_dbgstr_rect(Rect), DDSD, Flags, h);
597
598     if(!DDSD)
599         return DDERR_INVALIDPARAMS;
600
601     /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
602     EnterCriticalSection(&ddraw_cs);
603
604     /* Should I check for the handle to be NULL?
605      *
606      * The DDLOCK flags and the D3DLOCK flags are equal
607      * for the supported values. The others are ignored by WineD3D
608      */
609
610     if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
611        DDSD->dwSize != sizeof(DDSURFACEDESC2))
612     {
613         WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
614         LeaveCriticalSection(&ddraw_cs);
615         return DDERR_INVALIDPARAMS;
616     }
617
618     /* Windows zeroes this if the rect is invalid */
619     DDSD->lpSurface = 0;
620
621     if (Rect)
622     {
623         if ((Rect->left < 0)
624                 || (Rect->top < 0)
625                 || (Rect->left > Rect->right)
626                 || (Rect->top > Rect->bottom)
627                 || (Rect->right > This->surface_desc.dwWidth)
628                 || (Rect->bottom > This->surface_desc.dwHeight))
629         {
630             WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
631             LeaveCriticalSection(&ddraw_cs);
632             return DDERR_INVALIDPARAMS;
633         }
634     }
635
636     hr = IWineD3DSurface_Map(This->WineD3DSurface, &LockedRect, Rect, Flags);
637     if (FAILED(hr))
638     {
639         LeaveCriticalSection(&ddraw_cs);
640         switch(hr)
641         {
642             /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
643              * specific error. But since IWineD3DSurface::LockRect returns that error in this
644              * only occasion, keep d3d8 and d3d9 free from the return value override. There are
645              * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
646              * is much easier to do it in one place in ddraw
647              */
648             case WINED3DERR_INVALIDCALL:    return DDERR_SURFACEBUSY;
649             default:                        return hr;
650         }
651     }
652
653     /* Override the memory area. The pitch should be set already. Strangely windows
654      * does not set the LPSURFACE flag on locked surfaces !?!.
655      * DDSD->dwFlags |= DDSD_LPSURFACE;
656      */
657     This->surface_desc.lpSurface = LockedRect.pBits;
658     DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
659
660     TRACE("locked surface returning description :\n");
661     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
662
663     LeaveCriticalSection(&ddraw_cs);
664     return DD_OK;
665 }
666
667 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
668         DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
669 {
670     TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
671             iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
672
673     return ddraw_surface7_Lock((IDirectDrawSurface7 *)surface_from_surface3(iface),
674             rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
675 }
676
677 /*****************************************************************************
678  * IDirectDrawSurface7::Unlock
679  *
680  * Unlocks an locked surface
681  *
682  * Params:
683  *  Rect: Not used by this implementation
684  *
685  * Returns:
686  *  D3D_OK on success
687  *  For more details, see IWineD3DSurface::UnlockRect
688  *
689  *****************************************************************************/
690 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
691 {
692     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
693     HRESULT hr;
694
695     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
696
697     EnterCriticalSection(&ddraw_cs);
698     hr = IWineD3DSurface_Unmap(This->WineD3DSurface);
699     if (SUCCEEDED(hr))
700     {
701         This->surface_desc.lpSurface = NULL;
702     }
703     LeaveCriticalSection(&ddraw_cs);
704     return hr;
705 }
706
707 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
708 {
709     TRACE("iface %p, data %p.\n", iface, data);
710
711     /* data might not be the LPRECT of later versions, so drop it. */
712     return ddraw_surface7_Unlock((IDirectDrawSurface7 *)surface_from_surface3(iface), NULL);
713 }
714
715 /*****************************************************************************
716  * IDirectDrawSurface7::Flip
717  *
718  * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
719  * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
720  * the flip target is passed to WineD3D, even if the app didn't specify one
721  *
722  * Params:
723  *  DestOverride: Specifies the surface that will become the new front
724  *                buffer. If NULL, the current back buffer is used
725  *  Flags: some DirectDraw flags, see include/ddraw.h
726  *
727  * Returns:
728  *  DD_OK on success
729  *  DDERR_NOTFLIPPABLE if no flip target could be found
730  *  DDERR_INVALIDOBJECT if the surface isn't a front buffer
731  *  For more details, see IWineD3DSurface::Flip
732  *
733  *****************************************************************************/
734 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
735 {
736     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
737     IDirectDrawSurfaceImpl *Override = (IDirectDrawSurfaceImpl *)DestOverride;
738     IDirectDrawSurface7 *Override7;
739     HRESULT hr;
740
741     TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
742
743     /* Flip has to be called from a front buffer
744      * What about overlay surfaces, AFAIK they can flip too?
745      */
746     if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
747         return DDERR_INVALIDOBJECT; /* Unchecked */
748
749     EnterCriticalSection(&ddraw_cs);
750
751     /* WineD3D doesn't keep track of attached surface, so find the target */
752     if(!Override)
753     {
754         DDSCAPS2 Caps;
755
756         memset(&Caps, 0, sizeof(Caps));
757         Caps.dwCaps |= DDSCAPS_BACKBUFFER;
758         hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
759         if(hr != DD_OK)
760         {
761             ERR("Can't find a flip target\n");
762             LeaveCriticalSection(&ddraw_cs);
763             return DDERR_NOTFLIPPABLE; /* Unchecked */
764         }
765         Override = (IDirectDrawSurfaceImpl *)Override7;
766
767         /* For the GetAttachedSurface */
768         ddraw_surface7_Release(Override7);
769     }
770
771     hr = IWineD3DSurface_Flip(This->WineD3DSurface,
772                               Override->WineD3DSurface,
773                               Flags);
774     LeaveCriticalSection(&ddraw_cs);
775     return hr;
776 }
777
778 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
779 {
780     TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
781
782     return ddraw_surface7_Flip((IDirectDrawSurface7 *)surface_from_surface3(iface),
783             dst ? (IDirectDrawSurface7 *)surface_from_surface3(dst) : NULL, flags);
784 }
785
786 /*****************************************************************************
787  * IDirectDrawSurface7::Blt
788  *
789  * Performs a blit on the surface
790  *
791  * Params:
792  *  DestRect: Destination rectangle, can be NULL
793  *  SrcSurface: Source surface, can be NULL
794  *  SrcRect: Source rectangle, can be NULL
795  *  Flags: Blt flags
796  *  DDBltFx: Some extended blt parameters, connected to the flags
797  *
798  * Returns:
799  *  D3D_OK on success
800  *  See IWineD3DSurface::Blt for more details
801  *
802  *****************************************************************************/
803 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
804         IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
805 {
806     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
807     IDirectDrawSurfaceImpl *Src = (IDirectDrawSurfaceImpl *)SrcSurface;
808     HRESULT hr;
809
810     TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
811             iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
812
813     /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
814      * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
815      */
816     if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
817         WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
818         return DDERR_INVALIDPARAMS;
819     }
820
821     if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
822         WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
823         return DDERR_INVALIDPARAMS;
824     }
825
826     /* Sizes can change, therefore hold the lock when testing the rectangles */
827     EnterCriticalSection(&ddraw_cs);
828     if(DestRect)
829     {
830         if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
831            DestRect->right > This->surface_desc.dwWidth ||
832            DestRect->bottom > This->surface_desc.dwHeight)
833         {
834             WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
835             LeaveCriticalSection(&ddraw_cs);
836             return DDERR_INVALIDRECT;
837         }
838     }
839     if(Src && SrcRect)
840     {
841         if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
842            SrcRect->right > Src->surface_desc.dwWidth ||
843            SrcRect->bottom > Src->surface_desc.dwHeight)
844         {
845             WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
846             LeaveCriticalSection(&ddraw_cs);
847             return DDERR_INVALIDRECT;
848         }
849     }
850
851     if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
852         WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
853         LeaveCriticalSection(&ddraw_cs);
854         return DDERR_INVALIDPARAMS;
855     }
856
857     /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it does, copy the struct,
858      * and replace the ddraw surfaces with the wined3d surfaces
859      * So far no blitting operations using surfaces in the bltfx struct are supported anyway.
860      */
861     hr = IWineD3DSurface_Blt(This->WineD3DSurface, DestRect, Src ? Src->WineD3DSurface : NULL,
862             SrcRect, Flags, (WINEDDBLTFX *)DDBltFx, WINED3DTEXF_LINEAR);
863
864     LeaveCriticalSection(&ddraw_cs);
865     switch(hr)
866     {
867         case WINED3DERR_NOTAVAILABLE:       return DDERR_UNSUPPORTED;
868         case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
869         default:                            return hr;
870     }
871 }
872
873 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
874         IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
875 {
876     TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
877             iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
878
879     return ddraw_surface7_Blt((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_rect,
880             src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags, fx);
881 }
882
883 /*****************************************************************************
884  * IDirectDrawSurface7::AddAttachedSurface
885  *
886  * Attaches a surface to another surface. How the surface attachments work
887  * is not totally understood yet, and this method is prone to problems.
888  * he surface that is attached is AddRef-ed.
889  *
890  * Tests with complex surfaces suggest that the surface attachments form a
891  * tree, but no method to test this has been found yet.
892  *
893  * The attachment list consists of a first surface (first_attached) and
894  * for each surface a pointer to the next attached surface (next_attached).
895  * For the first surface, and a surface that has no attachments
896  * first_attached points to the surface itself. A surface that has
897  * no successors in the chain has next_attached set to NULL.
898  *
899  * Newly attached surfaces are attached right after the root surface.
900  * If a surface is attached to a complex surface compound, it's attached to
901  * the surface that the app requested, not the complex root. See
902  * GetAttachedSurface for a description how surfaces are found.
903  *
904  * This is how the current implementation works, and it was coded by looking
905  * at the needs of the applications.
906  *
907  * So far only Z-Buffer attachments are tested, and they are activated in
908  * WineD3D. Mipmaps could be tricky to activate in WineD3D.
909  * Back buffers should work in 2D mode, but they are not tested(They can be
910  * attached in older iface versions). Rendering to the front buffer and
911  * switching between that and double buffering is not yet implemented in
912  * WineD3D, so for 3D it might have unexpected results.
913  *
914  * ddraw_surface_attach_surface is the real thing,
915  * ddraw_surface7_AddAttachedSurface is a wrapper around it that
916  * performs additional checks. Version 7 of this interface is much more restrictive
917  * than its predecessors.
918  *
919  * Params:
920  *  Attach: Surface to attach to iface
921  *
922  * Returns:
923  *  DD_OK on success
924  *  DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
925  *
926  *****************************************************************************/
927 static HRESULT ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
928 {
929     TRACE("surface %p, attachment %p.\n", This, Surf);
930
931     if(Surf == This)
932         return DDERR_CANNOTATTACHSURFACE; /* unchecked */
933
934     EnterCriticalSection(&ddraw_cs);
935
936     /* Check if the surface is already attached somewhere */
937     if (Surf->next_attached || Surf->first_attached != Surf)
938     {
939         /* TODO: Test for the structure of the manual attachment. Is it a
940          * chain or a list? What happens if one surface is attached to 2
941          * different surfaces? */
942         WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
943                 Surf, Surf->next_attached, Surf->first_attached);
944
945         LeaveCriticalSection(&ddraw_cs);
946         return DDERR_SURFACEALREADYATTACHED;
947     }
948
949     /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
950     Surf->next_attached = This->next_attached;
951     Surf->first_attached = This->first_attached;
952     This->next_attached = Surf;
953
954     /* Check if the WineD3D depth stencil needs updating */
955     if(This->ddraw->d3ddevice)
956     {
957         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
958     }
959
960     ddraw_surface7_AddRef((IDirectDrawSurface7 *)Surf);
961     LeaveCriticalSection(&ddraw_cs);
962     return DD_OK;
963 }
964
965 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
966 {
967     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
968     IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
969
970     TRACE("iface %p, attachment %p.\n", iface, Attach);
971
972     /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
973     if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
974     {
975
976         WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
977               Surf->surface_desc.ddsCaps.dwCaps);
978         return DDERR_CANNOTATTACHSURFACE;
979     }
980
981     return ddraw_surface_attach_surface(This, Surf);
982 }
983
984 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
985 {
986     IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
987     IDirectDrawSurfaceImpl *attach_impl = surface_from_surface3(attachment);
988
989     TRACE("iface %p, attachment %p.\n", iface, attachment);
990
991     /* Tests suggest that
992      * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
993      * -> offscreen plain surfaces can be attached to primaries
994      * -> primaries can be attached to offscreen plain surfaces
995      * -> z buffers can be attached to primaries */
996     if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
997             && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
998     {
999         /* Sizes have to match */
1000         if (attach_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
1001                 || attach_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
1002         {
1003             WARN("Surface sizes do not match.\n");
1004             return DDERR_CANNOTATTACHSURFACE;
1005         }
1006         /* OK */
1007     }
1008     else if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1009             && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1010     {
1011         /* OK */
1012     }
1013     else
1014     {
1015         WARN("Invalid attachment combination.\n");
1016         return DDERR_CANNOTATTACHSURFACE;
1017     }
1018
1019     return ddraw_surface_attach_surface(surface, attach_impl);
1020 }
1021
1022 /*****************************************************************************
1023  * IDirectDrawSurface7::DeleteAttachedSurface
1024  *
1025  * Removes a surface from the attachment chain. The surface's refcount
1026  * is decreased by one after it has been removed
1027  *
1028  * Params:
1029  *  Flags: Some flags, not used by this implementation
1030  *  Attach: Surface to detach
1031  *
1032  * Returns:
1033  *  DD_OK on success
1034  *  DDERR_SURFACENOTATTACHED if the surface isn't attached to
1035  *
1036  *****************************************************************************/
1037 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1038         DWORD Flags, IDirectDrawSurface7 *Attach)
1039 {
1040     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1041     IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
1042     IDirectDrawSurfaceImpl *Prev = This;
1043
1044     TRACE("iface %p, flags %#x, attachment %p.\n", iface, Flags, Attach);
1045
1046     EnterCriticalSection(&ddraw_cs);
1047     if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1048     {
1049         LeaveCriticalSection(&ddraw_cs);
1050         return DDERR_CANNOTDETACHSURFACE;
1051     }
1052
1053     /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1054     if (This->surface_desc.ddsCaps.dwCaps &
1055         Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1056     {
1057         Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1058         /* FIXME: we should probably also subtract from dwMipMapCount of this
1059          * and all parent surfaces */
1060     }
1061
1062     /* Find the predecessor of the detached surface */
1063     while(Prev)
1064     {
1065         if(Prev->next_attached == Surf) break;
1066         Prev = Prev->next_attached;
1067     }
1068
1069     /* There must be a surface, otherwise there's a bug */
1070     assert(Prev != NULL);
1071
1072     /* Unchain the surface */
1073     Prev->next_attached = Surf->next_attached;
1074     Surf->next_attached = NULL;
1075     Surf->first_attached = Surf;
1076
1077     /* Check if the WineD3D depth stencil needs updating */
1078     if(This->ddraw->d3ddevice)
1079     {
1080         IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1081     }
1082
1083     ddraw_surface7_Release(Attach);
1084     LeaveCriticalSection(&ddraw_cs);
1085     return DD_OK;
1086 }
1087
1088 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1089         DWORD flags, IDirectDrawSurface3 *attachment)
1090 {
1091     TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1092
1093     return ddraw_surface7_DeleteAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
1094             attachment ? (IDirectDrawSurface7 *)surface_from_surface3(attachment) : NULL);
1095 }
1096
1097 /*****************************************************************************
1098  * IDirectDrawSurface7::AddOverlayDirtyRect
1099  *
1100  * "This method is not currently implemented"
1101  *
1102  * Params:
1103  *  Rect: ?
1104  *
1105  * Returns:
1106  *  DDERR_UNSUPPORTED
1107  *
1108  *****************************************************************************/
1109 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1110 {
1111     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1112
1113     return DDERR_UNSUPPORTED; /* unchecked */
1114 }
1115
1116 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1117 {
1118     TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1119
1120     return ddraw_surface7_AddOverlayDirtyRect((IDirectDrawSurface7 *)surface_from_surface3(iface), rect);
1121 }
1122
1123 /*****************************************************************************
1124  * IDirectDrawSurface7::GetDC
1125  *
1126  * Returns a GDI device context for the surface
1127  *
1128  * Params:
1129  *  hdc: Address of a HDC variable to store the dc to
1130  *
1131  * Returns:
1132  *  DD_OK on success
1133  *  DDERR_INVALIDPARAMS if hdc is NULL
1134  *  For details, see IWineD3DSurface::GetDC
1135  *
1136  *****************************************************************************/
1137 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1138 {
1139     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1140     HRESULT hr;
1141
1142     TRACE("iface %p, dc %p.\n", iface, hdc);
1143
1144     if(!hdc)
1145         return DDERR_INVALIDPARAMS;
1146
1147     EnterCriticalSection(&ddraw_cs);
1148     hr = IWineD3DSurface_GetDC(This->WineD3DSurface,
1149                                hdc);
1150     LeaveCriticalSection(&ddraw_cs);
1151     switch(hr)
1152     {
1153         /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1154          * touch *hdc
1155          */
1156         case WINED3DERR_INVALIDCALL:
1157             if(hdc) *hdc = NULL;
1158             return DDERR_INVALIDPARAMS;
1159
1160         default: return hr;
1161     }
1162 }
1163
1164 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1165 {
1166     TRACE("iface %p, dc %p.\n", iface, dc);
1167
1168     return ddraw_surface7_GetDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1169 }
1170
1171 /*****************************************************************************
1172  * IDirectDrawSurface7::ReleaseDC
1173  *
1174  * Releases the DC that was constructed with GetDC
1175  *
1176  * Params:
1177  *  hdc: HDC to release
1178  *
1179  * Returns:
1180  *  DD_OK on success
1181  *  For more details, see IWineD3DSurface::ReleaseDC
1182  *
1183  *****************************************************************************/
1184 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1185 {
1186     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1187     HRESULT hr;
1188
1189     TRACE("iface %p, dc %p.\n", iface, hdc);
1190
1191     EnterCriticalSection(&ddraw_cs);
1192     hr = IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
1193     LeaveCriticalSection(&ddraw_cs);
1194     return hr;
1195 }
1196
1197 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1198 {
1199     TRACE("iface %p, dc %p.\n", iface, dc);
1200
1201     return ddraw_surface7_ReleaseDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1202 }
1203
1204 /*****************************************************************************
1205  * IDirectDrawSurface7::GetCaps
1206  *
1207  * Returns the surface's caps
1208  *
1209  * Params:
1210  *  Caps: Address to write the caps to
1211  *
1212  * Returns:
1213  *  DD_OK on success
1214  *  DDERR_INVALIDPARAMS if Caps is NULL
1215  *
1216  *****************************************************************************/
1217 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1218 {
1219     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1220
1221     TRACE("iface %p, caps %p.\n", iface, Caps);
1222
1223     if(!Caps)
1224         return DDERR_INVALIDPARAMS;
1225
1226     *Caps = This->surface_desc.ddsCaps;
1227     return DD_OK;
1228 }
1229
1230 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1231 {
1232     DDSCAPS2 caps2;
1233     HRESULT hr;
1234
1235     TRACE("iface %p, caps %p.\n", iface, caps);
1236
1237     hr = ddraw_surface7_GetCaps((IDirectDrawSurface7 *)surface_from_surface3(iface), &caps2);
1238     if (FAILED(hr)) return hr;
1239
1240     caps->dwCaps = caps2.dwCaps;
1241     return hr;
1242 }
1243
1244 /*****************************************************************************
1245  * IDirectDrawSurface7::SetPriority
1246  *
1247  * Sets a texture priority for managed textures.
1248  *
1249  * Params:
1250  *  Priority: The new priority
1251  *
1252  * Returns:
1253  *  DD_OK on success
1254  *  For more details, see IWineD3DSurface::SetPriority
1255  *
1256  *****************************************************************************/
1257 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1258 {
1259     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1260     HRESULT hr;
1261
1262     TRACE("iface %p, priority %u.\n", iface, Priority);
1263
1264     EnterCriticalSection(&ddraw_cs);
1265     hr = IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
1266     LeaveCriticalSection(&ddraw_cs);
1267     return hr;
1268 }
1269
1270 /*****************************************************************************
1271  * IDirectDrawSurface7::GetPriority
1272  *
1273  * Returns the surface's priority
1274  *
1275  * Params:
1276  *  Priority: Address of a variable to write the priority to
1277  *
1278  * Returns:
1279  *  D3D_OK on success
1280  *  DDERR_INVALIDPARAMS if Priority == NULL
1281  *  For more details, see IWineD3DSurface::GetPriority
1282  *
1283  *****************************************************************************/
1284 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
1285 {
1286     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1287
1288     TRACE("iface %p, priority %p.\n", iface, Priority);
1289
1290     if(!Priority)
1291     {
1292         return DDERR_INVALIDPARAMS;
1293     }
1294
1295     EnterCriticalSection(&ddraw_cs);
1296     *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1297     LeaveCriticalSection(&ddraw_cs);
1298     return DD_OK;
1299 }
1300
1301 /*****************************************************************************
1302  * IDirectDrawSurface7::SetPrivateData
1303  *
1304  * Stores some data in the surface that is intended for the application's
1305  * use.
1306  *
1307  * Params:
1308  *  tag: GUID that identifies the data
1309  *  Data: Pointer to the private data
1310  *  Size: Size of the private data
1311  *  Flags: Some flags
1312  *
1313  * Returns:
1314  *  D3D_OK on success
1315  *  For more details, see IWineD3DSurface::SetPrivateData
1316  *
1317  *****************************************************************************/
1318 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
1319         REFGUID tag, void *Data, DWORD Size, DWORD Flags)
1320 {
1321     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1322     HRESULT hr;
1323
1324     TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
1325             iface, debugstr_guid(tag), Data, Size, Flags);
1326
1327     EnterCriticalSection(&ddraw_cs);
1328     hr = IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1329                                         tag,
1330                                         Data,
1331                                         Size,
1332                                         Flags);
1333     LeaveCriticalSection(&ddraw_cs);
1334     switch(hr)
1335     {
1336         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1337         default:                            return hr;
1338     }
1339 }
1340
1341 /*****************************************************************************
1342  * IDirectDrawSurface7::GetPrivateData
1343  *
1344  * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1345  *
1346  * Params:
1347  *  tag: GUID of the data to return
1348  *  Data: Address where to write the data to
1349  *  Size: Size of the buffer at Data
1350  *
1351  * Returns:
1352  *  DD_OK on success
1353  *  DDERR_INVALIDPARAMS if Data is NULL
1354  *  For more details, see IWineD3DSurface::GetPrivateData
1355  *
1356  *****************************************************************************/
1357 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
1358 {
1359     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1360     HRESULT hr;
1361
1362     TRACE("iface %p, tag %s, data %p, data_size %p.\n",
1363             iface, debugstr_guid(tag), Data, Size);
1364
1365     if(!Data)
1366         return DDERR_INVALIDPARAMS;
1367
1368     EnterCriticalSection(&ddraw_cs);
1369     hr = IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1370                                         tag,
1371                                         Data,
1372                                         Size);
1373     LeaveCriticalSection(&ddraw_cs);
1374     return hr;
1375 }
1376
1377 /*****************************************************************************
1378  * IDirectDrawSurface7::FreePrivateData
1379  *
1380  * Frees private data stored in the surface
1381  *
1382  * Params:
1383  *  tag: Tag of the data to free
1384  *
1385  * Returns:
1386  *  D3D_OK on success
1387  *  For more details, see IWineD3DSurface::FreePrivateData
1388  *
1389  *****************************************************************************/
1390 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
1391 {
1392     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1393     HRESULT hr;
1394
1395     TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
1396
1397     EnterCriticalSection(&ddraw_cs);
1398     hr = IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1399     LeaveCriticalSection(&ddraw_cs);
1400     return hr;
1401 }
1402
1403 /*****************************************************************************
1404  * IDirectDrawSurface7::PageLock
1405  *
1406  * Prevents a sysmem surface from being paged out
1407  *
1408  * Params:
1409  *  Flags: Not used, must be 0(unchecked)
1410  *
1411  * Returns:
1412  *  DD_OK, because it's a stub
1413  *
1414  *****************************************************************************/
1415 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
1416 {
1417     TRACE("iface %p, flags %#x.\n", iface, Flags);
1418
1419     /* This is Windows memory management related - we don't need this */
1420     return DD_OK;
1421 }
1422
1423 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
1424 {
1425     TRACE("iface %p, flags %#x.\n", iface, flags);
1426
1427     return ddraw_surface7_PageLock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1428 }
1429
1430 /*****************************************************************************
1431  * IDirectDrawSurface7::PageUnlock
1432  *
1433  * Allows a sysmem surface to be paged out
1434  *
1435  * Params:
1436  *  Flags: Not used, must be 0(unchecked)
1437  *
1438  * Returns:
1439  *  DD_OK, because it's a stub
1440  *
1441  *****************************************************************************/
1442 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
1443 {
1444     TRACE("iface %p, flags %#x.\n", iface, Flags);
1445
1446     return DD_OK;
1447 }
1448
1449 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
1450 {
1451     TRACE("iface %p, flags %#x.\n", iface, flags);
1452
1453     return ddraw_surface7_PageUnlock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1454 }
1455
1456 /*****************************************************************************
1457  * IDirectDrawSurface7::BltBatch
1458  *
1459  * An unimplemented function
1460  *
1461  * Params:
1462  *  ?
1463  *
1464  * Returns:
1465  *  DDERR_UNSUPPORTED
1466  *
1467  *****************************************************************************/
1468 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1469 {
1470     TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
1471
1472     /* MSDN: "not currently implemented" */
1473     return DDERR_UNSUPPORTED;
1474 }
1475
1476 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
1477 {
1478     TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
1479
1480     return ddraw_surface7_BltBatch((IDirectDrawSurface7 *)surface_from_surface3(iface), batch, count, flags);
1481 }
1482
1483 /*****************************************************************************
1484  * IDirectDrawSurface7::EnumAttachedSurfaces
1485  *
1486  * Enumerates all surfaces attached to this surface
1487  *
1488  * Params:
1489  *  context: Pointer to pass unmodified to the callback
1490  *  cb: Callback function to call for each surface
1491  *
1492  * Returns:
1493  *  DD_OK on success
1494  *  DDERR_INVALIDPARAMS if cb is NULL
1495  *
1496  *****************************************************************************/
1497 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1498         void *context, LPDDENUMSURFACESCALLBACK7 cb)
1499 {
1500     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1501     IDirectDrawSurfaceImpl *surf;
1502     DDSURFACEDESC2 desc;
1503     int i;
1504
1505     /* Attached surfaces aren't handled in WineD3D */
1506     TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
1507
1508     if(!cb)
1509         return DDERR_INVALIDPARAMS;
1510
1511     EnterCriticalSection(&ddraw_cs);
1512     for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1513     {
1514         surf = This->complex_array[i];
1515         if(!surf) break;
1516
1517         ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1518         desc = surf->surface_desc;
1519         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1520         if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1521         {
1522             LeaveCriticalSection(&ddraw_cs);
1523             return DD_OK;
1524         }
1525     }
1526
1527     for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1528     {
1529         ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1530         desc = surf->surface_desc;
1531         /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1532         if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1533         {
1534             LeaveCriticalSection(&ddraw_cs);
1535             return DD_OK;
1536         }
1537     }
1538
1539     TRACE(" end of enumeration.\n");
1540
1541     LeaveCriticalSection(&ddraw_cs);
1542     return DD_OK;
1543 }
1544
1545 struct callback_info
1546 {
1547     LPDDENUMSURFACESCALLBACK callback;
1548     void *context;
1549 };
1550
1551 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
1552 {
1553     const struct callback_info *info = context;
1554
1555     return info->callback((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_vtbl,
1556             (DDSURFACEDESC *)surface_desc, info->context);
1557 }
1558
1559 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
1560         void *context, LPDDENUMSURFACESCALLBACK callback)
1561 {
1562     struct callback_info info;
1563
1564     TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
1565
1566     info.callback = callback;
1567     info.context  = context;
1568
1569     return ddraw_surface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)surface_from_surface3(iface),
1570             &info, EnumCallback);
1571 }
1572
1573 /*****************************************************************************
1574  * IDirectDrawSurface7::EnumOverlayZOrders
1575  *
1576  * "Enumerates the overlay surfaces on the specified destination"
1577  *
1578  * Params:
1579  *  Flags: DDENUMOVERLAYZ_BACKTOFRONT  or DDENUMOVERLAYZ_FRONTTOBACK
1580  *  context: context to pass back to the callback
1581  *  cb: callback function to call for each enumerated surface
1582  *
1583  * Returns:
1584  *  DD_OK, because it's a stub
1585  *
1586  *****************************************************************************/
1587 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1588         DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
1589 {
1590     FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
1591
1592     return DD_OK;
1593 }
1594
1595 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
1596         DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
1597 {
1598     struct callback_info info;
1599
1600     TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
1601
1602     info.callback = callback;
1603     info.context  = context;
1604
1605     return ddraw_surface7_EnumOverlayZOrders((IDirectDrawSurface7 *)surface_from_surface3(iface),
1606             flags, &info, EnumCallback);
1607 }
1608
1609 /*****************************************************************************
1610  * IDirectDrawSurface7::GetBltStatus
1611  *
1612  * Returns the blitting status
1613  *
1614  * Params:
1615  *  Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1616  *
1617  * Returns:
1618  *  See IWineD3DSurface::Blt
1619  *
1620  *****************************************************************************/
1621 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1622 {
1623     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1624     HRESULT hr;
1625
1626     TRACE("iface %p, flags %#x.\n", iface, Flags);
1627
1628     EnterCriticalSection(&ddraw_cs);
1629     hr = IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1630     LeaveCriticalSection(&ddraw_cs);
1631     switch(hr)
1632     {
1633         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1634         default:                            return hr;
1635     }
1636 }
1637
1638 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
1639 {
1640     TRACE("iface %p, flags %#x.\n", iface, flags);
1641
1642     return ddraw_surface7_GetBltStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1643 }
1644
1645 /*****************************************************************************
1646  * IDirectDrawSurface7::GetColorKey
1647  *
1648  * Returns the color key assigned to the surface
1649  *
1650  * Params:
1651  *  Flags: Some flags
1652  *  CKey: Address to store the key to
1653  *
1654  * Returns:
1655  *  DD_OK on success
1656  *  DDERR_INVALIDPARAMS if CKey is NULL
1657  *
1658  *****************************************************************************/
1659 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
1660 {
1661     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1662
1663     TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
1664
1665     if(!CKey)
1666         return DDERR_INVALIDPARAMS;
1667
1668     EnterCriticalSection(&ddraw_cs);
1669
1670     switch (Flags)
1671     {
1672     case DDCKEY_DESTBLT:
1673         if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
1674         {
1675             LeaveCriticalSection(&ddraw_cs);
1676             return DDERR_NOCOLORKEY;
1677         }
1678         *CKey = This->surface_desc.ddckCKDestBlt;
1679         break;
1680
1681     case DDCKEY_DESTOVERLAY:
1682         if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
1683             {
1684             LeaveCriticalSection(&ddraw_cs);
1685             return DDERR_NOCOLORKEY;
1686             }
1687         *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1688         break;
1689
1690     case DDCKEY_SRCBLT:
1691         if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
1692         {
1693             LeaveCriticalSection(&ddraw_cs);
1694             return DDERR_NOCOLORKEY;
1695         }
1696         *CKey = This->surface_desc.ddckCKSrcBlt;
1697         break;
1698
1699     case DDCKEY_SRCOVERLAY:
1700         if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
1701         {
1702             LeaveCriticalSection(&ddraw_cs);
1703             return DDERR_NOCOLORKEY;
1704         }
1705         *CKey = This->surface_desc.ddckCKSrcOverlay;
1706         break;
1707
1708     default:
1709         LeaveCriticalSection(&ddraw_cs);
1710         return DDERR_INVALIDPARAMS;
1711     }
1712
1713     LeaveCriticalSection(&ddraw_cs);
1714     return DD_OK;
1715 }
1716
1717 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
1718 {
1719     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
1720
1721     return ddraw_surface7_GetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
1722 }
1723
1724 /*****************************************************************************
1725  * IDirectDrawSurface7::GetFlipStatus
1726  *
1727  * Returns the flipping status of the surface
1728  *
1729  * Params:
1730  *  Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1731  *
1732  * Returns:
1733  *  See IWineD3DSurface::GetFlipStatus
1734  *
1735  *****************************************************************************/
1736 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1737 {
1738     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1739     HRESULT hr;
1740
1741     TRACE("iface %p, flags %#x.\n", iface, Flags);
1742
1743     EnterCriticalSection(&ddraw_cs);
1744     hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1745     LeaveCriticalSection(&ddraw_cs);
1746     switch(hr)
1747     {
1748         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
1749         default:                            return hr;
1750     }
1751 }
1752
1753 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
1754 {
1755     TRACE("iface %p, flags %#x.\n", iface, flags);
1756
1757     return ddraw_surface7_GetFlipStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1758 }
1759
1760 /*****************************************************************************
1761  * IDirectDrawSurface7::GetOverlayPosition
1762  *
1763  * Returns the display coordinates of a visible and active overlay surface
1764  *
1765  * Params:
1766  *  X
1767  *  Y
1768  *
1769  * Returns:
1770  *  DDERR_NOTAOVERLAYSURFACE, because it's a stub
1771  *****************************************************************************/
1772 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
1773 {
1774     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1775     HRESULT hr;
1776
1777     TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
1778
1779     EnterCriticalSection(&ddraw_cs);
1780     hr = IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1781                                             X,
1782                                             Y);
1783     LeaveCriticalSection(&ddraw_cs);
1784     return hr;
1785 }
1786
1787 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
1788 {
1789     TRACE("iface %p, x %p, y %p.\n", iface, x, y);
1790
1791     return ddraw_surface7_GetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
1792 }
1793
1794 /*****************************************************************************
1795  * IDirectDrawSurface7::GetPixelFormat
1796  *
1797  * Returns the pixel format of the Surface
1798  *
1799  * Params:
1800  *  PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1801  *               format should be written
1802  *
1803  * Returns:
1804  *  DD_OK on success
1805  *  DDERR_INVALIDPARAMS if PixelFormat is NULL
1806  *
1807  *****************************************************************************/
1808 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
1809 {
1810     /* What is DDERR_INVALIDSURFACETYPE for here? */
1811     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1812
1813     TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
1814
1815     if(!PixelFormat)
1816         return DDERR_INVALIDPARAMS;
1817
1818     EnterCriticalSection(&ddraw_cs);
1819     DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1820     LeaveCriticalSection(&ddraw_cs);
1821
1822     return DD_OK;
1823 }
1824
1825 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
1826 {
1827     TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
1828
1829     return ddraw_surface7_GetPixelFormat((IDirectDrawSurface7 *)surface_from_surface3(iface), pixel_format);
1830 }
1831
1832 /*****************************************************************************
1833  * IDirectDrawSurface7::GetSurfaceDesc
1834  *
1835  * Returns the description of this surface
1836  *
1837  * Params:
1838  *  DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1839  *        surface desc
1840  *
1841  * Returns:
1842  *  DD_OK on success
1843  *  DDERR_INVALIDPARAMS if DDSD is NULL
1844  *
1845  *****************************************************************************/
1846 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
1847 {
1848     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1849
1850     TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1851
1852     if(!DDSD)
1853         return DDERR_INVALIDPARAMS;
1854
1855     if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1856     {
1857         WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1858         return DDERR_INVALIDPARAMS;
1859     }
1860
1861     EnterCriticalSection(&ddraw_cs);
1862     DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1863     TRACE("Returning surface desc:\n");
1864     if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1865
1866     LeaveCriticalSection(&ddraw_cs);
1867     return DD_OK;
1868 }
1869
1870 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
1871 {
1872     IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
1873
1874     TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1875
1876     if (!surface_desc) return DDERR_INVALIDPARAMS;
1877
1878     if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
1879     {
1880         WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
1881         return DDERR_INVALIDPARAMS;
1882     }
1883
1884     EnterCriticalSection(&ddraw_cs);
1885     DD_STRUCT_COPY_BYSIZE(surface_desc, (DDSURFACEDESC *)&surface->surface_desc);
1886     TRACE("Returning surface desc:\n");
1887     if (TRACE_ON(ddraw))
1888     {
1889         /* DDRAW_dump_surface_desc handles the smaller size */
1890         DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
1891     }
1892
1893     LeaveCriticalSection(&ddraw_cs);
1894     return DD_OK;
1895 }
1896
1897 /*****************************************************************************
1898  * IDirectDrawSurface7::Initialize
1899  *
1900  * Initializes the surface. This is a no-op in Wine
1901  *
1902  * Params:
1903  *  DD: Pointer to an DirectDraw interface
1904  *  DDSD: Surface description for initialization
1905  *
1906  * Returns:
1907  *  DDERR_ALREADYINITIALIZED
1908  *
1909  *****************************************************************************/
1910 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
1911         IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
1912 {
1913     TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1914
1915     return DDERR_ALREADYINITIALIZED;
1916 }
1917
1918 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
1919         IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
1920 {
1921     TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1922
1923     return ddraw_surface7_Initialize((IDirectDrawSurface7 *)surface_from_surface3(iface),
1924             ddraw, (DDSURFACEDESC2 *)surface_desc);
1925 }
1926
1927 /*****************************************************************************
1928  * IDirect3DTexture1::Initialize
1929  *
1930  * The sdk says it's not implemented
1931  *
1932  * Params:
1933  *  ?
1934  *
1935  * Returns
1936  *  DDERR_UNSUPPORTED
1937  *
1938  *****************************************************************************/
1939 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
1940         IDirect3DDevice *device, IDirectDrawSurface *surface)
1941 {
1942     TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
1943
1944     return DDERR_UNSUPPORTED; /* Unchecked */
1945 }
1946
1947 /*****************************************************************************
1948  * IDirectDrawSurface7::IsLost
1949  *
1950  * Checks if the surface is lost
1951  *
1952  * Returns:
1953  *  DD_OK, if the surface is usable
1954  *  DDERR_ISLOST if the surface is lost
1955  *  See IWineD3DSurface::IsLost for more details
1956  *
1957  *****************************************************************************/
1958 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
1959 {
1960     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1961     HRESULT hr;
1962
1963     TRACE("iface %p.\n", iface);
1964
1965     EnterCriticalSection(&ddraw_cs);
1966     /* We lose the surface if the implementation was changed */
1967     if(This->ImplType != This->ddraw->ImplType)
1968     {
1969         /* But this shouldn't happen. When we change the implementation,
1970          * all surfaces are re-created automatically, and their content
1971          * is copied
1972          */
1973         ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1974         LeaveCriticalSection(&ddraw_cs);
1975         return DDERR_SURFACELOST;
1976     }
1977
1978     hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
1979     LeaveCriticalSection(&ddraw_cs);
1980     switch(hr)
1981     {
1982         /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1983          * WineD3D uses the same error for surfaces
1984          */
1985         case WINED3DERR_DEVICELOST:         return DDERR_SURFACELOST;
1986         default:                            return hr;
1987     }
1988 }
1989
1990 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
1991 {
1992     TRACE("iface %p.\n", iface);
1993
1994     return ddraw_surface7_IsLost((IDirectDrawSurface7 *)surface_from_surface3(iface));
1995 }
1996
1997 /*****************************************************************************
1998  * IDirectDrawSurface7::Restore
1999  *
2000  * Restores a lost surface. This makes the surface usable again, but
2001  * doesn't reload its old contents
2002  *
2003  * Returns:
2004  *  DD_OK on success
2005  *  See IWineD3DSurface::Restore for more details
2006  *
2007  *****************************************************************************/
2008 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
2009 {
2010     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2011     HRESULT hr;
2012
2013     TRACE("iface %p.\n", iface);
2014
2015     EnterCriticalSection(&ddraw_cs);
2016     if(This->ImplType != This->ddraw->ImplType)
2017     {
2018         /* Call the recreation callback. Make sure to AddRef first */
2019         IDirectDrawSurface_AddRef(iface);
2020         ddraw_recreate_surfaces_cb(iface, &This->surface_desc, NULL /* Not needed */);
2021     }
2022     hr = IWineD3DSurface_Restore(This->WineD3DSurface);
2023     LeaveCriticalSection(&ddraw_cs);
2024     return hr;
2025 }
2026
2027 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
2028 {
2029     TRACE("iface %p.\n", iface);
2030
2031     return ddraw_surface7_Restore((IDirectDrawSurface7 *)surface_from_surface3(iface));
2032 }
2033
2034 /*****************************************************************************
2035  * IDirectDrawSurface7::SetOverlayPosition
2036  *
2037  * Changes the display coordinates of an overlay surface
2038  *
2039  * Params:
2040  *  X:
2041  *  Y:
2042  *
2043  * Returns:
2044  *   DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
2045  *****************************************************************************/
2046 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
2047 {
2048     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2049     HRESULT hr;
2050
2051     TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
2052
2053     EnterCriticalSection(&ddraw_cs);
2054     hr = IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
2055                                             X,
2056                                             Y);
2057     LeaveCriticalSection(&ddraw_cs);
2058     return hr;
2059 }
2060
2061 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
2062 {
2063     TRACE("iface %p, x %d, y %d.\n", iface, x, y);
2064
2065     return ddraw_surface7_SetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
2066 }
2067
2068 /*****************************************************************************
2069  * IDirectDrawSurface7::UpdateOverlay
2070  *
2071  * Modifies the attributes of an overlay surface.
2072  *
2073  * Params:
2074  *  SrcRect: The section of the source being used for the overlay
2075  *  DstSurface: Address of the surface that is overlaid
2076  *  DstRect: Place of the overlay
2077  *  Flags: some DDOVER_* flags
2078  *
2079  * Returns:
2080  *  DDERR_UNSUPPORTED, because we don't support overlays
2081  *
2082  *****************************************************************************/
2083 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
2084         IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
2085 {
2086     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2087     IDirectDrawSurfaceImpl *Dst = (IDirectDrawSurfaceImpl *)DstSurface;
2088     HRESULT hr;
2089
2090     TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2091             iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
2092
2093     EnterCriticalSection(&ddraw_cs);
2094     hr = IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
2095                                        SrcRect,
2096                                        Dst ? Dst->WineD3DSurface : NULL,
2097                                        DstRect,
2098                                        Flags,
2099                                        (WINEDDOVERLAYFX *) FX);
2100     LeaveCriticalSection(&ddraw_cs);
2101     switch(hr) {
2102         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2103         case WINEDDERR_NOTAOVERLAYSURFACE:  return DDERR_NOTAOVERLAYSURFACE;
2104         case WINEDDERR_OVERLAYNOTVISIBLE:   return DDERR_OVERLAYNOTVISIBLE;
2105         default:
2106             return hr;
2107     }
2108 }
2109
2110 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
2111         IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
2112 {
2113     TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2114             iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
2115
2116     return ddraw_surface7_UpdateOverlay((IDirectDrawSurface7 *)surface_from_surface3(iface), src_rect,
2117             dst_surface ? (IDirectDrawSurface7 *)surface_from_surface3(dst_surface) : NULL, dst_rect, flags, fx);
2118 }
2119
2120 /*****************************************************************************
2121  * IDirectDrawSurface7::UpdateOverlayDisplay
2122  *
2123  * The DX7 sdk says that it's not implemented
2124  *
2125  * Params:
2126  *  Flags: ?
2127  *
2128  * Returns: DDERR_UNSUPPORTED, because we don't support overlays
2129  *
2130  *****************************************************************************/
2131 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
2132 {
2133     TRACE("iface %p, flags %#x.\n", iface, Flags);
2134
2135     return DDERR_UNSUPPORTED;
2136 }
2137
2138 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
2139 {
2140     TRACE("iface %p, flags %#x.\n", iface, flags);
2141
2142     return ddraw_surface7_UpdateOverlayDisplay((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
2143 }
2144
2145 /*****************************************************************************
2146  * IDirectDrawSurface7::UpdateOverlayZOrder
2147  *
2148  * Sets an overlay's Z order
2149  *
2150  * Params:
2151  *  Flags: DDOVERZ_* flags
2152  *  DDSRef: Defines the relative position in the overlay chain
2153  *
2154  * Returns:
2155  *  DDERR_NOTOVERLAYSURFACE, because we don't support overlays
2156  *
2157  *****************************************************************************/
2158 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
2159         DWORD Flags, IDirectDrawSurface7 *DDSRef)
2160 {
2161     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2162     IDirectDrawSurfaceImpl *Ref = (IDirectDrawSurfaceImpl *)DDSRef;
2163     HRESULT hr;
2164
2165     TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
2166
2167     EnterCriticalSection(&ddraw_cs);
2168     hr =  IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
2169                                               Flags,
2170                                               Ref ? Ref->WineD3DSurface : NULL);
2171     LeaveCriticalSection(&ddraw_cs);
2172     return hr;
2173 }
2174
2175 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
2176         DWORD flags, IDirectDrawSurface3 *reference)
2177 {
2178     TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
2179
2180     return ddraw_surface7_UpdateOverlayZOrder((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
2181             reference ? (IDirectDrawSurface7 *)surface_from_surface3(reference) : NULL);
2182 }
2183
2184 /*****************************************************************************
2185  * IDirectDrawSurface7::GetDDInterface
2186  *
2187  * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
2188  * surface belongs to
2189  *
2190  * Params:
2191  *  DD: Address to write the interface pointer to
2192  *
2193  * Returns:
2194  *  DD_OK on success
2195  *  DDERR_INVALIDPARAMS if DD is NULL
2196  *
2197  *****************************************************************************/
2198 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
2199 {
2200     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2201
2202     TRACE("iface %p, ddraw %p.\n", iface, DD);
2203
2204     if(!DD)
2205         return DDERR_INVALIDPARAMS;
2206
2207     switch(This->version)
2208     {
2209         case 7:
2210             *DD = &This->ddraw->IDirectDraw7_iface;
2211             break;
2212
2213         case 4:
2214             *DD = &This->ddraw->IDirectDraw4_iface;
2215             break;
2216
2217         case 2:
2218             *DD = &This->ddraw->IDirectDraw2_iface;
2219             break;
2220
2221         case 1:
2222             *DD = &This->ddraw->IDirectDraw_iface;
2223             break;
2224
2225     }
2226     IUnknown_AddRef((IUnknown *)*DD);
2227
2228     return DD_OK;
2229 }
2230
2231 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
2232 {
2233     TRACE("iface %p, ddraw %p.\n", iface, ddraw);
2234
2235     return ddraw_surface7_GetDDInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), ddraw);
2236 }
2237
2238 /* This seems also windows implementation specific - I don't think WineD3D needs this */
2239 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
2240 {
2241     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2242     volatile IDirectDrawSurfaceImpl* vThis = This;
2243
2244     TRACE("iface %p.\n", iface);
2245
2246     EnterCriticalSection(&ddraw_cs);
2247     /* A uniqueness value of 0 is apparently special.
2248      * This needs to be checked.
2249      * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
2250      */
2251     while (1) {
2252         DWORD old_uniqueness_value = vThis->uniqueness_value;
2253         DWORD new_uniqueness_value = old_uniqueness_value+1;
2254
2255         if (old_uniqueness_value == 0) break;
2256         if (new_uniqueness_value == 0) new_uniqueness_value = 1;
2257
2258         if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
2259                                       old_uniqueness_value,
2260                                       new_uniqueness_value)
2261             == old_uniqueness_value)
2262             break;
2263     }
2264
2265     LeaveCriticalSection(&ddraw_cs);
2266     return DD_OK;
2267 }
2268
2269 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
2270 {
2271     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2272
2273     TRACE("iface %p, value %p.\n", iface, pValue);
2274
2275     EnterCriticalSection(&ddraw_cs);
2276     *pValue = This->uniqueness_value;
2277     LeaveCriticalSection(&ddraw_cs);
2278     return DD_OK;
2279 }
2280
2281 /*****************************************************************************
2282  * IDirectDrawSurface7::SetLOD
2283  *
2284  * Sets the level of detail of a texture
2285  *
2286  * Params:
2287  *  MaxLOD: LOD to set
2288  *
2289  * Returns:
2290  *  DD_OK on success
2291  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
2292  *
2293  *****************************************************************************/
2294 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
2295 {
2296     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2297     HRESULT hr;
2298
2299     TRACE("iface %p, lod %u.\n", iface, MaxLOD);
2300
2301     EnterCriticalSection(&ddraw_cs);
2302     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2303     {
2304         LeaveCriticalSection(&ddraw_cs);
2305         return DDERR_INVALIDOBJECT;
2306     }
2307
2308     if (!This->wined3d_texture)
2309     {
2310         ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2311         LeaveCriticalSection(&ddraw_cs);
2312         return DDERR_INVALIDOBJECT;
2313     }
2314
2315     hr = wined3d_texture_set_lod(This->wined3d_texture, MaxLOD);
2316     LeaveCriticalSection(&ddraw_cs);
2317     return hr;
2318 }
2319
2320 /*****************************************************************************
2321  * IDirectDrawSurface7::GetLOD
2322  *
2323  * Returns the level of detail of a Direct3D texture
2324  *
2325  * Params:
2326  *  MaxLOD: Address to write the LOD to
2327  *
2328  * Returns:
2329  *  DD_OK on success
2330  *  DDERR_INVALIDPARAMS if MaxLOD is NULL
2331  *  DDERR_INVALIDOBJECT if the surface is invalid for this method
2332  *
2333  *****************************************************************************/
2334 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
2335 {
2336     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2337
2338     TRACE("iface %p, lod %p.\n", iface, MaxLOD);
2339
2340     if(!MaxLOD)
2341         return DDERR_INVALIDPARAMS;
2342
2343     EnterCriticalSection(&ddraw_cs);
2344     if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2345     {
2346         LeaveCriticalSection(&ddraw_cs);
2347         return DDERR_INVALIDOBJECT;
2348     }
2349
2350     *MaxLOD = wined3d_texture_get_lod(This->wined3d_texture);
2351     LeaveCriticalSection(&ddraw_cs);
2352     return DD_OK;
2353 }
2354
2355 /*****************************************************************************
2356  * IDirectDrawSurface7::BltFast
2357  *
2358  * Performs a fast Blit.
2359  *
2360  * Params:
2361  *  dstx: The x coordinate to blit to on the destination
2362  *  dsty: The y coordinate to blit to on the destination
2363  *  Source: The source surface
2364  *  rsrc: The source rectangle
2365  *  trans: Type of transfer. Some DDBLTFAST_* flags
2366  *
2367  * Returns:
2368  *  DD_OK on success
2369  *  For more details, see IWineD3DSurface::BltFast
2370  *
2371  *****************************************************************************/
2372 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
2373         IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
2374 {
2375     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2376     IDirectDrawSurfaceImpl *src = (IDirectDrawSurfaceImpl *)Source;
2377     DWORD src_w, src_h, dst_w, dst_h;
2378     HRESULT hr;
2379
2380     TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2381             iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
2382
2383     dst_w = This->surface_desc.dwWidth;
2384     dst_h = This->surface_desc.dwHeight;
2385
2386     /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2387      * in that case
2388      */
2389     if(rsrc)
2390     {
2391         if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2392            rsrc->right > src->surface_desc.dwWidth ||
2393            rsrc->bottom > src->surface_desc.dwHeight)
2394         {
2395             WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2396             return DDERR_INVALIDRECT;
2397         }
2398
2399         src_w = rsrc->right - rsrc->left;
2400         src_h = rsrc->bottom - rsrc->top;
2401     }
2402     else
2403     {
2404         src_w = src->surface_desc.dwWidth;
2405         src_h = src->surface_desc.dwHeight;
2406     }
2407
2408     if (src_w > dst_w || dstx > dst_w - src_w
2409             || src_h > dst_h || dsty > dst_h - src_h)
2410     {
2411         WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
2412         return DDERR_INVALIDRECT;
2413     }
2414
2415     EnterCriticalSection(&ddraw_cs);
2416     hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
2417                                  dstx, dsty,
2418                                  src ? src->WineD3DSurface : NULL,
2419                                  rsrc,
2420                                  trans);
2421     LeaveCriticalSection(&ddraw_cs);
2422     switch(hr)
2423     {
2424         case WINED3DERR_NOTAVAILABLE:           return DDERR_UNSUPPORTED;
2425         case WINED3DERR_WRONGTEXTUREFORMAT:     return DDERR_INVALIDPIXELFORMAT;
2426         default:                                return hr;
2427     }
2428 }
2429
2430 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
2431         IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
2432 {
2433     TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2434             iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
2435
2436     return ddraw_surface7_BltFast((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_x, dst_y,
2437             src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags);
2438 }
2439
2440 /*****************************************************************************
2441  * IDirectDrawSurface7::GetClipper
2442  *
2443  * Returns the IDirectDrawClipper interface of the clipper assigned to this
2444  * surface
2445  *
2446  * Params:
2447  *  Clipper: Address to store the interface pointer at
2448  *
2449  * Returns:
2450  *  DD_OK on success
2451  *  DDERR_INVALIDPARAMS if Clipper is NULL
2452  *  DDERR_NOCLIPPERATTACHED if there's no clipper attached
2453  *
2454  *****************************************************************************/
2455 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
2456 {
2457     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2458
2459     TRACE("iface %p, clipper %p.\n", iface, Clipper);
2460
2461     if(!Clipper)
2462     {
2463         LeaveCriticalSection(&ddraw_cs);
2464         return DDERR_INVALIDPARAMS;
2465     }
2466
2467     EnterCriticalSection(&ddraw_cs);
2468     if(This->clipper == NULL)
2469     {
2470         LeaveCriticalSection(&ddraw_cs);
2471         return DDERR_NOCLIPPERATTACHED;
2472     }
2473
2474     *Clipper = (IDirectDrawClipper *)This->clipper;
2475     IDirectDrawClipper_AddRef(*Clipper);
2476     LeaveCriticalSection(&ddraw_cs);
2477     return DD_OK;
2478 }
2479
2480 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
2481 {
2482     TRACE("iface %p, clipper %p.\n", iface, clipper);
2483
2484     return ddraw_surface7_GetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2485 }
2486
2487 /*****************************************************************************
2488  * IDirectDrawSurface7::SetClipper
2489  *
2490  * Sets a clipper for the surface
2491  *
2492  * Params:
2493  *  Clipper: IDirectDrawClipper interface of the clipper to set
2494  *
2495  * Returns:
2496  *  DD_OK on success
2497  *
2498  *****************************************************************************/
2499 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper *Clipper)
2500 {
2501     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2502     IDirectDrawClipperImpl *oldClipper = This->clipper;
2503     HWND clipWindow;
2504     HRESULT hr;
2505
2506     TRACE("iface %p, clipper %p.\n", iface, Clipper);
2507
2508     EnterCriticalSection(&ddraw_cs);
2509     if ((IDirectDrawClipperImpl *)Clipper == This->clipper)
2510     {
2511         LeaveCriticalSection(&ddraw_cs);
2512         return DD_OK;
2513     }
2514
2515     This->clipper = (IDirectDrawClipperImpl *)Clipper;
2516
2517     if (Clipper != NULL)
2518         IDirectDrawClipper_AddRef(Clipper);
2519     if(oldClipper)
2520         IDirectDrawClipper_Release((IDirectDrawClipper *)oldClipper);
2521
2522     hr = IWineD3DSurface_SetClipper(This->WineD3DSurface, This->clipper ? This->clipper->wineD3DClipper : NULL);
2523
2524     if (This->wined3d_swapchain)
2525     {
2526         clipWindow = NULL;
2527         if(Clipper) {
2528             IDirectDrawClipper_GetHWnd(Clipper, &clipWindow);
2529         }
2530
2531         if (clipWindow)
2532             wined3d_swapchain_set_window(This->wined3d_swapchain, clipWindow);
2533         else
2534             wined3d_swapchain_set_window(This->wined3d_swapchain, This->ddraw->d3d_window);
2535     }
2536
2537     LeaveCriticalSection(&ddraw_cs);
2538     return hr;
2539 }
2540
2541 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
2542 {
2543     TRACE("iface %p, clipper %p.\n", iface, clipper);
2544
2545     return ddraw_surface7_SetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2546 }
2547
2548 /*****************************************************************************
2549  * IDirectDrawSurface7::SetSurfaceDesc
2550  *
2551  * Sets the surface description. It can override the pixel format, the surface
2552  * memory, ...
2553  * It's not really tested.
2554  *
2555  * Params:
2556  * DDSD: Pointer to the new surface description to set
2557  * Flags: Some flags
2558  *
2559  * Returns:
2560  *  DD_OK on success
2561  *  DDERR_INVALIDPARAMS if DDSD is NULL
2562  *
2563  *****************************************************************************/
2564 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
2565 {
2566     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2567     enum wined3d_format_id newFormat = WINED3DFMT_UNKNOWN;
2568     HRESULT hr;
2569
2570     TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
2571
2572     if(!DDSD)
2573         return DDERR_INVALIDPARAMS;
2574
2575     EnterCriticalSection(&ddraw_cs);
2576     if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2577     {
2578         newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2579
2580         if(newFormat == WINED3DFMT_UNKNOWN)
2581         {
2582             ERR("Requested to set an unknown pixelformat\n");
2583             LeaveCriticalSection(&ddraw_cs);
2584             return DDERR_INVALIDPARAMS;
2585         }
2586         if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2587         {
2588             hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2589                                            newFormat);
2590             if(hr != DD_OK)
2591             {
2592                 LeaveCriticalSection(&ddraw_cs);
2593                 return hr;
2594             }
2595         }
2596     }
2597     if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2598     {
2599         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2600                                     DDCKEY_DESTOVERLAY,
2601                                     (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2602     }
2603     if (DDSD->dwFlags & DDSD_CKDESTBLT)
2604     {
2605         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2606                                     DDCKEY_DESTBLT,
2607                                     (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2608     }
2609     if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2610     {
2611         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2612                                     DDCKEY_SRCOVERLAY,
2613                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2614     }
2615     if (DDSD->dwFlags & DDSD_CKSRCBLT)
2616     {
2617         IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2618                                     DDCKEY_SRCBLT,
2619                                     (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2620     }
2621     if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2622     {
2623         hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2624         if(hr != WINED3D_OK)
2625         {
2626             /* No need for a trace here, wined3d does that for us */
2627             switch(hr)
2628             {
2629                 case WINED3DERR_INVALIDCALL:
2630                     LeaveCriticalSection(&ddraw_cs);
2631                     return DDERR_INVALIDPARAMS;
2632                 default:
2633                     break; /* Go on */
2634             }
2635         }
2636     }
2637
2638     This->surface_desc = *DDSD;
2639
2640     LeaveCriticalSection(&ddraw_cs);
2641     return DD_OK;
2642 }
2643
2644 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
2645         DDSURFACEDESC *surface_desc, DWORD flags)
2646 {
2647     TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
2648
2649     return ddraw_surface7_SetSurfaceDesc((IDirectDrawSurface7 *)surface_from_surface3(iface),
2650             (DDSURFACEDESC2 *)surface_desc, flags);
2651 }
2652
2653 /*****************************************************************************
2654  * IDirectDrawSurface7::GetPalette
2655  *
2656  * Returns the IDirectDrawPalette interface of the palette currently assigned
2657  * to the surface
2658  *
2659  * Params:
2660  *  Pal: Address to write the interface pointer to
2661  *
2662  * Returns:
2663  *  DD_OK on success
2664  *  DDERR_INVALIDPARAMS if Pal is NULL
2665  *
2666  *****************************************************************************/
2667 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
2668 {
2669     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2670     struct wined3d_palette *wined3d_palette;
2671     HRESULT hr;
2672
2673     TRACE("iface %p, palette %p.\n", iface, Pal);
2674
2675     if(!Pal)
2676         return DDERR_INVALIDPARAMS;
2677
2678     EnterCriticalSection(&ddraw_cs);
2679     hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wined3d_palette);
2680     if (FAILED(hr))
2681     {
2682         LeaveCriticalSection(&ddraw_cs);
2683         return hr;
2684     }
2685
2686     if (wined3d_palette)
2687     {
2688         *Pal = wined3d_palette_get_parent(wined3d_palette);
2689         IDirectDrawPalette_AddRef(*Pal);
2690     }
2691     else
2692     {
2693         *Pal = NULL;
2694         hr = DDERR_NOPALETTEATTACHED;
2695     }
2696
2697     LeaveCriticalSection(&ddraw_cs);
2698     return hr;
2699 }
2700
2701 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
2702 {
2703     TRACE("iface %p, palette %p.\n", iface, palette);
2704
2705     return ddraw_surface7_GetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2706 }
2707
2708 /*****************************************************************************
2709  * SetColorKeyEnum
2710  *
2711  * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2712  * recursively in the surface tree
2713  *
2714  *****************************************************************************/
2715 struct SCKContext
2716 {
2717     HRESULT ret;
2718     WINEDDCOLORKEY *CKey;
2719     DWORD Flags;
2720 };
2721
2722 static HRESULT WINAPI
2723 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2724                 DDSURFACEDESC2 *desc,
2725                 void *context)
2726 {
2727     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)surface;
2728     struct SCKContext *ctx = context;
2729     HRESULT hr;
2730
2731     hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2732                                      ctx->Flags,
2733                                      ctx->CKey);
2734     if(hr != DD_OK)
2735     {
2736         WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2737         ctx->ret = hr;
2738     }
2739
2740     ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
2741     ddraw_surface7_Release(surface);
2742
2743     return DDENUMRET_OK;
2744 }
2745
2746 /*****************************************************************************
2747  * IDirectDrawSurface7::SetColorKey
2748  *
2749  * Sets the color keying options for the surface. Observations showed that
2750  * in case of complex surfaces the color key has to be assigned to all
2751  * sublevels.
2752  *
2753  * Params:
2754  *  Flags: DDCKEY_*
2755  *  CKey: The new color key
2756  *
2757  * Returns:
2758  *  DD_OK on success
2759  *  See IWineD3DSurface::SetColorKey for details
2760  *
2761  *****************************************************************************/
2762 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2763 {
2764     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2765     DDCOLORKEY FixedCKey;
2766     struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
2767
2768     TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2769
2770     EnterCriticalSection(&ddraw_cs);
2771     if (CKey)
2772     {
2773         FixedCKey = *CKey;
2774         /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
2775         if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
2776             FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
2777
2778         switch (Flags & ~DDCKEY_COLORSPACE)
2779         {
2780         case DDCKEY_DESTBLT:
2781             This->surface_desc.ddckCKDestBlt = FixedCKey;
2782             This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2783             break;
2784
2785         case DDCKEY_DESTOVERLAY:
2786             This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
2787             This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2788             break;
2789
2790         case DDCKEY_SRCOVERLAY:
2791             This->surface_desc.ddckCKSrcOverlay = FixedCKey;
2792             This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2793             break;
2794
2795         case DDCKEY_SRCBLT:
2796             This->surface_desc.ddckCKSrcBlt = FixedCKey;
2797             This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2798             break;
2799
2800         default:
2801             LeaveCriticalSection(&ddraw_cs);
2802             return DDERR_INVALIDPARAMS;
2803         }
2804     }
2805     else
2806     {
2807         switch (Flags & ~DDCKEY_COLORSPACE)
2808         {
2809         case DDCKEY_DESTBLT:
2810             This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2811             break;
2812
2813         case DDCKEY_DESTOVERLAY:
2814             This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2815             break;
2816
2817         case DDCKEY_SRCOVERLAY:
2818             This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2819             break;
2820
2821         case DDCKEY_SRCBLT:
2822             This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2823             break;
2824
2825         default:
2826             LeaveCriticalSection(&ddraw_cs);
2827             return DDERR_INVALIDPARAMS;
2828         }
2829     }
2830     ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface, Flags, ctx.CKey);
2831     ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
2832     LeaveCriticalSection(&ddraw_cs);
2833     switch(ctx.ret)
2834     {
2835         case WINED3DERR_INVALIDCALL:        return DDERR_INVALIDPARAMS;
2836         default:                            return ctx.ret;
2837     }
2838 }
2839
2840 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2841 {
2842     TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2843
2844     return ddraw_surface7_SetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
2845 }
2846
2847 /*****************************************************************************
2848  * IDirectDrawSurface7::SetPalette
2849  *
2850  * Assigns a DirectDrawPalette object to the surface
2851  *
2852  * Params:
2853  *  Pal: Interface to the palette to set
2854  *
2855  * Returns:
2856  *  DD_OK on success
2857  *
2858  *****************************************************************************/
2859 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
2860 {
2861     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2862     IDirectDrawPalette *oldPal;
2863     IDirectDrawSurfaceImpl *surf;
2864     IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
2865     HRESULT hr;
2866
2867     TRACE("iface %p, palette %p.\n", iface, Pal);
2868
2869     if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
2870             DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
2871         return DDERR_INVALIDPIXELFORMAT;
2872     }
2873
2874     /* Find the old palette */
2875     EnterCriticalSection(&ddraw_cs);
2876     hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2877     if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2878     {
2879         LeaveCriticalSection(&ddraw_cs);
2880         return hr;
2881     }
2882     if(oldPal) IDirectDrawPalette_Release(oldPal);  /* For the GetPalette */
2883
2884     /* Set the new Palette */
2885     IWineD3DSurface_SetPalette(This->WineD3DSurface,
2886                                PalImpl ? PalImpl->wineD3DPalette : NULL);
2887     /* AddRef the Palette */
2888     if(Pal) IDirectDrawPalette_AddRef(Pal);
2889
2890     /* Release the old palette */
2891     if(oldPal) IDirectDrawPalette_Release(oldPal);
2892
2893     /* If this is a front buffer, also update the back buffers
2894      * TODO: How do things work for palettized cube textures?
2895      */
2896     if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2897     {
2898         /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2899         DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2900
2901         surf = This;
2902         while(1)
2903         {
2904             IDirectDrawSurface7 *attach;
2905             HRESULT hr;
2906             hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &caps2, &attach);
2907             if(hr != DD_OK)
2908             {
2909                 break;
2910             }
2911
2912             TRACE("Setting palette on %p\n", attach);
2913             ddraw_surface7_SetPalette(attach, Pal);
2914             surf = (IDirectDrawSurfaceImpl *)attach;
2915             ddraw_surface7_Release(attach);
2916         }
2917     }
2918
2919     LeaveCriticalSection(&ddraw_cs);
2920     return DD_OK;
2921 }
2922
2923 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
2924 {
2925     TRACE("iface %p, palette %p.\n", iface, palette);
2926
2927     return ddraw_surface7_SetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2928 }
2929
2930 /**********************************************************
2931  * IDirectDrawGammaControl::GetGammaRamp
2932  *
2933  * Returns the current gamma ramp for a surface
2934  *
2935  * Params:
2936  *  flags: Ignored
2937  *  gamma_ramp: Address to write the ramp to
2938  *
2939  * Returns:
2940  *  DD_OK on success
2941  *  DDERR_INVALIDPARAMS if gamma_ramp is NULL
2942  *
2943  **********************************************************/
2944 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
2945         DWORD flags, DDGAMMARAMP *gamma_ramp)
2946 {
2947     IDirectDrawSurfaceImpl *surface = surface_from_gamma_control(iface);
2948
2949     TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
2950
2951     if (!gamma_ramp)
2952     {
2953         WARN("Invalid gamma_ramp passed.\n");
2954         return DDERR_INVALIDPARAMS;
2955     }
2956
2957     EnterCriticalSection(&ddraw_cs);
2958     if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2959     {
2960         /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP. */
2961         IWineD3DDevice_GetGammaRamp(surface->ddraw->wineD3DDevice, 0, (WINED3DGAMMARAMP *)gamma_ramp);
2962     }
2963     else
2964     {
2965         ERR("Not implemented for non-primary surfaces.\n");
2966     }
2967     LeaveCriticalSection(&ddraw_cs);
2968
2969     return DD_OK;
2970 }
2971
2972 /**********************************************************
2973  * IDirectDrawGammaControl::SetGammaRamp
2974  *
2975  * Sets the red, green and blue gamma ramps for
2976  *
2977  * Params:
2978  *  flags: Can be DDSGR_CALIBRATE to request calibration
2979  *  gamma_ramp: Structure containing the new gamma ramp
2980  *
2981  * Returns:
2982  *  DD_OK on success
2983  *  DDERR_INVALIDPARAMS if gamma_ramp is NULL
2984  *
2985  **********************************************************/
2986 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
2987         DWORD flags, DDGAMMARAMP *gamma_ramp)
2988 {
2989     IDirectDrawSurfaceImpl *surface = surface_from_gamma_control(iface);
2990
2991     TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
2992
2993     if (!gamma_ramp)
2994     {
2995         WARN("Invalid gamma_ramp passed.\n");
2996         return DDERR_INVALIDPARAMS;
2997     }
2998
2999     EnterCriticalSection(&ddraw_cs);
3000     if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3001     {
3002         /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP */
3003         IWineD3DDevice_SetGammaRamp(surface->ddraw->wineD3DDevice, 0, flags, (WINED3DGAMMARAMP *)gamma_ramp);
3004     }
3005     else
3006     {
3007         ERR("Not implemented for non-primary surfaces.\n");
3008     }
3009     LeaveCriticalSection(&ddraw_cs);
3010
3011     return DD_OK;
3012 }
3013
3014 /*****************************************************************************
3015  * IDirect3DTexture2::PaletteChanged
3016  *
3017  * Informs the texture about a palette change
3018  *
3019  * Params:
3020  *  start: Start index of the change
3021  *  count: The number of changed entries
3022  *
3023  * Returns
3024  *  D3D_OK, because it's a stub
3025  *
3026  *****************************************************************************/
3027 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
3028 {
3029     FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
3030
3031     return D3D_OK;
3032 }
3033
3034 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
3035 {
3036     IDirectDrawSurfaceImpl *surface = surface_from_texture1(iface);
3037
3038     TRACE("iface %p, start %u, count %u.\n", iface, start, count);
3039
3040     return d3d_texture2_PaletteChanged((IDirect3DTexture2 *)&surface->IDirect3DTexture2_vtbl, start, count);
3041 }
3042
3043 /*****************************************************************************
3044  * IDirect3DTexture::Unload
3045  *
3046  * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
3047  *
3048  *
3049  * Returns:
3050  *  DDERR_UNSUPPORTED
3051  *
3052  *****************************************************************************/
3053 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
3054 {
3055     WARN("iface %p. Not implemented.\n", iface);
3056
3057     return DDERR_UNSUPPORTED;
3058 }
3059
3060 /*****************************************************************************
3061  * IDirect3DTexture2::GetHandle
3062  *
3063  * Returns handle for the texture. At the moment, the interface
3064  * to the IWineD3DTexture is used.
3065  *
3066  * Params:
3067  *  device: Device this handle is assigned to
3068  *  handle: Address to store the handle at.
3069  *
3070  * Returns:
3071  *  D3D_OK
3072  *
3073  *****************************************************************************/
3074 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
3075         IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
3076 {
3077     IDirectDrawSurfaceImpl *surface = surface_from_texture2(iface);
3078
3079     TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
3080
3081     EnterCriticalSection(&ddraw_cs);
3082
3083     if (!surface->Handle)
3084     {
3085         DWORD h = ddraw_allocate_handle(&device_from_device2(device)->handle_table, surface, DDRAW_HANDLE_SURFACE);
3086         if (h == DDRAW_INVALID_HANDLE)
3087         {
3088             ERR("Failed to allocate a texture handle.\n");
3089             LeaveCriticalSection(&ddraw_cs);
3090             return DDERR_OUTOFMEMORY;
3091         }
3092
3093         surface->Handle = h + 1;
3094     }
3095
3096     TRACE("Returning handle %08x.\n", surface->Handle);
3097     *handle = surface->Handle;
3098
3099     LeaveCriticalSection(&ddraw_cs);
3100
3101     return D3D_OK;
3102 }
3103
3104 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
3105         IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
3106 {
3107     IDirect3DTexture2 *texture2 = (IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl;
3108     IDirect3DDevice2 *device2 = (IDirect3DDevice2 *)&device_from_device1(device)->IDirect3DDevice2_vtbl;
3109
3110     TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
3111
3112     return d3d_texture2_GetHandle(texture2, device2, handle);
3113 }
3114
3115 /*****************************************************************************
3116  * get_sub_mimaplevel
3117  *
3118  * Helper function that returns the next mipmap level
3119  *
3120  * tex_ptr: Surface of which to return the next level
3121  *
3122  *****************************************************************************/
3123 static IDirectDrawSurfaceImpl *get_sub_mimaplevel(IDirectDrawSurfaceImpl *surface)
3124 {
3125     /* Now go down the mipmap chain to the next surface */
3126     static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
3127     IDirectDrawSurface7 *next_level;
3128     HRESULT hr;
3129
3130     hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surface, &mipmap_caps, &next_level);
3131     if (FAILED(hr)) return NULL;
3132
3133     ddraw_surface7_Release(next_level);
3134
3135     return (IDirectDrawSurfaceImpl *)next_level;
3136 }
3137
3138 /*****************************************************************************
3139  * IDirect3DTexture2::Load
3140  *
3141  * Loads a texture created with the DDSCAPS_ALLOCONLOAD
3142  *
3143  * This function isn't relayed to WineD3D because the whole interface is
3144  * implemented in DDraw only. For speed improvements a implementation which
3145  * takes OpenGL more into account could be placed into WineD3D.
3146  *
3147  * Params:
3148  *  src_texture: Address of the texture to load
3149  *
3150  * Returns:
3151  *  D3D_OK on success
3152  *  D3DERR_TEXTURE_LOAD_FAILED.
3153  *
3154  *****************************************************************************/
3155 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
3156 {
3157     IDirectDrawSurfaceImpl *dst_surface = surface_from_texture2(iface);
3158     IDirectDrawSurfaceImpl *src_surface = surface_from_texture2(src_texture);
3159     HRESULT hr;
3160
3161     TRACE("iface %p, src_texture %p.\n", iface, src_texture);
3162
3163     if (src_surface == dst_surface)
3164     {
3165         TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
3166         return D3D_OK;
3167     }
3168
3169     EnterCriticalSection(&ddraw_cs);
3170
3171     if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3172             != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
3173             || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
3174     {
3175         ERR("Trying to load surfaces with different mip-map counts.\n");
3176     }
3177
3178     for (;;)
3179     {
3180         struct wined3d_palette *wined3d_dst_pal, *wined3d_src_pal;
3181         IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
3182         DDSURFACEDESC *src_desc, *dst_desc;
3183
3184         TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
3185                 src_surface, dst_surface, src_surface->mipmap_level);
3186
3187         /* Suppress the ALLOCONLOAD flag */
3188         dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
3189
3190         /* Get the palettes */
3191         hr = IWineD3DSurface_GetPalette(dst_surface->WineD3DSurface, &wined3d_dst_pal);
3192         if (FAILED(hr))
3193         {
3194             ERR("Failed to get destination palette, hr %#x.\n", hr);
3195             LeaveCriticalSection(&ddraw_cs);
3196             return D3DERR_TEXTURE_LOAD_FAILED;
3197         }
3198         if (wined3d_dst_pal)
3199             dst_pal = wined3d_palette_get_parent(wined3d_dst_pal);
3200
3201         hr = IWineD3DSurface_GetPalette(src_surface->WineD3DSurface, &wined3d_src_pal);
3202         if (FAILED(hr))
3203         {
3204             ERR("Failed to get source palette, hr %#x.\n", hr);
3205             LeaveCriticalSection(&ddraw_cs);
3206             return D3DERR_TEXTURE_LOAD_FAILED;
3207         }
3208         if (wined3d_src_pal)
3209             src_pal = wined3d_palette_get_parent(wined3d_src_pal);
3210
3211         if (src_pal)
3212         {
3213             PALETTEENTRY palent[256];
3214
3215             if (!dst_pal)
3216             {
3217                 LeaveCriticalSection(&ddraw_cs);
3218                 return DDERR_NOPALETTEATTACHED;
3219             }
3220             IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
3221             IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
3222         }
3223
3224         /* Copy one surface on the other */
3225         dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
3226         src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
3227
3228         if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
3229         {
3230             /* Should also check for same pixel format, u1.lPitch, ... */
3231             ERR("Error in surface sizes.\n");
3232             LeaveCriticalSection(&ddraw_cs);
3233             return D3DERR_TEXTURE_LOAD_FAILED;
3234         }
3235         else
3236         {
3237             WINED3DLOCKED_RECT src_rect, dst_rect;
3238
3239             /* Copy also the ColorKeying stuff */
3240             if (src_desc->dwFlags & DDSD_CKSRCBLT)
3241             {
3242                 dst_desc->dwFlags |= DDSD_CKSRCBLT;
3243                 dst_desc->ddckCKSrcBlt.dwColorSpaceLowValue = src_desc->ddckCKSrcBlt.dwColorSpaceLowValue;
3244                 dst_desc->ddckCKSrcBlt.dwColorSpaceHighValue = src_desc->ddckCKSrcBlt.dwColorSpaceHighValue;
3245             }
3246
3247             /* Copy the main memory texture into the surface that corresponds
3248              * to the OpenGL texture object. */
3249
3250             hr = IWineD3DSurface_Map(src_surface->WineD3DSurface, &src_rect, NULL, 0);
3251             if (FAILED(hr))
3252             {
3253                 ERR("Failed to lock source surface, hr %#x.\n", hr);
3254                 LeaveCriticalSection(&ddraw_cs);
3255                 return D3DERR_TEXTURE_LOAD_FAILED;
3256             }
3257
3258             hr = IWineD3DSurface_Map(dst_surface->WineD3DSurface, &dst_rect, NULL, 0);
3259             if (FAILED(hr))
3260             {
3261                 ERR("Failed to lock destination surface, hr %#x.\n", hr);
3262                 IWineD3DSurface_Unmap(src_surface->WineD3DSurface);
3263                 LeaveCriticalSection(&ddraw_cs);
3264                 return D3DERR_TEXTURE_LOAD_FAILED;
3265             }
3266
3267             if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
3268                 memcpy(dst_rect.pBits, src_rect.pBits, src_surface->surface_desc.u1.dwLinearSize);
3269             else
3270                 memcpy(dst_rect.pBits, src_rect.pBits, src_rect.Pitch * src_desc->dwHeight);
3271
3272             IWineD3DSurface_Unmap(src_surface->WineD3DSurface);
3273             IWineD3DSurface_Unmap(dst_surface->WineD3DSurface);
3274         }
3275
3276         if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3277             src_surface = get_sub_mimaplevel(src_surface);
3278         else
3279             src_surface = NULL;
3280
3281         if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3282             dst_surface = get_sub_mimaplevel(dst_surface);
3283         else
3284             dst_surface = NULL;
3285
3286         if (!src_surface || !dst_surface)
3287         {
3288             if (src_surface != dst_surface)
3289                 ERR("Loading surface with different mipmap structure.\n");
3290             break;
3291         }
3292     }
3293
3294     LeaveCriticalSection(&ddraw_cs);
3295
3296     return hr;
3297 }
3298
3299 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
3300 {
3301     TRACE("iface %p, src_texture %p.\n", iface, src_texture);
3302
3303     return d3d_texture2_Load((IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl,
3304             src_texture ? (IDirect3DTexture2 *)&surface_from_texture1(src_texture)->IDirect3DTexture2_vtbl : NULL);
3305 }
3306
3307 /*****************************************************************************
3308  * The VTable
3309  *****************************************************************************/
3310
3311 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
3312 {
3313     /* IUnknown */
3314     ddraw_surface7_QueryInterface,
3315     ddraw_surface7_AddRef,
3316     ddraw_surface7_Release,
3317     /* IDirectDrawSurface */
3318     ddraw_surface7_AddAttachedSurface,
3319     ddraw_surface7_AddOverlayDirtyRect,
3320     ddraw_surface7_Blt,
3321     ddraw_surface7_BltBatch,
3322     ddraw_surface7_BltFast,
3323     ddraw_surface7_DeleteAttachedSurface,
3324     ddraw_surface7_EnumAttachedSurfaces,
3325     ddraw_surface7_EnumOverlayZOrders,
3326     ddraw_surface7_Flip,
3327     ddraw_surface7_GetAttachedSurface,
3328     ddraw_surface7_GetBltStatus,
3329     ddraw_surface7_GetCaps,
3330     ddraw_surface7_GetClipper,
3331     ddraw_surface7_GetColorKey,
3332     ddraw_surface7_GetDC,
3333     ddraw_surface7_GetFlipStatus,
3334     ddraw_surface7_GetOverlayPosition,
3335     ddraw_surface7_GetPalette,
3336     ddraw_surface7_GetPixelFormat,
3337     ddraw_surface7_GetSurfaceDesc,
3338     ddraw_surface7_Initialize,
3339     ddraw_surface7_IsLost,
3340     ddraw_surface7_Lock,
3341     ddraw_surface7_ReleaseDC,
3342     ddraw_surface7_Restore,
3343     ddraw_surface7_SetClipper,
3344     ddraw_surface7_SetColorKey,
3345     ddraw_surface7_SetOverlayPosition,
3346     ddraw_surface7_SetPalette,
3347     ddraw_surface7_Unlock,
3348     ddraw_surface7_UpdateOverlay,
3349     ddraw_surface7_UpdateOverlayDisplay,
3350     ddraw_surface7_UpdateOverlayZOrder,
3351     /* IDirectDrawSurface2 */
3352     ddraw_surface7_GetDDInterface,
3353     ddraw_surface7_PageLock,
3354     ddraw_surface7_PageUnlock,
3355     /* IDirectDrawSurface3 */
3356     ddraw_surface7_SetSurfaceDesc,
3357     /* IDirectDrawSurface4 */
3358     ddraw_surface7_SetPrivateData,
3359     ddraw_surface7_GetPrivateData,
3360     ddraw_surface7_FreePrivateData,
3361     ddraw_surface7_GetUniquenessValue,
3362     ddraw_surface7_ChangeUniquenessValue,
3363     /* IDirectDrawSurface7 */
3364     ddraw_surface7_SetPriority,
3365     ddraw_surface7_GetPriority,
3366     ddraw_surface7_SetLOD,
3367     ddraw_surface7_GetLOD,
3368 };
3369
3370 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
3371 {
3372     /* IUnknown */
3373     ddraw_surface3_QueryInterface,
3374     ddraw_surface3_AddRef,
3375     ddraw_surface3_Release,
3376     /* IDirectDrawSurface */
3377     ddraw_surface3_AddAttachedSurface,
3378     ddraw_surface3_AddOverlayDirtyRect,
3379     ddraw_surface3_Blt,
3380     ddraw_surface3_BltBatch,
3381     ddraw_surface3_BltFast,
3382     ddraw_surface3_DeleteAttachedSurface,
3383     ddraw_surface3_EnumAttachedSurfaces,
3384     ddraw_surface3_EnumOverlayZOrders,
3385     ddraw_surface3_Flip,
3386     ddraw_surface3_GetAttachedSurface,
3387     ddraw_surface3_GetBltStatus,
3388     ddraw_surface3_GetCaps,
3389     ddraw_surface3_GetClipper,
3390     ddraw_surface3_GetColorKey,
3391     ddraw_surface3_GetDC,
3392     ddraw_surface3_GetFlipStatus,
3393     ddraw_surface3_GetOverlayPosition,
3394     ddraw_surface3_GetPalette,
3395     ddraw_surface3_GetPixelFormat,
3396     ddraw_surface3_GetSurfaceDesc,
3397     ddraw_surface3_Initialize,
3398     ddraw_surface3_IsLost,
3399     ddraw_surface3_Lock,
3400     ddraw_surface3_ReleaseDC,
3401     ddraw_surface3_Restore,
3402     ddraw_surface3_SetClipper,
3403     ddraw_surface3_SetColorKey,
3404     ddraw_surface3_SetOverlayPosition,
3405     ddraw_surface3_SetPalette,
3406     ddraw_surface3_Unlock,
3407     ddraw_surface3_UpdateOverlay,
3408     ddraw_surface3_UpdateOverlayDisplay,
3409     ddraw_surface3_UpdateOverlayZOrder,
3410     /* IDirectDrawSurface2 */
3411     ddraw_surface3_GetDDInterface,
3412     ddraw_surface3_PageLock,
3413     ddraw_surface3_PageUnlock,
3414     /* IDirectDrawSurface3 */
3415     ddraw_surface3_SetSurfaceDesc,
3416 };
3417
3418 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
3419 {
3420     ddraw_gamma_control_QueryInterface,
3421     ddraw_gamma_control_AddRef,
3422     ddraw_gamma_control_Release,
3423     ddraw_gamma_control_GetGammaRamp,
3424     ddraw_gamma_control_SetGammaRamp,
3425 };
3426
3427 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
3428 {
3429     d3d_texture2_QueryInterface,
3430     d3d_texture2_AddRef,
3431     d3d_texture2_Release,
3432     d3d_texture2_GetHandle,
3433     d3d_texture2_PaletteChanged,
3434     d3d_texture2_Load,
3435 };
3436
3437 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
3438 {
3439     d3d_texture1_QueryInterface,
3440     d3d_texture1_AddRef,
3441     d3d_texture1_Release,
3442     d3d_texture1_Initialize,
3443     d3d_texture1_GetHandle,
3444     d3d_texture1_PaletteChanged,
3445     d3d_texture1_Load,
3446     d3d_texture1_Unload,
3447 };
3448
3449 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
3450 {
3451     IDirectDrawSurfaceImpl *surface = parent;
3452
3453     TRACE("surface %p.\n", surface);
3454
3455     /* Check for attached surfaces and detach them. */
3456     if (surface->first_attached != surface)
3457     {
3458         IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)surface->first_attached;
3459         IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)surface;
3460
3461         /* Well, this shouldn't happen: The surface being attached is
3462          * referenced in AddAttachedSurface(), so it shouldn't be released
3463          * until DeleteAttachedSurface() is called, because the refcount is
3464          * held. It looks like the application released it often enough to
3465          * force this. */
3466         WARN("Surface is still attached to surface %p.\n", surface->first_attached);
3467
3468         /* The refcount will drop to -1 here */
3469         if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
3470             ERR("DeleteAttachedSurface failed.\n");
3471     }
3472
3473     while (surface->next_attached)
3474     {
3475         IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)surface;
3476         IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)surface->next_attached;
3477
3478         if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
3479             ERR("DeleteAttachedSurface failed.\n");
3480     }
3481
3482     /* Having a texture handle set implies that the device still exists. */
3483     if (surface->Handle)
3484         ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
3485
3486     /* Reduce the ddraw surface count. */
3487     InterlockedDecrement(&surface->ddraw->surfaces);
3488     list_remove(&surface->surface_list_entry);
3489
3490     HeapFree(GetProcessHeap(), 0, surface);
3491 }
3492
3493 const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
3494 {
3495     ddraw_surface_wined3d_object_destroyed,
3496 };
3497
3498 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
3499 {
3500     IDirectDrawSurfaceImpl *surface = parent;
3501
3502     TRACE("surface %p.\n", surface);
3503
3504     ddraw_surface_cleanup(surface);
3505 }
3506
3507 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
3508 {
3509     ddraw_texture_wined3d_object_destroyed,
3510 };
3511
3512 HRESULT ddraw_surface_create_texture(IDirectDrawSurfaceImpl *surface)
3513 {
3514     const DDSURFACEDESC2 *desc = &surface->surface_desc;
3515     enum wined3d_format_id format;
3516     WINED3DPOOL pool;
3517     UINT levels;
3518
3519     if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3520         levels = desc->u2.dwMipMapCount;
3521     else
3522         levels = 1;
3523
3524     /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM.
3525      * Should I forward the MANAGED cap to the managed pool? */
3526     if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
3527         pool = WINED3DPOOL_SYSTEMMEM;
3528     else
3529         pool = WINED3DPOOL_DEFAULT;
3530
3531     format = PixelFormat_DD2WineD3D(&surface->surface_desc.u4.ddpfPixelFormat);
3532     if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3533         return IWineD3DDevice_CreateCubeTexture(surface->ddraw->wineD3DDevice, desc->dwWidth,
3534                 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
3535     else
3536         return IWineD3DDevice_CreateTexture(surface->ddraw->wineD3DDevice, desc->dwWidth, desc->dwHeight,
3537                 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
3538 }
3539
3540 HRESULT ddraw_surface_init(IDirectDrawSurfaceImpl *surface, IDirectDrawImpl *ddraw,
3541         DDSURFACEDESC2 *desc, UINT mip_level, WINED3DSURFTYPE surface_type)
3542 {
3543     struct wined3d_resource_desc wined3d_desc;
3544     struct wined3d_resource *wined3d_resource;
3545     WINED3DPOOL pool = WINED3DPOOL_DEFAULT;
3546     enum wined3d_format_id format;
3547     DWORD usage = 0;
3548     HRESULT hr;
3549
3550     if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
3551             && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3552             && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
3553     {
3554         /* Tests show surfaces without memory flags get these flags added
3555          * right after creation. */
3556         desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
3557     }
3558
3559     if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3560     {
3561         usage |= WINED3DUSAGE_RENDERTARGET;
3562         desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
3563     }
3564
3565     if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && !(desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
3566     {
3567         usage |= WINED3DUSAGE_RENDERTARGET;
3568     }
3569
3570     if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
3571     {
3572         usage |= WINED3DUSAGE_OVERLAY;
3573     }
3574
3575     if (ddraw->depthstencil || (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
3576     {
3577         /* The depth stencil creation callback sets this flag. Set the
3578          * wined3d usage to let it know it's a depth/stencil surface. */
3579         usage |= WINED3DUSAGE_DEPTHSTENCIL;
3580     }
3581
3582     if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
3583     {
3584         pool = WINED3DPOOL_SYSTEMMEM;
3585     }
3586     else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
3587     {
3588         pool = WINED3DPOOL_MANAGED;
3589         /* Managed textures have the system memory flag set. */
3590         desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
3591     }
3592     else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
3593     {
3594         /* Videomemory adds localvidmem. This is mutually exclusive with
3595          * systemmemory and texturemanage. */
3596         desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
3597     }
3598
3599     format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
3600     if (format == WINED3DFMT_UNKNOWN)
3601     {
3602         WARN("Unsupported / unknown pixelformat.\n");
3603         return DDERR_INVALIDPIXELFORMAT;
3604     }
3605
3606     surface->lpVtbl = &ddraw_surface7_vtbl;
3607     surface->IDirectDrawSurface3_vtbl = &ddraw_surface3_vtbl;
3608     surface->IDirectDrawGammaControl_vtbl = &ddraw_gamma_control_vtbl;
3609     surface->IDirect3DTexture2_vtbl = &d3d_texture2_vtbl;
3610     surface->IDirect3DTexture_vtbl = &d3d_texture1_vtbl;
3611     surface->ref = 1;
3612     surface->version = 7;
3613     surface->ddraw = ddraw;
3614
3615     surface->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
3616     surface->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3617     DD_STRUCT_COPY_BYSIZE(&surface->surface_desc, desc);
3618
3619     surface->first_attached = surface;
3620     surface->ImplType = surface_type;
3621
3622     hr = IWineD3DDevice_CreateSurface(ddraw->wineD3DDevice, desc->dwWidth, desc->dwHeight, format,
3623             TRUE /* Lockable */, FALSE /* Discard */, mip_level, usage, pool,
3624             WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, surface_type, surface,
3625             &ddraw_surface_wined3d_parent_ops, &surface->WineD3DSurface);
3626     if (FAILED(hr))
3627     {
3628         WARN("Failed to create wined3d surface, hr %#x.\n", hr);
3629         return hr;
3630     }
3631
3632     surface->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
3633     wined3d_resource = IWineD3DSurface_GetResource(surface->WineD3DSurface);
3634     wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
3635
3636     format = wined3d_desc.format;
3637     if (format == WINED3DFMT_UNKNOWN)
3638     {
3639         FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN.\n");
3640     }
3641     PixelFormat_WineD3DtoDD(&surface->surface_desc.u4.ddpfPixelFormat, format);
3642
3643     /* Anno 1602 stores the pitch right after surface creation, so make sure
3644      * it's there. TODO: Test other fourcc formats. */
3645     if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
3646             || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
3647     {
3648         surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
3649         if (format == WINED3DFMT_DXT1)
3650         {
3651             surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height) / 2;
3652         }
3653         else
3654         {
3655             surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height);
3656         }
3657     }
3658     else
3659     {
3660         surface->surface_desc.dwFlags |= DDSD_PITCH;
3661         surface->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch(surface->WineD3DSurface);
3662     }
3663
3664     if (desc->dwFlags & DDSD_CKDESTOVERLAY)
3665     {
3666         IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3667                 DDCKEY_DESTOVERLAY, (WINEDDCOLORKEY *)&desc->u3.ddckCKDestOverlay);
3668     }
3669     if (desc->dwFlags & DDSD_CKDESTBLT)
3670     {
3671         IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3672                 DDCKEY_DESTBLT, (WINEDDCOLORKEY *)&desc->ddckCKDestBlt);
3673     }
3674     if (desc->dwFlags & DDSD_CKSRCOVERLAY)
3675     {
3676         IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3677                 DDCKEY_SRCOVERLAY, (WINEDDCOLORKEY *)&desc->ddckCKSrcOverlay);
3678     }
3679     if (desc->dwFlags & DDSD_CKSRCBLT)
3680     {
3681         IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3682                 DDCKEY_SRCBLT, (WINEDDCOLORKEY *)&desc->ddckCKSrcBlt);
3683     }
3684     if (desc->dwFlags & DDSD_LPSURFACE)
3685     {
3686         hr = IWineD3DSurface_SetMem(surface->WineD3DSurface, desc->lpSurface);
3687         if (FAILED(hr))
3688         {
3689             ERR("Failed to set surface memory, hr %#x.\n", hr);
3690             IWineD3DSurface_Release(surface->WineD3DSurface);
3691             return hr;
3692         }
3693     }
3694
3695     return DD_OK;
3696 }