mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / ddraw / surface.c
blob86cda045d92e45d30e1a4b968365b6fb429c2d9b
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 "ddraw_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
28 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface);
29 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface);
31 static inline struct ddraw_surface *impl_from_IDirectDrawGammaControl(IDirectDrawGammaControl *iface)
33 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawGammaControl_iface);
36 static BOOL ddraw_surface_is_lost(const struct ddraw_surface *surface)
38 return ddraw_surface_can_be_lost(surface)
39 && (surface->ddraw->device_state != DDRAW_DEVICE_STATE_OK || surface->is_lost);
42 static BOOL ddraw_gdi_is_front(struct ddraw *ddraw)
44 struct ddraw_surface *surface;
46 if (!ddraw->gdi_surface || !(surface = wined3d_texture_get_sub_resource_parent(ddraw->gdi_surface, 0)))
47 return FALSE;
49 return surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER;
52 /* This is slow, of course. Also, in case of locks, we can't prevent other
53 * applications from drawing to the screen while we've locked the frontbuffer.
54 * We'd like to do this in wined3d instead, but for that to work wined3d needs
55 * to support windowless rendering first. */
56 HRESULT ddraw_surface_update_frontbuffer(struct ddraw_surface *surface,
57 const RECT *rect, BOOL read, unsigned int swap_interval)
59 struct wined3d_texture *dst_texture, *wined3d_texture;
60 struct ddraw *ddraw = surface->ddraw;
61 HDC surface_dc, screen_dc;
62 int x, y, w, h;
63 HRESULT hr;
64 BOOL ret;
65 RECT r;
67 if (ddraw->flags & DDRAW_SWAPPED && !read)
69 ddraw->flags &= ~DDRAW_SWAPPED;
70 rect = NULL;
73 if (!rect)
75 SetRect(&r, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
76 rect = &r;
79 x = rect->left;
80 y = rect->top;
81 w = rect->right - rect->left;
82 h = rect->bottom - rect->top;
84 if (w <= 0 || h <= 0)
85 return DD_OK;
87 /* The interaction between ddraw and GDI drawing is not all that well
88 * documented, and somewhat arcane. In ddraw exclusive mode, GDI draws
89 * seemingly go to the *original* frontbuffer/primary surface, while ddraw
90 * draws/flips go to the *current* frontbuffer surface. The bottom line is
91 * that if the current frontbuffer is not the GDI frontbuffer, and there's
92 * e.g. a popup window in front of the ddraw swapchain window, we can't
93 * use wined3d_swapchain_present() to get the ddraw contents to the screen
94 * while in exclusive mode, since it would get obscured by the popup
95 * window. On the other hand, if the current frontbuffer *is* the GDI
96 * frontbuffer, that's what's supposed to happen; the popup should obscure
97 * (oart of) the ddraw swapchain window.
99 * This affects the "Deer Hunter" demo, which uses a popup window and GDI
100 * draws to draw part of the user interface. See also the "fswindow"
101 * sample is the DirectX 7 SDK. */
102 if (ddraw->swapchain_window && (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE)
103 || ddraw->swapchain_window == GetForegroundWindow() || ddraw_gdi_is_front(ddraw)))
105 /* Nothing to do, we control the frontbuffer, or at least the parts we
106 * care about. */
107 if (read)
108 return DD_OK;
110 if (swap_interval)
111 dst_texture = wined3d_swapchain_get_back_buffer(ddraw->wined3d_swapchain, 0);
112 else
113 dst_texture = ddraw->wined3d_frontbuffer;
115 if (SUCCEEDED(hr = wined3d_device_context_blt(ddraw->immediate_context, dst_texture, 0, rect,
116 ddraw_surface_get_any_texture(surface, DDRAW_SURFACE_READ), surface->sub_resource_idx, rect, 0,
117 NULL, WINED3D_TEXF_POINT)) && swap_interval)
119 hr = wined3d_swapchain_present(ddraw->wined3d_swapchain, rect, rect, NULL, swap_interval, 0);
120 ddraw->flags |= DDRAW_SWAPPED;
122 return hr;
125 wined3d_texture = ddraw_surface_get_default_texture(surface, read ? (rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE)
126 : DDRAW_SURFACE_READ);
128 if (FAILED(hr = wined3d_texture_get_dc(wined3d_texture, surface->sub_resource_idx, &surface_dc)))
130 ERR("Failed to get surface DC, hr %#x.\n", hr);
131 return hr;
133 if (surface->palette)
134 wined3d_palette_apply_to_dc(surface->palette->wined3d_palette, surface_dc);
136 if (!(screen_dc = GetDC(NULL)))
138 wined3d_texture_release_dc(wined3d_texture, surface->sub_resource_idx, surface_dc);
139 ERR("Failed to get screen DC.\n");
140 return E_FAIL;
143 if (read)
144 ret = BitBlt(surface_dc, x, y, w, h,
145 screen_dc, x, y, SRCCOPY);
146 else
147 ret = BitBlt(screen_dc, x, y, w, h,
148 surface_dc, x, y, SRCCOPY);
150 ReleaseDC(NULL, screen_dc);
151 wined3d_texture_release_dc(wined3d_texture, surface->sub_resource_idx, surface_dc);
153 if (!ret)
155 ERR("Failed to blit to/from screen.\n");
156 return E_FAIL;
159 return DD_OK;
162 /*****************************************************************************
163 * IUnknown parts follow
164 *****************************************************************************/
166 /*****************************************************************************
167 * IDirectDrawSurface7::QueryInterface
169 * A normal QueryInterface implementation. For QueryInterface rules
170 * see ddraw.c, IDirectDraw7::QueryInterface. This method
171 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
172 * in all versions, the IDirectDrawGammaControl interface and it can
173 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
175 * Params:
176 * riid: The interface id queried for
177 * obj: Address to write the pointer to
179 * Returns:
180 * S_OK on success
181 * E_NOINTERFACE if the requested interface wasn't found
183 *****************************************************************************/
184 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
186 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
188 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
190 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
191 *obj = NULL;
193 if(!riid)
194 return DDERR_INVALIDPARAMS;
196 if (IsEqualGUID(riid, &IID_IDirectDrawSurface7))
198 IDirectDrawSurface7_AddRef(iface);
199 *obj = iface;
200 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
201 return S_OK;
204 if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
206 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
207 *obj = &This->IDirectDrawSurface4_iface;
208 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
209 return S_OK;
212 if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
214 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
215 *obj = &This->IDirectDrawSurface3_iface;
216 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
217 return S_OK;
220 if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
222 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
223 *obj = &This->IDirectDrawSurface2_iface;
224 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
225 return S_OK;
228 if (IsEqualGUID(riid, &IID_IDirectDrawSurface)
229 || IsEqualGUID(riid, &IID_IUnknown))
231 IDirectDrawSurface_AddRef(&This->IDirectDrawSurface_iface);
232 *obj = &This->IDirectDrawSurface_iface;
233 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
234 return S_OK;
237 if (IsEqualGUID(riid, &IID_IDirectDrawGammaControl))
239 IDirectDrawGammaControl_AddRef(&This->IDirectDrawGammaControl_iface);
240 *obj = &This->IDirectDrawGammaControl_iface;
241 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
242 return S_OK;
245 if (IsEqualGUID(riid, &IID_IDirectDrawColorControl))
247 WARN("Color control not implemented.\n");
248 *obj = NULL;
249 return E_NOINTERFACE;
252 if (This->version != 7)
254 if (IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D)
255 || IsEqualGUID(riid, &IID_IDirect3DHALDevice)
256 || IsEqualGUID(riid, &IID_IDirect3DRGBDevice))
258 wined3d_mutex_lock();
259 if (!This->device1)
261 HRESULT hr;
263 if (FAILED(hr = d3d_device_create(This->ddraw, riid, This, (IUnknown *)&This->IDirectDrawSurface_iface,
264 1, &This->device1, (IUnknown *)&This->IDirectDrawSurface_iface)))
266 This->device1 = NULL;
267 wined3d_mutex_unlock();
268 WARN("Failed to create device, hr %#x.\n", hr);
269 return hr;
272 wined3d_mutex_unlock();
274 IDirect3DDevice_AddRef(&This->device1->IDirect3DDevice_iface);
275 *obj = &This->device1->IDirect3DDevice_iface;
276 return S_OK;
279 if (IsEqualGUID(&IID_IDirect3DTexture2, riid))
281 IDirect3DTexture2_AddRef(&This->IDirect3DTexture2_iface);
282 *obj = &This->IDirect3DTexture2_iface;
283 return S_OK;
286 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
288 IDirect3DTexture2_AddRef(&This->IDirect3DTexture_iface);
289 *obj = &This->IDirect3DTexture_iface;
290 return S_OK;
294 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
296 if (This->version != 7)
297 return E_INVALIDARG;
299 return E_NOINTERFACE;
302 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
304 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
306 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
308 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
311 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
313 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
315 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
317 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
320 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
322 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
324 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
326 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
329 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
331 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
333 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
335 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
338 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
339 REFIID riid, void **object)
341 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
343 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
345 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
348 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
350 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
352 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
354 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
357 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
359 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
361 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
363 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
366 static void ddraw_surface_add_iface(struct ddraw_surface *surface)
368 ULONG iface_count = InterlockedIncrement(&surface->iface_count);
369 TRACE("%p increasing iface count to %u.\n", surface, iface_count);
371 if (iface_count == 1)
373 if (surface->ifaceToRelease)
374 IUnknown_AddRef(surface->ifaceToRelease);
375 wined3d_mutex_lock();
376 if (surface->wined3d_rtv)
377 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
378 wined3d_texture_incref(surface->draw_texture ? surface->draw_texture : surface->wined3d_texture);
379 wined3d_mutex_unlock();
383 /*****************************************************************************
384 * IDirectDrawSurface7::AddRef
386 * A normal addref implementation
388 * Returns:
389 * The new refcount
391 *****************************************************************************/
392 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
394 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
395 ULONG refcount = InterlockedIncrement(&This->ref7);
397 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
399 if (refcount == 1)
401 ddraw_surface_add_iface(This);
404 return refcount;
407 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
409 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
410 ULONG refcount = InterlockedIncrement(&This->ref4);
412 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
414 if (refcount == 1)
416 ddraw_surface_add_iface(This);
419 return refcount;
422 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
424 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
425 ULONG refcount = InterlockedIncrement(&This->ref3);
427 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
429 if (refcount == 1)
431 ddraw_surface_add_iface(This);
434 return refcount;
437 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
439 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
440 ULONG refcount = InterlockedIncrement(&This->ref2);
442 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
444 if (refcount == 1)
446 ddraw_surface_add_iface(This);
449 return refcount;
452 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
454 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
455 ULONG refcount = InterlockedIncrement(&This->ref1);
457 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
459 if (refcount == 1)
461 ddraw_surface_add_iface(This);
464 return refcount;
467 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
469 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
470 ULONG refcount = InterlockedIncrement(&This->gamma_count);
472 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
474 if (refcount == 1)
476 ddraw_surface_add_iface(This);
479 return refcount;
482 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
484 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
486 TRACE("iface %p.\n", iface);
488 return IUnknown_AddRef(surface->texture_outer);
491 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
493 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
495 TRACE("iface %p.\n", iface);
497 return IUnknown_AddRef(surface->texture_outer);
500 static HRESULT ddraw_surface_set_palette(struct ddraw_surface *surface, IDirectDrawPalette *palette)
502 struct ddraw_palette *palette_impl = unsafe_impl_from_IDirectDrawPalette(palette);
503 struct ddraw_palette *prev;
505 TRACE("iface %p, palette %p.\n", surface, palette);
507 if (palette_impl && palette_impl->flags & DDPCAPS_ALPHA
508 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
510 WARN("Alpha palette set on non-texture surface, returning DDERR_INVALIDSURFACETYPE.\n");
511 return DDERR_INVALIDSURFACETYPE;
514 if (!format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
515 return DDERR_INVALIDPIXELFORMAT;
517 wined3d_mutex_lock();
519 prev = surface->palette;
520 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
522 if (prev)
523 prev->flags &= ~DDPCAPS_PRIMARYSURFACE;
524 if (palette_impl)
525 palette_impl->flags |= DDPCAPS_PRIMARYSURFACE;
526 wined3d_swapchain_set_palette(surface->ddraw->wined3d_swapchain,
527 palette_impl ? palette_impl->wined3d_palette : NULL);
528 ddraw_surface_update_frontbuffer(surface, NULL, FALSE, 0);
530 if (palette_impl)
531 IDirectDrawPalette_AddRef(&palette_impl->IDirectDrawPalette_iface);
532 if (prev)
533 IDirectDrawPalette_Release(&prev->IDirectDrawPalette_iface);
534 surface->palette = palette_impl;
536 wined3d_mutex_unlock();
538 return DD_OK;
541 static void ddraw_surface_cleanup(struct ddraw_surface *surface)
543 struct ddraw_surface *surf;
544 UINT i;
546 TRACE("surface %p.\n", surface);
548 /* The refcount test shows that the palette is detached when the surface
549 * is destroyed. */
550 ddraw_surface_set_palette(surface, NULL);
552 /* Loop through all complex attached surfaces and destroy them.
554 * Yet again, only the root can have more than one complexly attached
555 * surface, all the others have a total of one. */
556 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
558 if (!surface->complex_array[i])
559 break;
561 surf = surface->complex_array[i];
562 surface->complex_array[i] = NULL;
563 if (!surf->is_complex_root)
565 struct ddraw_texture *texture = wined3d_texture_get_parent(surf->wined3d_texture);
566 struct wined3d_device *wined3d_device = texture->wined3d_device;
567 struct ddraw_surface *root = texture->root;
569 ddraw_surface_cleanup(surf);
571 if (surf == root)
572 wined3d_device_decref(wined3d_device);
576 if (surface->device1)
577 IUnknown_Release(&surface->device1->IUnknown_inner);
579 if (surface->iface_count > 1)
581 /* This can happen when a complex surface is destroyed, because the
582 * 2nd surface was addref()ed when the app called
583 * GetAttachedSurface(). */
584 WARN("Destroying surface %p with refcounts 7: %u 4: %u 3: %u 2: %u 1: %u.\n",
585 surface, surface->ref7, surface->ref4, surface->ref3, surface->ref2, surface->ref1);
588 if (surface->wined3d_rtv)
589 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
590 wined3d_texture_decref(surface->draw_texture ? surface->draw_texture : surface->wined3d_texture);
593 static ULONG ddraw_surface_release_iface(struct ddraw_surface *This)
595 ULONG iface_count;
597 /* Prevent the surface from being destroyed if it's still attached to
598 * another surface. It will be destroyed when the root is destroyed. */
599 if (This->iface_count == 1 && This->attached_iface)
600 IUnknown_AddRef(This->attached_iface);
601 iface_count = InterlockedDecrement(&This->iface_count);
603 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
605 if (iface_count == 0)
607 struct ddraw_texture *texture = wined3d_texture_get_parent(This->wined3d_texture);
608 struct wined3d_device *wined3d_device = texture->wined3d_device;
609 IUnknown *release_iface = This->ifaceToRelease;
611 /* Complex attached surfaces are destroyed implicitly when the root is released */
612 wined3d_mutex_lock();
613 if(!This->is_complex_root)
615 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
616 wined3d_mutex_unlock();
617 return iface_count;
619 ddraw_surface_cleanup(This);
620 wined3d_mutex_unlock();
622 if (release_iface)
623 IUnknown_Release(release_iface);
624 /* Release the device only after anything that may reference it (the
625 * wined3d texture and rendertarget view in particular) is released. */
626 wined3d_device_decref(wined3d_device);
629 return iface_count;
632 /*****************************************************************************
633 * IDirectDrawSurface7::Release
635 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
636 * surface is destroyed.
638 * Destroying the surface is a bit tricky. For the connection between
639 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
640 * It has a nice graph explaining the connection.
642 * What happens here is basically this:
643 * When a surface is destroyed, its WineD3DSurface is released,
644 * and the refcount of the DirectDraw interface is reduced by 1. If it has
645 * complex surfaces attached to it, then these surfaces are destroyed too,
646 * regardless of their refcount. If any surface being destroyed has another
647 * surface attached to it (with a "soft" attachment, not complex), then
648 * this surface is detached with DeleteAttachedSurface.
650 * When the surface is a texture, the WineD3DTexture is released.
651 * If the surface is the Direct3D render target, then the D3D
652 * capabilities of the WineD3DDevice are uninitialized, which causes the
653 * swapchain to be released.
655 * When a complex sublevel falls to ref zero, then this is ignored.
657 * Returns:
658 * The new refcount
660 *****************************************************************************/
661 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
663 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
664 ULONG refcount = InterlockedDecrement(&This->ref7);
666 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
668 if (refcount == 0)
670 ddraw_surface_release_iface(This);
673 return refcount;
676 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
678 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
679 ULONG refcount = InterlockedDecrement(&This->ref4);
681 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
683 if (refcount == 0)
685 ddraw_surface_release_iface(This);
688 return refcount;
691 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
693 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
694 ULONG refcount = InterlockedDecrement(&This->ref3);
696 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
698 if (refcount == 0)
700 ddraw_surface_release_iface(This);
703 return refcount;
706 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
708 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
709 ULONG refcount = InterlockedDecrement(&This->ref2);
711 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
713 if (refcount == 0)
715 ddraw_surface_release_iface(This);
718 return refcount;
721 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
723 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
724 ULONG refcount = InterlockedDecrement(&This->ref1);
726 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
728 if (refcount == 0)
730 ddraw_surface_release_iface(This);
733 return refcount;
736 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
738 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
739 ULONG refcount = InterlockedDecrement(&This->gamma_count);
741 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
743 if (refcount == 0)
745 ddraw_surface_release_iface(This);
748 return refcount;
751 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
753 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
755 TRACE("iface %p.\n", iface);
757 return IUnknown_Release(surface->texture_outer);
760 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
762 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
764 TRACE("iface %p.\n", iface);
766 return IUnknown_Release(surface->texture_outer);
769 /*****************************************************************************
770 * IDirectDrawSurface7::GetAttachedSurface
772 * Returns an attached surface with the requested caps. Surface attachment
773 * and complex surfaces are not clearly described by the MSDN or sdk,
774 * so this method is tricky and likely to contain problems.
775 * This implementation searches the complex list first, then the
776 * attachment chain.
778 * The chains are searched from This down to the last surface in the chain,
779 * not from the first element in the chain. The first surface found is
780 * returned. The MSDN says that this method fails if more than one surface
781 * matches the caps, but it is not sure if that is right. The attachment
782 * structure may not even allow two matching surfaces.
784 * The found surface is AddRef-ed before it is returned.
786 * Params:
787 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
788 * Surface: Address to store the found surface
790 * Returns:
791 * DD_OK on success
792 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
793 * DDERR_NOTFOUND if no surface was found
795 *****************************************************************************/
796 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
797 DDSCAPS2 *caps, IDirectDrawSurface7 **surface)
799 struct ddraw_surface *head_surface = impl_from_IDirectDrawSurface7(iface);
800 struct ddraw_surface *surf;
801 DDSCAPS2 our_caps;
802 int i;
804 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, surface);
806 if (ddraw_surface_is_lost(head_surface))
808 WARN("Surface %p is lost.\n", head_surface);
810 *surface = NULL;
811 return DDERR_SURFACELOST;
814 wined3d_mutex_lock();
816 if(head_surface->version < 7)
818 /* Earlier dx apps put garbage into these members, clear them */
819 our_caps.dwCaps = caps->dwCaps;
820 our_caps.dwCaps2 = 0;
821 our_caps.dwCaps3 = 0;
822 our_caps.u1.dwCaps4 = 0;
824 else
826 our_caps = *caps;
829 TRACE("head_surface %p, looking for caps %#x, %#x, %#x, %#x.\n", head_surface, our_caps.dwCaps,
830 our_caps.dwCaps2, our_caps.dwCaps3, our_caps.u1.dwCaps4); /* FIXME: Better debugging */
832 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
834 surf = head_surface->complex_array[i];
835 if(!surf) break;
837 TRACE("Surface %p, caps %#x, %#x, %#x, %#x.\n", surf,
838 surf->surface_desc.ddsCaps.dwCaps,
839 surf->surface_desc.ddsCaps.dwCaps2,
840 surf->surface_desc.ddsCaps.dwCaps3,
841 surf->surface_desc.ddsCaps.u1.dwCaps4);
843 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
844 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
846 /* MSDN: "This method fails if more than one surface is attached
847 * that matches the capabilities requested."
849 * Not sure how to test this.
852 TRACE("head_surface %p, returning surface %p.\n", head_surface, surf);
853 *surface = &surf->IDirectDrawSurface7_iface;
854 ddraw_surface7_AddRef(*surface);
855 wined3d_mutex_unlock();
857 return DD_OK;
861 /* Next, look at the attachment chain */
862 surf = head_surface;
864 while( (surf = surf->next_attached) )
866 TRACE("Surface %p, caps %#x, %#x, %#x, %#x.\n", surf,
867 surf->surface_desc.ddsCaps.dwCaps,
868 surf->surface_desc.ddsCaps.dwCaps2,
869 surf->surface_desc.ddsCaps.dwCaps3,
870 surf->surface_desc.ddsCaps.u1.dwCaps4);
872 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
873 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
875 TRACE("head_surface %p, returning surface %p.\n", head_surface, surf);
876 *surface = &surf->IDirectDrawSurface7_iface;
877 ddraw_surface7_AddRef(*surface);
878 wined3d_mutex_unlock();
879 return DD_OK;
883 TRACE("head_surface %p, didn't find a valid surface.\n", head_surface);
885 wined3d_mutex_unlock();
887 *surface = NULL;
888 return DDERR_NOTFOUND;
891 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
892 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
894 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
895 struct ddraw_surface *attachment_impl;
896 IDirectDrawSurface7 *attachment7;
897 HRESULT hr;
899 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
901 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
902 caps, &attachment7);
903 if (FAILED(hr))
905 *attachment = NULL;
906 return hr;
908 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
909 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
910 ddraw_surface4_AddRef(*attachment);
911 ddraw_surface7_Release(attachment7);
913 return hr;
916 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
917 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
919 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
920 struct ddraw_surface *attachment_impl;
921 IDirectDrawSurface7 *attachment7;
922 DDSCAPS2 caps2;
923 HRESULT hr;
925 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
927 caps2.dwCaps = caps->dwCaps;
928 caps2.dwCaps2 = 0;
929 caps2.dwCaps3 = 0;
930 caps2.u1.dwCaps4 = 0;
932 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
933 &caps2, &attachment7);
934 if (FAILED(hr))
936 *attachment = NULL;
937 return hr;
939 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
940 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
941 ddraw_surface3_AddRef(*attachment);
942 ddraw_surface7_Release(attachment7);
944 return hr;
947 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
948 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
950 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
951 struct ddraw_surface *attachment_impl;
952 IDirectDrawSurface7 *attachment7;
953 DDSCAPS2 caps2;
954 HRESULT hr;
956 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
958 caps2.dwCaps = caps->dwCaps;
959 caps2.dwCaps2 = 0;
960 caps2.dwCaps3 = 0;
961 caps2.u1.dwCaps4 = 0;
963 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
964 &caps2, &attachment7);
965 if (FAILED(hr))
967 *attachment = NULL;
968 return hr;
970 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
971 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
972 ddraw_surface2_AddRef(*attachment);
973 ddraw_surface7_Release(attachment7);
975 return hr;
978 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
979 DDSCAPS *caps, IDirectDrawSurface **attachment)
981 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
982 struct ddraw_surface *attachment_impl;
983 IDirectDrawSurface7 *attachment7;
984 DDSCAPS2 caps2;
985 HRESULT hr;
987 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
989 caps2.dwCaps = caps->dwCaps;
990 caps2.dwCaps2 = 0;
991 caps2.dwCaps3 = 0;
992 caps2.u1.dwCaps4 = 0;
994 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
995 &caps2, &attachment7);
996 if (FAILED(hr))
998 *attachment = NULL;
999 return hr;
1001 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
1002 *attachment = &attachment_impl->IDirectDrawSurface_iface;
1003 ddraw_surface1_AddRef(*attachment);
1004 ddraw_surface7_Release(attachment7);
1006 return hr;
1009 /*****************************************************************************
1010 * IDirectDrawSurface7::Lock
1012 * Locks the surface and returns a pointer to the surface's memory
1014 * Params:
1015 * Rect: Rectangle to lock. If NULL, the whole surface is locked
1016 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
1017 * Flags: Locking flags, e.g Read only or write only
1018 * h: An event handle that's not used and must be NULL
1020 * Returns:
1021 * DD_OK on success
1022 * DDERR_INVALIDPARAMS if DDSD is NULL
1024 *****************************************************************************/
1025 static HRESULT surface_lock(struct ddraw_surface *surface,
1026 RECT *rect, DDSURFACEDESC2 *surface_desc, unsigned int surface_desc_size,
1027 DWORD flags, HANDLE h)
1029 struct wined3d_map_desc map_desc;
1030 unsigned int wined3d_flags;
1031 struct wined3d_box box;
1032 HRESULT hr = DD_OK;
1034 TRACE("surface %p, rect %s, surface_desc %p, surface_desc_size %u, flags %#x, h %p.\n",
1035 surface, wine_dbgstr_rect(rect), surface_desc, surface_desc_size, flags, h);
1037 /* surface->surface_desc.dwWidth and dwHeight are changeable, thus lock */
1038 wined3d_mutex_lock();
1040 /* Should I check for the handle to be NULL?
1042 * The DDLOCK flags and the D3DLOCK flags are equal
1043 * for the supported values. The others are ignored by WineD3D
1046 /* Windows zeroes this if the rect is invalid */
1047 surface_desc->lpSurface = NULL;
1049 if (rect)
1051 if ((rect->left < 0) || (rect->top < 0)
1052 || (rect->left > rect->right) || (rect->right > surface->surface_desc.dwWidth)
1053 || (rect->top > rect->bottom) || (rect->bottom > surface->surface_desc.dwHeight))
1055 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
1056 wined3d_mutex_unlock();
1057 return DDERR_INVALIDPARAMS;
1059 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
1062 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1063 hr = ddraw_surface_update_frontbuffer(surface, rect, TRUE, 0);
1064 if (SUCCEEDED(hr))
1066 wined3d_flags = wined3dmapflags_from_ddrawmapflags(flags);
1067 hr = wined3d_resource_map(wined3d_texture_get_resource
1068 (ddraw_surface_get_default_texture(surface, wined3d_flags & WINED3D_MAP_WRITE ? DDRAW_SURFACE_RW
1069 : DDRAW_SURFACE_READ)), surface->sub_resource_idx, &map_desc, rect ? &box : NULL, wined3d_flags);
1071 if (FAILED(hr))
1073 wined3d_mutex_unlock();
1074 switch(hr)
1076 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
1077 * specific error. But since wined3d returns that error in this only occasion,
1078 * keep d3d8 and d3d9 free from the return value override. There are many different
1079 * places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it is much easier
1080 * to do it in one place in ddraw.
1082 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
1083 default: return hr;
1087 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1089 if (flags & DDLOCK_READONLY)
1090 SetRectEmpty(&surface->ddraw->primary_lock);
1091 else if (rect)
1092 surface->ddraw->primary_lock = *rect;
1093 else
1094 SetRect(&surface->ddraw->primary_lock, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
1097 /* Windows does not set DDSD_LPSURFACE on locked surfaces. */
1098 DD_STRUCT_COPY_BYSIZE_(surface_desc, &surface->surface_desc, surface_desc_size, surface->surface_desc.dwSize);
1099 surface_desc->lpSurface = map_desc.data;
1101 TRACE("locked surface returning description :\n");
1102 if (TRACE_ON(ddraw))
1103 DDRAW_dump_surface_desc(surface_desc);
1105 wined3d_mutex_unlock();
1107 return DD_OK;
1110 static BOOL surface_validate_lock_desc(struct ddraw_surface *surface,
1111 const DDSURFACEDESC *desc, unsigned int *size)
1113 if (!desc)
1114 return FALSE;
1116 if (desc->dwSize == sizeof(DDSURFACEDESC) || desc->dwSize == sizeof(DDSURFACEDESC2))
1118 *size = desc->dwSize;
1119 return TRUE;
1122 if (surface->version == 7
1123 && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE
1124 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
1126 if (desc->dwSize >= sizeof(DDSURFACEDESC2))
1127 *size = sizeof(DDSURFACEDESC2);
1128 else
1129 *size = sizeof(DDSURFACEDESC);
1130 return TRUE;
1133 WARN("Invalid structure size %u.\n", desc->dwSize);
1134 return FALSE;
1137 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1138 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1140 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1141 unsigned int surface_desc_size;
1143 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1144 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1146 if (!surface_validate_lock_desc(surface, (DDSURFACEDESC *)surface_desc, &surface_desc_size))
1147 return DDERR_INVALIDPARAMS;
1149 if (ddraw_surface_is_lost(surface))
1151 WARN("Surface is lost.\n");
1152 return DDERR_SURFACELOST;
1155 return surface_lock(surface, rect, surface_desc, surface_desc_size, flags, h);
1158 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1159 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1161 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1162 unsigned int surface_desc_size;
1164 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1165 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1167 if (!surface_validate_lock_desc(surface, (DDSURFACEDESC *)surface_desc, &surface_desc_size))
1168 return DDERR_INVALIDPARAMS;
1170 if (ddraw_surface_is_lost(surface))
1172 WARN("Surface is lost.\n");
1173 return DDERR_SURFACELOST;
1176 return surface_lock(surface, rect, surface_desc, surface_desc_size, flags, h);
1179 static HRESULT ddraw_surface_lock_ddsd(struct ddraw_surface *surface, RECT *rect,
1180 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1182 unsigned int surface_desc_size;
1183 DDSURFACEDESC2 surface_desc2;
1184 HRESULT hr;
1186 if (!surface_validate_lock_desc(surface, surface_desc, &surface_desc_size))
1187 return DDERR_INVALIDPARAMS;
1189 if (ddraw_surface_is_lost(surface))
1191 WARN("Surface is lost.\n");
1192 return DDERR_SURFACELOST;
1195 surface_desc2.dwSize = surface_desc->dwSize;
1196 surface_desc2.dwFlags = 0;
1197 hr = surface_lock(surface, rect, &surface_desc2, surface_desc_size, flags, h);
1198 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1199 surface_desc->dwSize = surface_desc2.dwSize;
1200 return hr;
1203 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1204 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1206 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1208 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1209 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1211 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1214 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1215 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1217 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1219 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1220 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1222 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1225 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1226 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1228 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1230 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1231 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1233 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1236 /* FRAPS hooks IDirectDrawSurface::Unlock and expects the version 1 method to be called when the
1237 * game uses later interfaces. */
1238 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1240 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1241 HRESULT hr;
1243 TRACE("iface %p, data %p.\n", iface, data);
1245 wined3d_mutex_lock();
1246 hr = wined3d_resource_unmap(wined3d_texture_get_resource
1247 (ddraw_surface_get_default_texture(surface, 0)), surface->sub_resource_idx);
1248 if (SUCCEEDED(hr) && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1249 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE, 0);
1250 wined3d_mutex_unlock();
1252 return hr;
1255 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *rect)
1257 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1259 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1261 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, NULL);
1264 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *rect)
1266 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1268 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1270 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, NULL);
1273 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1275 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1277 TRACE("iface %p, data %p.\n", iface, data);
1279 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, data);
1282 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1284 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1286 TRACE("iface %p, data %p.\n", iface, data);
1288 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, data);
1291 static unsigned int ddraw_swap_interval_from_flags(DWORD flags)
1293 if (flags & DDFLIP_NOVSYNC)
1294 return 0;
1296 switch (flags & (DDFLIP_INTERVAL2 | DDFLIP_INTERVAL3 | DDFLIP_INTERVAL4))
1298 case DDFLIP_INTERVAL2:
1299 return 2;
1300 case DDFLIP_INTERVAL3:
1301 return 3;
1302 case DDFLIP_INTERVAL4:
1303 return 4;
1304 default:
1305 return 1;
1309 /* FRAPS hooks IDirectDrawSurface::Flip and expects the version 1 method to be called when the
1310 * game uses later interfaces. */
1311 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Flip(IDirectDrawSurface *iface,
1312 IDirectDrawSurface *src, DWORD flags)
1314 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
1315 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
1316 struct ddraw_texture *dst_ddraw_texture, *src_ddraw_texture;
1317 struct wined3d_rendertarget_view *tmp_rtv, *src_rtv, *rtv;
1318 DDSCAPS caps = {DDSCAPS_FLIP};
1319 struct wined3d_texture *texture, *draw_texture;
1320 IDirectDrawSurface *current;
1321 void *texture_memory;
1322 HRESULT hr;
1324 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1326 if (src == iface || !(dst_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1327 return DDERR_NOTFLIPPABLE;
1329 if (ddraw_surface_is_lost(dst_impl))
1330 return DDERR_SURFACELOST;
1332 wined3d_mutex_lock();
1334 if ((dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1335 && !(dst_impl->ddraw->cooperative_level & DDSCL_EXCLUSIVE))
1337 WARN("Not in exclusive mode.\n");
1338 wined3d_mutex_unlock();
1339 return DDERR_NOEXCLUSIVEMODE;
1342 tmp_rtv = ddraw_surface_get_rendertarget_view(dst_impl);
1343 if (dst_impl->sub_resource_idx)
1344 ERR("Invalid sub-resource index %u on surface %p.\n", dst_impl->sub_resource_idx, dst_impl);
1345 texture = dst_impl->wined3d_texture;
1346 rtv = wined3d_device_get_rendertarget_view(dst_impl->ddraw->wined3d_device, 0);
1347 dst_ddraw_texture = wined3d_texture_get_parent(dst_impl->wined3d_texture);
1348 texture_memory = dst_ddraw_texture->texture_memory;
1349 draw_texture = dst_impl->draw_texture;
1351 if (src_impl)
1353 for (current = iface; current != src;)
1355 if (FAILED(hr = ddraw_surface1_GetAttachedSurface(current, &caps, &current)))
1357 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1358 wined3d_mutex_unlock();
1359 return DDERR_NOTFLIPPABLE;
1361 ddraw_surface1_Release(current);
1362 if (current == iface)
1364 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1365 wined3d_mutex_unlock();
1366 return DDERR_NOTFLIPPABLE;
1370 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1371 if (rtv == dst_impl->wined3d_rtv)
1372 wined3d_device_context_set_rendertarget_view(dst_impl->ddraw->immediate_context, 0, src_rtv, FALSE);
1373 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1374 dst_impl->wined3d_rtv = src_rtv;
1375 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1376 if (src_impl->draw_texture)
1377 wined3d_texture_set_sub_resource_parent(src_impl->draw_texture, 0, dst_impl);
1378 src_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1379 dst_ddraw_texture->texture_memory = src_ddraw_texture->texture_memory;
1380 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), dst_ddraw_texture);
1381 if (src_impl->draw_texture)
1382 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->draw_texture), dst_ddraw_texture);
1383 dst_ddraw_texture = src_ddraw_texture;
1384 if (src_impl->sub_resource_idx)
1385 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1386 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1387 dst_impl->draw_texture = src_impl->draw_texture;
1389 else
1391 for (current = iface;;)
1393 if (FAILED(hr = ddraw_surface1_GetAttachedSurface(current, &caps, &current)))
1395 ERR("Can't find a flip target\n");
1396 wined3d_mutex_unlock();
1397 return DDERR_NOTFLIPPABLE; /* Unchecked */
1399 ddraw_surface1_Release(current);
1400 if (current == iface)
1402 dst_impl = impl_from_IDirectDrawSurface(iface);
1403 break;
1406 src_impl = impl_from_IDirectDrawSurface(current);
1407 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1408 if (rtv == dst_impl->wined3d_rtv)
1409 wined3d_device_context_set_rendertarget_view(dst_impl->ddraw->immediate_context, 0, src_rtv, FALSE);
1410 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1411 dst_impl->wined3d_rtv = src_rtv;
1412 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1413 if (src_impl->draw_texture)
1414 wined3d_texture_set_sub_resource_parent(src_impl->draw_texture, 0, dst_impl);
1415 src_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1416 dst_ddraw_texture->texture_memory = src_ddraw_texture->texture_memory;
1417 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), dst_ddraw_texture);
1418 if (src_impl->draw_texture)
1419 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->draw_texture), dst_ddraw_texture);
1420 dst_ddraw_texture = src_ddraw_texture;
1421 if (src_impl->sub_resource_idx)
1422 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1423 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1424 dst_impl->draw_texture = src_impl->draw_texture;
1425 dst_impl = src_impl;
1429 /* We don't have to worry about potential texture bindings, since
1430 * flippable surfaces can never be textures. */
1431 if (rtv == src_impl->wined3d_rtv)
1432 wined3d_device_context_set_rendertarget_view(dst_impl->ddraw->immediate_context, 0, tmp_rtv, FALSE);
1433 wined3d_rendertarget_view_set_parent(tmp_rtv, src_impl);
1434 src_impl->wined3d_rtv = tmp_rtv;
1435 wined3d_texture_set_sub_resource_parent(texture, 0, src_impl);
1436 if (draw_texture)
1437 wined3d_texture_set_sub_resource_parent(draw_texture, 0, src_impl);
1438 dst_ddraw_texture->texture_memory = texture_memory;
1439 wined3d_resource_set_parent(wined3d_texture_get_resource(texture), dst_ddraw_texture);
1440 if (draw_texture)
1441 wined3d_resource_set_parent(wined3d_texture_get_resource(draw_texture), dst_ddraw_texture);
1442 src_impl->wined3d_texture = texture;
1443 src_impl->draw_texture = draw_texture;
1445 if (flags & ~(DDFLIP_NOVSYNC | DDFLIP_INTERVAL2 | DDFLIP_INTERVAL3 | DDFLIP_INTERVAL4))
1447 static UINT once;
1448 if (!once++)
1449 FIXME("Ignoring flags %#x.\n", flags);
1450 else
1451 WARN("Ignoring flags %#x.\n", flags);
1454 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1455 hr = ddraw_surface_update_frontbuffer(dst_impl, NULL, FALSE, ddraw_swap_interval_from_flags(flags));
1456 else
1457 hr = DD_OK;
1459 wined3d_mutex_unlock();
1461 return hr;
1464 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Flip(IDirectDrawSurface7 *iface,
1465 IDirectDrawSurface7 *src, DWORD flags)
1467 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1468 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src);
1470 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1472 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1473 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1476 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Flip(IDirectDrawSurface4 *iface,
1477 IDirectDrawSurface4 *src, DWORD flags)
1479 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1480 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
1482 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1484 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1485 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1488 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Flip(IDirectDrawSurface3 *iface,
1489 IDirectDrawSurface3 *src, DWORD flags)
1491 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1492 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src);
1494 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1496 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1497 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1500 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Flip(IDirectDrawSurface2 *iface,
1501 IDirectDrawSurface2 *src, DWORD flags)
1503 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1504 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src);
1506 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1508 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1509 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1512 static HRESULT ddraw_surface_blt(struct ddraw_surface *dst_surface, const RECT *dst_rect,
1513 struct ddraw_surface *src_surface, const RECT *src_rect, DWORD flags, DWORD fill_colour,
1514 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1516 struct ddraw *ddraw = dst_surface->ddraw;
1517 struct wined3d_device *wined3d_device = ddraw->wined3d_device;
1518 struct wined3d_color colour;
1519 DWORD wined3d_flags;
1521 if (flags & DDBLT_COLORFILL)
1523 wined3d_flags = WINED3DCLEAR_TARGET;
1524 if (!(flags & DDBLT_ASYNC))
1525 wined3d_flags |= WINED3DCLEAR_SYNCHRONOUS;
1527 if (!wined3d_colour_from_ddraw_colour(&dst_surface->surface_desc.u4.ddpfPixelFormat,
1528 dst_surface->palette, fill_colour, &colour))
1529 return DDERR_INVALIDPARAMS;
1531 wined3d_device_apply_stateblock(wined3d_device, ddraw->state);
1532 ddraw_surface_get_draw_texture(dst_surface, dst_rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE);
1533 return wined3d_device_context_clear_rendertarget_view(ddraw->immediate_context,
1534 ddraw_surface_get_rendertarget_view(dst_surface),
1535 dst_rect, wined3d_flags, &colour, 0.0f, 0);
1538 if (flags & DDBLT_DEPTHFILL)
1540 wined3d_flags = WINED3DCLEAR_ZBUFFER;
1541 if (!(flags & DDBLT_ASYNC))
1542 wined3d_flags |= WINED3DCLEAR_SYNCHRONOUS;
1544 if (!wined3d_colour_from_ddraw_colour(&dst_surface->surface_desc.u4.ddpfPixelFormat,
1545 dst_surface->palette, fill_colour, &colour))
1546 return DDERR_INVALIDPARAMS;
1548 wined3d_device_apply_stateblock(wined3d_device, ddraw->state);
1549 ddraw_surface_get_draw_texture(dst_surface, dst_rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE);
1550 return wined3d_device_context_clear_rendertarget_view(ddraw->immediate_context,
1551 ddraw_surface_get_rendertarget_view(dst_surface),
1552 dst_rect, wined3d_flags, NULL, colour.r, 0);
1555 wined3d_flags = flags & ~DDBLT_ASYNC;
1556 if (wined3d_flags & ~WINED3D_BLT_MASK)
1558 FIXME("Unhandled flags %#x.\n", flags);
1559 return E_NOTIMPL;
1562 if (!(flags & DDBLT_ASYNC))
1563 wined3d_flags |= WINED3D_BLT_SYNCHRONOUS;
1565 return wined3d_device_context_blt(ddraw->immediate_context,
1566 ddraw_surface_get_any_texture(dst_surface, DDRAW_SURFACE_RW), dst_surface->sub_resource_idx, dst_rect,
1567 ddraw_surface_get_any_texture(src_surface, DDRAW_SURFACE_READ), src_surface->sub_resource_idx, src_rect,
1568 wined3d_flags, fx, filter);
1571 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1572 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags, DWORD fill_colour,
1573 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1575 RECT src_rect, dst_rect;
1576 float scale_x, scale_y;
1577 const RECT *clip_rect;
1578 UINT clip_list_size;
1579 RGNDATA *clip_list;
1580 HRESULT hr = DD_OK;
1581 UINT i;
1583 if (!dst_rect_in)
1584 SetRect(&dst_rect, 0, 0, dst_surface->surface_desc.dwWidth,
1585 dst_surface->surface_desc.dwHeight);
1586 else
1587 dst_rect = *dst_rect_in;
1589 if (IsRectEmpty(&dst_rect))
1590 return DDERR_INVALIDRECT;
1592 if (src_surface)
1594 if (!src_rect_in)
1595 SetRect(&src_rect, 0, 0, src_surface->surface_desc.dwWidth,
1596 src_surface->surface_desc.dwHeight);
1597 else
1598 src_rect = *src_rect_in;
1600 if (IsRectEmpty(&src_rect))
1601 return DDERR_INVALIDRECT;
1603 else
1605 SetRectEmpty(&src_rect);
1608 if (!dst_surface->clipper)
1610 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1611 hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect, TRUE, 0);
1612 if (SUCCEEDED(hr))
1613 hr = ddraw_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, flags, fill_colour, fx, filter);
1614 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
1615 hr = ddraw_surface_update_frontbuffer(dst_surface, &dst_rect, FALSE, 0);
1617 return hr;
1620 if (!ddraw_clipper_is_valid(dst_surface->clipper))
1622 FIXME("Attempting to blit with an invalid clipper.\n");
1623 return DDERR_INVALIDPARAMS;
1626 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1627 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1629 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1630 &dst_rect, NULL, &clip_list_size)))
1632 WARN("Failed to get clip list size, hr %#x.\n", hr);
1633 return hr;
1636 if (!(clip_list = heap_alloc(clip_list_size)))
1638 WARN("Failed to allocate clip list.\n");
1639 return E_OUTOFMEMORY;
1642 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1643 &dst_rect, clip_list, &clip_list_size)))
1645 WARN("Failed to get clip list, hr %#x.\n", hr);
1646 heap_free(clip_list);
1647 return hr;
1650 clip_rect = (RECT *)clip_list->Buffer;
1651 for (i = 0; i < clip_list->rdh.nCount; ++i)
1653 RECT src_rect_clipped = src_rect;
1655 if (src_surface)
1657 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1658 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1659 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1660 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1662 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1664 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE, 0)))
1665 break;
1669 if (FAILED(hr = ddraw_surface_blt(dst_surface, &clip_rect[i],
1670 src_surface, &src_rect_clipped, flags, fill_colour, fx, filter)))
1671 break;
1673 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1675 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE, 0)))
1676 break;
1680 heap_free(clip_list);
1681 return hr;
1684 /* FRAPS hooks IDirectDrawSurface::Blt and expects the version 1 method to be called when the
1685 * game uses later interfaces. */
1686 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1687 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1689 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
1690 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1691 struct wined3d_blt_fx wined3d_fx = {0};
1692 DWORD unsupported_flags;
1693 DWORD fill_colour = 0;
1694 HRESULT hr = DD_OK;
1695 DDBLTFX rop_fx;
1697 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1698 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1700 unsupported_flags = DDBLT_ALPHADEST
1701 | DDBLT_ALPHADESTCONSTOVERRIDE
1702 | DDBLT_ALPHADESTNEG
1703 | DDBLT_ALPHADESTSURFACEOVERRIDE
1704 | DDBLT_ALPHAEDGEBLEND
1705 | DDBLT_ALPHASRC
1706 | DDBLT_ALPHASRCCONSTOVERRIDE
1707 | DDBLT_ALPHASRCNEG
1708 | DDBLT_ALPHASRCSURFACEOVERRIDE
1709 | DDBLT_ZBUFFER
1710 | DDBLT_ZBUFFERDESTCONSTOVERRIDE
1711 | DDBLT_ZBUFFERDESTOVERRIDE
1712 | DDBLT_ZBUFFERSRCCONSTOVERRIDE
1713 | DDBLT_ZBUFFERSRCOVERRIDE;
1714 if (flags & unsupported_flags)
1716 WARN("Ignoring unsupported flags %#x.\n", flags & unsupported_flags);
1717 flags &= ~unsupported_flags;
1720 if ((flags & DDBLT_KEYSRCOVERRIDE) && (!fx || flags & DDBLT_KEYSRC))
1722 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1723 return DDERR_INVALIDPARAMS;
1726 if ((flags & DDBLT_KEYDESTOVERRIDE) && (!fx || flags & DDBLT_KEYDEST))
1728 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1729 return DDERR_INVALIDPARAMS;
1732 if (flags & DDBLT_DDROPS)
1734 FIXME("DDBLT_DDROPS not implemented.\n");
1735 if (fx)
1736 FIXME(" rop %#x, pattern %p.\n", fx->dwDDROP, fx->u5.lpDDSPattern);
1737 return DDERR_NORASTEROPHW;
1740 wined3d_mutex_lock();
1742 if (flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1744 if (flags & DDBLT_ROP)
1746 wined3d_mutex_unlock();
1747 WARN("DDBLT_ROP used with DDBLT_COLORFILL or DDBLT_DEPTHFILL, returning DDERR_INVALIDPARAMS.\n");
1748 return DDERR_INVALIDPARAMS;
1750 if (src_impl)
1752 wined3d_mutex_unlock();
1753 WARN("Depth or colorfill is not compatible with source surfaces, returning DDERR_INVALIDPARAMS\n");
1754 return DDERR_INVALIDPARAMS;
1756 if (!fx)
1758 wined3d_mutex_unlock();
1759 WARN("Depth or colorfill used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1760 return DDERR_INVALIDPARAMS;
1763 if ((flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) == (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1764 flags &= ~DDBLT_DEPTHFILL;
1766 if ((dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_COLORFILL))
1768 wined3d_mutex_unlock();
1769 WARN("DDBLT_COLORFILL used on a depth buffer, returning DDERR_INVALIDPARAMS.\n");
1770 return DDERR_INVALIDPARAMS;
1772 if (!(dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_DEPTHFILL))
1774 wined3d_mutex_unlock();
1775 WARN("DDBLT_DEPTHFILL used on a color buffer, returning DDERR_INVALIDPARAMS.\n");
1776 return DDERR_INVALIDPARAMS;
1780 if (flags & DDBLT_ROP)
1782 if (!fx)
1784 wined3d_mutex_unlock();
1785 WARN("DDBLT_ROP used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1786 return DDERR_INVALIDPARAMS;
1789 if (src_impl && src_rect
1790 && ((ULONG)src_rect->left >= src_rect->right || src_rect->right > src_impl->surface_desc.dwWidth
1791 || (ULONG)src_rect->top >= src_rect->bottom || src_rect->bottom > src_impl->surface_desc.dwHeight))
1793 wined3d_mutex_unlock();
1794 WARN("Invalid source rectangle.\n");
1795 return DDERR_INVALIDRECT;
1798 flags &= ~DDBLT_ROP;
1799 switch (fx->dwROP)
1801 case SRCCOPY:
1802 break;
1804 case WHITENESS:
1805 case BLACKNESS:
1806 rop_fx = *fx;
1808 if (fx->dwROP == WHITENESS)
1809 rop_fx.u5.dwFillColor = 0xffffffff;
1810 else
1811 rop_fx.u5.dwFillColor = 0;
1813 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1814 flags |= DDBLT_DEPTHFILL;
1815 else
1816 flags |= DDBLT_COLORFILL;
1818 fx = &rop_fx;
1819 break;
1821 default:
1822 wined3d_mutex_unlock();
1823 WARN("Unsupported ROP %#x used, returning DDERR_NORASTEROPHW.\n", fx->dwROP);
1824 return DDERR_NORASTEROPHW;
1828 if (!(flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) && !src_impl)
1830 WARN("No source surface.\n");
1831 wined3d_mutex_unlock();
1832 return DDERR_INVALIDPARAMS;
1835 if (flags & DDBLT_KEYSRC && (!src_impl || !(src_impl->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1837 WARN("DDBLT_KEYSRC blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1838 wined3d_mutex_unlock();
1839 return DDERR_INVALIDPARAMS;
1841 if (flags & DDBLT_KEYDEST && !(dst_impl->surface_desc.dwFlags & DDSD_CKDESTBLT))
1843 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1844 wined3d_mutex_unlock();
1845 return DDERR_INVALIDPARAMS;
1848 if (fx)
1850 wined3d_fx.fx = fx->dwDDFX;
1851 fill_colour = fx->u5.dwFillColor;
1852 wined3d_fx.dst_color_key.color_space_low_value = fx->ddckDestColorkey.dwColorSpaceLowValue;
1853 wined3d_fx.dst_color_key.color_space_high_value = fx->ddckDestColorkey.dwColorSpaceHighValue;
1854 wined3d_fx.src_color_key.color_space_low_value = fx->ddckSrcColorkey.dwColorSpaceLowValue;
1855 wined3d_fx.src_color_key.color_space_high_value = fx->ddckSrcColorkey.dwColorSpaceHighValue;
1858 hr = ddraw_surface_blt_clipped(dst_impl, dst_rect, src_impl,
1859 src_rect, flags, fill_colour, fx ? &wined3d_fx : NULL, WINED3D_TEXF_LINEAR);
1861 wined3d_mutex_unlock();
1862 switch(hr)
1864 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1865 default: return hr;
1869 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *dst_rect,
1870 IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1872 struct ddraw_surface *dst = impl_from_IDirectDrawSurface7(iface);
1873 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface7(src_surface);
1875 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1876 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1878 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1879 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1882 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1883 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1885 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1886 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1888 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1889 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1891 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1892 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1895 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1896 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1898 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1899 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1901 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1902 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1904 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1905 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1908 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1909 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1911 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1912 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1914 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1915 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1917 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1918 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1921 /*****************************************************************************
1922 * IDirectDrawSurface7::AddAttachedSurface
1924 * Attaches a surface to another surface. How the surface attachments work
1925 * is not totally understood yet, and this method is prone to problems.
1926 * The surface that is attached is AddRef-ed.
1928 * Tests with complex surfaces suggest that the surface attachments form a
1929 * tree, but no method to test this has been found yet.
1931 * The attachment list consists of a first surface (first_attached) and
1932 * for each surface a pointer to the next attached surface (next_attached).
1933 * For the first surface, and a surface that has no attachments
1934 * first_attached points to the surface itself. A surface that has
1935 * no successors in the chain has next_attached set to NULL.
1937 * Newly attached surfaces are attached right after the root surface.
1938 * If a surface is attached to a complex surface compound, it's attached to
1939 * the surface that the app requested, not the complex root. See
1940 * GetAttachedSurface for a description how surfaces are found.
1942 * This is how the current implementation works, and it was coded by looking
1943 * at the needs of the applications.
1945 * So far only Z-Buffer attachments are tested, and they are activated in
1946 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1947 * Back buffers should work in 2D mode, but they are not tested(They can be
1948 * attached in older iface versions). Rendering to the front buffer and
1949 * switching between that and double buffering is not yet implemented in
1950 * WineD3D, so for 3D it might have unexpected results.
1952 * ddraw_surface_attach_surface is the real thing,
1953 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1954 * performs additional checks. Version 7 of this interface is much more restrictive
1955 * than its predecessors.
1957 * Params:
1958 * Attach: Surface to attach to iface
1960 * Returns:
1961 * DD_OK on success
1962 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1964 *****************************************************************************/
1965 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1967 TRACE("surface %p, attachment %p.\n", This, Surf);
1969 if(Surf == This)
1970 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1972 wined3d_mutex_lock();
1974 /* Check if the surface is already attached somewhere */
1975 if (Surf->next_attached || Surf->first_attached != Surf)
1977 /* TODO: Test for the structure of the manual attachment. Is it a
1978 * chain or a list? What happens if one surface is attached to 2
1979 * different surfaces? */
1980 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1981 Surf, Surf->next_attached, Surf->first_attached);
1983 wined3d_mutex_unlock();
1984 return DDERR_SURFACEALREADYATTACHED;
1987 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1988 Surf->next_attached = This->next_attached;
1989 Surf->first_attached = This->first_attached;
1990 This->next_attached = Surf;
1992 /* Check if the WineD3D depth stencil needs updating */
1993 if (This->ddraw->d3ddevice)
1994 d3d_device_update_depth_stencil(This->ddraw->d3ddevice);
1996 wined3d_mutex_unlock();
1998 return DD_OK;
2001 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
2003 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
2004 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
2005 HRESULT hr;
2007 TRACE("iface %p, attachment %p.\n", iface, attachment);
2009 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
2010 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
2013 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
2014 attachment_impl->surface_desc.ddsCaps.dwCaps);
2015 return DDERR_CANNOTATTACHSURFACE;
2018 hr = ddraw_surface_attach_surface(This, attachment_impl);
2019 if (FAILED(hr))
2021 return hr;
2023 attachment_impl->attached_iface = (IUnknown *)attachment;
2024 IUnknown_AddRef(attachment_impl->attached_iface);
2025 return hr;
2028 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
2030 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2031 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
2032 HRESULT hr;
2034 TRACE("iface %p, attachment %p.\n", iface, attachment);
2036 /* Tests suggest that
2037 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
2038 * -> offscreen plain surfaces can be attached to primaries
2039 * -> primaries can be attached to offscreen plain surfaces
2040 * -> z buffers can be attached to primaries */
2041 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
2042 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
2044 /* Sizes have to match */
2045 if (attachment_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
2046 || attachment_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
2048 WARN("Surface sizes do not match.\n");
2049 return DDERR_CANNOTATTACHSURFACE;
2052 else if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
2053 || !(attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER)))
2055 WARN("Invalid attachment combination.\n");
2056 return DDERR_CANNOTATTACHSURFACE;
2059 if (FAILED(hr = ddraw_surface_attach_surface(surface, attachment_impl)))
2060 return hr;
2062 attachment_impl->attached_iface = (IUnknown *)attachment;
2063 IUnknown_AddRef(attachment_impl->attached_iface);
2064 return hr;
2067 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
2069 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2070 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2071 HRESULT hr;
2073 TRACE("iface %p, attachment %p.\n", iface, attachment);
2075 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2076 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2077 return hr;
2079 attachment_impl->attached_iface = (IUnknown *)attachment;
2080 IUnknown_AddRef(attachment_impl->attached_iface);
2081 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2082 return hr;
2085 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
2087 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2088 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2089 HRESULT hr;
2091 TRACE("iface %p, attachment %p.\n", iface, attachment);
2093 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2094 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2095 return hr;
2097 attachment_impl->attached_iface = (IUnknown *)attachment;
2098 IUnknown_AddRef(attachment_impl->attached_iface);
2099 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2100 return hr;
2103 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
2105 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2106 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2107 HRESULT hr;
2109 TRACE("iface %p, attachment %p.\n", iface, attachment);
2111 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2112 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2113 return hr;
2115 attachment_impl->attached_iface = (IUnknown *)attachment;
2116 IUnknown_AddRef(attachment_impl->attached_iface);
2117 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2118 return hr;
2121 /*****************************************************************************
2122 * IDirectDrawSurface7::DeleteAttachedSurface
2124 * Removes a surface from the attachment chain. The surface's refcount
2125 * is decreased by one after it has been removed
2127 * Params:
2128 * Flags: Some flags, not used by this implementation
2129 * Attach: Surface to detach
2131 * Returns:
2132 * DD_OK on success
2133 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
2135 *****************************************************************************/
2136 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
2137 struct ddraw_surface *attachment, IUnknown *detach_iface)
2139 struct ddraw_surface *prev = surface;
2141 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
2143 wined3d_mutex_lock();
2144 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
2146 wined3d_mutex_unlock();
2147 return DDERR_CANNOTDETACHSURFACE;
2150 if (attachment->attached_iface != detach_iface)
2152 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
2153 wined3d_mutex_unlock();
2154 return DDERR_SURFACENOTATTACHED;
2157 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
2158 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2160 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2161 /* FIXME: we should probably also subtract from dwMipMapCount of this
2162 * and all parent surfaces */
2165 /* Find the predecessor of the detached surface */
2166 while (prev->next_attached != attachment)
2168 if (!(prev = prev->next_attached))
2170 ERR("Failed to find predecessor of %p.\n", attachment);
2171 wined3d_mutex_unlock();
2172 return DDERR_SURFACENOTATTACHED;
2176 /* Unchain the surface */
2177 prev->next_attached = attachment->next_attached;
2178 attachment->next_attached = NULL;
2179 attachment->first_attached = attachment;
2181 /* Check if the wined3d depth stencil needs updating. Note that we don't
2182 * just call d3d_device_update_depth_stencil() here since it uses
2183 * QueryInterface(). Some applications, SCP - Containment Breach in
2184 * particular, modify the QueryInterface() pointer in the surface vtbl
2185 * but don't cleanup properly after the relevant dll is unloaded. */
2186 if (attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER
2187 && wined3d_device_get_depth_stencil_view(surface->ddraw->wined3d_device) == attachment->wined3d_rtv)
2188 wined3d_device_context_set_depth_stencil_view(surface->ddraw->immediate_context, NULL);
2189 wined3d_mutex_unlock();
2191 /* Set attached_iface to NULL before releasing it, the surface may go
2192 * away. */
2193 attachment->attached_iface = NULL;
2194 IUnknown_Release(detach_iface);
2196 return DD_OK;
2199 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
2200 DWORD flags, IDirectDrawSurface7 *attachment)
2202 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2203 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
2205 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2207 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2210 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
2211 DWORD flags, IDirectDrawSurface4 *attachment)
2213 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2214 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
2216 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2218 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2221 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
2222 DWORD flags, IDirectDrawSurface3 *attachment)
2224 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2225 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2227 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2229 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2232 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
2233 DWORD flags, IDirectDrawSurface2 *attachment)
2235 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2236 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2238 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2240 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2243 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
2244 DWORD flags, IDirectDrawSurface *attachment)
2246 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2247 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2249 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2251 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2254 /*****************************************************************************
2255 * IDirectDrawSurface7::AddOverlayDirtyRect
2257 * "This method is not currently implemented"
2259 * Params:
2260 * Rect: ?
2262 * Returns:
2263 * DDERR_UNSUPPORTED
2265 *****************************************************************************/
2266 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
2268 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
2270 return DDERR_UNSUPPORTED; /* unchecked */
2273 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
2275 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2277 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2279 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2282 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
2284 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2286 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2288 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2291 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
2293 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2295 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2297 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2300 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
2302 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2304 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2306 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2309 /*****************************************************************************
2310 * IDirectDrawSurface7::GetDC
2312 * Returns a GDI device context for the surface
2314 * Params:
2315 * hdc: Address of a HDC variable to store the dc to
2317 * Returns:
2318 * DD_OK on success
2319 * DDERR_INVALIDPARAMS if hdc is NULL
2321 *****************************************************************************/
2322 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *dc)
2324 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2325 HRESULT hr = DD_OK;
2327 TRACE("iface %p, dc %p.\n", iface, dc);
2329 if (!dc)
2330 return DDERR_INVALIDPARAMS;
2332 wined3d_mutex_lock();
2333 if (surface->dc)
2334 hr = DDERR_DCALREADYCREATED;
2335 else if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2336 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE, 0);
2337 if (SUCCEEDED(hr))
2338 hr = wined3d_texture_get_dc(ddraw_surface_get_default_texture(surface, DDRAW_SURFACE_RW), surface->sub_resource_idx, dc);
2340 if (SUCCEEDED(hr))
2342 surface->dc = *dc;
2344 if (format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
2346 const struct ddraw_palette *palette;
2348 if (surface->palette)
2349 palette = surface->palette;
2350 else if (surface->ddraw->primary)
2351 palette = surface->ddraw->primary->palette;
2352 else
2353 palette = NULL;
2355 if (palette)
2356 wined3d_palette_apply_to_dc(palette->wined3d_palette, *dc);
2360 wined3d_mutex_unlock();
2361 switch (hr)
2363 /* Some, but not all errors set *dc to NULL. E.g. DCALREADYCREATED
2364 * does not touch *dc. */
2365 case WINED3DERR_INVALIDCALL:
2366 *dc = NULL;
2367 return DDERR_CANTCREATEDC;
2369 default:
2370 return hr;
2374 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
2376 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2378 TRACE("iface %p, dc %p.\n", iface, dc);
2380 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2383 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
2385 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2387 TRACE("iface %p, dc %p.\n", iface, dc);
2389 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2392 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2394 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2396 TRACE("iface %p, dc %p.\n", iface, dc);
2398 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2401 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2403 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2405 TRACE("iface %p, dc %p.\n", iface, dc);
2407 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2410 /*****************************************************************************
2411 * IDirectDrawSurface7::ReleaseDC
2413 * Releases the DC that was constructed with GetDC
2415 * Params:
2416 * hdc: HDC to release
2418 * Returns:
2419 * DD_OK on success, error code otherwise.
2421 *****************************************************************************/
2422 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2424 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2425 HRESULT hr;
2427 TRACE("iface %p, dc %p.\n", iface, hdc);
2429 wined3d_mutex_lock();
2430 if (!surface->dc)
2432 hr = DDERR_NODC;
2434 else if (SUCCEEDED(hr = wined3d_texture_release_dc(ddraw_surface_get_default_texture(surface, 0),
2435 surface->sub_resource_idx, hdc)))
2437 surface->dc = NULL;
2438 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2439 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE, 0);
2441 wined3d_mutex_unlock();
2444 return hr;
2447 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2449 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2451 TRACE("iface %p, dc %p.\n", iface, dc);
2453 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2456 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2458 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2460 TRACE("iface %p, dc %p.\n", iface, dc);
2462 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2465 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2467 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2469 TRACE("iface %p, dc %p.\n", iface, dc);
2471 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2474 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2476 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2478 TRACE("iface %p, dc %p.\n", iface, dc);
2480 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2483 /*****************************************************************************
2484 * IDirectDrawSurface7::GetCaps
2486 * Returns the surface's caps
2488 * Params:
2489 * Caps: Address to write the caps to
2491 * Returns:
2492 * DD_OK on success
2493 * DDERR_INVALIDPARAMS if Caps is NULL
2495 *****************************************************************************/
2496 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2498 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2500 TRACE("iface %p, caps %p.\n", iface, Caps);
2502 if(!Caps)
2503 return DDERR_INVALIDPARAMS;
2505 *Caps = surface->surface_desc.ddsCaps;
2507 return DD_OK;
2510 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2512 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2514 TRACE("iface %p, caps %p.\n", iface, caps);
2516 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2519 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2521 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2522 DDSCAPS2 caps2;
2523 HRESULT hr;
2525 TRACE("iface %p, caps %p.\n", iface, caps);
2527 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2528 if (FAILED(hr)) return hr;
2530 caps->dwCaps = caps2.dwCaps;
2531 return hr;
2534 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2536 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2537 DDSCAPS2 caps2;
2538 HRESULT hr;
2540 TRACE("iface %p, caps %p.\n", iface, caps);
2542 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2543 if (FAILED(hr)) return hr;
2545 caps->dwCaps = caps2.dwCaps;
2546 return hr;
2549 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2551 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2552 DDSCAPS2 caps2;
2553 HRESULT hr;
2555 TRACE("iface %p, caps %p.\n", iface, caps);
2557 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2558 if (FAILED(hr)) return hr;
2560 caps->dwCaps = caps2.dwCaps;
2561 return hr;
2564 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD priority)
2566 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2567 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2568 HRESULT hr;
2569 struct wined3d_resource *resource;
2571 TRACE("iface %p, priority %u.\n", iface, priority);
2573 wined3d_mutex_lock();
2574 /* No need to check for offscreen plain surfaces or mipmap sublevels. SetPriority
2575 * calls on such surfaces segfault on Windows. */
2576 if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed))
2578 WARN("Called on non-managed texture returning DDERR_INVALIDPARAMS.\n");
2579 hr = DDERR_INVALIDPARAMS;
2581 else
2583 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2584 wined3d_resource_set_priority(resource, priority);
2585 if (surface->draw_texture)
2586 wined3d_resource_set_priority(wined3d_texture_get_resource(surface->draw_texture), priority);
2588 hr = DD_OK;
2590 wined3d_mutex_unlock();
2592 return hr;
2595 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *priority)
2597 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2598 const struct wined3d_resource *resource;
2599 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2600 HRESULT hr;
2602 TRACE("iface %p, priority %p.\n", iface, priority);
2604 wined3d_mutex_lock();
2605 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN)
2607 WARN("Called on offscreenplain surface, returning DDERR_INVALIDOBJECT.\n");
2608 hr = DDERR_INVALIDOBJECT;
2610 else if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed) || !surface->is_complex_root)
2612 WARN("Called on non-managed texture or non-root surface, returning DDERR_INVALIDPARAMS.\n");
2613 hr = DDERR_INVALIDPARAMS;
2615 else
2617 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2618 *priority = wined3d_resource_get_priority(resource);
2619 hr = DD_OK;
2621 wined3d_mutex_unlock();
2623 return hr;
2626 /*****************************************************************************
2627 * IDirectDrawSurface7::SetPrivateData
2629 * Stores some data in the surface that is intended for the application's
2630 * use.
2632 * Params:
2633 * tag: GUID that identifies the data
2634 * Data: Pointer to the private data
2635 * Size: Size of the private data
2636 * Flags: Some flags
2638 * Returns:
2639 * D3D_OK on success, error code otherwise.
2641 *****************************************************************************/
2642 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2643 REFGUID tag, void *data, DWORD size, DWORD flags)
2645 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2646 HRESULT hr;
2648 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2649 iface, debugstr_guid(tag), data, size, flags);
2651 if (!data)
2653 WARN("data is NULL, returning DDERR_INVALIDPARAMS.\n");
2654 return DDERR_INVALIDPARAMS;
2657 wined3d_mutex_lock();
2658 hr = wined3d_private_store_set_private_data(&surface->private_store, tag, data, size, flags);
2659 wined3d_mutex_unlock();
2660 return hr_ddraw_from_wined3d(hr);
2663 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2664 REFGUID tag, void *data, DWORD size, DWORD flags)
2666 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2668 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2669 iface, debugstr_guid(tag), data, size, flags);
2671 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2674 /*****************************************************************************
2675 * IDirectDrawSurface7::GetPrivateData
2677 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2679 * Params:
2680 * tag: GUID of the data to return
2681 * Data: Address where to write the data to
2682 * Size: Size of the buffer at Data
2684 * Returns:
2685 * DD_OK on success
2686 * DDERR_INVALIDPARAMS if Data is NULL
2688 *****************************************************************************/
2689 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *data, DWORD *size)
2691 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2692 const struct wined3d_private_data *stored_data;
2693 HRESULT hr;
2695 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2696 iface, debugstr_guid(tag), data, size);
2698 wined3d_mutex_lock();
2699 stored_data = wined3d_private_store_get_private_data(&surface->private_store, tag);
2700 if (!stored_data)
2702 hr = DDERR_NOTFOUND;
2703 goto done;
2705 if (!size)
2707 hr = DDERR_INVALIDPARAMS;
2708 goto done;
2710 if (*size < stored_data->size)
2712 *size = stored_data->size;
2713 hr = DDERR_MOREDATA;
2714 goto done;
2716 if (!data)
2718 hr = DDERR_INVALIDPARAMS;
2719 goto done;
2722 *size = stored_data->size;
2723 memcpy(data, stored_data->content.data, stored_data->size);
2724 hr = DD_OK;
2726 done:
2727 wined3d_mutex_unlock();
2728 return hr;
2731 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2733 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2735 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2736 iface, debugstr_guid(tag), data, size);
2738 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2741 /*****************************************************************************
2742 * IDirectDrawSurface7::FreePrivateData
2744 * Frees private data stored in the surface
2746 * Params:
2747 * tag: Tag of the data to free
2749 * Returns:
2750 * D3D_OK on success, error code otherwise.
2752 *****************************************************************************/
2753 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2755 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2756 struct wined3d_private_data *entry;
2758 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2760 wined3d_mutex_lock();
2761 entry = wined3d_private_store_get_private_data(&surface->private_store, tag);
2762 if (!entry)
2764 wined3d_mutex_unlock();
2765 return DDERR_NOTFOUND;
2768 wined3d_private_store_free_private_data(&surface->private_store, entry);
2769 wined3d_mutex_unlock();
2771 return DD_OK;
2774 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2776 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2778 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2780 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2783 /*****************************************************************************
2784 * IDirectDrawSurface7::PageLock
2786 * Prevents a sysmem surface from being paged out
2788 * Params:
2789 * Flags: Not used, must be 0(unchecked)
2791 * Returns:
2792 * DD_OK, because it's a stub
2794 *****************************************************************************/
2795 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2797 TRACE("iface %p, flags %#x.\n", iface, Flags);
2799 /* This is Windows memory management related - we don't need this */
2800 return DD_OK;
2803 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2805 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2807 TRACE("iface %p, flags %#x.\n", iface, flags);
2809 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2812 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2814 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2816 TRACE("iface %p, flags %#x.\n", iface, flags);
2818 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2821 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2823 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2825 TRACE("iface %p, flags %#x.\n", iface, flags);
2827 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2830 /*****************************************************************************
2831 * IDirectDrawSurface7::PageUnlock
2833 * Allows a sysmem surface to be paged out
2835 * Params:
2836 * Flags: Not used, must be 0(unchecked)
2838 * Returns:
2839 * DD_OK, because it's a stub
2841 *****************************************************************************/
2842 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2844 TRACE("iface %p, flags %#x.\n", iface, Flags);
2846 return DD_OK;
2849 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2851 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2853 TRACE("iface %p, flags %#x.\n", iface, flags);
2855 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2858 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2860 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2862 TRACE("iface %p, flags %#x.\n", iface, flags);
2864 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2867 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2869 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2871 TRACE("iface %p, flags %#x.\n", iface, flags);
2873 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2876 /*****************************************************************************
2877 * IDirectDrawSurface7::BltBatch
2879 * An unimplemented function
2881 * Params:
2884 * Returns:
2885 * DDERR_UNSUPPORTED
2887 *****************************************************************************/
2888 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2890 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2892 /* MSDN: "not currently implemented" */
2893 return DDERR_UNSUPPORTED;
2896 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2898 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2900 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2902 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2905 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2907 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2909 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2911 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2914 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2916 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2918 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2920 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2923 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2925 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2927 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2929 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2932 /*****************************************************************************
2933 * IDirectDrawSurface7::EnumAttachedSurfaces
2935 * Enumerates all surfaces attached to this surface
2937 * Params:
2938 * context: Pointer to pass unmodified to the callback
2939 * cb: Callback function to call for each surface
2941 * Returns:
2942 * DD_OK on success
2943 * DDERR_INVALIDPARAMS if cb is NULL
2945 *****************************************************************************/
2946 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2947 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2949 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2950 struct ddraw_surface *surf;
2951 DDSURFACEDESC2 desc;
2952 int i;
2954 /* Attached surfaces aren't handled in WineD3D */
2955 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2957 if(!cb)
2958 return DDERR_INVALIDPARAMS;
2960 wined3d_mutex_lock();
2962 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2964 surf = surface->complex_array[i];
2965 if(!surf) break;
2967 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2968 desc = surf->surface_desc;
2969 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2970 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2972 wined3d_mutex_unlock();
2973 return DD_OK;
2977 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2979 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2980 desc = surf->surface_desc;
2981 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2982 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2984 wined3d_mutex_unlock();
2985 return DD_OK;
2989 TRACE(" end of enumeration.\n");
2991 wined3d_mutex_unlock();
2993 return DD_OK;
2996 struct callback_info2
2998 LPDDENUMSURFACESCALLBACK2 callback;
2999 void *context;
3002 struct callback_info
3004 LPDDENUMSURFACESCALLBACK callback;
3005 void *context;
3008 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
3010 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3011 const struct callback_info2 *info = context;
3013 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3014 ddraw_surface7_Release(surface);
3016 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
3019 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
3021 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3022 const struct callback_info *info = context;
3024 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
3025 ddraw_surface7_Release(surface);
3027 /* FIXME: Check surface_test.dwSize */
3028 return info->callback(&surface_impl->IDirectDrawSurface_iface,
3029 (DDSURFACEDESC *)surface_desc, info->context);
3032 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
3033 void *context, LPDDENUMSURFACESCALLBACK2 callback)
3035 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3036 struct callback_info2 info;
3038 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3040 info.callback = callback;
3041 info.context = context;
3043 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3044 &info, EnumCallback2);
3047 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
3048 void *context, LPDDENUMSURFACESCALLBACK callback)
3050 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3051 struct callback_info info;
3053 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3055 info.callback = callback;
3056 info.context = context;
3058 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3059 &info, EnumCallback);
3062 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
3063 void *context, LPDDENUMSURFACESCALLBACK callback)
3065 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3066 struct callback_info info;
3068 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3070 info.callback = callback;
3071 info.context = context;
3073 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3074 &info, EnumCallback);
3077 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
3078 void *context, LPDDENUMSURFACESCALLBACK callback)
3080 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3081 struct callback_info info;
3083 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3085 info.callback = callback;
3086 info.context = context;
3088 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3089 &info, EnumCallback);
3092 /*****************************************************************************
3093 * IDirectDrawSurface7::EnumOverlayZOrders
3095 * "Enumerates the overlay surfaces on the specified destination"
3097 * Params:
3098 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
3099 * context: context to pass back to the callback
3100 * cb: callback function to call for each enumerated surface
3102 * Returns:
3103 * DD_OK, because it's a stub
3105 *****************************************************************************/
3106 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
3107 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
3109 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
3111 return DD_OK;
3114 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
3115 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3117 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3118 struct callback_info2 info;
3120 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3122 info.callback = callback;
3123 info.context = context;
3125 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3126 flags, &info, EnumCallback2);
3129 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
3130 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3132 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3133 struct callback_info info;
3135 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3137 info.callback = callback;
3138 info.context = context;
3140 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3141 flags, &info, EnumCallback);
3144 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
3145 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3147 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3148 struct callback_info info;
3150 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3152 info.callback = callback;
3153 info.context = context;
3155 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3156 flags, &info, EnumCallback);
3159 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
3160 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3162 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3163 struct callback_info info;
3165 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3167 info.callback = callback;
3168 info.context = context;
3170 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3171 flags, &info, EnumCallback);
3174 /*****************************************************************************
3175 * IDirectDrawSurface7::GetBltStatus
3177 * Returns the blitting status
3179 * Params:
3180 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
3182 *****************************************************************************/
3183 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3185 TRACE("iface %p, flags %#x.\n", iface, Flags);
3187 switch (Flags)
3189 case WINEDDGBS_CANBLT:
3190 case WINEDDGBS_ISBLTDONE:
3191 return DD_OK;
3193 default:
3194 return DDERR_INVALIDPARAMS;
3198 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
3200 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3202 TRACE("iface %p, flags %#x.\n", iface, flags);
3204 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3207 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
3209 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3211 TRACE("iface %p, flags %#x.\n", iface, flags);
3213 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3216 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
3218 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3220 TRACE("iface %p, flags %#x.\n", iface, flags);
3222 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3225 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
3227 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3229 TRACE("iface %p, flags %#x.\n", iface, flags);
3231 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3234 /*****************************************************************************
3235 * IDirectDrawSurface7::GetColorKey
3237 * Returns the color key assigned to the surface
3239 * Params:
3240 * Flags: Some flags
3241 * CKey: Address to store the key to
3243 * Returns:
3244 * DD_OK on success
3245 * DDERR_INVALIDPARAMS if CKey is NULL
3247 *****************************************************************************/
3248 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
3250 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3252 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
3254 if(!CKey)
3255 return DDERR_INVALIDPARAMS;
3257 wined3d_mutex_lock();
3259 switch (Flags)
3261 case DDCKEY_DESTBLT:
3262 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
3264 wined3d_mutex_unlock();
3265 return DDERR_NOCOLORKEY;
3267 *CKey = This->surface_desc.ddckCKDestBlt;
3268 break;
3270 case DDCKEY_DESTOVERLAY:
3271 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
3273 wined3d_mutex_unlock();
3274 return DDERR_NOCOLORKEY;
3276 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
3277 break;
3279 case DDCKEY_SRCBLT:
3280 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
3282 wined3d_mutex_unlock();
3283 return DDERR_NOCOLORKEY;
3285 *CKey = This->surface_desc.ddckCKSrcBlt;
3286 break;
3288 case DDCKEY_SRCOVERLAY:
3289 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
3291 wined3d_mutex_unlock();
3292 return DDERR_NOCOLORKEY;
3294 *CKey = This->surface_desc.ddckCKSrcOverlay;
3295 break;
3297 default:
3298 wined3d_mutex_unlock();
3299 return DDERR_INVALIDPARAMS;
3302 wined3d_mutex_unlock();
3304 return DD_OK;
3307 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
3309 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3311 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3313 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3316 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
3318 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3320 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3322 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3325 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
3327 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3329 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3331 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3334 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
3336 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3338 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3340 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3343 /*****************************************************************************
3344 * IDirectDrawSurface7::GetFlipStatus
3346 * Returns the flipping status of the surface
3348 * Params:
3349 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
3351 *****************************************************************************/
3352 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3354 TRACE("iface %p, flags %#x.\n", iface, Flags);
3356 /* XXX: DDERR_INVALIDSURFACETYPE */
3358 switch (Flags)
3360 case WINEDDGFS_CANFLIP:
3361 case WINEDDGFS_ISFLIPDONE:
3362 return DD_OK;
3364 default:
3365 return DDERR_INVALIDPARAMS;
3369 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
3371 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3373 TRACE("iface %p, flags %#x.\n", iface, flags);
3375 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3378 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
3380 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3382 TRACE("iface %p, flags %#x.\n", iface, flags);
3384 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3387 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
3389 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3391 TRACE("iface %p, flags %#x.\n", iface, flags);
3393 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3396 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
3398 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3400 TRACE("iface %p, flags %#x.\n", iface, flags);
3402 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3405 /*****************************************************************************
3406 * IDirectDrawSurface7::GetOverlayPosition
3408 * Returns the display coordinates of a visible and active overlay surface
3410 * Params:
3414 * Returns:
3415 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3416 *****************************************************************************/
3417 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *x, LONG *y)
3419 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3420 HRESULT hr;
3422 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3424 wined3d_mutex_lock();
3425 hr = wined3d_texture_get_overlay_position(surface->wined3d_texture,
3426 surface->sub_resource_idx, x, y);
3427 wined3d_mutex_unlock();
3429 return hr;
3432 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3434 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3436 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3438 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3441 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3443 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3445 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3447 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3450 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3452 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3454 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3456 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3459 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3461 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3463 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3465 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3468 /*****************************************************************************
3469 * IDirectDrawSurface7::GetPixelFormat
3471 * Returns the pixel format of the Surface
3473 * Params:
3474 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3475 * format should be written
3477 * Returns:
3478 * DD_OK on success
3479 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3481 *****************************************************************************/
3482 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3484 /* What is DDERR_INVALIDSURFACETYPE for here? */
3485 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3487 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3489 if(!PixelFormat)
3490 return DDERR_INVALIDPARAMS;
3492 wined3d_mutex_lock();
3493 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3494 wined3d_mutex_unlock();
3496 return DD_OK;
3499 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3501 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3503 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3505 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3508 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3510 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3512 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3514 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3517 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3519 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3521 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3523 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3526 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3528 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3530 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3532 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3535 /*****************************************************************************
3536 * IDirectDrawSurface7::GetSurfaceDesc
3538 * Returns the description of this surface
3540 * Params:
3541 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3542 * surface desc
3544 * Returns:
3545 * DD_OK on success
3546 * DDERR_INVALIDPARAMS if DDSD is NULL
3548 *****************************************************************************/
3549 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3551 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3553 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3555 if(!DDSD)
3556 return DDERR_INVALIDPARAMS;
3558 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3560 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3561 return DDERR_INVALIDPARAMS;
3564 wined3d_mutex_lock();
3565 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3566 TRACE("Returning surface desc:\n");
3567 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3568 wined3d_mutex_unlock();
3570 return DD_OK;
3573 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3575 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3577 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3579 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3582 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3584 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3586 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3588 if (!surface_desc) return DDERR_INVALIDPARAMS;
3590 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3592 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3593 return DDERR_INVALIDPARAMS;
3596 wined3d_mutex_lock();
3597 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3598 TRACE("Returning surface desc:\n");
3599 if (TRACE_ON(ddraw))
3601 /* DDRAW_dump_surface_desc handles the smaller size */
3602 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3604 wined3d_mutex_unlock();
3606 return DD_OK;
3609 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3611 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3613 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3615 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3618 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3620 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3622 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3624 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3627 /*****************************************************************************
3628 * IDirectDrawSurface7::Initialize
3630 * Initializes the surface. This is a no-op in Wine
3632 * Params:
3633 * DD: Pointer to an DirectDraw interface
3634 * DDSD: Surface description for initialization
3636 * Returns:
3637 * DDERR_ALREADYINITIALIZED
3639 *****************************************************************************/
3640 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3641 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3643 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3645 return DDERR_ALREADYINITIALIZED;
3648 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3649 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3651 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3653 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3655 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3656 ddraw, surface_desc);
3659 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3660 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3662 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3663 DDSURFACEDESC2 surface_desc2;
3665 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3667 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3668 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3669 ddraw, surface_desc ? &surface_desc2 : NULL);
3672 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3673 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3675 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3676 DDSURFACEDESC2 surface_desc2;
3678 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3680 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3681 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3682 ddraw, surface_desc ? &surface_desc2 : NULL);
3685 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3686 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3688 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3689 DDSURFACEDESC2 surface_desc2;
3691 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3693 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3694 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3695 ddraw, surface_desc ? &surface_desc2 : NULL);
3698 /*****************************************************************************
3699 * IDirect3DTexture1::Initialize
3701 * The sdk says it's not implemented
3703 * Params:
3706 * Returns
3707 * DDERR_UNSUPPORTED
3709 *****************************************************************************/
3710 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3711 IDirect3DDevice *device, IDirectDrawSurface *surface)
3713 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3715 return DDERR_UNSUPPORTED; /* Unchecked */
3718 /*****************************************************************************
3719 * IDirectDrawSurface7::IsLost
3721 * Checks if the surface is lost
3723 * Returns:
3724 * DD_OK, if the surface is usable
3725 * DDERR_ISLOST if the surface is lost
3727 *****************************************************************************/
3728 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3730 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3732 TRACE("iface %p.\n", iface);
3734 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3737 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3739 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3741 TRACE("iface %p.\n", iface);
3743 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3746 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3748 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3750 TRACE("iface %p.\n", iface);
3752 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3755 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3757 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3759 TRACE("iface %p.\n", iface);
3761 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3764 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3766 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3768 TRACE("iface %p.\n", iface);
3770 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3773 /*****************************************************************************
3774 * IDirectDrawSurface7::Restore
3776 * Restores a lost surface. This makes the surface usable again, but
3777 * doesn't reload its old contents
3779 * Returns:
3780 * DD_OK on success, error code otherwise.
3782 *****************************************************************************/
3783 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3785 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3786 unsigned int i;
3788 TRACE("iface %p.\n", iface);
3790 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3792 struct wined3d_swapchain *swapchain = surface->ddraw->wined3d_swapchain;
3793 struct wined3d_sub_resource_desc wined3d_desc;
3794 struct wined3d_display_mode mode;
3795 HRESULT hr;
3797 if (FAILED(hr = wined3d_swapchain_get_display_mode(swapchain, &mode, NULL)))
3799 WARN("Failed to get display mode, hr %#x.\n", hr);
3800 return hr;
3803 if (FAILED(hr = wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, 0, &wined3d_desc)))
3805 WARN("Failed to get resource desc, hr %#x.\n", hr);
3806 return hr;
3809 if (mode.width != wined3d_desc.width || mode.height != wined3d_desc.height)
3811 WARN("Display mode dimensions %ux%u don't match surface dimensions %ux%u.\n",
3812 mode.width, mode.height, wined3d_desc.width, wined3d_desc.height);
3813 return DDERR_WRONGMODE;
3816 if (mode.format_id != wined3d_desc.format)
3818 WARN("Display mode format %#x doesn't match surface format %#x.\n",
3819 mode.format_id, wined3d_desc.format);
3820 return DDERR_WRONGMODE;
3824 if (!ddraw_surface_can_be_lost(surface))
3825 return DD_OK;
3826 ddraw_update_lost_surfaces(surface->ddraw);
3827 if (surface->ddraw->device_state == DDRAW_DEVICE_STATE_LOST)
3828 return DDERR_WRONGMODE;
3830 surface->is_lost = FALSE;
3831 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
3833 if (surface->complex_array[i])
3834 surface->complex_array[i]->is_lost = FALSE;
3837 return DD_OK;
3840 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3842 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3844 TRACE("iface %p.\n", iface);
3846 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3849 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3851 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3853 TRACE("iface %p.\n", iface);
3855 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3858 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3860 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3862 TRACE("iface %p.\n", iface);
3864 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3867 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3869 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3871 TRACE("iface %p.\n", iface);
3873 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3876 /*****************************************************************************
3877 * IDirectDrawSurface7::SetOverlayPosition
3879 * Changes the display coordinates of an overlay surface
3881 * Params:
3882 * X:
3883 * Y:
3885 * Returns:
3886 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3887 *****************************************************************************/
3888 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG x, LONG y)
3890 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3891 HRESULT hr;
3893 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3895 wined3d_mutex_lock();
3896 hr = wined3d_texture_set_overlay_position(surface->wined3d_texture,
3897 surface->sub_resource_idx, x, y);
3898 wined3d_mutex_unlock();
3900 return hr;
3903 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3905 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3907 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3909 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3912 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3914 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3916 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3918 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3921 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3923 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3925 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3927 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3930 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3932 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3934 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3936 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3939 /*****************************************************************************
3940 * IDirectDrawSurface7::UpdateOverlay
3942 * Modifies the attributes of an overlay surface.
3944 * Params:
3945 * SrcRect: The section of the source being used for the overlay
3946 * DstSurface: Address of the surface that is overlaid
3947 * DstRect: Place of the overlay
3948 * Flags: some DDOVER_* flags
3950 * Returns:
3951 * DDERR_UNSUPPORTED, because we don't support overlays
3953 *****************************************************************************/
3954 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *src_rect,
3955 IDirectDrawSurface7 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3957 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3958 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(dst_surface);
3959 struct wined3d_texture *dst_wined3d_texture = NULL;
3960 unsigned int dst_sub_resource_idx = 0;
3961 HRESULT hr;
3963 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3964 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3966 if (fx)
3967 FIXME("Ignoring fx %p.\n", fx);
3969 wined3d_mutex_lock();
3970 if (dst_impl)
3972 dst_wined3d_texture = dst_impl->wined3d_texture;
3973 dst_sub_resource_idx = dst_impl->sub_resource_idx;
3975 hr = wined3d_texture_update_overlay(src_impl->wined3d_texture, src_impl->sub_resource_idx,
3976 src_rect, dst_wined3d_texture, dst_sub_resource_idx, dst_rect, flags);
3977 wined3d_mutex_unlock();
3979 return hr_ddraw_from_wined3d(hr);
3982 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3983 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3985 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3986 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3988 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3989 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3991 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3992 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3995 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3996 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3998 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
3999 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
4001 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4002 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4004 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4005 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4008 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
4009 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4011 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
4012 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
4014 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4015 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4017 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4018 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4021 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
4022 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4024 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
4025 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
4027 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4028 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4030 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4031 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4034 /*****************************************************************************
4035 * IDirectDrawSurface7::UpdateOverlayDisplay
4037 * The DX7 sdk says that it's not implemented
4039 * Params:
4040 * Flags: ?
4042 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
4044 *****************************************************************************/
4045 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
4047 TRACE("iface %p, flags %#x.\n", iface, Flags);
4049 return DDERR_UNSUPPORTED;
4052 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
4054 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4056 TRACE("iface %p, flags %#x.\n", iface, flags);
4058 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4061 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
4063 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4065 TRACE("iface %p, flags %#x.\n", iface, flags);
4067 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4070 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
4072 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4074 TRACE("iface %p, flags %#x.\n", iface, flags);
4076 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4079 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
4081 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4083 TRACE("iface %p, flags %#x.\n", iface, flags);
4085 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4088 /*****************************************************************************
4089 * IDirectDrawSurface7::UpdateOverlayZOrder
4091 * Sets an overlay's Z order
4093 * Params:
4094 * Flags: DDOVERZ_* flags
4095 * DDSRef: Defines the relative position in the overlay chain
4097 * Returns:
4098 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
4100 *****************************************************************************/
4101 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
4102 DWORD flags, IDirectDrawSurface7 *reference)
4104 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4106 FIXME("iface %p, flags %#x, reference %p stub!\n", iface, flags, reference);
4108 wined3d_mutex_lock();
4109 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OVERLAY))
4111 WARN("Not an overlay surface.\n");
4112 wined3d_mutex_unlock();
4113 return DDERR_NOTAOVERLAYSURFACE;
4115 wined3d_mutex_unlock();
4117 return DD_OK;
4120 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
4121 DWORD flags, IDirectDrawSurface4 *reference)
4123 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4124 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
4126 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4128 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4129 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4132 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
4133 DWORD flags, IDirectDrawSurface3 *reference)
4135 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4136 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
4138 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4140 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4141 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4144 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
4145 DWORD flags, IDirectDrawSurface2 *reference)
4147 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4148 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
4150 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4152 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4153 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4156 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
4157 DWORD flags, IDirectDrawSurface *reference)
4159 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4160 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
4162 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4164 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4165 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4168 /*****************************************************************************
4169 * IDirectDrawSurface7::GetDDInterface
4171 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
4172 * surface belongs to
4174 * Params:
4175 * DD: Address to write the interface pointer to
4177 * Returns:
4178 * DD_OK on success
4179 * DDERR_INVALIDPARAMS if DD is NULL
4181 *****************************************************************************/
4182 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
4184 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4186 TRACE("iface %p, ddraw %p.\n", iface, DD);
4188 if(!DD)
4189 return DDERR_INVALIDPARAMS;
4191 switch(This->version)
4193 case 7:
4194 *DD = &This->ddraw->IDirectDraw7_iface;
4195 break;
4197 case 4:
4198 *DD = &This->ddraw->IDirectDraw4_iface;
4199 break;
4201 case 2:
4202 *DD = &This->ddraw->IDirectDraw2_iface;
4203 break;
4205 case 1:
4206 *DD = &This->ddraw->IDirectDraw_iface;
4207 break;
4210 IUnknown_AddRef((IUnknown *)*DD);
4212 return DD_OK;
4215 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
4217 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4219 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4221 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4224 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
4226 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4228 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4230 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4233 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
4235 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4237 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4239 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4242 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
4244 TRACE("iface %p.\n", iface);
4246 return DD_OK;
4249 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
4251 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4253 TRACE("iface %p.\n", iface);
4255 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
4258 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
4260 TRACE("iface %p, value %p.\n", iface, pValue);
4262 *pValue = 0;
4264 return DD_OK;
4267 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
4269 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4271 TRACE("iface %p, value %p.\n", iface, pValue);
4273 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
4276 /*****************************************************************************
4277 * IDirectDrawSurface7::SetLOD
4279 * Sets the level of detail of a texture
4281 * Params:
4282 * MaxLOD: LOD to set
4284 * Returns:
4285 * DD_OK on success
4286 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4288 *****************************************************************************/
4289 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
4291 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4292 HRESULT hr;
4294 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
4296 wined3d_mutex_lock();
4297 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4299 wined3d_mutex_unlock();
4300 return DDERR_INVALIDOBJECT;
4303 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
4304 if (SUCCEEDED(hr) && surface->draw_texture)
4305 hr = wined3d_texture_set_lod(surface->draw_texture, MaxLOD);
4306 wined3d_mutex_unlock();
4308 return hr;
4311 /*****************************************************************************
4312 * IDirectDrawSurface7::GetLOD
4314 * Returns the level of detail of a Direct3D texture
4316 * Params:
4317 * MaxLOD: Address to write the LOD to
4319 * Returns:
4320 * DD_OK on success
4321 * DDERR_INVALIDPARAMS if MaxLOD is NULL
4322 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4324 *****************************************************************************/
4325 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4327 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4329 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4331 if(!MaxLOD)
4332 return DDERR_INVALIDPARAMS;
4334 wined3d_mutex_lock();
4335 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4337 wined3d_mutex_unlock();
4338 return DDERR_INVALIDOBJECT;
4341 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4342 wined3d_mutex_unlock();
4344 return DD_OK;
4347 /*****************************************************************************
4348 * IDirectDrawSurface7::BltFast
4350 * Performs a fast Blit.
4352 * Params:
4353 * dstx: The x coordinate to blit to on the destination
4354 * dsty: The y coordinate to blit to on the destination
4355 * Source: The source surface
4356 * rsrc: The source rectangle
4357 * trans: Type of transfer. Some DDBLTFAST_* flags
4359 * Returns:
4360 * DD_OK on success, error code otherwise.
4362 *****************************************************************************/
4363 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltFast(IDirectDrawSurface7 *iface,
4364 DWORD dst_x, DWORD dst_y, IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD trans)
4366 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
4367 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src_surface);
4368 DWORD flags = WINED3D_BLT_SYNCHRONOUS;
4369 DWORD src_w, src_h, dst_w, dst_h;
4370 HRESULT hr = DD_OK;
4371 RECT dst_rect, s;
4373 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4374 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), trans);
4376 dst_w = dst_impl->surface_desc.dwWidth;
4377 dst_h = dst_impl->surface_desc.dwHeight;
4379 if (!src_rect)
4381 SetRect(&s, 0, 0, src_impl->surface_desc.dwWidth, src_impl->surface_desc.dwHeight);
4382 src_rect = &s;
4385 src_w = src_rect->right - src_rect->left;
4386 src_h = src_rect->bottom - src_rect->top;
4387 if (src_w > dst_w || dst_x > dst_w - src_w
4388 || src_h > dst_h || dst_y > dst_h - src_h)
4390 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4391 return DDERR_INVALIDRECT;
4394 SetRect(&dst_rect, dst_x, dst_y, dst_x + src_w, dst_y + src_h);
4395 if (trans & DDBLTFAST_SRCCOLORKEY)
4396 flags |= WINED3D_BLT_SRC_CKEY;
4397 if (trans & DDBLTFAST_DESTCOLORKEY)
4398 flags |= WINED3D_BLT_DST_CKEY;
4399 if (trans & DDBLTFAST_WAIT)
4400 flags |= WINED3D_BLT_WAIT;
4401 if (trans & DDBLTFAST_DONOTWAIT)
4402 flags |= WINED3D_BLT_DO_NOT_WAIT;
4404 wined3d_mutex_lock();
4405 if (dst_impl->clipper)
4407 wined3d_mutex_unlock();
4408 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4409 return DDERR_BLTFASTCANTCLIP;
4412 if (src_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4413 hr = ddraw_surface_update_frontbuffer(src_impl, src_rect, TRUE, 0);
4414 if (SUCCEEDED(hr))
4415 hr = wined3d_device_context_blt(dst_impl->ddraw->immediate_context,
4416 ddraw_surface_get_any_texture(dst_impl, DDRAW_SURFACE_RW), dst_impl->sub_resource_idx, &dst_rect,
4417 ddraw_surface_get_any_texture(src_impl,DDRAW_SURFACE_READ), src_impl->sub_resource_idx, src_rect,
4418 flags, NULL, WINED3D_TEXF_POINT);
4419 if (SUCCEEDED(hr) && (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4420 hr = ddraw_surface_update_frontbuffer(dst_impl, &dst_rect, FALSE, 0);
4421 wined3d_mutex_unlock();
4423 switch(hr)
4425 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4426 default: return hr;
4430 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4431 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4433 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4434 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4436 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4437 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4439 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4440 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4443 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4444 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4446 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4447 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4449 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4450 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4452 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4453 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4456 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4457 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4459 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4460 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4462 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4463 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4465 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4466 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4469 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4470 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4472 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4473 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4475 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4476 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4478 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4479 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4482 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **clipper)
4484 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4486 TRACE("iface %p, clipper %p.\n", iface, clipper);
4488 if (!clipper)
4489 return DDERR_INVALIDPARAMS;
4491 wined3d_mutex_lock();
4492 if (!surface->clipper)
4494 wined3d_mutex_unlock();
4495 *clipper = NULL;
4496 return DDERR_NOCLIPPERATTACHED;
4499 *clipper = &surface->clipper->IDirectDrawClipper_iface;
4500 if (ddraw_clipper_is_valid(surface->clipper))
4501 IDirectDrawClipper_AddRef(*clipper);
4502 wined3d_mutex_unlock();
4504 return DD_OK;
4507 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4509 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4511 TRACE("iface %p, clipper %p.\n", iface, clipper);
4513 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4516 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4518 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4520 TRACE("iface %p, clipper %p.\n", iface, clipper);
4522 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4525 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4527 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4529 TRACE("iface %p, clipper %p.\n", iface, clipper);
4531 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4534 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4536 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4538 TRACE("iface %p, clipper %p.\n", iface, clipper);
4540 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4543 /*****************************************************************************
4544 * IDirectDrawSurface7::SetClipper
4546 * Sets a clipper for the surface
4548 * Params:
4549 * Clipper: IDirectDrawClipper interface of the clipper to set
4551 * Returns:
4552 * DD_OK on success
4554 *****************************************************************************/
4555 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4556 IDirectDrawClipper *iclipper)
4558 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4559 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4560 struct ddraw_clipper *old_clipper = This->clipper;
4561 HWND clipWindow;
4563 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4565 wined3d_mutex_lock();
4566 if (clipper == This->clipper)
4568 wined3d_mutex_unlock();
4569 return DD_OK;
4572 This->clipper = clipper;
4574 if (clipper != NULL)
4575 IDirectDrawClipper_AddRef(iclipper);
4576 if (old_clipper && ddraw_clipper_is_valid(old_clipper))
4577 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4579 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4581 clipWindow = NULL;
4582 if(clipper) {
4583 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4586 if (clipWindow)
4588 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4589 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4591 else
4593 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4594 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4598 wined3d_mutex_unlock();
4600 return DD_OK;
4603 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4605 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4607 TRACE("iface %p, clipper %p.\n", iface, clipper);
4609 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4612 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4614 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4616 TRACE("iface %p, clipper %p.\n", iface, clipper);
4618 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4621 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4623 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4625 TRACE("iface %p, clipper %p.\n", iface, clipper);
4627 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4630 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4632 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4634 TRACE("iface %p, clipper %p.\n", iface, clipper);
4636 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4639 /*****************************************************************************
4640 * IDirectDrawSurface7::SetSurfaceDesc
4642 * Sets the surface description. It can override the pixel format, the surface
4643 * memory, ...
4644 * It's not really tested.
4646 * Params:
4647 * DDSD: Pointer to the new surface description to set
4648 * Flags: Some flags
4650 * Returns:
4651 * DD_OK on success
4652 * DDERR_INVALIDPARAMS if DDSD is NULL
4654 *****************************************************************************/
4655 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4657 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4658 HRESULT hr;
4659 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4660 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4661 enum wined3d_format_id format_id;
4662 UINT pitch, width, height;
4664 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4666 if (!DDSD)
4668 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4669 return DDERR_INVALIDPARAMS;
4671 if (Flags)
4673 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4674 return DDERR_INVALIDPARAMS;
4676 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4677 || surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4678 || surface->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4680 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4681 return DDERR_INVALIDSURFACETYPE;
4684 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4685 * for PIXELFORMAT to work */
4686 if (DDSD->dwFlags & ~allowed_flags)
4688 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4689 return DDERR_INVALIDPARAMS;
4691 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4693 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4694 return DDERR_INVALIDPARAMS;
4696 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4698 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4699 return DDERR_INVALIDCAPS;
4701 if (DDSD->dwFlags & DDSD_WIDTH)
4703 if (!(DDSD->dwFlags & DDSD_PITCH))
4705 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4706 return DDERR_INVALIDPARAMS;
4708 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4710 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4711 DDSD->u1.lPitch, DDSD->dwWidth);
4712 return DDERR_INVALIDPARAMS;
4714 if (DDSD->dwWidth != surface->surface_desc.dwWidth)
4715 TRACE("Surface width changed from %u to %u.\n", surface->surface_desc.dwWidth, DDSD->dwWidth);
4716 if (DDSD->u1.lPitch != surface->surface_desc.u1.lPitch)
4717 TRACE("Surface pitch changed from %u to %u.\n", surface->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4718 pitch = DDSD->u1.lPitch;
4719 width = DDSD->dwWidth;
4721 else if (DDSD->dwFlags & DDSD_PITCH)
4723 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4724 return DDERR_INVALIDPARAMS;
4726 else
4728 pitch = surface->surface_desc.u1.lPitch;
4729 width = surface->surface_desc.dwWidth;
4732 if (DDSD->dwFlags & DDSD_HEIGHT)
4734 if (!DDSD->dwHeight)
4736 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4737 return DDERR_INVALIDPARAMS;
4739 if (DDSD->dwHeight != surface->surface_desc.dwHeight)
4740 TRACE("Surface height changed from %u to %u.\n", surface->surface_desc.dwHeight, DDSD->dwHeight);
4741 height = DDSD->dwHeight;
4743 else
4745 height = surface->surface_desc.dwHeight;
4748 wined3d_mutex_lock();
4749 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4751 enum wined3d_format_id current_format_id;
4752 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4754 if (format_id == WINED3DFMT_UNKNOWN)
4756 ERR("Requested to set an unknown pixelformat\n");
4757 wined3d_mutex_unlock();
4758 return DDERR_INVALIDPARAMS;
4760 current_format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4761 if (format_id != current_format_id)
4762 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4764 else
4766 format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4769 if (FAILED(hr = wined3d_texture_update_desc(surface->wined3d_texture, surface->sub_resource_idx,
4770 width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4772 WARN("Failed to update surface desc, hr %#x.\n", hr);
4773 wined3d_mutex_unlock();
4774 return hr_ddraw_from_wined3d(hr);
4777 if (surface->draw_texture && FAILED(hr = wined3d_texture_update_desc(surface->draw_texture,
4778 surface->sub_resource_idx, width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, NULL, 0)))
4780 ERR("Failed to update surface desc for draw_texture, hr %#x.\n", hr);
4781 wined3d_mutex_unlock();
4782 return hr_ddraw_from_wined3d(hr);
4785 if (DDSD->dwFlags & DDSD_WIDTH)
4786 surface->surface_desc.dwWidth = width;
4787 if (DDSD->dwFlags & DDSD_PITCH)
4788 surface->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4789 if (DDSD->dwFlags & DDSD_HEIGHT)
4790 surface->surface_desc.dwHeight = height;
4791 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4792 surface->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4794 wined3d_mutex_unlock();
4796 return DD_OK;
4799 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4800 DDSURFACEDESC2 *surface_desc, DWORD flags)
4802 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4804 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4806 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4807 surface_desc, flags);
4810 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4811 DDSURFACEDESC *surface_desc, DWORD flags)
4813 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4814 DDSURFACEDESC2 surface_desc2;
4816 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4818 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4819 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4820 surface_desc ? &surface_desc2 : NULL, flags);
4823 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4825 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4826 struct ddraw_palette *palette_impl;
4827 HRESULT hr = DD_OK;
4829 TRACE("iface %p, palette %p.\n", iface, palette);
4831 if (!palette)
4832 return DDERR_INVALIDPARAMS;
4833 if (ddraw_surface_is_lost(surface))
4835 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4836 return DDERR_SURFACELOST;
4839 wined3d_mutex_lock();
4840 if ((palette_impl = surface->palette))
4842 *palette = &palette_impl->IDirectDrawPalette_iface;
4843 IDirectDrawPalette_AddRef(*palette);
4845 else
4847 *palette = NULL;
4848 hr = DDERR_NOPALETTEATTACHED;
4850 wined3d_mutex_unlock();
4852 return hr;
4855 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4857 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4859 TRACE("iface %p, palette %p.\n", iface, palette);
4861 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4864 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4866 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4868 TRACE("iface %p, palette %p.\n", iface, palette);
4870 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4873 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4875 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4877 TRACE("iface %p, palette %p.\n", iface, palette);
4879 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4882 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4884 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4886 TRACE("iface %p, palette %p.\n", iface, palette);
4888 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4891 static HRESULT ddraw_surface_set_wined3d_textures_colour_key(struct ddraw_surface *surface, DWORD flags,
4892 struct wined3d_color_key *color_key)
4894 HRESULT hr;
4896 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags, color_key);
4897 if (surface->draw_texture && SUCCEEDED(hr))
4898 hr = wined3d_texture_set_color_key(surface->draw_texture, flags, color_key);
4900 return hr;
4903 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4905 DDCOLORKEY fixed_color_key;
4906 HRESULT hr = WINED3D_OK;
4908 if (flags & DDCKEY_COLORSPACE)
4910 if (color_key && color_key->dwColorSpaceLowValue != color_key->dwColorSpaceHighValue)
4912 WARN("Range color keys are not supported, returning DDERR_NOCOLORKEYHW.\n");
4913 return DDERR_NOCOLORKEYHW;
4915 flags &= ~DDCKEY_COLORSPACE;
4918 wined3d_mutex_lock();
4920 if (color_key)
4922 fixed_color_key.dwColorSpaceLowValue = fixed_color_key.dwColorSpaceHighValue = color_key->dwColorSpaceLowValue;
4923 switch (flags & ~DDCKEY_COLORSPACE)
4925 case DDCKEY_DESTBLT:
4926 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4927 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4928 break;
4930 case DDCKEY_DESTOVERLAY:
4931 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4932 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4933 break;
4935 case DDCKEY_SRCOVERLAY:
4936 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4937 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4938 break;
4940 case DDCKEY_SRCBLT:
4941 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4942 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4943 break;
4945 default:
4946 wined3d_mutex_unlock();
4947 return DDERR_INVALIDPARAMS;
4950 else
4952 switch (flags & ~DDCKEY_COLORSPACE)
4954 case DDCKEY_DESTBLT:
4955 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4956 break;
4958 case DDCKEY_DESTOVERLAY:
4959 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4960 break;
4962 case DDCKEY_SRCOVERLAY:
4963 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4964 break;
4966 case DDCKEY_SRCBLT:
4967 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4968 break;
4970 default:
4971 wined3d_mutex_unlock();
4972 return DDERR_INVALIDPARAMS;
4976 if (surface->is_complex_root)
4977 hr = ddraw_surface_set_wined3d_textures_colour_key(surface, flags,
4978 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4980 wined3d_mutex_unlock();
4982 return hr_ddraw_from_wined3d(hr);
4985 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4987 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4989 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4991 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4992 return DDERR_NOTONMIPMAPSUBLEVEL;
4994 return ddraw_surface_set_color_key(surface, flags, color_key);
4997 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4999 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
5001 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5003 return ddraw_surface_set_color_key(surface, flags, color_key);
5006 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
5008 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
5010 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5012 return ddraw_surface_set_color_key(surface, flags, color_key);
5015 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
5017 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
5019 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5021 return ddraw_surface_set_color_key(surface, flags, color_key);
5024 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
5026 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5028 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5030 return ddraw_surface_set_color_key(surface, flags, color_key);
5033 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
5035 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
5037 TRACE("iface %p, palette %p.\n", iface, palette);
5039 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
5040 return DDERR_NOTONMIPMAPSUBLEVEL;
5041 if (ddraw_surface_is_lost(surface))
5043 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5044 return DDERR_SURFACELOST;
5047 return ddraw_surface_set_palette(surface, palette);
5050 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
5052 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
5054 TRACE("iface %p, palette %p.\n", iface, palette);
5056 if (ddraw_surface_is_lost(surface))
5058 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5059 return DDERR_SURFACELOST;
5062 return ddraw_surface_set_palette(surface, palette);
5065 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
5067 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
5069 TRACE("iface %p, palette %p.\n", iface, palette);
5071 if (ddraw_surface_is_lost(surface))
5073 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5074 return DDERR_SURFACELOST;
5077 return ddraw_surface_set_palette(surface, palette);
5080 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
5082 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
5084 TRACE("iface %p, palette %p.\n", iface, palette);
5086 if (ddraw_surface_is_lost(surface))
5088 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5089 return DDERR_SURFACELOST;
5092 return ddraw_surface_set_palette(surface, palette);
5095 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
5097 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5099 TRACE("iface %p, palette %p.\n", iface, palette);
5101 if (ddraw_surface_is_lost(surface))
5103 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5104 return DDERR_SURFACELOST;
5107 return ddraw_surface_set_palette(surface, palette);
5110 /**********************************************************
5111 * IDirectDrawGammaControl::GetGammaRamp
5113 * Returns the current gamma ramp for a surface
5115 * Params:
5116 * flags: Ignored
5117 * gamma_ramp: Address to write the ramp to
5119 * Returns:
5120 * DD_OK on success
5121 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5123 **********************************************************/
5124 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
5125 DWORD flags, DDGAMMARAMP *gamma_ramp)
5127 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5129 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5131 if (!gamma_ramp)
5133 WARN("Invalid gamma_ramp passed.\n");
5134 return DDERR_INVALIDPARAMS;
5137 wined3d_mutex_lock();
5138 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5140 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5141 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
5143 else
5145 ERR("Not implemented for non-primary surfaces.\n");
5147 wined3d_mutex_unlock();
5149 return DD_OK;
5152 /**********************************************************
5153 * IDirectDrawGammaControl::SetGammaRamp
5155 * Sets the red, green and blue gamma ramps for
5157 * Params:
5158 * flags: Can be DDSGR_CALIBRATE to request calibration
5159 * gamma_ramp: Structure containing the new gamma ramp
5161 * Returns:
5162 * DD_OK on success
5163 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5165 **********************************************************/
5166 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
5167 DWORD flags, DDGAMMARAMP *gamma_ramp)
5169 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5171 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5173 if (!gamma_ramp)
5175 WARN("Invalid gamma_ramp passed.\n");
5176 return DDERR_INVALIDPARAMS;
5179 wined3d_mutex_lock();
5180 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5182 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5183 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
5184 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
5186 else
5188 ERR("Not implemented for non-primary surfaces.\n");
5190 wined3d_mutex_unlock();
5192 return DD_OK;
5195 /*****************************************************************************
5196 * IDirect3DTexture2::PaletteChanged
5198 * Informs the texture about a palette change
5200 * Params:
5201 * start: Start index of the change
5202 * count: The number of changed entries
5204 * Returns
5205 * D3D_OK, because it's a stub
5207 *****************************************************************************/
5208 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
5210 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
5212 return D3D_OK;
5215 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
5217 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5219 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
5221 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
5224 /*****************************************************************************
5225 * IDirect3DTexture::Unload
5227 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
5230 * Returns:
5231 * DDERR_UNSUPPORTED
5233 *****************************************************************************/
5234 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
5236 WARN("iface %p. Not implemented.\n", iface);
5238 return DDERR_UNSUPPORTED;
5241 /*****************************************************************************
5242 * IDirect3DTexture2::GetHandle
5244 * Returns handle for the texture.
5246 * Params:
5247 * device: Device this handle is assigned to
5248 * handle: Address to store the handle at.
5250 * Returns:
5251 * D3D_OK
5253 *****************************************************************************/
5254 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
5255 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
5257 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
5258 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
5260 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5262 wined3d_mutex_lock();
5264 if (!surface->Handle)
5266 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
5267 if (h == DDRAW_INVALID_HANDLE)
5269 ERR("Failed to allocate a texture handle.\n");
5270 wined3d_mutex_unlock();
5271 return DDERR_OUTOFMEMORY;
5274 surface->Handle = h + 1;
5277 TRACE("Returning handle %08x.\n", surface->Handle);
5278 *handle = surface->Handle;
5280 wined3d_mutex_unlock();
5282 return D3D_OK;
5285 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
5286 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
5288 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5289 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
5291 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5293 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
5294 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
5297 /*****************************************************************************
5298 * get_sub_mimaplevel
5300 * Helper function that returns the next mipmap level
5302 * tex_ptr: Surface of which to return the next level
5304 *****************************************************************************/
5305 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
5307 /* Now go down the mipmap chain to the next surface */
5308 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, {0} };
5309 IDirectDrawSurface7 *next_level;
5310 HRESULT hr;
5312 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
5313 if (FAILED(hr)) return NULL;
5315 ddraw_surface7_Release(next_level);
5317 return impl_from_IDirectDrawSurface7(next_level);
5320 /*****************************************************************************
5321 * IDirect3DTexture2::Load
5323 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5325 * This function isn't relayed to WineD3D because the whole interface is
5326 * implemented in DDraw only. For speed improvements an implementation which
5327 * takes OpenGL more into account could be placed into WineD3D.
5329 * Params:
5330 * src_texture: Address of the texture to load
5332 * Returns:
5333 * D3D_OK on success
5334 * D3DERR_TEXTURE_LOAD_FAILED.
5336 *****************************************************************************/
5337 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5339 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5340 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5341 struct wined3d_resource *dst_resource, *src_resource;
5342 HRESULT hr;
5344 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5346 if (src_surface == dst_surface)
5348 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5349 return D3D_OK;
5352 wined3d_mutex_lock();
5354 dst_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(dst_surface, DDRAW_SURFACE_WRITE));
5355 src_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(src_surface, DDRAW_SURFACE_READ));
5357 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5358 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5359 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5361 ERR("Trying to load surfaces with different mip-map counts.\n");
5364 for (;;)
5366 struct ddraw_palette *dst_pal, *src_pal;
5367 DDSURFACEDESC *src_desc, *dst_desc;
5369 TRACE("Copying surface %p to surface %p.\n", src_surface, dst_surface);
5371 /* Suppress the ALLOCONLOAD flag */
5372 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5374 /* Get the palettes */
5375 dst_pal = dst_surface->palette;
5376 src_pal = src_surface->palette;
5378 if (src_pal)
5380 PALETTEENTRY palent[256];
5382 if (!dst_pal)
5384 wined3d_mutex_unlock();
5385 return DDERR_NOPALETTEATTACHED;
5387 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5388 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5391 /* Copy one surface on the other */
5392 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5393 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5395 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5397 /* Should also check for same pixel format, u1.lPitch, ... */
5398 ERR("Error in surface sizes.\n");
5399 wined3d_mutex_unlock();
5400 return D3DERR_TEXTURE_LOAD_FAILED;
5402 else
5404 struct wined3d_map_desc src_map_desc, dst_map_desc;
5406 /* Copy the src blit color key if the source has one, don't erase
5407 * the destination's ckey if the source has none */
5408 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5410 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5411 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5414 if (FAILED(hr = wined3d_resource_map(src_resource,
5415 src_surface->sub_resource_idx, &src_map_desc, NULL, WINED3D_MAP_READ)))
5417 ERR("Failed to lock source surface, hr %#x.\n", hr);
5418 wined3d_mutex_unlock();
5419 return D3DERR_TEXTURE_LOAD_FAILED;
5422 if (FAILED(hr = wined3d_resource_map(dst_resource,
5423 dst_surface->sub_resource_idx, &dst_map_desc, NULL, WINED3D_MAP_WRITE)))
5425 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5426 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5427 wined3d_mutex_unlock();
5428 return D3DERR_TEXTURE_LOAD_FAILED;
5431 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5432 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5433 else
5434 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5436 wined3d_resource_unmap(dst_resource, dst_surface->sub_resource_idx);
5437 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5440 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5441 src_surface = get_sub_mimaplevel(src_surface);
5442 else
5443 src_surface = NULL;
5445 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5446 dst_surface = get_sub_mimaplevel(dst_surface);
5447 else
5448 dst_surface = NULL;
5450 if (!src_surface || !dst_surface)
5452 if (src_surface != dst_surface)
5453 ERR("Loading surface with different mipmap structure.\n");
5454 break;
5458 wined3d_mutex_unlock();
5460 return hr;
5463 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5465 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5466 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5468 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5470 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5471 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5474 /*****************************************************************************
5475 * The VTable
5476 *****************************************************************************/
5478 /* Some windowed mode wrappers expect this vtbl to be writable. */
5479 static struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5481 /* IUnknown */
5482 ddraw_surface7_QueryInterface,
5483 ddraw_surface7_AddRef,
5484 ddraw_surface7_Release,
5485 /* IDirectDrawSurface */
5486 ddraw_surface7_AddAttachedSurface,
5487 ddraw_surface7_AddOverlayDirtyRect,
5488 ddraw_surface7_Blt,
5489 ddraw_surface7_BltBatch,
5490 ddraw_surface7_BltFast,
5491 ddraw_surface7_DeleteAttachedSurface,
5492 ddraw_surface7_EnumAttachedSurfaces,
5493 ddraw_surface7_EnumOverlayZOrders,
5494 ddraw_surface7_Flip,
5495 ddraw_surface7_GetAttachedSurface,
5496 ddraw_surface7_GetBltStatus,
5497 ddraw_surface7_GetCaps,
5498 ddraw_surface7_GetClipper,
5499 ddraw_surface7_GetColorKey,
5500 ddraw_surface7_GetDC,
5501 ddraw_surface7_GetFlipStatus,
5502 ddraw_surface7_GetOverlayPosition,
5503 ddraw_surface7_GetPalette,
5504 ddraw_surface7_GetPixelFormat,
5505 ddraw_surface7_GetSurfaceDesc,
5506 ddraw_surface7_Initialize,
5507 ddraw_surface7_IsLost,
5508 ddraw_surface7_Lock,
5509 ddraw_surface7_ReleaseDC,
5510 ddraw_surface7_Restore,
5511 ddraw_surface7_SetClipper,
5512 ddraw_surface7_SetColorKey,
5513 ddraw_surface7_SetOverlayPosition,
5514 ddraw_surface7_SetPalette,
5515 ddraw_surface7_Unlock,
5516 ddraw_surface7_UpdateOverlay,
5517 ddraw_surface7_UpdateOverlayDisplay,
5518 ddraw_surface7_UpdateOverlayZOrder,
5519 /* IDirectDrawSurface2 */
5520 ddraw_surface7_GetDDInterface,
5521 ddraw_surface7_PageLock,
5522 ddraw_surface7_PageUnlock,
5523 /* IDirectDrawSurface3 */
5524 ddraw_surface7_SetSurfaceDesc,
5525 /* IDirectDrawSurface4 */
5526 ddraw_surface7_SetPrivateData,
5527 ddraw_surface7_GetPrivateData,
5528 ddraw_surface7_FreePrivateData,
5529 ddraw_surface7_GetUniquenessValue,
5530 ddraw_surface7_ChangeUniquenessValue,
5531 /* IDirectDrawSurface7 */
5532 ddraw_surface7_SetPriority,
5533 ddraw_surface7_GetPriority,
5534 ddraw_surface7_SetLOD,
5535 ddraw_surface7_GetLOD,
5538 /* Some windowed mode wrappers expect this vtbl to be writable. */
5539 static struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5541 /* IUnknown */
5542 ddraw_surface4_QueryInterface,
5543 ddraw_surface4_AddRef,
5544 ddraw_surface4_Release,
5545 /* IDirectDrawSurface */
5546 ddraw_surface4_AddAttachedSurface,
5547 ddraw_surface4_AddOverlayDirtyRect,
5548 ddraw_surface4_Blt,
5549 ddraw_surface4_BltBatch,
5550 ddraw_surface4_BltFast,
5551 ddraw_surface4_DeleteAttachedSurface,
5552 ddraw_surface4_EnumAttachedSurfaces,
5553 ddraw_surface4_EnumOverlayZOrders,
5554 ddraw_surface4_Flip,
5555 ddraw_surface4_GetAttachedSurface,
5556 ddraw_surface4_GetBltStatus,
5557 ddraw_surface4_GetCaps,
5558 ddraw_surface4_GetClipper,
5559 ddraw_surface4_GetColorKey,
5560 ddraw_surface4_GetDC,
5561 ddraw_surface4_GetFlipStatus,
5562 ddraw_surface4_GetOverlayPosition,
5563 ddraw_surface4_GetPalette,
5564 ddraw_surface4_GetPixelFormat,
5565 ddraw_surface4_GetSurfaceDesc,
5566 ddraw_surface4_Initialize,
5567 ddraw_surface4_IsLost,
5568 ddraw_surface4_Lock,
5569 ddraw_surface4_ReleaseDC,
5570 ddraw_surface4_Restore,
5571 ddraw_surface4_SetClipper,
5572 ddraw_surface4_SetColorKey,
5573 ddraw_surface4_SetOverlayPosition,
5574 ddraw_surface4_SetPalette,
5575 ddraw_surface4_Unlock,
5576 ddraw_surface4_UpdateOverlay,
5577 ddraw_surface4_UpdateOverlayDisplay,
5578 ddraw_surface4_UpdateOverlayZOrder,
5579 /* IDirectDrawSurface2 */
5580 ddraw_surface4_GetDDInterface,
5581 ddraw_surface4_PageLock,
5582 ddraw_surface4_PageUnlock,
5583 /* IDirectDrawSurface3 */
5584 ddraw_surface4_SetSurfaceDesc,
5585 /* IDirectDrawSurface4 */
5586 ddraw_surface4_SetPrivateData,
5587 ddraw_surface4_GetPrivateData,
5588 ddraw_surface4_FreePrivateData,
5589 ddraw_surface4_GetUniquenessValue,
5590 ddraw_surface4_ChangeUniquenessValue,
5593 /* Some windowed mode wrappers expect this vtbl to be writable. */
5594 static struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5596 /* IUnknown */
5597 ddraw_surface3_QueryInterface,
5598 ddraw_surface3_AddRef,
5599 ddraw_surface3_Release,
5600 /* IDirectDrawSurface */
5601 ddraw_surface3_AddAttachedSurface,
5602 ddraw_surface3_AddOverlayDirtyRect,
5603 ddraw_surface3_Blt,
5604 ddraw_surface3_BltBatch,
5605 ddraw_surface3_BltFast,
5606 ddraw_surface3_DeleteAttachedSurface,
5607 ddraw_surface3_EnumAttachedSurfaces,
5608 ddraw_surface3_EnumOverlayZOrders,
5609 ddraw_surface3_Flip,
5610 ddraw_surface3_GetAttachedSurface,
5611 ddraw_surface3_GetBltStatus,
5612 ddraw_surface3_GetCaps,
5613 ddraw_surface3_GetClipper,
5614 ddraw_surface3_GetColorKey,
5615 ddraw_surface3_GetDC,
5616 ddraw_surface3_GetFlipStatus,
5617 ddraw_surface3_GetOverlayPosition,
5618 ddraw_surface3_GetPalette,
5619 ddraw_surface3_GetPixelFormat,
5620 ddraw_surface3_GetSurfaceDesc,
5621 ddraw_surface3_Initialize,
5622 ddraw_surface3_IsLost,
5623 ddraw_surface3_Lock,
5624 ddraw_surface3_ReleaseDC,
5625 ddraw_surface3_Restore,
5626 ddraw_surface3_SetClipper,
5627 ddraw_surface3_SetColorKey,
5628 ddraw_surface3_SetOverlayPosition,
5629 ddraw_surface3_SetPalette,
5630 ddraw_surface3_Unlock,
5631 ddraw_surface3_UpdateOverlay,
5632 ddraw_surface3_UpdateOverlayDisplay,
5633 ddraw_surface3_UpdateOverlayZOrder,
5634 /* IDirectDrawSurface2 */
5635 ddraw_surface3_GetDDInterface,
5636 ddraw_surface3_PageLock,
5637 ddraw_surface3_PageUnlock,
5638 /* IDirectDrawSurface3 */
5639 ddraw_surface3_SetSurfaceDesc,
5642 /* Some windowed mode wrappers expect this vtbl to be writable. */
5643 static struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5645 /* IUnknown */
5646 ddraw_surface2_QueryInterface,
5647 ddraw_surface2_AddRef,
5648 ddraw_surface2_Release,
5649 /* IDirectDrawSurface */
5650 ddraw_surface2_AddAttachedSurface,
5651 ddraw_surface2_AddOverlayDirtyRect,
5652 ddraw_surface2_Blt,
5653 ddraw_surface2_BltBatch,
5654 ddraw_surface2_BltFast,
5655 ddraw_surface2_DeleteAttachedSurface,
5656 ddraw_surface2_EnumAttachedSurfaces,
5657 ddraw_surface2_EnumOverlayZOrders,
5658 ddraw_surface2_Flip,
5659 ddraw_surface2_GetAttachedSurface,
5660 ddraw_surface2_GetBltStatus,
5661 ddraw_surface2_GetCaps,
5662 ddraw_surface2_GetClipper,
5663 ddraw_surface2_GetColorKey,
5664 ddraw_surface2_GetDC,
5665 ddraw_surface2_GetFlipStatus,
5666 ddraw_surface2_GetOverlayPosition,
5667 ddraw_surface2_GetPalette,
5668 ddraw_surface2_GetPixelFormat,
5669 ddraw_surface2_GetSurfaceDesc,
5670 ddraw_surface2_Initialize,
5671 ddraw_surface2_IsLost,
5672 ddraw_surface2_Lock,
5673 ddraw_surface2_ReleaseDC,
5674 ddraw_surface2_Restore,
5675 ddraw_surface2_SetClipper,
5676 ddraw_surface2_SetColorKey,
5677 ddraw_surface2_SetOverlayPosition,
5678 ddraw_surface2_SetPalette,
5679 ddraw_surface2_Unlock,
5680 ddraw_surface2_UpdateOverlay,
5681 ddraw_surface2_UpdateOverlayDisplay,
5682 ddraw_surface2_UpdateOverlayZOrder,
5683 /* IDirectDrawSurface2 */
5684 ddraw_surface2_GetDDInterface,
5685 ddraw_surface2_PageLock,
5686 ddraw_surface2_PageUnlock,
5689 /* Bad Mojo Redux expects this vtbl to be writable. */
5690 static struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5692 /* IUnknown */
5693 ddraw_surface1_QueryInterface,
5694 ddraw_surface1_AddRef,
5695 ddraw_surface1_Release,
5696 /* IDirectDrawSurface */
5697 ddraw_surface1_AddAttachedSurface,
5698 ddraw_surface1_AddOverlayDirtyRect,
5699 ddraw_surface1_Blt,
5700 ddraw_surface1_BltBatch,
5701 ddraw_surface1_BltFast,
5702 ddraw_surface1_DeleteAttachedSurface,
5703 ddraw_surface1_EnumAttachedSurfaces,
5704 ddraw_surface1_EnumOverlayZOrders,
5705 ddraw_surface1_Flip,
5706 ddraw_surface1_GetAttachedSurface,
5707 ddraw_surface1_GetBltStatus,
5708 ddraw_surface1_GetCaps,
5709 ddraw_surface1_GetClipper,
5710 ddraw_surface1_GetColorKey,
5711 ddraw_surface1_GetDC,
5712 ddraw_surface1_GetFlipStatus,
5713 ddraw_surface1_GetOverlayPosition,
5714 ddraw_surface1_GetPalette,
5715 ddraw_surface1_GetPixelFormat,
5716 ddraw_surface1_GetSurfaceDesc,
5717 ddraw_surface1_Initialize,
5718 ddraw_surface1_IsLost,
5719 ddraw_surface1_Lock,
5720 ddraw_surface1_ReleaseDC,
5721 ddraw_surface1_Restore,
5722 ddraw_surface1_SetClipper,
5723 ddraw_surface1_SetColorKey,
5724 ddraw_surface1_SetOverlayPosition,
5725 ddraw_surface1_SetPalette,
5726 ddraw_surface1_Unlock,
5727 ddraw_surface1_UpdateOverlay,
5728 ddraw_surface1_UpdateOverlayDisplay,
5729 ddraw_surface1_UpdateOverlayZOrder,
5732 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5734 ddraw_gamma_control_QueryInterface,
5735 ddraw_gamma_control_AddRef,
5736 ddraw_gamma_control_Release,
5737 ddraw_gamma_control_GetGammaRamp,
5738 ddraw_gamma_control_SetGammaRamp,
5741 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5743 d3d_texture2_QueryInterface,
5744 d3d_texture2_AddRef,
5745 d3d_texture2_Release,
5746 d3d_texture2_GetHandle,
5747 d3d_texture2_PaletteChanged,
5748 d3d_texture2_Load,
5751 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5753 d3d_texture1_QueryInterface,
5754 d3d_texture1_AddRef,
5755 d3d_texture1_Release,
5756 d3d_texture1_Initialize,
5757 d3d_texture1_GetHandle,
5758 d3d_texture1_PaletteChanged,
5759 d3d_texture1_Load,
5760 d3d_texture1_Unload,
5763 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5764 * IDirectDrawSurface interface to ddraw methods. */
5765 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5767 if (!iface) return NULL;
5768 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5770 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5771 if (FAILED(hr))
5773 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5774 return NULL;
5776 IDirectDrawSurface7_Release(iface);
5778 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5781 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5783 if (!iface) return NULL;
5784 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5786 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5787 if (FAILED(hr))
5789 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5790 return NULL;
5792 IDirectDrawSurface4_Release(iface);
5794 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5797 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5799 if (!iface) return NULL;
5800 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5802 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5803 if (FAILED(hr))
5805 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5806 return NULL;
5808 IDirectDrawSurface3_Release(iface);
5810 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5813 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5815 if (!iface) return NULL;
5816 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5818 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5819 if (FAILED(hr))
5821 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5822 return NULL;
5824 IDirectDrawSurface2_Release(iface);
5826 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5829 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5831 if (!iface) return NULL;
5832 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5834 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5835 if (FAILED(hr))
5837 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5838 return NULL;
5840 IDirectDrawSurface_Release(iface);
5842 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5845 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5847 if (!iface) return NULL;
5848 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5849 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5852 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5854 if (!iface) return NULL;
5855 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5856 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5859 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5861 struct ddraw_surface *surface = parent;
5863 TRACE("surface %p.\n", surface);
5865 /* This shouldn't happen, ddraw_surface_release_iface() should prevent the
5866 * surface from being destroyed in this case. */
5867 if (surface->first_attached != surface)
5868 ERR("Surface is still attached to surface %p.\n", surface->first_attached);
5870 while (surface->next_attached)
5871 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5872 surface->next_attached, surface->next_attached->attached_iface)))
5873 ERR("DeleteAttachedSurface failed.\n");
5875 /* Having a texture handle set implies that the device still exists. */
5876 if (surface->Handle)
5877 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5879 /* Reduce the ddraw surface count. */
5880 list_remove(&surface->surface_list_entry);
5882 if (surface->clipper && ddraw_clipper_is_valid(surface->clipper))
5883 IDirectDrawClipper_Release(&surface->clipper->IDirectDrawClipper_iface);
5885 if (surface == surface->ddraw->primary)
5887 surface->ddraw->primary = NULL;
5888 surface->ddraw->gdi_surface = NULL;
5891 wined3d_private_store_cleanup(&surface->private_store);
5893 if (surface->draw_texture)
5894 wined3d_texture_decref(surface->wined3d_texture);
5896 heap_free(surface);
5899 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5901 ddraw_surface_wined3d_object_destroyed,
5904 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5906 struct ddraw_texture *texture = parent;
5908 TRACE("texture %p, texture_memory %p.\n", texture, texture->texture_memory);
5910 heap_free(texture->texture_memory);
5911 heap_free(parent);
5914 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5916 ddraw_texture_wined3d_object_destroyed,
5919 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5921 return DD_OK;
5924 static HRESULT ddraw_surface_reserve_memory(struct wined3d_texture *wined3d_texture)
5926 static const unsigned int extra_size = 0x10000;
5928 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
5929 struct wined3d_resource_desc resource_desc;
5930 struct wined3d_sub_resource_desc desc;
5931 unsigned int pitch, slice_pitch;
5932 unsigned int sub_resource_idx;
5933 HRESULT hr = WINED3D_OK;
5934 unsigned int offset;
5936 wined3d_resource_get_desc(wined3d_texture_get_resource(wined3d_texture), &resource_desc);
5937 if (!(texture->texture_memory = heap_alloc_zero(resource_desc.size + extra_size)))
5939 ERR("Out of memory.\n");
5940 return E_OUTOFMEMORY;
5942 TRACE("texture->texture_memory %p.\n", texture->texture_memory);
5944 offset = 0;
5945 sub_resource_idx = 0;
5946 while (wined3d_texture_get_sub_resource_desc(wined3d_texture, sub_resource_idx, &desc)
5947 == WINED3D_OK)
5949 wined3d_texture_get_pitch(wined3d_texture, sub_resource_idx, &pitch, &slice_pitch);
5951 if (FAILED(hr = wined3d_texture_update_desc(wined3d_texture, sub_resource_idx,
5952 desc.width, desc.height, resource_desc.format,
5953 desc.multisample_type, desc.multisample_quality,
5954 (BYTE *)texture->texture_memory + offset, pitch)))
5956 heap_free(texture->texture_memory);
5957 texture->texture_memory = NULL;
5958 break;
5960 ++sub_resource_idx;
5961 offset += desc.size;
5963 return hr;
5966 static HRESULT ddraw_surface_create_wined3d_texture(DDSURFACEDESC2 *desc, struct wined3d_device *wined3d_device,
5967 const struct wined3d_resource_desc *wined3d_desc, unsigned int layers, unsigned int levels,
5968 struct ddraw_texture *texture, struct wined3d_texture **wined3d_texture)
5970 struct wined3d_resource_desc draw_texture_desc;
5971 struct wined3d_texture *draw_texture;
5972 struct ddraw_surface *parent;
5973 unsigned int bind_flags;
5974 unsigned int i;
5975 HRESULT hr;
5977 bind_flags = 0;
5978 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5979 || (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5980 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
5982 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5983 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
5984 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5985 bind_flags |= WINED3D_BIND_RENDER_TARGET;
5987 if (!bind_flags || (wined3d_desc->access & WINED3D_RESOURCE_ACCESS_GPU && !(bind_flags & ~wined3d_desc->bind_flags)))
5988 goto no_draw_texture;
5990 draw_texture_desc = *wined3d_desc;
5991 draw_texture_desc.bind_flags = bind_flags;
5992 draw_texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5994 if (FAILED(hr = wined3d_texture_create(wined3d_device, &draw_texture_desc, layers,
5995 levels, 0, NULL, texture, &ddraw_texture_wined3d_parent_ops, &draw_texture)))
5997 WARN("Failed to create draw texture, hr %#x.\n", hr);
5998 goto no_draw_texture;
6000 wined3d_texture_decref(draw_texture);
6002 /* Some applications assume surfaces will always be mapped at the same
6003 * address. Some of those also assume that this address is valid even when
6004 * the surface isn't mapped, and that updates done this way will be
6005 * visible on the screen. The game Nox is such an application,
6006 * Commandos: Behind Enemy Lines is another. Setting
6007 * WINED3D_TEXTURE_CREATE_GET_DC_LENIENT will ensure this. */
6008 if (FAILED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6009 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, NULL, &ddraw_null_wined3d_parent_ops,
6010 wined3d_texture)))
6012 parent = wined3d_texture_get_sub_resource_parent(draw_texture, 0);
6013 if (texture->version == 7)
6014 IDirectDrawSurface7_Release(&parent->IDirectDrawSurface7_iface);
6015 else if (texture->version == 4)
6016 IDirectDrawSurface4_Release(&parent->IDirectDrawSurface4_iface);
6017 else
6018 IDirectDrawSurface_Release(&parent->IDirectDrawSurface_iface);
6019 return hr;
6021 wined3d_resource_set_parent(wined3d_texture_get_resource(*wined3d_texture), texture);
6022 for (i = 0; i < layers * levels; ++i)
6024 parent = wined3d_texture_get_sub_resource_parent(draw_texture, i);
6025 assert(parent->wined3d_texture == draw_texture);
6026 parent->draw_texture = draw_texture;
6027 parent->wined3d_texture = *wined3d_texture;
6028 wined3d_texture_set_sub_resource_parent(*wined3d_texture, i, parent);
6029 wined3d_texture_incref(*wined3d_texture);
6031 wined3d_texture_decref(*wined3d_texture);
6032 TRACE("Surface %p, created draw_texture %p, wined3d_texture %p.\n",
6033 wined3d_texture_get_sub_resource_parent(draw_texture, 0), draw_texture, wined3d_texture);
6034 return D3D_OK;
6036 no_draw_texture:
6037 if (SUCCEEDED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6038 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, texture, &ddraw_texture_wined3d_parent_ops,
6039 wined3d_texture)))
6040 wined3d_texture_decref(*wined3d_texture);
6041 return hr;
6044 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
6045 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
6047 struct wined3d_sub_resource_desc wined3d_mip_desc;
6048 struct ddraw_surface *root, *mip, **attach;
6049 struct wined3d_resource_desc wined3d_desc;
6050 DDPIXELFORMAT wined3d_display_mode_format;
6051 struct wined3d_texture *wined3d_texture;
6052 struct wined3d_display_mode mode;
6053 DDSURFACEDESC2 *desc, *mip_desc;
6054 struct ddraw_texture *texture;
6055 BOOL sysmem_fallback = FALSE;
6056 unsigned int layers = 1;
6057 unsigned int pitch = 0;
6058 BOOL reserve_memory;
6059 UINT levels, i, j;
6060 HRESULT hr;
6062 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
6063 ddraw, surface_desc, surface, outer_unknown, version);
6064 if (TRACE_ON(ddraw))
6066 TRACE("Requesting surface desc:\n");
6067 DDRAW_dump_surface_desc(surface_desc);
6070 if (outer_unknown)
6071 return CLASS_E_NOAGGREGATION;
6073 if (!surface)
6074 return E_POINTER;
6076 if (!(texture = heap_alloc(sizeof(*texture))))
6077 return E_OUTOFMEMORY;
6079 texture->texture_memory = NULL;
6080 texture->version = version;
6081 texture->surface_desc = *surface_desc;
6082 desc = &texture->surface_desc;
6084 /* Ensure DDSD_CAPS is always set. */
6085 desc->dwFlags |= DDSD_CAPS;
6087 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6089 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->u5.dwBackBufferCount)
6091 WARN("Tried to create a flippable surface without any back buffers.\n");
6092 heap_free(texture);
6093 return DDERR_INVALIDCAPS;
6096 if (!(desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX))
6098 WARN("Tried to create a flippable surface without DDSCAPS_COMPLEX.\n");
6099 heap_free(texture);
6100 return DDERR_INVALIDCAPS;
6103 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6105 WARN("Tried to create a flippable cubemap.\n");
6106 heap_free(texture);
6107 return DDERR_INVALIDPARAMS;
6110 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6112 FIXME("Flippable textures not implemented.\n");
6113 heap_free(texture);
6114 return DDERR_INVALIDCAPS;
6117 else
6119 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6121 WARN("Tried to specify a back buffer count for a non-flippable surface.\n");
6122 hr = desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP ? DDERR_INVALIDPARAMS : DDERR_INVALIDCAPS;
6123 heap_free(texture);
6124 return hr;
6128 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6130 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6132 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
6133 heap_free(texture);
6134 return DDERR_INVALIDCAPS;
6137 if ((desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX) && !(desc->ddsCaps.dwCaps & DDSCAPS_FLIP))
6139 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
6140 heap_free(texture);
6141 return DDERR_INVALIDCAPS;
6144 if ((desc->ddsCaps.dwCaps & DDSCAPS_FLIP) && !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
6146 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
6147 heap_free(texture);
6148 return DDERR_NOEXCLUSIVEMODE;
6152 /* This is a special case in ddrawex, but not allowed in ddraw. */
6153 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6154 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6156 WARN("Tried to create a surface in both system and video memory.\n");
6157 heap_free(texture);
6158 return DDERR_INVALIDCAPS;
6161 if ((desc->ddsCaps.dwCaps & (DDSCAPS_ALLOCONLOAD | DDSCAPS_MIPMAP))
6162 && !(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6164 WARN("Caps %#x require DDSCAPS_TEXTURE.\n", desc->ddsCaps.dwCaps);
6165 heap_free(texture);
6166 return DDERR_INVALIDCAPS;
6169 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
6170 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
6172 WARN("Cube map faces requested without cube map flag.\n");
6173 heap_free(texture);
6174 return DDERR_INVALIDCAPS;
6177 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6178 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
6180 WARN("Cube map without faces requested.\n");
6181 heap_free(texture);
6182 return DDERR_INVALIDPARAMS;
6185 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6186 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
6187 FIXME("Partial cube maps not implemented.\n");
6189 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6191 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6193 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
6194 heap_free(texture);
6195 return DDERR_INVALIDCAPS;
6197 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6199 WARN("DDSCAPS2_TEXTUREMANAGE used with DDSCAPS_VIDEOMEMORY "
6200 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
6201 heap_free(texture);
6202 return DDERR_INVALIDCAPS;
6206 if (desc->ddsCaps.dwCaps & DDSCAPS_WRITEONLY
6207 && !(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6209 WARN("DDSCAPS_WRITEONLY used without DDSCAPS2_TEXTUREMANAGE, returning DDERR_INVALIDCAPS.\n");
6210 heap_free(texture);
6211 return DDERR_INVALIDCAPS;
6214 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
6216 ERR("Failed to get display mode, hr %#x.\n", hr);
6217 heap_free(texture);
6218 return hr_ddraw_from_wined3d(hr);
6221 wined3d_display_mode_format.dwSize = sizeof(wined3d_display_mode_format);
6222 ddrawformat_from_wined3dformat(&wined3d_display_mode_format, mode.format_id);
6224 /* No pixelformat given? Use the current screen format. */
6225 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
6227 desc->dwFlags |= DDSD_PIXELFORMAT;
6228 desc->u4.ddpfPixelFormat = wined3d_display_mode_format;
6231 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6232 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
6233 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
6235 WARN("Unsupported / unknown pixelformat.\n");
6236 heap_free(texture);
6237 return DDERR_INVALIDPIXELFORMAT;
6240 /* No width or no height? Use the screen size. */
6241 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
6243 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
6245 WARN("No width / height specified.\n");
6246 heap_free(texture);
6247 return DDERR_INVALIDPARAMS;
6250 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
6251 desc->dwWidth = mode.width;
6252 desc->dwHeight = mode.height;
6255 if (!desc->dwWidth || !desc->dwHeight)
6257 heap_free(texture);
6258 return DDERR_INVALIDPARAMS;
6261 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6262 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
6264 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6266 /* The first surface is a front buffer, the back buffers are created
6267 * afterwards. */
6268 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
6269 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
6271 struct wined3d_swapchain_desc swapchain_desc;
6273 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
6274 swapchain_desc.backbuffer_width = mode.width;
6275 swapchain_desc.backbuffer_height = mode.height;
6276 swapchain_desc.backbuffer_format = mode.format_id;
6278 if (ddraw->d3ddevice)
6280 if (ddraw->d3ddevice->recording)
6281 wined3d_stateblock_decref(ddraw->d3ddevice->recording);
6282 ddraw->d3ddevice->recording = NULL;
6283 ddraw->d3ddevice->update_state = ddraw->d3ddevice->state;
6285 wined3d_stateblock_reset(ddraw->state);
6287 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
6288 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
6290 ERR("Failed to reset device.\n");
6291 heap_free(texture);
6292 return hr_ddraw_from_wined3d(hr);
6295 wined3d_stateblock_set_render_state(ddraw->state, WINED3D_RS_ZENABLE,
6296 !!swapchain_desc.enable_auto_depth_stencil);
6300 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
6301 wined3d_desc.multisample_quality = 0;
6302 wined3d_desc.usage = 0;
6303 wined3d_desc.bind_flags = 0;
6304 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6305 wined3d_desc.width = desc->dwWidth;
6306 wined3d_desc.height = desc->dwHeight;
6307 wined3d_desc.depth = 1;
6308 wined3d_desc.size = 0;
6310 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
6312 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
6313 /* Do not fail surface creation, only fail 3D device creation. */
6316 /* Mipmap count fixes */
6317 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6319 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
6321 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
6323 /* Mipmap count is given, should not be 0. */
6324 if (!desc->u2.dwMipMapCount)
6326 heap_free(texture);
6327 return DDERR_INVALIDPARAMS;
6330 else
6332 /* Undocumented feature: Create sublevels until either the
6333 * width or the height is 1. */
6334 if (version == 7)
6335 desc->u2.dwMipMapCount = wined3d_log2i(max(desc->dwWidth, desc->dwHeight)) + 1;
6336 else
6337 desc->u2.dwMipMapCount = wined3d_log2i(min(desc->dwWidth, desc->dwHeight)) + 1;
6340 else
6342 desc->u2.dwMipMapCount = 1;
6345 desc->dwFlags |= DDSD_MIPMAPCOUNT;
6346 levels = desc->u2.dwMipMapCount;
6348 else
6350 levels = 1;
6353 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
6355 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6357 unsigned int bind_flags = 0;
6358 DWORD usage = 0;
6360 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6362 usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6363 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6365 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6367 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6370 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6371 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6372 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6373 bind_flags |= WINED3D_BIND_RENDER_TARGET;
6375 if (!(ddraw->flags & DDRAW_NO3D) && SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d,
6376 ddraw->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL, mode.format_id,
6377 usage, bind_flags, WINED3D_RTYPE_TEXTURE_2D, wined3d_desc.format)))
6379 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
6381 else
6383 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6384 sysmem_fallback = TRUE;
6387 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6389 /* Tests show surfaces without memory flags get these flags added
6390 * right after creation. */
6391 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
6395 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6396 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6398 WARN("System memory overlays are not allowed.\n");
6399 heap_free(texture);
6400 return DDERR_NOOVERLAYHW;
6403 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
6405 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_CPU
6406 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6408 else
6410 if (!(ddraw->flags & DDRAW_NO3D))
6412 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6413 wined3d_desc.bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6414 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6415 wined3d_desc.bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6416 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6417 wined3d_desc.bind_flags |= WINED3D_BIND_RENDER_TARGET;
6420 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6422 wined3d_desc.bind_flags &= ~WINED3D_BIND_RENDER_TARGET;
6423 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU
6424 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6425 /* Managed textures have the system memory flag set. */
6426 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6428 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
6430 /* Videomemory adds localvidmem. This is mutually exclusive with
6431 * systemmemory and texturemanage. */
6432 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
6433 /* Dynamic resources can't be written by the GPU. */
6434 if (!(wined3d_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
6435 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
6439 if (desc->dwFlags & DDSD_LPSURFACE)
6441 if (wined3d_desc.access & WINED3D_RESOURCE_ACCESS_GPU)
6443 WARN("User memory surfaces should not be GPU accessible.\n");
6444 heap_free(texture);
6445 return DDERR_INVALIDCAPS;
6448 if (version < 4)
6450 WARN("User memory surfaces not supported before version 4.\n");
6451 heap_free(texture);
6452 return DDERR_INVALIDPARAMS;
6455 if (!desc->lpSurface)
6457 WARN("NULL surface memory pointer specified.\n");
6458 heap_free(texture);
6459 return DDERR_INVALIDPARAMS;
6462 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6464 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
6466 WARN("Pitch specified on a compressed user memory surface.\n");
6467 heap_free(texture);
6468 return DDERR_INVALIDPARAMS;
6471 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
6473 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
6474 heap_free(texture);
6475 return DDERR_INVALIDPARAMS;
6478 if ((desc->dwFlags & DDSD_LINEARSIZE)
6479 && desc->u1.dwLinearSize < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6480 wined3d_desc.format, wined3d_desc.width) * ((desc->dwHeight + 3) / 4))
6482 WARN("Invalid linear size %u specified.\n", desc->u1.dwLinearSize);
6483 heap_free(texture);
6484 return DDERR_INVALIDPARAMS;
6487 else
6489 if (!(desc->dwFlags & DDSD_PITCH))
6491 WARN("User memory surfaces should explicitly specify the pitch.\n");
6492 heap_free(texture);
6493 return DDERR_INVALIDPARAMS;
6496 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6497 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
6499 WARN("Invalid pitch %u specified.\n", desc->u1.lPitch);
6500 heap_free(texture);
6501 return DDERR_INVALIDPARAMS;
6504 pitch = desc->u1.lPitch;
6508 if (((desc->dwFlags & DDSD_CKDESTOVERLAY)
6509 && desc->u3.ddckCKDestOverlay.dwColorSpaceLowValue != desc->u3.ddckCKDestOverlay.dwColorSpaceHighValue)
6510 || ((desc->dwFlags & DDSD_CKDESTBLT)
6511 && desc->ddckCKDestBlt.dwColorSpaceLowValue != desc->ddckCKDestBlt.dwColorSpaceHighValue)
6512 || ((desc->dwFlags & DDSD_CKSRCOVERLAY)
6513 && desc->ddckCKSrcOverlay.dwColorSpaceLowValue != desc->ddckCKSrcOverlay.dwColorSpaceHighValue)
6514 || ((desc->dwFlags & DDSD_CKSRCBLT)
6515 && desc->ddckCKSrcBlt.dwColorSpaceLowValue != desc->ddckCKSrcBlt.dwColorSpaceHighValue))
6517 WARN("Range color keys not supported, returning DDERR_NOCOLORKEYHW.\n");
6518 heap_free(texture);
6519 return DDERR_NOCOLORKEYHW;
6522 if ((ddraw->flags & DDRAW_NO3D) && (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
6524 WARN("Video memory surfaces not supported without 3D support.\n");
6525 heap_free(texture);
6526 return DDERR_NODIRECTDRAWHW;
6529 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
6530 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
6532 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
6533 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
6535 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6537 wined3d_desc.usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6538 layers = 6;
6541 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, layers, levels,
6542 texture, &wined3d_texture)))
6544 WARN("Failed to create wined3d texture, hr %#x.\n", hr);
6545 heap_free(texture);
6546 return hr_ddraw_from_wined3d(hr);
6549 root = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6551 root->is_complex_root = TRUE;
6552 root->sysmem_fallback = sysmem_fallback;
6553 texture->root = root;
6554 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6556 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6557 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTOVERLAY,
6558 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6559 if (desc->dwFlags & DDSD_CKDESTBLT)
6560 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTBLT,
6561 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6562 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6563 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCOVERLAY,
6564 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6565 if (desc->dwFlags & DDSD_CKSRCBLT)
6566 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCBLT,
6567 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6569 for (i = 0; i < layers; ++i)
6571 attach = &root->complex_array[layers - 1 - i];
6573 for (j = 0; j < levels; ++j)
6575 mip = wined3d_texture_get_sub_resource_parent(wined3d_texture, i * levels + j);
6576 mip->sysmem_fallback = sysmem_fallback;
6577 mip_desc = &mip->surface_desc;
6578 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6579 mip_desc->u2.dwMipMapCount = levels - j;
6581 if (j)
6583 wined3d_texture_get_sub_resource_desc(wined3d_texture, i * levels + j, &wined3d_mip_desc);
6584 mip_desc->dwWidth = wined3d_mip_desc.width;
6585 mip_desc->dwHeight = wined3d_mip_desc.height;
6587 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6589 else
6591 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6594 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6596 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6598 switch (i)
6600 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6601 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6602 break;
6603 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6604 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6605 break;
6606 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6607 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6608 break;
6609 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6610 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6611 break;
6612 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6613 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6614 break;
6615 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6616 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6617 break;
6622 if (mip == root)
6623 continue;
6625 *attach = mip;
6626 attach = &mip->complex_array[0];
6630 if ((desc->dwFlags & DDSD_LPSURFACE) && FAILED(hr = wined3d_texture_update_desc(wined3d_texture, 0,
6631 wined3d_desc.width, wined3d_desc.height, wined3d_desc.format,
6632 WINED3D_MULTISAMPLE_NONE, 0, desc->lpSurface, pitch)))
6634 ERR("Failed to set surface memory, hr %#x.\n", hr);
6635 goto fail;
6638 reserve_memory = !(desc->dwFlags & DDSD_LPSURFACE)
6639 && desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY
6640 && wined3d_display_mode_format.u1.dwRGBBitCount <= 16;
6642 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture)))
6644 ERR("Failed to reserve surface memory, hr %#x.\n", hr);
6645 goto fail;
6648 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6650 unsigned int count = desc->u5.dwBackBufferCount;
6651 struct ddraw_surface *last = root;
6653 attach = &last->complex_array[0];
6654 for (i = 0; i < count; ++i)
6656 if (!(texture = heap_alloc(sizeof(*texture))))
6658 hr = E_OUTOFMEMORY;
6659 goto fail;
6662 texture->texture_memory = NULL;
6663 texture->version = version;
6664 texture->surface_desc = root->surface_desc;
6665 desc = &texture->surface_desc;
6667 /* Only one surface in the flipping chain is a back buffer, one is
6668 * a front buffer, the others are just flippable surfaces. */
6669 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6670 | DDSCAPS_BACKBUFFER);
6671 if (!i)
6672 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6673 desc->u5.dwBackBufferCount = 0;
6675 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, 1, 1,
6676 texture, &wined3d_texture)))
6678 heap_free(texture);
6679 hr = hr_ddraw_from_wined3d(hr);
6680 goto fail;
6683 last = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6684 last->sysmem_fallback = sysmem_fallback;
6685 texture->root = last;
6686 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6688 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6689 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6690 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6691 if (desc->dwFlags & DDSD_CKDESTBLT)
6692 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6693 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6694 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6695 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6696 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6697 if (desc->dwFlags & DDSD_CKSRCBLT)
6698 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6699 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6701 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture)))
6703 hr = hr_ddraw_from_wined3d(hr);
6704 goto fail;
6706 *attach = last;
6707 attach = &last->complex_array[0];
6709 *attach = root;
6712 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6714 ddraw->primary = root;
6715 ddraw->gdi_surface = root->wined3d_texture;
6717 *surface = root;
6719 return DD_OK;
6721 fail:
6722 if (version == 7)
6723 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6724 else if (version == 4)
6725 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6726 else
6727 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6729 return hr;
6732 void ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw,
6733 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
6734 const struct wined3d_parent_ops **parent_ops)
6736 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
6737 unsigned int texture_level, row_pitch, slice_pitch;
6738 DDSURFACEDESC2 *desc = &surface->surface_desc;
6739 unsigned int version = texture->version;
6741 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6742 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6743 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6744 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6745 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6746 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6747 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6748 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6749 surface->iface_count = 1;
6750 surface->version = version;
6751 surface->ddraw = ddraw;
6753 if (version == 7)
6755 surface->ref7 = 1;
6756 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6758 else if (version == 4)
6760 surface->ref4 = 1;
6761 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6763 else
6765 surface->ref1 = 1;
6766 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6769 *desc = texture->surface_desc;
6770 surface->first_attached = surface;
6772 texture_level = desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP ? sub_resource_idx % desc->u2.dwMipMapCount : 0;
6773 wined3d_texture_get_pitch(wined3d_texture, texture_level, &row_pitch, &slice_pitch);
6774 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6776 if (desc->dwFlags & DDSD_LPSURFACE)
6777 desc->u1.dwLinearSize = ~0u;
6778 else
6779 desc->u1.dwLinearSize = slice_pitch;
6780 desc->dwFlags |= DDSD_LINEARSIZE;
6781 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6783 else
6785 if (!(desc->dwFlags & DDSD_LPSURFACE))
6786 desc->u1.lPitch = row_pitch;
6787 desc->dwFlags |= DDSD_PITCH;
6788 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6790 desc->lpSurface = NULL;
6792 wined3d_texture_incref(surface->wined3d_texture = wined3d_texture);
6793 surface->sub_resource_idx = sub_resource_idx;
6794 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6795 surface->texture_location = DDRAW_SURFACE_LOCATION_DEFAULT;
6797 wined3d_private_store_init(&surface->private_store);
6800 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
6802 struct ddraw_surface *surface = parent;
6804 /* If the surface reference count drops to zero, we release our reference
6805 * to the view, but don't clear the pointer yet, in case e.g. a
6806 * GetRenderTarget() call brings the surface back before the view is
6807 * actually destroyed. When the view is destroyed, we need to clear the
6808 * pointer, or a subsequent surface AddRef() would reference it again.
6810 * This is safe because as long as the view still has a reference to the
6811 * texture, the surface is also still alive, and we're called before the
6812 * view releases that reference. */
6813 surface->wined3d_rtv = NULL;
6816 static const struct wined3d_parent_ops ddraw_view_wined3d_parent_ops =
6818 view_wined3d_object_destroyed,
6821 struct wined3d_rendertarget_view *ddraw_surface_get_rendertarget_view(struct ddraw_surface *surface)
6823 struct wined3d_texture *wined3d_texture;
6824 HRESULT hr;
6826 if (surface->wined3d_rtv)
6827 return surface->wined3d_rtv;
6829 wined3d_texture = surface->draw_texture ? surface->draw_texture : surface->wined3d_texture;
6830 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(wined3d_texture,
6831 surface->sub_resource_idx, surface, &ddraw_view_wined3d_parent_ops, &surface->wined3d_rtv)))
6833 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
6834 return NULL;
6837 return surface->wined3d_rtv;