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