ddraw: Make some functions hotpachable.
[wine.git] / dlls / ddraw / surface.c
blob77a3d444267fa314ec795a3cdccd5915ab57a9d7
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 library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include "ddraw_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
31 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface);
32 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface);
34 static inline struct ddraw_surface *impl_from_IDirectDrawGammaControl(IDirectDrawGammaControl *iface)
36 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawGammaControl_iface);
39 /* This is slow, of course. Also, in case of locks, we can't prevent other
40 * applications from drawing to the screen while we've locked the frontbuffer.
41 * We'd like to do this in wined3d instead, but for that to work wined3d needs
42 * to support windowless rendering first. */
43 HRESULT ddraw_surface_update_frontbuffer(struct ddraw_surface *surface, const RECT *rect, BOOL read)
45 HDC surface_dc, screen_dc;
46 int x, y, w, h;
47 HRESULT hr;
48 BOOL ret;
50 if (!rect)
52 x = 0;
53 y = 0;
54 w = surface->surface_desc.dwWidth;
55 h = surface->surface_desc.dwHeight;
57 else
59 x = rect->left;
60 y = rect->top;
61 w = rect->right - rect->left;
62 h = rect->bottom - rect->top;
65 if (w <= 0 || h <= 0)
66 return DD_OK;
68 if (surface->ddraw->swapchain_window)
70 /* Nothing to do, we control the frontbuffer, or at least the parts we
71 * care about. */
72 if (read)
73 return DD_OK;
75 return wined3d_surface_blt(surface->ddraw->wined3d_frontbuffer, rect,
76 surface->wined3d_surface, rect, 0, NULL, WINED3D_TEXF_POINT);
79 if (FAILED(hr = wined3d_surface_getdc(surface->wined3d_surface, &surface_dc)))
81 ERR("Failed to get surface DC, hr %#x.\n", hr);
82 return hr;
84 if (surface->palette)
85 wined3d_palette_apply_to_dc(surface->palette->wineD3DPalette, surface_dc);
87 if (!(screen_dc = GetDC(NULL)))
89 wined3d_surface_releasedc(surface->wined3d_surface, surface_dc);
90 ERR("Failed to get screen DC.\n");
91 return E_FAIL;
94 if (read)
95 ret = BitBlt(surface_dc, x, y, w, h,
96 screen_dc, x, y, SRCCOPY);
97 else
98 ret = BitBlt(screen_dc, x, y, w, h,
99 surface_dc, x, y, SRCCOPY);
101 ReleaseDC(NULL, screen_dc);
102 wined3d_surface_releasedc(surface->wined3d_surface, surface_dc);
104 if (!ret)
106 ERR("Failed to blit to/from screen.\n");
107 return E_FAIL;
110 return DD_OK;
113 /*****************************************************************************
114 * IUnknown parts follow
115 *****************************************************************************/
117 /*****************************************************************************
118 * IDirectDrawSurface7::QueryInterface
120 * A normal QueryInterface implementation. For QueryInterface rules
121 * see ddraw.c, IDirectDraw7::QueryInterface. This method
122 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
123 * in all versions, the IDirectDrawGammaControl interface and it can
124 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
126 * Params:
127 * riid: The interface id queried for
128 * obj: Address to write the pointer to
130 * Returns:
131 * S_OK on success
132 * E_NOINTERFACE if the requested interface wasn't found
134 *****************************************************************************/
135 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
137 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
139 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
141 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
142 *obj = NULL;
144 if(!riid)
145 return DDERR_INVALIDPARAMS;
147 if (IsEqualGUID(riid, &IID_IDirectDrawSurface7))
149 IDirectDrawSurface7_AddRef(iface);
150 *obj = iface;
151 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
152 return S_OK;
155 if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
157 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
158 *obj = &This->IDirectDrawSurface4_iface;
159 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
160 return S_OK;
163 if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
165 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
166 *obj = &This->IDirectDrawSurface3_iface;
167 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
168 return S_OK;
171 if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
173 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
174 *obj = &This->IDirectDrawSurface2_iface;
175 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
176 return S_OK;
179 if (IsEqualGUID(riid, &IID_IDirectDrawSurface)
180 || IsEqualGUID(riid, &IID_IUnknown))
182 IDirectDrawSurface_AddRef(&This->IDirectDrawSurface_iface);
183 *obj = &This->IDirectDrawSurface_iface;
184 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
185 return S_OK;
188 if (IsEqualGUID(riid, &IID_IDirectDrawGammaControl))
190 IDirectDrawGammaControl_AddRef(&This->IDirectDrawGammaControl_iface);
191 *obj = &This->IDirectDrawGammaControl_iface;
192 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
193 return S_OK;
196 if (IsEqualGUID(riid, &IID_IDirectDrawColorControl))
198 WARN("Color control not implemented.\n");
199 *obj = NULL;
200 return E_NOINTERFACE;
203 if (This->version != 7)
205 if (IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D)
206 || IsEqualGUID(riid, &IID_IDirect3DHALDevice)
207 || IsEqualGUID(riid, &IID_IDirect3DRGBDevice))
209 wined3d_mutex_lock();
210 if (!This->device1)
212 HRESULT hr;
214 if (FAILED(hr = d3d_device_create(This->ddraw, This, (IUnknown *)&This->IDirectDrawSurface_iface,
215 1, &This->device1, (IUnknown *)&This->IDirectDrawSurface_iface)))
217 This->device1 = NULL;
218 wined3d_mutex_unlock();
219 WARN("Failed to create device, hr %#x.\n", hr);
220 return hr;
223 wined3d_mutex_unlock();
225 IDirect3DDevice_AddRef(&This->device1->IDirect3DDevice_iface);
226 *obj = &This->device1->IDirect3DDevice_iface;
227 return S_OK;
230 if (IsEqualGUID(&IID_IDirect3DTexture2, riid))
232 IDirect3DTexture2_AddRef(&This->IDirect3DTexture2_iface);
233 *obj = &This->IDirect3DTexture2_iface;
234 return S_OK;
237 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
239 IDirect3DTexture2_AddRef(&This->IDirect3DTexture_iface);
240 *obj = &This->IDirect3DTexture_iface;
241 return S_OK;
245 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
247 if (This->version != 7)
248 return E_INVALIDARG;
250 return E_NOINTERFACE;
253 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
255 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
257 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
259 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
262 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
264 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
266 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
268 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
271 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
273 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
275 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
277 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
280 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
282 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
284 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
286 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
289 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
290 REFIID riid, void **object)
292 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
294 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
296 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
299 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
301 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
303 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
305 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
308 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
310 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
312 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
314 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
317 static void ddraw_surface_add_iface(struct ddraw_surface *surface)
319 ULONG iface_count = InterlockedIncrement(&surface->iface_count);
320 TRACE("%p increasing iface count to %u.\n", surface, iface_count);
322 if (iface_count == 1)
324 if (surface->ifaceToRelease)
325 IUnknown_AddRef(surface->ifaceToRelease);
326 wined3d_mutex_lock();
327 if (surface->wined3d_rtv)
328 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
329 if (surface->wined3d_surface)
330 wined3d_surface_incref(surface->wined3d_surface);
331 if (surface->wined3d_texture)
332 wined3d_texture_incref(surface->wined3d_texture);
333 wined3d_mutex_unlock();
337 /*****************************************************************************
338 * IDirectDrawSurface7::AddRef
340 * A normal addref implementation
342 * Returns:
343 * The new refcount
345 *****************************************************************************/
346 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
348 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
349 ULONG refcount = InterlockedIncrement(&This->ref7);
351 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
353 if (refcount == 1)
355 ddraw_surface_add_iface(This);
358 return refcount;
361 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
363 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
364 ULONG refcount = InterlockedIncrement(&This->ref4);
366 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
368 if (refcount == 1)
370 ddraw_surface_add_iface(This);
373 return refcount;
376 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
378 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
379 ULONG refcount = InterlockedIncrement(&This->ref3);
381 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
383 if (refcount == 1)
385 ddraw_surface_add_iface(This);
388 return refcount;
391 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
393 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
394 ULONG refcount = InterlockedIncrement(&This->ref2);
396 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
398 if (refcount == 1)
400 ddraw_surface_add_iface(This);
403 return refcount;
406 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
408 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
409 ULONG refcount = InterlockedIncrement(&This->ref1);
411 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
413 if (refcount == 1)
415 ddraw_surface_add_iface(This);
418 return refcount;
421 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
423 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
424 ULONG refcount = InterlockedIncrement(&This->gamma_count);
426 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
428 if (refcount == 1)
430 ddraw_surface_add_iface(This);
433 return refcount;
436 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
438 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
440 TRACE("iface %p.\n", iface);
442 return IUnknown_AddRef(surface->texture_outer);
445 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
447 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
449 TRACE("iface %p.\n", iface);
451 return IUnknown_AddRef(surface->texture_outer);
454 static HRESULT ddraw_surface_set_palette(struct ddraw_surface *surface, IDirectDrawPalette *palette)
456 struct ddraw_palette *palette_impl = unsafe_impl_from_IDirectDrawPalette(palette);
457 struct ddraw_palette *prev;
459 TRACE("iface %p, palette %p.\n", surface, palette);
461 if (palette_impl && palette_impl->flags & DDPCAPS_ALPHA
462 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
464 WARN("Alpha palette set on non-texture surface, returning DDERR_INVALIDSURFACETYPE.\n");
465 return DDERR_INVALIDSURFACETYPE;
468 if (!format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
469 return DDERR_INVALIDPIXELFORMAT;
471 wined3d_mutex_lock();
473 prev = surface->palette;
474 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
476 if (prev)
477 prev->flags &= ~DDPCAPS_PRIMARYSURFACE;
478 if (palette_impl)
479 palette_impl->flags |= DDPCAPS_PRIMARYSURFACE;
480 wined3d_swapchain_set_palette(surface->ddraw->wined3d_swapchain,
481 palette_impl ? palette_impl->wineD3DPalette : NULL);
482 ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
484 if (palette_impl)
485 IDirectDrawPalette_AddRef(&palette_impl->IDirectDrawPalette_iface);
486 if (prev)
487 IDirectDrawPalette_Release(&prev->IDirectDrawPalette_iface);
488 surface->palette = palette_impl;
490 wined3d_mutex_unlock();
492 return DD_OK;
495 static void ddraw_surface_cleanup(struct ddraw_surface *surface)
497 struct ddraw_surface *surf;
498 UINT i;
500 TRACE("surface %p.\n", surface);
502 /* The refcount test shows that the palette is detached when the surface
503 * is destroyed. */
504 ddraw_surface_set_palette(surface, NULL);
506 /* Loop through all complex attached surfaces and destroy them.
508 * Yet again, only the root can have more than one complexly attached
509 * surface, all the others have a total of one. */
510 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
512 if (!surface->complex_array[i])
513 break;
515 surf = surface->complex_array[i];
516 surface->complex_array[i] = NULL;
517 if (!surf->is_complex_root)
518 ddraw_surface_cleanup(surf);
521 if (surface->device1)
522 IUnknown_Release(&surface->device1->IUnknown_inner);
524 if (surface->iface_count > 1)
526 /* This can happen when a complex surface is destroyed, because the
527 * 2nd surface was addref()ed when the app called
528 * GetAttachedSurface(). */
529 WARN("Destroying surface %p with refcounts 7: %u 4: %u 3: %u 2: %u 1: %u.\n",
530 surface, surface->ref7, surface->ref4, surface->ref3, surface->ref2, surface->ref1);
533 if (surface->wined3d_rtv)
534 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
535 if (surface->wined3d_texture)
536 wined3d_texture_decref(surface->wined3d_texture);
537 if (surface->wined3d_surface)
538 wined3d_surface_decref(surface->wined3d_surface);
541 ULONG ddraw_surface_release_iface(struct ddraw_surface *This)
543 ULONG iface_count = InterlockedDecrement(&This->iface_count);
544 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
546 if (iface_count == 0)
548 IUnknown *release_iface = This->ifaceToRelease;
550 /* Complex attached surfaces are destroyed implicitly when the root is released */
551 wined3d_mutex_lock();
552 if(!This->is_complex_root)
554 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
555 wined3d_mutex_unlock();
556 return iface_count;
558 ddraw_surface_cleanup(This);
559 wined3d_mutex_unlock();
561 if (release_iface)
562 IUnknown_Release(release_iface);
565 return iface_count;
568 /*****************************************************************************
569 * IDirectDrawSurface7::Release
571 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
572 * surface is destroyed.
574 * Destroying the surface is a bit tricky. For the connection between
575 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
576 * It has a nice graph explaining the connection.
578 * What happens here is basically this:
579 * When a surface is destroyed, its WineD3DSurface is released,
580 * and the refcount of the DirectDraw interface is reduced by 1. If it has
581 * complex surfaces attached to it, then these surfaces are destroyed too,
582 * regardless of their refcount. If any surface being destroyed has another
583 * surface attached to it (with a "soft" attachment, not complex), then
584 * this surface is detached with DeleteAttachedSurface.
586 * When the surface is a texture, the WineD3DTexture is released.
587 * If the surface is the Direct3D render target, then the D3D
588 * capabilities of the WineD3DDevice are uninitialized, which causes the
589 * swapchain to be released.
591 * When a complex sublevel falls to ref zero, then this is ignored.
593 * Returns:
594 * The new refcount
596 *****************************************************************************/
597 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
599 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
600 ULONG refcount = InterlockedDecrement(&This->ref7);
602 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
604 if (refcount == 0)
606 ddraw_surface_release_iface(This);
609 return refcount;
612 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
614 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
615 ULONG refcount = InterlockedDecrement(&This->ref4);
617 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
619 if (refcount == 0)
621 ddraw_surface_release_iface(This);
624 return refcount;
627 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
629 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
630 ULONG refcount = InterlockedDecrement(&This->ref3);
632 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
634 if (refcount == 0)
636 ddraw_surface_release_iface(This);
639 return refcount;
642 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
644 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
645 ULONG refcount = InterlockedDecrement(&This->ref2);
647 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
649 if (refcount == 0)
651 ddraw_surface_release_iface(This);
654 return refcount;
657 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
659 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
660 ULONG refcount = InterlockedDecrement(&This->ref1);
662 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
664 if (refcount == 0)
666 ddraw_surface_release_iface(This);
669 return refcount;
672 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
674 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
675 ULONG refcount = InterlockedDecrement(&This->gamma_count);
677 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
679 if (refcount == 0)
681 ddraw_surface_release_iface(This);
684 return refcount;
687 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
689 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
691 TRACE("iface %p.\n", iface);
693 return IUnknown_Release(surface->texture_outer);
696 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
698 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
700 TRACE("iface %p.\n", iface);
702 return IUnknown_Release(surface->texture_outer);
705 /*****************************************************************************
706 * IDirectDrawSurface7::GetAttachedSurface
708 * Returns an attached surface with the requested caps. Surface attachment
709 * and complex surfaces are not clearly described by the MSDN or sdk,
710 * so this method is tricky and likely to contain problems.
711 * This implementation searches the complex list first, then the
712 * attachment chain.
714 * The chains are searched from This down to the last surface in the chain,
715 * not from the first element in the chain. The first surface found is
716 * returned. The MSDN says that this method fails if more than one surface
717 * matches the caps, but it is not sure if that is right. The attachment
718 * structure may not even allow two matching surfaces.
720 * The found surface is AddRef-ed before it is returned.
722 * Params:
723 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
724 * Surface: Address to store the found surface
726 * Returns:
727 * DD_OK on success
728 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
729 * DDERR_NOTFOUND if no surface was found
731 *****************************************************************************/
732 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
733 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
735 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
736 struct ddraw_surface *surf;
737 DDSCAPS2 our_caps;
738 int i;
740 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
742 wined3d_mutex_lock();
744 if(This->version < 7)
746 /* Earlier dx apps put garbage into these members, clear them */
747 our_caps.dwCaps = Caps->dwCaps;
748 our_caps.dwCaps2 = 0;
749 our_caps.dwCaps3 = 0;
750 our_caps.u1.dwCaps4 = 0;
752 else
754 our_caps = *Caps;
757 TRACE("(%p): Looking for caps: %x,%x,%x,%x\n", This, our_caps.dwCaps, our_caps.dwCaps2, our_caps.dwCaps3, our_caps.u1.dwCaps4); /* FIXME: Better debugging */
759 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
761 surf = This->complex_array[i];
762 if(!surf) break;
764 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
765 surf->surface_desc.ddsCaps.dwCaps,
766 surf->surface_desc.ddsCaps.dwCaps2,
767 surf->surface_desc.ddsCaps.dwCaps3,
768 surf->surface_desc.ddsCaps.u1.dwCaps4);
770 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
771 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
773 /* MSDN: "This method fails if more than one surface is attached
774 * that matches the capabilities requested."
776 * Not sure how to test this.
779 TRACE("(%p): Returning surface %p\n", This, surf);
780 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
781 *Surface = &surf->IDirectDrawSurface7_iface;
782 ddraw_surface7_AddRef(*Surface);
783 wined3d_mutex_unlock();
785 return DD_OK;
789 /* Next, look at the attachment chain */
790 surf = This;
792 while( (surf = surf->next_attached) )
794 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
795 surf->surface_desc.ddsCaps.dwCaps,
796 surf->surface_desc.ddsCaps.dwCaps2,
797 surf->surface_desc.ddsCaps.dwCaps3,
798 surf->surface_desc.ddsCaps.u1.dwCaps4);
800 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
801 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
803 TRACE("(%p): Returning surface %p\n", This, surf);
804 *Surface = &surf->IDirectDrawSurface7_iface;
805 ddraw_surface7_AddRef(*Surface);
806 wined3d_mutex_unlock();
807 return DD_OK;
811 TRACE("(%p) Didn't find a valid surface\n", This);
813 wined3d_mutex_unlock();
815 *Surface = NULL;
816 return DDERR_NOTFOUND;
819 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
820 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
822 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
823 struct ddraw_surface *attachment_impl;
824 IDirectDrawSurface7 *attachment7;
825 HRESULT hr;
827 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
829 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
830 caps, &attachment7);
831 if (FAILED(hr))
833 *attachment = NULL;
834 return hr;
836 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
837 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
838 ddraw_surface4_AddRef(*attachment);
839 ddraw_surface7_Release(attachment7);
841 return hr;
844 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
845 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
847 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
848 struct ddraw_surface *attachment_impl;
849 IDirectDrawSurface7 *attachment7;
850 DDSCAPS2 caps2;
851 HRESULT hr;
853 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
855 caps2.dwCaps = caps->dwCaps;
856 caps2.dwCaps2 = 0;
857 caps2.dwCaps3 = 0;
858 caps2.u1.dwCaps4 = 0;
860 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
861 &caps2, &attachment7);
862 if (FAILED(hr))
864 *attachment = NULL;
865 return hr;
867 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
868 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
869 ddraw_surface3_AddRef(*attachment);
870 ddraw_surface7_Release(attachment7);
872 return hr;
875 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
876 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
878 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
879 struct ddraw_surface *attachment_impl;
880 IDirectDrawSurface7 *attachment7;
881 DDSCAPS2 caps2;
882 HRESULT hr;
884 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
886 caps2.dwCaps = caps->dwCaps;
887 caps2.dwCaps2 = 0;
888 caps2.dwCaps3 = 0;
889 caps2.u1.dwCaps4 = 0;
891 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
892 &caps2, &attachment7);
893 if (FAILED(hr))
895 *attachment = NULL;
896 return hr;
898 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
899 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
900 ddraw_surface2_AddRef(*attachment);
901 ddraw_surface7_Release(attachment7);
903 return hr;
906 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
907 DDSCAPS *caps, IDirectDrawSurface **attachment)
909 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
910 struct ddraw_surface *attachment_impl;
911 IDirectDrawSurface7 *attachment7;
912 DDSCAPS2 caps2;
913 HRESULT hr;
915 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
917 caps2.dwCaps = caps->dwCaps;
918 caps2.dwCaps2 = 0;
919 caps2.dwCaps3 = 0;
920 caps2.u1.dwCaps4 = 0;
922 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
923 &caps2, &attachment7);
924 if (FAILED(hr))
926 *attachment = NULL;
927 return hr;
929 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
930 *attachment = &attachment_impl->IDirectDrawSurface_iface;
931 ddraw_surface1_AddRef(*attachment);
932 ddraw_surface7_Release(attachment7);
934 return hr;
937 /*****************************************************************************
938 * IDirectDrawSurface7::Lock
940 * Locks the surface and returns a pointer to the surface's memory
942 * Params:
943 * Rect: Rectangle to lock. If NULL, the whole surface is locked
944 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
945 * Flags: Locking flags, e.g Read only or write only
946 * h: An event handle that's not used and must be NULL
948 * Returns:
949 * DD_OK on success
950 * DDERR_INVALIDPARAMS if DDSD is NULL
951 * For more details, see IWineD3DSurface::LockRect
953 *****************************************************************************/
954 static HRESULT surface_lock(struct ddraw_surface *This,
955 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
957 struct wined3d_map_desc map_desc;
958 HRESULT hr = DD_OK;
960 TRACE("This %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
961 This, wine_dbgstr_rect(Rect), DDSD, Flags, h);
963 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
964 wined3d_mutex_lock();
966 /* Should I check for the handle to be NULL?
968 * The DDLOCK flags and the D3DLOCK flags are equal
969 * for the supported values. The others are ignored by WineD3D
972 /* Windows zeroes this if the rect is invalid */
973 DDSD->lpSurface = 0;
975 if (Rect)
977 if ((Rect->left < 0)
978 || (Rect->top < 0)
979 || (Rect->left > Rect->right)
980 || (Rect->top > Rect->bottom)
981 || (Rect->right > This->surface_desc.dwWidth)
982 || (Rect->bottom > This->surface_desc.dwHeight))
984 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
985 wined3d_mutex_unlock();
986 return DDERR_INVALIDPARAMS;
990 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
991 hr = ddraw_surface_update_frontbuffer(This, Rect, TRUE);
992 if (SUCCEEDED(hr))
993 hr = wined3d_surface_map(This->wined3d_surface, &map_desc, Rect, Flags);
994 if (FAILED(hr))
996 wined3d_mutex_unlock();
997 switch(hr)
999 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
1000 * specific error. But since IWineD3DSurface::LockRect returns that error in this
1001 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
1002 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
1003 * is much easier to do it in one place in ddraw
1005 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
1006 default: return hr;
1010 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1012 if (Flags & DDLOCK_READONLY)
1013 memset(&This->ddraw->primary_lock, 0, sizeof(This->ddraw->primary_lock));
1014 else if (Rect)
1015 This->ddraw->primary_lock = *Rect;
1016 else
1017 SetRect(&This->ddraw->primary_lock, 0, 0, This->surface_desc.dwWidth, This->surface_desc.dwHeight);
1020 /* Windows does not set DDSD_LPSURFACE on locked surfaces. */
1021 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
1022 DDSD->lpSurface = map_desc.data;
1024 TRACE("locked surface returning description :\n");
1025 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1027 wined3d_mutex_unlock();
1029 return DD_OK;
1032 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1033 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1035 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1037 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1038 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1040 if (!surface_desc) return DDERR_INVALIDPARAMS;
1041 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1042 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1044 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1045 return DDERR_INVALIDPARAMS;
1047 return surface_lock(surface, rect, surface_desc, flags, h);
1050 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1051 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1053 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1055 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1056 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1058 if (!surface_desc) return DDERR_INVALIDPARAMS;
1059 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1060 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1062 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1063 return DDERR_INVALIDPARAMS;
1065 return surface_lock(surface, rect, surface_desc, flags, h);
1068 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1069 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1071 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1072 DDSURFACEDESC2 surface_desc2;
1073 HRESULT hr;
1075 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1076 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1078 if (!surface_desc) return DDERR_INVALIDPARAMS;
1079 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1080 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1082 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1083 return DDERR_INVALIDPARAMS;
1086 surface_desc2.dwSize = surface_desc->dwSize;
1087 surface_desc2.dwFlags = 0;
1088 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1089 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1090 surface_desc->dwSize = surface_desc2.dwSize;
1091 return hr;
1094 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1095 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1097 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1098 DDSURFACEDESC2 surface_desc2;
1099 HRESULT hr;
1101 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1102 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1104 if (!surface_desc) return DDERR_INVALIDPARAMS;
1105 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1106 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1108 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1109 return DDERR_INVALIDPARAMS;
1112 surface_desc2.dwSize = surface_desc->dwSize;
1113 surface_desc2.dwFlags = 0;
1114 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1115 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1116 surface_desc->dwSize = surface_desc2.dwSize;
1117 return hr;
1120 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1121 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1123 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1124 DDSURFACEDESC2 surface_desc2;
1125 HRESULT hr;
1126 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1127 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1129 if (!surface_desc) return DDERR_INVALIDPARAMS;
1130 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1131 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1133 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1134 return DDERR_INVALIDPARAMS;
1137 surface_desc2.dwSize = surface_desc->dwSize;
1138 surface_desc2.dwFlags = 0;
1139 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1140 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1141 surface_desc->dwSize = surface_desc2.dwSize;
1142 return hr;
1145 /*****************************************************************************
1146 * IDirectDrawSurface7::Unlock
1148 * Unlocks an locked surface
1150 * Params:
1151 * Rect: Not used by this implementation
1153 * Returns:
1154 * D3D_OK on success
1155 * For more details, see IWineD3DSurface::UnlockRect
1157 *****************************************************************************/
1158 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
1160 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1161 HRESULT hr;
1163 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1165 wined3d_mutex_lock();
1166 hr = wined3d_surface_unmap(surface->wined3d_surface);
1167 if (SUCCEEDED(hr) && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1168 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE);
1169 wined3d_mutex_unlock();
1171 return hr;
1174 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1176 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1178 TRACE("iface %p, rect %p.\n", iface, pRect);
1180 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, pRect);
1183 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1185 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1187 TRACE("iface %p, data %p.\n", iface, data);
1189 /* data might not be the LPRECT of later versions, so drop it. */
1190 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1193 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1195 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1197 TRACE("iface %p, data %p.\n", iface, data);
1199 /* data might not be the LPRECT of later versions, so drop it. */
1200 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1203 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1205 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1207 TRACE("iface %p, data %p.\n", iface, data);
1209 /* data might not be the LPRECT of later versions, so drop it. */
1210 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1213 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Flip(IDirectDrawSurface7 *iface,
1214 IDirectDrawSurface7 *src, DWORD flags)
1216 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
1217 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src);
1218 struct wined3d_rendertarget_view *tmp_rtv, *src_rtv, *rtv;
1219 struct ddraw_texture *ddraw_texture, *prev_ddraw_texture;
1220 DDSCAPS2 caps = {DDSCAPS_FLIP, 0, 0, {0}};
1221 struct wined3d_texture *texture;
1222 IDirectDrawSurface7 *current;
1223 struct wined3d_surface *tmp;
1224 HRESULT hr;
1226 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1228 if (src == iface || !(dst_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1229 return DDERR_NOTFLIPPABLE;
1231 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
1232 return DDERR_SURFACELOST;
1234 wined3d_mutex_lock();
1236 tmp_rtv = ddraw_surface_get_rendertarget_view(dst_impl);
1237 tmp = dst_impl->wined3d_surface;
1238 texture = dst_impl->wined3d_texture;
1239 rtv = wined3d_device_get_rendertarget_view(dst_impl->ddraw->wined3d_device, 0);
1240 ddraw_texture = wined3d_texture_get_parent(dst_impl->wined3d_texture);
1242 if (src_impl)
1244 for (current = iface; current != src;)
1246 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1248 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1249 wined3d_mutex_unlock();
1250 return DDERR_NOTFLIPPABLE;
1252 ddraw_surface7_Release(current);
1253 if (current == iface)
1255 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1256 wined3d_mutex_unlock();
1257 return DDERR_NOTFLIPPABLE;
1261 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1262 if (rtv == dst_impl->wined3d_rtv)
1263 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1264 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1265 dst_impl->wined3d_rtv = src_rtv;
1266 wined3d_resource_set_parent(wined3d_surface_get_resource(src_impl->wined3d_surface), dst_impl);
1267 dst_impl->wined3d_surface = src_impl->wined3d_surface;
1268 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1269 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1270 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1271 ddraw_texture = prev_ddraw_texture;
1273 else
1275 for (current = iface;;)
1277 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1279 ERR("Can't find a flip target\n");
1280 wined3d_mutex_unlock();
1281 return DDERR_NOTFLIPPABLE; /* Unchecked */
1283 ddraw_surface7_Release(current);
1284 if (current == iface)
1286 dst_impl = impl_from_IDirectDrawSurface7(iface);
1287 break;
1290 src_impl = impl_from_IDirectDrawSurface7(current);
1291 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1292 if (rtv == dst_impl->wined3d_rtv)
1293 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1294 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1295 dst_impl->wined3d_rtv = src_rtv;
1296 wined3d_resource_set_parent(wined3d_surface_get_resource(src_impl->wined3d_surface), dst_impl);
1297 dst_impl->wined3d_surface = src_impl->wined3d_surface;
1298 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1299 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1300 ddraw_texture = prev_ddraw_texture;
1301 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1302 dst_impl = src_impl;
1306 /* We don't have to worry about potential texture bindings, since
1307 * flippable surfaces can never be textures. */
1308 if (rtv == src_impl->wined3d_rtv)
1309 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, tmp_rtv, FALSE);
1310 wined3d_rendertarget_view_set_parent(tmp_rtv, src_impl);
1311 src_impl->wined3d_rtv = tmp_rtv;
1312 wined3d_resource_set_parent(wined3d_surface_get_resource(tmp), src_impl);
1313 src_impl->wined3d_surface = tmp;
1314 wined3d_resource_set_parent(wined3d_texture_get_resource(texture), ddraw_texture);
1315 src_impl->wined3d_texture = texture;
1317 if (flags)
1319 static UINT once;
1320 if (!once++)
1321 FIXME("Ignoring flags %#x.\n", flags);
1322 else
1323 WARN("Ignoring flags %#x.\n", flags);
1326 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1327 hr = ddraw_surface_update_frontbuffer(dst_impl, NULL, FALSE);
1328 else
1329 hr = DD_OK;
1331 wined3d_mutex_unlock();
1333 return hr;
1336 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Flip(IDirectDrawSurface4 *iface,
1337 IDirectDrawSurface4 *dst, DWORD flags)
1339 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1340 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1342 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1344 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1345 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1348 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Flip(IDirectDrawSurface3 *iface,
1349 IDirectDrawSurface3 *dst, DWORD flags)
1351 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1352 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1354 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1356 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1357 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1360 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Flip(IDirectDrawSurface2 *iface,
1361 IDirectDrawSurface2 *dst, DWORD flags)
1363 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1364 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1366 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1368 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1369 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1372 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Flip(IDirectDrawSurface *iface,
1373 IDirectDrawSurface *dst, DWORD flags)
1375 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1376 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1378 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1380 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1381 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1384 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1385 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags,
1386 const WINEDDBLTFX *fx, enum wined3d_texture_filter_type filter)
1388 struct wined3d_surface *wined3d_src_surface = src_surface ? src_surface->wined3d_surface : NULL;
1389 RECT src_rect, dst_rect;
1390 float scale_x, scale_y;
1391 const RECT *clip_rect;
1392 UINT clip_list_size;
1393 RGNDATA *clip_list;
1394 HRESULT hr = DD_OK;
1395 UINT i;
1397 if (!dst_surface->clipper)
1399 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1400 hr = ddraw_surface_update_frontbuffer(src_surface, src_rect_in, TRUE);
1401 if (SUCCEEDED(hr))
1402 hr = wined3d_surface_blt(dst_surface->wined3d_surface, dst_rect_in,
1403 wined3d_src_surface, src_rect_in, flags, fx, filter);
1404 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
1405 hr = ddraw_surface_update_frontbuffer(dst_surface, dst_rect_in, FALSE);
1407 return hr;
1410 if (!dst_rect_in)
1412 dst_rect.left = 0;
1413 dst_rect.top = 0;
1414 dst_rect.right = dst_surface->surface_desc.dwWidth;
1415 dst_rect.bottom = dst_surface->surface_desc.dwHeight;
1417 else
1419 dst_rect = *dst_rect_in;
1422 if (IsRectEmpty(&dst_rect))
1423 return DDERR_INVALIDRECT;
1425 if (src_surface)
1427 if (!src_rect_in)
1429 src_rect.left = 0;
1430 src_rect.top = 0;
1431 src_rect.right = src_surface->surface_desc.dwWidth;
1432 src_rect.bottom = src_surface->surface_desc.dwHeight;
1434 else
1436 src_rect = *src_rect_in;
1439 if (IsRectEmpty(&src_rect))
1440 return DDERR_INVALIDRECT;
1442 else
1444 SetRect(&src_rect, 0, 0, 0, 0);
1447 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1448 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1450 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1451 &dst_rect, NULL, &clip_list_size)))
1453 WARN("Failed to get clip list size, hr %#x.\n", hr);
1454 return hr;
1457 if (!(clip_list = HeapAlloc(GetProcessHeap(), 0, clip_list_size)))
1459 WARN("Failed to allocate clip list.\n");
1460 return E_OUTOFMEMORY;
1463 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1464 &dst_rect, clip_list, &clip_list_size)))
1466 WARN("Failed to get clip list, hr %#x.\n", hr);
1467 HeapFree(GetProcessHeap(), 0, clip_list);
1468 return hr;
1471 clip_rect = (RECT *)clip_list->Buffer;
1472 for (i = 0; i < clip_list->rdh.nCount; ++i)
1474 RECT src_rect_clipped = src_rect;
1476 if (src_surface)
1478 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1479 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1480 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1481 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1483 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1485 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE)))
1486 break;
1490 if (FAILED(hr = wined3d_surface_blt(dst_surface->wined3d_surface, &clip_rect[i],
1491 wined3d_src_surface, &src_rect_clipped, flags, fx, filter)))
1492 break;
1494 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1496 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE)))
1497 break;
1501 HeapFree(GetProcessHeap(), 0, clip_list);
1502 return hr;
1505 /*****************************************************************************
1506 * IDirectDrawSurface7::Blt
1508 * Performs a blit on the surface
1510 * Params:
1511 * DestRect: Destination rectangle, can be NULL
1512 * SrcSurface: Source surface, can be NULL
1513 * SrcRect: Source rectangle, can be NULL
1514 * Flags: Blt flags
1515 * DDBltFx: Some extended blt parameters, connected to the flags
1517 * Returns:
1518 * D3D_OK on success
1519 * See IWineD3DSurface::Blt for more details
1521 *****************************************************************************/
1522 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1523 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1525 struct ddraw_surface *dst_surface = impl_from_IDirectDrawSurface7(iface);
1526 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1527 HRESULT hr = DD_OK;
1528 DDBLTFX rop_fx;
1530 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1531 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1533 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1534 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1536 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1537 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1538 return DDERR_INVALIDPARAMS;
1541 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1542 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1543 return DDERR_INVALIDPARAMS;
1546 wined3d_mutex_lock();
1548 if (Flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1550 if (Flags & DDBLT_ROP)
1552 wined3d_mutex_unlock();
1553 WARN("DDBLT_ROP used with DDBLT_COLORFILL or DDBLT_DEPTHFILL, returning DDERR_INVALIDPARAMS.\n");
1554 return DDERR_INVALIDPARAMS;
1556 if (src_surface)
1558 wined3d_mutex_unlock();
1559 WARN("Depth or colorfill is not compatible with source surfaces, returning DDERR_INVALIDPARAMS\n");
1560 return DDERR_INVALIDPARAMS;
1562 if (!DDBltFx)
1564 wined3d_mutex_unlock();
1565 WARN("Depth or colorfill used with DDBltFx = NULL, returning DDERR_INVALIDPARAMS.\n");
1566 return DDERR_INVALIDPARAMS;
1569 if ((Flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) == (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1570 Flags &= ~DDBLT_DEPTHFILL;
1572 if ((dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (Flags & DDBLT_COLORFILL))
1574 wined3d_mutex_unlock();
1575 WARN("DDBLT_COLORFILL used on a depth buffer, returning DDERR_INVALIDPARAMS.\n");
1576 return DDERR_INVALIDPARAMS;
1578 if (!(dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (Flags & DDBLT_DEPTHFILL))
1580 wined3d_mutex_unlock();
1581 WARN("DDBLT_DEPTHFILL used on a color buffer, returning DDERR_INVALIDPARAMS.\n");
1582 return DDERR_INVALIDPARAMS;
1586 if (Flags & DDBLT_ROP)
1588 if (!DDBltFx)
1590 wined3d_mutex_unlock();
1591 WARN("DDBLT_ROP used with DDBltFx = NULL, returning DDERR_INVALIDPARAMS.\n");
1592 return DDERR_INVALIDPARAMS;
1595 Flags &= ~DDBLT_ROP;
1596 switch (DDBltFx->dwROP)
1598 case SRCCOPY:
1599 break;
1601 case WHITENESS:
1602 case BLACKNESS:
1603 rop_fx = *DDBltFx;
1605 if (DDBltFx->dwROP == WHITENESS)
1606 rop_fx.u5.dwFillColor = 0xffffffff;
1607 else
1608 rop_fx.u5.dwFillColor = 0;
1610 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1611 Flags |= DDBLT_DEPTHFILL;
1612 else
1613 Flags |= DDBLT_COLORFILL;
1615 DDBltFx = &rop_fx;
1616 break;
1618 default:
1619 wined3d_mutex_unlock();
1620 WARN("Unsupported ROP %#x used, returning DDERR_NORASTEROPHW.\n", DDBltFx->dwROP);
1621 return DDERR_NORASTEROPHW;
1625 if (Flags & DDBLT_KEYSRC && (!src_surface || !(src_surface->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1627 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1628 wined3d_mutex_unlock();
1629 return DDERR_INVALIDPARAMS;
1632 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1633 * does, copy the struct, and replace the ddraw surfaces with the wined3d
1634 * surfaces. So far no blitting operations using surfaces in the bltfx
1635 * struct are supported anyway. */
1636 hr = ddraw_surface_blt_clipped(dst_surface, DestRect, src_surface, SrcRect,
1637 Flags, (WINEDDBLTFX *)DDBltFx, WINED3D_TEXF_LINEAR);
1639 wined3d_mutex_unlock();
1640 switch(hr)
1642 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1643 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
1644 default: return hr;
1648 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1649 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1651 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1652 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1654 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1655 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1657 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1658 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1661 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1662 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1664 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1665 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1667 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1668 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1670 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1671 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1674 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1675 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1677 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1678 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1680 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1681 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1683 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1684 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1687 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1688 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1690 struct ddraw_surface *dst = impl_from_IDirectDrawSurface(iface);
1691 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1693 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1694 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1696 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1697 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1700 /*****************************************************************************
1701 * IDirectDrawSurface7::AddAttachedSurface
1703 * Attaches a surface to another surface. How the surface attachments work
1704 * is not totally understood yet, and this method is prone to problems.
1705 * The surface that is attached is AddRef-ed.
1707 * Tests with complex surfaces suggest that the surface attachments form a
1708 * tree, but no method to test this has been found yet.
1710 * The attachment list consists of a first surface (first_attached) and
1711 * for each surface a pointer to the next attached surface (next_attached).
1712 * For the first surface, and a surface that has no attachments
1713 * first_attached points to the surface itself. A surface that has
1714 * no successors in the chain has next_attached set to NULL.
1716 * Newly attached surfaces are attached right after the root surface.
1717 * If a surface is attached to a complex surface compound, it's attached to
1718 * the surface that the app requested, not the complex root. See
1719 * GetAttachedSurface for a description how surfaces are found.
1721 * This is how the current implementation works, and it was coded by looking
1722 * at the needs of the applications.
1724 * So far only Z-Buffer attachments are tested, and they are activated in
1725 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1726 * Back buffers should work in 2D mode, but they are not tested(They can be
1727 * attached in older iface versions). Rendering to the front buffer and
1728 * switching between that and double buffering is not yet implemented in
1729 * WineD3D, so for 3D it might have unexpected results.
1731 * ddraw_surface_attach_surface is the real thing,
1732 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1733 * performs additional checks. Version 7 of this interface is much more restrictive
1734 * than its predecessors.
1736 * Params:
1737 * Attach: Surface to attach to iface
1739 * Returns:
1740 * DD_OK on success
1741 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1743 *****************************************************************************/
1744 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1746 TRACE("surface %p, attachment %p.\n", This, Surf);
1748 if(Surf == This)
1749 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1751 wined3d_mutex_lock();
1753 /* Check if the surface is already attached somewhere */
1754 if (Surf->next_attached || Surf->first_attached != Surf)
1756 /* TODO: Test for the structure of the manual attachment. Is it a
1757 * chain or a list? What happens if one surface is attached to 2
1758 * different surfaces? */
1759 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1760 Surf, Surf->next_attached, Surf->first_attached);
1762 wined3d_mutex_unlock();
1763 return DDERR_SURFACEALREADYATTACHED;
1766 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1767 Surf->next_attached = This->next_attached;
1768 Surf->first_attached = This->first_attached;
1769 This->next_attached = Surf;
1771 /* Check if the WineD3D depth stencil needs updating */
1772 if (This->ddraw->d3ddevice)
1773 d3d_device_update_depth_stencil(This->ddraw->d3ddevice);
1775 wined3d_mutex_unlock();
1777 return DD_OK;
1780 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
1782 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
1783 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1784 HRESULT hr;
1786 TRACE("iface %p, attachment %p.\n", iface, attachment);
1788 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1789 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1792 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1793 attachment_impl->surface_desc.ddsCaps.dwCaps);
1794 return DDERR_CANNOTATTACHSURFACE;
1797 hr = ddraw_surface_attach_surface(This, attachment_impl);
1798 if (FAILED(hr))
1800 return hr;
1802 attachment_impl->attached_iface = (IUnknown *)attachment;
1803 IUnknown_AddRef(attachment_impl->attached_iface);
1804 return hr;
1807 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1809 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1810 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1811 HRESULT hr;
1813 TRACE("iface %p, attachment %p.\n", iface, attachment);
1815 /* Tests suggest that
1816 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1817 * -> offscreen plain surfaces can be attached to primaries
1818 * -> primaries can be attached to offscreen plain surfaces
1819 * -> z buffers can be attached to primaries */
1820 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1821 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1823 /* Sizes have to match */
1824 if (attachment_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
1825 || attachment_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
1827 WARN("Surface sizes do not match.\n");
1828 return DDERR_CANNOTATTACHSURFACE;
1831 else if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
1832 || !(attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER)))
1834 WARN("Invalid attachment combination.\n");
1835 return DDERR_CANNOTATTACHSURFACE;
1838 if (FAILED(hr = ddraw_surface_attach_surface(surface, attachment_impl)))
1839 return hr;
1841 attachment_impl->attached_iface = (IUnknown *)attachment;
1842 IUnknown_AddRef(attachment_impl->attached_iface);
1843 return hr;
1846 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1848 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1849 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1850 HRESULT hr;
1852 TRACE("iface %p, attachment %p.\n", iface, attachment);
1854 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1855 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1856 return hr;
1858 attachment_impl->attached_iface = (IUnknown *)attachment;
1859 IUnknown_AddRef(attachment_impl->attached_iface);
1860 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1861 return hr;
1864 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1866 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1867 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1868 HRESULT hr;
1870 TRACE("iface %p, attachment %p.\n", iface, attachment);
1872 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1873 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1874 return hr;
1876 attachment_impl->attached_iface = (IUnknown *)attachment;
1877 IUnknown_AddRef(attachment_impl->attached_iface);
1878 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1879 return hr;
1882 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1884 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1885 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1886 HRESULT hr;
1888 TRACE("iface %p, attachment %p.\n", iface, attachment);
1890 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1891 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1892 return hr;
1894 attachment_impl->attached_iface = (IUnknown *)attachment;
1895 IUnknown_AddRef(attachment_impl->attached_iface);
1896 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1897 return hr;
1900 /*****************************************************************************
1901 * IDirectDrawSurface7::DeleteAttachedSurface
1903 * Removes a surface from the attachment chain. The surface's refcount
1904 * is decreased by one after it has been removed
1906 * Params:
1907 * Flags: Some flags, not used by this implementation
1908 * Attach: Surface to detach
1910 * Returns:
1911 * DD_OK on success
1912 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1914 *****************************************************************************/
1915 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
1916 struct ddraw_surface *attachment, IUnknown *detach_iface)
1918 struct ddraw_surface *prev = surface;
1920 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
1922 wined3d_mutex_lock();
1923 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
1925 wined3d_mutex_unlock();
1926 return DDERR_CANNOTDETACHSURFACE;
1929 if (attachment->attached_iface != detach_iface)
1931 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
1932 wined3d_mutex_unlock();
1933 return DDERR_SURFACENOTATTACHED;
1936 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1937 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1939 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1940 /* FIXME: we should probably also subtract from dwMipMapCount of this
1941 * and all parent surfaces */
1944 /* Find the predecessor of the detached surface */
1945 while (prev)
1947 if (prev->next_attached == attachment)
1948 break;
1949 prev = prev->next_attached;
1952 /* There must be a surface, otherwise there's a bug */
1953 assert(prev);
1955 /* Unchain the surface */
1956 prev->next_attached = attachment->next_attached;
1957 attachment->next_attached = NULL;
1958 attachment->first_attached = attachment;
1960 /* Check if the wined3d depth stencil needs updating. */
1961 if (surface->ddraw->d3ddevice)
1962 d3d_device_update_depth_stencil(surface->ddraw->d3ddevice);
1963 wined3d_mutex_unlock();
1965 /* Set attached_iface to NULL before releasing it, the surface may go
1966 * away. */
1967 attachment->attached_iface = NULL;
1968 IUnknown_Release(detach_iface);
1970 return DD_OK;
1973 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1974 DWORD flags, IDirectDrawSurface7 *attachment)
1976 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1977 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1979 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1981 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1984 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1985 DWORD flags, IDirectDrawSurface4 *attachment)
1987 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1988 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1990 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1992 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1995 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1996 DWORD flags, IDirectDrawSurface3 *attachment)
1998 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1999 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2001 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2003 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2006 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
2007 DWORD flags, IDirectDrawSurface2 *attachment)
2009 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2010 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2012 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2014 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2017 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
2018 DWORD flags, IDirectDrawSurface *attachment)
2020 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2021 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2023 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2025 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2028 /*****************************************************************************
2029 * IDirectDrawSurface7::AddOverlayDirtyRect
2031 * "This method is not currently implemented"
2033 * Params:
2034 * Rect: ?
2036 * Returns:
2037 * DDERR_UNSUPPORTED
2039 *****************************************************************************/
2040 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
2042 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
2044 return DDERR_UNSUPPORTED; /* unchecked */
2047 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
2049 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2051 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2053 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2056 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
2058 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2060 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2062 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2065 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
2067 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2069 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2071 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2074 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
2076 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2078 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2080 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2083 /*****************************************************************************
2084 * IDirectDrawSurface7::GetDC
2086 * Returns a GDI device context for the surface
2088 * Params:
2089 * hdc: Address of a HDC variable to store the dc to
2091 * Returns:
2092 * DD_OK on success
2093 * DDERR_INVALIDPARAMS if hdc is NULL
2094 * For details, see IWineD3DSurface::GetDC
2096 *****************************************************************************/
2097 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
2099 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2100 HRESULT hr = DD_OK;
2102 TRACE("iface %p, dc %p.\n", iface, hdc);
2104 if(!hdc)
2105 return DDERR_INVALIDPARAMS;
2107 wined3d_mutex_lock();
2108 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2109 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE);
2110 if (SUCCEEDED(hr))
2111 hr = wined3d_surface_getdc(surface->wined3d_surface, hdc);
2113 if (SUCCEEDED(hr) && format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
2115 const struct ddraw_palette *palette;
2117 if (surface->palette)
2118 palette = surface->palette;
2119 else if (surface->ddraw->primary)
2120 palette = surface->ddraw->primary->palette;
2121 else
2122 palette = NULL;
2124 if (palette)
2125 wined3d_palette_apply_to_dc(palette->wineD3DPalette, *hdc);
2128 wined3d_mutex_unlock();
2129 switch(hr)
2131 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
2132 * touch *hdc
2134 case WINED3DERR_INVALIDCALL:
2135 if(hdc) *hdc = NULL;
2136 return DDERR_INVALIDPARAMS;
2138 default: return hr;
2142 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
2144 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2146 TRACE("iface %p, dc %p.\n", iface, dc);
2148 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2151 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
2153 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2155 TRACE("iface %p, dc %p.\n", iface, dc);
2157 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2160 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2162 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2164 TRACE("iface %p, dc %p.\n", iface, dc);
2166 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2169 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2171 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2173 TRACE("iface %p, dc %p.\n", iface, dc);
2175 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2178 /*****************************************************************************
2179 * IDirectDrawSurface7::ReleaseDC
2181 * Releases the DC that was constructed with GetDC
2183 * Params:
2184 * hdc: HDC to release
2186 * Returns:
2187 * DD_OK on success
2188 * For more details, see IWineD3DSurface::ReleaseDC
2190 *****************************************************************************/
2191 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2193 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2194 HRESULT hr;
2196 TRACE("iface %p, dc %p.\n", iface, hdc);
2198 wined3d_mutex_lock();
2199 hr = wined3d_surface_releasedc(surface->wined3d_surface, hdc);
2200 if (SUCCEEDED(hr) && (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2201 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
2202 wined3d_mutex_unlock();
2204 return hr;
2207 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2209 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2211 TRACE("iface %p, dc %p.\n", iface, dc);
2213 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2216 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2218 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2220 TRACE("iface %p, dc %p.\n", iface, dc);
2222 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2225 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2227 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2229 TRACE("iface %p, dc %p.\n", iface, dc);
2231 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2234 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2236 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2238 TRACE("iface %p, dc %p.\n", iface, dc);
2240 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2243 /*****************************************************************************
2244 * IDirectDrawSurface7::GetCaps
2246 * Returns the surface's caps
2248 * Params:
2249 * Caps: Address to write the caps to
2251 * Returns:
2252 * DD_OK on success
2253 * DDERR_INVALIDPARAMS if Caps is NULL
2255 *****************************************************************************/
2256 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2258 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2260 TRACE("iface %p, caps %p.\n", iface, Caps);
2262 if(!Caps)
2263 return DDERR_INVALIDPARAMS;
2265 *Caps = surface->surface_desc.ddsCaps;
2267 return DD_OK;
2270 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2272 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2274 TRACE("iface %p, caps %p.\n", iface, caps);
2276 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2279 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2281 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2282 DDSCAPS2 caps2;
2283 HRESULT hr;
2285 TRACE("iface %p, caps %p.\n", iface, caps);
2287 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2288 if (FAILED(hr)) return hr;
2290 caps->dwCaps = caps2.dwCaps;
2291 return hr;
2294 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2296 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2297 DDSCAPS2 caps2;
2298 HRESULT hr;
2300 TRACE("iface %p, caps %p.\n", iface, caps);
2302 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2303 if (FAILED(hr)) return hr;
2305 caps->dwCaps = caps2.dwCaps;
2306 return hr;
2309 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2311 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2312 DDSCAPS2 caps2;
2313 HRESULT hr;
2315 TRACE("iface %p, caps %p.\n", iface, caps);
2317 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2318 if (FAILED(hr)) return hr;
2320 caps->dwCaps = caps2.dwCaps;
2321 return hr;
2324 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD priority)
2326 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2327 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2328 HRESULT hr;
2329 struct wined3d_resource *resource;
2331 TRACE("iface %p, priority %u.\n", iface, priority);
2333 wined3d_mutex_lock();
2334 /* No need to check for offscreen plain surfaces or mipmap sublevels. SetPriority
2335 * calls on such surfaces segfault on Windows. */
2336 if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed))
2338 WARN("Called on non-managed texture returning DDERR_INVALIDPARAMS.\n");
2339 hr = DDERR_INVALIDPARAMS;
2341 else
2343 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2344 wined3d_resource_set_priority(resource, priority);
2345 hr = DD_OK;
2347 wined3d_mutex_unlock();
2349 return hr;
2352 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *priority)
2354 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2355 const struct wined3d_resource *resource;
2356 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2357 HRESULT hr;
2359 TRACE("iface %p, priority %p.\n", iface, priority);
2361 wined3d_mutex_lock();
2362 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN)
2364 WARN("Called on offscreenplain surface, returning DDERR_INVALIDOBJECT.\n");
2365 hr = DDERR_INVALIDOBJECT;
2367 else if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed) || !surface->wined3d_texture)
2369 WARN("Called on non-managed texture or mipmap sublevel, returning DDERR_INVALIDPARAMS.\n");
2370 hr = DDERR_INVALIDPARAMS;
2372 else
2374 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2375 *priority = wined3d_resource_get_priority(resource);
2376 hr = DD_OK;
2378 wined3d_mutex_unlock();
2380 return hr;
2383 /*****************************************************************************
2384 * IDirectDrawSurface7::SetPrivateData
2386 * Stores some data in the surface that is intended for the application's
2387 * use.
2389 * Params:
2390 * tag: GUID that identifies the data
2391 * Data: Pointer to the private data
2392 * Size: Size of the private data
2393 * Flags: Some flags
2395 * Returns:
2396 * D3D_OK on success
2397 * For more details, see IWineD3DSurface::SetPrivateData
2399 *****************************************************************************/
2400 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2401 REFGUID tag, void *data, DWORD size, DWORD flags)
2403 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2404 HRESULT hr;
2406 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2407 iface, debugstr_guid(tag), data, size, flags);
2409 if (!data)
2411 WARN("data is NULL, returning DDERR_INVALIDPARAMS.\n");
2412 return DDERR_INVALIDPARAMS;
2415 wined3d_mutex_lock();
2416 hr = wined3d_private_store_set_private_data(&surface->private_store, tag, data, size, flags);
2417 wined3d_mutex_unlock();
2418 return hr_ddraw_from_wined3d(hr);
2421 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2422 REFGUID tag, void *data, DWORD size, DWORD flags)
2424 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2426 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2427 iface, debugstr_guid(tag), data, size, flags);
2429 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2432 /*****************************************************************************
2433 * IDirectDrawSurface7::GetPrivateData
2435 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2437 * Params:
2438 * tag: GUID of the data to return
2439 * Data: Address where to write the data to
2440 * Size: Size of the buffer at Data
2442 * Returns:
2443 * DD_OK on success
2444 * DDERR_INVALIDPARAMS if Data is NULL
2445 * For more details, see IWineD3DSurface::GetPrivateData
2447 *****************************************************************************/
2448 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *data, DWORD *size)
2450 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2451 const struct wined3d_private_data *stored_data;
2452 HRESULT hr;
2454 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2455 iface, debugstr_guid(tag), data, size);
2457 wined3d_mutex_lock();
2458 stored_data = wined3d_private_store_get_private_data(&surface->private_store, tag);
2459 if (!stored_data)
2461 hr = DDERR_NOTFOUND;
2462 goto done;
2464 if (!size)
2466 hr = DDERR_INVALIDPARAMS;
2467 goto done;
2469 if (*size < stored_data->size)
2471 *size = stored_data->size;
2472 hr = DDERR_MOREDATA;
2473 goto done;
2475 if (!data)
2477 hr = DDERR_INVALIDPARAMS;
2478 goto done;
2481 *size = stored_data->size;
2482 memcpy(data, stored_data->content.data, stored_data->size);
2483 hr = DD_OK;
2485 done:
2486 wined3d_mutex_unlock();
2487 return hr;
2490 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2492 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2494 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2495 iface, debugstr_guid(tag), data, size);
2497 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2500 /*****************************************************************************
2501 * IDirectDrawSurface7::FreePrivateData
2503 * Frees private data stored in the surface
2505 * Params:
2506 * tag: Tag of the data to free
2508 * Returns:
2509 * D3D_OK on success
2510 * For more details, see IWineD3DSurface::FreePrivateData
2512 *****************************************************************************/
2513 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2515 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2516 struct wined3d_private_data *entry;
2518 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2520 wined3d_mutex_lock();
2521 entry = wined3d_private_store_get_private_data(&surface->private_store, tag);
2522 if (!entry)
2524 wined3d_mutex_unlock();
2525 return DDERR_NOTFOUND;
2528 wined3d_private_store_free_private_data(&surface->private_store, entry);
2529 wined3d_mutex_unlock();
2531 return DD_OK;
2534 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2536 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2538 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2540 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2543 /*****************************************************************************
2544 * IDirectDrawSurface7::PageLock
2546 * Prevents a sysmem surface from being paged out
2548 * Params:
2549 * Flags: Not used, must be 0(unchecked)
2551 * Returns:
2552 * DD_OK, because it's a stub
2554 *****************************************************************************/
2555 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2557 TRACE("iface %p, flags %#x.\n", iface, Flags);
2559 /* This is Windows memory management related - we don't need this */
2560 return DD_OK;
2563 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2565 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2567 TRACE("iface %p, flags %#x.\n", iface, flags);
2569 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2572 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2574 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2576 TRACE("iface %p, flags %#x.\n", iface, flags);
2578 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2581 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2583 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2585 TRACE("iface %p, flags %#x.\n", iface, flags);
2587 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2590 /*****************************************************************************
2591 * IDirectDrawSurface7::PageUnlock
2593 * Allows a sysmem surface to be paged out
2595 * Params:
2596 * Flags: Not used, must be 0(unchecked)
2598 * Returns:
2599 * DD_OK, because it's a stub
2601 *****************************************************************************/
2602 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2604 TRACE("iface %p, flags %#x.\n", iface, Flags);
2606 return DD_OK;
2609 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2611 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2613 TRACE("iface %p, flags %#x.\n", iface, flags);
2615 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2618 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2620 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2622 TRACE("iface %p, flags %#x.\n", iface, flags);
2624 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2627 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2629 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2631 TRACE("iface %p, flags %#x.\n", iface, flags);
2633 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2636 /*****************************************************************************
2637 * IDirectDrawSurface7::BltBatch
2639 * An unimplemented function
2641 * Params:
2644 * Returns:
2645 * DDERR_UNSUPPORTED
2647 *****************************************************************************/
2648 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2650 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2652 /* MSDN: "not currently implemented" */
2653 return DDERR_UNSUPPORTED;
2656 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2658 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2660 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2662 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2665 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2667 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2669 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2671 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2674 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2676 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2678 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2680 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2683 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2685 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2687 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2689 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2692 /*****************************************************************************
2693 * IDirectDrawSurface7::EnumAttachedSurfaces
2695 * Enumerates all surfaces attached to this surface
2697 * Params:
2698 * context: Pointer to pass unmodified to the callback
2699 * cb: Callback function to call for each surface
2701 * Returns:
2702 * DD_OK on success
2703 * DDERR_INVALIDPARAMS if cb is NULL
2705 *****************************************************************************/
2706 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2707 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2709 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2710 struct ddraw_surface *surf;
2711 DDSURFACEDESC2 desc;
2712 int i;
2714 /* Attached surfaces aren't handled in WineD3D */
2715 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2717 if(!cb)
2718 return DDERR_INVALIDPARAMS;
2720 wined3d_mutex_lock();
2722 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2724 surf = surface->complex_array[i];
2725 if(!surf) break;
2727 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2728 desc = surf->surface_desc;
2729 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2730 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2732 wined3d_mutex_unlock();
2733 return DD_OK;
2737 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2739 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2740 desc = surf->surface_desc;
2741 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2742 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2744 wined3d_mutex_unlock();
2745 return DD_OK;
2749 TRACE(" end of enumeration.\n");
2751 wined3d_mutex_unlock();
2753 return DD_OK;
2756 struct callback_info2
2758 LPDDENUMSURFACESCALLBACK2 callback;
2759 void *context;
2762 struct callback_info
2764 LPDDENUMSURFACESCALLBACK callback;
2765 void *context;
2768 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2770 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2771 const struct callback_info2 *info = context;
2773 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
2774 ddraw_surface7_Release(surface);
2776 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
2779 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2781 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2782 const struct callback_info *info = context;
2784 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
2785 ddraw_surface7_Release(surface);
2787 /* FIXME: Check surface_test.dwSize */
2788 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2789 (DDSURFACEDESC *)surface_desc, info->context);
2792 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2793 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2795 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2796 struct callback_info2 info;
2798 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2800 info.callback = callback;
2801 info.context = context;
2803 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2804 &info, EnumCallback2);
2807 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2808 void *context, LPDDENUMSURFACESCALLBACK callback)
2810 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2811 struct callback_info info;
2813 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2815 info.callback = callback;
2816 info.context = context;
2818 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2819 &info, EnumCallback);
2822 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2823 void *context, LPDDENUMSURFACESCALLBACK callback)
2825 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2826 struct callback_info info;
2828 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2830 info.callback = callback;
2831 info.context = context;
2833 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2834 &info, EnumCallback);
2837 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2838 void *context, LPDDENUMSURFACESCALLBACK callback)
2840 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2841 struct callback_info info;
2843 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2845 info.callback = callback;
2846 info.context = context;
2848 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2849 &info, EnumCallback);
2852 /*****************************************************************************
2853 * IDirectDrawSurface7::EnumOverlayZOrders
2855 * "Enumerates the overlay surfaces on the specified destination"
2857 * Params:
2858 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2859 * context: context to pass back to the callback
2860 * cb: callback function to call for each enumerated surface
2862 * Returns:
2863 * DD_OK, because it's a stub
2865 *****************************************************************************/
2866 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2867 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2869 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2871 return DD_OK;
2874 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2875 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2877 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2878 struct callback_info2 info;
2880 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2882 info.callback = callback;
2883 info.context = context;
2885 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2886 flags, &info, EnumCallback2);
2889 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2890 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2892 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2893 struct callback_info info;
2895 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2897 info.callback = callback;
2898 info.context = context;
2900 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2901 flags, &info, EnumCallback);
2904 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2905 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2907 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2908 struct callback_info info;
2910 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2912 info.callback = callback;
2913 info.context = context;
2915 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2916 flags, &info, EnumCallback);
2919 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2920 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2922 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2923 struct callback_info info;
2925 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2927 info.callback = callback;
2928 info.context = context;
2930 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2931 flags, &info, EnumCallback);
2934 /*****************************************************************************
2935 * IDirectDrawSurface7::GetBltStatus
2937 * Returns the blitting status
2939 * Params:
2940 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2942 * Returns:
2943 * See IWineD3DSurface::Blt
2945 *****************************************************************************/
2946 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2948 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2949 HRESULT hr;
2951 TRACE("iface %p, flags %#x.\n", iface, Flags);
2953 wined3d_mutex_lock();
2954 hr = wined3d_surface_get_blt_status(surface->wined3d_surface, Flags);
2955 wined3d_mutex_unlock();
2956 switch(hr)
2958 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2959 default: return hr;
2963 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2965 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2967 TRACE("iface %p, flags %#x.\n", iface, flags);
2969 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2972 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2974 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2976 TRACE("iface %p, flags %#x.\n", iface, flags);
2978 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2981 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2983 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2985 TRACE("iface %p, flags %#x.\n", iface, flags);
2987 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2990 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
2992 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2994 TRACE("iface %p, flags %#x.\n", iface, flags);
2996 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2999 /*****************************************************************************
3000 * IDirectDrawSurface7::GetColorKey
3002 * Returns the color key assigned to the surface
3004 * Params:
3005 * Flags: Some flags
3006 * CKey: Address to store the key to
3008 * Returns:
3009 * DD_OK on success
3010 * DDERR_INVALIDPARAMS if CKey is NULL
3012 *****************************************************************************/
3013 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
3015 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3017 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
3019 if(!CKey)
3020 return DDERR_INVALIDPARAMS;
3022 wined3d_mutex_lock();
3024 switch (Flags)
3026 case DDCKEY_DESTBLT:
3027 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
3029 wined3d_mutex_unlock();
3030 return DDERR_NOCOLORKEY;
3032 *CKey = This->surface_desc.ddckCKDestBlt;
3033 break;
3035 case DDCKEY_DESTOVERLAY:
3036 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
3038 wined3d_mutex_unlock();
3039 return DDERR_NOCOLORKEY;
3041 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
3042 break;
3044 case DDCKEY_SRCBLT:
3045 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
3047 wined3d_mutex_unlock();
3048 return DDERR_NOCOLORKEY;
3050 *CKey = This->surface_desc.ddckCKSrcBlt;
3051 break;
3053 case DDCKEY_SRCOVERLAY:
3054 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
3056 wined3d_mutex_unlock();
3057 return DDERR_NOCOLORKEY;
3059 *CKey = This->surface_desc.ddckCKSrcOverlay;
3060 break;
3062 default:
3063 wined3d_mutex_unlock();
3064 return DDERR_INVALIDPARAMS;
3067 wined3d_mutex_unlock();
3069 return DD_OK;
3072 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
3074 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3076 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3078 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3081 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
3083 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3085 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3087 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3090 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
3092 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3094 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3096 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3099 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
3101 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3103 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3105 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3108 /*****************************************************************************
3109 * IDirectDrawSurface7::GetFlipStatus
3111 * Returns the flipping status of the surface
3113 * Params:
3114 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
3116 * Returns:
3117 * See IWineD3DSurface::GetFlipStatus
3119 *****************************************************************************/
3120 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3122 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3123 HRESULT hr;
3125 TRACE("iface %p, flags %#x.\n", iface, Flags);
3127 wined3d_mutex_lock();
3128 hr = wined3d_surface_get_flip_status(surface->wined3d_surface, Flags);
3129 wined3d_mutex_unlock();
3131 switch(hr)
3133 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3134 default: return hr;
3138 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
3140 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3142 TRACE("iface %p, flags %#x.\n", iface, flags);
3144 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3147 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
3149 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3151 TRACE("iface %p, flags %#x.\n", iface, flags);
3153 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3156 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
3158 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3160 TRACE("iface %p, flags %#x.\n", iface, flags);
3162 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3165 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
3167 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3169 TRACE("iface %p, flags %#x.\n", iface, flags);
3171 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3174 /*****************************************************************************
3175 * IDirectDrawSurface7::GetOverlayPosition
3177 * Returns the display coordinates of a visible and active overlay surface
3179 * Params:
3183 * Returns:
3184 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3185 *****************************************************************************/
3186 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
3188 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3189 HRESULT hr;
3191 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
3193 wined3d_mutex_lock();
3194 hr = wined3d_surface_get_overlay_position(surface->wined3d_surface, X, Y);
3195 wined3d_mutex_unlock();
3197 return hr;
3200 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3202 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3204 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3206 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3209 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3211 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3213 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3215 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3218 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3220 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3222 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3224 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3227 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3229 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3231 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3233 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3236 /*****************************************************************************
3237 * IDirectDrawSurface7::GetPixelFormat
3239 * Returns the pixel format of the Surface
3241 * Params:
3242 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3243 * format should be written
3245 * Returns:
3246 * DD_OK on success
3247 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3249 *****************************************************************************/
3250 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3252 /* What is DDERR_INVALIDSURFACETYPE for here? */
3253 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3255 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3257 if(!PixelFormat)
3258 return DDERR_INVALIDPARAMS;
3260 wined3d_mutex_lock();
3261 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3262 wined3d_mutex_unlock();
3264 return DD_OK;
3267 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3269 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3271 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3273 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3276 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3278 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3280 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3282 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3285 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3287 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3289 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3291 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3294 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3296 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3298 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3300 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3303 /*****************************************************************************
3304 * IDirectDrawSurface7::GetSurfaceDesc
3306 * Returns the description of this surface
3308 * Params:
3309 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3310 * surface desc
3312 * Returns:
3313 * DD_OK on success
3314 * DDERR_INVALIDPARAMS if DDSD is NULL
3316 *****************************************************************************/
3317 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3319 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3321 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3323 if(!DDSD)
3324 return DDERR_INVALIDPARAMS;
3326 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3328 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3329 return DDERR_INVALIDPARAMS;
3332 wined3d_mutex_lock();
3333 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3334 TRACE("Returning surface desc:\n");
3335 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3336 wined3d_mutex_unlock();
3338 return DD_OK;
3341 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3343 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3345 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3347 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3350 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3352 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3354 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3356 if (!surface_desc) return DDERR_INVALIDPARAMS;
3358 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3360 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3361 return DDERR_INVALIDPARAMS;
3364 wined3d_mutex_lock();
3365 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3366 TRACE("Returning surface desc:\n");
3367 if (TRACE_ON(ddraw))
3369 /* DDRAW_dump_surface_desc handles the smaller size */
3370 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3372 wined3d_mutex_unlock();
3374 return DD_OK;
3377 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3379 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3381 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3383 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3386 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3388 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3390 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3392 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3395 /*****************************************************************************
3396 * IDirectDrawSurface7::Initialize
3398 * Initializes the surface. This is a no-op in Wine
3400 * Params:
3401 * DD: Pointer to an DirectDraw interface
3402 * DDSD: Surface description for initialization
3404 * Returns:
3405 * DDERR_ALREADYINITIALIZED
3407 *****************************************************************************/
3408 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3409 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3411 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3413 return DDERR_ALREADYINITIALIZED;
3416 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3417 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3419 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3421 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3423 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3424 ddraw, surface_desc);
3427 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3428 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3430 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3431 DDSURFACEDESC2 surface_desc2;
3433 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3435 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3436 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3437 ddraw, surface_desc ? &surface_desc2 : NULL);
3440 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3441 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3443 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3444 DDSURFACEDESC2 surface_desc2;
3446 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3448 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3449 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3450 ddraw, surface_desc ? &surface_desc2 : NULL);
3453 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3454 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3456 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3457 DDSURFACEDESC2 surface_desc2;
3459 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3461 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3462 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3463 ddraw, surface_desc ? &surface_desc2 : NULL);
3466 /*****************************************************************************
3467 * IDirect3DTexture1::Initialize
3469 * The sdk says it's not implemented
3471 * Params:
3474 * Returns
3475 * DDERR_UNSUPPORTED
3477 *****************************************************************************/
3478 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3479 IDirect3DDevice *device, IDirectDrawSurface *surface)
3481 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3483 return DDERR_UNSUPPORTED; /* Unchecked */
3486 /*****************************************************************************
3487 * IDirectDrawSurface7::IsLost
3489 * Checks if the surface is lost
3491 * Returns:
3492 * DD_OK, if the surface is usable
3493 * DDERR_ISLOST if the surface is lost
3494 * See IWineD3DSurface::IsLost for more details
3496 *****************************************************************************/
3497 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3499 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3500 HRESULT hr;
3502 TRACE("iface %p.\n", iface);
3504 wined3d_mutex_lock();
3505 hr = wined3d_surface_is_lost(surface->wined3d_surface);
3506 wined3d_mutex_unlock();
3508 switch(hr)
3510 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
3511 * WineD3D uses the same error for surfaces
3513 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
3514 default: return hr;
3518 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3520 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3522 TRACE("iface %p.\n", iface);
3524 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3527 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3529 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3531 TRACE("iface %p.\n", iface);
3533 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3536 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3538 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3540 TRACE("iface %p.\n", iface);
3542 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3545 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3547 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3549 TRACE("iface %p.\n", iface);
3551 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3554 /*****************************************************************************
3555 * IDirectDrawSurface7::Restore
3557 * Restores a lost surface. This makes the surface usable again, but
3558 * doesn't reload its old contents
3560 * Returns:
3561 * DD_OK on success
3562 * See IWineD3DSurface::Restore for more details
3564 *****************************************************************************/
3565 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3567 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3568 HRESULT hr;
3570 TRACE("iface %p.\n", iface);
3572 wined3d_mutex_lock();
3573 hr = wined3d_surface_restore(surface->wined3d_surface);
3574 wined3d_mutex_unlock();
3576 return hr;
3579 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3581 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3583 TRACE("iface %p.\n", iface);
3585 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3588 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3590 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3592 TRACE("iface %p.\n", iface);
3594 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3597 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3599 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3601 TRACE("iface %p.\n", iface);
3603 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3606 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3608 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3610 TRACE("iface %p.\n", iface);
3612 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3615 /*****************************************************************************
3616 * IDirectDrawSurface7::SetOverlayPosition
3618 * Changes the display coordinates of an overlay surface
3620 * Params:
3621 * X:
3622 * Y:
3624 * Returns:
3625 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3626 *****************************************************************************/
3627 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
3629 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3630 HRESULT hr;
3632 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
3634 wined3d_mutex_lock();
3635 hr = wined3d_surface_set_overlay_position(surface->wined3d_surface, X, Y);
3636 wined3d_mutex_unlock();
3638 return hr;
3641 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3643 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3645 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3647 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3650 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3652 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3654 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3656 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3659 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3661 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3663 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3665 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3668 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3670 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3672 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3674 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3677 /*****************************************************************************
3678 * IDirectDrawSurface7::UpdateOverlay
3680 * Modifies the attributes of an overlay surface.
3682 * Params:
3683 * SrcRect: The section of the source being used for the overlay
3684 * DstSurface: Address of the surface that is overlaid
3685 * DstRect: Place of the overlay
3686 * Flags: some DDOVER_* flags
3688 * Returns:
3689 * DDERR_UNSUPPORTED, because we don't support overlays
3691 *****************************************************************************/
3692 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
3693 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
3695 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3696 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(DstSurface);
3697 HRESULT hr;
3699 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3700 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
3702 wined3d_mutex_lock();
3703 hr = wined3d_surface_update_overlay(src_impl->wined3d_surface, SrcRect,
3704 dst_impl ? dst_impl->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
3705 wined3d_mutex_unlock();
3707 switch(hr) {
3708 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3709 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3710 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3711 default:
3712 return hr;
3716 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3717 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3719 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3720 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3722 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3723 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3725 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3726 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3729 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3730 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3732 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
3733 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3735 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3736 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3738 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3739 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3742 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3743 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3745 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
3746 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3748 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3749 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3751 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3752 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3755 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3756 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3758 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
3759 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3761 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3762 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3764 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3765 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3768 /*****************************************************************************
3769 * IDirectDrawSurface7::UpdateOverlayDisplay
3771 * The DX7 sdk says that it's not implemented
3773 * Params:
3774 * Flags: ?
3776 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3778 *****************************************************************************/
3779 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3781 TRACE("iface %p, flags %#x.\n", iface, Flags);
3783 return DDERR_UNSUPPORTED;
3786 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3788 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3790 TRACE("iface %p, flags %#x.\n", iface, flags);
3792 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3795 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3797 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3799 TRACE("iface %p, flags %#x.\n", iface, flags);
3801 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3804 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3806 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3808 TRACE("iface %p, flags %#x.\n", iface, flags);
3810 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3813 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3815 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3817 TRACE("iface %p, flags %#x.\n", iface, flags);
3819 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3822 /*****************************************************************************
3823 * IDirectDrawSurface7::UpdateOverlayZOrder
3825 * Sets an overlay's Z order
3827 * Params:
3828 * Flags: DDOVERZ_* flags
3829 * DDSRef: Defines the relative position in the overlay chain
3831 * Returns:
3832 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3834 *****************************************************************************/
3835 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3836 DWORD Flags, IDirectDrawSurface7 *DDSRef)
3838 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3839 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface7(DDSRef);
3840 HRESULT hr;
3842 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
3844 wined3d_mutex_lock();
3845 hr = wined3d_surface_update_overlay_z_order(surface->wined3d_surface,
3846 Flags, reference_impl ? reference_impl->wined3d_surface : NULL);
3847 wined3d_mutex_unlock();
3849 return hr;
3852 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3853 DWORD flags, IDirectDrawSurface4 *reference)
3855 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3856 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3858 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3860 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3861 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3864 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3865 DWORD flags, IDirectDrawSurface3 *reference)
3867 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3868 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3870 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3872 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3873 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3876 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3877 DWORD flags, IDirectDrawSurface2 *reference)
3879 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3880 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3882 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3884 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3885 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3888 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3889 DWORD flags, IDirectDrawSurface *reference)
3891 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3892 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3894 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3896 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3897 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3900 /*****************************************************************************
3901 * IDirectDrawSurface7::GetDDInterface
3903 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3904 * surface belongs to
3906 * Params:
3907 * DD: Address to write the interface pointer to
3909 * Returns:
3910 * DD_OK on success
3911 * DDERR_INVALIDPARAMS if DD is NULL
3913 *****************************************************************************/
3914 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3916 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3918 TRACE("iface %p, ddraw %p.\n", iface, DD);
3920 if(!DD)
3921 return DDERR_INVALIDPARAMS;
3923 switch(This->version)
3925 case 7:
3926 *DD = &This->ddraw->IDirectDraw7_iface;
3927 break;
3929 case 4:
3930 *DD = &This->ddraw->IDirectDraw4_iface;
3931 break;
3933 case 2:
3934 *DD = &This->ddraw->IDirectDraw2_iface;
3935 break;
3937 case 1:
3938 *DD = &This->ddraw->IDirectDraw_iface;
3939 break;
3942 IUnknown_AddRef((IUnknown *)*DD);
3944 return DD_OK;
3947 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3949 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3951 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3953 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3956 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3958 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3960 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3962 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3965 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3967 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3969 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3971 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3974 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3976 TRACE("iface %p.\n", iface);
3978 return DD_OK;
3981 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
3983 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3985 TRACE("iface %p.\n", iface);
3987 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
3990 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
3992 TRACE("iface %p, value %p.\n", iface, pValue);
3994 *pValue = 0;
3996 return DD_OK;
3999 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
4001 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4003 TRACE("iface %p, value %p.\n", iface, pValue);
4005 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
4008 /*****************************************************************************
4009 * IDirectDrawSurface7::SetLOD
4011 * Sets the level of detail of a texture
4013 * Params:
4014 * MaxLOD: LOD to set
4016 * Returns:
4017 * DD_OK on success
4018 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4020 *****************************************************************************/
4021 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
4023 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4024 HRESULT hr;
4026 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
4028 wined3d_mutex_lock();
4029 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4031 wined3d_mutex_unlock();
4032 return DDERR_INVALIDOBJECT;
4035 if (!surface->wined3d_texture)
4037 ERR("The ddraw surface has no wined3d texture.\n");
4038 wined3d_mutex_unlock();
4039 return DDERR_INVALIDOBJECT;
4042 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
4043 wined3d_mutex_unlock();
4045 return hr;
4048 /*****************************************************************************
4049 * IDirectDrawSurface7::GetLOD
4051 * Returns the level of detail of a Direct3D texture
4053 * Params:
4054 * MaxLOD: Address to write the LOD to
4056 * Returns:
4057 * DD_OK on success
4058 * DDERR_INVALIDPARAMS if MaxLOD is NULL
4059 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4061 *****************************************************************************/
4062 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4064 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4066 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4068 if(!MaxLOD)
4069 return DDERR_INVALIDPARAMS;
4071 wined3d_mutex_lock();
4072 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4074 wined3d_mutex_unlock();
4075 return DDERR_INVALIDOBJECT;
4078 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4079 wined3d_mutex_unlock();
4081 return DD_OK;
4084 /*****************************************************************************
4085 * IDirectDrawSurface7::BltFast
4087 * Performs a fast Blit.
4089 * Params:
4090 * dstx: The x coordinate to blit to on the destination
4091 * dsty: The y coordinate to blit to on the destination
4092 * Source: The source surface
4093 * rsrc: The source rectangle
4094 * trans: Type of transfer. Some DDBLTFAST_* flags
4096 * Returns:
4097 * DD_OK on success
4098 * For more details, see IWineD3DSurface::BltFast
4100 *****************************************************************************/
4101 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
4102 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
4104 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4105 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface7(Source);
4106 DWORD src_w, src_h, dst_w, dst_h;
4107 HRESULT hr = DD_OK;
4108 DWORD flags = 0;
4109 RECT dst_rect;
4111 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4112 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
4114 dst_w = This->surface_desc.dwWidth;
4115 dst_h = This->surface_desc.dwHeight;
4117 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
4118 * in that case
4120 if(rsrc)
4122 src_w = rsrc->right - rsrc->left;
4123 src_h = rsrc->bottom - rsrc->top;
4125 else
4127 src_w = src->surface_desc.dwWidth;
4128 src_h = src->surface_desc.dwHeight;
4131 if (src_w > dst_w || dstx > dst_w - src_w
4132 || src_h > dst_h || dsty > dst_h - src_h)
4134 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4135 return DDERR_INVALIDRECT;
4138 SetRect(&dst_rect, dstx, dsty, dstx + src_w, dsty + src_h);
4139 if (trans & DDBLTFAST_SRCCOLORKEY)
4140 flags |= WINEDDBLT_KEYSRC;
4141 if (trans & DDBLTFAST_DESTCOLORKEY)
4142 flags |= WINEDDBLT_KEYDEST;
4143 if (trans & DDBLTFAST_WAIT)
4144 flags |= WINEDDBLT_WAIT;
4145 if (trans & DDBLTFAST_DONOTWAIT)
4146 flags |= WINEDDBLT_DONOTWAIT;
4148 wined3d_mutex_lock();
4149 if (This->clipper)
4151 wined3d_mutex_unlock();
4152 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4153 return DDERR_BLTFASTCANTCLIP;
4156 if (src->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4157 hr = ddraw_surface_update_frontbuffer(src, rsrc, TRUE);
4158 if (SUCCEEDED(hr))
4159 hr = wined3d_surface_blt(This->wined3d_surface, &dst_rect,
4160 src->wined3d_surface, rsrc, flags, NULL, WINED3D_TEXF_POINT);
4161 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4162 hr = ddraw_surface_update_frontbuffer(This, &dst_rect, FALSE);
4163 wined3d_mutex_unlock();
4165 switch(hr)
4167 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4168 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
4169 default: return hr;
4173 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4174 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4176 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4177 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4179 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4180 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4182 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4183 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4186 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4187 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4189 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4190 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4192 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4193 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4195 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4196 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4199 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4200 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4202 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4203 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4205 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4206 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4208 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4209 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4212 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4213 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4215 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4216 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4218 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4219 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4221 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4222 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4225 /*****************************************************************************
4226 * IDirectDrawSurface7::GetClipper
4228 * Returns the IDirectDrawClipper interface of the clipper assigned to this
4229 * surface
4231 * Params:
4232 * Clipper: Address to store the interface pointer at
4234 * Returns:
4235 * DD_OK on success
4236 * DDERR_INVALIDPARAMS if Clipper is NULL
4237 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
4239 *****************************************************************************/
4240 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
4242 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4244 TRACE("iface %p, clipper %p.\n", iface, Clipper);
4246 if (!Clipper)
4247 return DDERR_INVALIDPARAMS;
4249 wined3d_mutex_lock();
4250 if (!surface->clipper)
4252 wined3d_mutex_unlock();
4253 return DDERR_NOCLIPPERATTACHED;
4256 *Clipper = (IDirectDrawClipper *)surface->clipper;
4257 IDirectDrawClipper_AddRef(*Clipper);
4258 wined3d_mutex_unlock();
4260 return DD_OK;
4263 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4265 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4267 TRACE("iface %p, clipper %p.\n", iface, clipper);
4269 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4272 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4274 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4276 TRACE("iface %p, clipper %p.\n", iface, clipper);
4278 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4281 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4283 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4285 TRACE("iface %p, clipper %p.\n", iface, clipper);
4287 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4290 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4292 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4294 TRACE("iface %p, clipper %p.\n", iface, clipper);
4296 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4299 /*****************************************************************************
4300 * IDirectDrawSurface7::SetClipper
4302 * Sets a clipper for the surface
4304 * Params:
4305 * Clipper: IDirectDrawClipper interface of the clipper to set
4307 * Returns:
4308 * DD_OK on success
4310 *****************************************************************************/
4311 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4312 IDirectDrawClipper *iclipper)
4314 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4315 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4316 struct ddraw_clipper *old_clipper = This->clipper;
4317 HWND clipWindow;
4319 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4321 wined3d_mutex_lock();
4322 if (clipper == This->clipper)
4324 wined3d_mutex_unlock();
4325 return DD_OK;
4328 This->clipper = clipper;
4330 if (clipper != NULL)
4331 IDirectDrawClipper_AddRef(iclipper);
4332 if (old_clipper)
4333 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4335 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4337 clipWindow = NULL;
4338 if(clipper) {
4339 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4342 if (clipWindow)
4344 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4345 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4347 else
4349 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4350 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4354 wined3d_mutex_unlock();
4356 return DD_OK;
4359 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4361 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4363 TRACE("iface %p, clipper %p.\n", iface, clipper);
4365 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4368 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4370 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4372 TRACE("iface %p, clipper %p.\n", iface, clipper);
4374 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4377 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4379 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4381 TRACE("iface %p, clipper %p.\n", iface, clipper);
4383 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4386 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4388 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4390 TRACE("iface %p, clipper %p.\n", iface, clipper);
4392 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4395 /*****************************************************************************
4396 * IDirectDrawSurface7::SetSurfaceDesc
4398 * Sets the surface description. It can override the pixel format, the surface
4399 * memory, ...
4400 * It's not really tested.
4402 * Params:
4403 * DDSD: Pointer to the new surface description to set
4404 * Flags: Some flags
4406 * Returns:
4407 * DD_OK on success
4408 * DDERR_INVALIDPARAMS if DDSD is NULL
4410 *****************************************************************************/
4411 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4413 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4414 HRESULT hr;
4415 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4416 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4417 enum wined3d_format_id format_id;
4418 UINT pitch, width, height;
4420 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4422 if (!DDSD)
4424 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4425 return DDERR_INVALIDPARAMS;
4427 if (Flags)
4429 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4430 return DDERR_INVALIDPARAMS;
4432 if (!(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4433 || This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4434 || This->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4436 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4437 return DDERR_INVALIDSURFACETYPE;
4440 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4441 * for PIXELFORMAT to work */
4442 if (DDSD->dwFlags & ~allowed_flags)
4444 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4445 return DDERR_INVALIDPARAMS;
4447 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4449 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4450 return DDERR_INVALIDPARAMS;
4452 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4454 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4455 return DDERR_INVALIDCAPS;
4457 if (DDSD->dwFlags & DDSD_WIDTH)
4459 if (!(DDSD->dwFlags & DDSD_PITCH))
4461 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4462 return DDERR_INVALIDPARAMS;
4464 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4466 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4467 DDSD->u1.lPitch, DDSD->dwWidth);
4468 return DDERR_INVALIDPARAMS;
4470 if (DDSD->dwWidth != This->surface_desc.dwWidth)
4471 TRACE("Surface width changed from %u to %u.\n", This->surface_desc.dwWidth, DDSD->dwWidth);
4472 if (DDSD->u1.lPitch != This->surface_desc.u1.lPitch)
4473 TRACE("Surface pitch changed from %u to %u.\n", This->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4474 pitch = DDSD->u1.lPitch;
4475 width = DDSD->dwWidth;
4477 else if (DDSD->dwFlags & DDSD_PITCH)
4479 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4480 return DDERR_INVALIDPARAMS;
4482 else
4484 pitch = This->surface_desc.u1.lPitch;
4485 width = This->surface_desc.dwWidth;
4488 if (DDSD->dwFlags & DDSD_HEIGHT)
4490 if (!DDSD->dwHeight)
4492 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4493 return DDERR_INVALIDPARAMS;
4495 if (DDSD->dwHeight != This->surface_desc.dwHeight)
4496 TRACE("Surface height changed from %u to %u.\n", This->surface_desc.dwHeight, DDSD->dwHeight);
4497 height = DDSD->dwHeight;
4499 else
4501 height = This->surface_desc.dwHeight;
4504 wined3d_mutex_lock();
4505 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4507 enum wined3d_format_id current_format_id;
4508 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4510 if (format_id == WINED3DFMT_UNKNOWN)
4512 ERR("Requested to set an unknown pixelformat\n");
4513 wined3d_mutex_unlock();
4514 return DDERR_INVALIDPARAMS;
4516 current_format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4517 if (format_id != current_format_id)
4518 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4520 else
4522 format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4525 if (FAILED(hr = wined3d_texture_update_desc(This->wined3d_texture, width, height,
4526 format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4528 WARN("Failed to update surface desc, hr %#x.\n", hr);
4529 wined3d_mutex_unlock();
4530 return hr_ddraw_from_wined3d(hr);
4533 if (DDSD->dwFlags & DDSD_WIDTH)
4534 This->surface_desc.dwWidth = width;
4535 if (DDSD->dwFlags & DDSD_PITCH)
4536 This->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4537 if (DDSD->dwFlags & DDSD_HEIGHT)
4538 This->surface_desc.dwHeight = height;
4539 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4540 This->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4542 wined3d_mutex_unlock();
4544 return DD_OK;
4547 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4548 DDSURFACEDESC2 *surface_desc, DWORD flags)
4550 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4552 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4554 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4555 surface_desc, flags);
4558 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4559 DDSURFACEDESC *surface_desc, DWORD flags)
4561 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4562 DDSURFACEDESC2 surface_desc2;
4564 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4566 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4567 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4568 surface_desc ? &surface_desc2 : NULL, flags);
4571 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4573 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4574 struct ddraw_palette *palette_impl;
4575 HRESULT hr = DD_OK;
4577 TRACE("iface %p, palette %p.\n", iface, palette);
4579 if (!palette)
4580 return DDERR_INVALIDPARAMS;
4581 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4583 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4584 return DDERR_SURFACELOST;
4587 wined3d_mutex_lock();
4588 if ((palette_impl = surface->palette))
4590 *palette = &palette_impl->IDirectDrawPalette_iface;
4591 IDirectDrawPalette_AddRef(*palette);
4593 else
4595 *palette = NULL;
4596 hr = DDERR_NOPALETTEATTACHED;
4598 wined3d_mutex_unlock();
4600 return hr;
4603 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4605 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4607 TRACE("iface %p, palette %p.\n", iface, palette);
4609 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4612 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4614 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4616 TRACE("iface %p, palette %p.\n", iface, palette);
4618 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4621 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4623 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4625 TRACE("iface %p, palette %p.\n", iface, palette);
4627 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4630 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4632 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4634 TRACE("iface %p, palette %p.\n", iface, palette);
4636 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4639 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4641 DDCOLORKEY fixed_color_key;
4642 HRESULT hr = WINED3D_OK;
4644 wined3d_mutex_lock();
4646 if (color_key)
4648 fixed_color_key = *color_key;
4649 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
4650 if (fixed_color_key.dwColorSpaceHighValue < fixed_color_key.dwColorSpaceLowValue)
4651 fixed_color_key.dwColorSpaceHighValue = fixed_color_key.dwColorSpaceLowValue;
4653 switch (flags & ~DDCKEY_COLORSPACE)
4655 case DDCKEY_DESTBLT:
4656 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4657 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4658 break;
4660 case DDCKEY_DESTOVERLAY:
4661 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4662 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4663 break;
4665 case DDCKEY_SRCOVERLAY:
4666 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4667 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4668 break;
4670 case DDCKEY_SRCBLT:
4671 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4672 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4673 break;
4675 default:
4676 wined3d_mutex_unlock();
4677 return DDERR_INVALIDPARAMS;
4680 else
4682 switch (flags & ~DDCKEY_COLORSPACE)
4684 case DDCKEY_DESTBLT:
4685 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4686 break;
4688 case DDCKEY_DESTOVERLAY:
4689 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4690 break;
4692 case DDCKEY_SRCOVERLAY:
4693 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4694 break;
4696 case DDCKEY_SRCBLT:
4697 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4698 break;
4700 default:
4701 wined3d_mutex_unlock();
4702 return DDERR_INVALIDPARAMS;
4706 if (surface->wined3d_texture)
4707 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags,
4708 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4710 wined3d_mutex_unlock();
4712 return hr_ddraw_from_wined3d(hr);
4715 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4717 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4719 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4721 wined3d_mutex_lock();
4722 if (!surface->wined3d_texture)
4724 wined3d_mutex_unlock();
4725 return DDERR_NOTONMIPMAPSUBLEVEL;
4727 wined3d_mutex_unlock();
4729 return ddraw_surface_set_color_key(surface, flags, color_key);
4732 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4734 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4736 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4738 return ddraw_surface_set_color_key(surface, flags, color_key);
4741 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4743 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4745 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4747 return ddraw_surface_set_color_key(surface, flags, color_key);
4750 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4752 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4754 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4756 return ddraw_surface_set_color_key(surface, flags, color_key);
4759 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4761 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4763 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4765 return ddraw_surface_set_color_key(surface, flags, color_key);
4768 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
4770 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4772 TRACE("iface %p, palette %p.\n", iface, palette);
4774 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4775 return DDERR_NOTONMIPMAPSUBLEVEL;
4776 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4778 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4779 return DDERR_SURFACELOST;
4782 return ddraw_surface_set_palette(surface, palette);
4785 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4787 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4789 TRACE("iface %p, palette %p.\n", iface, palette);
4791 if (IDirectDrawSurface4_IsLost(iface) == DDERR_SURFACELOST)
4793 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4794 return DDERR_SURFACELOST;
4797 return ddraw_surface_set_palette(surface, palette);
4800 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4802 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4804 TRACE("iface %p, palette %p.\n", iface, palette);
4806 if (IDirectDrawSurface3_IsLost(iface) == DDERR_SURFACELOST)
4808 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4809 return DDERR_SURFACELOST;
4812 return ddraw_surface_set_palette(surface, palette);
4815 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4817 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4819 TRACE("iface %p, palette %p.\n", iface, palette);
4821 if (IDirectDrawSurface2_IsLost(iface) == DDERR_SURFACELOST)
4823 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4824 return DDERR_SURFACELOST;
4827 return ddraw_surface_set_palette(surface, palette);
4830 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4832 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4834 TRACE("iface %p, palette %p.\n", iface, palette);
4836 if (IDirectDrawSurface_IsLost(iface) == DDERR_SURFACELOST)
4838 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4839 return DDERR_SURFACELOST;
4842 return ddraw_surface_set_palette(surface, palette);
4845 /**********************************************************
4846 * IDirectDrawGammaControl::GetGammaRamp
4848 * Returns the current gamma ramp for a surface
4850 * Params:
4851 * flags: Ignored
4852 * gamma_ramp: Address to write the ramp to
4854 * Returns:
4855 * DD_OK on success
4856 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4858 **********************************************************/
4859 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4860 DWORD flags, DDGAMMARAMP *gamma_ramp)
4862 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4864 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4866 if (!gamma_ramp)
4868 WARN("Invalid gamma_ramp passed.\n");
4869 return DDERR_INVALIDPARAMS;
4872 wined3d_mutex_lock();
4873 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4875 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4876 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
4878 else
4880 ERR("Not implemented for non-primary surfaces.\n");
4882 wined3d_mutex_unlock();
4884 return DD_OK;
4887 /**********************************************************
4888 * IDirectDrawGammaControl::SetGammaRamp
4890 * Sets the red, green and blue gamma ramps for
4892 * Params:
4893 * flags: Can be DDSGR_CALIBRATE to request calibration
4894 * gamma_ramp: Structure containing the new gamma ramp
4896 * Returns:
4897 * DD_OK on success
4898 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4900 **********************************************************/
4901 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4902 DWORD flags, DDGAMMARAMP *gamma_ramp)
4904 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4906 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4908 if (!gamma_ramp)
4910 WARN("Invalid gamma_ramp passed.\n");
4911 return DDERR_INVALIDPARAMS;
4914 wined3d_mutex_lock();
4915 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4917 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4918 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
4919 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
4921 else
4923 ERR("Not implemented for non-primary surfaces.\n");
4925 wined3d_mutex_unlock();
4927 return DD_OK;
4930 /*****************************************************************************
4931 * IDirect3DTexture2::PaletteChanged
4933 * Informs the texture about a palette change
4935 * Params:
4936 * start: Start index of the change
4937 * count: The number of changed entries
4939 * Returns
4940 * D3D_OK, because it's a stub
4942 *****************************************************************************/
4943 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4945 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4947 return D3D_OK;
4950 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4952 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
4954 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4956 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
4959 /*****************************************************************************
4960 * IDirect3DTexture::Unload
4962 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4965 * Returns:
4966 * DDERR_UNSUPPORTED
4968 *****************************************************************************/
4969 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4971 WARN("iface %p. Not implemented.\n", iface);
4973 return DDERR_UNSUPPORTED;
4976 /*****************************************************************************
4977 * IDirect3DTexture2::GetHandle
4979 * Returns handle for the texture. At the moment, the interface
4980 * to the IWineD3DTexture is used.
4982 * Params:
4983 * device: Device this handle is assigned to
4984 * handle: Address to store the handle at.
4986 * Returns:
4987 * D3D_OK
4989 *****************************************************************************/
4990 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4991 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4993 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
4994 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
4996 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4998 wined3d_mutex_lock();
5000 if (!surface->Handle)
5002 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
5003 if (h == DDRAW_INVALID_HANDLE)
5005 ERR("Failed to allocate a texture handle.\n");
5006 wined3d_mutex_unlock();
5007 return DDERR_OUTOFMEMORY;
5010 surface->Handle = h + 1;
5013 TRACE("Returning handle %08x.\n", surface->Handle);
5014 *handle = surface->Handle;
5016 wined3d_mutex_unlock();
5018 return D3D_OK;
5021 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
5022 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
5024 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5025 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
5027 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5029 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
5030 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
5033 /*****************************************************************************
5034 * get_sub_mimaplevel
5036 * Helper function that returns the next mipmap level
5038 * tex_ptr: Surface of which to return the next level
5040 *****************************************************************************/
5041 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
5043 /* Now go down the mipmap chain to the next surface */
5044 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, {0} };
5045 IDirectDrawSurface7 *next_level;
5046 HRESULT hr;
5048 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
5049 if (FAILED(hr)) return NULL;
5051 ddraw_surface7_Release(next_level);
5053 return impl_from_IDirectDrawSurface7(next_level);
5056 /*****************************************************************************
5057 * IDirect3DTexture2::Load
5059 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5061 * This function isn't relayed to WineD3D because the whole interface is
5062 * implemented in DDraw only. For speed improvements an implementation which
5063 * takes OpenGL more into account could be placed into WineD3D.
5065 * Params:
5066 * src_texture: Address of the texture to load
5068 * Returns:
5069 * D3D_OK on success
5070 * D3DERR_TEXTURE_LOAD_FAILED.
5072 *****************************************************************************/
5073 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5075 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5076 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5077 HRESULT hr;
5079 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5081 if (src_surface == dst_surface)
5083 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5084 return D3D_OK;
5087 wined3d_mutex_lock();
5089 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5090 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5091 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5093 ERR("Trying to load surfaces with different mip-map counts.\n");
5096 for (;;)
5098 struct ddraw_palette *dst_pal, *src_pal;
5099 DDSURFACEDESC *src_desc, *dst_desc;
5101 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
5102 src_surface, dst_surface, src_surface->mipmap_level);
5104 /* Suppress the ALLOCONLOAD flag */
5105 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5107 /* Get the palettes */
5108 dst_pal = dst_surface->palette;
5109 src_pal = src_surface->palette;
5111 if (src_pal)
5113 PALETTEENTRY palent[256];
5115 if (!dst_pal)
5117 wined3d_mutex_unlock();
5118 return DDERR_NOPALETTEATTACHED;
5120 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5121 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5124 /* Copy one surface on the other */
5125 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5126 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5128 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5130 /* Should also check for same pixel format, u1.lPitch, ... */
5131 ERR("Error in surface sizes.\n");
5132 wined3d_mutex_unlock();
5133 return D3DERR_TEXTURE_LOAD_FAILED;
5135 else
5137 struct wined3d_map_desc src_map_desc, dst_map_desc;
5139 /* Copy the src blit color key if the source has one, don't erase
5140 * the destination's ckey if the source has none */
5141 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5143 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5144 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5147 /* Copy the main memory texture into the surface that corresponds
5148 * to the OpenGL texture object. */
5150 hr = wined3d_surface_map(src_surface->wined3d_surface, &src_map_desc, NULL, 0);
5151 if (FAILED(hr))
5153 ERR("Failed to lock source surface, hr %#x.\n", hr);
5154 wined3d_mutex_unlock();
5155 return D3DERR_TEXTURE_LOAD_FAILED;
5158 hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_map_desc, NULL, 0);
5159 if (FAILED(hr))
5161 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5162 wined3d_surface_unmap(src_surface->wined3d_surface);
5163 wined3d_mutex_unlock();
5164 return D3DERR_TEXTURE_LOAD_FAILED;
5167 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5168 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5169 else
5170 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5172 wined3d_surface_unmap(src_surface->wined3d_surface);
5173 wined3d_surface_unmap(dst_surface->wined3d_surface);
5176 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5177 src_surface = get_sub_mimaplevel(src_surface);
5178 else
5179 src_surface = NULL;
5181 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5182 dst_surface = get_sub_mimaplevel(dst_surface);
5183 else
5184 dst_surface = NULL;
5186 if (!src_surface || !dst_surface)
5188 if (src_surface != dst_surface)
5189 ERR("Loading surface with different mipmap structure.\n");
5190 break;
5194 wined3d_mutex_unlock();
5196 return hr;
5199 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5201 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5202 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5204 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5206 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5207 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5210 /*****************************************************************************
5211 * The VTable
5212 *****************************************************************************/
5214 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5216 /* IUnknown */
5217 ddraw_surface7_QueryInterface,
5218 ddraw_surface7_AddRef,
5219 ddraw_surface7_Release,
5220 /* IDirectDrawSurface */
5221 ddraw_surface7_AddAttachedSurface,
5222 ddraw_surface7_AddOverlayDirtyRect,
5223 ddraw_surface7_Blt,
5224 ddraw_surface7_BltBatch,
5225 ddraw_surface7_BltFast,
5226 ddraw_surface7_DeleteAttachedSurface,
5227 ddraw_surface7_EnumAttachedSurfaces,
5228 ddraw_surface7_EnumOverlayZOrders,
5229 ddraw_surface7_Flip,
5230 ddraw_surface7_GetAttachedSurface,
5231 ddraw_surface7_GetBltStatus,
5232 ddraw_surface7_GetCaps,
5233 ddraw_surface7_GetClipper,
5234 ddraw_surface7_GetColorKey,
5235 ddraw_surface7_GetDC,
5236 ddraw_surface7_GetFlipStatus,
5237 ddraw_surface7_GetOverlayPosition,
5238 ddraw_surface7_GetPalette,
5239 ddraw_surface7_GetPixelFormat,
5240 ddraw_surface7_GetSurfaceDesc,
5241 ddraw_surface7_Initialize,
5242 ddraw_surface7_IsLost,
5243 ddraw_surface7_Lock,
5244 ddraw_surface7_ReleaseDC,
5245 ddraw_surface7_Restore,
5246 ddraw_surface7_SetClipper,
5247 ddraw_surface7_SetColorKey,
5248 ddraw_surface7_SetOverlayPosition,
5249 ddraw_surface7_SetPalette,
5250 ddraw_surface7_Unlock,
5251 ddraw_surface7_UpdateOverlay,
5252 ddraw_surface7_UpdateOverlayDisplay,
5253 ddraw_surface7_UpdateOverlayZOrder,
5254 /* IDirectDrawSurface2 */
5255 ddraw_surface7_GetDDInterface,
5256 ddraw_surface7_PageLock,
5257 ddraw_surface7_PageUnlock,
5258 /* IDirectDrawSurface3 */
5259 ddraw_surface7_SetSurfaceDesc,
5260 /* IDirectDrawSurface4 */
5261 ddraw_surface7_SetPrivateData,
5262 ddraw_surface7_GetPrivateData,
5263 ddraw_surface7_FreePrivateData,
5264 ddraw_surface7_GetUniquenessValue,
5265 ddraw_surface7_ChangeUniquenessValue,
5266 /* IDirectDrawSurface7 */
5267 ddraw_surface7_SetPriority,
5268 ddraw_surface7_GetPriority,
5269 ddraw_surface7_SetLOD,
5270 ddraw_surface7_GetLOD,
5273 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5275 /* IUnknown */
5276 ddraw_surface4_QueryInterface,
5277 ddraw_surface4_AddRef,
5278 ddraw_surface4_Release,
5279 /* IDirectDrawSurface */
5280 ddraw_surface4_AddAttachedSurface,
5281 ddraw_surface4_AddOverlayDirtyRect,
5282 ddraw_surface4_Blt,
5283 ddraw_surface4_BltBatch,
5284 ddraw_surface4_BltFast,
5285 ddraw_surface4_DeleteAttachedSurface,
5286 ddraw_surface4_EnumAttachedSurfaces,
5287 ddraw_surface4_EnumOverlayZOrders,
5288 ddraw_surface4_Flip,
5289 ddraw_surface4_GetAttachedSurface,
5290 ddraw_surface4_GetBltStatus,
5291 ddraw_surface4_GetCaps,
5292 ddraw_surface4_GetClipper,
5293 ddraw_surface4_GetColorKey,
5294 ddraw_surface4_GetDC,
5295 ddraw_surface4_GetFlipStatus,
5296 ddraw_surface4_GetOverlayPosition,
5297 ddraw_surface4_GetPalette,
5298 ddraw_surface4_GetPixelFormat,
5299 ddraw_surface4_GetSurfaceDesc,
5300 ddraw_surface4_Initialize,
5301 ddraw_surface4_IsLost,
5302 ddraw_surface4_Lock,
5303 ddraw_surface4_ReleaseDC,
5304 ddraw_surface4_Restore,
5305 ddraw_surface4_SetClipper,
5306 ddraw_surface4_SetColorKey,
5307 ddraw_surface4_SetOverlayPosition,
5308 ddraw_surface4_SetPalette,
5309 ddraw_surface4_Unlock,
5310 ddraw_surface4_UpdateOverlay,
5311 ddraw_surface4_UpdateOverlayDisplay,
5312 ddraw_surface4_UpdateOverlayZOrder,
5313 /* IDirectDrawSurface2 */
5314 ddraw_surface4_GetDDInterface,
5315 ddraw_surface4_PageLock,
5316 ddraw_surface4_PageUnlock,
5317 /* IDirectDrawSurface3 */
5318 ddraw_surface4_SetSurfaceDesc,
5319 /* IDirectDrawSurface4 */
5320 ddraw_surface4_SetPrivateData,
5321 ddraw_surface4_GetPrivateData,
5322 ddraw_surface4_FreePrivateData,
5323 ddraw_surface4_GetUniquenessValue,
5324 ddraw_surface4_ChangeUniquenessValue,
5327 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5329 /* IUnknown */
5330 ddraw_surface3_QueryInterface,
5331 ddraw_surface3_AddRef,
5332 ddraw_surface3_Release,
5333 /* IDirectDrawSurface */
5334 ddraw_surface3_AddAttachedSurface,
5335 ddraw_surface3_AddOverlayDirtyRect,
5336 ddraw_surface3_Blt,
5337 ddraw_surface3_BltBatch,
5338 ddraw_surface3_BltFast,
5339 ddraw_surface3_DeleteAttachedSurface,
5340 ddraw_surface3_EnumAttachedSurfaces,
5341 ddraw_surface3_EnumOverlayZOrders,
5342 ddraw_surface3_Flip,
5343 ddraw_surface3_GetAttachedSurface,
5344 ddraw_surface3_GetBltStatus,
5345 ddraw_surface3_GetCaps,
5346 ddraw_surface3_GetClipper,
5347 ddraw_surface3_GetColorKey,
5348 ddraw_surface3_GetDC,
5349 ddraw_surface3_GetFlipStatus,
5350 ddraw_surface3_GetOverlayPosition,
5351 ddraw_surface3_GetPalette,
5352 ddraw_surface3_GetPixelFormat,
5353 ddraw_surface3_GetSurfaceDesc,
5354 ddraw_surface3_Initialize,
5355 ddraw_surface3_IsLost,
5356 ddraw_surface3_Lock,
5357 ddraw_surface3_ReleaseDC,
5358 ddraw_surface3_Restore,
5359 ddraw_surface3_SetClipper,
5360 ddraw_surface3_SetColorKey,
5361 ddraw_surface3_SetOverlayPosition,
5362 ddraw_surface3_SetPalette,
5363 ddraw_surface3_Unlock,
5364 ddraw_surface3_UpdateOverlay,
5365 ddraw_surface3_UpdateOverlayDisplay,
5366 ddraw_surface3_UpdateOverlayZOrder,
5367 /* IDirectDrawSurface2 */
5368 ddraw_surface3_GetDDInterface,
5369 ddraw_surface3_PageLock,
5370 ddraw_surface3_PageUnlock,
5371 /* IDirectDrawSurface3 */
5372 ddraw_surface3_SetSurfaceDesc,
5375 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5377 /* IUnknown */
5378 ddraw_surface2_QueryInterface,
5379 ddraw_surface2_AddRef,
5380 ddraw_surface2_Release,
5381 /* IDirectDrawSurface */
5382 ddraw_surface2_AddAttachedSurface,
5383 ddraw_surface2_AddOverlayDirtyRect,
5384 ddraw_surface2_Blt,
5385 ddraw_surface2_BltBatch,
5386 ddraw_surface2_BltFast,
5387 ddraw_surface2_DeleteAttachedSurface,
5388 ddraw_surface2_EnumAttachedSurfaces,
5389 ddraw_surface2_EnumOverlayZOrders,
5390 ddraw_surface2_Flip,
5391 ddraw_surface2_GetAttachedSurface,
5392 ddraw_surface2_GetBltStatus,
5393 ddraw_surface2_GetCaps,
5394 ddraw_surface2_GetClipper,
5395 ddraw_surface2_GetColorKey,
5396 ddraw_surface2_GetDC,
5397 ddraw_surface2_GetFlipStatus,
5398 ddraw_surface2_GetOverlayPosition,
5399 ddraw_surface2_GetPalette,
5400 ddraw_surface2_GetPixelFormat,
5401 ddraw_surface2_GetSurfaceDesc,
5402 ddraw_surface2_Initialize,
5403 ddraw_surface2_IsLost,
5404 ddraw_surface2_Lock,
5405 ddraw_surface2_ReleaseDC,
5406 ddraw_surface2_Restore,
5407 ddraw_surface2_SetClipper,
5408 ddraw_surface2_SetColorKey,
5409 ddraw_surface2_SetOverlayPosition,
5410 ddraw_surface2_SetPalette,
5411 ddraw_surface2_Unlock,
5412 ddraw_surface2_UpdateOverlay,
5413 ddraw_surface2_UpdateOverlayDisplay,
5414 ddraw_surface2_UpdateOverlayZOrder,
5415 /* IDirectDrawSurface2 */
5416 ddraw_surface2_GetDDInterface,
5417 ddraw_surface2_PageLock,
5418 ddraw_surface2_PageUnlock,
5421 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5423 /* IUnknown */
5424 ddraw_surface1_QueryInterface,
5425 ddraw_surface1_AddRef,
5426 ddraw_surface1_Release,
5427 /* IDirectDrawSurface */
5428 ddraw_surface1_AddAttachedSurface,
5429 ddraw_surface1_AddOverlayDirtyRect,
5430 ddraw_surface1_Blt,
5431 ddraw_surface1_BltBatch,
5432 ddraw_surface1_BltFast,
5433 ddraw_surface1_DeleteAttachedSurface,
5434 ddraw_surface1_EnumAttachedSurfaces,
5435 ddraw_surface1_EnumOverlayZOrders,
5436 ddraw_surface1_Flip,
5437 ddraw_surface1_GetAttachedSurface,
5438 ddraw_surface1_GetBltStatus,
5439 ddraw_surface1_GetCaps,
5440 ddraw_surface1_GetClipper,
5441 ddraw_surface1_GetColorKey,
5442 ddraw_surface1_GetDC,
5443 ddraw_surface1_GetFlipStatus,
5444 ddraw_surface1_GetOverlayPosition,
5445 ddraw_surface1_GetPalette,
5446 ddraw_surface1_GetPixelFormat,
5447 ddraw_surface1_GetSurfaceDesc,
5448 ddraw_surface1_Initialize,
5449 ddraw_surface1_IsLost,
5450 ddraw_surface1_Lock,
5451 ddraw_surface1_ReleaseDC,
5452 ddraw_surface1_Restore,
5453 ddraw_surface1_SetClipper,
5454 ddraw_surface1_SetColorKey,
5455 ddraw_surface1_SetOverlayPosition,
5456 ddraw_surface1_SetPalette,
5457 ddraw_surface1_Unlock,
5458 ddraw_surface1_UpdateOverlay,
5459 ddraw_surface1_UpdateOverlayDisplay,
5460 ddraw_surface1_UpdateOverlayZOrder,
5463 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5465 ddraw_gamma_control_QueryInterface,
5466 ddraw_gamma_control_AddRef,
5467 ddraw_gamma_control_Release,
5468 ddraw_gamma_control_GetGammaRamp,
5469 ddraw_gamma_control_SetGammaRamp,
5472 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5474 d3d_texture2_QueryInterface,
5475 d3d_texture2_AddRef,
5476 d3d_texture2_Release,
5477 d3d_texture2_GetHandle,
5478 d3d_texture2_PaletteChanged,
5479 d3d_texture2_Load,
5482 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5484 d3d_texture1_QueryInterface,
5485 d3d_texture1_AddRef,
5486 d3d_texture1_Release,
5487 d3d_texture1_Initialize,
5488 d3d_texture1_GetHandle,
5489 d3d_texture1_PaletteChanged,
5490 d3d_texture1_Load,
5491 d3d_texture1_Unload,
5494 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5495 * IDirectDrawSurface interface to ddraw methods. */
5496 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5498 if (!iface) return NULL;
5499 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5501 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5502 if (FAILED(hr))
5504 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5505 return NULL;
5507 IDirectDrawSurface7_Release(iface);
5509 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5512 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5514 if (!iface) return NULL;
5515 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5517 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5518 if (FAILED(hr))
5520 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5521 return NULL;
5523 IDirectDrawSurface4_Release(iface);
5525 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5528 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5530 if (!iface) return NULL;
5531 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5533 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5534 if (FAILED(hr))
5536 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5537 return NULL;
5539 IDirectDrawSurface3_Release(iface);
5541 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5544 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5546 if (!iface) return NULL;
5547 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5549 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5550 if (FAILED(hr))
5552 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5553 return NULL;
5555 IDirectDrawSurface2_Release(iface);
5557 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5560 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5562 if (!iface) return NULL;
5563 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5565 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5566 if (FAILED(hr))
5568 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5569 return NULL;
5571 IDirectDrawSurface_Release(iface);
5573 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5576 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5578 if (!iface) return NULL;
5579 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5580 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5583 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5585 if (!iface) return NULL;
5586 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5587 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5590 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5592 struct ddraw_surface *surface = parent;
5594 TRACE("surface %p.\n", surface);
5596 /* Check for attached surfaces and detach them. */
5597 if (surface->first_attached != surface)
5599 /* Well, this shouldn't happen: The surface being attached is
5600 * referenced in AddAttachedSurface(), so it shouldn't be released
5601 * until DeleteAttachedSurface() is called, because the refcount is
5602 * held. It looks like the application released it often enough to
5603 * force this. */
5604 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
5606 /* The refcount will drop to -1 here */
5607 if (FAILED(ddraw_surface_delete_attached_surface(surface->first_attached, surface, surface->attached_iface)))
5608 ERR("DeleteAttachedSurface failed.\n");
5611 while (surface->next_attached)
5612 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5613 surface->next_attached, surface->next_attached->attached_iface)))
5614 ERR("DeleteAttachedSurface failed.\n");
5616 /* Having a texture handle set implies that the device still exists. */
5617 if (surface->Handle)
5618 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5620 /* Reduce the ddraw surface count. */
5621 list_remove(&surface->surface_list_entry);
5623 if (surface->clipper)
5624 IDirectDrawClipper_Release(&surface->clipper->IDirectDrawClipper_iface);
5626 if (surface == surface->ddraw->primary)
5627 surface->ddraw->primary = NULL;
5629 wined3d_private_store_cleanup(&surface->private_store);
5631 HeapFree(GetProcessHeap(), 0, surface);
5634 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5636 ddraw_surface_wined3d_object_destroyed,
5639 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5641 TRACE("parent %p.\n", parent);
5643 HeapFree(GetProcessHeap(), 0, parent);
5646 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5648 ddraw_texture_wined3d_object_destroyed,
5651 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5653 return DD_OK;
5656 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
5657 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
5659 struct ddraw_surface *root, *mip, **attach;
5660 struct wined3d_resource_desc wined3d_desc;
5661 struct wined3d_texture *wined3d_texture;
5662 struct wined3d_resource *resource;
5663 struct wined3d_display_mode mode;
5664 DDSURFACEDESC2 *desc, *mip_desc;
5665 struct ddraw_texture *texture;
5666 UINT layers, levels, i, j;
5667 unsigned int pitch = 0;
5668 HRESULT hr;
5670 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
5671 ddraw, surface_desc, surface, outer_unknown, version);
5672 if (TRACE_ON(ddraw))
5674 TRACE("Requesting surface desc:\n");
5675 DDRAW_dump_surface_desc(surface_desc);
5678 if (outer_unknown)
5679 return CLASS_E_NOAGGREGATION;
5681 if (!surface)
5682 return E_POINTER;
5684 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
5685 return E_OUTOFMEMORY;
5687 texture->version = version;
5688 texture->surface_desc = *surface_desc;
5689 desc = &texture->surface_desc;
5691 /* Ensure DDSD_CAPS is always set. */
5692 desc->dwFlags |= DDSD_CAPS;
5694 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5696 DWORD flippable = desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_COMPLEX);
5698 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5700 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
5701 HeapFree(GetProcessHeap(), 0, texture);
5702 return DDERR_INVALIDCAPS;
5705 if (flippable)
5707 if (flippable != (DDSCAPS_FLIP | DDSCAPS_COMPLEX))
5709 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
5710 HeapFree(GetProcessHeap(), 0, texture);
5711 return DDERR_INVALIDCAPS;
5714 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->u5.dwBackBufferCount)
5716 WARN("Tried to create a flippable primary surface without any back buffers.\n");
5717 HeapFree(GetProcessHeap(), 0, texture);
5718 return DDERR_INVALIDCAPS;
5721 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
5723 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
5724 HeapFree(GetProcessHeap(), 0, texture);
5725 return DDERR_NOEXCLUSIVEMODE;
5730 /* This is a special case in ddrawex, but not allowed in ddraw. */
5731 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5732 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5734 WARN("Tried to create a surface in both system and video memory.\n");
5735 HeapFree(GetProcessHeap(), 0, texture);
5736 return DDERR_INVALIDCAPS;
5739 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
5740 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
5742 WARN("Cube map faces requested without cube map flag.\n");
5743 HeapFree(GetProcessHeap(), 0, texture);
5744 return DDERR_INVALIDCAPS;
5747 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5748 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
5750 WARN("Cube map without faces requested.\n");
5751 HeapFree(GetProcessHeap(), 0, texture);
5752 return DDERR_INVALIDPARAMS;
5755 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5756 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
5757 FIXME("Partial cube maps not implemented.\n");
5759 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
5761 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5763 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
5764 HeapFree(GetProcessHeap(), 0, texture);
5765 return DDERR_INVALIDCAPS;
5767 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5769 WARN("DDSCAPS2_TEXTUREMANAGE used width DDSCAPS_VIDEOMEMORY "
5770 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
5771 HeapFree(GetProcessHeap(), 0, texture);
5772 return DDERR_INVALIDCAPS;
5776 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
5778 ERR("Failed to get display mode, hr %#x.\n", hr);
5779 HeapFree(GetProcessHeap(), 0, texture);
5780 return hr_ddraw_from_wined3d(hr);
5783 /* No pixelformat given? Use the current screen format. */
5784 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
5786 desc->dwFlags |= DDSD_PIXELFORMAT;
5787 desc->u4.ddpfPixelFormat.dwSize = sizeof(desc->u4.ddpfPixelFormat);
5788 ddrawformat_from_wined3dformat(&desc->u4.ddpfPixelFormat, mode.format_id);
5791 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
5792 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
5794 WARN("Unsupported / unknown pixelformat.\n");
5795 HeapFree(GetProcessHeap(), 0, texture);
5796 return DDERR_INVALIDPIXELFORMAT;
5799 /* No width or no height? Use the screen size. */
5800 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
5802 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
5804 WARN("No width / height specified.\n");
5805 HeapFree(GetProcessHeap(), 0, texture);
5806 return DDERR_INVALIDPARAMS;
5809 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
5810 desc->dwWidth = mode.width;
5811 desc->dwHeight = mode.height;
5814 if (!desc->dwWidth || !desc->dwHeight)
5816 HeapFree(GetProcessHeap(), 0, texture);
5817 return DDERR_INVALIDPARAMS;
5820 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5822 /* The first surface is a front buffer, the back buffers are created
5823 * afterwards. */
5824 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
5825 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
5826 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5827 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
5829 struct wined3d_swapchain_desc swapchain_desc;
5831 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
5832 swapchain_desc.backbuffer_width = mode.width;
5833 swapchain_desc.backbuffer_height = mode.height;
5834 swapchain_desc.backbuffer_format = mode.format_id;
5836 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
5837 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
5839 ERR("Failed to reset device.\n");
5840 HeapFree(GetProcessHeap(), 0, texture);
5841 return hr_ddraw_from_wined3d(hr);
5846 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5847 wined3d_desc.multisample_quality = 0;
5848 wined3d_desc.usage = 0;
5849 wined3d_desc.pool = WINED3D_POOL_DEFAULT;
5850 wined3d_desc.width = desc->dwWidth;
5851 wined3d_desc.height = desc->dwHeight;
5852 wined3d_desc.depth = 1;
5853 wined3d_desc.size = 0;
5855 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
5857 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
5858 /* Do not fail surface creation, only fail 3D device creation. */
5861 /* Mipmap count fixes */
5862 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5864 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
5866 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
5868 /* Mipmap count is given, should not be 0. */
5869 if (!desc->u2.dwMipMapCount)
5871 HeapFree(GetProcessHeap(), 0, texture);
5872 return DDERR_INVALIDPARAMS;
5875 else
5877 /* Undocumented feature: Create sublevels until either the
5878 * width or the height is 1. */
5879 desc->u2.dwMipMapCount = wined3d_log2i(min(desc->dwWidth, desc->dwHeight)) + 1;
5882 else
5884 desc->u2.dwMipMapCount = 1;
5887 desc->dwFlags |= DDSD_MIPMAPCOUNT;
5888 levels = desc->u2.dwMipMapCount;
5890 else
5892 levels = 1;
5895 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
5897 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
5899 enum wined3d_resource_type rtype;
5900 DWORD usage = 0;
5902 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5903 rtype = WINED3D_RTYPE_CUBE_TEXTURE;
5904 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5905 rtype = WINED3D_RTYPE_TEXTURE;
5906 else
5907 rtype = WINED3D_RTYPE_SURFACE;
5909 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5910 usage = WINED3DUSAGE_DEPTHSTENCIL;
5911 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5912 usage = WINED3DUSAGE_RENDERTARGET;
5914 if (SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
5915 WINED3D_DEVICE_TYPE_HAL, mode.format_id, usage, rtype, wined3d_desc.format)))
5916 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
5917 else
5918 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5920 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5922 /* Tests show surfaces without memory flags get these flags added
5923 * right after creation. */
5924 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5928 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5929 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5931 WARN("System memory overlays are not allowed.\n");
5932 HeapFree(GetProcessHeap(), 0, texture);
5933 return DDERR_NOOVERLAYHW;
5936 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5938 wined3d_desc.pool = WINED3D_POOL_SYSTEM_MEM;
5940 else
5942 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5943 wined3d_desc.usage |= WINED3DUSAGE_TEXTURE;
5944 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5945 wined3d_desc.usage |= WINED3DUSAGE_DEPTHSTENCIL;
5946 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5947 wined3d_desc.usage |= WINED3DUSAGE_RENDERTARGET;
5949 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
5951 wined3d_desc.pool = WINED3D_POOL_MANAGED;
5952 /* Managed textures have the system memory flag set. */
5953 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5955 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5957 /* Videomemory adds localvidmem. This is mutually exclusive with
5958 * systemmemory and texturemanage. */
5959 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
5960 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
5964 /* If the surface is of the 'ALLOCONLOAD' type, ignore the LPSURFACE
5965 * field. Frank Herbert's Dune specifies a NULL pointer for lpSurface. */
5966 if ((desc->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD) || !desc->lpSurface)
5967 desc->dwFlags &= ~DDSD_LPSURFACE;
5968 if (desc->dwFlags & DDSD_LPSURFACE)
5970 if (wined3d_desc.pool != WINED3D_POOL_SYSTEM_MEM)
5972 WARN("User memory surfaces should be in the system memory pool.\n");
5973 HeapFree(GetProcessHeap(), 0, texture);
5974 return DDERR_INVALIDCAPS;
5977 if (version < 4)
5979 WARN("User memory surfaces not supported before version 4.\n");
5980 HeapFree(GetProcessHeap(), 0, texture);
5981 return DDERR_INVALIDPARAMS;
5984 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
5986 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
5988 WARN("Pitch specified on a compressed user memory surface.\n");
5989 HeapFree(GetProcessHeap(), 0, texture);
5990 return DDERR_INVALIDPARAMS;
5993 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
5995 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
5996 HeapFree(GetProcessHeap(), 0, texture);
5997 return DDERR_INVALIDPARAMS;
6000 if ((desc->dwFlags & DDSD_LINEARSIZE)
6001 && desc->u1.dwLinearSize < wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
6002 wined3d_desc.format, wined3d_desc.width) * ((desc->dwHeight + 3) / 4))
6004 WARN("Invalid linear size %u specified.\n", desc->u1.dwLinearSize);
6005 HeapFree(GetProcessHeap(), 0, texture);
6006 return DDERR_INVALIDPARAMS;
6009 else
6011 if (!(desc->dwFlags & DDSD_PITCH))
6013 WARN("User memory surfaces should explicitly specify the pitch.\n");
6014 HeapFree(GetProcessHeap(), 0, texture);
6015 return DDERR_INVALIDPARAMS;
6018 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
6019 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
6021 WARN("Invalid pitch %u specified.\n", desc->u1.lPitch);
6022 HeapFree(GetProcessHeap(), 0, texture);
6023 return DDERR_INVALIDPARAMS;
6026 pitch = desc->u1.lPitch;
6030 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
6031 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
6033 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
6034 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
6036 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6038 wined3d_desc.resource_type = WINED3D_RTYPE_CUBE_TEXTURE;
6039 layers = 6;
6041 else
6043 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE;
6044 layers = 1;
6047 /* Some applications assume surfaces will always be mapped at the same
6048 * address. Some of those also assume that this address is valid even when
6049 * the surface isn't mapped, and that updates done this way will be
6050 * visible on the screen. The game Nox is such an application,
6051 * Commandos: Behind Enemy Lines is another. We set
6052 * WINED3D_SURFACE_PIN_SYSMEM because of this. */
6053 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, levels,
6054 WINED3D_SURFACE_PIN_SYSMEM, NULL, texture, &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
6056 WARN("Failed to create wined3d texture, hr %#x.\n", hr);
6057 HeapFree(GetProcessHeap(), 0, texture);
6058 return hr_ddraw_from_wined3d(hr);
6061 resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
6062 root = wined3d_resource_get_parent(resource);
6063 root->wined3d_texture = wined3d_texture;
6064 root->is_complex_root = TRUE;
6065 texture->root = root;
6067 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6068 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6069 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6070 if (desc->dwFlags & DDSD_CKDESTBLT)
6071 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6072 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6073 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6074 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6075 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6076 if (desc->dwFlags & DDSD_CKSRCBLT)
6077 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6078 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6080 for (i = 0; i < layers; ++i)
6082 attach = &root->complex_array[layers - 1 - i];
6084 for (j = 0; j < levels; ++j)
6086 resource = wined3d_texture_get_sub_resource(wined3d_texture, i * levels + j);
6087 mip = wined3d_resource_get_parent(resource);
6088 mip_desc = &mip->surface_desc;
6090 if (j)
6091 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6092 else
6093 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6095 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6097 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6099 switch (i)
6101 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6102 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6103 break;
6104 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6105 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6106 break;
6107 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6108 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6109 break;
6110 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6111 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6112 break;
6113 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6114 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6115 break;
6116 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6117 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6118 break;
6123 if (mip == root)
6124 continue;
6126 *attach = mip;
6127 attach = &mip->complex_array[0];
6131 if ((desc->dwFlags & DDSD_LPSURFACE) && FAILED(hr = wined3d_texture_update_desc(wined3d_texture,
6132 wined3d_desc.width, wined3d_desc.height, wined3d_desc.format,
6133 WINED3D_MULTISAMPLE_NONE, 0, desc->lpSurface, pitch)))
6135 ERR("Failed to set surface memory, hr %#x.\n", hr);
6136 goto fail;
6139 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6141 unsigned int count = desc->u5.dwBackBufferCount;
6142 struct ddraw_surface *last = root;
6144 attach = &last->complex_array[0];
6145 for (i = 0; i < count; ++i)
6147 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
6149 hr = E_OUTOFMEMORY;
6150 goto fail;
6153 texture->version = version;
6154 texture->surface_desc = root->surface_desc;
6155 desc = &texture->surface_desc;
6157 /* Only one surface in the flipping chain is a back buffer, one is
6158 * a front buffer, the others are just flippable surfaces. */
6159 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6160 | DDSCAPS_BACKBUFFER);
6161 if (!i)
6162 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6163 desc->u5.dwBackBufferCount = 0;
6165 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, 1,
6166 WINED3D_SURFACE_PIN_SYSMEM, NULL, texture, &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
6168 HeapFree(GetProcessHeap(), 0, texture);
6169 hr = hr_ddraw_from_wined3d(hr);
6170 goto fail;
6173 resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
6174 last = wined3d_resource_get_parent(resource);
6175 last->wined3d_texture = wined3d_texture;
6176 texture->root = last;
6178 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6179 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6180 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6181 if (desc->dwFlags & DDSD_CKDESTBLT)
6182 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6183 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6184 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6185 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6186 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6187 if (desc->dwFlags & DDSD_CKSRCBLT)
6188 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6189 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6191 *attach = last;
6192 attach = &last->complex_array[0];
6194 *attach = root;
6197 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6198 ddraw->primary = root;
6199 *surface = root;
6201 return DD_OK;
6203 fail:
6204 if (version == 7)
6205 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6206 else if (version == 4)
6207 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6208 else
6209 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6211 return hr;
6214 void ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw, struct ddraw_texture *texture,
6215 struct wined3d_surface *wined3d_surface, const struct wined3d_parent_ops **parent_ops)
6217 DDSURFACEDESC2 *desc = &surface->surface_desc;
6218 struct wined3d_resource_desc wined3d_desc;
6219 unsigned int version = texture->version;
6221 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6222 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6223 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6224 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6225 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6226 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6227 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6228 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6229 surface->iface_count = 1;
6230 surface->version = version;
6231 surface->ddraw = ddraw;
6233 if (version == 7)
6235 surface->ref7 = 1;
6236 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6238 else if (version == 4)
6240 surface->ref4 = 1;
6241 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6243 else
6245 surface->ref1 = 1;
6246 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6249 *desc = texture->surface_desc;
6250 wined3d_resource_get_desc(wined3d_surface_get_resource(wined3d_surface), &wined3d_desc);
6251 desc->dwWidth = wined3d_desc.width;
6252 desc->dwHeight = wined3d_desc.height;
6253 surface->first_attached = surface;
6255 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6257 if (desc->dwFlags & DDSD_LPSURFACE)
6258 desc->u1.dwLinearSize = ~0u;
6259 else
6260 desc->u1.dwLinearSize = wined3d_surface_get_pitch(wined3d_surface) * ((desc->dwHeight + 3) / 4);
6261 desc->dwFlags |= DDSD_LINEARSIZE;
6262 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6264 else
6266 if (!(desc->dwFlags & DDSD_LPSURFACE))
6267 desc->u1.lPitch = wined3d_surface_get_pitch(wined3d_surface);
6268 desc->dwFlags |= DDSD_PITCH;
6269 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6271 desc->lpSurface = NULL;
6273 wined3d_surface_incref(wined3d_surface);
6274 surface->wined3d_surface = wined3d_surface;
6275 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6277 wined3d_private_store_init(&surface->private_store);
6280 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
6282 struct ddraw_surface *surface = parent;
6284 /* If the surface reference count drops to zero, we release our reference
6285 * to the view, but don't clear the pointer yet, in case e.g. a
6286 * GetRenderTarget() call brings the surface back before the view is
6287 * actually destroyed. When the view is destroyed, we need to clear the
6288 * pointer, or a subsequent surface AddRef() would reference it again.
6290 * This is safe because as long as the view still has a reference to the
6291 * texture, the surface is also still alive, and we're called before the
6292 * view releases that reference. */
6293 surface->wined3d_rtv = NULL;
6296 static const struct wined3d_parent_ops ddraw_view_wined3d_parent_ops =
6298 view_wined3d_object_destroyed,
6301 struct wined3d_rendertarget_view *ddraw_surface_get_rendertarget_view(struct ddraw_surface *surface)
6303 HRESULT hr;
6305 if (surface->wined3d_rtv)
6306 return surface->wined3d_rtv;
6308 if (FAILED(hr = wined3d_rendertarget_view_create_from_surface(surface->wined3d_surface,
6309 surface, &ddraw_view_wined3d_parent_ops, &surface->wined3d_rtv)))
6311 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
6312 return NULL;
6315 return surface->wined3d_rtv;