ddraw: Separate IDirectDrawSurface4 reference count.
[wine.git] / dlls / ddraw / surface.c
blob248aa376543918fc135db3db26670ce6b419275f
1 /* DirectDraw Surface Implementation
3 * Copyright (c) 1997-2000 Marcus Meissner
4 * Copyright (c) 1998-2000 Lionel Ulmer
5 * Copyright (c) 2000-2001 TransGaming Technologies Inc.
6 * Copyright (c) 2006 Stefan Dösinger
7 * Copyright (c) 2011 Ričardas Barkauskas for CodeWeavers
9 * This file contains the (internal) driver registration functions,
10 * driver enumeration APIs and DirectDraw creation functions.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include "ddraw_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
34 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface);
35 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface);
37 static inline IDirectDrawSurfaceImpl *impl_from_IDirectDrawGammaControl(IDirectDrawGammaControl *iface)
39 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawGammaControl_iface);
42 /*****************************************************************************
43 * IUnknown parts follow
44 *****************************************************************************/
46 /*****************************************************************************
47 * IDirectDrawSurface7::QueryInterface
49 * A normal QueryInterface implementation. For QueryInterface rules
50 * see ddraw.c, IDirectDraw7::QueryInterface. This method
51 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
52 * in all versions, the IDirectDrawGammaControl interface and it can
53 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
55 * Params:
56 * riid: The interface id queried for
57 * obj: Address to write the pointer to
59 * Returns:
60 * S_OK on success
61 * E_NOINTERFACE if the requested interface wasn't found
63 *****************************************************************************/
64 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
66 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
68 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
70 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
71 *obj = NULL;
73 if(!riid)
74 return DDERR_INVALIDPARAMS;
76 if (IsEqualGUID(riid, &IID_IUnknown)
77 || IsEqualGUID(riid, &IID_IDirectDrawSurface7) )
79 IUnknown_AddRef(iface);
80 *obj = iface;
81 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
82 return S_OK;
84 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
86 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
87 *obj = &This->IDirectDrawSurface4_iface;
88 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
89 return S_OK;
91 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
93 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
94 *obj = &This->IDirectDrawSurface3_iface;
95 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
96 return S_OK;
98 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
100 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
101 *obj = &This->IDirectDrawSurface2_iface;
102 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
103 return S_OK;
105 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface))
107 IUnknown_AddRef(iface);
108 *obj = &This->IDirectDrawSurface_iface;
109 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
110 return S_OK;
112 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
114 IUnknown_AddRef(iface);
115 *obj = &This->IDirectDrawGammaControl_iface;
116 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
117 return S_OK;
119 else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
120 IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
121 IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
123 IDirect3DDevice7 *d3d;
125 /* Call into IDirect3D7 for creation */
126 IDirect3D7_CreateDevice(&This->ddraw->IDirect3D7_iface, riid, &This->IDirectDrawSurface7_iface,
127 &d3d);
129 if (d3d)
131 *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
132 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
133 return S_OK;
136 WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
137 return E_NOINTERFACE;
139 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
140 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
142 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
144 *obj = &This->IDirect3DTexture_vtbl;
145 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
147 else
149 *obj = &This->IDirect3DTexture2_vtbl;
150 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
152 IUnknown_AddRef( (IUnknown *) *obj);
153 return S_OK;
156 ERR("No interface\n");
157 return E_NOINTERFACE;
160 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
162 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
163 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
165 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
168 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
170 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
171 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
173 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
176 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
178 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
179 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
181 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
184 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
186 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
187 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
189 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
192 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
193 REFIID riid, void **object)
195 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
197 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
199 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
202 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
204 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
205 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
207 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
210 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
212 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
213 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
215 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
218 static void ddraw_surface_add_iface(IDirectDrawSurfaceImpl *This)
220 ULONG iface_count = InterlockedIncrement(&This->iface_count);
221 TRACE("%p increasing iface count to %u.\n", This, iface_count);
223 if (iface_count == 1)
225 EnterCriticalSection(&ddraw_cs);
226 if (This->wined3d_surface)
227 wined3d_surface_incref(This->wined3d_surface);
228 if (This->wined3d_texture)
229 wined3d_texture_incref(This->wined3d_texture);
230 LeaveCriticalSection(&ddraw_cs);
234 /*****************************************************************************
235 * IDirectDrawSurface7::AddRef
237 * A normal addref implementation
239 * Returns:
240 * The new refcount
242 *****************************************************************************/
243 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
245 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
246 ULONG refcount = InterlockedIncrement(&This->ref);
248 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
250 if (refcount == 1)
252 ddraw_surface_add_iface(This);
255 return refcount;
258 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
260 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
261 ULONG refcount = InterlockedIncrement(&This->ref4);
263 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
265 if (refcount == 1)
267 ddraw_surface_add_iface(This);
270 return refcount;
273 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
275 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
276 ULONG refcount = InterlockedIncrement(&This->ref3);
278 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
280 if (refcount == 1)
282 ddraw_surface_add_iface(This);
285 return refcount;
288 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
290 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
291 ULONG refcount = InterlockedIncrement(&This->ref2);
293 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
295 if (refcount == 1)
297 ddraw_surface_add_iface(This);
300 return refcount;
303 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
305 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
306 TRACE("iface %p.\n", iface);
308 return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
311 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
313 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
315 TRACE("iface %p.\n", iface);
317 return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
320 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
322 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
323 TRACE("iface %p.\n", iface);
325 return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
328 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
330 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
331 TRACE("iface %p.\n", iface);
333 return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
336 /*****************************************************************************
337 * ddraw_surface_destroy
339 * A helper function for IDirectDrawSurface7::Release
341 * Frees the surface, regardless of its refcount.
342 * See IDirectDrawSurface7::Release for more information
344 * Params:
345 * This: Surface to free
347 *****************************************************************************/
348 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
350 TRACE("surface %p.\n", This);
352 /* Check the iface count and give a warning */
353 if(This->iface_count > 1)
355 /* This can happen when a complex surface is destroyed,
356 * because the 2nd surface was addref()ed when the app
357 * called GetAttachedSurface
359 WARN("(%p): Destroying surface with refcounts %d 4: %d 3: %d 2: %d\n",
360 This, This->ref, This->ref4, This->ref3, This->ref2);
363 if (This->wined3d_surface)
364 wined3d_surface_decref(This->wined3d_surface);
367 static void ddraw_surface_cleanup(IDirectDrawSurfaceImpl *surface)
369 IDirectDrawSurfaceImpl *surf;
370 IUnknown *ifaceToRelease;
371 UINT i;
373 TRACE("surface %p.\n", surface);
375 if (surface->wined3d_swapchain)
377 IDirectDrawImpl *ddraw = surface->ddraw;
379 /* If it's the render target, destroy the D3D device. */
380 if (ddraw->d3d_initialized && surface == ddraw->d3d_target)
382 TRACE("Destroying the render target, uninitializing D3D.\n");
384 for (i = 0; i < ddraw->numConvertedDecls; ++i)
386 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
388 HeapFree(GetProcessHeap(), 0, ddraw->decls);
389 ddraw->numConvertedDecls = 0;
391 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
393 ERR("Failed to uninit 3D.\n");
395 else
397 /* Free the d3d window if one was created. */
398 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
400 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
401 DestroyWindow(ddraw->d3d_window);
402 ddraw->d3d_window = 0;
406 ddraw->d3d_initialized = FALSE;
407 ddraw->d3d_target = NULL;
409 else
411 wined3d_device_uninit_gdi(ddraw->wined3d_device);
414 surface->wined3d_swapchain = NULL;
416 /* Reset to the default surface implementation type. This is needed
417 * if applications use non render target surfaces and expect blits to
418 * work after destroying the render target.
420 * TODO: Recreate existing offscreen surfaces. */
421 ddraw->ImplType = DefaultSurfaceType;
423 TRACE("D3D unloaded.\n");
426 /* The refcount test shows that the palette is detached when the surface
427 * is destroyed. */
428 IDirectDrawSurface7_SetPalette(&surface->IDirectDrawSurface7_iface, NULL);
430 /* Loop through all complex attached surfaces and destroy them.
432 * Yet again, only the root can have more than one complexly attached
433 * surface, all the others have a total of one. */
434 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
436 if (!surface->complex_array[i])
437 break;
439 surf = surface->complex_array[i];
440 surface->complex_array[i] = NULL;
441 while (surf)
443 IDirectDrawSurfaceImpl *destroy = surf;
444 surf = surf->complex_array[0]; /* Iterate through the "tree" */
445 ddraw_surface_destroy(destroy); /* Destroy it */
449 ifaceToRelease = surface->ifaceToRelease;
451 /* Destroy the root surface. */
452 ddraw_surface_destroy(surface);
454 /* Reduce the ddraw refcount */
455 if (ifaceToRelease)
456 IUnknown_Release(ifaceToRelease);
459 ULONG ddraw_surface_release_iface(IDirectDrawSurfaceImpl *This)
461 ULONG iface_count = InterlockedDecrement(&This->iface_count);
462 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
464 if (iface_count == 0)
466 /* Complex attached surfaces are destroyed implicitly when the root is released */
467 EnterCriticalSection(&ddraw_cs);
468 if(!This->is_complex_root)
470 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
471 LeaveCriticalSection(&ddraw_cs);
472 return iface_count;
474 if (This->wined3d_texture) /* If it's a texture, destroy the wined3d texture. */
475 wined3d_texture_decref(This->wined3d_texture);
476 else
477 ddraw_surface_cleanup(This);
478 LeaveCriticalSection(&ddraw_cs);
481 return iface_count;
484 /*****************************************************************************
485 * IDirectDrawSurface7::Release
487 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
488 * surface is destroyed.
490 * Destroying the surface is a bit tricky. For the connection between
491 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
492 * It has a nice graph explaining the connection.
494 * What happens here is basically this:
495 * When a surface is destroyed, its WineD3DSurface is released,
496 * and the refcount of the DirectDraw interface is reduced by 1. If it has
497 * complex surfaces attached to it, then these surfaces are destroyed too,
498 * regardless of their refcount. If any surface being destroyed has another
499 * surface attached to it (with a "soft" attachment, not complex), then
500 * this surface is detached with DeleteAttachedSurface.
502 * When the surface is a texture, the WineD3DTexture is released.
503 * If the surface is the Direct3D render target, then the D3D
504 * capabilities of the WineD3DDevice are uninitialized, which causes the
505 * swapchain to be released.
507 * When a complex sublevel falls to ref zero, then this is ignored.
509 * Returns:
510 * The new refcount
512 *****************************************************************************/
513 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
515 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
516 ULONG refcount = InterlockedDecrement(&This->ref);
518 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
520 if (refcount == 0)
522 ddraw_surface_release_iface(This);
525 return refcount;
528 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
530 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
531 ULONG refcount = InterlockedDecrement(&This->ref4);
533 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
535 if (refcount == 0)
537 ddraw_surface_release_iface(This);
540 return refcount;
543 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
545 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
546 ULONG refcount = InterlockedDecrement(&This->ref3);
548 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
550 if (refcount == 0)
552 ddraw_surface_release_iface(This);
555 return refcount;
558 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
560 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
561 ULONG refcount = InterlockedDecrement(&This->ref2);
563 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
565 if (refcount == 0)
567 ddraw_surface_release_iface(This);
570 return refcount;
573 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
575 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
576 TRACE("iface %p.\n", iface);
578 return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
581 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
583 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
585 TRACE("iface %p.\n", iface);
587 return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
590 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
592 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
593 TRACE("iface %p.\n", iface);
595 return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
598 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
600 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
601 TRACE("iface %p.\n", iface);
603 return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
606 /*****************************************************************************
607 * IDirectDrawSurface7::GetAttachedSurface
609 * Returns an attached surface with the requested caps. Surface attachment
610 * and complex surfaces are not clearly described by the MSDN or sdk,
611 * so this method is tricky and likely to contain problems.
612 * This implementation searches the complex list first, then the
613 * attachment chain.
615 * The chains are searched from This down to the last surface in the chain,
616 * not from the first element in the chain. The first surface found is
617 * returned. The MSDN says that this method fails if more than one surface
618 * matches the caps, but it is not sure if that is right. The attachment
619 * structure may not even allow two matching surfaces.
621 * The found surface is AddRef-ed before it is returned.
623 * Params:
624 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
625 * Surface: Address to store the found surface
627 * Returns:
628 * DD_OK on success
629 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
630 * DDERR_NOTFOUND if no surface was found
632 *****************************************************************************/
633 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
634 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
636 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
637 IDirectDrawSurfaceImpl *surf;
638 DDSCAPS2 our_caps;
639 int i;
641 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
643 EnterCriticalSection(&ddraw_cs);
645 if(This->version < 7)
647 /* Earlier dx apps put garbage into these members, clear them */
648 our_caps.dwCaps = Caps->dwCaps;
649 our_caps.dwCaps2 = 0;
650 our_caps.dwCaps3 = 0;
651 our_caps.dwCaps4 = 0;
653 else
655 our_caps = *Caps;
658 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 */
660 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
662 surf = This->complex_array[i];
663 if(!surf) break;
665 if (TRACE_ON(ddraw))
667 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
668 surf->surface_desc.ddsCaps.dwCaps,
669 surf->surface_desc.ddsCaps.dwCaps2,
670 surf->surface_desc.ddsCaps.dwCaps3,
671 surf->surface_desc.ddsCaps.dwCaps4);
674 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
675 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
677 /* MSDN: "This method fails if more than one surface is attached
678 * that matches the capabilities requested."
680 * Not sure how to test this.
683 TRACE("(%p): Returning surface %p\n", This, surf);
684 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
685 *Surface = &surf->IDirectDrawSurface7_iface;
686 ddraw_surface7_AddRef(*Surface);
687 LeaveCriticalSection(&ddraw_cs);
688 return DD_OK;
692 /* Next, look at the attachment chain */
693 surf = This;
695 while( (surf = surf->next_attached) )
697 if (TRACE_ON(ddraw))
699 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
700 surf->surface_desc.ddsCaps.dwCaps,
701 surf->surface_desc.ddsCaps.dwCaps2,
702 surf->surface_desc.ddsCaps.dwCaps3,
703 surf->surface_desc.ddsCaps.dwCaps4);
706 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
707 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
709 TRACE("(%p): Returning surface %p\n", This, surf);
710 *Surface = &surf->IDirectDrawSurface7_iface;
711 ddraw_surface7_AddRef(*Surface);
712 LeaveCriticalSection(&ddraw_cs);
713 return DD_OK;
717 TRACE("(%p) Didn't find a valid surface\n", This);
718 LeaveCriticalSection(&ddraw_cs);
720 *Surface = NULL;
721 return DDERR_NOTFOUND;
724 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
725 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
727 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
728 IDirectDrawSurface7 *attachment7;
729 IDirectDrawSurfaceImpl *attachment_impl;
730 HRESULT hr;
732 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
734 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
735 caps, &attachment7);
736 if (FAILED(hr))
738 *attachment = NULL;
739 return hr;
741 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
742 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
743 ddraw_surface4_AddRef(*attachment);
744 ddraw_surface7_Release(attachment7);
746 return hr;
749 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
750 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
752 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
753 IDirectDrawSurface7 *attachment7;
754 IDirectDrawSurfaceImpl *attachment_impl;
755 DDSCAPS2 caps2;
756 HRESULT hr;
758 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
760 caps2.dwCaps = caps->dwCaps;
761 caps2.dwCaps2 = 0;
762 caps2.dwCaps3 = 0;
763 caps2.dwCaps4 = 0;
765 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
766 &caps2, &attachment7);
767 if (FAILED(hr))
769 *attachment = NULL;
770 return hr;
772 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
773 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
774 ddraw_surface3_AddRef(*attachment);
775 ddraw_surface7_Release(attachment7);
777 return hr;
780 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
781 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
783 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
784 IDirectDrawSurface7 *attachment7;
785 IDirectDrawSurfaceImpl *attachment_impl;
786 DDSCAPS2 caps2;
787 HRESULT hr;
789 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
791 caps2.dwCaps = caps->dwCaps;
792 caps2.dwCaps2 = 0;
793 caps2.dwCaps3 = 0;
794 caps2.dwCaps4 = 0;
796 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
797 &caps2, &attachment7);
798 if (FAILED(hr))
800 *attachment = NULL;
801 return hr;
803 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
804 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
805 ddraw_surface2_AddRef(*attachment);
806 ddraw_surface7_Release(attachment7);
808 return hr;
811 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
812 DDSCAPS *caps, IDirectDrawSurface **attachment)
814 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
815 IDirectDrawSurface7 *attachment7;
816 IDirectDrawSurfaceImpl *attachment_impl;
817 DDSCAPS2 caps2;
818 HRESULT hr;
820 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
822 caps2.dwCaps = caps->dwCaps;
823 caps2.dwCaps2 = 0;
824 caps2.dwCaps3 = 0;
825 caps2.dwCaps4 = 0;
827 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
828 &caps2, &attachment7);
829 if (FAILED(hr))
831 *attachment = NULL;
832 return hr;
834 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
835 *attachment = &attachment_impl->IDirectDrawSurface_iface;
837 return hr;
840 /*****************************************************************************
841 * IDirectDrawSurface7::Lock
843 * Locks the surface and returns a pointer to the surface's memory
845 * Params:
846 * Rect: Rectangle to lock. If NULL, the whole surface is locked
847 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
848 * Flags: Locking flags, e.g Read only or write only
849 * h: An event handle that's not used and must be NULL
851 * Returns:
852 * DD_OK on success
853 * DDERR_INVALIDPARAMS if DDSD is NULL
854 * For more details, see IWineD3DSurface::LockRect
856 *****************************************************************************/
857 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
858 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
860 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
861 WINED3DLOCKED_RECT LockedRect;
862 HRESULT hr;
864 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
865 iface, wine_dbgstr_rect(Rect), DDSD, Flags, h);
867 if(!DDSD)
868 return DDERR_INVALIDPARAMS;
870 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
871 EnterCriticalSection(&ddraw_cs);
873 /* Should I check for the handle to be NULL?
875 * The DDLOCK flags and the D3DLOCK flags are equal
876 * for the supported values. The others are ignored by WineD3D
879 if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
880 DDSD->dwSize != sizeof(DDSURFACEDESC2))
882 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDSD->dwSize);
883 LeaveCriticalSection(&ddraw_cs);
884 return DDERR_INVALIDPARAMS;
887 /* Windows zeroes this if the rect is invalid */
888 DDSD->lpSurface = 0;
890 if (Rect)
892 if ((Rect->left < 0)
893 || (Rect->top < 0)
894 || (Rect->left > Rect->right)
895 || (Rect->top > Rect->bottom)
896 || (Rect->right > This->surface_desc.dwWidth)
897 || (Rect->bottom > This->surface_desc.dwHeight))
899 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
900 LeaveCriticalSection(&ddraw_cs);
901 return DDERR_INVALIDPARAMS;
905 hr = wined3d_surface_map(This->wined3d_surface, &LockedRect, Rect, Flags);
906 if (FAILED(hr))
908 LeaveCriticalSection(&ddraw_cs);
909 switch(hr)
911 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
912 * specific error. But since IWineD3DSurface::LockRect returns that error in this
913 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
914 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
915 * is much easier to do it in one place in ddraw
917 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
918 default: return hr;
922 /* Override the memory area. The pitch should be set already. Strangely windows
923 * does not set the LPSURFACE flag on locked surfaces !?!.
924 * DDSD->dwFlags |= DDSD_LPSURFACE;
926 This->surface_desc.lpSurface = LockedRect.pBits;
927 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
929 TRACE("locked surface returning description :\n");
930 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
932 LeaveCriticalSection(&ddraw_cs);
933 return DD_OK;
936 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
937 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
939 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
940 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
941 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
943 return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
944 rect, surface_desc, flags, h);
947 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
948 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
950 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
951 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
952 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
954 return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
955 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
958 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
959 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
961 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
962 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
963 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
965 return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
966 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
969 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
970 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
972 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
973 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
974 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
976 return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
977 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
980 /*****************************************************************************
981 * IDirectDrawSurface7::Unlock
983 * Unlocks an locked surface
985 * Params:
986 * Rect: Not used by this implementation
988 * Returns:
989 * D3D_OK on success
990 * For more details, see IWineD3DSurface::UnlockRect
992 *****************************************************************************/
993 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
995 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
996 HRESULT hr;
998 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1000 EnterCriticalSection(&ddraw_cs);
1001 hr = wined3d_surface_unmap(This->wined3d_surface);
1002 if (SUCCEEDED(hr))
1004 This->surface_desc.lpSurface = NULL;
1006 LeaveCriticalSection(&ddraw_cs);
1007 return hr;
1010 static HRESULT WINAPI ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1012 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1013 TRACE("iface %p, rect %p.\n", iface, pRect);
1015 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, pRect);
1018 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1020 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1021 TRACE("iface %p, data %p.\n", iface, data);
1023 /* data might not be the LPRECT of later versions, so drop it. */
1024 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1027 static HRESULT WINAPI ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1029 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1030 TRACE("iface %p, data %p.\n", iface, data);
1032 /* data might not be the LPRECT of later versions, so drop it. */
1033 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1036 static HRESULT WINAPI ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1038 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1039 TRACE("iface %p, data %p.\n", iface, data);
1041 /* data might not be the LPRECT of later versions, so drop it. */
1042 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1045 /*****************************************************************************
1046 * IDirectDrawSurface7::Flip
1048 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
1049 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
1050 * the flip target is passed to WineD3D, even if the app didn't specify one
1052 * Params:
1053 * DestOverride: Specifies the surface that will become the new front
1054 * buffer. If NULL, the current back buffer is used
1055 * Flags: some DirectDraw flags, see include/ddraw.h
1057 * Returns:
1058 * DD_OK on success
1059 * DDERR_NOTFLIPPABLE if no flip target could be found
1060 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
1061 * For more details, see IWineD3DSurface::Flip
1063 *****************************************************************************/
1064 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
1066 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1067 IDirectDrawSurfaceImpl *Override = unsafe_impl_from_IDirectDrawSurface7(DestOverride);
1068 IDirectDrawSurface7 *Override7;
1069 HRESULT hr;
1071 TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
1073 /* Flip has to be called from a front buffer
1074 * What about overlay surfaces, AFAIK they can flip too?
1076 if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
1077 return DDERR_INVALIDOBJECT; /* Unchecked */
1079 EnterCriticalSection(&ddraw_cs);
1081 /* WineD3D doesn't keep track of attached surface, so find the target */
1082 if(!Override)
1084 DDSCAPS2 Caps;
1086 memset(&Caps, 0, sizeof(Caps));
1087 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
1088 hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
1089 if(hr != DD_OK)
1091 ERR("Can't find a flip target\n");
1092 LeaveCriticalSection(&ddraw_cs);
1093 return DDERR_NOTFLIPPABLE; /* Unchecked */
1095 Override = impl_from_IDirectDrawSurface7(Override7);
1097 /* For the GetAttachedSurface */
1098 ddraw_surface7_Release(Override7);
1101 hr = wined3d_surface_flip(This->wined3d_surface, Override->wined3d_surface, Flags);
1102 LeaveCriticalSection(&ddraw_cs);
1103 return hr;
1106 static HRESULT WINAPI ddraw_surface4_Flip(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *dst, DWORD flags)
1108 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1109 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1110 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1112 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1113 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1116 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
1118 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1119 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1120 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1122 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1123 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1126 static HRESULT WINAPI ddraw_surface2_Flip(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *dst, DWORD flags)
1128 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1129 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1130 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1132 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1133 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1136 static HRESULT WINAPI ddraw_surface1_Flip(IDirectDrawSurface *iface, IDirectDrawSurface *dst, DWORD flags)
1138 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1139 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1140 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1142 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1143 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1146 /*****************************************************************************
1147 * IDirectDrawSurface7::Blt
1149 * Performs a blit on the surface
1151 * Params:
1152 * DestRect: Destination rectangle, can be NULL
1153 * SrcSurface: Source surface, can be NULL
1154 * SrcRect: Source rectangle, can be NULL
1155 * Flags: Blt flags
1156 * DDBltFx: Some extended blt parameters, connected to the flags
1158 * Returns:
1159 * D3D_OK on success
1160 * See IWineD3DSurface::Blt for more details
1162 *****************************************************************************/
1163 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1164 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1166 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1167 IDirectDrawSurfaceImpl *Src = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1168 HRESULT hr;
1170 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1171 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1173 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1174 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1176 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1177 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1178 return DDERR_INVALIDPARAMS;
1181 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1182 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1183 return DDERR_INVALIDPARAMS;
1186 /* Sizes can change, therefore hold the lock when testing the rectangles */
1187 EnterCriticalSection(&ddraw_cs);
1188 if(DestRect)
1190 if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
1191 DestRect->right > This->surface_desc.dwWidth ||
1192 DestRect->bottom > This->surface_desc.dwHeight)
1194 WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
1195 LeaveCriticalSection(&ddraw_cs);
1196 return DDERR_INVALIDRECT;
1199 if(Src && SrcRect)
1201 if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
1202 SrcRect->right > Src->surface_desc.dwWidth ||
1203 SrcRect->bottom > Src->surface_desc.dwHeight)
1205 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
1206 LeaveCriticalSection(&ddraw_cs);
1207 return DDERR_INVALIDRECT;
1211 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
1212 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1213 LeaveCriticalSection(&ddraw_cs);
1214 return DDERR_INVALIDPARAMS;
1217 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1218 * does, copy the struct, and replace the ddraw surfaces with the wined3d
1219 * surfaces. So far no blitting operations using surfaces in the bltfx
1220 * struct are supported anyway. */
1221 hr = wined3d_surface_blt(This->wined3d_surface, DestRect, Src ? Src->wined3d_surface : NULL,
1222 SrcRect, Flags, (WINEDDBLTFX *)DDBltFx, WINED3DTEXF_LINEAR);
1224 LeaveCriticalSection(&ddraw_cs);
1225 switch(hr)
1227 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1228 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
1229 default: return hr;
1233 static HRESULT WINAPI ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1234 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1236 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1237 IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1238 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1239 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1241 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1242 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1245 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1246 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1248 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1249 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1250 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1251 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1253 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1254 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1257 static HRESULT WINAPI ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1258 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1260 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1261 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1262 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1263 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1265 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1266 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1269 static HRESULT WINAPI ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1270 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1272 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1273 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1274 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1275 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1277 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1278 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1281 /*****************************************************************************
1282 * IDirectDrawSurface7::AddAttachedSurface
1284 * Attaches a surface to another surface. How the surface attachments work
1285 * is not totally understood yet, and this method is prone to problems.
1286 * he surface that is attached is AddRef-ed.
1288 * Tests with complex surfaces suggest that the surface attachments form a
1289 * tree, but no method to test this has been found yet.
1291 * The attachment list consists of a first surface (first_attached) and
1292 * for each surface a pointer to the next attached surface (next_attached).
1293 * For the first surface, and a surface that has no attachments
1294 * first_attached points to the surface itself. A surface that has
1295 * no successors in the chain has next_attached set to NULL.
1297 * Newly attached surfaces are attached right after the root surface.
1298 * If a surface is attached to a complex surface compound, it's attached to
1299 * the surface that the app requested, not the complex root. See
1300 * GetAttachedSurface for a description how surfaces are found.
1302 * This is how the current implementation works, and it was coded by looking
1303 * at the needs of the applications.
1305 * So far only Z-Buffer attachments are tested, and they are activated in
1306 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1307 * Back buffers should work in 2D mode, but they are not tested(They can be
1308 * attached in older iface versions). Rendering to the front buffer and
1309 * switching between that and double buffering is not yet implemented in
1310 * WineD3D, so for 3D it might have unexpected results.
1312 * ddraw_surface_attach_surface is the real thing,
1313 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1314 * performs additional checks. Version 7 of this interface is much more restrictive
1315 * than its predecessors.
1317 * Params:
1318 * Attach: Surface to attach to iface
1320 * Returns:
1321 * DD_OK on success
1322 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1324 *****************************************************************************/
1325 static HRESULT ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
1327 TRACE("surface %p, attachment %p.\n", This, Surf);
1329 if(Surf == This)
1330 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1332 EnterCriticalSection(&ddraw_cs);
1334 /* Check if the surface is already attached somewhere */
1335 if (Surf->next_attached || Surf->first_attached != Surf)
1337 /* TODO: Test for the structure of the manual attachment. Is it a
1338 * chain or a list? What happens if one surface is attached to 2
1339 * different surfaces? */
1340 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1341 Surf, Surf->next_attached, Surf->first_attached);
1343 LeaveCriticalSection(&ddraw_cs);
1344 return DDERR_SURFACEALREADYATTACHED;
1347 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1348 Surf->next_attached = This->next_attached;
1349 Surf->first_attached = This->first_attached;
1350 This->next_attached = Surf;
1352 /* Check if the WineD3D depth stencil needs updating */
1353 if(This->ddraw->d3ddevice)
1355 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1358 LeaveCriticalSection(&ddraw_cs);
1359 return DD_OK;
1362 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
1364 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1365 IDirectDrawSurfaceImpl *Surf = unsafe_impl_from_IDirectDrawSurface7(Attach);
1366 HRESULT hr;
1368 TRACE("iface %p, attachment %p.\n", iface, Attach);
1370 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1371 if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1374 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1375 Surf->surface_desc.ddsCaps.dwCaps);
1376 return DDERR_CANNOTATTACHSURFACE;
1379 hr = ddraw_surface_attach_surface(This, Surf);
1380 if (FAILED(hr))
1382 return hr;
1384 ddraw_surface7_AddRef(Attach);
1385 return hr;
1388 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1390 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1391 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1392 HRESULT hr;
1394 TRACE("iface %p, attachment %p.\n", iface, attachment);
1396 hr = ddraw_surface7_AddAttachedSurface(&This->IDirectDrawSurface7_iface,
1397 attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1398 if (FAILED(hr))
1400 return hr;
1402 ddraw_surface4_AddRef(attachment);
1403 ddraw_surface7_Release(&attachment_impl->IDirectDrawSurface7_iface);
1404 return hr;
1406 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1408 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1409 IDirectDrawSurfaceImpl *attach_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1410 HRESULT hr;
1412 TRACE("iface %p, attachment %p.\n", iface, attachment);
1414 /* Tests suggest that
1415 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1416 * -> offscreen plain surfaces can be attached to primaries
1417 * -> primaries can be attached to offscreen plain surfaces
1418 * -> z buffers can be attached to primaries */
1419 if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1420 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1422 /* Sizes have to match */
1423 if (attach_impl->surface_desc.dwWidth != This->surface_desc.dwWidth
1424 || attach_impl->surface_desc.dwHeight != This->surface_desc.dwHeight)
1426 WARN("Surface sizes do not match.\n");
1427 return DDERR_CANNOTATTACHSURFACE;
1429 /* OK */
1431 else if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1432 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1434 /* OK */
1436 else
1438 WARN("Invalid attachment combination.\n");
1439 return DDERR_CANNOTATTACHSURFACE;
1442 hr = ddraw_surface_attach_surface(This, attach_impl);
1443 if (FAILED(hr))
1445 return hr;
1447 ddraw_surface3_AddRef(attachment);
1448 return hr;
1451 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1453 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1454 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1455 HRESULT hr;
1457 TRACE("iface %p, attachment %p.\n", iface, attachment);
1459 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1460 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1461 if (FAILED(hr))
1463 return hr;
1465 ddraw_surface2_AddRef(attachment);
1466 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1467 return hr;
1470 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1472 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1473 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1474 HRESULT hr;
1476 TRACE("iface %p, attachment %p.\n", iface, attachment);
1478 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1479 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1480 if (FAILED(hr))
1482 return hr;
1484 ddraw_surface1_AddRef(attachment);
1485 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1486 return hr;
1489 /*****************************************************************************
1490 * IDirectDrawSurface7::DeleteAttachedSurface
1492 * Removes a surface from the attachment chain. The surface's refcount
1493 * is decreased by one after it has been removed
1495 * Params:
1496 * Flags: Some flags, not used by this implementation
1497 * Attach: Surface to detach
1499 * Returns:
1500 * DD_OK on success
1501 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1503 *****************************************************************************/
1504 static HRESULT ddraw_surface_delete_attached_surface(IDirectDrawSurfaceImpl *This,
1505 IDirectDrawSurfaceImpl *Surf)
1507 IDirectDrawSurfaceImpl *Prev = This;
1509 TRACE("surface %p, attachment %p.\n", This, Surf);
1511 EnterCriticalSection(&ddraw_cs);
1512 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1514 LeaveCriticalSection(&ddraw_cs);
1515 return DDERR_CANNOTDETACHSURFACE;
1518 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1519 if (This->surface_desc.ddsCaps.dwCaps &
1520 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1522 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1523 /* FIXME: we should probably also subtract from dwMipMapCount of this
1524 * and all parent surfaces */
1527 /* Find the predecessor of the detached surface */
1528 while(Prev)
1530 if(Prev->next_attached == Surf) break;
1531 Prev = Prev->next_attached;
1534 /* There must be a surface, otherwise there's a bug */
1535 assert(Prev != NULL);
1537 /* Unchain the surface */
1538 Prev->next_attached = Surf->next_attached;
1539 Surf->next_attached = NULL;
1540 Surf->first_attached = Surf;
1542 /* Check if the WineD3D depth stencil needs updating */
1543 if(This->ddraw->d3ddevice)
1545 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1547 LeaveCriticalSection(&ddraw_cs);
1548 return DD_OK;
1551 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1552 DWORD flags, IDirectDrawSurface7 *attachment)
1554 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1555 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1556 HRESULT hr;
1558 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1560 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1561 if (FAILED(hr))
1563 return hr;
1565 ddraw_surface7_Release(attachment);
1566 return hr;
1569 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1570 DWORD flags, IDirectDrawSurface4 *attachment)
1572 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1573 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1574 HRESULT hr;
1576 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1578 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1579 if (FAILED(hr))
1581 return hr;
1583 ddraw_surface4_Release(attachment);
1584 return hr;
1587 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1588 DWORD flags, IDirectDrawSurface3 *attachment)
1590 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1591 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1592 HRESULT hr;
1593 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1595 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1596 if (FAILED(hr))
1598 return hr;
1600 ddraw_surface3_Release(attachment);
1601 return hr;
1604 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
1605 DWORD flags, IDirectDrawSurface2 *attachment)
1607 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1608 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1609 HRESULT hr;
1610 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1612 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1613 if (FAILED(hr))
1615 return hr;
1617 ddraw_surface2_Release(attachment);
1618 return hr;
1621 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
1622 DWORD flags, IDirectDrawSurface *attachment)
1624 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1625 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1626 HRESULT hr;
1627 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1629 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1630 if (FAILED(hr))
1632 return hr;
1634 ddraw_surface1_Release(attachment);
1635 return hr;
1638 /*****************************************************************************
1639 * IDirectDrawSurface7::AddOverlayDirtyRect
1641 * "This method is not currently implemented"
1643 * Params:
1644 * Rect: ?
1646 * Returns:
1647 * DDERR_UNSUPPORTED
1649 *****************************************************************************/
1650 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1652 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1654 return DDERR_UNSUPPORTED; /* unchecked */
1657 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
1659 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1660 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1662 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1665 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1667 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1668 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1670 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1673 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
1675 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1676 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1678 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1681 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
1683 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1684 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1686 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1689 /*****************************************************************************
1690 * IDirectDrawSurface7::GetDC
1692 * Returns a GDI device context for the surface
1694 * Params:
1695 * hdc: Address of a HDC variable to store the dc to
1697 * Returns:
1698 * DD_OK on success
1699 * DDERR_INVALIDPARAMS if hdc is NULL
1700 * For details, see IWineD3DSurface::GetDC
1702 *****************************************************************************/
1703 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1705 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1706 HRESULT hr;
1708 TRACE("iface %p, dc %p.\n", iface, hdc);
1710 if(!hdc)
1711 return DDERR_INVALIDPARAMS;
1713 EnterCriticalSection(&ddraw_cs);
1714 hr = wined3d_surface_getdc(This->wined3d_surface, hdc);
1715 LeaveCriticalSection(&ddraw_cs);
1716 switch(hr)
1718 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1719 * touch *hdc
1721 case WINED3DERR_INVALIDCALL:
1722 if(hdc) *hdc = NULL;
1723 return DDERR_INVALIDPARAMS;
1725 default: return hr;
1729 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
1731 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1732 TRACE("iface %p, dc %p.\n", iface, dc);
1734 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1737 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1739 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1740 TRACE("iface %p, dc %p.\n", iface, dc);
1742 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1745 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
1747 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1748 TRACE("iface %p, dc %p.\n", iface, dc);
1750 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1753 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
1755 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1756 TRACE("iface %p, dc %p.\n", iface, dc);
1758 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1761 /*****************************************************************************
1762 * IDirectDrawSurface7::ReleaseDC
1764 * Releases the DC that was constructed with GetDC
1766 * Params:
1767 * hdc: HDC to release
1769 * Returns:
1770 * DD_OK on success
1771 * For more details, see IWineD3DSurface::ReleaseDC
1773 *****************************************************************************/
1774 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1776 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1777 HRESULT hr;
1779 TRACE("iface %p, dc %p.\n", iface, hdc);
1781 EnterCriticalSection(&ddraw_cs);
1782 hr = wined3d_surface_releasedc(This->wined3d_surface, hdc);
1783 LeaveCriticalSection(&ddraw_cs);
1784 return hr;
1787 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
1789 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1790 TRACE("iface %p, dc %p.\n", iface, dc);
1792 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1795 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1797 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1798 TRACE("iface %p, dc %p.\n", iface, dc);
1800 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1803 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
1805 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1806 TRACE("iface %p, dc %p.\n", iface, dc);
1808 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1811 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
1813 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1814 TRACE("iface %p, dc %p.\n", iface, dc);
1816 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1819 /*****************************************************************************
1820 * IDirectDrawSurface7::GetCaps
1822 * Returns the surface's caps
1824 * Params:
1825 * Caps: Address to write the caps to
1827 * Returns:
1828 * DD_OK on success
1829 * DDERR_INVALIDPARAMS if Caps is NULL
1831 *****************************************************************************/
1832 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1834 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1836 TRACE("iface %p, caps %p.\n", iface, Caps);
1838 if(!Caps)
1839 return DDERR_INVALIDPARAMS;
1841 *Caps = This->surface_desc.ddsCaps;
1842 return DD_OK;
1845 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
1847 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1848 TRACE("iface %p, caps %p.\n", iface, caps);
1850 return ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, caps);
1853 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1855 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1856 DDSCAPS2 caps2;
1857 HRESULT hr;
1859 TRACE("iface %p, caps %p.\n", iface, caps);
1861 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1862 if (FAILED(hr)) return hr;
1864 caps->dwCaps = caps2.dwCaps;
1865 return hr;
1868 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
1870 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1871 DDSCAPS2 caps2;
1872 HRESULT hr;
1874 TRACE("iface %p, caps %p.\n", iface, caps);
1876 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1877 if (FAILED(hr)) return hr;
1879 caps->dwCaps = caps2.dwCaps;
1880 return hr;
1883 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
1885 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1886 DDSCAPS2 caps2;
1887 HRESULT hr;
1889 TRACE("iface %p, caps %p.\n", iface, caps);
1891 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1892 if (FAILED(hr)) return hr;
1894 caps->dwCaps = caps2.dwCaps;
1895 return hr;
1898 /*****************************************************************************
1899 * IDirectDrawSurface7::SetPriority
1901 * Sets a texture priority for managed textures.
1903 * Params:
1904 * Priority: The new priority
1906 * Returns:
1907 * DD_OK on success
1908 * For more details, see IWineD3DSurface::SetPriority
1910 *****************************************************************************/
1911 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1913 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1914 HRESULT hr;
1916 TRACE("iface %p, priority %u.\n", iface, Priority);
1918 EnterCriticalSection(&ddraw_cs);
1919 hr = wined3d_surface_set_priority(This->wined3d_surface, Priority);
1920 LeaveCriticalSection(&ddraw_cs);
1921 return hr;
1924 /*****************************************************************************
1925 * IDirectDrawSurface7::GetPriority
1927 * Returns the surface's priority
1929 * Params:
1930 * Priority: Address of a variable to write the priority to
1932 * Returns:
1933 * D3D_OK on success
1934 * DDERR_INVALIDPARAMS if Priority == NULL
1935 * For more details, see IWineD3DSurface::GetPriority
1937 *****************************************************************************/
1938 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
1940 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1942 TRACE("iface %p, priority %p.\n", iface, Priority);
1944 if(!Priority)
1946 return DDERR_INVALIDPARAMS;
1949 EnterCriticalSection(&ddraw_cs);
1950 *Priority = wined3d_surface_get_priority(This->wined3d_surface);
1951 LeaveCriticalSection(&ddraw_cs);
1952 return DD_OK;
1955 /*****************************************************************************
1956 * IDirectDrawSurface7::SetPrivateData
1958 * Stores some data in the surface that is intended for the application's
1959 * use.
1961 * Params:
1962 * tag: GUID that identifies the data
1963 * Data: Pointer to the private data
1964 * Size: Size of the private data
1965 * Flags: Some flags
1967 * Returns:
1968 * D3D_OK on success
1969 * For more details, see IWineD3DSurface::SetPrivateData
1971 *****************************************************************************/
1972 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
1973 REFGUID tag, void *Data, DWORD Size, DWORD Flags)
1975 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1976 struct wined3d_resource *resource;
1977 HRESULT hr;
1979 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
1980 iface, debugstr_guid(tag), Data, Size, Flags);
1982 EnterCriticalSection(&ddraw_cs);
1983 resource = wined3d_surface_get_resource(This->wined3d_surface);
1984 hr = wined3d_resource_set_private_data(resource, tag, Data, Size, Flags);
1985 LeaveCriticalSection(&ddraw_cs);
1986 switch(hr)
1988 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1989 default: return hr;
1993 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
1994 REFGUID tag, void *data, DWORD size, DWORD flags)
1996 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1997 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
1998 iface, debugstr_guid(tag), data, size, flags);
2000 return ddraw_surface7_SetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size, flags);
2003 /*****************************************************************************
2004 * IDirectDrawSurface7::GetPrivateData
2006 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2008 * Params:
2009 * tag: GUID of the data to return
2010 * Data: Address where to write the data to
2011 * Size: Size of the buffer at Data
2013 * Returns:
2014 * DD_OK on success
2015 * DDERR_INVALIDPARAMS if Data is NULL
2016 * For more details, see IWineD3DSurface::GetPrivateData
2018 *****************************************************************************/
2019 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
2021 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2022 struct wined3d_resource *resource;
2023 HRESULT hr;
2025 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2026 iface, debugstr_guid(tag), Data, Size);
2028 if(!Data)
2029 return DDERR_INVALIDPARAMS;
2031 EnterCriticalSection(&ddraw_cs);
2032 resource = wined3d_surface_get_resource(This->wined3d_surface);
2033 hr = wined3d_resource_get_private_data(resource, tag, Data, Size);
2034 LeaveCriticalSection(&ddraw_cs);
2035 return hr;
2038 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2040 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2041 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2042 iface, debugstr_guid(tag), data, size);
2044 return ddraw_surface7_GetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size);
2047 /*****************************************************************************
2048 * IDirectDrawSurface7::FreePrivateData
2050 * Frees private data stored in the surface
2052 * Params:
2053 * tag: Tag of the data to free
2055 * Returns:
2056 * D3D_OK on success
2057 * For more details, see IWineD3DSurface::FreePrivateData
2059 *****************************************************************************/
2060 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2062 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2063 struct wined3d_resource *resource;
2064 HRESULT hr;
2066 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2068 EnterCriticalSection(&ddraw_cs);
2069 resource = wined3d_surface_get_resource(This->wined3d_surface);
2070 hr = wined3d_resource_free_private_data(resource, tag);
2071 LeaveCriticalSection(&ddraw_cs);
2072 return hr;
2075 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2077 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2078 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2080 return ddraw_surface7_FreePrivateData(&This->IDirectDrawSurface7_iface, tag);
2083 /*****************************************************************************
2084 * IDirectDrawSurface7::PageLock
2086 * Prevents a sysmem surface from being paged out
2088 * Params:
2089 * Flags: Not used, must be 0(unchecked)
2091 * Returns:
2092 * DD_OK, because it's a stub
2094 *****************************************************************************/
2095 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2097 TRACE("iface %p, flags %#x.\n", iface, Flags);
2099 /* This is Windows memory management related - we don't need this */
2100 return DD_OK;
2103 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2105 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2106 TRACE("iface %p, flags %#x.\n", iface, flags);
2108 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2111 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2113 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2114 TRACE("iface %p, flags %#x.\n", iface, flags);
2116 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2119 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2121 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2122 TRACE("iface %p, flags %#x.\n", iface, flags);
2124 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2127 /*****************************************************************************
2128 * IDirectDrawSurface7::PageUnlock
2130 * Allows a sysmem surface to be paged out
2132 * Params:
2133 * Flags: Not used, must be 0(unchecked)
2135 * Returns:
2136 * DD_OK, because it's a stub
2138 *****************************************************************************/
2139 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2141 TRACE("iface %p, flags %#x.\n", iface, Flags);
2143 return DD_OK;
2146 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2148 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2149 TRACE("iface %p, flags %#x.\n", iface, flags);
2151 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2154 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2156 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2157 TRACE("iface %p, flags %#x.\n", iface, flags);
2159 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2162 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2164 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2165 TRACE("iface %p, flags %#x.\n", iface, flags);
2167 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2170 /*****************************************************************************
2171 * IDirectDrawSurface7::BltBatch
2173 * An unimplemented function
2175 * Params:
2178 * Returns:
2179 * DDERR_UNSUPPORTED
2181 *****************************************************************************/
2182 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2184 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2186 /* MSDN: "not currently implemented" */
2187 return DDERR_UNSUPPORTED;
2190 static HRESULT WINAPI ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2192 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2193 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2195 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2198 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2200 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2201 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2203 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2206 static HRESULT WINAPI ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2208 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2209 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2211 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2214 static HRESULT WINAPI ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2216 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2217 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2219 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2222 /*****************************************************************************
2223 * IDirectDrawSurface7::EnumAttachedSurfaces
2225 * Enumerates all surfaces attached to this surface
2227 * Params:
2228 * context: Pointer to pass unmodified to the callback
2229 * cb: Callback function to call for each surface
2231 * Returns:
2232 * DD_OK on success
2233 * DDERR_INVALIDPARAMS if cb is NULL
2235 *****************************************************************************/
2236 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2237 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2239 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2240 IDirectDrawSurfaceImpl *surf;
2241 DDSURFACEDESC2 desc;
2242 int i;
2244 /* Attached surfaces aren't handled in WineD3D */
2245 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2247 if(!cb)
2248 return DDERR_INVALIDPARAMS;
2250 EnterCriticalSection(&ddraw_cs);
2251 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2253 surf = This->complex_array[i];
2254 if(!surf) break;
2256 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2257 desc = surf->surface_desc;
2258 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2259 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2261 LeaveCriticalSection(&ddraw_cs);
2262 return DD_OK;
2266 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
2268 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2269 desc = surf->surface_desc;
2270 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2271 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2273 LeaveCriticalSection(&ddraw_cs);
2274 return DD_OK;
2278 TRACE(" end of enumeration.\n");
2280 LeaveCriticalSection(&ddraw_cs);
2281 return DD_OK;
2284 struct callback_info2
2286 LPDDENUMSURFACESCALLBACK2 callback;
2287 void *context;
2290 struct callback_info
2292 LPDDENUMSURFACESCALLBACK callback;
2293 void *context;
2296 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2298 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
2299 const struct callback_info2 *info = context;
2301 ddraw_surface4_AddRef(&This->IDirectDrawSurface4_iface);
2302 ddraw_surface7_Release(surface);
2304 return info->callback(&This->IDirectDrawSurface4_iface, surface_desc, info->context);
2307 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2309 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
2310 const struct callback_info *info = context;
2312 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2313 (DDSURFACEDESC *)surface_desc, info->context);
2316 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2317 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2319 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2320 struct callback_info2 info;
2322 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2324 info.callback = callback;
2325 info.context = context;
2327 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2328 &info, EnumCallback2);
2331 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2332 void *context, LPDDENUMSURFACESCALLBACK callback)
2334 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2335 struct callback_info info;
2337 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2339 info.callback = callback;
2340 info.context = context;
2342 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2343 &info, EnumCallback);
2346 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2347 void *context, LPDDENUMSURFACESCALLBACK callback)
2349 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2350 struct callback_info info;
2352 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2354 info.callback = callback;
2355 info.context = context;
2357 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2358 &info, EnumCallback);
2361 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2362 void *context, LPDDENUMSURFACESCALLBACK callback)
2364 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2365 struct callback_info info;
2367 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2369 info.callback = callback;
2370 info.context = context;
2372 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2373 &info, EnumCallback);
2376 /*****************************************************************************
2377 * IDirectDrawSurface7::EnumOverlayZOrders
2379 * "Enumerates the overlay surfaces on the specified destination"
2381 * Params:
2382 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2383 * context: context to pass back to the callback
2384 * cb: callback function to call for each enumerated surface
2386 * Returns:
2387 * DD_OK, because it's a stub
2389 *****************************************************************************/
2390 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2391 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2393 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2395 return DD_OK;
2398 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2399 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2401 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2402 struct callback_info2 info;
2404 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2406 info.callback = callback;
2407 info.context = context;
2409 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2410 flags, &info, EnumCallback2);
2413 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2414 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2416 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2417 struct callback_info info;
2419 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2421 info.callback = callback;
2422 info.context = context;
2424 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2425 flags, &info, EnumCallback);
2428 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2429 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2431 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2432 struct callback_info info;
2434 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2436 info.callback = callback;
2437 info.context = context;
2439 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2440 flags, &info, EnumCallback);
2443 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2444 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2446 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2447 struct callback_info info;
2449 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2451 info.callback = callback;
2452 info.context = context;
2454 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2455 flags, &info, EnumCallback);
2458 /*****************************************************************************
2459 * IDirectDrawSurface7::GetBltStatus
2461 * Returns the blitting status
2463 * Params:
2464 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2466 * Returns:
2467 * See IWineD3DSurface::Blt
2469 *****************************************************************************/
2470 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2472 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2473 HRESULT hr;
2475 TRACE("iface %p, flags %#x.\n", iface, Flags);
2477 EnterCriticalSection(&ddraw_cs);
2478 hr = wined3d_surface_get_blt_status(This->wined3d_surface, Flags);
2479 LeaveCriticalSection(&ddraw_cs);
2480 switch(hr)
2482 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2483 default: return hr;
2487 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2489 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2490 TRACE("iface %p, flags %#x.\n", iface, flags);
2492 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2495 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2497 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2498 TRACE("iface %p, flags %#x.\n", iface, flags);
2500 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2503 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2505 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2506 TRACE("iface %p, flags %#x.\n", iface, flags);
2508 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2511 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
2513 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2514 TRACE("iface %p, flags %#x.\n", iface, flags);
2516 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2519 /*****************************************************************************
2520 * IDirectDrawSurface7::GetColorKey
2522 * Returns the color key assigned to the surface
2524 * Params:
2525 * Flags: Some flags
2526 * CKey: Address to store the key to
2528 * Returns:
2529 * DD_OK on success
2530 * DDERR_INVALIDPARAMS if CKey is NULL
2532 *****************************************************************************/
2533 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2535 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2537 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2539 if(!CKey)
2540 return DDERR_INVALIDPARAMS;
2542 EnterCriticalSection(&ddraw_cs);
2544 switch (Flags)
2546 case DDCKEY_DESTBLT:
2547 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
2549 LeaveCriticalSection(&ddraw_cs);
2550 return DDERR_NOCOLORKEY;
2552 *CKey = This->surface_desc.ddckCKDestBlt;
2553 break;
2555 case DDCKEY_DESTOVERLAY:
2556 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
2558 LeaveCriticalSection(&ddraw_cs);
2559 return DDERR_NOCOLORKEY;
2561 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
2562 break;
2564 case DDCKEY_SRCBLT:
2565 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
2567 LeaveCriticalSection(&ddraw_cs);
2568 return DDERR_NOCOLORKEY;
2570 *CKey = This->surface_desc.ddckCKSrcBlt;
2571 break;
2573 case DDCKEY_SRCOVERLAY:
2574 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
2576 LeaveCriticalSection(&ddraw_cs);
2577 return DDERR_NOCOLORKEY;
2579 *CKey = This->surface_desc.ddckCKSrcOverlay;
2580 break;
2582 default:
2583 LeaveCriticalSection(&ddraw_cs);
2584 return DDERR_INVALIDPARAMS;
2587 LeaveCriticalSection(&ddraw_cs);
2588 return DD_OK;
2591 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
2593 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2594 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2596 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2599 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2601 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2602 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2604 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2607 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
2609 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2610 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2612 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2615 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
2617 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2618 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2620 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2623 /*****************************************************************************
2624 * IDirectDrawSurface7::GetFlipStatus
2626 * Returns the flipping status of the surface
2628 * Params:
2629 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
2631 * Returns:
2632 * See IWineD3DSurface::GetFlipStatus
2634 *****************************************************************************/
2635 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2637 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2638 HRESULT hr;
2640 TRACE("iface %p, flags %#x.\n", iface, Flags);
2642 EnterCriticalSection(&ddraw_cs);
2643 hr = wined3d_surface_get_flip_status(This->wined3d_surface, Flags);
2644 LeaveCriticalSection(&ddraw_cs);
2645 switch(hr)
2647 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2648 default: return hr;
2652 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
2654 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2655 TRACE("iface %p, flags %#x.\n", iface, flags);
2657 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2660 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
2662 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2663 TRACE("iface %p, flags %#x.\n", iface, flags);
2665 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2668 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
2670 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2671 TRACE("iface %p, flags %#x.\n", iface, flags);
2673 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2676 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
2678 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2679 TRACE("iface %p, flags %#x.\n", iface, flags);
2681 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2684 /*****************************************************************************
2685 * IDirectDrawSurface7::GetOverlayPosition
2687 * Returns the display coordinates of a visible and active overlay surface
2689 * Params:
2693 * Returns:
2694 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
2695 *****************************************************************************/
2696 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
2698 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2699 HRESULT hr;
2701 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
2703 EnterCriticalSection(&ddraw_cs);
2704 hr = wined3d_surface_get_overlay_position(This->wined3d_surface, X, Y);
2705 LeaveCriticalSection(&ddraw_cs);
2706 return hr;
2709 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
2711 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2712 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2714 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2717 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
2719 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2720 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2722 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2725 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
2727 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2728 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2730 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2733 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
2735 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2736 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2738 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2741 /*****************************************************************************
2742 * IDirectDrawSurface7::GetPixelFormat
2744 * Returns the pixel format of the Surface
2746 * Params:
2747 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
2748 * format should be written
2750 * Returns:
2751 * DD_OK on success
2752 * DDERR_INVALIDPARAMS if PixelFormat is NULL
2754 *****************************************************************************/
2755 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
2757 /* What is DDERR_INVALIDSURFACETYPE for here? */
2758 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2760 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
2762 if(!PixelFormat)
2763 return DDERR_INVALIDPARAMS;
2765 EnterCriticalSection(&ddraw_cs);
2766 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
2767 LeaveCriticalSection(&ddraw_cs);
2769 return DD_OK;
2772 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
2774 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2775 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2777 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2780 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
2782 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2783 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2785 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2788 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
2790 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2791 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2793 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2796 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
2798 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2799 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2801 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2804 /*****************************************************************************
2805 * IDirectDrawSurface7::GetSurfaceDesc
2807 * Returns the description of this surface
2809 * Params:
2810 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
2811 * surface desc
2813 * Returns:
2814 * DD_OK on success
2815 * DDERR_INVALIDPARAMS if DDSD is NULL
2817 *****************************************************************************/
2818 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
2820 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2822 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2824 if(!DDSD)
2825 return DDERR_INVALIDPARAMS;
2827 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
2829 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
2830 return DDERR_INVALIDPARAMS;
2833 EnterCriticalSection(&ddraw_cs);
2834 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
2835 TRACE("Returning surface desc:\n");
2836 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
2838 LeaveCriticalSection(&ddraw_cs);
2839 return DD_OK;
2842 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
2844 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2845 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2847 return ddraw_surface7_GetSurfaceDesc(&This->IDirectDrawSurface7_iface, DDSD);
2850 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
2852 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2854 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
2856 if (!surface_desc) return DDERR_INVALIDPARAMS;
2858 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
2860 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
2861 return DDERR_INVALIDPARAMS;
2864 EnterCriticalSection(&ddraw_cs);
2865 DD_STRUCT_COPY_BYSIZE(surface_desc, (DDSURFACEDESC *)&This->surface_desc);
2866 TRACE("Returning surface desc:\n");
2867 if (TRACE_ON(ddraw))
2869 /* DDRAW_dump_surface_desc handles the smaller size */
2870 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2873 LeaveCriticalSection(&ddraw_cs);
2874 return DD_OK;
2877 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
2879 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2880 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2882 return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
2885 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
2887 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2888 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2890 return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
2893 /*****************************************************************************
2894 * IDirectDrawSurface7::Initialize
2896 * Initializes the surface. This is a no-op in Wine
2898 * Params:
2899 * DD: Pointer to an DirectDraw interface
2900 * DDSD: Surface description for initialization
2902 * Returns:
2903 * DDERR_ALREADYINITIALIZED
2905 *****************************************************************************/
2906 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
2907 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
2909 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2911 return DDERR_ALREADYINITIALIZED;
2914 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
2915 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
2917 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2918 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2920 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2921 ddraw, surface_desc);
2924 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
2925 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2927 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2928 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2930 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2931 ddraw, (DDSURFACEDESC2 *)surface_desc);
2934 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
2935 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2937 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2938 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2940 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2941 ddraw, (DDSURFACEDESC2 *)surface_desc);
2944 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
2945 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2947 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2948 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2950 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2951 ddraw, (DDSURFACEDESC2 *)surface_desc);
2954 /*****************************************************************************
2955 * IDirect3DTexture1::Initialize
2957 * The sdk says it's not implemented
2959 * Params:
2962 * Returns
2963 * DDERR_UNSUPPORTED
2965 *****************************************************************************/
2966 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
2967 IDirect3DDevice *device, IDirectDrawSurface *surface)
2969 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
2971 return DDERR_UNSUPPORTED; /* Unchecked */
2974 /*****************************************************************************
2975 * IDirectDrawSurface7::IsLost
2977 * Checks if the surface is lost
2979 * Returns:
2980 * DD_OK, if the surface is usable
2981 * DDERR_ISLOST if the surface is lost
2982 * See IWineD3DSurface::IsLost for more details
2984 *****************************************************************************/
2985 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
2987 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2988 HRESULT hr;
2990 TRACE("iface %p.\n", iface);
2992 EnterCriticalSection(&ddraw_cs);
2993 /* We lose the surface if the implementation was changed */
2994 if(This->ImplType != This->ddraw->ImplType)
2996 /* But this shouldn't happen. When we change the implementation,
2997 * all surfaces are re-created automatically, and their content
2998 * is copied
3000 ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
3001 LeaveCriticalSection(&ddraw_cs);
3002 return DDERR_SURFACELOST;
3005 hr = wined3d_surface_is_lost(This->wined3d_surface);
3006 LeaveCriticalSection(&ddraw_cs);
3007 switch(hr)
3009 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
3010 * WineD3D uses the same error for surfaces
3012 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
3013 default: return hr;
3017 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3019 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3020 TRACE("iface %p.\n", iface);
3022 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3025 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3027 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3028 TRACE("iface %p.\n", iface);
3030 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3033 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3035 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3036 TRACE("iface %p.\n", iface);
3038 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3041 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3043 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3044 TRACE("iface %p.\n", iface);
3046 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3049 /*****************************************************************************
3050 * IDirectDrawSurface7::Restore
3052 * Restores a lost surface. This makes the surface usable again, but
3053 * doesn't reload its old contents
3055 * Returns:
3056 * DD_OK on success
3057 * See IWineD3DSurface::Restore for more details
3059 *****************************************************************************/
3060 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3062 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3063 HRESULT hr;
3065 TRACE("iface %p.\n", iface);
3067 EnterCriticalSection(&ddraw_cs);
3068 if(This->ImplType != This->ddraw->ImplType)
3070 /* Call the recreation callback. Make sure to AddRef first */
3071 IDirectDrawSurface_AddRef(iface);
3072 ddraw_recreate_surfaces_cb(iface, &This->surface_desc, NULL /* Not needed */);
3074 hr = wined3d_surface_restore(This->wined3d_surface);
3075 LeaveCriticalSection(&ddraw_cs);
3076 return hr;
3079 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3081 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3082 TRACE("iface %p.\n", iface);
3084 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3087 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3089 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3090 TRACE("iface %p.\n", iface);
3092 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3095 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3097 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3098 TRACE("iface %p.\n", iface);
3100 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3103 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3105 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3106 TRACE("iface %p.\n", iface);
3108 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3111 /*****************************************************************************
3112 * IDirectDrawSurface7::SetOverlayPosition
3114 * Changes the display coordinates of an overlay surface
3116 * Params:
3117 * X:
3118 * Y:
3120 * Returns:
3121 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3122 *****************************************************************************/
3123 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
3125 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3126 HRESULT hr;
3128 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
3130 EnterCriticalSection(&ddraw_cs);
3131 hr = wined3d_surface_set_overlay_position(This->wined3d_surface, X, Y);
3132 LeaveCriticalSection(&ddraw_cs);
3133 return hr;
3136 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3138 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3139 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3141 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3144 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3146 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3147 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3149 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3152 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3154 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3155 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3157 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3160 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3162 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3163 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3165 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3168 /*****************************************************************************
3169 * IDirectDrawSurface7::UpdateOverlay
3171 * Modifies the attributes of an overlay surface.
3173 * Params:
3174 * SrcRect: The section of the source being used for the overlay
3175 * DstSurface: Address of the surface that is overlaid
3176 * DstRect: Place of the overlay
3177 * Flags: some DDOVER_* flags
3179 * Returns:
3180 * DDERR_UNSUPPORTED, because we don't support overlays
3182 *****************************************************************************/
3183 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
3184 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
3186 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3187 IDirectDrawSurfaceImpl *Dst = unsafe_impl_from_IDirectDrawSurface7(DstSurface);
3188 HRESULT hr;
3190 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3191 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
3193 EnterCriticalSection(&ddraw_cs);
3194 hr = wined3d_surface_update_overlay(This->wined3d_surface, SrcRect,
3195 Dst ? Dst->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
3196 LeaveCriticalSection(&ddraw_cs);
3197 switch(hr) {
3198 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3199 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3200 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3201 default:
3202 return hr;
3206 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3207 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3209 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3210 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3211 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3212 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3214 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3215 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3218 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3219 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3221 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3222 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3223 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3224 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3226 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3227 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3230 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3231 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3233 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3234 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3235 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3236 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3238 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3239 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3242 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3243 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3245 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3246 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3247 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3248 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3250 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3251 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3254 /*****************************************************************************
3255 * IDirectDrawSurface7::UpdateOverlayDisplay
3257 * The DX7 sdk says that it's not implemented
3259 * Params:
3260 * Flags: ?
3262 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3264 *****************************************************************************/
3265 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3267 TRACE("iface %p, flags %#x.\n", iface, Flags);
3269 return DDERR_UNSUPPORTED;
3272 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3274 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3275 TRACE("iface %p, flags %#x.\n", iface, flags);
3277 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3280 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3282 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3283 TRACE("iface %p, flags %#x.\n", iface, flags);
3285 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3288 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3290 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3291 TRACE("iface %p, flags %#x.\n", iface, flags);
3293 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3296 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3298 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3299 TRACE("iface %p, flags %#x.\n", iface, flags);
3301 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3304 /*****************************************************************************
3305 * IDirectDrawSurface7::UpdateOverlayZOrder
3307 * Sets an overlay's Z order
3309 * Params:
3310 * Flags: DDOVERZ_* flags
3311 * DDSRef: Defines the relative position in the overlay chain
3313 * Returns:
3314 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3316 *****************************************************************************/
3317 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3318 DWORD Flags, IDirectDrawSurface7 *DDSRef)
3320 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3321 IDirectDrawSurfaceImpl *Ref = unsafe_impl_from_IDirectDrawSurface7(DDSRef);
3322 HRESULT hr;
3324 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
3326 EnterCriticalSection(&ddraw_cs);
3327 hr = wined3d_surface_update_overlay_z_order(This->wined3d_surface,
3328 Flags, Ref ? Ref->wined3d_surface : NULL);
3329 LeaveCriticalSection(&ddraw_cs);
3330 return hr;
3333 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3334 DWORD flags, IDirectDrawSurface4 *reference)
3336 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3337 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3338 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3340 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3341 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3344 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3345 DWORD flags, IDirectDrawSurface3 *reference)
3347 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3348 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3349 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3351 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3352 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3355 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3356 DWORD flags, IDirectDrawSurface2 *reference)
3358 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3359 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3360 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3362 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3363 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3366 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3367 DWORD flags, IDirectDrawSurface *reference)
3369 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3370 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3371 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3373 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3374 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3377 /*****************************************************************************
3378 * IDirectDrawSurface7::GetDDInterface
3380 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3381 * surface belongs to
3383 * Params:
3384 * DD: Address to write the interface pointer to
3386 * Returns:
3387 * DD_OK on success
3388 * DDERR_INVALIDPARAMS if DD is NULL
3390 *****************************************************************************/
3391 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3393 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3395 TRACE("iface %p, ddraw %p.\n", iface, DD);
3397 if(!DD)
3398 return DDERR_INVALIDPARAMS;
3400 switch(This->version)
3402 case 7:
3403 *DD = &This->ddraw->IDirectDraw7_iface;
3404 break;
3406 case 4:
3407 *DD = &This->ddraw->IDirectDraw4_iface;
3408 break;
3410 case 2:
3411 *DD = &This->ddraw->IDirectDraw2_iface;
3412 break;
3414 case 1:
3415 *DD = &This->ddraw->IDirectDraw_iface;
3416 break;
3419 IUnknown_AddRef((IUnknown *)*DD);
3421 return DD_OK;
3424 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3426 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3427 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3429 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3432 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3434 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3435 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3437 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3440 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3442 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3443 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3445 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3448 /* This seems also windows implementation specific - I don't think WineD3D needs this */
3449 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3451 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3452 volatile IDirectDrawSurfaceImpl* vThis = This;
3454 TRACE("iface %p.\n", iface);
3456 EnterCriticalSection(&ddraw_cs);
3457 /* A uniqueness value of 0 is apparently special.
3458 * This needs to be checked.
3459 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
3461 while (1) {
3462 DWORD old_uniqueness_value = vThis->uniqueness_value;
3463 DWORD new_uniqueness_value = old_uniqueness_value+1;
3465 if (old_uniqueness_value == 0) break;
3466 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
3468 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
3469 old_uniqueness_value,
3470 new_uniqueness_value)
3471 == old_uniqueness_value)
3472 break;
3475 LeaveCriticalSection(&ddraw_cs);
3476 return DD_OK;
3479 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
3481 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3482 TRACE("iface %p.\n", iface);
3484 return ddraw_surface7_ChangeUniquenessValue(&This->IDirectDrawSurface7_iface);
3487 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
3489 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3491 TRACE("iface %p, value %p.\n", iface, pValue);
3493 EnterCriticalSection(&ddraw_cs);
3494 *pValue = This->uniqueness_value;
3495 LeaveCriticalSection(&ddraw_cs);
3496 return DD_OK;
3499 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
3501 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3502 TRACE("iface %p, value %p.\n", iface, pValue);
3504 return ddraw_surface7_GetUniquenessValue(&This->IDirectDrawSurface7_iface, pValue);
3507 /*****************************************************************************
3508 * IDirectDrawSurface7::SetLOD
3510 * Sets the level of detail of a texture
3512 * Params:
3513 * MaxLOD: LOD to set
3515 * Returns:
3516 * DD_OK on success
3517 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3519 *****************************************************************************/
3520 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
3522 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3523 HRESULT hr;
3525 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
3527 EnterCriticalSection(&ddraw_cs);
3528 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3530 LeaveCriticalSection(&ddraw_cs);
3531 return DDERR_INVALIDOBJECT;
3534 if (!This->wined3d_texture)
3536 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
3537 LeaveCriticalSection(&ddraw_cs);
3538 return DDERR_INVALIDOBJECT;
3541 hr = wined3d_texture_set_lod(This->wined3d_texture, MaxLOD);
3542 LeaveCriticalSection(&ddraw_cs);
3543 return hr;
3546 /*****************************************************************************
3547 * IDirectDrawSurface7::GetLOD
3549 * Returns the level of detail of a Direct3D texture
3551 * Params:
3552 * MaxLOD: Address to write the LOD to
3554 * Returns:
3555 * DD_OK on success
3556 * DDERR_INVALIDPARAMS if MaxLOD is NULL
3557 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3559 *****************************************************************************/
3560 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
3562 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3564 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
3566 if(!MaxLOD)
3567 return DDERR_INVALIDPARAMS;
3569 EnterCriticalSection(&ddraw_cs);
3570 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3572 LeaveCriticalSection(&ddraw_cs);
3573 return DDERR_INVALIDOBJECT;
3576 *MaxLOD = wined3d_texture_get_lod(This->wined3d_texture);
3577 LeaveCriticalSection(&ddraw_cs);
3578 return DD_OK;
3581 /*****************************************************************************
3582 * IDirectDrawSurface7::BltFast
3584 * Performs a fast Blit.
3586 * Params:
3587 * dstx: The x coordinate to blit to on the destination
3588 * dsty: The y coordinate to blit to on the destination
3589 * Source: The source surface
3590 * rsrc: The source rectangle
3591 * trans: Type of transfer. Some DDBLTFAST_* flags
3593 * Returns:
3594 * DD_OK on success
3595 * For more details, see IWineD3DSurface::BltFast
3597 *****************************************************************************/
3598 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
3599 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
3601 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3602 IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface7(Source);
3603 DWORD src_w, src_h, dst_w, dst_h;
3604 HRESULT hr;
3606 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3607 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
3609 dst_w = This->surface_desc.dwWidth;
3610 dst_h = This->surface_desc.dwHeight;
3612 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
3613 * in that case
3615 if(rsrc)
3617 if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
3618 rsrc->right > src->surface_desc.dwWidth ||
3619 rsrc->bottom > src->surface_desc.dwHeight)
3621 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
3622 return DDERR_INVALIDRECT;
3625 src_w = rsrc->right - rsrc->left;
3626 src_h = rsrc->bottom - rsrc->top;
3628 else
3630 src_w = src->surface_desc.dwWidth;
3631 src_h = src->surface_desc.dwHeight;
3634 if (src_w > dst_w || dstx > dst_w - src_w
3635 || src_h > dst_h || dsty > dst_h - src_h)
3637 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
3638 return DDERR_INVALIDRECT;
3641 EnterCriticalSection(&ddraw_cs);
3642 hr = wined3d_surface_bltfast(This->wined3d_surface, dstx, dsty,
3643 src->wined3d_surface, rsrc, trans);
3644 LeaveCriticalSection(&ddraw_cs);
3645 switch(hr)
3647 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
3648 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
3649 default: return hr;
3653 static HRESULT WINAPI ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
3654 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
3656 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3657 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
3658 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3659 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3661 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3662 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3665 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
3666 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
3668 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3669 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
3670 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3671 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3673 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3674 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3677 static HRESULT WINAPI ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
3678 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
3680 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3681 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
3682 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3683 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3685 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3686 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3689 static HRESULT WINAPI ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
3690 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
3692 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3693 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
3694 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3695 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3697 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3698 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3701 /*****************************************************************************
3702 * IDirectDrawSurface7::GetClipper
3704 * Returns the IDirectDrawClipper interface of the clipper assigned to this
3705 * surface
3707 * Params:
3708 * Clipper: Address to store the interface pointer at
3710 * Returns:
3711 * DD_OK on success
3712 * DDERR_INVALIDPARAMS if Clipper is NULL
3713 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
3715 *****************************************************************************/
3716 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
3718 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3720 TRACE("iface %p, clipper %p.\n", iface, Clipper);
3722 if(!Clipper)
3724 LeaveCriticalSection(&ddraw_cs);
3725 return DDERR_INVALIDPARAMS;
3728 EnterCriticalSection(&ddraw_cs);
3729 if(This->clipper == NULL)
3731 LeaveCriticalSection(&ddraw_cs);
3732 return DDERR_NOCLIPPERATTACHED;
3735 *Clipper = (IDirectDrawClipper *)This->clipper;
3736 IDirectDrawClipper_AddRef(*Clipper);
3737 LeaveCriticalSection(&ddraw_cs);
3738 return DD_OK;
3741 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
3743 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3744 TRACE("iface %p, clipper %p.\n", iface, clipper);
3746 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3749 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
3751 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3752 TRACE("iface %p, clipper %p.\n", iface, clipper);
3754 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3757 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
3759 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3760 TRACE("iface %p, clipper %p.\n", iface, clipper);
3762 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3765 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
3767 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3768 TRACE("iface %p, clipper %p.\n", iface, clipper);
3770 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3773 /*****************************************************************************
3774 * IDirectDrawSurface7::SetClipper
3776 * Sets a clipper for the surface
3778 * Params:
3779 * Clipper: IDirectDrawClipper interface of the clipper to set
3781 * Returns:
3782 * DD_OK on success
3784 *****************************************************************************/
3785 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
3786 IDirectDrawClipper *iclipper)
3788 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3789 IDirectDrawClipperImpl *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
3790 IDirectDrawClipperImpl *oldClipper = This->clipper;
3791 HWND clipWindow;
3792 HRESULT hr;
3794 TRACE("iface %p, clipper %p.\n", iface, iclipper);
3796 EnterCriticalSection(&ddraw_cs);
3797 if (clipper == This->clipper)
3799 LeaveCriticalSection(&ddraw_cs);
3800 return DD_OK;
3803 This->clipper = clipper;
3805 if (clipper != NULL)
3806 IDirectDrawClipper_AddRef(iclipper);
3807 if(oldClipper)
3808 IDirectDrawClipper_Release(&oldClipper->IDirectDrawClipper_iface);
3810 hr = wined3d_surface_set_clipper(This->wined3d_surface,
3811 This->clipper ? This->clipper->wineD3DClipper : NULL);
3813 if (This->wined3d_swapchain)
3815 clipWindow = NULL;
3816 if(clipper) {
3817 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
3820 if (clipWindow)
3821 wined3d_swapchain_set_window(This->wined3d_swapchain, clipWindow);
3822 else
3823 wined3d_swapchain_set_window(This->wined3d_swapchain, This->ddraw->d3d_window);
3826 LeaveCriticalSection(&ddraw_cs);
3827 return hr;
3830 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
3832 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3833 TRACE("iface %p, clipper %p.\n", iface, clipper);
3835 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3838 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
3840 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3841 TRACE("iface %p, clipper %p.\n", iface, clipper);
3843 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3846 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
3848 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3849 TRACE("iface %p, clipper %p.\n", iface, clipper);
3851 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3854 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
3856 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3857 TRACE("iface %p, clipper %p.\n", iface, clipper);
3859 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3862 /*****************************************************************************
3863 * IDirectDrawSurface7::SetSurfaceDesc
3865 * Sets the surface description. It can override the pixel format, the surface
3866 * memory, ...
3867 * It's not really tested.
3869 * Params:
3870 * DDSD: Pointer to the new surface description to set
3871 * Flags: Some flags
3873 * Returns:
3874 * DD_OK on success
3875 * DDERR_INVALIDPARAMS if DDSD is NULL
3877 *****************************************************************************/
3878 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
3880 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3881 enum wined3d_format_id newFormat = WINED3DFMT_UNKNOWN;
3882 HRESULT hr;
3884 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
3886 if(!DDSD)
3887 return DDERR_INVALIDPARAMS;
3889 EnterCriticalSection(&ddraw_cs);
3890 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
3892 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
3894 if(newFormat == WINED3DFMT_UNKNOWN)
3896 ERR("Requested to set an unknown pixelformat\n");
3897 LeaveCriticalSection(&ddraw_cs);
3898 return DDERR_INVALIDPARAMS;
3900 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
3902 hr = wined3d_surface_set_format(This->wined3d_surface, newFormat);
3903 if (FAILED(hr))
3905 LeaveCriticalSection(&ddraw_cs);
3906 return hr;
3910 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
3912 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTOVERLAY,
3913 (WINEDDCOLORKEY *)&DDSD->u3.ddckCKDestOverlay);
3915 if (DDSD->dwFlags & DDSD_CKDESTBLT)
3917 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTBLT,
3918 (WINEDDCOLORKEY *)&DDSD->ddckCKDestBlt);
3920 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
3922 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCOVERLAY,
3923 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcOverlay);
3925 if (DDSD->dwFlags & DDSD_CKSRCBLT)
3927 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCBLT,
3928 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcBlt);
3930 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
3932 hr = wined3d_surface_set_mem(This->wined3d_surface, DDSD->lpSurface);
3933 if (FAILED(hr))
3935 /* No need for a trace here, wined3d does that for us */
3936 switch(hr)
3938 case WINED3DERR_INVALIDCALL:
3939 LeaveCriticalSection(&ddraw_cs);
3940 return DDERR_INVALIDPARAMS;
3941 default:
3942 break; /* Go on */
3947 This->surface_desc = *DDSD;
3949 LeaveCriticalSection(&ddraw_cs);
3950 return DD_OK;
3953 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
3954 DDSURFACEDESC2 *surface_desc, DWORD flags)
3956 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3957 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
3959 return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
3960 surface_desc, flags);
3963 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
3964 DDSURFACEDESC *surface_desc, DWORD flags)
3966 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3967 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
3969 return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
3970 (DDSURFACEDESC2 *)surface_desc, flags);
3973 /*****************************************************************************
3974 * IDirectDrawSurface7::GetPalette
3976 * Returns the IDirectDrawPalette interface of the palette currently assigned
3977 * to the surface
3979 * Params:
3980 * Pal: Address to write the interface pointer to
3982 * Returns:
3983 * DD_OK on success
3984 * DDERR_INVALIDPARAMS if Pal is NULL
3986 *****************************************************************************/
3987 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
3989 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3990 struct wined3d_palette *wined3d_palette;
3991 HRESULT hr = DD_OK;
3993 TRACE("iface %p, palette %p.\n", iface, Pal);
3995 if(!Pal)
3996 return DDERR_INVALIDPARAMS;
3998 EnterCriticalSection(&ddraw_cs);
3999 wined3d_palette = wined3d_surface_get_palette(This->wined3d_surface);
4000 if (wined3d_palette)
4002 *Pal = wined3d_palette_get_parent(wined3d_palette);
4003 IDirectDrawPalette_AddRef(*Pal);
4005 else
4007 *Pal = NULL;
4008 hr = DDERR_NOPALETTEATTACHED;
4011 LeaveCriticalSection(&ddraw_cs);
4012 return hr;
4015 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4017 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4018 TRACE("iface %p, palette %p.\n", iface, palette);
4020 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4023 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4025 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4026 TRACE("iface %p, palette %p.\n", iface, palette);
4028 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4031 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4033 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4034 TRACE("iface %p, palette %p.\n", iface, palette);
4036 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4039 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4041 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4042 TRACE("iface %p, palette %p.\n", iface, palette);
4044 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4047 /*****************************************************************************
4048 * SetColorKeyEnum
4050 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
4051 * recursively in the surface tree
4053 *****************************************************************************/
4054 struct SCKContext
4056 HRESULT ret;
4057 WINEDDCOLORKEY *CKey;
4058 DWORD Flags;
4061 static HRESULT WINAPI
4062 SetColorKeyEnum(IDirectDrawSurface7 *surface,
4063 DDSURFACEDESC2 *desc,
4064 void *context)
4066 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
4067 struct SCKContext *ctx = context;
4068 HRESULT hr;
4070 hr = wined3d_surface_set_color_key(This->wined3d_surface, ctx->Flags, ctx->CKey);
4071 if (FAILED(hr))
4073 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
4074 ctx->ret = hr;
4077 ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
4078 ddraw_surface7_Release(surface);
4080 return DDENUMRET_OK;
4083 /*****************************************************************************
4084 * IDirectDrawSurface7::SetColorKey
4086 * Sets the color keying options for the surface. Observations showed that
4087 * in case of complex surfaces the color key has to be assigned to all
4088 * sublevels.
4090 * Params:
4091 * Flags: DDCKEY_*
4092 * CKey: The new color key
4094 * Returns:
4095 * DD_OK on success
4096 * See IWineD3DSurface::SetColorKey for details
4098 *****************************************************************************/
4099 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
4101 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4102 DDCOLORKEY FixedCKey;
4103 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
4105 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
4107 EnterCriticalSection(&ddraw_cs);
4108 if (CKey)
4110 FixedCKey = *CKey;
4111 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
4112 if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
4113 FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
4115 switch (Flags & ~DDCKEY_COLORSPACE)
4117 case DDCKEY_DESTBLT:
4118 This->surface_desc.ddckCKDestBlt = FixedCKey;
4119 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4120 break;
4122 case DDCKEY_DESTOVERLAY:
4123 This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
4124 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4125 break;
4127 case DDCKEY_SRCOVERLAY:
4128 This->surface_desc.ddckCKSrcOverlay = FixedCKey;
4129 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4130 break;
4132 case DDCKEY_SRCBLT:
4133 This->surface_desc.ddckCKSrcBlt = FixedCKey;
4134 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4135 break;
4137 default:
4138 LeaveCriticalSection(&ddraw_cs);
4139 return DDERR_INVALIDPARAMS;
4142 else
4144 switch (Flags & ~DDCKEY_COLORSPACE)
4146 case DDCKEY_DESTBLT:
4147 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4148 break;
4150 case DDCKEY_DESTOVERLAY:
4151 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4152 break;
4154 case DDCKEY_SRCOVERLAY:
4155 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4156 break;
4158 case DDCKEY_SRCBLT:
4159 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4160 break;
4162 default:
4163 LeaveCriticalSection(&ddraw_cs);
4164 return DDERR_INVALIDPARAMS;
4167 ctx.ret = wined3d_surface_set_color_key(This->wined3d_surface, Flags, ctx.CKey);
4168 ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
4169 LeaveCriticalSection(&ddraw_cs);
4170 switch(ctx.ret)
4172 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
4173 default: return ctx.ret;
4177 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4179 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4180 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4182 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4185 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4187 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4188 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4190 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4193 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4195 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4196 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4198 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4201 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4203 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4204 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4206 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4209 /*****************************************************************************
4210 * IDirectDrawSurface7::SetPalette
4212 * Assigns a DirectDrawPalette object to the surface
4214 * Params:
4215 * Pal: Interface to the palette to set
4217 * Returns:
4218 * DD_OK on success
4220 *****************************************************************************/
4221 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
4223 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4224 IDirectDrawPalette *oldPal;
4225 IDirectDrawSurfaceImpl *surf;
4226 IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
4227 HRESULT hr;
4229 TRACE("iface %p, palette %p.\n", iface, Pal);
4231 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
4232 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
4233 return DDERR_INVALIDPIXELFORMAT;
4236 /* Find the old palette */
4237 EnterCriticalSection(&ddraw_cs);
4238 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
4239 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
4241 LeaveCriticalSection(&ddraw_cs);
4242 return hr;
4244 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
4246 /* Set the new Palette */
4247 wined3d_surface_set_palette(This->wined3d_surface, PalImpl ? PalImpl->wineD3DPalette : NULL);
4248 /* AddRef the Palette */
4249 if(Pal) IDirectDrawPalette_AddRef(Pal);
4251 /* Release the old palette */
4252 if(oldPal) IDirectDrawPalette_Release(oldPal);
4254 /* If this is a front buffer, also update the back buffers
4255 * TODO: How do things work for palettized cube textures?
4257 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
4259 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
4260 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
4262 surf = This;
4263 while(1)
4265 IDirectDrawSurface7 *attach;
4266 HRESULT hr;
4267 hr = ddraw_surface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &caps2, &attach);
4268 if(hr != DD_OK)
4270 break;
4273 TRACE("Setting palette on %p\n", attach);
4274 ddraw_surface7_SetPalette(attach, Pal);
4275 surf = impl_from_IDirectDrawSurface7(attach);
4276 ddraw_surface7_Release(attach);
4280 LeaveCriticalSection(&ddraw_cs);
4281 return DD_OK;
4284 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4286 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4287 TRACE("iface %p, palette %p.\n", iface, palette);
4289 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4292 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4294 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4295 TRACE("iface %p, palette %p.\n", iface, palette);
4297 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4300 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4302 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4303 TRACE("iface %p, palette %p.\n", iface, palette);
4305 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4308 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4310 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4311 TRACE("iface %p, palette %p.\n", iface, palette);
4313 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4316 /**********************************************************
4317 * IDirectDrawGammaControl::GetGammaRamp
4319 * Returns the current gamma ramp for a surface
4321 * Params:
4322 * flags: Ignored
4323 * gamma_ramp: Address to write the ramp to
4325 * Returns:
4326 * DD_OK on success
4327 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4329 **********************************************************/
4330 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4331 DWORD flags, DDGAMMARAMP *gamma_ramp)
4333 IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4335 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4337 if (!gamma_ramp)
4339 WARN("Invalid gamma_ramp passed.\n");
4340 return DDERR_INVALIDPARAMS;
4343 EnterCriticalSection(&ddraw_cs);
4344 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4346 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP. */
4347 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (WINED3DGAMMARAMP *)gamma_ramp);
4349 else
4351 ERR("Not implemented for non-primary surfaces.\n");
4353 LeaveCriticalSection(&ddraw_cs);
4355 return DD_OK;
4358 /**********************************************************
4359 * IDirectDrawGammaControl::SetGammaRamp
4361 * Sets the red, green and blue gamma ramps for
4363 * Params:
4364 * flags: Can be DDSGR_CALIBRATE to request calibration
4365 * gamma_ramp: Structure containing the new gamma ramp
4367 * Returns:
4368 * DD_OK on success
4369 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4371 **********************************************************/
4372 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4373 DWORD flags, DDGAMMARAMP *gamma_ramp)
4375 IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4377 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4379 if (!gamma_ramp)
4381 WARN("Invalid gamma_ramp passed.\n");
4382 return DDERR_INVALIDPARAMS;
4385 EnterCriticalSection(&ddraw_cs);
4386 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4388 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP */
4389 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device, 0, flags, (WINED3DGAMMARAMP *)gamma_ramp);
4391 else
4393 ERR("Not implemented for non-primary surfaces.\n");
4395 LeaveCriticalSection(&ddraw_cs);
4397 return DD_OK;
4400 /*****************************************************************************
4401 * IDirect3DTexture2::PaletteChanged
4403 * Informs the texture about a palette change
4405 * Params:
4406 * start: Start index of the change
4407 * count: The number of changed entries
4409 * Returns
4410 * D3D_OK, because it's a stub
4412 *****************************************************************************/
4413 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4415 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4417 return D3D_OK;
4420 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4422 IDirectDrawSurfaceImpl *surface = surface_from_texture1(iface);
4424 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4426 return d3d_texture2_PaletteChanged((IDirect3DTexture2 *)&surface->IDirect3DTexture2_vtbl, start, count);
4429 /*****************************************************************************
4430 * IDirect3DTexture::Unload
4432 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4435 * Returns:
4436 * DDERR_UNSUPPORTED
4438 *****************************************************************************/
4439 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4441 WARN("iface %p. Not implemented.\n", iface);
4443 return DDERR_UNSUPPORTED;
4446 /*****************************************************************************
4447 * IDirect3DTexture2::GetHandle
4449 * Returns handle for the texture. At the moment, the interface
4450 * to the IWineD3DTexture is used.
4452 * Params:
4453 * device: Device this handle is assigned to
4454 * handle: Address to store the handle at.
4456 * Returns:
4457 * D3D_OK
4459 *****************************************************************************/
4460 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4461 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4463 IDirectDrawSurfaceImpl *surface = surface_from_texture2(iface);
4465 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4467 EnterCriticalSection(&ddraw_cs);
4469 if (!surface->Handle)
4471 DWORD h = ddraw_allocate_handle(&device_from_device2(device)->handle_table, surface, DDRAW_HANDLE_SURFACE);
4472 if (h == DDRAW_INVALID_HANDLE)
4474 ERR("Failed to allocate a texture handle.\n");
4475 LeaveCriticalSection(&ddraw_cs);
4476 return DDERR_OUTOFMEMORY;
4479 surface->Handle = h + 1;
4482 TRACE("Returning handle %08x.\n", surface->Handle);
4483 *handle = surface->Handle;
4485 LeaveCriticalSection(&ddraw_cs);
4487 return D3D_OK;
4490 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
4491 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
4493 IDirect3DTexture2 *texture2 = (IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl;
4494 IDirect3DDevice2 *device2 = (IDirect3DDevice2 *)&device_from_device1(device)->IDirect3DDevice2_vtbl;
4496 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4498 return d3d_texture2_GetHandle(texture2, device2, handle);
4501 /*****************************************************************************
4502 * get_sub_mimaplevel
4504 * Helper function that returns the next mipmap level
4506 * tex_ptr: Surface of which to return the next level
4508 *****************************************************************************/
4509 static IDirectDrawSurfaceImpl *get_sub_mimaplevel(IDirectDrawSurfaceImpl *surface)
4511 /* Now go down the mipmap chain to the next surface */
4512 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
4513 IDirectDrawSurface7 *next_level;
4514 HRESULT hr;
4516 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
4517 if (FAILED(hr)) return NULL;
4519 ddraw_surface7_Release(next_level);
4521 return impl_from_IDirectDrawSurface7(next_level);
4524 /*****************************************************************************
4525 * IDirect3DTexture2::Load
4527 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
4529 * This function isn't relayed to WineD3D because the whole interface is
4530 * implemented in DDraw only. For speed improvements a implementation which
4531 * takes OpenGL more into account could be placed into WineD3D.
4533 * Params:
4534 * src_texture: Address of the texture to load
4536 * Returns:
4537 * D3D_OK on success
4538 * D3DERR_TEXTURE_LOAD_FAILED.
4540 *****************************************************************************/
4541 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
4543 IDirectDrawSurfaceImpl *dst_surface = surface_from_texture2(iface);
4544 IDirectDrawSurfaceImpl *src_surface = surface_from_texture2(src_texture);
4545 HRESULT hr;
4547 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4549 if (src_surface == dst_surface)
4551 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
4552 return D3D_OK;
4555 EnterCriticalSection(&ddraw_cs);
4557 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4558 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
4559 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
4561 ERR("Trying to load surfaces with different mip-map counts.\n");
4564 for (;;)
4566 struct wined3d_palette *wined3d_dst_pal, *wined3d_src_pal;
4567 IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
4568 DDSURFACEDESC *src_desc, *dst_desc;
4570 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
4571 src_surface, dst_surface, src_surface->mipmap_level);
4573 /* Suppress the ALLOCONLOAD flag */
4574 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
4576 /* Get the palettes */
4577 wined3d_dst_pal = wined3d_surface_get_palette(dst_surface->wined3d_surface);
4578 if (wined3d_dst_pal)
4579 dst_pal = wined3d_palette_get_parent(wined3d_dst_pal);
4581 wined3d_src_pal = wined3d_surface_get_palette(src_surface->wined3d_surface);
4582 if (wined3d_src_pal)
4583 src_pal = wined3d_palette_get_parent(wined3d_src_pal);
4585 if (src_pal)
4587 PALETTEENTRY palent[256];
4589 if (!dst_pal)
4591 LeaveCriticalSection(&ddraw_cs);
4592 return DDERR_NOPALETTEATTACHED;
4594 IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
4595 IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
4598 /* Copy one surface on the other */
4599 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
4600 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
4602 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
4604 /* Should also check for same pixel format, u1.lPitch, ... */
4605 ERR("Error in surface sizes.\n");
4606 LeaveCriticalSection(&ddraw_cs);
4607 return D3DERR_TEXTURE_LOAD_FAILED;
4609 else
4611 WINED3DLOCKED_RECT src_rect, dst_rect;
4613 /* Copy also the ColorKeying stuff */
4614 if (src_desc->dwFlags & DDSD_CKSRCBLT)
4616 dst_desc->dwFlags |= DDSD_CKSRCBLT;
4617 dst_desc->ddckCKSrcBlt.dwColorSpaceLowValue = src_desc->ddckCKSrcBlt.dwColorSpaceLowValue;
4618 dst_desc->ddckCKSrcBlt.dwColorSpaceHighValue = src_desc->ddckCKSrcBlt.dwColorSpaceHighValue;
4621 /* Copy the main memory texture into the surface that corresponds
4622 * to the OpenGL texture object. */
4624 hr = wined3d_surface_map(src_surface->wined3d_surface, &src_rect, NULL, 0);
4625 if (FAILED(hr))
4627 ERR("Failed to lock source surface, hr %#x.\n", hr);
4628 LeaveCriticalSection(&ddraw_cs);
4629 return D3DERR_TEXTURE_LOAD_FAILED;
4632 hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_rect, NULL, 0);
4633 if (FAILED(hr))
4635 ERR("Failed to lock destination surface, hr %#x.\n", hr);
4636 wined3d_surface_unmap(src_surface->wined3d_surface);
4637 LeaveCriticalSection(&ddraw_cs);
4638 return D3DERR_TEXTURE_LOAD_FAILED;
4641 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
4642 memcpy(dst_rect.pBits, src_rect.pBits, src_surface->surface_desc.u1.dwLinearSize);
4643 else
4644 memcpy(dst_rect.pBits, src_rect.pBits, src_rect.Pitch * src_desc->dwHeight);
4646 wined3d_surface_unmap(src_surface->wined3d_surface);
4647 wined3d_surface_unmap(dst_surface->wined3d_surface);
4650 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4651 src_surface = get_sub_mimaplevel(src_surface);
4652 else
4653 src_surface = NULL;
4655 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4656 dst_surface = get_sub_mimaplevel(dst_surface);
4657 else
4658 dst_surface = NULL;
4660 if (!src_surface || !dst_surface)
4662 if (src_surface != dst_surface)
4663 ERR("Loading surface with different mipmap structure.\n");
4664 break;
4668 LeaveCriticalSection(&ddraw_cs);
4670 return hr;
4673 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
4675 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4677 return d3d_texture2_Load((IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl,
4678 src_texture ? (IDirect3DTexture2 *)&surface_from_texture1(src_texture)->IDirect3DTexture2_vtbl : NULL);
4681 /*****************************************************************************
4682 * The VTable
4683 *****************************************************************************/
4685 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
4687 /* IUnknown */
4688 ddraw_surface7_QueryInterface,
4689 ddraw_surface7_AddRef,
4690 ddraw_surface7_Release,
4691 /* IDirectDrawSurface */
4692 ddraw_surface7_AddAttachedSurface,
4693 ddraw_surface7_AddOverlayDirtyRect,
4694 ddraw_surface7_Blt,
4695 ddraw_surface7_BltBatch,
4696 ddraw_surface7_BltFast,
4697 ddraw_surface7_DeleteAttachedSurface,
4698 ddraw_surface7_EnumAttachedSurfaces,
4699 ddraw_surface7_EnumOverlayZOrders,
4700 ddraw_surface7_Flip,
4701 ddraw_surface7_GetAttachedSurface,
4702 ddraw_surface7_GetBltStatus,
4703 ddraw_surface7_GetCaps,
4704 ddraw_surface7_GetClipper,
4705 ddraw_surface7_GetColorKey,
4706 ddraw_surface7_GetDC,
4707 ddraw_surface7_GetFlipStatus,
4708 ddraw_surface7_GetOverlayPosition,
4709 ddraw_surface7_GetPalette,
4710 ddraw_surface7_GetPixelFormat,
4711 ddraw_surface7_GetSurfaceDesc,
4712 ddraw_surface7_Initialize,
4713 ddraw_surface7_IsLost,
4714 ddraw_surface7_Lock,
4715 ddraw_surface7_ReleaseDC,
4716 ddraw_surface7_Restore,
4717 ddraw_surface7_SetClipper,
4718 ddraw_surface7_SetColorKey,
4719 ddraw_surface7_SetOverlayPosition,
4720 ddraw_surface7_SetPalette,
4721 ddraw_surface7_Unlock,
4722 ddraw_surface7_UpdateOverlay,
4723 ddraw_surface7_UpdateOverlayDisplay,
4724 ddraw_surface7_UpdateOverlayZOrder,
4725 /* IDirectDrawSurface2 */
4726 ddraw_surface7_GetDDInterface,
4727 ddraw_surface7_PageLock,
4728 ddraw_surface7_PageUnlock,
4729 /* IDirectDrawSurface3 */
4730 ddraw_surface7_SetSurfaceDesc,
4731 /* IDirectDrawSurface4 */
4732 ddraw_surface7_SetPrivateData,
4733 ddraw_surface7_GetPrivateData,
4734 ddraw_surface7_FreePrivateData,
4735 ddraw_surface7_GetUniquenessValue,
4736 ddraw_surface7_ChangeUniquenessValue,
4737 /* IDirectDrawSurface7 */
4738 ddraw_surface7_SetPriority,
4739 ddraw_surface7_GetPriority,
4740 ddraw_surface7_SetLOD,
4741 ddraw_surface7_GetLOD,
4744 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
4746 /* IUnknown */
4747 ddraw_surface4_QueryInterface,
4748 ddraw_surface4_AddRef,
4749 ddraw_surface4_Release,
4750 /* IDirectDrawSurface */
4751 ddraw_surface4_AddAttachedSurface,
4752 ddraw_surface4_AddOverlayDirtyRect,
4753 ddraw_surface4_Blt,
4754 ddraw_surface4_BltBatch,
4755 ddraw_surface4_BltFast,
4756 ddraw_surface4_DeleteAttachedSurface,
4757 ddraw_surface4_EnumAttachedSurfaces,
4758 ddraw_surface4_EnumOverlayZOrders,
4759 ddraw_surface4_Flip,
4760 ddraw_surface4_GetAttachedSurface,
4761 ddraw_surface4_GetBltStatus,
4762 ddraw_surface4_GetCaps,
4763 ddraw_surface4_GetClipper,
4764 ddraw_surface4_GetColorKey,
4765 ddraw_surface4_GetDC,
4766 ddraw_surface4_GetFlipStatus,
4767 ddraw_surface4_GetOverlayPosition,
4768 ddraw_surface4_GetPalette,
4769 ddraw_surface4_GetPixelFormat,
4770 ddraw_surface4_GetSurfaceDesc,
4771 ddraw_surface4_Initialize,
4772 ddraw_surface4_IsLost,
4773 ddraw_surface4_Lock,
4774 ddraw_surface4_ReleaseDC,
4775 ddraw_surface4_Restore,
4776 ddraw_surface4_SetClipper,
4777 ddraw_surface4_SetColorKey,
4778 ddraw_surface4_SetOverlayPosition,
4779 ddraw_surface4_SetPalette,
4780 ddraw_surface4_Unlock,
4781 ddraw_surface4_UpdateOverlay,
4782 ddraw_surface4_UpdateOverlayDisplay,
4783 ddraw_surface4_UpdateOverlayZOrder,
4784 /* IDirectDrawSurface2 */
4785 ddraw_surface4_GetDDInterface,
4786 ddraw_surface4_PageLock,
4787 ddraw_surface4_PageUnlock,
4788 /* IDirectDrawSurface3 */
4789 ddraw_surface4_SetSurfaceDesc,
4790 /* IDirectDrawSurface4 */
4791 ddraw_surface4_SetPrivateData,
4792 ddraw_surface4_GetPrivateData,
4793 ddraw_surface4_FreePrivateData,
4794 ddraw_surface4_GetUniquenessValue,
4795 ddraw_surface4_ChangeUniquenessValue,
4798 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
4800 /* IUnknown */
4801 ddraw_surface3_QueryInterface,
4802 ddraw_surface3_AddRef,
4803 ddraw_surface3_Release,
4804 /* IDirectDrawSurface */
4805 ddraw_surface3_AddAttachedSurface,
4806 ddraw_surface3_AddOverlayDirtyRect,
4807 ddraw_surface3_Blt,
4808 ddraw_surface3_BltBatch,
4809 ddraw_surface3_BltFast,
4810 ddraw_surface3_DeleteAttachedSurface,
4811 ddraw_surface3_EnumAttachedSurfaces,
4812 ddraw_surface3_EnumOverlayZOrders,
4813 ddraw_surface3_Flip,
4814 ddraw_surface3_GetAttachedSurface,
4815 ddraw_surface3_GetBltStatus,
4816 ddraw_surface3_GetCaps,
4817 ddraw_surface3_GetClipper,
4818 ddraw_surface3_GetColorKey,
4819 ddraw_surface3_GetDC,
4820 ddraw_surface3_GetFlipStatus,
4821 ddraw_surface3_GetOverlayPosition,
4822 ddraw_surface3_GetPalette,
4823 ddraw_surface3_GetPixelFormat,
4824 ddraw_surface3_GetSurfaceDesc,
4825 ddraw_surface3_Initialize,
4826 ddraw_surface3_IsLost,
4827 ddraw_surface3_Lock,
4828 ddraw_surface3_ReleaseDC,
4829 ddraw_surface3_Restore,
4830 ddraw_surface3_SetClipper,
4831 ddraw_surface3_SetColorKey,
4832 ddraw_surface3_SetOverlayPosition,
4833 ddraw_surface3_SetPalette,
4834 ddraw_surface3_Unlock,
4835 ddraw_surface3_UpdateOverlay,
4836 ddraw_surface3_UpdateOverlayDisplay,
4837 ddraw_surface3_UpdateOverlayZOrder,
4838 /* IDirectDrawSurface2 */
4839 ddraw_surface3_GetDDInterface,
4840 ddraw_surface3_PageLock,
4841 ddraw_surface3_PageUnlock,
4842 /* IDirectDrawSurface3 */
4843 ddraw_surface3_SetSurfaceDesc,
4846 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
4848 /* IUnknown */
4849 ddraw_surface2_QueryInterface,
4850 ddraw_surface2_AddRef,
4851 ddraw_surface2_Release,
4852 /* IDirectDrawSurface */
4853 ddraw_surface2_AddAttachedSurface,
4854 ddraw_surface2_AddOverlayDirtyRect,
4855 ddraw_surface2_Blt,
4856 ddraw_surface2_BltBatch,
4857 ddraw_surface2_BltFast,
4858 ddraw_surface2_DeleteAttachedSurface,
4859 ddraw_surface2_EnumAttachedSurfaces,
4860 ddraw_surface2_EnumOverlayZOrders,
4861 ddraw_surface2_Flip,
4862 ddraw_surface2_GetAttachedSurface,
4863 ddraw_surface2_GetBltStatus,
4864 ddraw_surface2_GetCaps,
4865 ddraw_surface2_GetClipper,
4866 ddraw_surface2_GetColorKey,
4867 ddraw_surface2_GetDC,
4868 ddraw_surface2_GetFlipStatus,
4869 ddraw_surface2_GetOverlayPosition,
4870 ddraw_surface2_GetPalette,
4871 ddraw_surface2_GetPixelFormat,
4872 ddraw_surface2_GetSurfaceDesc,
4873 ddraw_surface2_Initialize,
4874 ddraw_surface2_IsLost,
4875 ddraw_surface2_Lock,
4876 ddraw_surface2_ReleaseDC,
4877 ddraw_surface2_Restore,
4878 ddraw_surface2_SetClipper,
4879 ddraw_surface2_SetColorKey,
4880 ddraw_surface2_SetOverlayPosition,
4881 ddraw_surface2_SetPalette,
4882 ddraw_surface2_Unlock,
4883 ddraw_surface2_UpdateOverlay,
4884 ddraw_surface2_UpdateOverlayDisplay,
4885 ddraw_surface2_UpdateOverlayZOrder,
4886 /* IDirectDrawSurface2 */
4887 ddraw_surface2_GetDDInterface,
4888 ddraw_surface2_PageLock,
4889 ddraw_surface2_PageUnlock,
4892 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
4894 /* IUnknown */
4895 ddraw_surface1_QueryInterface,
4896 ddraw_surface1_AddRef,
4897 ddraw_surface1_Release,
4898 /* IDirectDrawSurface */
4899 ddraw_surface1_AddAttachedSurface,
4900 ddraw_surface1_AddOverlayDirtyRect,
4901 ddraw_surface1_Blt,
4902 ddraw_surface1_BltBatch,
4903 ddraw_surface1_BltFast,
4904 ddraw_surface1_DeleteAttachedSurface,
4905 ddraw_surface1_EnumAttachedSurfaces,
4906 ddraw_surface1_EnumOverlayZOrders,
4907 ddraw_surface1_Flip,
4908 ddraw_surface1_GetAttachedSurface,
4909 ddraw_surface1_GetBltStatus,
4910 ddraw_surface1_GetCaps,
4911 ddraw_surface1_GetClipper,
4912 ddraw_surface1_GetColorKey,
4913 ddraw_surface1_GetDC,
4914 ddraw_surface1_GetFlipStatus,
4915 ddraw_surface1_GetOverlayPosition,
4916 ddraw_surface1_GetPalette,
4917 ddraw_surface1_GetPixelFormat,
4918 ddraw_surface1_GetSurfaceDesc,
4919 ddraw_surface1_Initialize,
4920 ddraw_surface1_IsLost,
4921 ddraw_surface1_Lock,
4922 ddraw_surface1_ReleaseDC,
4923 ddraw_surface1_Restore,
4924 ddraw_surface1_SetClipper,
4925 ddraw_surface1_SetColorKey,
4926 ddraw_surface1_SetOverlayPosition,
4927 ddraw_surface1_SetPalette,
4928 ddraw_surface1_Unlock,
4929 ddraw_surface1_UpdateOverlay,
4930 ddraw_surface1_UpdateOverlayDisplay,
4931 ddraw_surface1_UpdateOverlayZOrder,
4934 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
4936 ddraw_gamma_control_QueryInterface,
4937 ddraw_gamma_control_AddRef,
4938 ddraw_gamma_control_Release,
4939 ddraw_gamma_control_GetGammaRamp,
4940 ddraw_gamma_control_SetGammaRamp,
4943 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
4945 d3d_texture2_QueryInterface,
4946 d3d_texture2_AddRef,
4947 d3d_texture2_Release,
4948 d3d_texture2_GetHandle,
4949 d3d_texture2_PaletteChanged,
4950 d3d_texture2_Load,
4953 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
4955 d3d_texture1_QueryInterface,
4956 d3d_texture1_AddRef,
4957 d3d_texture1_Release,
4958 d3d_texture1_Initialize,
4959 d3d_texture1_GetHandle,
4960 d3d_texture1_PaletteChanged,
4961 d3d_texture1_Load,
4962 d3d_texture1_Unload,
4965 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
4967 if (!iface) return NULL;
4968 assert(iface->lpVtbl == &ddraw_surface7_vtbl);
4969 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface7_iface);
4972 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
4974 if (!iface) return NULL;
4975 assert(iface->lpVtbl == &ddraw_surface4_vtbl);
4976 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface4_iface);
4979 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
4981 if (!iface) return NULL;
4982 assert(iface->lpVtbl == &ddraw_surface3_vtbl);
4983 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface3_iface);
4986 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
4988 if (!iface) return NULL;
4989 assert(iface->lpVtbl == &ddraw_surface2_vtbl);
4990 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface2_iface);
4993 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
4995 if (!iface) return NULL;
4996 assert(iface->lpVtbl == &ddraw_surface1_vtbl);
4997 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface_iface);
5000 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5002 IDirectDrawSurfaceImpl *surface = parent;
5004 TRACE("surface %p.\n", surface);
5006 /* Check for attached surfaces and detach them. */
5007 if (surface->first_attached != surface)
5009 IDirectDrawSurface7 *root = &surface->first_attached->IDirectDrawSurface7_iface;
5010 IDirectDrawSurface7 *detach = &surface->IDirectDrawSurface7_iface;
5012 /* Well, this shouldn't happen: The surface being attached is
5013 * referenced in AddAttachedSurface(), so it shouldn't be released
5014 * until DeleteAttachedSurface() is called, because the refcount is
5015 * held. It looks like the application released it often enough to
5016 * force this. */
5017 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
5019 /* The refcount will drop to -1 here */
5020 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
5021 ERR("DeleteAttachedSurface failed.\n");
5024 while (surface->next_attached)
5026 IDirectDrawSurface7 *root = &surface->IDirectDrawSurface7_iface;
5027 IDirectDrawSurface7 *detach = &surface->next_attached->IDirectDrawSurface7_iface;
5029 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
5030 ERR("DeleteAttachedSurface failed.\n");
5033 /* Having a texture handle set implies that the device still exists. */
5034 if (surface->Handle)
5035 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5037 /* Reduce the ddraw surface count. */
5038 InterlockedDecrement(&surface->ddraw->surfaces);
5039 list_remove(&surface->surface_list_entry);
5041 HeapFree(GetProcessHeap(), 0, surface);
5044 const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5046 ddraw_surface_wined3d_object_destroyed,
5049 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5051 IDirectDrawSurfaceImpl *surface = parent;
5053 TRACE("surface %p.\n", surface);
5055 ddraw_surface_cleanup(surface);
5058 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5060 ddraw_texture_wined3d_object_destroyed,
5063 HRESULT ddraw_surface_create_texture(IDirectDrawSurfaceImpl *surface)
5065 const DDSURFACEDESC2 *desc = &surface->surface_desc;
5066 enum wined3d_format_id format;
5067 WINED3DPOOL pool;
5068 UINT levels;
5070 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5071 levels = desc->u2.dwMipMapCount;
5072 else
5073 levels = 1;
5075 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM.
5076 * Should I forward the MANAGED cap to the managed pool? */
5077 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5078 pool = WINED3DPOOL_SYSTEMMEM;
5079 else
5080 pool = WINED3DPOOL_DEFAULT;
5082 format = PixelFormat_DD2WineD3D(&surface->surface_desc.u4.ddpfPixelFormat);
5083 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5084 return wined3d_texture_create_cube(surface->ddraw->wined3d_device, desc->dwWidth,
5085 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5086 else
5087 return wined3d_texture_create_2d(surface->ddraw->wined3d_device, desc->dwWidth, desc->dwHeight,
5088 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5091 HRESULT ddraw_surface_init(IDirectDrawSurfaceImpl *surface, IDirectDrawImpl *ddraw,
5092 DDSURFACEDESC2 *desc, UINT mip_level, WINED3DSURFTYPE surface_type, UINT version)
5094 struct wined3d_resource_desc wined3d_desc;
5095 struct wined3d_resource *wined3d_resource;
5096 WINED3DPOOL pool = WINED3DPOOL_DEFAULT;
5097 enum wined3d_format_id format;
5098 DWORD usage = 0;
5099 HRESULT hr;
5101 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5102 && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5103 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
5105 /* Tests show surfaces without memory flags get these flags added
5106 * right after creation. */
5107 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5110 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5112 usage |= WINED3DUSAGE_RENDERTARGET;
5113 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5116 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && !(desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
5118 usage |= WINED3DUSAGE_RENDERTARGET;
5121 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
5123 usage |= WINED3DUSAGE_OVERLAY;
5126 if (ddraw->depthstencil || (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
5128 /* The depth stencil creation callback sets this flag. Set the
5129 * wined3d usage to let it know it's a depth/stencil surface. */
5130 usage |= WINED3DUSAGE_DEPTHSTENCIL;
5133 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5135 pool = WINED3DPOOL_SYSTEMMEM;
5137 else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
5139 pool = WINED3DPOOL_MANAGED;
5140 /* Managed textures have the system memory flag set. */
5141 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5143 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5145 /* Videomemory adds localvidmem. This is mutually exclusive with
5146 * systemmemory and texturemanage. */
5147 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
5150 format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
5151 if (format == WINED3DFMT_UNKNOWN)
5153 WARN("Unsupported / unknown pixelformat.\n");
5154 return DDERR_INVALIDPIXELFORMAT;
5157 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
5158 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
5159 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
5160 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
5161 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
5162 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
5163 surface->IDirect3DTexture2_vtbl = &d3d_texture2_vtbl;
5164 surface->IDirect3DTexture_vtbl = &d3d_texture1_vtbl;
5165 surface->iface_count = 1;
5166 surface->version = version;
5167 surface->ddraw = ddraw;
5169 if (version == 4)
5171 surface->ref4 = 1;
5173 else
5175 surface->ref = 1;
5178 copy_to_surfacedesc2(&surface->surface_desc, desc);
5180 surface->first_attached = surface;
5181 surface->ImplType = surface_type;
5183 hr = wined3d_surface_create(ddraw->wined3d_device, desc->dwWidth, desc->dwHeight, format,
5184 TRUE /* Lockable */, FALSE /* Discard */, mip_level, usage, pool,
5185 WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, surface_type, surface,
5186 &ddraw_surface_wined3d_parent_ops, &surface->wined3d_surface);
5187 if (FAILED(hr))
5189 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
5190 return hr;
5193 surface->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
5194 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
5195 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
5197 format = wined3d_desc.format;
5198 if (format == WINED3DFMT_UNKNOWN)
5200 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN.\n");
5202 PixelFormat_WineD3DtoDD(&surface->surface_desc.u4.ddpfPixelFormat, format);
5204 /* Anno 1602 stores the pitch right after surface creation, so make sure
5205 * it's there. TODO: Test other fourcc formats. */
5206 if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
5207 || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
5209 surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
5210 if (format == WINED3DFMT_DXT1)
5212 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height) / 2;
5214 else
5216 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height);
5219 else
5221 surface->surface_desc.dwFlags |= DDSD_PITCH;
5222 surface->surface_desc.u1.lPitch = wined3d_surface_get_pitch(surface->wined3d_surface);
5225 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
5227 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTOVERLAY,
5228 (WINEDDCOLORKEY *)&desc->u3.ddckCKDestOverlay);
5230 if (desc->dwFlags & DDSD_CKDESTBLT)
5232 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTBLT,
5233 (WINEDDCOLORKEY *)&desc->ddckCKDestBlt);
5235 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
5237 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCOVERLAY,
5238 (WINEDDCOLORKEY *)&desc->ddckCKSrcOverlay);
5240 if (desc->dwFlags & DDSD_CKSRCBLT)
5242 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCBLT,
5243 (WINEDDCOLORKEY *)&desc->ddckCKSrcBlt);
5245 if (desc->dwFlags & DDSD_LPSURFACE)
5247 hr = wined3d_surface_set_mem(surface->wined3d_surface, desc->lpSurface);
5248 if (FAILED(hr))
5250 ERR("Failed to set surface memory, hr %#x.\n", hr);
5251 wined3d_surface_decref(surface->wined3d_surface);
5252 return hr;
5256 return DD_OK;