wined3d: Invalidate sRGB write state in wined3d_cs_exec_set_rendertarget_view() if...
[wine.git] / dlls / ddraw / surface.c
blob19a1ae3d2d2631e3482838e99a35aa93d0631cee
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_texture_blt(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 struct wined3d_box box;
1031 HRESULT hr = DD_OK;
1033 TRACE("surface %p, rect %s, surface_desc %p, surface_desc_size %u, flags %#x, h %p.\n",
1034 surface, wine_dbgstr_rect(rect), surface_desc, surface_desc_size, flags, h);
1036 /* surface->surface_desc.dwWidth and dwHeight are changeable, thus lock */
1037 wined3d_mutex_lock();
1039 /* Should I check for the handle to be NULL?
1041 * The DDLOCK flags and the D3DLOCK flags are equal
1042 * for the supported values. The others are ignored by WineD3D
1045 /* Windows zeroes this if the rect is invalid */
1046 surface_desc->lpSurface = NULL;
1048 if (rect)
1050 if ((rect->left < 0) || (rect->top < 0)
1051 || (rect->left > rect->right) || (rect->right > surface->surface_desc.dwWidth)
1052 || (rect->top > rect->bottom) || (rect->bottom > surface->surface_desc.dwHeight))
1054 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
1055 wined3d_mutex_unlock();
1056 return DDERR_INVALIDPARAMS;
1058 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
1061 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1062 hr = ddraw_surface_update_frontbuffer(surface, rect, TRUE, 0);
1063 if (SUCCEEDED(hr))
1064 hr = wined3d_resource_map(wined3d_texture_get_resource
1065 (ddraw_surface_get_default_texture(surface, DDRAW_SURFACE_RW)), surface->sub_resource_idx,
1066 &map_desc, rect ? &box : NULL, wined3dmapflags_from_ddrawmapflags(flags));
1067 if (FAILED(hr))
1069 wined3d_mutex_unlock();
1070 switch(hr)
1072 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
1073 * specific error. But since wined3d returns that error in this only occasion,
1074 * keep d3d8 and d3d9 free from the return value override. There are many different
1075 * places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it is much easier
1076 * to do it in one place in ddraw.
1078 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
1079 default: return hr;
1083 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1085 if (flags & DDLOCK_READONLY)
1086 SetRectEmpty(&surface->ddraw->primary_lock);
1087 else if (rect)
1088 surface->ddraw->primary_lock = *rect;
1089 else
1090 SetRect(&surface->ddraw->primary_lock, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
1093 /* Windows does not set DDSD_LPSURFACE on locked surfaces. */
1094 DD_STRUCT_COPY_BYSIZE_(surface_desc, &surface->surface_desc, surface_desc_size, surface->surface_desc.dwSize);
1095 surface_desc->lpSurface = map_desc.data;
1097 TRACE("locked surface returning description :\n");
1098 if (TRACE_ON(ddraw))
1099 DDRAW_dump_surface_desc(surface_desc);
1101 wined3d_mutex_unlock();
1103 return DD_OK;
1106 static BOOL surface_validate_lock_desc(struct ddraw_surface *surface,
1107 const DDSURFACEDESC *desc, unsigned int *size)
1109 if (!desc)
1110 return FALSE;
1112 if (desc->dwSize == sizeof(DDSURFACEDESC) || desc->dwSize == sizeof(DDSURFACEDESC2))
1114 *size = desc->dwSize;
1115 return TRUE;
1118 if (surface->version == 7
1119 && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE
1120 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
1122 if (desc->dwSize >= sizeof(DDSURFACEDESC2))
1123 *size = sizeof(DDSURFACEDESC2);
1124 else
1125 *size = sizeof(DDSURFACEDESC);
1126 return TRUE;
1129 WARN("Invalid structure size %u.\n", desc->dwSize);
1130 return FALSE;
1133 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1134 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1136 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1137 unsigned int surface_desc_size;
1139 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1140 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1142 if (!surface_validate_lock_desc(surface, (DDSURFACEDESC *)surface_desc, &surface_desc_size))
1143 return DDERR_INVALIDPARAMS;
1145 if (ddraw_surface_is_lost(surface))
1147 WARN("Surface is lost.\n");
1148 return DDERR_SURFACELOST;
1151 return surface_lock(surface, rect, surface_desc, surface_desc_size, flags, h);
1154 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1155 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1157 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1158 unsigned int surface_desc_size;
1160 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1161 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1163 if (!surface_validate_lock_desc(surface, (DDSURFACEDESC *)surface_desc, &surface_desc_size))
1164 return DDERR_INVALIDPARAMS;
1166 if (ddraw_surface_is_lost(surface))
1168 WARN("Surface is lost.\n");
1169 return DDERR_SURFACELOST;
1172 return surface_lock(surface, rect, surface_desc, surface_desc_size, flags, h);
1175 static HRESULT ddraw_surface_lock_ddsd(struct ddraw_surface *surface, RECT *rect,
1176 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1178 unsigned int surface_desc_size;
1179 DDSURFACEDESC2 surface_desc2;
1180 HRESULT hr;
1182 if (!surface_validate_lock_desc(surface, surface_desc, &surface_desc_size))
1183 return DDERR_INVALIDPARAMS;
1185 if (ddraw_surface_is_lost(surface))
1187 WARN("Surface is lost.\n");
1188 return DDERR_SURFACELOST;
1191 surface_desc2.dwSize = surface_desc->dwSize;
1192 surface_desc2.dwFlags = 0;
1193 hr = surface_lock(surface, rect, &surface_desc2, surface_desc_size, flags, h);
1194 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1195 surface_desc->dwSize = surface_desc2.dwSize;
1196 return hr;
1199 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1200 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1202 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1204 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1205 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1207 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1210 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1211 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1213 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1215 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1216 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1218 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1221 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1222 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1224 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1226 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1227 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1229 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1232 /* FRAPS hooks IDirectDrawSurface::Unlock and expects the version 1 method to be called when the
1233 * game uses later interfaces. */
1234 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1236 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1237 HRESULT hr;
1239 TRACE("iface %p, data %p.\n", iface, data);
1241 wined3d_mutex_lock();
1242 hr = wined3d_resource_unmap(wined3d_texture_get_resource
1243 (ddraw_surface_get_default_texture(surface, 0)), surface->sub_resource_idx);
1244 if (SUCCEEDED(hr) && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1245 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE, 0);
1246 wined3d_mutex_unlock();
1248 return hr;
1251 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *rect)
1253 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1255 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1257 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, NULL);
1260 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *rect)
1262 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1264 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1266 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, NULL);
1269 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1271 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1273 TRACE("iface %p, data %p.\n", iface, data);
1275 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, data);
1278 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1280 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1282 TRACE("iface %p, data %p.\n", iface, data);
1284 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, data);
1287 static unsigned int ddraw_swap_interval_from_flags(DWORD flags)
1289 if (flags & DDFLIP_NOVSYNC)
1290 return 0;
1292 switch (flags & (DDFLIP_INTERVAL2 | DDFLIP_INTERVAL3 | DDFLIP_INTERVAL4))
1294 case DDFLIP_INTERVAL2:
1295 return 2;
1296 case DDFLIP_INTERVAL3:
1297 return 3;
1298 case DDFLIP_INTERVAL4:
1299 return 4;
1300 default:
1301 return 1;
1305 /* FRAPS hooks IDirectDrawSurface::Flip and expects the version 1 method to be called when the
1306 * game uses later interfaces. */
1307 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Flip(IDirectDrawSurface *iface,
1308 IDirectDrawSurface *src, DWORD flags)
1310 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
1311 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
1312 struct ddraw_texture *dst_ddraw_texture, *src_ddraw_texture;
1313 struct wined3d_rendertarget_view *tmp_rtv, *src_rtv, *rtv;
1314 DDSCAPS caps = {DDSCAPS_FLIP};
1315 struct wined3d_texture *texture, *draw_texture;
1316 IDirectDrawSurface *current;
1317 void *texture_memory;
1318 HRESULT hr;
1320 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1322 if (src == iface || !(dst_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1323 return DDERR_NOTFLIPPABLE;
1325 if (ddraw_surface_is_lost(dst_impl))
1326 return DDERR_SURFACELOST;
1328 wined3d_mutex_lock();
1330 if ((dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1331 && !(dst_impl->ddraw->cooperative_level & DDSCL_EXCLUSIVE))
1333 WARN("Not in exclusive mode.\n");
1334 wined3d_mutex_unlock();
1335 return DDERR_NOEXCLUSIVEMODE;
1338 tmp_rtv = ddraw_surface_get_rendertarget_view(dst_impl);
1339 if (dst_impl->sub_resource_idx)
1340 ERR("Invalid sub-resource index %u on surface %p.\n", dst_impl->sub_resource_idx, dst_impl);
1341 texture = dst_impl->wined3d_texture;
1342 rtv = wined3d_device_get_rendertarget_view(dst_impl->ddraw->wined3d_device, 0);
1343 dst_ddraw_texture = wined3d_texture_get_parent(dst_impl->wined3d_texture);
1344 texture_memory = dst_ddraw_texture->texture_memory;
1345 draw_texture = dst_impl->draw_texture;
1347 if (src_impl)
1349 for (current = iface; current != src;)
1351 if (FAILED(hr = ddraw_surface1_GetAttachedSurface(current, &caps, &current)))
1353 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1354 wined3d_mutex_unlock();
1355 return DDERR_NOTFLIPPABLE;
1357 ddraw_surface1_Release(current);
1358 if (current == iface)
1360 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1361 wined3d_mutex_unlock();
1362 return DDERR_NOTFLIPPABLE;
1366 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1367 if (rtv == dst_impl->wined3d_rtv)
1368 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1369 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1370 dst_impl->wined3d_rtv = src_rtv;
1371 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1372 if (src_impl->draw_texture)
1373 wined3d_texture_set_sub_resource_parent(src_impl->draw_texture, 0, dst_impl);
1374 src_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1375 dst_ddraw_texture->texture_memory = src_ddraw_texture->texture_memory;
1376 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), dst_ddraw_texture);
1377 if (src_impl->draw_texture)
1378 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->draw_texture), dst_ddraw_texture);
1379 dst_ddraw_texture = src_ddraw_texture;
1380 if (src_impl->sub_resource_idx)
1381 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1382 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1383 dst_impl->draw_texture = src_impl->draw_texture;
1385 else
1387 for (current = iface;;)
1389 if (FAILED(hr = ddraw_surface1_GetAttachedSurface(current, &caps, &current)))
1391 ERR("Can't find a flip target\n");
1392 wined3d_mutex_unlock();
1393 return DDERR_NOTFLIPPABLE; /* Unchecked */
1395 ddraw_surface1_Release(current);
1396 if (current == iface)
1398 dst_impl = impl_from_IDirectDrawSurface(iface);
1399 break;
1402 src_impl = impl_from_IDirectDrawSurface(current);
1403 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1404 if (rtv == dst_impl->wined3d_rtv)
1405 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1406 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1407 dst_impl->wined3d_rtv = src_rtv;
1408 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1409 if (src_impl->draw_texture)
1410 wined3d_texture_set_sub_resource_parent(src_impl->draw_texture, 0, dst_impl);
1411 src_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1412 dst_ddraw_texture->texture_memory = src_ddraw_texture->texture_memory;
1413 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), dst_ddraw_texture);
1414 if (src_impl->draw_texture)
1415 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->draw_texture), dst_ddraw_texture);
1416 dst_ddraw_texture = src_ddraw_texture;
1417 if (src_impl->sub_resource_idx)
1418 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1419 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1420 dst_impl->draw_texture = src_impl->draw_texture;
1421 dst_impl = src_impl;
1425 /* We don't have to worry about potential texture bindings, since
1426 * flippable surfaces can never be textures. */
1427 if (rtv == src_impl->wined3d_rtv)
1428 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, tmp_rtv, FALSE);
1429 wined3d_rendertarget_view_set_parent(tmp_rtv, src_impl);
1430 src_impl->wined3d_rtv = tmp_rtv;
1431 wined3d_texture_set_sub_resource_parent(texture, 0, src_impl);
1432 if (draw_texture)
1433 wined3d_texture_set_sub_resource_parent(draw_texture, 0, src_impl);
1434 dst_ddraw_texture->texture_memory = texture_memory;
1435 wined3d_resource_set_parent(wined3d_texture_get_resource(texture), dst_ddraw_texture);
1436 if (draw_texture)
1437 wined3d_resource_set_parent(wined3d_texture_get_resource(draw_texture), dst_ddraw_texture);
1438 src_impl->wined3d_texture = texture;
1439 src_impl->draw_texture = draw_texture;
1441 if (flags & ~(DDFLIP_NOVSYNC | DDFLIP_INTERVAL2 | DDFLIP_INTERVAL3 | DDFLIP_INTERVAL4))
1443 static UINT once;
1444 if (!once++)
1445 FIXME("Ignoring flags %#x.\n", flags);
1446 else
1447 WARN("Ignoring flags %#x.\n", flags);
1450 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1451 hr = ddraw_surface_update_frontbuffer(dst_impl, NULL, FALSE, ddraw_swap_interval_from_flags(flags));
1452 else
1453 hr = DD_OK;
1455 wined3d_mutex_unlock();
1457 return hr;
1460 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Flip(IDirectDrawSurface7 *iface,
1461 IDirectDrawSurface7 *src, DWORD flags)
1463 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1464 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src);
1466 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1468 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1469 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1472 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Flip(IDirectDrawSurface4 *iface,
1473 IDirectDrawSurface4 *src, DWORD flags)
1475 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1476 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
1478 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1480 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1481 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1484 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Flip(IDirectDrawSurface3 *iface,
1485 IDirectDrawSurface3 *src, DWORD flags)
1487 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1488 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src);
1490 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1492 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1493 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1496 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Flip(IDirectDrawSurface2 *iface,
1497 IDirectDrawSurface2 *src, DWORD flags)
1499 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1500 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src);
1502 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1504 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1505 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1508 static HRESULT ddraw_surface_blt(struct ddraw_surface *dst_surface, const RECT *dst_rect,
1509 struct ddraw_surface *src_surface, const RECT *src_rect, DWORD flags, DWORD fill_colour,
1510 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1512 struct wined3d_device *wined3d_device = dst_surface->ddraw->wined3d_device;
1513 struct wined3d_color colour;
1514 DWORD wined3d_flags;
1516 if (flags & DDBLT_COLORFILL)
1518 wined3d_flags = WINED3DCLEAR_TARGET;
1519 if (!(flags & DDBLT_ASYNC))
1520 wined3d_flags |= WINED3DCLEAR_SYNCHRONOUS;
1522 if (!wined3d_colour_from_ddraw_colour(&dst_surface->surface_desc.u4.ddpfPixelFormat,
1523 dst_surface->palette, fill_colour, &colour))
1524 return DDERR_INVALIDPARAMS;
1526 wined3d_device_apply_stateblock(wined3d_device, dst_surface->ddraw->state);
1527 ddraw_surface_get_draw_texture(dst_surface, dst_rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE);
1528 return wined3d_device_clear_rendertarget_view(wined3d_device,
1529 ddraw_surface_get_rendertarget_view(dst_surface),
1530 dst_rect, wined3d_flags, &colour, 0.0f, 0);
1533 if (flags & DDBLT_DEPTHFILL)
1535 wined3d_flags = WINED3DCLEAR_ZBUFFER;
1536 if (!(flags & DDBLT_ASYNC))
1537 wined3d_flags |= WINED3DCLEAR_SYNCHRONOUS;
1539 if (!wined3d_colour_from_ddraw_colour(&dst_surface->surface_desc.u4.ddpfPixelFormat,
1540 dst_surface->palette, fill_colour, &colour))
1541 return DDERR_INVALIDPARAMS;
1543 wined3d_device_apply_stateblock(wined3d_device, dst_surface->ddraw->state);
1544 ddraw_surface_get_draw_texture(dst_surface, dst_rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE);
1545 return wined3d_device_clear_rendertarget_view(wined3d_device,
1546 ddraw_surface_get_rendertarget_view(dst_surface),
1547 dst_rect, wined3d_flags, NULL, colour.r, 0);
1550 wined3d_flags = flags & ~DDBLT_ASYNC;
1551 if (wined3d_flags & ~WINED3D_BLT_MASK)
1553 FIXME("Unhandled flags %#x.\n", flags);
1554 return E_NOTIMPL;
1557 if (!(flags & DDBLT_ASYNC))
1558 wined3d_flags |= WINED3D_BLT_SYNCHRONOUS;
1560 return wined3d_texture_blt(ddraw_surface_get_any_texture(dst_surface, DDRAW_SURFACE_RW),
1561 dst_surface->sub_resource_idx, dst_rect, ddraw_surface_get_any_texture(src_surface, DDRAW_SURFACE_READ),
1562 src_surface->sub_resource_idx, src_rect, wined3d_flags, fx, filter);
1565 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1566 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags, DWORD fill_colour,
1567 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1569 RECT src_rect, dst_rect;
1570 float scale_x, scale_y;
1571 const RECT *clip_rect;
1572 UINT clip_list_size;
1573 RGNDATA *clip_list;
1574 HRESULT hr = DD_OK;
1575 UINT i;
1577 if (!dst_rect_in)
1578 SetRect(&dst_rect, 0, 0, dst_surface->surface_desc.dwWidth,
1579 dst_surface->surface_desc.dwHeight);
1580 else
1581 dst_rect = *dst_rect_in;
1583 if (IsRectEmpty(&dst_rect))
1584 return DDERR_INVALIDRECT;
1586 if (src_surface)
1588 if (!src_rect_in)
1589 SetRect(&src_rect, 0, 0, src_surface->surface_desc.dwWidth,
1590 src_surface->surface_desc.dwHeight);
1591 else
1592 src_rect = *src_rect_in;
1594 if (IsRectEmpty(&src_rect))
1595 return DDERR_INVALIDRECT;
1597 else
1599 SetRectEmpty(&src_rect);
1602 if (!dst_surface->clipper)
1604 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1605 hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect, TRUE, 0);
1606 if (SUCCEEDED(hr))
1607 hr = ddraw_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, flags, fill_colour, fx, filter);
1608 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
1609 hr = ddraw_surface_update_frontbuffer(dst_surface, &dst_rect, FALSE, 0);
1611 return hr;
1614 if (!ddraw_clipper_is_valid(dst_surface->clipper))
1616 FIXME("Attempting to blit with an invalid clipper.\n");
1617 return DDERR_INVALIDPARAMS;
1620 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1621 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1623 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1624 &dst_rect, NULL, &clip_list_size)))
1626 WARN("Failed to get clip list size, hr %#x.\n", hr);
1627 return hr;
1630 if (!(clip_list = heap_alloc(clip_list_size)))
1632 WARN("Failed to allocate clip list.\n");
1633 return E_OUTOFMEMORY;
1636 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1637 &dst_rect, clip_list, &clip_list_size)))
1639 WARN("Failed to get clip list, hr %#x.\n", hr);
1640 heap_free(clip_list);
1641 return hr;
1644 clip_rect = (RECT *)clip_list->Buffer;
1645 for (i = 0; i < clip_list->rdh.nCount; ++i)
1647 RECT src_rect_clipped = src_rect;
1649 if (src_surface)
1651 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1652 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1653 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1654 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1656 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1658 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE, 0)))
1659 break;
1663 if (FAILED(hr = ddraw_surface_blt(dst_surface, &clip_rect[i],
1664 src_surface, &src_rect_clipped, flags, fill_colour, fx, filter)))
1665 break;
1667 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1669 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE, 0)))
1670 break;
1674 heap_free(clip_list);
1675 return hr;
1678 /* FRAPS hooks IDirectDrawSurface::Blt and expects the version 1 method to be called when the
1679 * game uses later interfaces. */
1680 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1681 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1683 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
1684 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1685 struct wined3d_blt_fx wined3d_fx;
1686 DWORD unsupported_flags;
1687 DWORD fill_colour = 0;
1688 HRESULT hr = DD_OK;
1689 DDBLTFX rop_fx;
1691 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1692 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1694 unsupported_flags = DDBLT_ALPHADEST
1695 | DDBLT_ALPHADESTCONSTOVERRIDE
1696 | DDBLT_ALPHADESTNEG
1697 | DDBLT_ALPHADESTSURFACEOVERRIDE
1698 | DDBLT_ALPHAEDGEBLEND
1699 | DDBLT_ALPHASRC
1700 | DDBLT_ALPHASRCCONSTOVERRIDE
1701 | DDBLT_ALPHASRCNEG
1702 | DDBLT_ALPHASRCSURFACEOVERRIDE
1703 | DDBLT_ZBUFFER
1704 | DDBLT_ZBUFFERDESTCONSTOVERRIDE
1705 | DDBLT_ZBUFFERDESTOVERRIDE
1706 | DDBLT_ZBUFFERSRCCONSTOVERRIDE
1707 | DDBLT_ZBUFFERSRCOVERRIDE;
1708 if (flags & unsupported_flags)
1710 WARN("Ignoring unsupported flags %#x.\n", flags & unsupported_flags);
1711 flags &= ~unsupported_flags;
1714 if ((flags & DDBLT_KEYSRCOVERRIDE) && (!fx || flags & DDBLT_KEYSRC))
1716 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1717 return DDERR_INVALIDPARAMS;
1720 if ((flags & DDBLT_KEYDESTOVERRIDE) && (!fx || flags & DDBLT_KEYDEST))
1722 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1723 return DDERR_INVALIDPARAMS;
1726 if (flags & DDBLT_DDROPS)
1728 FIXME("DDBLT_DDROPS not implemented.\n");
1729 if (fx)
1730 FIXME(" rop %#x, pattern %p.\n", fx->dwDDROP, fx->u5.lpDDSPattern);
1731 return DDERR_NORASTEROPHW;
1734 wined3d_mutex_lock();
1736 if (flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1738 if (flags & DDBLT_ROP)
1740 wined3d_mutex_unlock();
1741 WARN("DDBLT_ROP used with DDBLT_COLORFILL or DDBLT_DEPTHFILL, returning DDERR_INVALIDPARAMS.\n");
1742 return DDERR_INVALIDPARAMS;
1744 if (src_impl)
1746 wined3d_mutex_unlock();
1747 WARN("Depth or colorfill is not compatible with source surfaces, returning DDERR_INVALIDPARAMS\n");
1748 return DDERR_INVALIDPARAMS;
1750 if (!fx)
1752 wined3d_mutex_unlock();
1753 WARN("Depth or colorfill used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1754 return DDERR_INVALIDPARAMS;
1757 if ((flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) == (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1758 flags &= ~DDBLT_DEPTHFILL;
1760 if ((dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_COLORFILL))
1762 wined3d_mutex_unlock();
1763 WARN("DDBLT_COLORFILL used on a depth buffer, returning DDERR_INVALIDPARAMS.\n");
1764 return DDERR_INVALIDPARAMS;
1766 if (!(dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_DEPTHFILL))
1768 wined3d_mutex_unlock();
1769 WARN("DDBLT_DEPTHFILL used on a color buffer, returning DDERR_INVALIDPARAMS.\n");
1770 return DDERR_INVALIDPARAMS;
1774 if (flags & DDBLT_ROP)
1776 if (!fx)
1778 wined3d_mutex_unlock();
1779 WARN("DDBLT_ROP used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1780 return DDERR_INVALIDPARAMS;
1783 if (src_impl && src_rect
1784 && ((ULONG)src_rect->left >= src_rect->right || src_rect->right > src_impl->surface_desc.dwWidth
1785 || (ULONG)src_rect->top >= src_rect->bottom || src_rect->bottom > src_impl->surface_desc.dwHeight))
1787 wined3d_mutex_unlock();
1788 WARN("Invalid source rectangle.\n");
1789 return DDERR_INVALIDRECT;
1792 flags &= ~DDBLT_ROP;
1793 switch (fx->dwROP)
1795 case SRCCOPY:
1796 break;
1798 case WHITENESS:
1799 case BLACKNESS:
1800 rop_fx = *fx;
1802 if (fx->dwROP == WHITENESS)
1803 rop_fx.u5.dwFillColor = 0xffffffff;
1804 else
1805 rop_fx.u5.dwFillColor = 0;
1807 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1808 flags |= DDBLT_DEPTHFILL;
1809 else
1810 flags |= DDBLT_COLORFILL;
1812 fx = &rop_fx;
1813 break;
1815 default:
1816 wined3d_mutex_unlock();
1817 WARN("Unsupported ROP %#x used, returning DDERR_NORASTEROPHW.\n", fx->dwROP);
1818 return DDERR_NORASTEROPHW;
1822 if (!(flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) && !src_impl)
1824 WARN("No source surface.\n");
1825 wined3d_mutex_unlock();
1826 return DDERR_INVALIDPARAMS;
1829 if (flags & DDBLT_KEYSRC && (!src_impl || !(src_impl->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1831 WARN("DDBLT_KEYSRC blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1832 wined3d_mutex_unlock();
1833 return DDERR_INVALIDPARAMS;
1835 if (flags & DDBLT_KEYDEST && !(dst_impl->surface_desc.dwFlags & DDSD_CKDESTBLT))
1837 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1838 wined3d_mutex_unlock();
1839 return DDERR_INVALIDPARAMS;
1842 if (fx)
1844 wined3d_fx.fx = fx->dwDDFX;
1845 fill_colour = fx->u5.dwFillColor;
1846 wined3d_fx.dst_color_key.color_space_low_value = fx->ddckDestColorkey.dwColorSpaceLowValue;
1847 wined3d_fx.dst_color_key.color_space_high_value = fx->ddckDestColorkey.dwColorSpaceHighValue;
1848 wined3d_fx.src_color_key.color_space_low_value = fx->ddckSrcColorkey.dwColorSpaceLowValue;
1849 wined3d_fx.src_color_key.color_space_high_value = fx->ddckSrcColorkey.dwColorSpaceHighValue;
1852 hr = ddraw_surface_blt_clipped(dst_impl, dst_rect, src_impl,
1853 src_rect, flags, fill_colour, fx ? &wined3d_fx : NULL, WINED3D_TEXF_LINEAR);
1855 wined3d_mutex_unlock();
1856 switch(hr)
1858 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1859 default: return hr;
1863 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *dst_rect,
1864 IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1866 struct ddraw_surface *dst = impl_from_IDirectDrawSurface7(iface);
1867 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface7(src_surface);
1869 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1870 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1872 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1873 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1876 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1877 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1879 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1880 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1882 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1883 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1885 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1886 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1889 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1890 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1892 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1893 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1895 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1896 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1898 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1899 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1902 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1903 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1905 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1906 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1908 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1909 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1911 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1912 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1915 /*****************************************************************************
1916 * IDirectDrawSurface7::AddAttachedSurface
1918 * Attaches a surface to another surface. How the surface attachments work
1919 * is not totally understood yet, and this method is prone to problems.
1920 * The surface that is attached is AddRef-ed.
1922 * Tests with complex surfaces suggest that the surface attachments form a
1923 * tree, but no method to test this has been found yet.
1925 * The attachment list consists of a first surface (first_attached) and
1926 * for each surface a pointer to the next attached surface (next_attached).
1927 * For the first surface, and a surface that has no attachments
1928 * first_attached points to the surface itself. A surface that has
1929 * no successors in the chain has next_attached set to NULL.
1931 * Newly attached surfaces are attached right after the root surface.
1932 * If a surface is attached to a complex surface compound, it's attached to
1933 * the surface that the app requested, not the complex root. See
1934 * GetAttachedSurface for a description how surfaces are found.
1936 * This is how the current implementation works, and it was coded by looking
1937 * at the needs of the applications.
1939 * So far only Z-Buffer attachments are tested, and they are activated in
1940 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1941 * Back buffers should work in 2D mode, but they are not tested(They can be
1942 * attached in older iface versions). Rendering to the front buffer and
1943 * switching between that and double buffering is not yet implemented in
1944 * WineD3D, so for 3D it might have unexpected results.
1946 * ddraw_surface_attach_surface is the real thing,
1947 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1948 * performs additional checks. Version 7 of this interface is much more restrictive
1949 * than its predecessors.
1951 * Params:
1952 * Attach: Surface to attach to iface
1954 * Returns:
1955 * DD_OK on success
1956 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1958 *****************************************************************************/
1959 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1961 TRACE("surface %p, attachment %p.\n", This, Surf);
1963 if(Surf == This)
1964 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1966 wined3d_mutex_lock();
1968 /* Check if the surface is already attached somewhere */
1969 if (Surf->next_attached || Surf->first_attached != Surf)
1971 /* TODO: Test for the structure of the manual attachment. Is it a
1972 * chain or a list? What happens if one surface is attached to 2
1973 * different surfaces? */
1974 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1975 Surf, Surf->next_attached, Surf->first_attached);
1977 wined3d_mutex_unlock();
1978 return DDERR_SURFACEALREADYATTACHED;
1981 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1982 Surf->next_attached = This->next_attached;
1983 Surf->first_attached = This->first_attached;
1984 This->next_attached = Surf;
1986 /* Check if the WineD3D depth stencil needs updating */
1987 if (This->ddraw->d3ddevice)
1988 d3d_device_update_depth_stencil(This->ddraw->d3ddevice);
1990 wined3d_mutex_unlock();
1992 return DD_OK;
1995 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
1997 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
1998 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1999 HRESULT hr;
2001 TRACE("iface %p, attachment %p.\n", iface, attachment);
2003 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
2004 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
2007 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
2008 attachment_impl->surface_desc.ddsCaps.dwCaps);
2009 return DDERR_CANNOTATTACHSURFACE;
2012 hr = ddraw_surface_attach_surface(This, attachment_impl);
2013 if (FAILED(hr))
2015 return hr;
2017 attachment_impl->attached_iface = (IUnknown *)attachment;
2018 IUnknown_AddRef(attachment_impl->attached_iface);
2019 return hr;
2022 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
2024 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2025 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
2026 HRESULT hr;
2028 TRACE("iface %p, attachment %p.\n", iface, attachment);
2030 /* Tests suggest that
2031 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
2032 * -> offscreen plain surfaces can be attached to primaries
2033 * -> primaries can be attached to offscreen plain surfaces
2034 * -> z buffers can be attached to primaries */
2035 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
2036 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
2038 /* Sizes have to match */
2039 if (attachment_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
2040 || attachment_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
2042 WARN("Surface sizes do not match.\n");
2043 return DDERR_CANNOTATTACHSURFACE;
2046 else if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
2047 || !(attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER)))
2049 WARN("Invalid attachment combination.\n");
2050 return DDERR_CANNOTATTACHSURFACE;
2053 if (FAILED(hr = ddraw_surface_attach_surface(surface, attachment_impl)))
2054 return hr;
2056 attachment_impl->attached_iface = (IUnknown *)attachment;
2057 IUnknown_AddRef(attachment_impl->attached_iface);
2058 return hr;
2061 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
2063 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2064 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2065 HRESULT hr;
2067 TRACE("iface %p, attachment %p.\n", iface, attachment);
2069 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2070 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2071 return hr;
2073 attachment_impl->attached_iface = (IUnknown *)attachment;
2074 IUnknown_AddRef(attachment_impl->attached_iface);
2075 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2076 return hr;
2079 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
2081 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2082 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2083 HRESULT hr;
2085 TRACE("iface %p, attachment %p.\n", iface, attachment);
2087 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2088 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2089 return hr;
2091 attachment_impl->attached_iface = (IUnknown *)attachment;
2092 IUnknown_AddRef(attachment_impl->attached_iface);
2093 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2094 return hr;
2097 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
2099 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2100 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2101 HRESULT hr;
2103 TRACE("iface %p, attachment %p.\n", iface, attachment);
2105 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2106 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2107 return hr;
2109 attachment_impl->attached_iface = (IUnknown *)attachment;
2110 IUnknown_AddRef(attachment_impl->attached_iface);
2111 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2112 return hr;
2115 /*****************************************************************************
2116 * IDirectDrawSurface7::DeleteAttachedSurface
2118 * Removes a surface from the attachment chain. The surface's refcount
2119 * is decreased by one after it has been removed
2121 * Params:
2122 * Flags: Some flags, not used by this implementation
2123 * Attach: Surface to detach
2125 * Returns:
2126 * DD_OK on success
2127 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
2129 *****************************************************************************/
2130 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
2131 struct ddraw_surface *attachment, IUnknown *detach_iface)
2133 struct ddraw_surface *prev = surface;
2135 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
2137 wined3d_mutex_lock();
2138 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
2140 wined3d_mutex_unlock();
2141 return DDERR_CANNOTDETACHSURFACE;
2144 if (attachment->attached_iface != detach_iface)
2146 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
2147 wined3d_mutex_unlock();
2148 return DDERR_SURFACENOTATTACHED;
2151 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
2152 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2154 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2155 /* FIXME: we should probably also subtract from dwMipMapCount of this
2156 * and all parent surfaces */
2159 /* Find the predecessor of the detached surface */
2160 while (prev->next_attached != attachment)
2162 if (!(prev = prev->next_attached))
2164 ERR("Failed to find predecessor of %p.\n", attachment);
2165 wined3d_mutex_unlock();
2166 return DDERR_SURFACENOTATTACHED;
2170 /* Unchain the surface */
2171 prev->next_attached = attachment->next_attached;
2172 attachment->next_attached = NULL;
2173 attachment->first_attached = attachment;
2175 /* Check if the wined3d depth stencil needs updating. Note that we don't
2176 * just call d3d_device_update_depth_stencil() here since it uses
2177 * QueryInterface(). Some applications, SCP - Containment Breach in
2178 * particular, modify the QueryInterface() pointer in the surface vtbl
2179 * but don't cleanup properly after the relevant dll is unloaded. */
2180 if (attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER
2181 && wined3d_device_get_depth_stencil_view(surface->ddraw->wined3d_device) == attachment->wined3d_rtv)
2182 wined3d_device_set_depth_stencil_view(surface->ddraw->wined3d_device, NULL);
2183 wined3d_mutex_unlock();
2185 /* Set attached_iface to NULL before releasing it, the surface may go
2186 * away. */
2187 attachment->attached_iface = NULL;
2188 IUnknown_Release(detach_iface);
2190 return DD_OK;
2193 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
2194 DWORD flags, IDirectDrawSurface7 *attachment)
2196 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2197 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
2199 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2201 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2204 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
2205 DWORD flags, IDirectDrawSurface4 *attachment)
2207 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2208 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
2210 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2212 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2215 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
2216 DWORD flags, IDirectDrawSurface3 *attachment)
2218 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2219 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2221 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2223 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2226 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
2227 DWORD flags, IDirectDrawSurface2 *attachment)
2229 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2230 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2232 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2234 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2237 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
2238 DWORD flags, IDirectDrawSurface *attachment)
2240 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2241 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2243 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2245 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2248 /*****************************************************************************
2249 * IDirectDrawSurface7::AddOverlayDirtyRect
2251 * "This method is not currently implemented"
2253 * Params:
2254 * Rect: ?
2256 * Returns:
2257 * DDERR_UNSUPPORTED
2259 *****************************************************************************/
2260 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
2262 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
2264 return DDERR_UNSUPPORTED; /* unchecked */
2267 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
2269 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2271 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2273 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2276 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
2278 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2280 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2282 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2285 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
2287 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2289 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2291 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2294 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
2296 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2298 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2300 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2303 /*****************************************************************************
2304 * IDirectDrawSurface7::GetDC
2306 * Returns a GDI device context for the surface
2308 * Params:
2309 * hdc: Address of a HDC variable to store the dc to
2311 * Returns:
2312 * DD_OK on success
2313 * DDERR_INVALIDPARAMS if hdc is NULL
2315 *****************************************************************************/
2316 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *dc)
2318 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2319 HRESULT hr = DD_OK;
2321 TRACE("iface %p, dc %p.\n", iface, dc);
2323 if (!dc)
2324 return DDERR_INVALIDPARAMS;
2326 wined3d_mutex_lock();
2327 if (surface->dc)
2328 hr = DDERR_DCALREADYCREATED;
2329 else if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2330 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE, 0);
2331 if (SUCCEEDED(hr))
2332 hr = wined3d_texture_get_dc(ddraw_surface_get_default_texture(surface, DDRAW_SURFACE_RW), surface->sub_resource_idx, dc);
2334 if (SUCCEEDED(hr))
2336 surface->dc = *dc;
2338 if (format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
2340 const struct ddraw_palette *palette;
2342 if (surface->palette)
2343 palette = surface->palette;
2344 else if (surface->ddraw->primary)
2345 palette = surface->ddraw->primary->palette;
2346 else
2347 palette = NULL;
2349 if (palette)
2350 wined3d_palette_apply_to_dc(palette->wined3d_palette, *dc);
2354 wined3d_mutex_unlock();
2355 switch (hr)
2357 /* Some, but not all errors set *dc to NULL. E.g. DCALREADYCREATED
2358 * does not touch *dc. */
2359 case WINED3DERR_INVALIDCALL:
2360 *dc = NULL;
2361 return DDERR_CANTCREATEDC;
2363 default:
2364 return hr;
2368 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
2370 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2372 TRACE("iface %p, dc %p.\n", iface, dc);
2374 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2377 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
2379 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2381 TRACE("iface %p, dc %p.\n", iface, dc);
2383 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2386 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2388 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2390 TRACE("iface %p, dc %p.\n", iface, dc);
2392 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2395 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2397 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2399 TRACE("iface %p, dc %p.\n", iface, dc);
2401 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2404 /*****************************************************************************
2405 * IDirectDrawSurface7::ReleaseDC
2407 * Releases the DC that was constructed with GetDC
2409 * Params:
2410 * hdc: HDC to release
2412 * Returns:
2413 * DD_OK on success, error code otherwise.
2415 *****************************************************************************/
2416 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2418 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2419 HRESULT hr;
2421 TRACE("iface %p, dc %p.\n", iface, hdc);
2423 wined3d_mutex_lock();
2424 if (!surface->dc)
2426 hr = DDERR_NODC;
2428 else if (SUCCEEDED(hr = wined3d_texture_release_dc(ddraw_surface_get_default_texture(surface, 0),
2429 surface->sub_resource_idx, hdc)))
2431 surface->dc = NULL;
2432 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2433 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE, 0);
2435 wined3d_mutex_unlock();
2438 return hr;
2441 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2443 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2445 TRACE("iface %p, dc %p.\n", iface, dc);
2447 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2450 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2452 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2454 TRACE("iface %p, dc %p.\n", iface, dc);
2456 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2459 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2461 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2463 TRACE("iface %p, dc %p.\n", iface, dc);
2465 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2468 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2470 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2472 TRACE("iface %p, dc %p.\n", iface, dc);
2474 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2477 /*****************************************************************************
2478 * IDirectDrawSurface7::GetCaps
2480 * Returns the surface's caps
2482 * Params:
2483 * Caps: Address to write the caps to
2485 * Returns:
2486 * DD_OK on success
2487 * DDERR_INVALIDPARAMS if Caps is NULL
2489 *****************************************************************************/
2490 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2492 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2494 TRACE("iface %p, caps %p.\n", iface, Caps);
2496 if(!Caps)
2497 return DDERR_INVALIDPARAMS;
2499 *Caps = surface->surface_desc.ddsCaps;
2501 return DD_OK;
2504 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2506 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2508 TRACE("iface %p, caps %p.\n", iface, caps);
2510 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2513 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2515 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2516 DDSCAPS2 caps2;
2517 HRESULT hr;
2519 TRACE("iface %p, caps %p.\n", iface, caps);
2521 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2522 if (FAILED(hr)) return hr;
2524 caps->dwCaps = caps2.dwCaps;
2525 return hr;
2528 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2530 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2531 DDSCAPS2 caps2;
2532 HRESULT hr;
2534 TRACE("iface %p, caps %p.\n", iface, caps);
2536 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2537 if (FAILED(hr)) return hr;
2539 caps->dwCaps = caps2.dwCaps;
2540 return hr;
2543 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2545 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2546 DDSCAPS2 caps2;
2547 HRESULT hr;
2549 TRACE("iface %p, caps %p.\n", iface, caps);
2551 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2552 if (FAILED(hr)) return hr;
2554 caps->dwCaps = caps2.dwCaps;
2555 return hr;
2558 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD priority)
2560 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2561 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2562 HRESULT hr;
2563 struct wined3d_resource *resource;
2565 TRACE("iface %p, priority %u.\n", iface, priority);
2567 wined3d_mutex_lock();
2568 /* No need to check for offscreen plain surfaces or mipmap sublevels. SetPriority
2569 * calls on such surfaces segfault on Windows. */
2570 if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed))
2572 WARN("Called on non-managed texture returning DDERR_INVALIDPARAMS.\n");
2573 hr = DDERR_INVALIDPARAMS;
2575 else
2577 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2578 wined3d_resource_set_priority(resource, priority);
2579 if (surface->draw_texture)
2580 wined3d_resource_set_priority(wined3d_texture_get_resource(surface->draw_texture), priority);
2582 hr = DD_OK;
2584 wined3d_mutex_unlock();
2586 return hr;
2589 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *priority)
2591 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2592 const struct wined3d_resource *resource;
2593 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2594 HRESULT hr;
2596 TRACE("iface %p, priority %p.\n", iface, priority);
2598 wined3d_mutex_lock();
2599 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN)
2601 WARN("Called on offscreenplain surface, returning DDERR_INVALIDOBJECT.\n");
2602 hr = DDERR_INVALIDOBJECT;
2604 else if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed) || !surface->is_complex_root)
2606 WARN("Called on non-managed texture or non-root surface, returning DDERR_INVALIDPARAMS.\n");
2607 hr = DDERR_INVALIDPARAMS;
2609 else
2611 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2612 *priority = wined3d_resource_get_priority(resource);
2613 hr = DD_OK;
2615 wined3d_mutex_unlock();
2617 return hr;
2620 /*****************************************************************************
2621 * IDirectDrawSurface7::SetPrivateData
2623 * Stores some data in the surface that is intended for the application's
2624 * use.
2626 * Params:
2627 * tag: GUID that identifies the data
2628 * Data: Pointer to the private data
2629 * Size: Size of the private data
2630 * Flags: Some flags
2632 * Returns:
2633 * D3D_OK on success, error code otherwise.
2635 *****************************************************************************/
2636 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2637 REFGUID tag, void *data, DWORD size, DWORD flags)
2639 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2640 HRESULT hr;
2642 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2643 iface, debugstr_guid(tag), data, size, flags);
2645 if (!data)
2647 WARN("data is NULL, returning DDERR_INVALIDPARAMS.\n");
2648 return DDERR_INVALIDPARAMS;
2651 wined3d_mutex_lock();
2652 hr = wined3d_private_store_set_private_data(&surface->private_store, tag, data, size, flags);
2653 wined3d_mutex_unlock();
2654 return hr_ddraw_from_wined3d(hr);
2657 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2658 REFGUID tag, void *data, DWORD size, DWORD flags)
2660 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2662 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2663 iface, debugstr_guid(tag), data, size, flags);
2665 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2668 /*****************************************************************************
2669 * IDirectDrawSurface7::GetPrivateData
2671 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2673 * Params:
2674 * tag: GUID of the data to return
2675 * Data: Address where to write the data to
2676 * Size: Size of the buffer at Data
2678 * Returns:
2679 * DD_OK on success
2680 * DDERR_INVALIDPARAMS if Data is NULL
2682 *****************************************************************************/
2683 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *data, DWORD *size)
2685 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2686 const struct wined3d_private_data *stored_data;
2687 HRESULT hr;
2689 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2690 iface, debugstr_guid(tag), data, size);
2692 wined3d_mutex_lock();
2693 stored_data = wined3d_private_store_get_private_data(&surface->private_store, tag);
2694 if (!stored_data)
2696 hr = DDERR_NOTFOUND;
2697 goto done;
2699 if (!size)
2701 hr = DDERR_INVALIDPARAMS;
2702 goto done;
2704 if (*size < stored_data->size)
2706 *size = stored_data->size;
2707 hr = DDERR_MOREDATA;
2708 goto done;
2710 if (!data)
2712 hr = DDERR_INVALIDPARAMS;
2713 goto done;
2716 *size = stored_data->size;
2717 memcpy(data, stored_data->content.data, stored_data->size);
2718 hr = DD_OK;
2720 done:
2721 wined3d_mutex_unlock();
2722 return hr;
2725 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2727 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2729 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2730 iface, debugstr_guid(tag), data, size);
2732 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2735 /*****************************************************************************
2736 * IDirectDrawSurface7::FreePrivateData
2738 * Frees private data stored in the surface
2740 * Params:
2741 * tag: Tag of the data to free
2743 * Returns:
2744 * D3D_OK on success, error code otherwise.
2746 *****************************************************************************/
2747 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2749 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2750 struct wined3d_private_data *entry;
2752 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2754 wined3d_mutex_lock();
2755 entry = wined3d_private_store_get_private_data(&surface->private_store, tag);
2756 if (!entry)
2758 wined3d_mutex_unlock();
2759 return DDERR_NOTFOUND;
2762 wined3d_private_store_free_private_data(&surface->private_store, entry);
2763 wined3d_mutex_unlock();
2765 return DD_OK;
2768 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2770 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2772 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2774 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2777 /*****************************************************************************
2778 * IDirectDrawSurface7::PageLock
2780 * Prevents a sysmem surface from being paged out
2782 * Params:
2783 * Flags: Not used, must be 0(unchecked)
2785 * Returns:
2786 * DD_OK, because it's a stub
2788 *****************************************************************************/
2789 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2791 TRACE("iface %p, flags %#x.\n", iface, Flags);
2793 /* This is Windows memory management related - we don't need this */
2794 return DD_OK;
2797 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2799 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2801 TRACE("iface %p, flags %#x.\n", iface, flags);
2803 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2806 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2808 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2810 TRACE("iface %p, flags %#x.\n", iface, flags);
2812 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2815 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2817 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2819 TRACE("iface %p, flags %#x.\n", iface, flags);
2821 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2824 /*****************************************************************************
2825 * IDirectDrawSurface7::PageUnlock
2827 * Allows a sysmem surface to be paged out
2829 * Params:
2830 * Flags: Not used, must be 0(unchecked)
2832 * Returns:
2833 * DD_OK, because it's a stub
2835 *****************************************************************************/
2836 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2838 TRACE("iface %p, flags %#x.\n", iface, Flags);
2840 return DD_OK;
2843 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2845 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2847 TRACE("iface %p, flags %#x.\n", iface, flags);
2849 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2852 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2854 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2856 TRACE("iface %p, flags %#x.\n", iface, flags);
2858 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2861 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2863 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2865 TRACE("iface %p, flags %#x.\n", iface, flags);
2867 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2870 /*****************************************************************************
2871 * IDirectDrawSurface7::BltBatch
2873 * An unimplemented function
2875 * Params:
2878 * Returns:
2879 * DDERR_UNSUPPORTED
2881 *****************************************************************************/
2882 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2884 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2886 /* MSDN: "not currently implemented" */
2887 return DDERR_UNSUPPORTED;
2890 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2892 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2894 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2896 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2899 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2901 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2903 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2905 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2908 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2910 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2912 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2914 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2917 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2919 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2921 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2923 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2926 /*****************************************************************************
2927 * IDirectDrawSurface7::EnumAttachedSurfaces
2929 * Enumerates all surfaces attached to this surface
2931 * Params:
2932 * context: Pointer to pass unmodified to the callback
2933 * cb: Callback function to call for each surface
2935 * Returns:
2936 * DD_OK on success
2937 * DDERR_INVALIDPARAMS if cb is NULL
2939 *****************************************************************************/
2940 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2941 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2943 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2944 struct ddraw_surface *surf;
2945 DDSURFACEDESC2 desc;
2946 int i;
2948 /* Attached surfaces aren't handled in WineD3D */
2949 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2951 if(!cb)
2952 return DDERR_INVALIDPARAMS;
2954 wined3d_mutex_lock();
2956 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2958 surf = surface->complex_array[i];
2959 if(!surf) break;
2961 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2962 desc = surf->surface_desc;
2963 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2964 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2966 wined3d_mutex_unlock();
2967 return DD_OK;
2971 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2973 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2974 desc = surf->surface_desc;
2975 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2976 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2978 wined3d_mutex_unlock();
2979 return DD_OK;
2983 TRACE(" end of enumeration.\n");
2985 wined3d_mutex_unlock();
2987 return DD_OK;
2990 struct callback_info2
2992 LPDDENUMSURFACESCALLBACK2 callback;
2993 void *context;
2996 struct callback_info
2998 LPDDENUMSURFACESCALLBACK callback;
2999 void *context;
3002 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
3004 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3005 const struct callback_info2 *info = context;
3007 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3008 ddraw_surface7_Release(surface);
3010 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
3013 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
3015 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3016 const struct callback_info *info = context;
3018 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
3019 ddraw_surface7_Release(surface);
3021 /* FIXME: Check surface_test.dwSize */
3022 return info->callback(&surface_impl->IDirectDrawSurface_iface,
3023 (DDSURFACEDESC *)surface_desc, info->context);
3026 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
3027 void *context, LPDDENUMSURFACESCALLBACK2 callback)
3029 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3030 struct callback_info2 info;
3032 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3034 info.callback = callback;
3035 info.context = context;
3037 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3038 &info, EnumCallback2);
3041 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
3042 void *context, LPDDENUMSURFACESCALLBACK callback)
3044 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3045 struct callback_info info;
3047 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3049 info.callback = callback;
3050 info.context = context;
3052 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3053 &info, EnumCallback);
3056 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
3057 void *context, LPDDENUMSURFACESCALLBACK callback)
3059 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3060 struct callback_info info;
3062 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3064 info.callback = callback;
3065 info.context = context;
3067 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3068 &info, EnumCallback);
3071 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
3072 void *context, LPDDENUMSURFACESCALLBACK callback)
3074 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3075 struct callback_info info;
3077 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3079 info.callback = callback;
3080 info.context = context;
3082 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3083 &info, EnumCallback);
3086 /*****************************************************************************
3087 * IDirectDrawSurface7::EnumOverlayZOrders
3089 * "Enumerates the overlay surfaces on the specified destination"
3091 * Params:
3092 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
3093 * context: context to pass back to the callback
3094 * cb: callback function to call for each enumerated surface
3096 * Returns:
3097 * DD_OK, because it's a stub
3099 *****************************************************************************/
3100 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
3101 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
3103 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
3105 return DD_OK;
3108 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
3109 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3111 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3112 struct callback_info2 info;
3114 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3116 info.callback = callback;
3117 info.context = context;
3119 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3120 flags, &info, EnumCallback2);
3123 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
3124 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3126 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3127 struct callback_info info;
3129 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3131 info.callback = callback;
3132 info.context = context;
3134 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3135 flags, &info, EnumCallback);
3138 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
3139 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3141 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3142 struct callback_info info;
3144 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3146 info.callback = callback;
3147 info.context = context;
3149 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3150 flags, &info, EnumCallback);
3153 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
3154 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3156 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3157 struct callback_info info;
3159 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3161 info.callback = callback;
3162 info.context = context;
3164 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3165 flags, &info, EnumCallback);
3168 /*****************************************************************************
3169 * IDirectDrawSurface7::GetBltStatus
3171 * Returns the blitting status
3173 * Params:
3174 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
3176 *****************************************************************************/
3177 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3179 TRACE("iface %p, flags %#x.\n", iface, Flags);
3181 switch (Flags)
3183 case WINEDDGBS_CANBLT:
3184 case WINEDDGBS_ISBLTDONE:
3185 return DD_OK;
3187 default:
3188 return DDERR_INVALIDPARAMS;
3192 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
3194 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3196 TRACE("iface %p, flags %#x.\n", iface, flags);
3198 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3201 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
3203 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3205 TRACE("iface %p, flags %#x.\n", iface, flags);
3207 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3210 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
3212 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3214 TRACE("iface %p, flags %#x.\n", iface, flags);
3216 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3219 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
3221 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3223 TRACE("iface %p, flags %#x.\n", iface, flags);
3225 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3228 /*****************************************************************************
3229 * IDirectDrawSurface7::GetColorKey
3231 * Returns the color key assigned to the surface
3233 * Params:
3234 * Flags: Some flags
3235 * CKey: Address to store the key to
3237 * Returns:
3238 * DD_OK on success
3239 * DDERR_INVALIDPARAMS if CKey is NULL
3241 *****************************************************************************/
3242 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
3244 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3246 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
3248 if(!CKey)
3249 return DDERR_INVALIDPARAMS;
3251 wined3d_mutex_lock();
3253 switch (Flags)
3255 case DDCKEY_DESTBLT:
3256 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
3258 wined3d_mutex_unlock();
3259 return DDERR_NOCOLORKEY;
3261 *CKey = This->surface_desc.ddckCKDestBlt;
3262 break;
3264 case DDCKEY_DESTOVERLAY:
3265 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
3267 wined3d_mutex_unlock();
3268 return DDERR_NOCOLORKEY;
3270 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
3271 break;
3273 case DDCKEY_SRCBLT:
3274 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
3276 wined3d_mutex_unlock();
3277 return DDERR_NOCOLORKEY;
3279 *CKey = This->surface_desc.ddckCKSrcBlt;
3280 break;
3282 case DDCKEY_SRCOVERLAY:
3283 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
3285 wined3d_mutex_unlock();
3286 return DDERR_NOCOLORKEY;
3288 *CKey = This->surface_desc.ddckCKSrcOverlay;
3289 break;
3291 default:
3292 wined3d_mutex_unlock();
3293 return DDERR_INVALIDPARAMS;
3296 wined3d_mutex_unlock();
3298 return DD_OK;
3301 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
3303 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3305 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3307 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3310 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
3312 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3314 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3316 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3319 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
3321 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3323 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3325 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3328 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
3330 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3332 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3334 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3337 /*****************************************************************************
3338 * IDirectDrawSurface7::GetFlipStatus
3340 * Returns the flipping status of the surface
3342 * Params:
3343 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
3345 *****************************************************************************/
3346 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3348 TRACE("iface %p, flags %#x.\n", iface, Flags);
3350 /* XXX: DDERR_INVALIDSURFACETYPE */
3352 switch (Flags)
3354 case WINEDDGFS_CANFLIP:
3355 case WINEDDGFS_ISFLIPDONE:
3356 return DD_OK;
3358 default:
3359 return DDERR_INVALIDPARAMS;
3363 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
3365 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3367 TRACE("iface %p, flags %#x.\n", iface, flags);
3369 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3372 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
3374 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3376 TRACE("iface %p, flags %#x.\n", iface, flags);
3378 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3381 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
3383 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3385 TRACE("iface %p, flags %#x.\n", iface, flags);
3387 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3390 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
3392 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3394 TRACE("iface %p, flags %#x.\n", iface, flags);
3396 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3399 /*****************************************************************************
3400 * IDirectDrawSurface7::GetOverlayPosition
3402 * Returns the display coordinates of a visible and active overlay surface
3404 * Params:
3408 * Returns:
3409 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3410 *****************************************************************************/
3411 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *x, LONG *y)
3413 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3414 HRESULT hr;
3416 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3418 wined3d_mutex_lock();
3419 hr = wined3d_texture_get_overlay_position(surface->wined3d_texture,
3420 surface->sub_resource_idx, x, y);
3421 wined3d_mutex_unlock();
3423 return hr;
3426 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3428 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3430 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3432 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3435 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3437 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3439 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3441 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3444 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3446 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3448 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3450 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3453 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3455 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3457 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3459 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3462 /*****************************************************************************
3463 * IDirectDrawSurface7::GetPixelFormat
3465 * Returns the pixel format of the Surface
3467 * Params:
3468 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3469 * format should be written
3471 * Returns:
3472 * DD_OK on success
3473 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3475 *****************************************************************************/
3476 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3478 /* What is DDERR_INVALIDSURFACETYPE for here? */
3479 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3481 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3483 if(!PixelFormat)
3484 return DDERR_INVALIDPARAMS;
3486 wined3d_mutex_lock();
3487 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3488 wined3d_mutex_unlock();
3490 return DD_OK;
3493 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3495 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3497 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3499 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3502 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3504 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3506 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3508 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3511 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3513 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3515 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3517 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3520 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3522 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3524 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3526 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3529 /*****************************************************************************
3530 * IDirectDrawSurface7::GetSurfaceDesc
3532 * Returns the description of this surface
3534 * Params:
3535 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3536 * surface desc
3538 * Returns:
3539 * DD_OK on success
3540 * DDERR_INVALIDPARAMS if DDSD is NULL
3542 *****************************************************************************/
3543 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3545 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3547 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3549 if(!DDSD)
3550 return DDERR_INVALIDPARAMS;
3552 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3554 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3555 return DDERR_INVALIDPARAMS;
3558 wined3d_mutex_lock();
3559 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3560 TRACE("Returning surface desc:\n");
3561 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3562 wined3d_mutex_unlock();
3564 return DD_OK;
3567 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3569 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3571 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3573 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3576 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3578 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3580 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3582 if (!surface_desc) return DDERR_INVALIDPARAMS;
3584 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3586 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3587 return DDERR_INVALIDPARAMS;
3590 wined3d_mutex_lock();
3591 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3592 TRACE("Returning surface desc:\n");
3593 if (TRACE_ON(ddraw))
3595 /* DDRAW_dump_surface_desc handles the smaller size */
3596 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3598 wined3d_mutex_unlock();
3600 return DD_OK;
3603 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3605 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3607 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3609 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3612 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3614 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3616 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3618 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3621 /*****************************************************************************
3622 * IDirectDrawSurface7::Initialize
3624 * Initializes the surface. This is a no-op in Wine
3626 * Params:
3627 * DD: Pointer to an DirectDraw interface
3628 * DDSD: Surface description for initialization
3630 * Returns:
3631 * DDERR_ALREADYINITIALIZED
3633 *****************************************************************************/
3634 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3635 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3637 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3639 return DDERR_ALREADYINITIALIZED;
3642 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3643 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3645 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3647 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3649 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3650 ddraw, surface_desc);
3653 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3654 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3656 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3657 DDSURFACEDESC2 surface_desc2;
3659 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3661 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3662 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3663 ddraw, surface_desc ? &surface_desc2 : NULL);
3666 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3667 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3669 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3670 DDSURFACEDESC2 surface_desc2;
3672 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3674 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3675 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3676 ddraw, surface_desc ? &surface_desc2 : NULL);
3679 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3680 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3682 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3683 DDSURFACEDESC2 surface_desc2;
3685 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3687 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3688 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3689 ddraw, surface_desc ? &surface_desc2 : NULL);
3692 /*****************************************************************************
3693 * IDirect3DTexture1::Initialize
3695 * The sdk says it's not implemented
3697 * Params:
3700 * Returns
3701 * DDERR_UNSUPPORTED
3703 *****************************************************************************/
3704 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3705 IDirect3DDevice *device, IDirectDrawSurface *surface)
3707 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3709 return DDERR_UNSUPPORTED; /* Unchecked */
3712 /*****************************************************************************
3713 * IDirectDrawSurface7::IsLost
3715 * Checks if the surface is lost
3717 * Returns:
3718 * DD_OK, if the surface is usable
3719 * DDERR_ISLOST if the surface is lost
3721 *****************************************************************************/
3722 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3724 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3726 TRACE("iface %p.\n", iface);
3728 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3731 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3733 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3735 TRACE("iface %p.\n", iface);
3737 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3740 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3742 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3744 TRACE("iface %p.\n", iface);
3746 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3749 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3751 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3753 TRACE("iface %p.\n", iface);
3755 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3758 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3760 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3762 TRACE("iface %p.\n", iface);
3764 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3767 /*****************************************************************************
3768 * IDirectDrawSurface7::Restore
3770 * Restores a lost surface. This makes the surface usable again, but
3771 * doesn't reload its old contents
3773 * Returns:
3774 * DD_OK on success, error code otherwise.
3776 *****************************************************************************/
3777 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3779 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3780 unsigned int i;
3782 TRACE("iface %p.\n", iface);
3784 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3786 struct wined3d_swapchain *swapchain = surface->ddraw->wined3d_swapchain;
3787 struct wined3d_sub_resource_desc wined3d_desc;
3788 struct wined3d_display_mode mode;
3789 HRESULT hr;
3791 if (FAILED(hr = wined3d_swapchain_get_display_mode(swapchain, &mode, NULL)))
3793 WARN("Failed to get display mode, hr %#x.\n", hr);
3794 return hr;
3797 if (FAILED(hr = wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, 0, &wined3d_desc)))
3799 WARN("Failed to get resource desc, hr %#x.\n", hr);
3800 return hr;
3803 if (mode.width != wined3d_desc.width || mode.height != wined3d_desc.height)
3805 WARN("Display mode dimensions %ux%u don't match surface dimensions %ux%u.\n",
3806 mode.width, mode.height, wined3d_desc.width, wined3d_desc.height);
3807 return DDERR_WRONGMODE;
3810 if (mode.format_id != wined3d_desc.format)
3812 WARN("Display mode format %#x doesn't match surface format %#x.\n",
3813 mode.format_id, wined3d_desc.format);
3814 return DDERR_WRONGMODE;
3818 if (!ddraw_surface_can_be_lost(surface))
3819 return DD_OK;
3820 ddraw_update_lost_surfaces(surface->ddraw);
3821 if (surface->ddraw->device_state == DDRAW_DEVICE_STATE_LOST)
3822 return DDERR_WRONGMODE;
3824 surface->is_lost = FALSE;
3825 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
3827 if (surface->complex_array[i])
3828 surface->complex_array[i]->is_lost = FALSE;
3831 return DD_OK;
3834 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3836 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3838 TRACE("iface %p.\n", iface);
3840 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3843 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3845 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3847 TRACE("iface %p.\n", iface);
3849 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3852 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3854 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3856 TRACE("iface %p.\n", iface);
3858 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3861 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3863 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3865 TRACE("iface %p.\n", iface);
3867 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3870 /*****************************************************************************
3871 * IDirectDrawSurface7::SetOverlayPosition
3873 * Changes the display coordinates of an overlay surface
3875 * Params:
3876 * X:
3877 * Y:
3879 * Returns:
3880 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3881 *****************************************************************************/
3882 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG x, LONG y)
3884 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3885 HRESULT hr;
3887 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3889 wined3d_mutex_lock();
3890 hr = wined3d_texture_set_overlay_position(surface->wined3d_texture,
3891 surface->sub_resource_idx, x, y);
3892 wined3d_mutex_unlock();
3894 return hr;
3897 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3899 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3901 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3903 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3906 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3908 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3910 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3912 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3915 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3917 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3919 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3921 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3924 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3926 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3928 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3930 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3933 /*****************************************************************************
3934 * IDirectDrawSurface7::UpdateOverlay
3936 * Modifies the attributes of an overlay surface.
3938 * Params:
3939 * SrcRect: The section of the source being used for the overlay
3940 * DstSurface: Address of the surface that is overlaid
3941 * DstRect: Place of the overlay
3942 * Flags: some DDOVER_* flags
3944 * Returns:
3945 * DDERR_UNSUPPORTED, because we don't support overlays
3947 *****************************************************************************/
3948 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *src_rect,
3949 IDirectDrawSurface7 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3951 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3952 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(dst_surface);
3953 struct wined3d_texture *dst_wined3d_texture = NULL;
3954 unsigned int dst_sub_resource_idx = 0;
3955 HRESULT hr;
3957 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3958 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3960 if (fx)
3961 FIXME("Ignoring fx %p.\n", fx);
3963 wined3d_mutex_lock();
3964 if (dst_impl)
3966 dst_wined3d_texture = dst_impl->wined3d_texture;
3967 dst_sub_resource_idx = dst_impl->sub_resource_idx;
3969 hr = wined3d_texture_update_overlay(src_impl->wined3d_texture, src_impl->sub_resource_idx,
3970 src_rect, dst_wined3d_texture, dst_sub_resource_idx, dst_rect, flags);
3971 wined3d_mutex_unlock();
3973 return hr_ddraw_from_wined3d(hr);
3976 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3977 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3979 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3980 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3982 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3983 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3985 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3986 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3989 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3990 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3992 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
3993 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3995 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3996 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3998 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3999 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4002 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
4003 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4005 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
4006 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
4008 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4009 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4011 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4012 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4015 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
4016 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4018 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
4019 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
4021 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4022 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4024 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4025 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4028 /*****************************************************************************
4029 * IDirectDrawSurface7::UpdateOverlayDisplay
4031 * The DX7 sdk says that it's not implemented
4033 * Params:
4034 * Flags: ?
4036 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
4038 *****************************************************************************/
4039 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
4041 TRACE("iface %p, flags %#x.\n", iface, Flags);
4043 return DDERR_UNSUPPORTED;
4046 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
4048 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4050 TRACE("iface %p, flags %#x.\n", iface, flags);
4052 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4055 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
4057 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4059 TRACE("iface %p, flags %#x.\n", iface, flags);
4061 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4064 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
4066 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4068 TRACE("iface %p, flags %#x.\n", iface, flags);
4070 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4073 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
4075 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4077 TRACE("iface %p, flags %#x.\n", iface, flags);
4079 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4082 /*****************************************************************************
4083 * IDirectDrawSurface7::UpdateOverlayZOrder
4085 * Sets an overlay's Z order
4087 * Params:
4088 * Flags: DDOVERZ_* flags
4089 * DDSRef: Defines the relative position in the overlay chain
4091 * Returns:
4092 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
4094 *****************************************************************************/
4095 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
4096 DWORD flags, IDirectDrawSurface7 *reference)
4098 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4100 FIXME("iface %p, flags %#x, reference %p stub!\n", iface, flags, reference);
4102 wined3d_mutex_lock();
4103 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OVERLAY))
4105 WARN("Not an overlay surface.\n");
4106 wined3d_mutex_unlock();
4107 return DDERR_NOTAOVERLAYSURFACE;
4109 wined3d_mutex_unlock();
4111 return DD_OK;
4114 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
4115 DWORD flags, IDirectDrawSurface4 *reference)
4117 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4118 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
4120 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4122 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4123 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4126 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
4127 DWORD flags, IDirectDrawSurface3 *reference)
4129 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4130 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
4132 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4134 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4135 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4138 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
4139 DWORD flags, IDirectDrawSurface2 *reference)
4141 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4142 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
4144 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4146 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4147 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4150 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
4151 DWORD flags, IDirectDrawSurface *reference)
4153 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4154 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
4156 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4158 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4159 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4162 /*****************************************************************************
4163 * IDirectDrawSurface7::GetDDInterface
4165 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
4166 * surface belongs to
4168 * Params:
4169 * DD: Address to write the interface pointer to
4171 * Returns:
4172 * DD_OK on success
4173 * DDERR_INVALIDPARAMS if DD is NULL
4175 *****************************************************************************/
4176 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
4178 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4180 TRACE("iface %p, ddraw %p.\n", iface, DD);
4182 if(!DD)
4183 return DDERR_INVALIDPARAMS;
4185 switch(This->version)
4187 case 7:
4188 *DD = &This->ddraw->IDirectDraw7_iface;
4189 break;
4191 case 4:
4192 *DD = &This->ddraw->IDirectDraw4_iface;
4193 break;
4195 case 2:
4196 *DD = &This->ddraw->IDirectDraw2_iface;
4197 break;
4199 case 1:
4200 *DD = &This->ddraw->IDirectDraw_iface;
4201 break;
4204 IUnknown_AddRef((IUnknown *)*DD);
4206 return DD_OK;
4209 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
4211 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4213 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4215 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4218 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
4220 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4222 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4224 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4227 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
4229 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4231 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4233 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4236 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
4238 TRACE("iface %p.\n", iface);
4240 return DD_OK;
4243 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
4245 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4247 TRACE("iface %p.\n", iface);
4249 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
4252 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
4254 TRACE("iface %p, value %p.\n", iface, pValue);
4256 *pValue = 0;
4258 return DD_OK;
4261 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
4263 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4265 TRACE("iface %p, value %p.\n", iface, pValue);
4267 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
4270 /*****************************************************************************
4271 * IDirectDrawSurface7::SetLOD
4273 * Sets the level of detail of a texture
4275 * Params:
4276 * MaxLOD: LOD to set
4278 * Returns:
4279 * DD_OK on success
4280 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4282 *****************************************************************************/
4283 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
4285 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4286 HRESULT hr;
4288 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
4290 wined3d_mutex_lock();
4291 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4293 wined3d_mutex_unlock();
4294 return DDERR_INVALIDOBJECT;
4297 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
4298 if (SUCCEEDED(hr) && surface->draw_texture)
4299 hr = wined3d_texture_set_lod(surface->draw_texture, MaxLOD);
4300 wined3d_mutex_unlock();
4302 return hr;
4305 /*****************************************************************************
4306 * IDirectDrawSurface7::GetLOD
4308 * Returns the level of detail of a Direct3D texture
4310 * Params:
4311 * MaxLOD: Address to write the LOD to
4313 * Returns:
4314 * DD_OK on success
4315 * DDERR_INVALIDPARAMS if MaxLOD is NULL
4316 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4318 *****************************************************************************/
4319 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4321 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4323 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4325 if(!MaxLOD)
4326 return DDERR_INVALIDPARAMS;
4328 wined3d_mutex_lock();
4329 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4331 wined3d_mutex_unlock();
4332 return DDERR_INVALIDOBJECT;
4335 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4336 wined3d_mutex_unlock();
4338 return DD_OK;
4341 /*****************************************************************************
4342 * IDirectDrawSurface7::BltFast
4344 * Performs a fast Blit.
4346 * Params:
4347 * dstx: The x coordinate to blit to on the destination
4348 * dsty: The y coordinate to blit to on the destination
4349 * Source: The source surface
4350 * rsrc: The source rectangle
4351 * trans: Type of transfer. Some DDBLTFAST_* flags
4353 * Returns:
4354 * DD_OK on success, error code otherwise.
4356 *****************************************************************************/
4357 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltFast(IDirectDrawSurface7 *iface,
4358 DWORD dst_x, DWORD dst_y, IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD trans)
4360 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
4361 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src_surface);
4362 DWORD flags = WINED3D_BLT_SYNCHRONOUS;
4363 DWORD src_w, src_h, dst_w, dst_h;
4364 HRESULT hr = DD_OK;
4365 RECT dst_rect, s;
4367 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4368 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), trans);
4370 dst_w = dst_impl->surface_desc.dwWidth;
4371 dst_h = dst_impl->surface_desc.dwHeight;
4373 if (!src_rect)
4375 SetRect(&s, 0, 0, src_impl->surface_desc.dwWidth, src_impl->surface_desc.dwHeight);
4376 src_rect = &s;
4379 src_w = src_rect->right - src_rect->left;
4380 src_h = src_rect->bottom - src_rect->top;
4381 if (src_w > dst_w || dst_x > dst_w - src_w
4382 || src_h > dst_h || dst_y > dst_h - src_h)
4384 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4385 return DDERR_INVALIDRECT;
4388 SetRect(&dst_rect, dst_x, dst_y, dst_x + src_w, dst_y + src_h);
4389 if (trans & DDBLTFAST_SRCCOLORKEY)
4390 flags |= WINED3D_BLT_SRC_CKEY;
4391 if (trans & DDBLTFAST_DESTCOLORKEY)
4392 flags |= WINED3D_BLT_DST_CKEY;
4393 if (trans & DDBLTFAST_WAIT)
4394 flags |= WINED3D_BLT_WAIT;
4395 if (trans & DDBLTFAST_DONOTWAIT)
4396 flags |= WINED3D_BLT_DO_NOT_WAIT;
4398 wined3d_mutex_lock();
4399 if (dst_impl->clipper)
4401 wined3d_mutex_unlock();
4402 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4403 return DDERR_BLTFASTCANTCLIP;
4406 if (src_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4407 hr = ddraw_surface_update_frontbuffer(src_impl, src_rect, TRUE, 0);
4408 if (SUCCEEDED(hr))
4409 hr = wined3d_texture_blt(ddraw_surface_get_any_texture(dst_impl, DDRAW_SURFACE_RW),
4410 dst_impl->sub_resource_idx, &dst_rect, ddraw_surface_get_any_texture(src_impl,DDRAW_SURFACE_READ),
4411 src_impl->sub_resource_idx, src_rect, flags, NULL, WINED3D_TEXF_POINT);
4412 if (SUCCEEDED(hr) && (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4413 hr = ddraw_surface_update_frontbuffer(dst_impl, &dst_rect, FALSE, 0);
4414 wined3d_mutex_unlock();
4416 switch(hr)
4418 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4419 default: return hr;
4423 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4424 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4426 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4427 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4429 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4430 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4432 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4433 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4436 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4437 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4439 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4440 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4442 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4443 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4445 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4446 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4449 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4450 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4452 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4453 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4455 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4456 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4458 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4459 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4462 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4463 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4465 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4466 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4468 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4469 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4471 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4472 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4475 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **clipper)
4477 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4479 TRACE("iface %p, clipper %p.\n", iface, clipper);
4481 if (!clipper)
4482 return DDERR_INVALIDPARAMS;
4484 wined3d_mutex_lock();
4485 if (!surface->clipper)
4487 wined3d_mutex_unlock();
4488 *clipper = NULL;
4489 return DDERR_NOCLIPPERATTACHED;
4492 *clipper = &surface->clipper->IDirectDrawClipper_iface;
4493 if (ddraw_clipper_is_valid(surface->clipper))
4494 IDirectDrawClipper_AddRef(*clipper);
4495 wined3d_mutex_unlock();
4497 return DD_OK;
4500 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4502 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4504 TRACE("iface %p, clipper %p.\n", iface, clipper);
4506 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4509 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4511 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4513 TRACE("iface %p, clipper %p.\n", iface, clipper);
4515 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4518 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4520 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4522 TRACE("iface %p, clipper %p.\n", iface, clipper);
4524 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4527 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4529 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4531 TRACE("iface %p, clipper %p.\n", iface, clipper);
4533 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4536 /*****************************************************************************
4537 * IDirectDrawSurface7::SetClipper
4539 * Sets a clipper for the surface
4541 * Params:
4542 * Clipper: IDirectDrawClipper interface of the clipper to set
4544 * Returns:
4545 * DD_OK on success
4547 *****************************************************************************/
4548 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4549 IDirectDrawClipper *iclipper)
4551 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4552 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4553 struct ddraw_clipper *old_clipper = This->clipper;
4554 HWND clipWindow;
4556 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4558 wined3d_mutex_lock();
4559 if (clipper == This->clipper)
4561 wined3d_mutex_unlock();
4562 return DD_OK;
4565 This->clipper = clipper;
4567 if (clipper != NULL)
4568 IDirectDrawClipper_AddRef(iclipper);
4569 if (old_clipper && ddraw_clipper_is_valid(old_clipper))
4570 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4572 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4574 clipWindow = NULL;
4575 if(clipper) {
4576 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4579 if (clipWindow)
4581 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4582 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4584 else
4586 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4587 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4591 wined3d_mutex_unlock();
4593 return DD_OK;
4596 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4598 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4600 TRACE("iface %p, clipper %p.\n", iface, clipper);
4602 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4605 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4607 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4609 TRACE("iface %p, clipper %p.\n", iface, clipper);
4611 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4614 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4616 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4618 TRACE("iface %p, clipper %p.\n", iface, clipper);
4620 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4623 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4625 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4627 TRACE("iface %p, clipper %p.\n", iface, clipper);
4629 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4632 /*****************************************************************************
4633 * IDirectDrawSurface7::SetSurfaceDesc
4635 * Sets the surface description. It can override the pixel format, the surface
4636 * memory, ...
4637 * It's not really tested.
4639 * Params:
4640 * DDSD: Pointer to the new surface description to set
4641 * Flags: Some flags
4643 * Returns:
4644 * DD_OK on success
4645 * DDERR_INVALIDPARAMS if DDSD is NULL
4647 *****************************************************************************/
4648 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4650 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4651 HRESULT hr;
4652 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4653 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4654 enum wined3d_format_id format_id;
4655 UINT pitch, width, height;
4657 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4659 if (!DDSD)
4661 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4662 return DDERR_INVALIDPARAMS;
4664 if (Flags)
4666 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4667 return DDERR_INVALIDPARAMS;
4669 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4670 || surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4671 || surface->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4673 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4674 return DDERR_INVALIDSURFACETYPE;
4677 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4678 * for PIXELFORMAT to work */
4679 if (DDSD->dwFlags & ~allowed_flags)
4681 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4682 return DDERR_INVALIDPARAMS;
4684 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4686 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4687 return DDERR_INVALIDPARAMS;
4689 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4691 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4692 return DDERR_INVALIDCAPS;
4694 if (DDSD->dwFlags & DDSD_WIDTH)
4696 if (!(DDSD->dwFlags & DDSD_PITCH))
4698 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4699 return DDERR_INVALIDPARAMS;
4701 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4703 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4704 DDSD->u1.lPitch, DDSD->dwWidth);
4705 return DDERR_INVALIDPARAMS;
4707 if (DDSD->dwWidth != surface->surface_desc.dwWidth)
4708 TRACE("Surface width changed from %u to %u.\n", surface->surface_desc.dwWidth, DDSD->dwWidth);
4709 if (DDSD->u1.lPitch != surface->surface_desc.u1.lPitch)
4710 TRACE("Surface pitch changed from %u to %u.\n", surface->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4711 pitch = DDSD->u1.lPitch;
4712 width = DDSD->dwWidth;
4714 else if (DDSD->dwFlags & DDSD_PITCH)
4716 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4717 return DDERR_INVALIDPARAMS;
4719 else
4721 pitch = surface->surface_desc.u1.lPitch;
4722 width = surface->surface_desc.dwWidth;
4725 if (DDSD->dwFlags & DDSD_HEIGHT)
4727 if (!DDSD->dwHeight)
4729 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4730 return DDERR_INVALIDPARAMS;
4732 if (DDSD->dwHeight != surface->surface_desc.dwHeight)
4733 TRACE("Surface height changed from %u to %u.\n", surface->surface_desc.dwHeight, DDSD->dwHeight);
4734 height = DDSD->dwHeight;
4736 else
4738 height = surface->surface_desc.dwHeight;
4741 wined3d_mutex_lock();
4742 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4744 enum wined3d_format_id current_format_id;
4745 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4747 if (format_id == WINED3DFMT_UNKNOWN)
4749 ERR("Requested to set an unknown pixelformat\n");
4750 wined3d_mutex_unlock();
4751 return DDERR_INVALIDPARAMS;
4753 current_format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4754 if (format_id != current_format_id)
4755 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4757 else
4759 format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4762 if (FAILED(hr = wined3d_texture_update_desc(surface->wined3d_texture, surface->sub_resource_idx,
4763 width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4765 WARN("Failed to update surface desc, hr %#x.\n", hr);
4766 wined3d_mutex_unlock();
4767 return hr_ddraw_from_wined3d(hr);
4770 if (surface->draw_texture && FAILED(hr = wined3d_texture_update_desc(surface->draw_texture,
4771 surface->sub_resource_idx, width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, NULL, 0)))
4773 ERR("Failed to update surface desc for draw_texture, hr %#x.\n", hr);
4774 wined3d_mutex_unlock();
4775 return hr_ddraw_from_wined3d(hr);
4778 if (DDSD->dwFlags & DDSD_WIDTH)
4779 surface->surface_desc.dwWidth = width;
4780 if (DDSD->dwFlags & DDSD_PITCH)
4781 surface->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4782 if (DDSD->dwFlags & DDSD_HEIGHT)
4783 surface->surface_desc.dwHeight = height;
4784 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4785 surface->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4787 wined3d_mutex_unlock();
4789 return DD_OK;
4792 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4793 DDSURFACEDESC2 *surface_desc, DWORD flags)
4795 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4797 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4799 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4800 surface_desc, flags);
4803 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4804 DDSURFACEDESC *surface_desc, DWORD flags)
4806 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4807 DDSURFACEDESC2 surface_desc2;
4809 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4811 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4812 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4813 surface_desc ? &surface_desc2 : NULL, flags);
4816 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4818 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4819 struct ddraw_palette *palette_impl;
4820 HRESULT hr = DD_OK;
4822 TRACE("iface %p, palette %p.\n", iface, palette);
4824 if (!palette)
4825 return DDERR_INVALIDPARAMS;
4826 if (ddraw_surface_is_lost(surface))
4828 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4829 return DDERR_SURFACELOST;
4832 wined3d_mutex_lock();
4833 if ((palette_impl = surface->palette))
4835 *palette = &palette_impl->IDirectDrawPalette_iface;
4836 IDirectDrawPalette_AddRef(*palette);
4838 else
4840 *palette = NULL;
4841 hr = DDERR_NOPALETTEATTACHED;
4843 wined3d_mutex_unlock();
4845 return hr;
4848 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4850 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4852 TRACE("iface %p, palette %p.\n", iface, palette);
4854 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4857 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4859 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4861 TRACE("iface %p, palette %p.\n", iface, palette);
4863 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4866 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4868 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4870 TRACE("iface %p, palette %p.\n", iface, palette);
4872 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4875 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4877 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4879 TRACE("iface %p, palette %p.\n", iface, palette);
4881 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4884 static HRESULT ddraw_surface_set_wined3d_textures_colour_key(struct ddraw_surface *surface, DWORD flags,
4885 struct wined3d_color_key *color_key)
4887 HRESULT hr;
4889 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags, color_key);
4890 if (surface->draw_texture && SUCCEEDED(hr))
4891 hr = wined3d_texture_set_color_key(surface->draw_texture, flags, color_key);
4893 return hr;
4896 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4898 DDCOLORKEY fixed_color_key;
4899 HRESULT hr = WINED3D_OK;
4901 if (flags & DDCKEY_COLORSPACE)
4903 if (color_key && color_key->dwColorSpaceLowValue != color_key->dwColorSpaceHighValue)
4905 WARN("Range color keys are not supported, returning DDERR_NOCOLORKEYHW.\n");
4906 return DDERR_NOCOLORKEYHW;
4908 flags &= ~DDCKEY_COLORSPACE;
4911 wined3d_mutex_lock();
4913 if (color_key)
4915 fixed_color_key.dwColorSpaceLowValue = fixed_color_key.dwColorSpaceHighValue = color_key->dwColorSpaceLowValue;
4916 switch (flags & ~DDCKEY_COLORSPACE)
4918 case DDCKEY_DESTBLT:
4919 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4920 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4921 break;
4923 case DDCKEY_DESTOVERLAY:
4924 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4925 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4926 break;
4928 case DDCKEY_SRCOVERLAY:
4929 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4930 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4931 break;
4933 case DDCKEY_SRCBLT:
4934 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4935 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4936 break;
4938 default:
4939 wined3d_mutex_unlock();
4940 return DDERR_INVALIDPARAMS;
4943 else
4945 switch (flags & ~DDCKEY_COLORSPACE)
4947 case DDCKEY_DESTBLT:
4948 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4949 break;
4951 case DDCKEY_DESTOVERLAY:
4952 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4953 break;
4955 case DDCKEY_SRCOVERLAY:
4956 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4957 break;
4959 case DDCKEY_SRCBLT:
4960 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4961 break;
4963 default:
4964 wined3d_mutex_unlock();
4965 return DDERR_INVALIDPARAMS;
4969 if (surface->is_complex_root)
4970 hr = ddraw_surface_set_wined3d_textures_colour_key(surface, flags,
4971 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4973 wined3d_mutex_unlock();
4975 return hr_ddraw_from_wined3d(hr);
4978 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4980 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4982 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4984 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4985 return DDERR_NOTONMIPMAPSUBLEVEL;
4987 return ddraw_surface_set_color_key(surface, flags, color_key);
4990 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4992 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4994 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4996 return ddraw_surface_set_color_key(surface, flags, color_key);
4999 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
5001 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
5003 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5005 return ddraw_surface_set_color_key(surface, flags, color_key);
5008 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
5010 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
5012 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5014 return ddraw_surface_set_color_key(surface, flags, color_key);
5017 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
5019 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5021 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5023 return ddraw_surface_set_color_key(surface, flags, color_key);
5026 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
5028 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
5030 TRACE("iface %p, palette %p.\n", iface, palette);
5032 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
5033 return DDERR_NOTONMIPMAPSUBLEVEL;
5034 if (ddraw_surface_is_lost(surface))
5036 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5037 return DDERR_SURFACELOST;
5040 return ddraw_surface_set_palette(surface, palette);
5043 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
5045 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
5047 TRACE("iface %p, palette %p.\n", iface, palette);
5049 if (ddraw_surface_is_lost(surface))
5051 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5052 return DDERR_SURFACELOST;
5055 return ddraw_surface_set_palette(surface, palette);
5058 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
5060 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
5062 TRACE("iface %p, palette %p.\n", iface, palette);
5064 if (ddraw_surface_is_lost(surface))
5066 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5067 return DDERR_SURFACELOST;
5070 return ddraw_surface_set_palette(surface, palette);
5073 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
5075 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
5077 TRACE("iface %p, palette %p.\n", iface, palette);
5079 if (ddraw_surface_is_lost(surface))
5081 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5082 return DDERR_SURFACELOST;
5085 return ddraw_surface_set_palette(surface, palette);
5088 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
5090 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5092 TRACE("iface %p, palette %p.\n", iface, palette);
5094 if (ddraw_surface_is_lost(surface))
5096 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5097 return DDERR_SURFACELOST;
5100 return ddraw_surface_set_palette(surface, palette);
5103 /**********************************************************
5104 * IDirectDrawGammaControl::GetGammaRamp
5106 * Returns the current gamma ramp for a surface
5108 * Params:
5109 * flags: Ignored
5110 * gamma_ramp: Address to write the ramp to
5112 * Returns:
5113 * DD_OK on success
5114 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5116 **********************************************************/
5117 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
5118 DWORD flags, DDGAMMARAMP *gamma_ramp)
5120 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5122 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5124 if (!gamma_ramp)
5126 WARN("Invalid gamma_ramp passed.\n");
5127 return DDERR_INVALIDPARAMS;
5130 wined3d_mutex_lock();
5131 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5133 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5134 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
5136 else
5138 ERR("Not implemented for non-primary surfaces.\n");
5140 wined3d_mutex_unlock();
5142 return DD_OK;
5145 /**********************************************************
5146 * IDirectDrawGammaControl::SetGammaRamp
5148 * Sets the red, green and blue gamma ramps for
5150 * Params:
5151 * flags: Can be DDSGR_CALIBRATE to request calibration
5152 * gamma_ramp: Structure containing the new gamma ramp
5154 * Returns:
5155 * DD_OK on success
5156 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5158 **********************************************************/
5159 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
5160 DWORD flags, DDGAMMARAMP *gamma_ramp)
5162 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5164 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5166 if (!gamma_ramp)
5168 WARN("Invalid gamma_ramp passed.\n");
5169 return DDERR_INVALIDPARAMS;
5172 wined3d_mutex_lock();
5173 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5175 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5176 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
5177 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
5179 else
5181 ERR("Not implemented for non-primary surfaces.\n");
5183 wined3d_mutex_unlock();
5185 return DD_OK;
5188 /*****************************************************************************
5189 * IDirect3DTexture2::PaletteChanged
5191 * Informs the texture about a palette change
5193 * Params:
5194 * start: Start index of the change
5195 * count: The number of changed entries
5197 * Returns
5198 * D3D_OK, because it's a stub
5200 *****************************************************************************/
5201 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
5203 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
5205 return D3D_OK;
5208 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
5210 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5212 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
5214 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
5217 /*****************************************************************************
5218 * IDirect3DTexture::Unload
5220 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
5223 * Returns:
5224 * DDERR_UNSUPPORTED
5226 *****************************************************************************/
5227 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
5229 WARN("iface %p. Not implemented.\n", iface);
5231 return DDERR_UNSUPPORTED;
5234 /*****************************************************************************
5235 * IDirect3DTexture2::GetHandle
5237 * Returns handle for the texture.
5239 * Params:
5240 * device: Device this handle is assigned to
5241 * handle: Address to store the handle at.
5243 * Returns:
5244 * D3D_OK
5246 *****************************************************************************/
5247 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
5248 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
5250 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
5251 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
5253 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5255 wined3d_mutex_lock();
5257 if (!surface->Handle)
5259 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
5260 if (h == DDRAW_INVALID_HANDLE)
5262 ERR("Failed to allocate a texture handle.\n");
5263 wined3d_mutex_unlock();
5264 return DDERR_OUTOFMEMORY;
5267 surface->Handle = h + 1;
5270 TRACE("Returning handle %08x.\n", surface->Handle);
5271 *handle = surface->Handle;
5273 wined3d_mutex_unlock();
5275 return D3D_OK;
5278 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
5279 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
5281 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5282 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
5284 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5286 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
5287 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
5290 /*****************************************************************************
5291 * get_sub_mimaplevel
5293 * Helper function that returns the next mipmap level
5295 * tex_ptr: Surface of which to return the next level
5297 *****************************************************************************/
5298 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
5300 /* Now go down the mipmap chain to the next surface */
5301 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, {0} };
5302 IDirectDrawSurface7 *next_level;
5303 HRESULT hr;
5305 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
5306 if (FAILED(hr)) return NULL;
5308 ddraw_surface7_Release(next_level);
5310 return impl_from_IDirectDrawSurface7(next_level);
5313 /*****************************************************************************
5314 * IDirect3DTexture2::Load
5316 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5318 * This function isn't relayed to WineD3D because the whole interface is
5319 * implemented in DDraw only. For speed improvements an implementation which
5320 * takes OpenGL more into account could be placed into WineD3D.
5322 * Params:
5323 * src_texture: Address of the texture to load
5325 * Returns:
5326 * D3D_OK on success
5327 * D3DERR_TEXTURE_LOAD_FAILED.
5329 *****************************************************************************/
5330 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5332 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5333 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5334 struct wined3d_resource *dst_resource, *src_resource;
5335 HRESULT hr;
5337 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5339 if (src_surface == dst_surface)
5341 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5342 return D3D_OK;
5345 wined3d_mutex_lock();
5347 dst_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(dst_surface, DDRAW_SURFACE_WRITE));
5348 src_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(src_surface, DDRAW_SURFACE_READ));
5350 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5351 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5352 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5354 ERR("Trying to load surfaces with different mip-map counts.\n");
5357 for (;;)
5359 struct ddraw_palette *dst_pal, *src_pal;
5360 DDSURFACEDESC *src_desc, *dst_desc;
5362 TRACE("Copying surface %p to surface %p.\n", src_surface, dst_surface);
5364 /* Suppress the ALLOCONLOAD flag */
5365 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5367 /* Get the palettes */
5368 dst_pal = dst_surface->palette;
5369 src_pal = src_surface->palette;
5371 if (src_pal)
5373 PALETTEENTRY palent[256];
5375 if (!dst_pal)
5377 wined3d_mutex_unlock();
5378 return DDERR_NOPALETTEATTACHED;
5380 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5381 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5384 /* Copy one surface on the other */
5385 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5386 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5388 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5390 /* Should also check for same pixel format, u1.lPitch, ... */
5391 ERR("Error in surface sizes.\n");
5392 wined3d_mutex_unlock();
5393 return D3DERR_TEXTURE_LOAD_FAILED;
5395 else
5397 struct wined3d_map_desc src_map_desc, dst_map_desc;
5399 /* Copy the src blit color key if the source has one, don't erase
5400 * the destination's ckey if the source has none */
5401 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5403 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5404 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5407 if (FAILED(hr = wined3d_resource_map(src_resource,
5408 src_surface->sub_resource_idx, &src_map_desc, NULL, WINED3D_MAP_READ)))
5410 ERR("Failed to lock source surface, hr %#x.\n", hr);
5411 wined3d_mutex_unlock();
5412 return D3DERR_TEXTURE_LOAD_FAILED;
5415 if (FAILED(hr = wined3d_resource_map(dst_resource,
5416 dst_surface->sub_resource_idx, &dst_map_desc, NULL, WINED3D_MAP_WRITE)))
5418 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5419 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5420 wined3d_mutex_unlock();
5421 return D3DERR_TEXTURE_LOAD_FAILED;
5424 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5425 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5426 else
5427 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5429 wined3d_resource_unmap(dst_resource, dst_surface->sub_resource_idx);
5430 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5433 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5434 src_surface = get_sub_mimaplevel(src_surface);
5435 else
5436 src_surface = NULL;
5438 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5439 dst_surface = get_sub_mimaplevel(dst_surface);
5440 else
5441 dst_surface = NULL;
5443 if (!src_surface || !dst_surface)
5445 if (src_surface != dst_surface)
5446 ERR("Loading surface with different mipmap structure.\n");
5447 break;
5451 wined3d_mutex_unlock();
5453 return hr;
5456 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5458 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5459 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5461 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5463 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5464 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5467 /*****************************************************************************
5468 * The VTable
5469 *****************************************************************************/
5471 /* Some windowed mode wrappers expect this vtbl to be writable. */
5472 static struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5474 /* IUnknown */
5475 ddraw_surface7_QueryInterface,
5476 ddraw_surface7_AddRef,
5477 ddraw_surface7_Release,
5478 /* IDirectDrawSurface */
5479 ddraw_surface7_AddAttachedSurface,
5480 ddraw_surface7_AddOverlayDirtyRect,
5481 ddraw_surface7_Blt,
5482 ddraw_surface7_BltBatch,
5483 ddraw_surface7_BltFast,
5484 ddraw_surface7_DeleteAttachedSurface,
5485 ddraw_surface7_EnumAttachedSurfaces,
5486 ddraw_surface7_EnumOverlayZOrders,
5487 ddraw_surface7_Flip,
5488 ddraw_surface7_GetAttachedSurface,
5489 ddraw_surface7_GetBltStatus,
5490 ddraw_surface7_GetCaps,
5491 ddraw_surface7_GetClipper,
5492 ddraw_surface7_GetColorKey,
5493 ddraw_surface7_GetDC,
5494 ddraw_surface7_GetFlipStatus,
5495 ddraw_surface7_GetOverlayPosition,
5496 ddraw_surface7_GetPalette,
5497 ddraw_surface7_GetPixelFormat,
5498 ddraw_surface7_GetSurfaceDesc,
5499 ddraw_surface7_Initialize,
5500 ddraw_surface7_IsLost,
5501 ddraw_surface7_Lock,
5502 ddraw_surface7_ReleaseDC,
5503 ddraw_surface7_Restore,
5504 ddraw_surface7_SetClipper,
5505 ddraw_surface7_SetColorKey,
5506 ddraw_surface7_SetOverlayPosition,
5507 ddraw_surface7_SetPalette,
5508 ddraw_surface7_Unlock,
5509 ddraw_surface7_UpdateOverlay,
5510 ddraw_surface7_UpdateOverlayDisplay,
5511 ddraw_surface7_UpdateOverlayZOrder,
5512 /* IDirectDrawSurface2 */
5513 ddraw_surface7_GetDDInterface,
5514 ddraw_surface7_PageLock,
5515 ddraw_surface7_PageUnlock,
5516 /* IDirectDrawSurface3 */
5517 ddraw_surface7_SetSurfaceDesc,
5518 /* IDirectDrawSurface4 */
5519 ddraw_surface7_SetPrivateData,
5520 ddraw_surface7_GetPrivateData,
5521 ddraw_surface7_FreePrivateData,
5522 ddraw_surface7_GetUniquenessValue,
5523 ddraw_surface7_ChangeUniquenessValue,
5524 /* IDirectDrawSurface7 */
5525 ddraw_surface7_SetPriority,
5526 ddraw_surface7_GetPriority,
5527 ddraw_surface7_SetLOD,
5528 ddraw_surface7_GetLOD,
5531 /* Some windowed mode wrappers expect this vtbl to be writable. */
5532 static struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5534 /* IUnknown */
5535 ddraw_surface4_QueryInterface,
5536 ddraw_surface4_AddRef,
5537 ddraw_surface4_Release,
5538 /* IDirectDrawSurface */
5539 ddraw_surface4_AddAttachedSurface,
5540 ddraw_surface4_AddOverlayDirtyRect,
5541 ddraw_surface4_Blt,
5542 ddraw_surface4_BltBatch,
5543 ddraw_surface4_BltFast,
5544 ddraw_surface4_DeleteAttachedSurface,
5545 ddraw_surface4_EnumAttachedSurfaces,
5546 ddraw_surface4_EnumOverlayZOrders,
5547 ddraw_surface4_Flip,
5548 ddraw_surface4_GetAttachedSurface,
5549 ddraw_surface4_GetBltStatus,
5550 ddraw_surface4_GetCaps,
5551 ddraw_surface4_GetClipper,
5552 ddraw_surface4_GetColorKey,
5553 ddraw_surface4_GetDC,
5554 ddraw_surface4_GetFlipStatus,
5555 ddraw_surface4_GetOverlayPosition,
5556 ddraw_surface4_GetPalette,
5557 ddraw_surface4_GetPixelFormat,
5558 ddraw_surface4_GetSurfaceDesc,
5559 ddraw_surface4_Initialize,
5560 ddraw_surface4_IsLost,
5561 ddraw_surface4_Lock,
5562 ddraw_surface4_ReleaseDC,
5563 ddraw_surface4_Restore,
5564 ddraw_surface4_SetClipper,
5565 ddraw_surface4_SetColorKey,
5566 ddraw_surface4_SetOverlayPosition,
5567 ddraw_surface4_SetPalette,
5568 ddraw_surface4_Unlock,
5569 ddraw_surface4_UpdateOverlay,
5570 ddraw_surface4_UpdateOverlayDisplay,
5571 ddraw_surface4_UpdateOverlayZOrder,
5572 /* IDirectDrawSurface2 */
5573 ddraw_surface4_GetDDInterface,
5574 ddraw_surface4_PageLock,
5575 ddraw_surface4_PageUnlock,
5576 /* IDirectDrawSurface3 */
5577 ddraw_surface4_SetSurfaceDesc,
5578 /* IDirectDrawSurface4 */
5579 ddraw_surface4_SetPrivateData,
5580 ddraw_surface4_GetPrivateData,
5581 ddraw_surface4_FreePrivateData,
5582 ddraw_surface4_GetUniquenessValue,
5583 ddraw_surface4_ChangeUniquenessValue,
5586 /* Some windowed mode wrappers expect this vtbl to be writable. */
5587 static struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5589 /* IUnknown */
5590 ddraw_surface3_QueryInterface,
5591 ddraw_surface3_AddRef,
5592 ddraw_surface3_Release,
5593 /* IDirectDrawSurface */
5594 ddraw_surface3_AddAttachedSurface,
5595 ddraw_surface3_AddOverlayDirtyRect,
5596 ddraw_surface3_Blt,
5597 ddraw_surface3_BltBatch,
5598 ddraw_surface3_BltFast,
5599 ddraw_surface3_DeleteAttachedSurface,
5600 ddraw_surface3_EnumAttachedSurfaces,
5601 ddraw_surface3_EnumOverlayZOrders,
5602 ddraw_surface3_Flip,
5603 ddraw_surface3_GetAttachedSurface,
5604 ddraw_surface3_GetBltStatus,
5605 ddraw_surface3_GetCaps,
5606 ddraw_surface3_GetClipper,
5607 ddraw_surface3_GetColorKey,
5608 ddraw_surface3_GetDC,
5609 ddraw_surface3_GetFlipStatus,
5610 ddraw_surface3_GetOverlayPosition,
5611 ddraw_surface3_GetPalette,
5612 ddraw_surface3_GetPixelFormat,
5613 ddraw_surface3_GetSurfaceDesc,
5614 ddraw_surface3_Initialize,
5615 ddraw_surface3_IsLost,
5616 ddraw_surface3_Lock,
5617 ddraw_surface3_ReleaseDC,
5618 ddraw_surface3_Restore,
5619 ddraw_surface3_SetClipper,
5620 ddraw_surface3_SetColorKey,
5621 ddraw_surface3_SetOverlayPosition,
5622 ddraw_surface3_SetPalette,
5623 ddraw_surface3_Unlock,
5624 ddraw_surface3_UpdateOverlay,
5625 ddraw_surface3_UpdateOverlayDisplay,
5626 ddraw_surface3_UpdateOverlayZOrder,
5627 /* IDirectDrawSurface2 */
5628 ddraw_surface3_GetDDInterface,
5629 ddraw_surface3_PageLock,
5630 ddraw_surface3_PageUnlock,
5631 /* IDirectDrawSurface3 */
5632 ddraw_surface3_SetSurfaceDesc,
5635 /* Some windowed mode wrappers expect this vtbl to be writable. */
5636 static struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5638 /* IUnknown */
5639 ddraw_surface2_QueryInterface,
5640 ddraw_surface2_AddRef,
5641 ddraw_surface2_Release,
5642 /* IDirectDrawSurface */
5643 ddraw_surface2_AddAttachedSurface,
5644 ddraw_surface2_AddOverlayDirtyRect,
5645 ddraw_surface2_Blt,
5646 ddraw_surface2_BltBatch,
5647 ddraw_surface2_BltFast,
5648 ddraw_surface2_DeleteAttachedSurface,
5649 ddraw_surface2_EnumAttachedSurfaces,
5650 ddraw_surface2_EnumOverlayZOrders,
5651 ddraw_surface2_Flip,
5652 ddraw_surface2_GetAttachedSurface,
5653 ddraw_surface2_GetBltStatus,
5654 ddraw_surface2_GetCaps,
5655 ddraw_surface2_GetClipper,
5656 ddraw_surface2_GetColorKey,
5657 ddraw_surface2_GetDC,
5658 ddraw_surface2_GetFlipStatus,
5659 ddraw_surface2_GetOverlayPosition,
5660 ddraw_surface2_GetPalette,
5661 ddraw_surface2_GetPixelFormat,
5662 ddraw_surface2_GetSurfaceDesc,
5663 ddraw_surface2_Initialize,
5664 ddraw_surface2_IsLost,
5665 ddraw_surface2_Lock,
5666 ddraw_surface2_ReleaseDC,
5667 ddraw_surface2_Restore,
5668 ddraw_surface2_SetClipper,
5669 ddraw_surface2_SetColorKey,
5670 ddraw_surface2_SetOverlayPosition,
5671 ddraw_surface2_SetPalette,
5672 ddraw_surface2_Unlock,
5673 ddraw_surface2_UpdateOverlay,
5674 ddraw_surface2_UpdateOverlayDisplay,
5675 ddraw_surface2_UpdateOverlayZOrder,
5676 /* IDirectDrawSurface2 */
5677 ddraw_surface2_GetDDInterface,
5678 ddraw_surface2_PageLock,
5679 ddraw_surface2_PageUnlock,
5682 /* Bad Mojo Redux expects this vtbl to be writable. */
5683 static struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5685 /* IUnknown */
5686 ddraw_surface1_QueryInterface,
5687 ddraw_surface1_AddRef,
5688 ddraw_surface1_Release,
5689 /* IDirectDrawSurface */
5690 ddraw_surface1_AddAttachedSurface,
5691 ddraw_surface1_AddOverlayDirtyRect,
5692 ddraw_surface1_Blt,
5693 ddraw_surface1_BltBatch,
5694 ddraw_surface1_BltFast,
5695 ddraw_surface1_DeleteAttachedSurface,
5696 ddraw_surface1_EnumAttachedSurfaces,
5697 ddraw_surface1_EnumOverlayZOrders,
5698 ddraw_surface1_Flip,
5699 ddraw_surface1_GetAttachedSurface,
5700 ddraw_surface1_GetBltStatus,
5701 ddraw_surface1_GetCaps,
5702 ddraw_surface1_GetClipper,
5703 ddraw_surface1_GetColorKey,
5704 ddraw_surface1_GetDC,
5705 ddraw_surface1_GetFlipStatus,
5706 ddraw_surface1_GetOverlayPosition,
5707 ddraw_surface1_GetPalette,
5708 ddraw_surface1_GetPixelFormat,
5709 ddraw_surface1_GetSurfaceDesc,
5710 ddraw_surface1_Initialize,
5711 ddraw_surface1_IsLost,
5712 ddraw_surface1_Lock,
5713 ddraw_surface1_ReleaseDC,
5714 ddraw_surface1_Restore,
5715 ddraw_surface1_SetClipper,
5716 ddraw_surface1_SetColorKey,
5717 ddraw_surface1_SetOverlayPosition,
5718 ddraw_surface1_SetPalette,
5719 ddraw_surface1_Unlock,
5720 ddraw_surface1_UpdateOverlay,
5721 ddraw_surface1_UpdateOverlayDisplay,
5722 ddraw_surface1_UpdateOverlayZOrder,
5725 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5727 ddraw_gamma_control_QueryInterface,
5728 ddraw_gamma_control_AddRef,
5729 ddraw_gamma_control_Release,
5730 ddraw_gamma_control_GetGammaRamp,
5731 ddraw_gamma_control_SetGammaRamp,
5734 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5736 d3d_texture2_QueryInterface,
5737 d3d_texture2_AddRef,
5738 d3d_texture2_Release,
5739 d3d_texture2_GetHandle,
5740 d3d_texture2_PaletteChanged,
5741 d3d_texture2_Load,
5744 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5746 d3d_texture1_QueryInterface,
5747 d3d_texture1_AddRef,
5748 d3d_texture1_Release,
5749 d3d_texture1_Initialize,
5750 d3d_texture1_GetHandle,
5751 d3d_texture1_PaletteChanged,
5752 d3d_texture1_Load,
5753 d3d_texture1_Unload,
5756 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5757 * IDirectDrawSurface interface to ddraw methods. */
5758 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5760 if (!iface) return NULL;
5761 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5763 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5764 if (FAILED(hr))
5766 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5767 return NULL;
5769 IDirectDrawSurface7_Release(iface);
5771 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5774 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5776 if (!iface) return NULL;
5777 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5779 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5780 if (FAILED(hr))
5782 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5783 return NULL;
5785 IDirectDrawSurface4_Release(iface);
5787 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5790 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5792 if (!iface) return NULL;
5793 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5795 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5796 if (FAILED(hr))
5798 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5799 return NULL;
5801 IDirectDrawSurface3_Release(iface);
5803 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5806 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5808 if (!iface) return NULL;
5809 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5811 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5812 if (FAILED(hr))
5814 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5815 return NULL;
5817 IDirectDrawSurface2_Release(iface);
5819 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5822 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5824 if (!iface) return NULL;
5825 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5827 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5828 if (FAILED(hr))
5830 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5831 return NULL;
5833 IDirectDrawSurface_Release(iface);
5835 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5838 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5840 if (!iface) return NULL;
5841 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5842 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5845 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5847 if (!iface) return NULL;
5848 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5849 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5852 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5854 struct ddraw_surface *surface = parent;
5856 TRACE("surface %p.\n", surface);
5858 /* This shouldn't happen, ddraw_surface_release_iface() should prevent the
5859 * surface from being destroyed in this case. */
5860 if (surface->first_attached != surface)
5861 ERR("Surface is still attached to surface %p.\n", surface->first_attached);
5863 while (surface->next_attached)
5864 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5865 surface->next_attached, surface->next_attached->attached_iface)))
5866 ERR("DeleteAttachedSurface failed.\n");
5868 /* Having a texture handle set implies that the device still exists. */
5869 if (surface->Handle)
5870 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5872 /* Reduce the ddraw surface count. */
5873 list_remove(&surface->surface_list_entry);
5875 if (surface->clipper && ddraw_clipper_is_valid(surface->clipper))
5876 IDirectDrawClipper_Release(&surface->clipper->IDirectDrawClipper_iface);
5878 if (surface == surface->ddraw->primary)
5880 surface->ddraw->primary = NULL;
5881 surface->ddraw->gdi_surface = NULL;
5884 wined3d_private_store_cleanup(&surface->private_store);
5886 if (surface->draw_texture)
5887 wined3d_texture_decref(surface->wined3d_texture);
5889 heap_free(surface);
5892 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5894 ddraw_surface_wined3d_object_destroyed,
5897 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5899 struct ddraw_texture *texture = parent;
5901 TRACE("texture %p, texture_memory %p.\n", texture, texture->texture_memory);
5903 heap_free(texture->texture_memory);
5904 heap_free(parent);
5907 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5909 ddraw_texture_wined3d_object_destroyed,
5912 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5914 return DD_OK;
5917 static HRESULT ddraw_surface_reserve_memory(struct wined3d_texture *wined3d_texture)
5919 static const unsigned int extra_size = 0x10000;
5921 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
5922 struct wined3d_resource_desc resource_desc;
5923 struct wined3d_sub_resource_desc desc;
5924 unsigned int pitch, slice_pitch;
5925 unsigned int sub_resource_idx;
5926 HRESULT hr = WINED3D_OK;
5927 unsigned int offset;
5929 wined3d_resource_get_desc(wined3d_texture_get_resource(wined3d_texture), &resource_desc);
5930 if (!(texture->texture_memory = heap_alloc_zero(resource_desc.size + extra_size)))
5932 ERR("Out of memory.\n");
5933 return E_OUTOFMEMORY;
5935 TRACE("texture->texture_memory %p.\n", texture->texture_memory);
5937 offset = 0;
5938 sub_resource_idx = 0;
5939 while (wined3d_texture_get_sub_resource_desc(wined3d_texture, sub_resource_idx, &desc)
5940 == WINED3D_OK)
5942 wined3d_texture_get_pitch(wined3d_texture, sub_resource_idx, &pitch, &slice_pitch);
5944 if (FAILED(hr = wined3d_texture_update_desc(wined3d_texture, sub_resource_idx,
5945 desc.width, desc.height, resource_desc.format,
5946 desc.multisample_type, desc.multisample_quality,
5947 (BYTE *)texture->texture_memory + offset, pitch)))
5949 heap_free(texture->texture_memory);
5950 texture->texture_memory = NULL;
5951 break;
5953 ++sub_resource_idx;
5954 offset += desc.size;
5956 return hr;
5959 static HRESULT ddraw_surface_create_wined3d_texture(DDSURFACEDESC2 *desc, struct wined3d_device *wined3d_device,
5960 const struct wined3d_resource_desc *wined3d_desc, unsigned int layers, unsigned int levels,
5961 struct ddraw_texture *texture, struct wined3d_texture **wined3d_texture)
5963 struct wined3d_resource_desc draw_texture_desc;
5964 struct wined3d_texture *draw_texture;
5965 struct ddraw_surface *parent;
5966 unsigned int bind_flags;
5967 unsigned int i;
5968 HRESULT hr;
5970 bind_flags = 0;
5971 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5972 || (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5973 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
5975 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5976 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
5977 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5978 bind_flags |= WINED3D_BIND_RENDER_TARGET;
5980 if (!bind_flags || (wined3d_desc->access & WINED3D_RESOURCE_ACCESS_GPU && !(bind_flags & ~wined3d_desc->bind_flags)))
5981 goto no_draw_texture;
5983 draw_texture_desc = *wined3d_desc;
5984 draw_texture_desc.bind_flags = bind_flags;
5985 draw_texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5987 if (FAILED(hr = wined3d_texture_create(wined3d_device, &draw_texture_desc, layers,
5988 levels, 0, NULL, texture, &ddraw_texture_wined3d_parent_ops, &draw_texture)))
5990 WARN("Failed to create draw texture, hr %#x.\n", hr);
5991 goto no_draw_texture;
5993 wined3d_texture_decref(draw_texture);
5995 /* Some applications assume surfaces will always be mapped at the same
5996 * address. Some of those also assume that this address is valid even when
5997 * the surface isn't mapped, and that updates done this way will be
5998 * visible on the screen. The game Nox is such an application,
5999 * Commandos: Behind Enemy Lines is another. Setting
6000 * WINED3D_TEXTURE_CREATE_GET_DC_LENIENT will ensure this. */
6001 if (FAILED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6002 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, NULL, &ddraw_null_wined3d_parent_ops,
6003 wined3d_texture)))
6005 parent = wined3d_texture_get_sub_resource_parent(draw_texture, 0);
6006 if (texture->version == 7)
6007 IDirectDrawSurface7_Release(&parent->IDirectDrawSurface7_iface);
6008 else if (texture->version == 4)
6009 IDirectDrawSurface4_Release(&parent->IDirectDrawSurface4_iface);
6010 else
6011 IDirectDrawSurface_Release(&parent->IDirectDrawSurface_iface);
6012 return hr;
6014 wined3d_resource_set_parent(wined3d_texture_get_resource(*wined3d_texture), texture);
6015 for (i = 0; i < layers * levels; ++i)
6017 parent = wined3d_texture_get_sub_resource_parent(draw_texture, i);
6018 assert(parent->wined3d_texture == draw_texture);
6019 parent->draw_texture = draw_texture;
6020 parent->wined3d_texture = *wined3d_texture;
6021 wined3d_texture_set_sub_resource_parent(*wined3d_texture, i, parent);
6022 wined3d_texture_incref(*wined3d_texture);
6024 wined3d_texture_decref(*wined3d_texture);
6025 TRACE("Surface %p, created draw_texture %p, wined3d_texture %p.\n",
6026 wined3d_texture_get_sub_resource_parent(draw_texture, 0), draw_texture, wined3d_texture);
6027 return D3D_OK;
6029 no_draw_texture:
6030 if (SUCCEEDED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6031 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, texture, &ddraw_texture_wined3d_parent_ops,
6032 wined3d_texture)))
6033 wined3d_texture_decref(*wined3d_texture);
6034 return hr;
6037 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
6038 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
6040 struct wined3d_sub_resource_desc wined3d_mip_desc;
6041 struct ddraw_surface *root, *mip, **attach;
6042 struct wined3d_resource_desc wined3d_desc;
6043 DDPIXELFORMAT wined3d_display_mode_format;
6044 struct wined3d_texture *wined3d_texture;
6045 struct wined3d_display_mode mode;
6046 DDSURFACEDESC2 *desc, *mip_desc;
6047 struct ddraw_texture *texture;
6048 BOOL sysmem_fallback = FALSE;
6049 unsigned int layers = 1;
6050 unsigned int pitch = 0;
6051 BOOL reserve_memory;
6052 UINT levels, i, j;
6053 HRESULT hr;
6055 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
6056 ddraw, surface_desc, surface, outer_unknown, version);
6057 if (TRACE_ON(ddraw))
6059 TRACE("Requesting surface desc:\n");
6060 DDRAW_dump_surface_desc(surface_desc);
6063 if (outer_unknown)
6064 return CLASS_E_NOAGGREGATION;
6066 if (!surface)
6067 return E_POINTER;
6069 if (!(texture = heap_alloc(sizeof(*texture))))
6070 return E_OUTOFMEMORY;
6072 texture->texture_memory = NULL;
6073 texture->version = version;
6074 texture->surface_desc = *surface_desc;
6075 desc = &texture->surface_desc;
6077 /* Ensure DDSD_CAPS is always set. */
6078 desc->dwFlags |= DDSD_CAPS;
6080 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6082 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->u5.dwBackBufferCount)
6084 WARN("Tried to create a flippable surface without any back buffers.\n");
6085 heap_free(texture);
6086 return DDERR_INVALIDCAPS;
6089 if (!(desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX))
6091 WARN("Tried to create a flippable surface without DDSCAPS_COMPLEX.\n");
6092 heap_free(texture);
6093 return DDERR_INVALIDCAPS;
6096 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6098 WARN("Tried to create a flippable cubemap.\n");
6099 heap_free(texture);
6100 return DDERR_INVALIDPARAMS;
6103 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6105 FIXME("Flippable textures not implemented.\n");
6106 heap_free(texture);
6107 return DDERR_INVALIDCAPS;
6110 else
6112 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6114 WARN("Tried to specify a back buffer count for a non-flippable surface.\n");
6115 hr = desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP ? DDERR_INVALIDPARAMS : DDERR_INVALIDCAPS;
6116 heap_free(texture);
6117 return hr;
6121 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6123 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6125 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
6126 heap_free(texture);
6127 return DDERR_INVALIDCAPS;
6130 if ((desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX) && !(desc->ddsCaps.dwCaps & DDSCAPS_FLIP))
6132 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
6133 heap_free(texture);
6134 return DDERR_INVALIDCAPS;
6137 if ((desc->ddsCaps.dwCaps & DDSCAPS_FLIP) && !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
6139 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
6140 heap_free(texture);
6141 return DDERR_NOEXCLUSIVEMODE;
6145 /* This is a special case in ddrawex, but not allowed in ddraw. */
6146 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6147 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6149 WARN("Tried to create a surface in both system and video memory.\n");
6150 heap_free(texture);
6151 return DDERR_INVALIDCAPS;
6154 if ((desc->ddsCaps.dwCaps & (DDSCAPS_ALLOCONLOAD | DDSCAPS_MIPMAP))
6155 && !(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6157 WARN("Caps %#x require DDSCAPS_TEXTURE.\n", desc->ddsCaps.dwCaps);
6158 heap_free(texture);
6159 return DDERR_INVALIDCAPS;
6162 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
6163 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
6165 WARN("Cube map faces requested without cube map flag.\n");
6166 heap_free(texture);
6167 return DDERR_INVALIDCAPS;
6170 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6171 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
6173 WARN("Cube map without faces requested.\n");
6174 heap_free(texture);
6175 return DDERR_INVALIDPARAMS;
6178 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6179 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
6180 FIXME("Partial cube maps not implemented.\n");
6182 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6184 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6186 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
6187 heap_free(texture);
6188 return DDERR_INVALIDCAPS;
6190 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6192 WARN("DDSCAPS2_TEXTUREMANAGE used with DDSCAPS_VIDEOMEMORY "
6193 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
6194 heap_free(texture);
6195 return DDERR_INVALIDCAPS;
6199 if (desc->ddsCaps.dwCaps & DDSCAPS_WRITEONLY
6200 && !(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6202 WARN("DDSCAPS_WRITEONLY used without DDSCAPS2_TEXTUREMANAGE, returning DDERR_INVALIDCAPS.\n");
6203 heap_free(texture);
6204 return DDERR_INVALIDCAPS;
6207 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
6209 ERR("Failed to get display mode, hr %#x.\n", hr);
6210 heap_free(texture);
6211 return hr_ddraw_from_wined3d(hr);
6214 wined3d_display_mode_format.dwSize = sizeof(wined3d_display_mode_format);
6215 ddrawformat_from_wined3dformat(&wined3d_display_mode_format, mode.format_id);
6217 /* No pixelformat given? Use the current screen format. */
6218 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
6220 desc->dwFlags |= DDSD_PIXELFORMAT;
6221 desc->u4.ddpfPixelFormat = wined3d_display_mode_format;
6224 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6225 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
6226 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
6228 WARN("Unsupported / unknown pixelformat.\n");
6229 heap_free(texture);
6230 return DDERR_INVALIDPIXELFORMAT;
6233 /* No width or no height? Use the screen size. */
6234 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
6236 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
6238 WARN("No width / height specified.\n");
6239 heap_free(texture);
6240 return DDERR_INVALIDPARAMS;
6243 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
6244 desc->dwWidth = mode.width;
6245 desc->dwHeight = mode.height;
6248 if (!desc->dwWidth || !desc->dwHeight)
6250 heap_free(texture);
6251 return DDERR_INVALIDPARAMS;
6254 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6255 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
6257 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6259 /* The first surface is a front buffer, the back buffers are created
6260 * afterwards. */
6261 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
6262 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
6264 struct wined3d_swapchain_desc swapchain_desc;
6266 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
6267 swapchain_desc.backbuffer_width = mode.width;
6268 swapchain_desc.backbuffer_height = mode.height;
6269 swapchain_desc.backbuffer_format = mode.format_id;
6271 if (ddraw->d3ddevice)
6273 if (ddraw->d3ddevice->recording)
6274 wined3d_stateblock_decref(ddraw->d3ddevice->recording);
6275 ddraw->d3ddevice->recording = NULL;
6276 ddraw->d3ddevice->update_state = ddraw->d3ddevice->state;
6278 wined3d_stateblock_reset(ddraw->state);
6280 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
6281 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
6283 ERR("Failed to reset device.\n");
6284 heap_free(texture);
6285 return hr_ddraw_from_wined3d(hr);
6288 wined3d_stateblock_set_render_state(ddraw->state, WINED3D_RS_ZENABLE,
6289 !!swapchain_desc.enable_auto_depth_stencil);
6293 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
6294 wined3d_desc.multisample_quality = 0;
6295 wined3d_desc.usage = 0;
6296 wined3d_desc.bind_flags = 0;
6297 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6298 wined3d_desc.width = desc->dwWidth;
6299 wined3d_desc.height = desc->dwHeight;
6300 wined3d_desc.depth = 1;
6301 wined3d_desc.size = 0;
6303 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
6305 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
6306 /* Do not fail surface creation, only fail 3D device creation. */
6309 /* Mipmap count fixes */
6310 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6312 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
6314 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
6316 /* Mipmap count is given, should not be 0. */
6317 if (!desc->u2.dwMipMapCount)
6319 heap_free(texture);
6320 return DDERR_INVALIDPARAMS;
6323 else
6325 /* Undocumented feature: Create sublevels until either the
6326 * width or the height is 1. */
6327 if (version == 7)
6328 desc->u2.dwMipMapCount = wined3d_log2i(max(desc->dwWidth, desc->dwHeight)) + 1;
6329 else
6330 desc->u2.dwMipMapCount = wined3d_log2i(min(desc->dwWidth, desc->dwHeight)) + 1;
6333 else
6335 desc->u2.dwMipMapCount = 1;
6338 desc->dwFlags |= DDSD_MIPMAPCOUNT;
6339 levels = desc->u2.dwMipMapCount;
6341 else
6343 levels = 1;
6346 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
6348 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6350 unsigned int bind_flags = 0;
6351 DWORD usage = 0;
6353 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6355 usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6356 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6358 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6360 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6363 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6364 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6365 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6366 bind_flags |= WINED3D_BIND_RENDER_TARGET;
6368 if (!(ddraw->flags & DDRAW_NO3D) && SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d,
6369 ddraw->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL, mode.format_id,
6370 usage, bind_flags, WINED3D_RTYPE_TEXTURE_2D, wined3d_desc.format)))
6372 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
6374 else
6376 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6377 sysmem_fallback = TRUE;
6380 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6382 /* Tests show surfaces without memory flags get these flags added
6383 * right after creation. */
6384 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
6388 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6389 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6391 WARN("System memory overlays are not allowed.\n");
6392 heap_free(texture);
6393 return DDERR_NOOVERLAYHW;
6396 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
6398 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_CPU
6399 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6401 else
6403 if (!(ddraw->flags & DDRAW_NO3D))
6405 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6406 wined3d_desc.bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6407 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6408 wined3d_desc.bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6409 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6410 wined3d_desc.bind_flags |= WINED3D_BIND_RENDER_TARGET;
6413 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6415 wined3d_desc.bind_flags &= ~WINED3D_BIND_RENDER_TARGET;
6416 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU
6417 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6418 /* Managed textures have the system memory flag set. */
6419 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6421 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
6423 /* Videomemory adds localvidmem. This is mutually exclusive with
6424 * systemmemory and texturemanage. */
6425 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
6426 /* Dynamic resources can't be written by the GPU. */
6427 if (!(wined3d_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
6428 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
6432 if (desc->dwFlags & DDSD_LPSURFACE)
6434 if (wined3d_desc.access & WINED3D_RESOURCE_ACCESS_GPU)
6436 WARN("User memory surfaces should not be GPU accessible.\n");
6437 heap_free(texture);
6438 return DDERR_INVALIDCAPS;
6441 if (version < 4)
6443 WARN("User memory surfaces not supported before version 4.\n");
6444 heap_free(texture);
6445 return DDERR_INVALIDPARAMS;
6448 if (!desc->lpSurface)
6450 WARN("NULL surface memory pointer specified.\n");
6451 heap_free(texture);
6452 return DDERR_INVALIDPARAMS;
6455 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6457 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
6459 WARN("Pitch specified on a compressed user memory surface.\n");
6460 heap_free(texture);
6461 return DDERR_INVALIDPARAMS;
6464 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
6466 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
6467 heap_free(texture);
6468 return DDERR_INVALIDPARAMS;
6471 if ((desc->dwFlags & DDSD_LINEARSIZE)
6472 && desc->u1.dwLinearSize < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6473 wined3d_desc.format, wined3d_desc.width) * ((desc->dwHeight + 3) / 4))
6475 WARN("Invalid linear size %u specified.\n", desc->u1.dwLinearSize);
6476 heap_free(texture);
6477 return DDERR_INVALIDPARAMS;
6480 else
6482 if (!(desc->dwFlags & DDSD_PITCH))
6484 WARN("User memory surfaces should explicitly specify the pitch.\n");
6485 heap_free(texture);
6486 return DDERR_INVALIDPARAMS;
6489 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6490 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
6492 WARN("Invalid pitch %u specified.\n", desc->u1.lPitch);
6493 heap_free(texture);
6494 return DDERR_INVALIDPARAMS;
6497 pitch = desc->u1.lPitch;
6501 if (((desc->dwFlags & DDSD_CKDESTOVERLAY)
6502 && desc->u3.ddckCKDestOverlay.dwColorSpaceLowValue != desc->u3.ddckCKDestOverlay.dwColorSpaceHighValue)
6503 || ((desc->dwFlags & DDSD_CKDESTBLT)
6504 && desc->ddckCKDestBlt.dwColorSpaceLowValue != desc->ddckCKDestBlt.dwColorSpaceHighValue)
6505 || ((desc->dwFlags & DDSD_CKSRCOVERLAY)
6506 && desc->ddckCKSrcOverlay.dwColorSpaceLowValue != desc->ddckCKSrcOverlay.dwColorSpaceHighValue)
6507 || ((desc->dwFlags & DDSD_CKSRCBLT)
6508 && desc->ddckCKSrcBlt.dwColorSpaceLowValue != desc->ddckCKSrcBlt.dwColorSpaceHighValue))
6510 WARN("Range color keys not supported, returning DDERR_NOCOLORKEYHW.\n");
6511 heap_free(texture);
6512 return DDERR_NOCOLORKEYHW;
6515 if ((ddraw->flags & DDRAW_NO3D) && (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
6517 WARN("Video memory surfaces not supported without 3D support.\n");
6518 heap_free(texture);
6519 return DDERR_NODIRECTDRAWHW;
6522 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
6523 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
6525 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
6526 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
6528 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6530 wined3d_desc.usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6531 layers = 6;
6534 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, layers, levels,
6535 texture, &wined3d_texture)))
6537 WARN("Failed to create wined3d texture, hr %#x.\n", hr);
6538 heap_free(texture);
6539 return hr_ddraw_from_wined3d(hr);
6542 root = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6544 root->is_complex_root = TRUE;
6545 root->sysmem_fallback = sysmem_fallback;
6546 texture->root = root;
6547 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6549 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6550 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTOVERLAY,
6551 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6552 if (desc->dwFlags & DDSD_CKDESTBLT)
6553 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTBLT,
6554 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6555 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6556 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCOVERLAY,
6557 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6558 if (desc->dwFlags & DDSD_CKSRCBLT)
6559 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCBLT,
6560 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6562 for (i = 0; i < layers; ++i)
6564 attach = &root->complex_array[layers - 1 - i];
6566 for (j = 0; j < levels; ++j)
6568 mip = wined3d_texture_get_sub_resource_parent(wined3d_texture, i * levels + j);
6569 mip->sysmem_fallback = sysmem_fallback;
6570 mip_desc = &mip->surface_desc;
6571 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6572 mip_desc->u2.dwMipMapCount = levels - j;
6574 if (j)
6576 wined3d_texture_get_sub_resource_desc(wined3d_texture, i * levels + j, &wined3d_mip_desc);
6577 mip_desc->dwWidth = wined3d_mip_desc.width;
6578 mip_desc->dwHeight = wined3d_mip_desc.height;
6580 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6582 else
6584 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6587 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6589 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6591 switch (i)
6593 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6594 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6595 break;
6596 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6597 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6598 break;
6599 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6600 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6601 break;
6602 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6603 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6604 break;
6605 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6606 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6607 break;
6608 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6609 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6610 break;
6615 if (mip == root)
6616 continue;
6618 *attach = mip;
6619 attach = &mip->complex_array[0];
6623 if ((desc->dwFlags & DDSD_LPSURFACE) && FAILED(hr = wined3d_texture_update_desc(wined3d_texture, 0,
6624 wined3d_desc.width, wined3d_desc.height, wined3d_desc.format,
6625 WINED3D_MULTISAMPLE_NONE, 0, desc->lpSurface, pitch)))
6627 ERR("Failed to set surface memory, hr %#x.\n", hr);
6628 goto fail;
6631 reserve_memory = !(desc->dwFlags & DDSD_LPSURFACE)
6632 && desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY
6633 && wined3d_display_mode_format.u1.dwRGBBitCount <= 16;
6635 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture)))
6637 ERR("Failed to reserve surface memory, hr %#x.\n", hr);
6638 goto fail;
6641 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6643 unsigned int count = desc->u5.dwBackBufferCount;
6644 struct ddraw_surface *last = root;
6646 attach = &last->complex_array[0];
6647 for (i = 0; i < count; ++i)
6649 if (!(texture = heap_alloc(sizeof(*texture))))
6651 hr = E_OUTOFMEMORY;
6652 goto fail;
6655 texture->texture_memory = NULL;
6656 texture->version = version;
6657 texture->surface_desc = root->surface_desc;
6658 desc = &texture->surface_desc;
6660 /* Only one surface in the flipping chain is a back buffer, one is
6661 * a front buffer, the others are just flippable surfaces. */
6662 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6663 | DDSCAPS_BACKBUFFER);
6664 if (!i)
6665 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6666 desc->u5.dwBackBufferCount = 0;
6668 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, 1, 1,
6669 texture, &wined3d_texture)))
6671 heap_free(texture);
6672 hr = hr_ddraw_from_wined3d(hr);
6673 goto fail;
6676 last = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6677 last->sysmem_fallback = sysmem_fallback;
6678 texture->root = last;
6679 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6681 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6682 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6683 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6684 if (desc->dwFlags & DDSD_CKDESTBLT)
6685 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6686 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6687 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6688 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6689 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6690 if (desc->dwFlags & DDSD_CKSRCBLT)
6691 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6692 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6694 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture)))
6696 hr = hr_ddraw_from_wined3d(hr);
6697 goto fail;
6699 *attach = last;
6700 attach = &last->complex_array[0];
6702 *attach = root;
6705 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6707 ddraw->primary = root;
6708 ddraw->gdi_surface = root->wined3d_texture;
6710 *surface = root;
6712 return DD_OK;
6714 fail:
6715 if (version == 7)
6716 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6717 else if (version == 4)
6718 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6719 else
6720 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6722 return hr;
6725 void ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw,
6726 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
6727 const struct wined3d_parent_ops **parent_ops)
6729 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
6730 unsigned int texture_level, row_pitch, slice_pitch;
6731 DDSURFACEDESC2 *desc = &surface->surface_desc;
6732 unsigned int version = texture->version;
6734 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6735 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6736 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6737 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6738 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6739 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6740 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6741 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6742 surface->iface_count = 1;
6743 surface->version = version;
6744 surface->ddraw = ddraw;
6746 if (version == 7)
6748 surface->ref7 = 1;
6749 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6751 else if (version == 4)
6753 surface->ref4 = 1;
6754 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6756 else
6758 surface->ref1 = 1;
6759 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6762 *desc = texture->surface_desc;
6763 surface->first_attached = surface;
6765 texture_level = desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP ? sub_resource_idx % desc->u2.dwMipMapCount : 0;
6766 wined3d_texture_get_pitch(wined3d_texture, texture_level, &row_pitch, &slice_pitch);
6767 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6769 if (desc->dwFlags & DDSD_LPSURFACE)
6770 desc->u1.dwLinearSize = ~0u;
6771 else
6772 desc->u1.dwLinearSize = slice_pitch;
6773 desc->dwFlags |= DDSD_LINEARSIZE;
6774 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6776 else
6778 if (!(desc->dwFlags & DDSD_LPSURFACE))
6779 desc->u1.lPitch = row_pitch;
6780 desc->dwFlags |= DDSD_PITCH;
6781 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6783 desc->lpSurface = NULL;
6785 wined3d_texture_incref(surface->wined3d_texture = wined3d_texture);
6786 surface->sub_resource_idx = sub_resource_idx;
6787 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6788 surface->texture_location = DDRAW_SURFACE_LOCATION_DEFAULT;
6790 wined3d_private_store_init(&surface->private_store);
6793 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
6795 struct ddraw_surface *surface = parent;
6797 /* If the surface reference count drops to zero, we release our reference
6798 * to the view, but don't clear the pointer yet, in case e.g. a
6799 * GetRenderTarget() call brings the surface back before the view is
6800 * actually destroyed. When the view is destroyed, we need to clear the
6801 * pointer, or a subsequent surface AddRef() would reference it again.
6803 * This is safe because as long as the view still has a reference to the
6804 * texture, the surface is also still alive, and we're called before the
6805 * view releases that reference. */
6806 surface->wined3d_rtv = NULL;
6809 static const struct wined3d_parent_ops ddraw_view_wined3d_parent_ops =
6811 view_wined3d_object_destroyed,
6814 struct wined3d_rendertarget_view *ddraw_surface_get_rendertarget_view(struct ddraw_surface *surface)
6816 struct wined3d_texture *wined3d_texture;
6817 HRESULT hr;
6819 if (surface->wined3d_rtv)
6820 return surface->wined3d_rtv;
6822 wined3d_texture = surface->draw_texture ? surface->draw_texture : surface->wined3d_texture;
6823 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(wined3d_texture,
6824 surface->sub_resource_idx, surface, &ddraw_view_wined3d_parent_ops, &surface->wined3d_rtv)))
6826 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
6827 return NULL;
6830 return surface->wined3d_rtv;