ddraw: Use real flips.
[wine/multimedia.git] / dlls / ddraw / surface.c
blob96caa3f27572f5a04f73efc70304f8f1a3ca8965
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 static HRESULT ddraw_surface_update_frontbuffer(IDirectDrawSurfaceImpl *surface)
44 return wined3d_surface_blt(surface->ddraw->wined3d_frontbuffer, NULL,
45 surface->wined3d_surface, NULL, 0, NULL, WINED3DTEXF_POINT);
48 /*****************************************************************************
49 * IUnknown parts follow
50 *****************************************************************************/
52 /*****************************************************************************
53 * IDirectDrawSurface7::QueryInterface
55 * A normal QueryInterface implementation. For QueryInterface rules
56 * see ddraw.c, IDirectDraw7::QueryInterface. This method
57 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
58 * in all versions, the IDirectDrawGammaControl interface and it can
59 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
61 * Params:
62 * riid: The interface id queried for
63 * obj: Address to write the pointer to
65 * Returns:
66 * S_OK on success
67 * E_NOINTERFACE if the requested interface wasn't found
69 *****************************************************************************/
70 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
72 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
74 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
76 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
77 *obj = NULL;
79 if(!riid)
80 return DDERR_INVALIDPARAMS;
82 if (IsEqualGUID(riid, &IID_IUnknown)
83 || IsEqualGUID(riid, &IID_IDirectDrawSurface7) )
85 IDirectDrawSurface7_AddRef(iface);
86 *obj = iface;
87 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
88 return S_OK;
90 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
92 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
93 *obj = &This->IDirectDrawSurface4_iface;
94 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
95 return S_OK;
97 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
99 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
100 *obj = &This->IDirectDrawSurface3_iface;
101 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
102 return S_OK;
104 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
106 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
107 *obj = &This->IDirectDrawSurface2_iface;
108 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
109 return S_OK;
111 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface))
113 IDirectDrawSurface_AddRef(&This->IDirectDrawSurface_iface);
114 *obj = &This->IDirectDrawSurface_iface;
115 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
116 return S_OK;
118 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
120 IDirectDrawGammaControl_AddRef(&This->IDirectDrawGammaControl_iface);
121 *obj = &This->IDirectDrawGammaControl_iface;
122 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
123 return S_OK;
125 else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
126 IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
127 IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
129 IDirect3DDevice7 *d3d;
131 /* Call into IDirect3D7 for creation */
132 IDirect3D7_CreateDevice(&This->ddraw->IDirect3D7_iface, riid, &This->IDirectDrawSurface7_iface,
133 &d3d);
135 if (d3d)
137 *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
138 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
139 return S_OK;
142 WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
143 return E_NOINTERFACE;
145 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
146 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
148 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
150 *obj = &This->IDirect3DTexture_iface;
151 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
153 else
155 *obj = &This->IDirect3DTexture2_iface;
156 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
158 IUnknown_AddRef( (IUnknown *) *obj);
159 return S_OK;
162 ERR("No interface\n");
163 return E_NOINTERFACE;
166 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
168 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
169 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
171 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
174 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
176 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
177 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
179 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
182 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
184 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
185 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
187 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
190 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
192 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
193 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
195 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
198 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
199 REFIID riid, void **object)
201 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
203 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
205 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
208 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
210 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture2(iface);
211 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
213 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
216 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
218 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture(iface);
219 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
221 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
224 static void ddraw_surface_add_iface(IDirectDrawSurfaceImpl *This)
226 ULONG iface_count = InterlockedIncrement(&This->iface_count);
227 TRACE("%p increasing iface count to %u.\n", This, iface_count);
229 if (iface_count == 1)
231 EnterCriticalSection(&ddraw_cs);
232 if (This->wined3d_surface)
233 wined3d_surface_incref(This->wined3d_surface);
234 if (This->wined3d_texture)
235 wined3d_texture_incref(This->wined3d_texture);
236 LeaveCriticalSection(&ddraw_cs);
240 /*****************************************************************************
241 * IDirectDrawSurface7::AddRef
243 * A normal addref implementation
245 * Returns:
246 * The new refcount
248 *****************************************************************************/
249 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
251 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
252 ULONG refcount = InterlockedIncrement(&This->ref7);
254 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
256 if (refcount == 1)
258 ddraw_surface_add_iface(This);
261 return refcount;
264 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
266 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
267 ULONG refcount = InterlockedIncrement(&This->ref4);
269 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
271 if (refcount == 1)
273 ddraw_surface_add_iface(This);
276 return refcount;
279 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
281 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
282 ULONG refcount = InterlockedIncrement(&This->ref3);
284 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
286 if (refcount == 1)
288 ddraw_surface_add_iface(This);
291 return refcount;
294 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
296 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
297 ULONG refcount = InterlockedIncrement(&This->ref2);
299 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
301 if (refcount == 1)
303 ddraw_surface_add_iface(This);
306 return refcount;
309 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
311 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
312 ULONG refcount = InterlockedIncrement(&This->ref1);
314 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
316 if (refcount == 1)
318 ddraw_surface_add_iface(This);
321 return refcount;
324 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
326 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
327 ULONG refcount = InterlockedIncrement(&This->gamma_count);
329 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
331 if (refcount == 1)
333 ddraw_surface_add_iface(This);
336 return refcount;
339 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
341 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture2(iface);
342 TRACE("iface %p.\n", iface);
344 return ddraw_surface1_AddRef(&This->IDirectDrawSurface_iface);
347 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
349 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture(iface);
350 TRACE("iface %p.\n", iface);
352 return ddraw_surface1_AddRef(&This->IDirectDrawSurface_iface);
355 /*****************************************************************************
356 * ddraw_surface_destroy
358 * A helper function for IDirectDrawSurface7::Release
360 * Frees the surface, regardless of its refcount.
361 * See IDirectDrawSurface7::Release for more information
363 * Params:
364 * This: Surface to free
366 *****************************************************************************/
367 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
369 TRACE("surface %p.\n", This);
371 /* Check the iface count and give a warning */
372 if(This->iface_count > 1)
374 /* This can happen when a complex surface is destroyed,
375 * because the 2nd surface was addref()ed when the app
376 * called GetAttachedSurface
378 WARN("(%p): Destroying surface with refcounts 7: %d 4: %d 3: %d 2: %d 1: %d\n",
379 This, This->ref7, This->ref4, This->ref3, This->ref2, This->ref1);
382 if (This->wined3d_surface)
383 wined3d_surface_decref(This->wined3d_surface);
386 static void ddraw_surface_cleanup(IDirectDrawSurfaceImpl *surface)
388 IDirectDrawSurfaceImpl *surf;
389 IUnknown *ifaceToRelease;
390 UINT i;
392 TRACE("surface %p.\n", surface);
394 if (surface->wined3d_swapchain)
396 IDirectDrawImpl *ddraw = surface->ddraw;
398 /* If it's the render target, destroy the D3D device. */
399 if (ddraw->d3d_initialized && surface == ddraw->d3d_target)
401 TRACE("Destroying the render target, uninitializing D3D.\n");
403 for (i = 0; i < ddraw->numConvertedDecls; ++i)
405 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
407 HeapFree(GetProcessHeap(), 0, ddraw->decls);
408 ddraw->numConvertedDecls = 0;
410 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
412 ERR("Failed to uninit 3D.\n");
414 else
416 /* Free the d3d window if one was created. */
417 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
419 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
420 DestroyWindow(ddraw->d3d_window);
421 ddraw->d3d_window = 0;
425 ddraw->d3d_initialized = FALSE;
426 ddraw->d3d_target = NULL;
428 else
430 wined3d_device_uninit_gdi(ddraw->wined3d_device);
433 surface->wined3d_swapchain = NULL;
435 TRACE("D3D unloaded.\n");
438 /* The refcount test shows that the palette is detached when the surface
439 * is destroyed. */
440 IDirectDrawSurface7_SetPalette(&surface->IDirectDrawSurface7_iface, NULL);
442 /* Loop through all complex attached surfaces and destroy them.
444 * Yet again, only the root can have more than one complexly attached
445 * surface, all the others have a total of one. */
446 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
448 if (!surface->complex_array[i])
449 break;
451 surf = surface->complex_array[i];
452 surface->complex_array[i] = NULL;
453 while (surf)
455 IDirectDrawSurfaceImpl *destroy = surf;
456 surf = surf->complex_array[0]; /* Iterate through the "tree" */
457 ddraw_surface_destroy(destroy); /* Destroy it */
461 ifaceToRelease = surface->ifaceToRelease;
463 /* Destroy the root surface. */
464 ddraw_surface_destroy(surface);
466 /* Reduce the ddraw refcount */
467 if (ifaceToRelease)
468 IUnknown_Release(ifaceToRelease);
471 ULONG ddraw_surface_release_iface(IDirectDrawSurfaceImpl *This)
473 ULONG iface_count = InterlockedDecrement(&This->iface_count);
474 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
476 if (iface_count == 0)
478 /* Complex attached surfaces are destroyed implicitly when the root is released */
479 EnterCriticalSection(&ddraw_cs);
480 if(!This->is_complex_root)
482 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
483 LeaveCriticalSection(&ddraw_cs);
484 return iface_count;
486 if (This->wined3d_texture) /* If it's a texture, destroy the wined3d texture. */
487 wined3d_texture_decref(This->wined3d_texture);
488 else
489 ddraw_surface_cleanup(This);
490 LeaveCriticalSection(&ddraw_cs);
493 return iface_count;
496 /*****************************************************************************
497 * IDirectDrawSurface7::Release
499 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
500 * surface is destroyed.
502 * Destroying the surface is a bit tricky. For the connection between
503 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
504 * It has a nice graph explaining the connection.
506 * What happens here is basically this:
507 * When a surface is destroyed, its WineD3DSurface is released,
508 * and the refcount of the DirectDraw interface is reduced by 1. If it has
509 * complex surfaces attached to it, then these surfaces are destroyed too,
510 * regardless of their refcount. If any surface being destroyed has another
511 * surface attached to it (with a "soft" attachment, not complex), then
512 * this surface is detached with DeleteAttachedSurface.
514 * When the surface is a texture, the WineD3DTexture is released.
515 * If the surface is the Direct3D render target, then the D3D
516 * capabilities of the WineD3DDevice are uninitialized, which causes the
517 * swapchain to be released.
519 * When a complex sublevel falls to ref zero, then this is ignored.
521 * Returns:
522 * The new refcount
524 *****************************************************************************/
525 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
527 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
528 ULONG refcount = InterlockedDecrement(&This->ref7);
530 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
532 if (refcount == 0)
534 ddraw_surface_release_iface(This);
537 return refcount;
540 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
542 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
543 ULONG refcount = InterlockedDecrement(&This->ref4);
545 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
547 if (refcount == 0)
549 ddraw_surface_release_iface(This);
552 return refcount;
555 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
557 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
558 ULONG refcount = InterlockedDecrement(&This->ref3);
560 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
562 if (refcount == 0)
564 ddraw_surface_release_iface(This);
567 return refcount;
570 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
572 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
573 ULONG refcount = InterlockedDecrement(&This->ref2);
575 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
577 if (refcount == 0)
579 ddraw_surface_release_iface(This);
582 return refcount;
585 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
587 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
588 ULONG refcount = InterlockedDecrement(&This->ref1);
590 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
592 if (refcount == 0)
594 ddraw_surface_release_iface(This);
597 return refcount;
600 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
602 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
603 ULONG refcount = InterlockedDecrement(&This->gamma_count);
605 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
607 if (refcount == 0)
609 ddraw_surface_release_iface(This);
612 return refcount;
615 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
617 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture2(iface);
618 TRACE("iface %p.\n", iface);
620 return ddraw_surface1_Release(&This->IDirectDrawSurface_iface);
623 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
625 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture(iface);
626 TRACE("iface %p.\n", iface);
628 return ddraw_surface1_Release(&This->IDirectDrawSurface_iface);
631 /*****************************************************************************
632 * IDirectDrawSurface7::GetAttachedSurface
634 * Returns an attached surface with the requested caps. Surface attachment
635 * and complex surfaces are not clearly described by the MSDN or sdk,
636 * so this method is tricky and likely to contain problems.
637 * This implementation searches the complex list first, then the
638 * attachment chain.
640 * The chains are searched from This down to the last surface in the chain,
641 * not from the first element in the chain. The first surface found is
642 * returned. The MSDN says that this method fails if more than one surface
643 * matches the caps, but it is not sure if that is right. The attachment
644 * structure may not even allow two matching surfaces.
646 * The found surface is AddRef-ed before it is returned.
648 * Params:
649 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
650 * Surface: Address to store the found surface
652 * Returns:
653 * DD_OK on success
654 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
655 * DDERR_NOTFOUND if no surface was found
657 *****************************************************************************/
658 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
659 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
661 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
662 IDirectDrawSurfaceImpl *surf;
663 DDSCAPS2 our_caps;
664 int i;
666 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
668 EnterCriticalSection(&ddraw_cs);
670 if(This->version < 7)
672 /* Earlier dx apps put garbage into these members, clear them */
673 our_caps.dwCaps = Caps->dwCaps;
674 our_caps.dwCaps2 = 0;
675 our_caps.dwCaps3 = 0;
676 our_caps.dwCaps4 = 0;
678 else
680 our_caps = *Caps;
683 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 */
685 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
687 surf = This->complex_array[i];
688 if(!surf) break;
690 if (TRACE_ON(ddraw))
692 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
693 surf->surface_desc.ddsCaps.dwCaps,
694 surf->surface_desc.ddsCaps.dwCaps2,
695 surf->surface_desc.ddsCaps.dwCaps3,
696 surf->surface_desc.ddsCaps.dwCaps4);
699 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
700 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
702 /* MSDN: "This method fails if more than one surface is attached
703 * that matches the capabilities requested."
705 * Not sure how to test this.
708 TRACE("(%p): Returning surface %p\n", This, surf);
709 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
710 *Surface = &surf->IDirectDrawSurface7_iface;
711 ddraw_surface7_AddRef(*Surface);
712 LeaveCriticalSection(&ddraw_cs);
713 return DD_OK;
717 /* Next, look at the attachment chain */
718 surf = This;
720 while( (surf = surf->next_attached) )
722 if (TRACE_ON(ddraw))
724 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
725 surf->surface_desc.ddsCaps.dwCaps,
726 surf->surface_desc.ddsCaps.dwCaps2,
727 surf->surface_desc.ddsCaps.dwCaps3,
728 surf->surface_desc.ddsCaps.dwCaps4);
731 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
732 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
734 TRACE("(%p): Returning surface %p\n", This, surf);
735 *Surface = &surf->IDirectDrawSurface7_iface;
736 ddraw_surface7_AddRef(*Surface);
737 LeaveCriticalSection(&ddraw_cs);
738 return DD_OK;
742 TRACE("(%p) Didn't find a valid surface\n", This);
743 LeaveCriticalSection(&ddraw_cs);
745 *Surface = NULL;
746 return DDERR_NOTFOUND;
749 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
750 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
752 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
753 IDirectDrawSurface7 *attachment7;
754 IDirectDrawSurfaceImpl *attachment_impl;
755 HRESULT hr;
757 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
759 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
760 caps, &attachment7);
761 if (FAILED(hr))
763 *attachment = NULL;
764 return hr;
766 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
767 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
768 ddraw_surface4_AddRef(*attachment);
769 ddraw_surface7_Release(attachment7);
771 return hr;
774 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
775 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
777 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
778 IDirectDrawSurface7 *attachment7;
779 IDirectDrawSurfaceImpl *attachment_impl;
780 DDSCAPS2 caps2;
781 HRESULT hr;
783 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
785 caps2.dwCaps = caps->dwCaps;
786 caps2.dwCaps2 = 0;
787 caps2.dwCaps3 = 0;
788 caps2.dwCaps4 = 0;
790 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
791 &caps2, &attachment7);
792 if (FAILED(hr))
794 *attachment = NULL;
795 return hr;
797 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
798 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
799 ddraw_surface3_AddRef(*attachment);
800 ddraw_surface7_Release(attachment7);
802 return hr;
805 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
806 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
808 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
809 IDirectDrawSurface7 *attachment7;
810 IDirectDrawSurfaceImpl *attachment_impl;
811 DDSCAPS2 caps2;
812 HRESULT hr;
814 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
816 caps2.dwCaps = caps->dwCaps;
817 caps2.dwCaps2 = 0;
818 caps2.dwCaps3 = 0;
819 caps2.dwCaps4 = 0;
821 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
822 &caps2, &attachment7);
823 if (FAILED(hr))
825 *attachment = NULL;
826 return hr;
828 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
829 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
830 ddraw_surface2_AddRef(*attachment);
831 ddraw_surface7_Release(attachment7);
833 return hr;
836 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
837 DDSCAPS *caps, IDirectDrawSurface **attachment)
839 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
840 IDirectDrawSurface7 *attachment7;
841 IDirectDrawSurfaceImpl *attachment_impl;
842 DDSCAPS2 caps2;
843 HRESULT hr;
845 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
847 caps2.dwCaps = caps->dwCaps;
848 caps2.dwCaps2 = 0;
849 caps2.dwCaps3 = 0;
850 caps2.dwCaps4 = 0;
852 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
853 &caps2, &attachment7);
854 if (FAILED(hr))
856 *attachment = NULL;
857 return hr;
859 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
860 *attachment = &attachment_impl->IDirectDrawSurface_iface;
861 ddraw_surface1_AddRef(*attachment);
862 ddraw_surface7_Release(attachment7);
864 return hr;
867 /*****************************************************************************
868 * IDirectDrawSurface7::Lock
870 * Locks the surface and returns a pointer to the surface's memory
872 * Params:
873 * Rect: Rectangle to lock. If NULL, the whole surface is locked
874 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
875 * Flags: Locking flags, e.g Read only or write only
876 * h: An event handle that's not used and must be NULL
878 * Returns:
879 * DD_OK on success
880 * DDERR_INVALIDPARAMS if DDSD is NULL
881 * For more details, see IWineD3DSurface::LockRect
883 *****************************************************************************/
884 static HRESULT surface_lock(IDirectDrawSurfaceImpl *This,
885 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
887 WINED3DLOCKED_RECT LockedRect;
888 HRESULT hr;
890 TRACE("This %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
891 This, wine_dbgstr_rect(Rect), DDSD, Flags, h);
893 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
894 EnterCriticalSection(&ddraw_cs);
896 /* Should I check for the handle to be NULL?
898 * The DDLOCK flags and the D3DLOCK flags are equal
899 * for the supported values. The others are ignored by WineD3D
902 /* Windows zeroes this if the rect is invalid */
903 DDSD->lpSurface = 0;
905 if (Rect)
907 if ((Rect->left < 0)
908 || (Rect->top < 0)
909 || (Rect->left > Rect->right)
910 || (Rect->top > Rect->bottom)
911 || (Rect->right > This->surface_desc.dwWidth)
912 || (Rect->bottom > This->surface_desc.dwHeight))
914 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
915 LeaveCriticalSection(&ddraw_cs);
916 return DDERR_INVALIDPARAMS;
920 hr = wined3d_surface_map(This->wined3d_surface, &LockedRect, Rect, Flags);
921 if (FAILED(hr))
923 LeaveCriticalSection(&ddraw_cs);
924 switch(hr)
926 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
927 * specific error. But since IWineD3DSurface::LockRect returns that error in this
928 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
929 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
930 * is much easier to do it in one place in ddraw
932 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
933 default: return hr;
937 /* Override the memory area. The pitch should be set already. Strangely windows
938 * does not set the LPSURFACE flag on locked surfaces !?!.
939 * DDSD->dwFlags |= DDSD_LPSURFACE;
941 This->surface_desc.lpSurface = LockedRect.pBits;
942 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
944 TRACE("locked surface returning description :\n");
945 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
947 LeaveCriticalSection(&ddraw_cs);
948 return DD_OK;
951 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
952 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
954 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
955 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
956 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
958 if (!surface_desc) return DDERR_INVALIDPARAMS;
959 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
960 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
962 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
963 return DDERR_INVALIDPARAMS;
965 return surface_lock(This, rect, surface_desc, flags, h);
968 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
969 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
971 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
972 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
973 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
975 if (!surface_desc) return DDERR_INVALIDPARAMS;
976 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
977 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
979 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
980 return DDERR_INVALIDPARAMS;
982 return surface_lock(This, rect, surface_desc, flags, h);
985 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
986 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
988 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
989 DDSURFACEDESC2 surface_desc2;
990 HRESULT hr;
991 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
992 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
994 if (!surface_desc) return DDERR_INVALIDPARAMS;
995 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
996 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
998 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
999 return DDERR_INVALIDPARAMS;
1002 surface_desc2.dwSize = surface_desc->dwSize;
1003 hr = surface_lock(This, rect, &surface_desc2, flags, h);
1004 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1005 surface_desc->dwSize = surface_desc2.dwSize;
1006 return hr;
1009 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1010 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1012 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1013 DDSURFACEDESC2 surface_desc2;
1014 HRESULT hr;
1015 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1016 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1018 if (!surface_desc) return DDERR_INVALIDPARAMS;
1019 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1020 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1022 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1023 return DDERR_INVALIDPARAMS;
1026 surface_desc2.dwSize = surface_desc->dwSize;
1027 hr = surface_lock(This, rect, &surface_desc2, flags, h);
1028 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1029 surface_desc->dwSize = surface_desc2.dwSize;
1030 return hr;
1033 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1034 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1036 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1037 DDSURFACEDESC2 surface_desc2;
1038 HRESULT hr;
1039 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1040 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1042 if (!surface_desc) return DDERR_INVALIDPARAMS;
1043 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1044 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1046 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1047 return DDERR_INVALIDPARAMS;
1050 surface_desc2.dwSize = surface_desc->dwSize;
1051 hr = surface_lock(This, rect, &surface_desc2, flags, h);
1052 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1053 surface_desc->dwSize = surface_desc2.dwSize;
1054 return hr;
1057 /*****************************************************************************
1058 * IDirectDrawSurface7::Unlock
1060 * Unlocks an locked surface
1062 * Params:
1063 * Rect: Not used by this implementation
1065 * Returns:
1066 * D3D_OK on success
1067 * For more details, see IWineD3DSurface::UnlockRect
1069 *****************************************************************************/
1070 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
1072 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1073 HRESULT hr;
1075 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1077 EnterCriticalSection(&ddraw_cs);
1078 hr = wined3d_surface_unmap(This->wined3d_surface);
1079 if (SUCCEEDED(hr))
1081 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1082 hr = ddraw_surface_update_frontbuffer(This);
1083 This->surface_desc.lpSurface = NULL;
1085 LeaveCriticalSection(&ddraw_cs);
1086 return hr;
1089 static HRESULT WINAPI ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1091 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1092 TRACE("iface %p, rect %p.\n", iface, pRect);
1094 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, pRect);
1097 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1099 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1100 TRACE("iface %p, data %p.\n", iface, data);
1102 /* data might not be the LPRECT of later versions, so drop it. */
1103 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1106 static HRESULT WINAPI ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1108 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1109 TRACE("iface %p, data %p.\n", iface, data);
1111 /* data might not be the LPRECT of later versions, so drop it. */
1112 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1115 static HRESULT WINAPI ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1117 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1118 TRACE("iface %p, data %p.\n", iface, data);
1120 /* data might not be the LPRECT of later versions, so drop it. */
1121 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1124 /*****************************************************************************
1125 * IDirectDrawSurface7::Flip
1127 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
1128 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
1129 * the flip target is passed to WineD3D, even if the app didn't specify one
1131 * Params:
1132 * DestOverride: Specifies the surface that will become the new front
1133 * buffer. If NULL, the current back buffer is used
1134 * Flags: some DirectDraw flags, see include/ddraw.h
1136 * Returns:
1137 * DD_OK on success
1138 * DDERR_NOTFLIPPABLE if no flip target could be found
1139 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
1140 * For more details, see IWineD3DSurface::Flip
1142 *****************************************************************************/
1143 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
1145 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1146 IDirectDrawSurfaceImpl *Override = unsafe_impl_from_IDirectDrawSurface7(DestOverride);
1147 IDirectDrawSurface7 *Override7;
1148 HRESULT hr;
1150 TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
1152 /* Flip has to be called from a front buffer
1153 * What about overlay surfaces, AFAIK they can flip too?
1155 if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
1156 return DDERR_INVALIDOBJECT; /* Unchecked */
1158 EnterCriticalSection(&ddraw_cs);
1160 /* WineD3D doesn't keep track of attached surface, so find the target */
1161 if(!Override)
1163 DDSCAPS2 Caps;
1165 memset(&Caps, 0, sizeof(Caps));
1166 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
1167 hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
1168 if(hr != DD_OK)
1170 ERR("Can't find a flip target\n");
1171 LeaveCriticalSection(&ddraw_cs);
1172 return DDERR_NOTFLIPPABLE; /* Unchecked */
1174 Override = impl_from_IDirectDrawSurface7(Override7);
1176 /* For the GetAttachedSurface */
1177 ddraw_surface7_Release(Override7);
1180 hr = wined3d_surface_flip(This->wined3d_surface, Override->wined3d_surface, Flags);
1181 if (SUCCEEDED(hr) && This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1182 hr = ddraw_surface_update_frontbuffer(This);
1184 LeaveCriticalSection(&ddraw_cs);
1185 return hr;
1188 static HRESULT WINAPI ddraw_surface4_Flip(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *dst, DWORD flags)
1190 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1191 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1192 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1194 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1195 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1198 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
1200 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1201 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1202 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1204 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1205 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1208 static HRESULT WINAPI ddraw_surface2_Flip(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *dst, DWORD flags)
1210 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1211 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1212 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1214 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1215 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1218 static HRESULT WINAPI ddraw_surface1_Flip(IDirectDrawSurface *iface, IDirectDrawSurface *dst, DWORD flags)
1220 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1221 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1222 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1224 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1225 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1228 /*****************************************************************************
1229 * IDirectDrawSurface7::Blt
1231 * Performs a blit on the surface
1233 * Params:
1234 * DestRect: Destination rectangle, can be NULL
1235 * SrcSurface: Source surface, can be NULL
1236 * SrcRect: Source rectangle, can be NULL
1237 * Flags: Blt flags
1238 * DDBltFx: Some extended blt parameters, connected to the flags
1240 * Returns:
1241 * D3D_OK on success
1242 * See IWineD3DSurface::Blt for more details
1244 *****************************************************************************/
1245 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1246 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1248 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1249 IDirectDrawSurfaceImpl *Src = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1250 HRESULT hr;
1252 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1253 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1255 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1256 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1258 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1259 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1260 return DDERR_INVALIDPARAMS;
1263 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1264 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1265 return DDERR_INVALIDPARAMS;
1268 EnterCriticalSection(&ddraw_cs);
1270 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
1271 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1272 LeaveCriticalSection(&ddraw_cs);
1273 return DDERR_INVALIDPARAMS;
1276 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1277 * does, copy the struct, and replace the ddraw surfaces with the wined3d
1278 * surfaces. So far no blitting operations using surfaces in the bltfx
1279 * struct are supported anyway. */
1280 hr = wined3d_surface_blt(This->wined3d_surface, DestRect, Src ? Src->wined3d_surface : NULL,
1281 SrcRect, Flags, (WINEDDBLTFX *)DDBltFx, WINED3DTEXF_LINEAR);
1282 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER))
1283 hr = ddraw_surface_update_frontbuffer(This);
1285 LeaveCriticalSection(&ddraw_cs);
1286 switch(hr)
1288 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1289 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
1290 default: return hr;
1294 static HRESULT WINAPI ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1295 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1297 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1298 IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1299 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1300 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1302 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1303 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1306 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1307 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1309 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1310 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1311 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1312 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1314 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1315 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1318 static HRESULT WINAPI ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1319 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1321 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1322 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1323 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1324 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1326 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1327 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1330 static HRESULT WINAPI ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1331 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1333 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1334 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1335 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1336 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1338 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1339 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1342 /*****************************************************************************
1343 * IDirectDrawSurface7::AddAttachedSurface
1345 * Attaches a surface to another surface. How the surface attachments work
1346 * is not totally understood yet, and this method is prone to problems.
1347 * he surface that is attached is AddRef-ed.
1349 * Tests with complex surfaces suggest that the surface attachments form a
1350 * tree, but no method to test this has been found yet.
1352 * The attachment list consists of a first surface (first_attached) and
1353 * for each surface a pointer to the next attached surface (next_attached).
1354 * For the first surface, and a surface that has no attachments
1355 * first_attached points to the surface itself. A surface that has
1356 * no successors in the chain has next_attached set to NULL.
1358 * Newly attached surfaces are attached right after the root surface.
1359 * If a surface is attached to a complex surface compound, it's attached to
1360 * the surface that the app requested, not the complex root. See
1361 * GetAttachedSurface for a description how surfaces are found.
1363 * This is how the current implementation works, and it was coded by looking
1364 * at the needs of the applications.
1366 * So far only Z-Buffer attachments are tested, and they are activated in
1367 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1368 * Back buffers should work in 2D mode, but they are not tested(They can be
1369 * attached in older iface versions). Rendering to the front buffer and
1370 * switching between that and double buffering is not yet implemented in
1371 * WineD3D, so for 3D it might have unexpected results.
1373 * ddraw_surface_attach_surface is the real thing,
1374 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1375 * performs additional checks. Version 7 of this interface is much more restrictive
1376 * than its predecessors.
1378 * Params:
1379 * Attach: Surface to attach to iface
1381 * Returns:
1382 * DD_OK on success
1383 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1385 *****************************************************************************/
1386 static HRESULT ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
1388 TRACE("surface %p, attachment %p.\n", This, Surf);
1390 if(Surf == This)
1391 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1393 EnterCriticalSection(&ddraw_cs);
1395 /* Check if the surface is already attached somewhere */
1396 if (Surf->next_attached || Surf->first_attached != Surf)
1398 /* TODO: Test for the structure of the manual attachment. Is it a
1399 * chain or a list? What happens if one surface is attached to 2
1400 * different surfaces? */
1401 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1402 Surf, Surf->next_attached, Surf->first_attached);
1404 LeaveCriticalSection(&ddraw_cs);
1405 return DDERR_SURFACEALREADYATTACHED;
1408 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1409 Surf->next_attached = This->next_attached;
1410 Surf->first_attached = This->first_attached;
1411 This->next_attached = Surf;
1413 /* Check if the WineD3D depth stencil needs updating */
1414 if(This->ddraw->d3ddevice)
1416 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1419 LeaveCriticalSection(&ddraw_cs);
1420 return DD_OK;
1423 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
1425 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1426 IDirectDrawSurfaceImpl *Surf = unsafe_impl_from_IDirectDrawSurface7(Attach);
1427 HRESULT hr;
1429 TRACE("iface %p, attachment %p.\n", iface, Attach);
1431 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1432 if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1435 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1436 Surf->surface_desc.ddsCaps.dwCaps);
1437 return DDERR_CANNOTATTACHSURFACE;
1440 hr = ddraw_surface_attach_surface(This, Surf);
1441 if (FAILED(hr))
1443 return hr;
1445 ddraw_surface7_AddRef(Attach);
1446 return hr;
1449 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1451 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1452 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1453 HRESULT hr;
1455 TRACE("iface %p, attachment %p.\n", iface, attachment);
1457 hr = ddraw_surface7_AddAttachedSurface(&This->IDirectDrawSurface7_iface,
1458 attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1459 if (FAILED(hr))
1461 return hr;
1463 ddraw_surface4_AddRef(attachment);
1464 ddraw_surface7_Release(&attachment_impl->IDirectDrawSurface7_iface);
1465 return hr;
1467 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1469 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1470 IDirectDrawSurfaceImpl *attach_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1471 HRESULT hr;
1473 TRACE("iface %p, attachment %p.\n", iface, attachment);
1475 /* Tests suggest that
1476 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1477 * -> offscreen plain surfaces can be attached to primaries
1478 * -> primaries can be attached to offscreen plain surfaces
1479 * -> z buffers can be attached to primaries */
1480 if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1481 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1483 /* Sizes have to match */
1484 if (attach_impl->surface_desc.dwWidth != This->surface_desc.dwWidth
1485 || attach_impl->surface_desc.dwHeight != This->surface_desc.dwHeight)
1487 WARN("Surface sizes do not match.\n");
1488 return DDERR_CANNOTATTACHSURFACE;
1490 /* OK */
1492 else if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1493 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1495 /* OK */
1497 else
1499 WARN("Invalid attachment combination.\n");
1500 return DDERR_CANNOTATTACHSURFACE;
1503 hr = ddraw_surface_attach_surface(This, attach_impl);
1504 if (FAILED(hr))
1506 return hr;
1508 ddraw_surface3_AddRef(attachment);
1509 return hr;
1512 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1514 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1515 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1516 HRESULT hr;
1518 TRACE("iface %p, attachment %p.\n", iface, attachment);
1520 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1521 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1522 if (FAILED(hr))
1524 return hr;
1526 ddraw_surface2_AddRef(attachment);
1527 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1528 return hr;
1531 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1533 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1534 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1535 HRESULT hr;
1537 TRACE("iface %p, attachment %p.\n", iface, attachment);
1539 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1540 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1541 if (FAILED(hr))
1543 return hr;
1545 ddraw_surface1_AddRef(attachment);
1546 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1547 return hr;
1550 /*****************************************************************************
1551 * IDirectDrawSurface7::DeleteAttachedSurface
1553 * Removes a surface from the attachment chain. The surface's refcount
1554 * is decreased by one after it has been removed
1556 * Params:
1557 * Flags: Some flags, not used by this implementation
1558 * Attach: Surface to detach
1560 * Returns:
1561 * DD_OK on success
1562 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1564 *****************************************************************************/
1565 static HRESULT ddraw_surface_delete_attached_surface(IDirectDrawSurfaceImpl *This,
1566 IDirectDrawSurfaceImpl *Surf)
1568 IDirectDrawSurfaceImpl *Prev = This;
1570 TRACE("surface %p, attachment %p.\n", This, Surf);
1572 EnterCriticalSection(&ddraw_cs);
1573 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1575 LeaveCriticalSection(&ddraw_cs);
1576 return DDERR_CANNOTDETACHSURFACE;
1579 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1580 if (This->surface_desc.ddsCaps.dwCaps &
1581 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1583 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1584 /* FIXME: we should probably also subtract from dwMipMapCount of this
1585 * and all parent surfaces */
1588 /* Find the predecessor of the detached surface */
1589 while(Prev)
1591 if(Prev->next_attached == Surf) break;
1592 Prev = Prev->next_attached;
1595 /* There must be a surface, otherwise there's a bug */
1596 assert(Prev != NULL);
1598 /* Unchain the surface */
1599 Prev->next_attached = Surf->next_attached;
1600 Surf->next_attached = NULL;
1601 Surf->first_attached = Surf;
1603 /* Check if the WineD3D depth stencil needs updating */
1604 if(This->ddraw->d3ddevice)
1606 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1608 LeaveCriticalSection(&ddraw_cs);
1609 return DD_OK;
1612 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1613 DWORD flags, IDirectDrawSurface7 *attachment)
1615 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1616 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1617 HRESULT hr;
1619 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1621 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1622 if (FAILED(hr))
1624 return hr;
1626 ddraw_surface7_Release(attachment);
1627 return hr;
1630 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1631 DWORD flags, IDirectDrawSurface4 *attachment)
1633 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1634 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1635 HRESULT hr;
1637 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1639 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1640 if (FAILED(hr))
1642 return hr;
1644 ddraw_surface4_Release(attachment);
1645 return hr;
1648 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1649 DWORD flags, IDirectDrawSurface3 *attachment)
1651 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1652 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1653 HRESULT hr;
1654 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1656 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1657 if (FAILED(hr))
1659 return hr;
1661 ddraw_surface3_Release(attachment);
1662 return hr;
1665 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
1666 DWORD flags, IDirectDrawSurface2 *attachment)
1668 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1669 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1670 HRESULT hr;
1671 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1673 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1674 if (FAILED(hr))
1676 return hr;
1678 ddraw_surface2_Release(attachment);
1679 return hr;
1682 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
1683 DWORD flags, IDirectDrawSurface *attachment)
1685 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1686 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1687 HRESULT hr;
1688 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1690 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1691 if (FAILED(hr))
1693 return hr;
1695 ddraw_surface1_Release(attachment);
1696 return hr;
1699 /*****************************************************************************
1700 * IDirectDrawSurface7::AddOverlayDirtyRect
1702 * "This method is not currently implemented"
1704 * Params:
1705 * Rect: ?
1707 * Returns:
1708 * DDERR_UNSUPPORTED
1710 *****************************************************************************/
1711 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1713 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1715 return DDERR_UNSUPPORTED; /* unchecked */
1718 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
1720 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1721 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1723 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1726 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1728 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1729 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1731 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1734 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
1736 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1737 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1739 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1742 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
1744 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1745 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1747 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1750 /*****************************************************************************
1751 * IDirectDrawSurface7::GetDC
1753 * Returns a GDI device context for the surface
1755 * Params:
1756 * hdc: Address of a HDC variable to store the dc to
1758 * Returns:
1759 * DD_OK on success
1760 * DDERR_INVALIDPARAMS if hdc is NULL
1761 * For details, see IWineD3DSurface::GetDC
1763 *****************************************************************************/
1764 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1766 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1767 HRESULT hr;
1769 TRACE("iface %p, dc %p.\n", iface, hdc);
1771 if(!hdc)
1772 return DDERR_INVALIDPARAMS;
1774 EnterCriticalSection(&ddraw_cs);
1775 hr = wined3d_surface_getdc(This->wined3d_surface, hdc);
1776 LeaveCriticalSection(&ddraw_cs);
1777 switch(hr)
1779 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1780 * touch *hdc
1782 case WINED3DERR_INVALIDCALL:
1783 if(hdc) *hdc = NULL;
1784 return DDERR_INVALIDPARAMS;
1786 default: return hr;
1790 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
1792 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1793 TRACE("iface %p, dc %p.\n", iface, dc);
1795 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1798 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1800 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1801 TRACE("iface %p, dc %p.\n", iface, dc);
1803 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1806 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
1808 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1809 TRACE("iface %p, dc %p.\n", iface, dc);
1811 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1814 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
1816 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1817 TRACE("iface %p, dc %p.\n", iface, dc);
1819 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1822 /*****************************************************************************
1823 * IDirectDrawSurface7::ReleaseDC
1825 * Releases the DC that was constructed with GetDC
1827 * Params:
1828 * hdc: HDC to release
1830 * Returns:
1831 * DD_OK on success
1832 * For more details, see IWineD3DSurface::ReleaseDC
1834 *****************************************************************************/
1835 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1837 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1838 HRESULT hr;
1840 TRACE("iface %p, dc %p.\n", iface, hdc);
1842 EnterCriticalSection(&ddraw_cs);
1843 hr = wined3d_surface_releasedc(This->wined3d_surface, hdc);
1844 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER))
1845 hr = ddraw_surface_update_frontbuffer(This);
1846 LeaveCriticalSection(&ddraw_cs);
1847 return hr;
1850 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
1852 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1853 TRACE("iface %p, dc %p.\n", iface, dc);
1855 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1858 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1860 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1861 TRACE("iface %p, dc %p.\n", iface, dc);
1863 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1866 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
1868 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1869 TRACE("iface %p, dc %p.\n", iface, dc);
1871 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1874 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
1876 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1877 TRACE("iface %p, dc %p.\n", iface, dc);
1879 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1882 /*****************************************************************************
1883 * IDirectDrawSurface7::GetCaps
1885 * Returns the surface's caps
1887 * Params:
1888 * Caps: Address to write the caps to
1890 * Returns:
1891 * DD_OK on success
1892 * DDERR_INVALIDPARAMS if Caps is NULL
1894 *****************************************************************************/
1895 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1897 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1899 TRACE("iface %p, caps %p.\n", iface, Caps);
1901 if(!Caps)
1902 return DDERR_INVALIDPARAMS;
1904 *Caps = This->surface_desc.ddsCaps;
1905 return DD_OK;
1908 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
1910 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1911 TRACE("iface %p, caps %p.\n", iface, caps);
1913 return ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, caps);
1916 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1918 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1919 DDSCAPS2 caps2;
1920 HRESULT hr;
1922 TRACE("iface %p, caps %p.\n", iface, caps);
1924 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1925 if (FAILED(hr)) return hr;
1927 caps->dwCaps = caps2.dwCaps;
1928 return hr;
1931 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
1933 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1934 DDSCAPS2 caps2;
1935 HRESULT hr;
1937 TRACE("iface %p, caps %p.\n", iface, caps);
1939 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1940 if (FAILED(hr)) return hr;
1942 caps->dwCaps = caps2.dwCaps;
1943 return hr;
1946 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
1948 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1949 DDSCAPS2 caps2;
1950 HRESULT hr;
1952 TRACE("iface %p, caps %p.\n", iface, caps);
1954 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1955 if (FAILED(hr)) return hr;
1957 caps->dwCaps = caps2.dwCaps;
1958 return hr;
1961 /*****************************************************************************
1962 * IDirectDrawSurface7::SetPriority
1964 * Sets a texture priority for managed textures.
1966 * Params:
1967 * Priority: The new priority
1969 * Returns:
1970 * DD_OK on success
1971 * For more details, see IWineD3DSurface::SetPriority
1973 *****************************************************************************/
1974 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1976 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1977 HRESULT hr;
1979 TRACE("iface %p, priority %u.\n", iface, Priority);
1981 EnterCriticalSection(&ddraw_cs);
1982 hr = wined3d_surface_set_priority(This->wined3d_surface, Priority);
1983 LeaveCriticalSection(&ddraw_cs);
1984 return hr;
1987 /*****************************************************************************
1988 * IDirectDrawSurface7::GetPriority
1990 * Returns the surface's priority
1992 * Params:
1993 * Priority: Address of a variable to write the priority to
1995 * Returns:
1996 * D3D_OK on success
1997 * DDERR_INVALIDPARAMS if Priority == NULL
1998 * For more details, see IWineD3DSurface::GetPriority
2000 *****************************************************************************/
2001 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
2003 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2005 TRACE("iface %p, priority %p.\n", iface, Priority);
2007 if(!Priority)
2009 return DDERR_INVALIDPARAMS;
2012 EnterCriticalSection(&ddraw_cs);
2013 *Priority = wined3d_surface_get_priority(This->wined3d_surface);
2014 LeaveCriticalSection(&ddraw_cs);
2015 return DD_OK;
2018 /*****************************************************************************
2019 * IDirectDrawSurface7::SetPrivateData
2021 * Stores some data in the surface that is intended for the application's
2022 * use.
2024 * Params:
2025 * tag: GUID that identifies the data
2026 * Data: Pointer to the private data
2027 * Size: Size of the private data
2028 * Flags: Some flags
2030 * Returns:
2031 * D3D_OK on success
2032 * For more details, see IWineD3DSurface::SetPrivateData
2034 *****************************************************************************/
2035 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2036 REFGUID tag, void *Data, DWORD Size, DWORD Flags)
2038 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2039 struct wined3d_resource *resource;
2040 HRESULT hr;
2042 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2043 iface, debugstr_guid(tag), Data, Size, Flags);
2045 EnterCriticalSection(&ddraw_cs);
2046 resource = wined3d_surface_get_resource(This->wined3d_surface);
2047 hr = wined3d_resource_set_private_data(resource, tag, Data, Size, Flags);
2048 LeaveCriticalSection(&ddraw_cs);
2049 switch(hr)
2051 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2052 default: return hr;
2056 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2057 REFGUID tag, void *data, DWORD size, DWORD flags)
2059 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2060 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2061 iface, debugstr_guid(tag), data, size, flags);
2063 return ddraw_surface7_SetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size, flags);
2066 /*****************************************************************************
2067 * IDirectDrawSurface7::GetPrivateData
2069 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2071 * Params:
2072 * tag: GUID of the data to return
2073 * Data: Address where to write the data to
2074 * Size: Size of the buffer at Data
2076 * Returns:
2077 * DD_OK on success
2078 * DDERR_INVALIDPARAMS if Data is NULL
2079 * For more details, see IWineD3DSurface::GetPrivateData
2081 *****************************************************************************/
2082 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
2084 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2085 struct wined3d_resource *resource;
2086 HRESULT hr;
2088 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2089 iface, debugstr_guid(tag), Data, Size);
2091 if(!Data)
2092 return DDERR_INVALIDPARAMS;
2094 EnterCriticalSection(&ddraw_cs);
2095 resource = wined3d_surface_get_resource(This->wined3d_surface);
2096 hr = wined3d_resource_get_private_data(resource, tag, Data, Size);
2097 LeaveCriticalSection(&ddraw_cs);
2098 return hr;
2101 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2103 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2104 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2105 iface, debugstr_guid(tag), data, size);
2107 return ddraw_surface7_GetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size);
2110 /*****************************************************************************
2111 * IDirectDrawSurface7::FreePrivateData
2113 * Frees private data stored in the surface
2115 * Params:
2116 * tag: Tag of the data to free
2118 * Returns:
2119 * D3D_OK on success
2120 * For more details, see IWineD3DSurface::FreePrivateData
2122 *****************************************************************************/
2123 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2125 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2126 struct wined3d_resource *resource;
2127 HRESULT hr;
2129 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2131 EnterCriticalSection(&ddraw_cs);
2132 resource = wined3d_surface_get_resource(This->wined3d_surface);
2133 hr = wined3d_resource_free_private_data(resource, tag);
2134 LeaveCriticalSection(&ddraw_cs);
2135 return hr;
2138 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2140 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2141 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2143 return ddraw_surface7_FreePrivateData(&This->IDirectDrawSurface7_iface, tag);
2146 /*****************************************************************************
2147 * IDirectDrawSurface7::PageLock
2149 * Prevents a sysmem surface from being paged out
2151 * Params:
2152 * Flags: Not used, must be 0(unchecked)
2154 * Returns:
2155 * DD_OK, because it's a stub
2157 *****************************************************************************/
2158 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2160 TRACE("iface %p, flags %#x.\n", iface, Flags);
2162 /* This is Windows memory management related - we don't need this */
2163 return DD_OK;
2166 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2168 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2169 TRACE("iface %p, flags %#x.\n", iface, flags);
2171 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2174 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2176 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2177 TRACE("iface %p, flags %#x.\n", iface, flags);
2179 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2182 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2184 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2185 TRACE("iface %p, flags %#x.\n", iface, flags);
2187 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2190 /*****************************************************************************
2191 * IDirectDrawSurface7::PageUnlock
2193 * Allows a sysmem surface to be paged out
2195 * Params:
2196 * Flags: Not used, must be 0(unchecked)
2198 * Returns:
2199 * DD_OK, because it's a stub
2201 *****************************************************************************/
2202 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2204 TRACE("iface %p, flags %#x.\n", iface, Flags);
2206 return DD_OK;
2209 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2211 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2212 TRACE("iface %p, flags %#x.\n", iface, flags);
2214 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2217 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2219 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2220 TRACE("iface %p, flags %#x.\n", iface, flags);
2222 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2225 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2227 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2228 TRACE("iface %p, flags %#x.\n", iface, flags);
2230 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2233 /*****************************************************************************
2234 * IDirectDrawSurface7::BltBatch
2236 * An unimplemented function
2238 * Params:
2241 * Returns:
2242 * DDERR_UNSUPPORTED
2244 *****************************************************************************/
2245 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2247 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2249 /* MSDN: "not currently implemented" */
2250 return DDERR_UNSUPPORTED;
2253 static HRESULT WINAPI ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2255 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2256 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2258 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2261 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2263 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2264 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2266 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2269 static HRESULT WINAPI ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2271 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2272 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2274 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2277 static HRESULT WINAPI ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2279 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2280 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2282 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2285 /*****************************************************************************
2286 * IDirectDrawSurface7::EnumAttachedSurfaces
2288 * Enumerates all surfaces attached to this surface
2290 * Params:
2291 * context: Pointer to pass unmodified to the callback
2292 * cb: Callback function to call for each surface
2294 * Returns:
2295 * DD_OK on success
2296 * DDERR_INVALIDPARAMS if cb is NULL
2298 *****************************************************************************/
2299 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2300 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2302 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2303 IDirectDrawSurfaceImpl *surf;
2304 DDSURFACEDESC2 desc;
2305 int i;
2307 /* Attached surfaces aren't handled in WineD3D */
2308 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2310 if(!cb)
2311 return DDERR_INVALIDPARAMS;
2313 EnterCriticalSection(&ddraw_cs);
2314 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2316 surf = This->complex_array[i];
2317 if(!surf) break;
2319 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2320 desc = surf->surface_desc;
2321 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2322 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2324 LeaveCriticalSection(&ddraw_cs);
2325 return DD_OK;
2329 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
2331 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2332 desc = surf->surface_desc;
2333 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2334 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2336 LeaveCriticalSection(&ddraw_cs);
2337 return DD_OK;
2341 TRACE(" end of enumeration.\n");
2343 LeaveCriticalSection(&ddraw_cs);
2344 return DD_OK;
2347 struct callback_info2
2349 LPDDENUMSURFACESCALLBACK2 callback;
2350 void *context;
2353 struct callback_info
2355 LPDDENUMSURFACESCALLBACK callback;
2356 void *context;
2359 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2361 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
2362 const struct callback_info2 *info = context;
2364 ddraw_surface4_AddRef(&This->IDirectDrawSurface4_iface);
2365 ddraw_surface7_Release(surface);
2367 return info->callback(&This->IDirectDrawSurface4_iface, surface_desc, info->context);
2370 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2372 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
2373 const struct callback_info *info = context;
2375 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
2376 ddraw_surface7_Release(surface);
2378 /* FIXME: Check surface_test.dwSize */
2379 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2380 (DDSURFACEDESC *)surface_desc, info->context);
2383 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2384 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2386 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2387 struct callback_info2 info;
2389 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2391 info.callback = callback;
2392 info.context = context;
2394 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2395 &info, EnumCallback2);
2398 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2399 void *context, LPDDENUMSURFACESCALLBACK callback)
2401 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2402 struct callback_info info;
2404 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2406 info.callback = callback;
2407 info.context = context;
2409 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2410 &info, EnumCallback);
2413 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2414 void *context, LPDDENUMSURFACESCALLBACK callback)
2416 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2417 struct callback_info info;
2419 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2421 info.callback = callback;
2422 info.context = context;
2424 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2425 &info, EnumCallback);
2428 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2429 void *context, LPDDENUMSURFACESCALLBACK callback)
2431 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2432 struct callback_info info;
2434 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2436 info.callback = callback;
2437 info.context = context;
2439 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2440 &info, EnumCallback);
2443 /*****************************************************************************
2444 * IDirectDrawSurface7::EnumOverlayZOrders
2446 * "Enumerates the overlay surfaces on the specified destination"
2448 * Params:
2449 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2450 * context: context to pass back to the callback
2451 * cb: callback function to call for each enumerated surface
2453 * Returns:
2454 * DD_OK, because it's a stub
2456 *****************************************************************************/
2457 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2458 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2460 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2462 return DD_OK;
2465 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2466 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2468 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2469 struct callback_info2 info;
2471 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2473 info.callback = callback;
2474 info.context = context;
2476 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2477 flags, &info, EnumCallback2);
2480 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2481 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2483 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2484 struct callback_info info;
2486 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2488 info.callback = callback;
2489 info.context = context;
2491 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2492 flags, &info, EnumCallback);
2495 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2496 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2498 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2499 struct callback_info info;
2501 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2503 info.callback = callback;
2504 info.context = context;
2506 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2507 flags, &info, EnumCallback);
2510 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2511 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2513 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2514 struct callback_info info;
2516 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2518 info.callback = callback;
2519 info.context = context;
2521 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2522 flags, &info, EnumCallback);
2525 /*****************************************************************************
2526 * IDirectDrawSurface7::GetBltStatus
2528 * Returns the blitting status
2530 * Params:
2531 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2533 * Returns:
2534 * See IWineD3DSurface::Blt
2536 *****************************************************************************/
2537 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2539 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2540 HRESULT hr;
2542 TRACE("iface %p, flags %#x.\n", iface, Flags);
2544 EnterCriticalSection(&ddraw_cs);
2545 hr = wined3d_surface_get_blt_status(This->wined3d_surface, Flags);
2546 LeaveCriticalSection(&ddraw_cs);
2547 switch(hr)
2549 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2550 default: return hr;
2554 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2556 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2557 TRACE("iface %p, flags %#x.\n", iface, flags);
2559 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2562 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2564 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2565 TRACE("iface %p, flags %#x.\n", iface, flags);
2567 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2570 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2572 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2573 TRACE("iface %p, flags %#x.\n", iface, flags);
2575 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2578 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
2580 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2581 TRACE("iface %p, flags %#x.\n", iface, flags);
2583 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2586 /*****************************************************************************
2587 * IDirectDrawSurface7::GetColorKey
2589 * Returns the color key assigned to the surface
2591 * Params:
2592 * Flags: Some flags
2593 * CKey: Address to store the key to
2595 * Returns:
2596 * DD_OK on success
2597 * DDERR_INVALIDPARAMS if CKey is NULL
2599 *****************************************************************************/
2600 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2602 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2604 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2606 if(!CKey)
2607 return DDERR_INVALIDPARAMS;
2609 EnterCriticalSection(&ddraw_cs);
2611 switch (Flags)
2613 case DDCKEY_DESTBLT:
2614 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
2616 LeaveCriticalSection(&ddraw_cs);
2617 return DDERR_NOCOLORKEY;
2619 *CKey = This->surface_desc.ddckCKDestBlt;
2620 break;
2622 case DDCKEY_DESTOVERLAY:
2623 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
2625 LeaveCriticalSection(&ddraw_cs);
2626 return DDERR_NOCOLORKEY;
2628 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
2629 break;
2631 case DDCKEY_SRCBLT:
2632 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
2634 LeaveCriticalSection(&ddraw_cs);
2635 return DDERR_NOCOLORKEY;
2637 *CKey = This->surface_desc.ddckCKSrcBlt;
2638 break;
2640 case DDCKEY_SRCOVERLAY:
2641 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
2643 LeaveCriticalSection(&ddraw_cs);
2644 return DDERR_NOCOLORKEY;
2646 *CKey = This->surface_desc.ddckCKSrcOverlay;
2647 break;
2649 default:
2650 LeaveCriticalSection(&ddraw_cs);
2651 return DDERR_INVALIDPARAMS;
2654 LeaveCriticalSection(&ddraw_cs);
2655 return DD_OK;
2658 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
2660 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2661 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2663 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2666 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2668 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2669 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2671 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2674 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
2676 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2677 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2679 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2682 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
2684 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2685 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2687 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2690 /*****************************************************************************
2691 * IDirectDrawSurface7::GetFlipStatus
2693 * Returns the flipping status of the surface
2695 * Params:
2696 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
2698 * Returns:
2699 * See IWineD3DSurface::GetFlipStatus
2701 *****************************************************************************/
2702 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2704 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2705 HRESULT hr;
2707 TRACE("iface %p, flags %#x.\n", iface, Flags);
2709 EnterCriticalSection(&ddraw_cs);
2710 hr = wined3d_surface_get_flip_status(This->wined3d_surface, Flags);
2711 LeaveCriticalSection(&ddraw_cs);
2712 switch(hr)
2714 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2715 default: return hr;
2719 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
2721 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2722 TRACE("iface %p, flags %#x.\n", iface, flags);
2724 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2727 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
2729 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2730 TRACE("iface %p, flags %#x.\n", iface, flags);
2732 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2735 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
2737 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2738 TRACE("iface %p, flags %#x.\n", iface, flags);
2740 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2743 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
2745 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2746 TRACE("iface %p, flags %#x.\n", iface, flags);
2748 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2751 /*****************************************************************************
2752 * IDirectDrawSurface7::GetOverlayPosition
2754 * Returns the display coordinates of a visible and active overlay surface
2756 * Params:
2760 * Returns:
2761 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
2762 *****************************************************************************/
2763 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
2765 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2766 HRESULT hr;
2768 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
2770 EnterCriticalSection(&ddraw_cs);
2771 hr = wined3d_surface_get_overlay_position(This->wined3d_surface, X, Y);
2772 LeaveCriticalSection(&ddraw_cs);
2773 return hr;
2776 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
2778 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2779 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2781 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2784 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
2786 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2787 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2789 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2792 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
2794 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2795 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2797 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2800 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
2802 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2803 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2805 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2808 /*****************************************************************************
2809 * IDirectDrawSurface7::GetPixelFormat
2811 * Returns the pixel format of the Surface
2813 * Params:
2814 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
2815 * format should be written
2817 * Returns:
2818 * DD_OK on success
2819 * DDERR_INVALIDPARAMS if PixelFormat is NULL
2821 *****************************************************************************/
2822 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
2824 /* What is DDERR_INVALIDSURFACETYPE for here? */
2825 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2827 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
2829 if(!PixelFormat)
2830 return DDERR_INVALIDPARAMS;
2832 EnterCriticalSection(&ddraw_cs);
2833 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
2834 LeaveCriticalSection(&ddraw_cs);
2836 return DD_OK;
2839 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
2841 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2842 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2844 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2847 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
2849 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2850 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2852 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2855 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
2857 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2858 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2860 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2863 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
2865 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2866 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2868 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2871 /*****************************************************************************
2872 * IDirectDrawSurface7::GetSurfaceDesc
2874 * Returns the description of this surface
2876 * Params:
2877 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
2878 * surface desc
2880 * Returns:
2881 * DD_OK on success
2882 * DDERR_INVALIDPARAMS if DDSD is NULL
2884 *****************************************************************************/
2885 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
2887 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2889 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2891 if(!DDSD)
2892 return DDERR_INVALIDPARAMS;
2894 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
2896 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
2897 return DDERR_INVALIDPARAMS;
2900 EnterCriticalSection(&ddraw_cs);
2901 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
2902 TRACE("Returning surface desc:\n");
2903 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
2905 LeaveCriticalSection(&ddraw_cs);
2906 return DD_OK;
2909 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
2911 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2912 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2914 return ddraw_surface7_GetSurfaceDesc(&This->IDirectDrawSurface7_iface, DDSD);
2917 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
2919 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2921 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
2923 if (!surface_desc) return DDERR_INVALIDPARAMS;
2925 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
2927 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
2928 return DDERR_INVALIDPARAMS;
2931 EnterCriticalSection(&ddraw_cs);
2932 DDSD2_to_DDSD(&This->surface_desc, surface_desc);
2933 TRACE("Returning surface desc:\n");
2934 if (TRACE_ON(ddraw))
2936 /* DDRAW_dump_surface_desc handles the smaller size */
2937 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2940 LeaveCriticalSection(&ddraw_cs);
2941 return DD_OK;
2944 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
2946 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2947 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2949 return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
2952 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
2954 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2955 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2957 return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
2960 /*****************************************************************************
2961 * IDirectDrawSurface7::Initialize
2963 * Initializes the surface. This is a no-op in Wine
2965 * Params:
2966 * DD: Pointer to an DirectDraw interface
2967 * DDSD: Surface description for initialization
2969 * Returns:
2970 * DDERR_ALREADYINITIALIZED
2972 *****************************************************************************/
2973 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
2974 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
2976 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2978 return DDERR_ALREADYINITIALIZED;
2981 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
2982 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
2984 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2985 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2987 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2988 ddraw, surface_desc);
2991 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
2992 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2994 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2995 DDSURFACEDESC2 surface_desc2;
2996 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2998 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
2999 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
3000 ddraw, surface_desc ? &surface_desc2 : NULL);
3003 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3004 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3006 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3007 DDSURFACEDESC2 surface_desc2;
3008 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3010 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3011 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
3012 ddraw, surface_desc ? &surface_desc2 : NULL);
3015 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3016 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3018 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3019 DDSURFACEDESC2 surface_desc2;
3020 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3022 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3023 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
3024 ddraw, surface_desc ? &surface_desc2 : NULL);
3027 /*****************************************************************************
3028 * IDirect3DTexture1::Initialize
3030 * The sdk says it's not implemented
3032 * Params:
3035 * Returns
3036 * DDERR_UNSUPPORTED
3038 *****************************************************************************/
3039 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3040 IDirect3DDevice *device, IDirectDrawSurface *surface)
3042 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3044 return DDERR_UNSUPPORTED; /* Unchecked */
3047 /*****************************************************************************
3048 * IDirectDrawSurface7::IsLost
3050 * Checks if the surface is lost
3052 * Returns:
3053 * DD_OK, if the surface is usable
3054 * DDERR_ISLOST if the surface is lost
3055 * See IWineD3DSurface::IsLost for more details
3057 *****************************************************************************/
3058 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3060 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3061 HRESULT hr;
3063 TRACE("iface %p.\n", iface);
3065 EnterCriticalSection(&ddraw_cs);
3066 hr = wined3d_surface_is_lost(This->wined3d_surface);
3067 LeaveCriticalSection(&ddraw_cs);
3068 switch(hr)
3070 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
3071 * WineD3D uses the same error for surfaces
3073 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
3074 default: return hr;
3078 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3080 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3081 TRACE("iface %p.\n", iface);
3083 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3086 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3088 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3089 TRACE("iface %p.\n", iface);
3091 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3094 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3096 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3097 TRACE("iface %p.\n", iface);
3099 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3102 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3104 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3105 TRACE("iface %p.\n", iface);
3107 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3110 /*****************************************************************************
3111 * IDirectDrawSurface7::Restore
3113 * Restores a lost surface. This makes the surface usable again, but
3114 * doesn't reload its old contents
3116 * Returns:
3117 * DD_OK on success
3118 * See IWineD3DSurface::Restore for more details
3120 *****************************************************************************/
3121 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3123 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3124 HRESULT hr;
3126 TRACE("iface %p.\n", iface);
3128 EnterCriticalSection(&ddraw_cs);
3129 hr = wined3d_surface_restore(This->wined3d_surface);
3130 LeaveCriticalSection(&ddraw_cs);
3131 return hr;
3134 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3136 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3137 TRACE("iface %p.\n", iface);
3139 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3142 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3144 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3145 TRACE("iface %p.\n", iface);
3147 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3150 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3152 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3153 TRACE("iface %p.\n", iface);
3155 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3158 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3160 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3161 TRACE("iface %p.\n", iface);
3163 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3166 /*****************************************************************************
3167 * IDirectDrawSurface7::SetOverlayPosition
3169 * Changes the display coordinates of an overlay surface
3171 * Params:
3172 * X:
3173 * Y:
3175 * Returns:
3176 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3177 *****************************************************************************/
3178 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
3180 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3181 HRESULT hr;
3183 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
3185 EnterCriticalSection(&ddraw_cs);
3186 hr = wined3d_surface_set_overlay_position(This->wined3d_surface, X, Y);
3187 LeaveCriticalSection(&ddraw_cs);
3188 return hr;
3191 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3193 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3194 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3196 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3199 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3201 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3202 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3204 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3207 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3209 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3210 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3212 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3215 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3217 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3218 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3220 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3223 /*****************************************************************************
3224 * IDirectDrawSurface7::UpdateOverlay
3226 * Modifies the attributes of an overlay surface.
3228 * Params:
3229 * SrcRect: The section of the source being used for the overlay
3230 * DstSurface: Address of the surface that is overlaid
3231 * DstRect: Place of the overlay
3232 * Flags: some DDOVER_* flags
3234 * Returns:
3235 * DDERR_UNSUPPORTED, because we don't support overlays
3237 *****************************************************************************/
3238 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
3239 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
3241 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3242 IDirectDrawSurfaceImpl *Dst = unsafe_impl_from_IDirectDrawSurface7(DstSurface);
3243 HRESULT hr;
3245 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3246 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
3248 EnterCriticalSection(&ddraw_cs);
3249 hr = wined3d_surface_update_overlay(This->wined3d_surface, SrcRect,
3250 Dst ? Dst->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
3251 LeaveCriticalSection(&ddraw_cs);
3252 switch(hr) {
3253 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3254 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3255 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3256 default:
3257 return hr;
3261 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3262 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3264 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3265 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3266 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3267 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3269 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3270 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3273 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3274 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3276 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3277 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3278 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3279 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3281 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3282 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3285 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3286 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3288 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3289 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3290 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3291 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3293 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3294 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3297 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3298 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3300 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3301 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3302 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3303 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3305 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3306 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3309 /*****************************************************************************
3310 * IDirectDrawSurface7::UpdateOverlayDisplay
3312 * The DX7 sdk says that it's not implemented
3314 * Params:
3315 * Flags: ?
3317 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3319 *****************************************************************************/
3320 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3322 TRACE("iface %p, flags %#x.\n", iface, Flags);
3324 return DDERR_UNSUPPORTED;
3327 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3329 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3330 TRACE("iface %p, flags %#x.\n", iface, flags);
3332 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3335 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3337 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3338 TRACE("iface %p, flags %#x.\n", iface, flags);
3340 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3343 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3345 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3346 TRACE("iface %p, flags %#x.\n", iface, flags);
3348 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3351 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3353 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3354 TRACE("iface %p, flags %#x.\n", iface, flags);
3356 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3359 /*****************************************************************************
3360 * IDirectDrawSurface7::UpdateOverlayZOrder
3362 * Sets an overlay's Z order
3364 * Params:
3365 * Flags: DDOVERZ_* flags
3366 * DDSRef: Defines the relative position in the overlay chain
3368 * Returns:
3369 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3371 *****************************************************************************/
3372 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3373 DWORD Flags, IDirectDrawSurface7 *DDSRef)
3375 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3376 IDirectDrawSurfaceImpl *Ref = unsafe_impl_from_IDirectDrawSurface7(DDSRef);
3377 HRESULT hr;
3379 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
3381 EnterCriticalSection(&ddraw_cs);
3382 hr = wined3d_surface_update_overlay_z_order(This->wined3d_surface,
3383 Flags, Ref ? Ref->wined3d_surface : NULL);
3384 LeaveCriticalSection(&ddraw_cs);
3385 return hr;
3388 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3389 DWORD flags, IDirectDrawSurface4 *reference)
3391 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3392 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3393 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3395 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3396 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3399 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3400 DWORD flags, IDirectDrawSurface3 *reference)
3402 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3403 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3404 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3406 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3407 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3410 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3411 DWORD flags, IDirectDrawSurface2 *reference)
3413 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3414 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3415 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3417 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3418 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3421 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3422 DWORD flags, IDirectDrawSurface *reference)
3424 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3425 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3426 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3428 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3429 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3432 /*****************************************************************************
3433 * IDirectDrawSurface7::GetDDInterface
3435 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3436 * surface belongs to
3438 * Params:
3439 * DD: Address to write the interface pointer to
3441 * Returns:
3442 * DD_OK on success
3443 * DDERR_INVALIDPARAMS if DD is NULL
3445 *****************************************************************************/
3446 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3448 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3450 TRACE("iface %p, ddraw %p.\n", iface, DD);
3452 if(!DD)
3453 return DDERR_INVALIDPARAMS;
3455 switch(This->version)
3457 case 7:
3458 *DD = &This->ddraw->IDirectDraw7_iface;
3459 break;
3461 case 4:
3462 *DD = &This->ddraw->IDirectDraw4_iface;
3463 break;
3465 case 2:
3466 *DD = &This->ddraw->IDirectDraw2_iface;
3467 break;
3469 case 1:
3470 *DD = &This->ddraw->IDirectDraw_iface;
3471 break;
3474 IUnknown_AddRef((IUnknown *)*DD);
3476 return DD_OK;
3479 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3481 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3482 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3484 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3487 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3489 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3490 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3492 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3495 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3497 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3498 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3500 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3503 /* This seems also windows implementation specific - I don't think WineD3D needs this */
3504 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3506 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3507 volatile IDirectDrawSurfaceImpl* vThis = This;
3509 TRACE("iface %p.\n", iface);
3511 EnterCriticalSection(&ddraw_cs);
3512 /* A uniqueness value of 0 is apparently special.
3513 * This needs to be checked.
3514 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
3516 while (1) {
3517 DWORD old_uniqueness_value = vThis->uniqueness_value;
3518 DWORD new_uniqueness_value = old_uniqueness_value+1;
3520 if (old_uniqueness_value == 0) break;
3521 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
3523 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
3524 old_uniqueness_value,
3525 new_uniqueness_value)
3526 == old_uniqueness_value)
3527 break;
3530 LeaveCriticalSection(&ddraw_cs);
3531 return DD_OK;
3534 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
3536 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3537 TRACE("iface %p.\n", iface);
3539 return ddraw_surface7_ChangeUniquenessValue(&This->IDirectDrawSurface7_iface);
3542 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
3544 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3546 TRACE("iface %p, value %p.\n", iface, pValue);
3548 EnterCriticalSection(&ddraw_cs);
3549 *pValue = This->uniqueness_value;
3550 LeaveCriticalSection(&ddraw_cs);
3551 return DD_OK;
3554 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
3556 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3557 TRACE("iface %p, value %p.\n", iface, pValue);
3559 return ddraw_surface7_GetUniquenessValue(&This->IDirectDrawSurface7_iface, pValue);
3562 /*****************************************************************************
3563 * IDirectDrawSurface7::SetLOD
3565 * Sets the level of detail of a texture
3567 * Params:
3568 * MaxLOD: LOD to set
3570 * Returns:
3571 * DD_OK on success
3572 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3574 *****************************************************************************/
3575 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
3577 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3578 HRESULT hr;
3580 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
3582 EnterCriticalSection(&ddraw_cs);
3583 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3585 LeaveCriticalSection(&ddraw_cs);
3586 return DDERR_INVALIDOBJECT;
3589 if (!This->wined3d_texture)
3591 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
3592 LeaveCriticalSection(&ddraw_cs);
3593 return DDERR_INVALIDOBJECT;
3596 hr = wined3d_texture_set_lod(This->wined3d_texture, MaxLOD);
3597 LeaveCriticalSection(&ddraw_cs);
3598 return hr;
3601 /*****************************************************************************
3602 * IDirectDrawSurface7::GetLOD
3604 * Returns the level of detail of a Direct3D texture
3606 * Params:
3607 * MaxLOD: Address to write the LOD to
3609 * Returns:
3610 * DD_OK on success
3611 * DDERR_INVALIDPARAMS if MaxLOD is NULL
3612 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3614 *****************************************************************************/
3615 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
3617 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3619 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
3621 if(!MaxLOD)
3622 return DDERR_INVALIDPARAMS;
3624 EnterCriticalSection(&ddraw_cs);
3625 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3627 LeaveCriticalSection(&ddraw_cs);
3628 return DDERR_INVALIDOBJECT;
3631 *MaxLOD = wined3d_texture_get_lod(This->wined3d_texture);
3632 LeaveCriticalSection(&ddraw_cs);
3633 return DD_OK;
3636 /*****************************************************************************
3637 * IDirectDrawSurface7::BltFast
3639 * Performs a fast Blit.
3641 * Params:
3642 * dstx: The x coordinate to blit to on the destination
3643 * dsty: The y coordinate to blit to on the destination
3644 * Source: The source surface
3645 * rsrc: The source rectangle
3646 * trans: Type of transfer. Some DDBLTFAST_* flags
3648 * Returns:
3649 * DD_OK on success
3650 * For more details, see IWineD3DSurface::BltFast
3652 *****************************************************************************/
3653 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
3654 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
3656 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3657 IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface7(Source);
3658 DWORD src_w, src_h, dst_w, dst_h;
3659 HRESULT hr;
3661 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3662 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
3664 dst_w = This->surface_desc.dwWidth;
3665 dst_h = This->surface_desc.dwHeight;
3667 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
3668 * in that case
3670 if(rsrc)
3672 src_w = rsrc->right - rsrc->left;
3673 src_h = rsrc->bottom - rsrc->top;
3675 else
3677 src_w = src->surface_desc.dwWidth;
3678 src_h = src->surface_desc.dwHeight;
3681 if (src_w > dst_w || dstx > dst_w - src_w
3682 || src_h > dst_h || dsty > dst_h - src_h)
3684 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
3685 return DDERR_INVALIDRECT;
3688 EnterCriticalSection(&ddraw_cs);
3689 hr = wined3d_surface_bltfast(This->wined3d_surface, dstx, dsty,
3690 src->wined3d_surface, rsrc, trans);
3691 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER))
3692 hr = ddraw_surface_update_frontbuffer(This);
3693 LeaveCriticalSection(&ddraw_cs);
3694 switch(hr)
3696 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
3697 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
3698 default: return hr;
3702 static HRESULT WINAPI ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
3703 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
3705 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3706 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
3707 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3708 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3710 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3711 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3714 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
3715 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
3717 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3718 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
3719 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3720 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3722 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3723 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3726 static HRESULT WINAPI ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
3727 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
3729 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3730 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
3731 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3732 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3734 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3735 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3738 static HRESULT WINAPI ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
3739 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
3741 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3742 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
3743 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3744 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3746 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3747 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3750 /*****************************************************************************
3751 * IDirectDrawSurface7::GetClipper
3753 * Returns the IDirectDrawClipper interface of the clipper assigned to this
3754 * surface
3756 * Params:
3757 * Clipper: Address to store the interface pointer at
3759 * Returns:
3760 * DD_OK on success
3761 * DDERR_INVALIDPARAMS if Clipper is NULL
3762 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
3764 *****************************************************************************/
3765 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
3767 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3769 TRACE("iface %p, clipper %p.\n", iface, Clipper);
3771 if(!Clipper)
3773 LeaveCriticalSection(&ddraw_cs);
3774 return DDERR_INVALIDPARAMS;
3777 EnterCriticalSection(&ddraw_cs);
3778 if(This->clipper == NULL)
3780 LeaveCriticalSection(&ddraw_cs);
3781 return DDERR_NOCLIPPERATTACHED;
3784 *Clipper = (IDirectDrawClipper *)This->clipper;
3785 IDirectDrawClipper_AddRef(*Clipper);
3786 LeaveCriticalSection(&ddraw_cs);
3787 return DD_OK;
3790 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
3792 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3793 TRACE("iface %p, clipper %p.\n", iface, clipper);
3795 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3798 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
3800 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3801 TRACE("iface %p, clipper %p.\n", iface, clipper);
3803 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3806 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
3808 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3809 TRACE("iface %p, clipper %p.\n", iface, clipper);
3811 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3814 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
3816 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3817 TRACE("iface %p, clipper %p.\n", iface, clipper);
3819 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3822 /*****************************************************************************
3823 * IDirectDrawSurface7::SetClipper
3825 * Sets a clipper for the surface
3827 * Params:
3828 * Clipper: IDirectDrawClipper interface of the clipper to set
3830 * Returns:
3831 * DD_OK on success
3833 *****************************************************************************/
3834 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
3835 IDirectDrawClipper *iclipper)
3837 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3838 IDirectDrawClipperImpl *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
3839 IDirectDrawClipperImpl *oldClipper = This->clipper;
3840 HWND clipWindow;
3841 HRESULT hr;
3843 TRACE("iface %p, clipper %p.\n", iface, iclipper);
3845 EnterCriticalSection(&ddraw_cs);
3846 if (clipper == This->clipper)
3848 LeaveCriticalSection(&ddraw_cs);
3849 return DD_OK;
3852 This->clipper = clipper;
3854 if (clipper != NULL)
3855 IDirectDrawClipper_AddRef(iclipper);
3856 if(oldClipper)
3857 IDirectDrawClipper_Release(&oldClipper->IDirectDrawClipper_iface);
3859 hr = wined3d_surface_set_clipper(This->wined3d_surface,
3860 This->clipper ? This->clipper->wineD3DClipper : NULL);
3862 if (This->wined3d_swapchain)
3864 clipWindow = NULL;
3865 if(clipper) {
3866 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
3869 if (clipWindow)
3870 wined3d_swapchain_set_window(This->wined3d_swapchain, clipWindow);
3871 else
3872 wined3d_swapchain_set_window(This->wined3d_swapchain, This->ddraw->d3d_window);
3875 LeaveCriticalSection(&ddraw_cs);
3876 return hr;
3879 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
3881 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3882 TRACE("iface %p, clipper %p.\n", iface, clipper);
3884 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3887 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
3889 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3890 TRACE("iface %p, clipper %p.\n", iface, clipper);
3892 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3895 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
3897 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3898 TRACE("iface %p, clipper %p.\n", iface, clipper);
3900 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3903 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
3905 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3906 TRACE("iface %p, clipper %p.\n", iface, clipper);
3908 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3911 /*****************************************************************************
3912 * IDirectDrawSurface7::SetSurfaceDesc
3914 * Sets the surface description. It can override the pixel format, the surface
3915 * memory, ...
3916 * It's not really tested.
3918 * Params:
3919 * DDSD: Pointer to the new surface description to set
3920 * Flags: Some flags
3922 * Returns:
3923 * DD_OK on success
3924 * DDERR_INVALIDPARAMS if DDSD is NULL
3926 *****************************************************************************/
3927 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
3929 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3930 enum wined3d_format_id newFormat = WINED3DFMT_UNKNOWN;
3931 HRESULT hr;
3933 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
3935 if (!DDSD)
3937 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
3938 return DDERR_INVALIDPARAMS;
3940 if (Flags)
3942 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
3943 return DDERR_INVALIDPARAMS;
3946 EnterCriticalSection(&ddraw_cs);
3947 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
3949 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
3951 if(newFormat == WINED3DFMT_UNKNOWN)
3953 ERR("Requested to set an unknown pixelformat\n");
3954 LeaveCriticalSection(&ddraw_cs);
3955 return DDERR_INVALIDPARAMS;
3957 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
3959 hr = wined3d_surface_set_format(This->wined3d_surface, newFormat);
3960 if (FAILED(hr))
3962 LeaveCriticalSection(&ddraw_cs);
3963 return hr;
3967 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
3969 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTOVERLAY,
3970 (WINEDDCOLORKEY *)&DDSD->u3.ddckCKDestOverlay);
3972 if (DDSD->dwFlags & DDSD_CKDESTBLT)
3974 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTBLT,
3975 (WINEDDCOLORKEY *)&DDSD->ddckCKDestBlt);
3977 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
3979 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCOVERLAY,
3980 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcOverlay);
3982 if (DDSD->dwFlags & DDSD_CKSRCBLT)
3984 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCBLT,
3985 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcBlt);
3987 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
3989 hr = wined3d_surface_set_mem(This->wined3d_surface, DDSD->lpSurface);
3990 if (FAILED(hr))
3992 /* No need for a trace here, wined3d does that for us */
3993 switch(hr)
3995 case WINED3DERR_INVALIDCALL:
3996 LeaveCriticalSection(&ddraw_cs);
3997 return DDERR_INVALIDPARAMS;
3998 default:
3999 break; /* Go on */
4004 This->surface_desc = *DDSD;
4006 LeaveCriticalSection(&ddraw_cs);
4007 return DD_OK;
4010 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4011 DDSURFACEDESC2 *surface_desc, DWORD flags)
4013 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4014 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4016 return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
4017 surface_desc, flags);
4020 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4021 DDSURFACEDESC *surface_desc, DWORD flags)
4023 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4024 DDSURFACEDESC2 surface_desc2;
4025 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4027 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4028 return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
4029 surface_desc ? &surface_desc2 : NULL, flags);
4032 /*****************************************************************************
4033 * IDirectDrawSurface7::GetPalette
4035 * Returns the IDirectDrawPalette interface of the palette currently assigned
4036 * to the surface
4038 * Params:
4039 * Pal: Address to write the interface pointer to
4041 * Returns:
4042 * DD_OK on success
4043 * DDERR_INVALIDPARAMS if Pal is NULL
4045 *****************************************************************************/
4046 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
4048 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4049 struct wined3d_palette *wined3d_palette;
4050 HRESULT hr = DD_OK;
4052 TRACE("iface %p, palette %p.\n", iface, Pal);
4054 if(!Pal)
4055 return DDERR_INVALIDPARAMS;
4057 EnterCriticalSection(&ddraw_cs);
4058 wined3d_palette = wined3d_surface_get_palette(This->wined3d_surface);
4059 if (wined3d_palette)
4061 *Pal = wined3d_palette_get_parent(wined3d_palette);
4062 IDirectDrawPalette_AddRef(*Pal);
4064 else
4066 *Pal = NULL;
4067 hr = DDERR_NOPALETTEATTACHED;
4070 LeaveCriticalSection(&ddraw_cs);
4071 return hr;
4074 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4076 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4077 TRACE("iface %p, palette %p.\n", iface, palette);
4079 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4082 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4084 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4085 TRACE("iface %p, palette %p.\n", iface, palette);
4087 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4090 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4092 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4093 TRACE("iface %p, palette %p.\n", iface, palette);
4095 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4098 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4100 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4101 TRACE("iface %p, palette %p.\n", iface, palette);
4103 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4106 /*****************************************************************************
4107 * SetColorKeyEnum
4109 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
4110 * recursively in the surface tree
4112 *****************************************************************************/
4113 struct SCKContext
4115 HRESULT ret;
4116 WINEDDCOLORKEY *CKey;
4117 DWORD Flags;
4120 static HRESULT WINAPI
4121 SetColorKeyEnum(IDirectDrawSurface7 *surface,
4122 DDSURFACEDESC2 *desc,
4123 void *context)
4125 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
4126 struct SCKContext *ctx = context;
4127 HRESULT hr;
4129 hr = wined3d_surface_set_color_key(This->wined3d_surface, ctx->Flags, ctx->CKey);
4130 if (FAILED(hr))
4132 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
4133 ctx->ret = hr;
4136 ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
4137 ddraw_surface7_Release(surface);
4139 return DDENUMRET_OK;
4142 /*****************************************************************************
4143 * IDirectDrawSurface7::SetColorKey
4145 * Sets the color keying options for the surface. Observations showed that
4146 * in case of complex surfaces the color key has to be assigned to all
4147 * sublevels.
4149 * Params:
4150 * Flags: DDCKEY_*
4151 * CKey: The new color key
4153 * Returns:
4154 * DD_OK on success
4155 * See IWineD3DSurface::SetColorKey for details
4157 *****************************************************************************/
4158 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
4160 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4161 DDCOLORKEY FixedCKey;
4162 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
4164 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
4166 EnterCriticalSection(&ddraw_cs);
4167 if (CKey)
4169 FixedCKey = *CKey;
4170 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
4171 if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
4172 FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
4174 switch (Flags & ~DDCKEY_COLORSPACE)
4176 case DDCKEY_DESTBLT:
4177 This->surface_desc.ddckCKDestBlt = FixedCKey;
4178 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4179 break;
4181 case DDCKEY_DESTOVERLAY:
4182 This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
4183 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4184 break;
4186 case DDCKEY_SRCOVERLAY:
4187 This->surface_desc.ddckCKSrcOverlay = FixedCKey;
4188 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4189 break;
4191 case DDCKEY_SRCBLT:
4192 This->surface_desc.ddckCKSrcBlt = FixedCKey;
4193 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4194 break;
4196 default:
4197 LeaveCriticalSection(&ddraw_cs);
4198 return DDERR_INVALIDPARAMS;
4201 else
4203 switch (Flags & ~DDCKEY_COLORSPACE)
4205 case DDCKEY_DESTBLT:
4206 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4207 break;
4209 case DDCKEY_DESTOVERLAY:
4210 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4211 break;
4213 case DDCKEY_SRCOVERLAY:
4214 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4215 break;
4217 case DDCKEY_SRCBLT:
4218 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4219 break;
4221 default:
4222 LeaveCriticalSection(&ddraw_cs);
4223 return DDERR_INVALIDPARAMS;
4226 ctx.ret = wined3d_surface_set_color_key(This->wined3d_surface, Flags, ctx.CKey);
4227 ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
4228 LeaveCriticalSection(&ddraw_cs);
4229 switch(ctx.ret)
4231 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
4232 default: return ctx.ret;
4236 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4238 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4239 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4241 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4244 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4246 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4247 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4249 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4252 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4254 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4255 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4257 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4260 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4262 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4263 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4265 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4268 /*****************************************************************************
4269 * IDirectDrawSurface7::SetPalette
4271 * Assigns a DirectDrawPalette object to the surface
4273 * Params:
4274 * Pal: Interface to the palette to set
4276 * Returns:
4277 * DD_OK on success
4279 *****************************************************************************/
4280 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
4282 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4283 IDirectDrawPalette *oldPal;
4284 IDirectDrawSurfaceImpl *surf;
4285 IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
4286 HRESULT hr;
4288 TRACE("iface %p, palette %p.\n", iface, Pal);
4290 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
4291 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
4292 return DDERR_INVALIDPIXELFORMAT;
4295 if (This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4297 return DDERR_NOTONMIPMAPSUBLEVEL;
4300 /* Find the old palette */
4301 EnterCriticalSection(&ddraw_cs);
4302 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
4303 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
4305 LeaveCriticalSection(&ddraw_cs);
4306 return hr;
4308 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
4310 /* Set the new Palette */
4311 wined3d_surface_set_palette(This->wined3d_surface, PalImpl ? PalImpl->wineD3DPalette : NULL);
4312 /* AddRef the Palette */
4313 if(Pal) IDirectDrawPalette_AddRef(Pal);
4315 /* Release the old palette */
4316 if(oldPal) IDirectDrawPalette_Release(oldPal);
4318 /* Update the wined3d frontbuffer if this is the frontbuffer. */
4319 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER) && This->ddraw->wined3d_frontbuffer)
4321 hr = wined3d_surface_set_palette(This->ddraw->wined3d_frontbuffer, PalImpl ? PalImpl->wineD3DPalette : NULL);
4322 if (FAILED(hr))
4323 ERR("Failed to set frontbuffer palette, hr %#x.\n", hr);
4326 /* If this is a front buffer, also update the back buffers
4327 * TODO: How do things work for palettized cube textures?
4329 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
4331 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
4332 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
4334 surf = This;
4335 while(1)
4337 IDirectDrawSurface7 *attach;
4338 HRESULT hr;
4339 hr = ddraw_surface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &caps2, &attach);
4340 if(hr != DD_OK)
4342 break;
4345 TRACE("Setting palette on %p\n", attach);
4346 ddraw_surface7_SetPalette(attach, Pal);
4347 surf = impl_from_IDirectDrawSurface7(attach);
4348 ddraw_surface7_Release(attach);
4352 LeaveCriticalSection(&ddraw_cs);
4353 return DD_OK;
4356 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4358 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4359 TRACE("iface %p, palette %p.\n", iface, palette);
4361 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4364 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4366 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4367 TRACE("iface %p, palette %p.\n", iface, palette);
4369 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4372 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4374 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4375 TRACE("iface %p, palette %p.\n", iface, palette);
4377 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4380 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4382 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4383 TRACE("iface %p, palette %p.\n", iface, palette);
4385 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4388 /**********************************************************
4389 * IDirectDrawGammaControl::GetGammaRamp
4391 * Returns the current gamma ramp for a surface
4393 * Params:
4394 * flags: Ignored
4395 * gamma_ramp: Address to write the ramp to
4397 * Returns:
4398 * DD_OK on success
4399 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4401 **********************************************************/
4402 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4403 DWORD flags, DDGAMMARAMP *gamma_ramp)
4405 IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4407 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4409 if (!gamma_ramp)
4411 WARN("Invalid gamma_ramp passed.\n");
4412 return DDERR_INVALIDPARAMS;
4415 EnterCriticalSection(&ddraw_cs);
4416 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4418 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP. */
4419 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (WINED3DGAMMARAMP *)gamma_ramp);
4421 else
4423 ERR("Not implemented for non-primary surfaces.\n");
4425 LeaveCriticalSection(&ddraw_cs);
4427 return DD_OK;
4430 /**********************************************************
4431 * IDirectDrawGammaControl::SetGammaRamp
4433 * Sets the red, green and blue gamma ramps for
4435 * Params:
4436 * flags: Can be DDSGR_CALIBRATE to request calibration
4437 * gamma_ramp: Structure containing the new gamma ramp
4439 * Returns:
4440 * DD_OK on success
4441 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4443 **********************************************************/
4444 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4445 DWORD flags, DDGAMMARAMP *gamma_ramp)
4447 IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4449 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4451 if (!gamma_ramp)
4453 WARN("Invalid gamma_ramp passed.\n");
4454 return DDERR_INVALIDPARAMS;
4457 EnterCriticalSection(&ddraw_cs);
4458 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4460 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP */
4461 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device, 0, flags, (WINED3DGAMMARAMP *)gamma_ramp);
4463 else
4465 ERR("Not implemented for non-primary surfaces.\n");
4467 LeaveCriticalSection(&ddraw_cs);
4469 return DD_OK;
4472 /*****************************************************************************
4473 * IDirect3DTexture2::PaletteChanged
4475 * Informs the texture about a palette change
4477 * Params:
4478 * start: Start index of the change
4479 * count: The number of changed entries
4481 * Returns
4482 * D3D_OK, because it's a stub
4484 *****************************************************************************/
4485 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4487 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4489 return D3D_OK;
4492 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4494 IDirectDrawSurfaceImpl *surface = impl_from_IDirect3DTexture(iface);
4496 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4498 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
4501 /*****************************************************************************
4502 * IDirect3DTexture::Unload
4504 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4507 * Returns:
4508 * DDERR_UNSUPPORTED
4510 *****************************************************************************/
4511 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4513 WARN("iface %p. Not implemented.\n", iface);
4515 return DDERR_UNSUPPORTED;
4518 /*****************************************************************************
4519 * IDirect3DTexture2::GetHandle
4521 * Returns handle for the texture. At the moment, the interface
4522 * to the IWineD3DTexture is used.
4524 * Params:
4525 * device: Device this handle is assigned to
4526 * handle: Address to store the handle at.
4528 * Returns:
4529 * D3D_OK
4531 *****************************************************************************/
4532 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4533 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4535 IDirectDrawSurfaceImpl *surface = impl_from_IDirect3DTexture2(iface);
4537 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4539 EnterCriticalSection(&ddraw_cs);
4541 if (!surface->Handle)
4543 DWORD h = ddraw_allocate_handle(&device_from_device2(device)->handle_table, surface, DDRAW_HANDLE_SURFACE);
4544 if (h == DDRAW_INVALID_HANDLE)
4546 ERR("Failed to allocate a texture handle.\n");
4547 LeaveCriticalSection(&ddraw_cs);
4548 return DDERR_OUTOFMEMORY;
4551 surface->Handle = h + 1;
4554 TRACE("Returning handle %08x.\n", surface->Handle);
4555 *handle = surface->Handle;
4557 LeaveCriticalSection(&ddraw_cs);
4559 return D3D_OK;
4562 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
4563 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
4565 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture(iface);
4566 IDirect3DDevice2 *device2 = (IDirect3DDevice2 *)&device_from_device1(device)->IDirect3DDevice2_vtbl;
4568 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4570 return d3d_texture2_GetHandle(&This->IDirect3DTexture2_iface, device2, handle);
4573 /*****************************************************************************
4574 * get_sub_mimaplevel
4576 * Helper function that returns the next mipmap level
4578 * tex_ptr: Surface of which to return the next level
4580 *****************************************************************************/
4581 static IDirectDrawSurfaceImpl *get_sub_mimaplevel(IDirectDrawSurfaceImpl *surface)
4583 /* Now go down the mipmap chain to the next surface */
4584 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
4585 IDirectDrawSurface7 *next_level;
4586 HRESULT hr;
4588 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
4589 if (FAILED(hr)) return NULL;
4591 ddraw_surface7_Release(next_level);
4593 return impl_from_IDirectDrawSurface7(next_level);
4596 /*****************************************************************************
4597 * IDirect3DTexture2::Load
4599 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
4601 * This function isn't relayed to WineD3D because the whole interface is
4602 * implemented in DDraw only. For speed improvements a implementation which
4603 * takes OpenGL more into account could be placed into WineD3D.
4605 * Params:
4606 * src_texture: Address of the texture to load
4608 * Returns:
4609 * D3D_OK on success
4610 * D3DERR_TEXTURE_LOAD_FAILED.
4612 *****************************************************************************/
4613 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
4615 IDirectDrawSurfaceImpl *dst_surface = impl_from_IDirect3DTexture2(iface);
4616 IDirectDrawSurfaceImpl *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
4617 HRESULT hr;
4619 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4621 if (src_surface == dst_surface)
4623 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
4624 return D3D_OK;
4627 EnterCriticalSection(&ddraw_cs);
4629 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4630 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
4631 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
4633 ERR("Trying to load surfaces with different mip-map counts.\n");
4636 for (;;)
4638 struct wined3d_palette *wined3d_dst_pal, *wined3d_src_pal;
4639 IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
4640 DDSURFACEDESC *src_desc, *dst_desc;
4642 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
4643 src_surface, dst_surface, src_surface->mipmap_level);
4645 /* Suppress the ALLOCONLOAD flag */
4646 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
4648 /* Get the palettes */
4649 wined3d_dst_pal = wined3d_surface_get_palette(dst_surface->wined3d_surface);
4650 if (wined3d_dst_pal)
4651 dst_pal = wined3d_palette_get_parent(wined3d_dst_pal);
4653 wined3d_src_pal = wined3d_surface_get_palette(src_surface->wined3d_surface);
4654 if (wined3d_src_pal)
4655 src_pal = wined3d_palette_get_parent(wined3d_src_pal);
4657 if (src_pal)
4659 PALETTEENTRY palent[256];
4661 if (!dst_pal)
4663 LeaveCriticalSection(&ddraw_cs);
4664 return DDERR_NOPALETTEATTACHED;
4666 IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
4667 IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
4670 /* Copy one surface on the other */
4671 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
4672 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
4674 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
4676 /* Should also check for same pixel format, u1.lPitch, ... */
4677 ERR("Error in surface sizes.\n");
4678 LeaveCriticalSection(&ddraw_cs);
4679 return D3DERR_TEXTURE_LOAD_FAILED;
4681 else
4683 WINED3DLOCKED_RECT src_rect, dst_rect;
4685 /* Copy also the ColorKeying stuff */
4686 if (src_desc->dwFlags & DDSD_CKSRCBLT)
4688 dst_desc->dwFlags |= DDSD_CKSRCBLT;
4689 dst_desc->ddckCKSrcBlt.dwColorSpaceLowValue = src_desc->ddckCKSrcBlt.dwColorSpaceLowValue;
4690 dst_desc->ddckCKSrcBlt.dwColorSpaceHighValue = src_desc->ddckCKSrcBlt.dwColorSpaceHighValue;
4693 /* Copy the main memory texture into the surface that corresponds
4694 * to the OpenGL texture object. */
4696 hr = wined3d_surface_map(src_surface->wined3d_surface, &src_rect, NULL, 0);
4697 if (FAILED(hr))
4699 ERR("Failed to lock source surface, hr %#x.\n", hr);
4700 LeaveCriticalSection(&ddraw_cs);
4701 return D3DERR_TEXTURE_LOAD_FAILED;
4704 hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_rect, NULL, 0);
4705 if (FAILED(hr))
4707 ERR("Failed to lock destination surface, hr %#x.\n", hr);
4708 wined3d_surface_unmap(src_surface->wined3d_surface);
4709 LeaveCriticalSection(&ddraw_cs);
4710 return D3DERR_TEXTURE_LOAD_FAILED;
4713 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
4714 memcpy(dst_rect.pBits, src_rect.pBits, src_surface->surface_desc.u1.dwLinearSize);
4715 else
4716 memcpy(dst_rect.pBits, src_rect.pBits, src_rect.Pitch * src_desc->dwHeight);
4718 wined3d_surface_unmap(src_surface->wined3d_surface);
4719 wined3d_surface_unmap(dst_surface->wined3d_surface);
4722 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4723 src_surface = get_sub_mimaplevel(src_surface);
4724 else
4725 src_surface = NULL;
4727 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4728 dst_surface = get_sub_mimaplevel(dst_surface);
4729 else
4730 dst_surface = NULL;
4732 if (!src_surface || !dst_surface)
4734 if (src_surface != dst_surface)
4735 ERR("Loading surface with different mipmap structure.\n");
4736 break;
4740 LeaveCriticalSection(&ddraw_cs);
4742 return hr;
4745 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
4747 IDirectDrawSurfaceImpl* This = impl_from_IDirect3DTexture(iface);
4748 IDirectDrawSurfaceImpl* src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
4749 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4751 return d3d_texture2_Load(&This->IDirect3DTexture2_iface,
4752 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
4755 /*****************************************************************************
4756 * The VTable
4757 *****************************************************************************/
4759 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
4761 /* IUnknown */
4762 ddraw_surface7_QueryInterface,
4763 ddraw_surface7_AddRef,
4764 ddraw_surface7_Release,
4765 /* IDirectDrawSurface */
4766 ddraw_surface7_AddAttachedSurface,
4767 ddraw_surface7_AddOverlayDirtyRect,
4768 ddraw_surface7_Blt,
4769 ddraw_surface7_BltBatch,
4770 ddraw_surface7_BltFast,
4771 ddraw_surface7_DeleteAttachedSurface,
4772 ddraw_surface7_EnumAttachedSurfaces,
4773 ddraw_surface7_EnumOverlayZOrders,
4774 ddraw_surface7_Flip,
4775 ddraw_surface7_GetAttachedSurface,
4776 ddraw_surface7_GetBltStatus,
4777 ddraw_surface7_GetCaps,
4778 ddraw_surface7_GetClipper,
4779 ddraw_surface7_GetColorKey,
4780 ddraw_surface7_GetDC,
4781 ddraw_surface7_GetFlipStatus,
4782 ddraw_surface7_GetOverlayPosition,
4783 ddraw_surface7_GetPalette,
4784 ddraw_surface7_GetPixelFormat,
4785 ddraw_surface7_GetSurfaceDesc,
4786 ddraw_surface7_Initialize,
4787 ddraw_surface7_IsLost,
4788 ddraw_surface7_Lock,
4789 ddraw_surface7_ReleaseDC,
4790 ddraw_surface7_Restore,
4791 ddraw_surface7_SetClipper,
4792 ddraw_surface7_SetColorKey,
4793 ddraw_surface7_SetOverlayPosition,
4794 ddraw_surface7_SetPalette,
4795 ddraw_surface7_Unlock,
4796 ddraw_surface7_UpdateOverlay,
4797 ddraw_surface7_UpdateOverlayDisplay,
4798 ddraw_surface7_UpdateOverlayZOrder,
4799 /* IDirectDrawSurface2 */
4800 ddraw_surface7_GetDDInterface,
4801 ddraw_surface7_PageLock,
4802 ddraw_surface7_PageUnlock,
4803 /* IDirectDrawSurface3 */
4804 ddraw_surface7_SetSurfaceDesc,
4805 /* IDirectDrawSurface4 */
4806 ddraw_surface7_SetPrivateData,
4807 ddraw_surface7_GetPrivateData,
4808 ddraw_surface7_FreePrivateData,
4809 ddraw_surface7_GetUniquenessValue,
4810 ddraw_surface7_ChangeUniquenessValue,
4811 /* IDirectDrawSurface7 */
4812 ddraw_surface7_SetPriority,
4813 ddraw_surface7_GetPriority,
4814 ddraw_surface7_SetLOD,
4815 ddraw_surface7_GetLOD,
4818 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
4820 /* IUnknown */
4821 ddraw_surface4_QueryInterface,
4822 ddraw_surface4_AddRef,
4823 ddraw_surface4_Release,
4824 /* IDirectDrawSurface */
4825 ddraw_surface4_AddAttachedSurface,
4826 ddraw_surface4_AddOverlayDirtyRect,
4827 ddraw_surface4_Blt,
4828 ddraw_surface4_BltBatch,
4829 ddraw_surface4_BltFast,
4830 ddraw_surface4_DeleteAttachedSurface,
4831 ddraw_surface4_EnumAttachedSurfaces,
4832 ddraw_surface4_EnumOverlayZOrders,
4833 ddraw_surface4_Flip,
4834 ddraw_surface4_GetAttachedSurface,
4835 ddraw_surface4_GetBltStatus,
4836 ddraw_surface4_GetCaps,
4837 ddraw_surface4_GetClipper,
4838 ddraw_surface4_GetColorKey,
4839 ddraw_surface4_GetDC,
4840 ddraw_surface4_GetFlipStatus,
4841 ddraw_surface4_GetOverlayPosition,
4842 ddraw_surface4_GetPalette,
4843 ddraw_surface4_GetPixelFormat,
4844 ddraw_surface4_GetSurfaceDesc,
4845 ddraw_surface4_Initialize,
4846 ddraw_surface4_IsLost,
4847 ddraw_surface4_Lock,
4848 ddraw_surface4_ReleaseDC,
4849 ddraw_surface4_Restore,
4850 ddraw_surface4_SetClipper,
4851 ddraw_surface4_SetColorKey,
4852 ddraw_surface4_SetOverlayPosition,
4853 ddraw_surface4_SetPalette,
4854 ddraw_surface4_Unlock,
4855 ddraw_surface4_UpdateOverlay,
4856 ddraw_surface4_UpdateOverlayDisplay,
4857 ddraw_surface4_UpdateOverlayZOrder,
4858 /* IDirectDrawSurface2 */
4859 ddraw_surface4_GetDDInterface,
4860 ddraw_surface4_PageLock,
4861 ddraw_surface4_PageUnlock,
4862 /* IDirectDrawSurface3 */
4863 ddraw_surface4_SetSurfaceDesc,
4864 /* IDirectDrawSurface4 */
4865 ddraw_surface4_SetPrivateData,
4866 ddraw_surface4_GetPrivateData,
4867 ddraw_surface4_FreePrivateData,
4868 ddraw_surface4_GetUniquenessValue,
4869 ddraw_surface4_ChangeUniquenessValue,
4872 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
4874 /* IUnknown */
4875 ddraw_surface3_QueryInterface,
4876 ddraw_surface3_AddRef,
4877 ddraw_surface3_Release,
4878 /* IDirectDrawSurface */
4879 ddraw_surface3_AddAttachedSurface,
4880 ddraw_surface3_AddOverlayDirtyRect,
4881 ddraw_surface3_Blt,
4882 ddraw_surface3_BltBatch,
4883 ddraw_surface3_BltFast,
4884 ddraw_surface3_DeleteAttachedSurface,
4885 ddraw_surface3_EnumAttachedSurfaces,
4886 ddraw_surface3_EnumOverlayZOrders,
4887 ddraw_surface3_Flip,
4888 ddraw_surface3_GetAttachedSurface,
4889 ddraw_surface3_GetBltStatus,
4890 ddraw_surface3_GetCaps,
4891 ddraw_surface3_GetClipper,
4892 ddraw_surface3_GetColorKey,
4893 ddraw_surface3_GetDC,
4894 ddraw_surface3_GetFlipStatus,
4895 ddraw_surface3_GetOverlayPosition,
4896 ddraw_surface3_GetPalette,
4897 ddraw_surface3_GetPixelFormat,
4898 ddraw_surface3_GetSurfaceDesc,
4899 ddraw_surface3_Initialize,
4900 ddraw_surface3_IsLost,
4901 ddraw_surface3_Lock,
4902 ddraw_surface3_ReleaseDC,
4903 ddraw_surface3_Restore,
4904 ddraw_surface3_SetClipper,
4905 ddraw_surface3_SetColorKey,
4906 ddraw_surface3_SetOverlayPosition,
4907 ddraw_surface3_SetPalette,
4908 ddraw_surface3_Unlock,
4909 ddraw_surface3_UpdateOverlay,
4910 ddraw_surface3_UpdateOverlayDisplay,
4911 ddraw_surface3_UpdateOverlayZOrder,
4912 /* IDirectDrawSurface2 */
4913 ddraw_surface3_GetDDInterface,
4914 ddraw_surface3_PageLock,
4915 ddraw_surface3_PageUnlock,
4916 /* IDirectDrawSurface3 */
4917 ddraw_surface3_SetSurfaceDesc,
4920 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
4922 /* IUnknown */
4923 ddraw_surface2_QueryInterface,
4924 ddraw_surface2_AddRef,
4925 ddraw_surface2_Release,
4926 /* IDirectDrawSurface */
4927 ddraw_surface2_AddAttachedSurface,
4928 ddraw_surface2_AddOverlayDirtyRect,
4929 ddraw_surface2_Blt,
4930 ddraw_surface2_BltBatch,
4931 ddraw_surface2_BltFast,
4932 ddraw_surface2_DeleteAttachedSurface,
4933 ddraw_surface2_EnumAttachedSurfaces,
4934 ddraw_surface2_EnumOverlayZOrders,
4935 ddraw_surface2_Flip,
4936 ddraw_surface2_GetAttachedSurface,
4937 ddraw_surface2_GetBltStatus,
4938 ddraw_surface2_GetCaps,
4939 ddraw_surface2_GetClipper,
4940 ddraw_surface2_GetColorKey,
4941 ddraw_surface2_GetDC,
4942 ddraw_surface2_GetFlipStatus,
4943 ddraw_surface2_GetOverlayPosition,
4944 ddraw_surface2_GetPalette,
4945 ddraw_surface2_GetPixelFormat,
4946 ddraw_surface2_GetSurfaceDesc,
4947 ddraw_surface2_Initialize,
4948 ddraw_surface2_IsLost,
4949 ddraw_surface2_Lock,
4950 ddraw_surface2_ReleaseDC,
4951 ddraw_surface2_Restore,
4952 ddraw_surface2_SetClipper,
4953 ddraw_surface2_SetColorKey,
4954 ddraw_surface2_SetOverlayPosition,
4955 ddraw_surface2_SetPalette,
4956 ddraw_surface2_Unlock,
4957 ddraw_surface2_UpdateOverlay,
4958 ddraw_surface2_UpdateOverlayDisplay,
4959 ddraw_surface2_UpdateOverlayZOrder,
4960 /* IDirectDrawSurface2 */
4961 ddraw_surface2_GetDDInterface,
4962 ddraw_surface2_PageLock,
4963 ddraw_surface2_PageUnlock,
4966 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
4968 /* IUnknown */
4969 ddraw_surface1_QueryInterface,
4970 ddraw_surface1_AddRef,
4971 ddraw_surface1_Release,
4972 /* IDirectDrawSurface */
4973 ddraw_surface1_AddAttachedSurface,
4974 ddraw_surface1_AddOverlayDirtyRect,
4975 ddraw_surface1_Blt,
4976 ddraw_surface1_BltBatch,
4977 ddraw_surface1_BltFast,
4978 ddraw_surface1_DeleteAttachedSurface,
4979 ddraw_surface1_EnumAttachedSurfaces,
4980 ddraw_surface1_EnumOverlayZOrders,
4981 ddraw_surface1_Flip,
4982 ddraw_surface1_GetAttachedSurface,
4983 ddraw_surface1_GetBltStatus,
4984 ddraw_surface1_GetCaps,
4985 ddraw_surface1_GetClipper,
4986 ddraw_surface1_GetColorKey,
4987 ddraw_surface1_GetDC,
4988 ddraw_surface1_GetFlipStatus,
4989 ddraw_surface1_GetOverlayPosition,
4990 ddraw_surface1_GetPalette,
4991 ddraw_surface1_GetPixelFormat,
4992 ddraw_surface1_GetSurfaceDesc,
4993 ddraw_surface1_Initialize,
4994 ddraw_surface1_IsLost,
4995 ddraw_surface1_Lock,
4996 ddraw_surface1_ReleaseDC,
4997 ddraw_surface1_Restore,
4998 ddraw_surface1_SetClipper,
4999 ddraw_surface1_SetColorKey,
5000 ddraw_surface1_SetOverlayPosition,
5001 ddraw_surface1_SetPalette,
5002 ddraw_surface1_Unlock,
5003 ddraw_surface1_UpdateOverlay,
5004 ddraw_surface1_UpdateOverlayDisplay,
5005 ddraw_surface1_UpdateOverlayZOrder,
5008 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5010 ddraw_gamma_control_QueryInterface,
5011 ddraw_gamma_control_AddRef,
5012 ddraw_gamma_control_Release,
5013 ddraw_gamma_control_GetGammaRamp,
5014 ddraw_gamma_control_SetGammaRamp,
5017 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5019 d3d_texture2_QueryInterface,
5020 d3d_texture2_AddRef,
5021 d3d_texture2_Release,
5022 d3d_texture2_GetHandle,
5023 d3d_texture2_PaletteChanged,
5024 d3d_texture2_Load,
5027 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5029 d3d_texture1_QueryInterface,
5030 d3d_texture1_AddRef,
5031 d3d_texture1_Release,
5032 d3d_texture1_Initialize,
5033 d3d_texture1_GetHandle,
5034 d3d_texture1_PaletteChanged,
5035 d3d_texture1_Load,
5036 d3d_texture1_Unload,
5039 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5041 if (!iface) return NULL;
5042 assert(iface->lpVtbl == &ddraw_surface7_vtbl);
5043 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface7_iface);
5046 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5048 if (!iface) return NULL;
5049 assert(iface->lpVtbl == &ddraw_surface4_vtbl);
5050 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface4_iface);
5053 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5055 if (!iface) return NULL;
5056 assert(iface->lpVtbl == &ddraw_surface3_vtbl);
5057 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface3_iface);
5060 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5062 if (!iface) return NULL;
5063 assert(iface->lpVtbl == &ddraw_surface2_vtbl);
5064 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface2_iface);
5067 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5069 if (!iface) return NULL;
5070 assert(iface->lpVtbl == &ddraw_surface1_vtbl);
5071 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface_iface);
5074 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5076 if (!iface) return NULL;
5077 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5078 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirect3DTexture2_iface);
5081 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5083 if (!iface) return NULL;
5084 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5085 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirect3DTexture_iface);
5088 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5090 IDirectDrawSurfaceImpl *surface = parent;
5092 TRACE("surface %p.\n", surface);
5094 /* Check for attached surfaces and detach them. */
5095 if (surface->first_attached != surface)
5097 IDirectDrawSurface7 *root = &surface->first_attached->IDirectDrawSurface7_iface;
5098 IDirectDrawSurface7 *detach = &surface->IDirectDrawSurface7_iface;
5100 /* Well, this shouldn't happen: The surface being attached is
5101 * referenced in AddAttachedSurface(), so it shouldn't be released
5102 * until DeleteAttachedSurface() is called, because the refcount is
5103 * held. It looks like the application released it often enough to
5104 * force this. */
5105 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
5107 /* The refcount will drop to -1 here */
5108 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
5109 ERR("DeleteAttachedSurface failed.\n");
5112 while (surface->next_attached)
5114 IDirectDrawSurface7 *root = &surface->IDirectDrawSurface7_iface;
5115 IDirectDrawSurface7 *detach = &surface->next_attached->IDirectDrawSurface7_iface;
5117 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
5118 ERR("DeleteAttachedSurface failed.\n");
5121 /* Having a texture handle set implies that the device still exists. */
5122 if (surface->Handle)
5123 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5125 /* Reduce the ddraw surface count. */
5126 list_remove(&surface->surface_list_entry);
5128 if (surface == surface->ddraw->primary)
5129 surface->ddraw->primary = NULL;
5131 HeapFree(GetProcessHeap(), 0, surface);
5134 const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5136 ddraw_surface_wined3d_object_destroyed,
5139 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5141 IDirectDrawSurfaceImpl *surface = parent;
5143 TRACE("surface %p.\n", surface);
5145 ddraw_surface_cleanup(surface);
5148 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5150 ddraw_texture_wined3d_object_destroyed,
5153 HRESULT ddraw_surface_create_texture(IDirectDrawSurfaceImpl *surface)
5155 const DDSURFACEDESC2 *desc = &surface->surface_desc;
5156 enum wined3d_format_id format;
5157 WINED3DPOOL pool;
5158 UINT levels;
5160 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5161 levels = desc->u2.dwMipMapCount;
5162 else
5163 levels = 1;
5165 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM.
5166 * Should I forward the MANAGED cap to the managed pool? */
5167 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5168 pool = WINED3DPOOL_SYSTEMMEM;
5169 else
5170 pool = WINED3DPOOL_DEFAULT;
5172 format = PixelFormat_DD2WineD3D(&surface->surface_desc.u4.ddpfPixelFormat);
5173 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5174 return wined3d_texture_create_cube(surface->ddraw->wined3d_device, desc->dwWidth,
5175 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5176 else
5177 return wined3d_texture_create_2d(surface->ddraw->wined3d_device, desc->dwWidth, desc->dwHeight,
5178 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5181 HRESULT ddraw_surface_init(IDirectDrawSurfaceImpl *surface, IDirectDrawImpl *ddraw,
5182 DDSURFACEDESC2 *desc, UINT mip_level, UINT version)
5184 WINED3DPOOL pool = WINED3DPOOL_DEFAULT;
5185 enum wined3d_format_id format;
5186 DWORD usage = 0;
5187 HRESULT hr;
5189 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5190 && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5191 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
5193 /* Tests show surfaces without memory flags get these flags added
5194 * right after creation. */
5195 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5198 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5200 usage |= WINED3DUSAGE_RENDERTARGET;
5201 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5204 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && !(desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
5206 usage |= WINED3DUSAGE_RENDERTARGET;
5209 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
5211 usage |= WINED3DUSAGE_OVERLAY;
5214 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5215 usage |= WINED3DUSAGE_DEPTHSTENCIL;
5217 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5219 pool = WINED3DPOOL_SYSTEMMEM;
5221 else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
5223 pool = WINED3DPOOL_MANAGED;
5224 /* Managed textures have the system memory flag set. */
5225 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5227 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5229 /* Videomemory adds localvidmem. This is mutually exclusive with
5230 * systemmemory and texturemanage. */
5231 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
5234 format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
5235 if (format == WINED3DFMT_UNKNOWN)
5237 WARN("Unsupported / unknown pixelformat.\n");
5238 return DDERR_INVALIDPIXELFORMAT;
5241 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
5242 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
5243 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
5244 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
5245 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
5246 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
5247 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
5248 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
5249 surface->iface_count = 1;
5250 surface->version = version;
5251 surface->ddraw = ddraw;
5253 if (version == 7)
5255 surface->ref7 = 1;
5257 else if (version == 4)
5259 surface->ref4 = 1;
5261 else
5263 surface->ref1 = 1;
5266 copy_to_surfacedesc2(&surface->surface_desc, desc);
5268 surface->first_attached = surface;
5270 hr = wined3d_surface_create(ddraw->wined3d_device, desc->dwWidth, desc->dwHeight, format,
5271 TRUE /* Lockable */, FALSE /* Discard */, mip_level, usage, pool,
5272 WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, DefaultSurfaceType, surface,
5273 &ddraw_surface_wined3d_parent_ops, &surface->wined3d_surface);
5274 if (FAILED(hr))
5276 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
5277 return hr;
5280 /* Anno 1602 stores the pitch right after surface creation, so make sure
5281 * it's there. TODO: Test other fourcc formats. */
5282 if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
5283 || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
5285 surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
5286 if (format == WINED3DFMT_DXT1)
5288 surface->surface_desc.u1.dwLinearSize = max(4, desc->dwWidth) * max(4, desc->dwHeight) / 2;
5290 else
5292 surface->surface_desc.u1.dwLinearSize = max(4, desc->dwWidth) * max(4, desc->dwHeight);
5295 else
5297 surface->surface_desc.dwFlags |= DDSD_PITCH;
5298 surface->surface_desc.u1.lPitch = wined3d_surface_get_pitch(surface->wined3d_surface);
5301 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
5303 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTOVERLAY,
5304 (WINEDDCOLORKEY *)&desc->u3.ddckCKDestOverlay);
5306 if (desc->dwFlags & DDSD_CKDESTBLT)
5308 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTBLT,
5309 (WINEDDCOLORKEY *)&desc->ddckCKDestBlt);
5311 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
5313 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCOVERLAY,
5314 (WINEDDCOLORKEY *)&desc->ddckCKSrcOverlay);
5316 if (desc->dwFlags & DDSD_CKSRCBLT)
5318 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCBLT,
5319 (WINEDDCOLORKEY *)&desc->ddckCKSrcBlt);
5321 if (desc->dwFlags & DDSD_LPSURFACE)
5323 hr = wined3d_surface_set_mem(surface->wined3d_surface, desc->lpSurface);
5324 if (FAILED(hr))
5326 ERR("Failed to set surface memory, hr %#x.\n", hr);
5327 wined3d_surface_decref(surface->wined3d_surface);
5328 return hr;
5332 return DD_OK;