netapi32: Convert the Unix library to the __wine_unix_call interface.
[wine.git] / dlls / ddraw / surface.c
blob25aa2be41bbbf9cdcbdb413074651392d1337b4c
1 /* DirectDraw Surface Implementation
3 * Copyright (c) 1997-2000 Marcus Meissner
4 * Copyright (c) 1998-2000 Lionel Ulmer
5 * Copyright (c) 2000-2001 TransGaming Technologies Inc.
6 * Copyright (c) 2006 Stefan Dösinger
7 * Copyright (c) 2011 Ričardas Barkauskas for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "ddraw_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
28 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface);
29 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface);
31 static inline struct ddraw_surface *impl_from_IDirectDrawGammaControl(IDirectDrawGammaControl *iface)
33 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawGammaControl_iface);
36 static BOOL ddraw_surface_is_lost(const struct ddraw_surface *surface)
38 return ddraw_surface_can_be_lost(surface)
39 && (surface->ddraw->device_state != DDRAW_DEVICE_STATE_OK || surface->is_lost);
42 static BOOL ddraw_gdi_is_front(struct ddraw *ddraw)
44 struct ddraw_surface *surface;
46 if (!ddraw->gdi_surface || !(surface = wined3d_texture_get_sub_resource_parent(ddraw->gdi_surface, 0)))
47 return FALSE;
49 return surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER;
52 /* This is slow, of course. Also, in case of locks, we can't prevent other
53 * applications from drawing to the screen while we've locked the frontbuffer.
54 * We'd like to do this in wined3d instead, but for that to work wined3d needs
55 * to support windowless rendering first. */
56 HRESULT ddraw_surface_update_frontbuffer(struct ddraw_surface *surface,
57 const RECT *rect, BOOL read, unsigned int swap_interval)
59 struct wined3d_texture *dst_texture, *wined3d_texture;
60 struct ddraw *ddraw = surface->ddraw;
61 HDC surface_dc, screen_dc;
62 int x, y, w, h;
63 HRESULT hr;
64 BOOL ret;
65 RECT r;
67 if (ddraw->flags & DDRAW_SWAPPED && !read)
69 ddraw->flags &= ~DDRAW_SWAPPED;
70 rect = NULL;
73 if (!rect)
75 SetRect(&r, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
76 rect = &r;
79 x = rect->left;
80 y = rect->top;
81 w = rect->right - rect->left;
82 h = rect->bottom - rect->top;
84 if (w <= 0 || h <= 0)
85 return DD_OK;
87 /* The interaction between ddraw and GDI drawing is not all that well
88 * documented, and somewhat arcane. In ddraw exclusive mode, GDI draws
89 * seemingly go to the *original* frontbuffer/primary surface, while ddraw
90 * draws/flips go to the *current* frontbuffer surface. The bottom line is
91 * that if the current frontbuffer is not the GDI frontbuffer, and there's
92 * e.g. a popup window in front of the ddraw swapchain window, we can't
93 * use wined3d_swapchain_present() to get the ddraw contents to the screen
94 * while in exclusive mode, since it would get obscured by the popup
95 * window. On the other hand, if the current frontbuffer *is* the GDI
96 * frontbuffer, that's what's supposed to happen; the popup should obscure
97 * (oart of) the ddraw swapchain window.
99 * This affects the "Deer Hunter" demo, which uses a popup window and GDI
100 * draws to draw part of the user interface. See also the "fswindow"
101 * sample is the DirectX 7 SDK. */
102 if (ddraw->swapchain_window && (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE)
103 || ddraw->swapchain_window == GetForegroundWindow() || ddraw_gdi_is_front(ddraw)))
105 /* Nothing to do, we control the frontbuffer, or at least the parts we
106 * care about. */
107 if (read)
108 return DD_OK;
110 if (swap_interval)
111 dst_texture = wined3d_swapchain_get_back_buffer(ddraw->wined3d_swapchain, 0);
112 else
113 dst_texture = ddraw->wined3d_frontbuffer;
115 if (SUCCEEDED(hr = wined3d_device_context_blt(ddraw->immediate_context, dst_texture, 0, rect,
116 ddraw_surface_get_any_texture(surface, DDRAW_SURFACE_READ), surface->sub_resource_idx, rect, 0,
117 NULL, WINED3D_TEXF_POINT)) && swap_interval)
119 hr = wined3d_swapchain_present(ddraw->wined3d_swapchain, rect, rect, NULL, swap_interval, 0);
120 ddraw->flags |= DDRAW_SWAPPED;
122 return hr;
125 wined3d_texture = ddraw_surface_get_default_texture(surface, read ? (rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE)
126 : DDRAW_SURFACE_READ);
128 if (FAILED(hr = wined3d_texture_get_dc(wined3d_texture, surface->sub_resource_idx, &surface_dc)))
130 ERR("Failed to get surface DC, hr %#x.\n", hr);
131 return hr;
133 if (surface->palette)
134 wined3d_palette_apply_to_dc(surface->palette->wined3d_palette, surface_dc);
136 if (!(screen_dc = GetDC(NULL)))
138 wined3d_texture_release_dc(wined3d_texture, surface->sub_resource_idx, surface_dc);
139 ERR("Failed to get screen DC.\n");
140 return E_FAIL;
143 if (read)
144 ret = BitBlt(surface_dc, x, y, w, h,
145 screen_dc, x, y, SRCCOPY);
146 else
147 ret = BitBlt(screen_dc, x, y, w, h,
148 surface_dc, x, y, SRCCOPY);
150 ReleaseDC(NULL, screen_dc);
151 wined3d_texture_release_dc(wined3d_texture, surface->sub_resource_idx, surface_dc);
153 if (!ret)
155 ERR("Failed to blit to/from screen.\n");
156 return E_FAIL;
159 return DD_OK;
162 /*****************************************************************************
163 * IUnknown parts follow
164 *****************************************************************************/
166 /*****************************************************************************
167 * IDirectDrawSurface7::QueryInterface
169 * A normal QueryInterface implementation. For QueryInterface rules
170 * see ddraw.c, IDirectDraw7::QueryInterface. This method
171 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
172 * in all versions, the IDirectDrawGammaControl interface and it can
173 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
175 * Params:
176 * riid: The interface id queried for
177 * obj: Address to write the pointer to
179 * Returns:
180 * S_OK on success
181 * E_NOINTERFACE if the requested interface wasn't found
183 *****************************************************************************/
184 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
186 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
188 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
190 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
191 *obj = NULL;
193 if(!riid)
194 return DDERR_INVALIDPARAMS;
196 if (IsEqualGUID(riid, &IID_IDirectDrawSurface7))
198 IDirectDrawSurface7_AddRef(iface);
199 *obj = iface;
200 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
201 return S_OK;
204 if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
206 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
207 *obj = &This->IDirectDrawSurface4_iface;
208 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
209 return S_OK;
212 if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
214 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
215 *obj = &This->IDirectDrawSurface3_iface;
216 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
217 return S_OK;
220 if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
222 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
223 *obj = &This->IDirectDrawSurface2_iface;
224 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
225 return S_OK;
228 if (IsEqualGUID(riid, &IID_IDirectDrawSurface)
229 || IsEqualGUID(riid, &IID_IUnknown))
231 IDirectDrawSurface_AddRef(&This->IDirectDrawSurface_iface);
232 *obj = &This->IDirectDrawSurface_iface;
233 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
234 return S_OK;
237 if (IsEqualGUID(riid, &IID_IDirectDrawGammaControl))
239 IDirectDrawGammaControl_AddRef(&This->IDirectDrawGammaControl_iface);
240 *obj = &This->IDirectDrawGammaControl_iface;
241 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
242 return S_OK;
245 if (IsEqualGUID(riid, &IID_IDirectDrawColorControl))
247 WARN("Color control not implemented.\n");
248 *obj = NULL;
249 return E_NOINTERFACE;
252 if (This->version != 7)
254 if (IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D)
255 || IsEqualGUID(riid, &IID_IDirect3DHALDevice)
256 || IsEqualGUID(riid, &IID_IDirect3DRGBDevice))
258 wined3d_mutex_lock();
259 if (!This->device1)
261 HRESULT hr;
263 if (FAILED(hr = d3d_device_create(This->ddraw, riid, This, (IUnknown *)&This->IDirectDrawSurface_iface,
264 1, &This->device1, (IUnknown *)&This->IDirectDrawSurface_iface)))
266 This->device1 = NULL;
267 wined3d_mutex_unlock();
268 WARN("Failed to create device, hr %#x.\n", hr);
269 return hr;
272 wined3d_mutex_unlock();
274 IDirect3DDevice_AddRef(&This->device1->IDirect3DDevice_iface);
275 *obj = &This->device1->IDirect3DDevice_iface;
276 return S_OK;
279 if (IsEqualGUID(&IID_IDirect3DTexture2, riid))
281 IDirect3DTexture2_AddRef(&This->IDirect3DTexture2_iface);
282 *obj = &This->IDirect3DTexture2_iface;
283 return S_OK;
286 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
288 IDirect3DTexture2_AddRef(&This->IDirect3DTexture_iface);
289 *obj = &This->IDirect3DTexture_iface;
290 return S_OK;
294 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
296 if (This->version != 7)
297 return E_INVALIDARG;
299 return E_NOINTERFACE;
302 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
304 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
306 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
308 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
311 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
313 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
315 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
317 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
320 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
322 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
324 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
326 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
329 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
331 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
333 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
335 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
338 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
339 REFIID riid, void **object)
341 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
343 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
345 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
348 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
350 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
352 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
354 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
357 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
359 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
361 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
363 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
366 static void ddraw_surface_add_iface(struct ddraw_surface *surface)
368 ULONG iface_count = InterlockedIncrement(&surface->iface_count);
369 TRACE("%p increasing iface count to %u.\n", surface, iface_count);
371 if (iface_count == 1)
373 if (surface->ifaceToRelease)
374 IUnknown_AddRef(surface->ifaceToRelease);
375 wined3d_mutex_lock();
376 if (surface->wined3d_rtv)
377 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
378 wined3d_texture_incref(surface->draw_texture ? surface->draw_texture : surface->wined3d_texture);
379 wined3d_mutex_unlock();
383 /*****************************************************************************
384 * IDirectDrawSurface7::AddRef
386 * A normal addref implementation
388 * Returns:
389 * The new refcount
391 *****************************************************************************/
392 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
394 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
395 ULONG refcount = InterlockedIncrement(&This->ref7);
397 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
399 if (refcount == 1)
401 ddraw_surface_add_iface(This);
404 return refcount;
407 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
409 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
410 ULONG refcount = InterlockedIncrement(&This->ref4);
412 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
414 if (refcount == 1)
416 ddraw_surface_add_iface(This);
419 return refcount;
422 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
424 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
425 ULONG refcount = InterlockedIncrement(&This->ref3);
427 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
429 if (refcount == 1)
431 ddraw_surface_add_iface(This);
434 return refcount;
437 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
439 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
440 ULONG refcount = InterlockedIncrement(&This->ref2);
442 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
444 if (refcount == 1)
446 ddraw_surface_add_iface(This);
449 return refcount;
452 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
454 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
455 ULONG refcount = InterlockedIncrement(&This->ref1);
457 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
459 if (refcount == 1)
461 ddraw_surface_add_iface(This);
464 return refcount;
467 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
469 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
470 ULONG refcount = InterlockedIncrement(&This->gamma_count);
472 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
474 if (refcount == 1)
476 ddraw_surface_add_iface(This);
479 return refcount;
482 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
484 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
486 TRACE("iface %p.\n", iface);
488 return IUnknown_AddRef(surface->texture_outer);
491 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
493 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
495 TRACE("iface %p.\n", iface);
497 return IUnknown_AddRef(surface->texture_outer);
500 static HRESULT ddraw_surface_set_palette(struct ddraw_surface *surface, IDirectDrawPalette *palette)
502 struct ddraw_palette *palette_impl = unsafe_impl_from_IDirectDrawPalette(palette);
503 struct ddraw_palette *prev;
505 TRACE("iface %p, palette %p.\n", surface, palette);
507 if (palette_impl && palette_impl->flags & DDPCAPS_ALPHA
508 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
510 WARN("Alpha palette set on non-texture surface, returning DDERR_INVALIDSURFACETYPE.\n");
511 return DDERR_INVALIDSURFACETYPE;
514 if (!format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
515 return DDERR_INVALIDPIXELFORMAT;
517 wined3d_mutex_lock();
519 prev = surface->palette;
520 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
522 if (prev)
523 prev->flags &= ~DDPCAPS_PRIMARYSURFACE;
524 if (palette_impl)
525 palette_impl->flags |= DDPCAPS_PRIMARYSURFACE;
526 wined3d_swapchain_set_palette(surface->ddraw->wined3d_swapchain,
527 palette_impl ? palette_impl->wined3d_palette : NULL);
528 ddraw_surface_update_frontbuffer(surface, NULL, FALSE, 0);
530 if (palette_impl)
531 IDirectDrawPalette_AddRef(&palette_impl->IDirectDrawPalette_iface);
532 if (prev)
533 IDirectDrawPalette_Release(&prev->IDirectDrawPalette_iface);
534 surface->palette = palette_impl;
536 wined3d_mutex_unlock();
538 return DD_OK;
541 static void ddraw_surface_cleanup(struct ddraw_surface *surface)
543 struct ddraw_surface *surf;
544 UINT i;
546 TRACE("surface %p.\n", surface);
548 /* The refcount test shows that the palette is detached when the surface
549 * is destroyed. */
550 ddraw_surface_set_palette(surface, NULL);
552 /* Loop through all complex attached surfaces and destroy them.
554 * Yet again, only the root can have more than one complexly attached
555 * surface, all the others have a total of one. */
556 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
558 if (!surface->complex_array[i])
559 break;
561 surf = surface->complex_array[i];
562 surface->complex_array[i] = NULL;
563 if (!surf->is_complex_root)
565 struct ddraw_texture *texture = wined3d_texture_get_parent(surf->wined3d_texture);
566 struct wined3d_device *wined3d_device = texture->wined3d_device;
567 struct ddraw_surface *root = texture->root;
569 ddraw_surface_cleanup(surf);
571 if (surf == root)
572 wined3d_device_decref(wined3d_device);
576 if (surface->device1)
577 IUnknown_Release(&surface->device1->IUnknown_inner);
579 if (surface->iface_count > 1)
581 /* This can happen when a complex surface is destroyed, because the
582 * 2nd surface was addref()ed when the app called
583 * GetAttachedSurface(). */
584 WARN("Destroying surface %p with refcounts 7: %u 4: %u 3: %u 2: %u 1: %u.\n",
585 surface, surface->ref7, surface->ref4, surface->ref3, surface->ref2, surface->ref1);
588 if (surface->wined3d_rtv)
589 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
590 wined3d_texture_decref(surface->draw_texture ? surface->draw_texture : surface->wined3d_texture);
593 static ULONG ddraw_surface_release_iface(struct ddraw_surface *This)
595 ULONG iface_count;
597 /* Prevent the surface from being destroyed if it's still attached to
598 * another surface. It will be destroyed when the root is destroyed. */
599 if (This->iface_count == 1 && This->attached_iface)
600 IUnknown_AddRef(This->attached_iface);
601 iface_count = InterlockedDecrement(&This->iface_count);
603 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
605 if (iface_count == 0)
607 struct ddraw_texture *texture = wined3d_texture_get_parent(This->wined3d_texture);
608 struct wined3d_device *wined3d_device = texture->wined3d_device;
609 IUnknown *release_iface = This->ifaceToRelease;
611 /* Complex attached surfaces are destroyed implicitly when the root is released */
612 wined3d_mutex_lock();
613 if(!This->is_complex_root)
615 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
616 wined3d_mutex_unlock();
617 return iface_count;
619 ddraw_surface_cleanup(This);
620 wined3d_mutex_unlock();
622 if (release_iface)
623 IUnknown_Release(release_iface);
624 /* Release the device only after anything that may reference it (the
625 * wined3d texture and rendertarget view in particular) is released. */
626 wined3d_device_decref(wined3d_device);
629 return iface_count;
632 /*****************************************************************************
633 * IDirectDrawSurface7::Release
635 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
636 * surface is destroyed.
638 * Destroying the surface is a bit tricky. For the connection between
639 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
640 * It has a nice graph explaining the connection.
642 * What happens here is basically this:
643 * When a surface is destroyed, its WineD3DSurface is released,
644 * and the refcount of the DirectDraw interface is reduced by 1. If it has
645 * complex surfaces attached to it, then these surfaces are destroyed too,
646 * regardless of their refcount. If any surface being destroyed has another
647 * surface attached to it (with a "soft" attachment, not complex), then
648 * this surface is detached with DeleteAttachedSurface.
650 * When the surface is a texture, the WineD3DTexture is released.
651 * If the surface is the Direct3D render target, then the D3D
652 * capabilities of the WineD3DDevice are uninitialized, which causes the
653 * swapchain to be released.
655 * When a complex sublevel falls to ref zero, then this is ignored.
657 * Returns:
658 * The new refcount
660 *****************************************************************************/
661 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
663 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
664 ULONG refcount = InterlockedDecrement(&This->ref7);
666 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
668 if (refcount == 0)
670 ddraw_surface_release_iface(This);
673 return refcount;
676 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
678 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
679 ULONG refcount = InterlockedDecrement(&This->ref4);
681 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
683 if (refcount == 0)
685 ddraw_surface_release_iface(This);
688 return refcount;
691 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
693 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
694 ULONG refcount = InterlockedDecrement(&This->ref3);
696 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
698 if (refcount == 0)
700 ddraw_surface_release_iface(This);
703 return refcount;
706 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
708 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
709 ULONG refcount = InterlockedDecrement(&This->ref2);
711 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
713 if (refcount == 0)
715 ddraw_surface_release_iface(This);
718 return refcount;
721 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
723 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
724 ULONG refcount = InterlockedDecrement(&This->ref1);
726 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
728 if (refcount == 0)
730 ddraw_surface_release_iface(This);
733 return refcount;
736 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
738 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
739 ULONG refcount = InterlockedDecrement(&This->gamma_count);
741 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
743 if (refcount == 0)
745 ddraw_surface_release_iface(This);
748 return refcount;
751 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
753 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
755 TRACE("iface %p.\n", iface);
757 return IUnknown_Release(surface->texture_outer);
760 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
762 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
764 TRACE("iface %p.\n", iface);
766 return IUnknown_Release(surface->texture_outer);
769 /*****************************************************************************
770 * IDirectDrawSurface7::GetAttachedSurface
772 * Returns an attached surface with the requested caps. Surface attachment
773 * and complex surfaces are not clearly described by the MSDN or sdk,
774 * so this method is tricky and likely to contain problems.
775 * This implementation searches the complex list first, then the
776 * attachment chain.
778 * The chains are searched from This down to the last surface in the chain,
779 * not from the first element in the chain. The first surface found is
780 * returned. The MSDN says that this method fails if more than one surface
781 * matches the caps, but it is not sure if that is right. The attachment
782 * structure may not even allow two matching surfaces.
784 * The found surface is AddRef-ed before it is returned.
786 * Params:
787 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
788 * Surface: Address to store the found surface
790 * Returns:
791 * DD_OK on success
792 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
793 * DDERR_NOTFOUND if no surface was found
795 *****************************************************************************/
796 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
797 DDSCAPS2 *caps, IDirectDrawSurface7 **surface)
799 struct ddraw_surface *head_surface = impl_from_IDirectDrawSurface7(iface);
800 struct ddraw_surface *surf;
801 DDSCAPS2 our_caps;
802 int i;
804 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, surface);
806 if (ddraw_surface_is_lost(head_surface))
808 WARN("Surface %p is lost.\n", head_surface);
810 *surface = NULL;
811 return DDERR_SURFACELOST;
814 wined3d_mutex_lock();
816 if(head_surface->version < 7)
818 /* Earlier dx apps put garbage into these members, clear them */
819 our_caps.dwCaps = caps->dwCaps;
820 our_caps.dwCaps2 = 0;
821 our_caps.dwCaps3 = 0;
822 our_caps.u1.dwCaps4 = 0;
824 else
826 our_caps = *caps;
829 TRACE("head_surface %p, looking for caps %#x, %#x, %#x, %#x.\n", head_surface, our_caps.dwCaps,
830 our_caps.dwCaps2, our_caps.dwCaps3, our_caps.u1.dwCaps4); /* FIXME: Better debugging */
832 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
834 surf = head_surface->complex_array[i];
835 if(!surf) break;
837 TRACE("Surface %p, caps %#x, %#x, %#x, %#x.\n", surf,
838 surf->surface_desc.ddsCaps.dwCaps,
839 surf->surface_desc.ddsCaps.dwCaps2,
840 surf->surface_desc.ddsCaps.dwCaps3,
841 surf->surface_desc.ddsCaps.u1.dwCaps4);
843 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
844 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
846 /* MSDN: "This method fails if more than one surface is attached
847 * that matches the capabilities requested."
849 * Not sure how to test this.
852 TRACE("head_surface %p, returning surface %p.\n", head_surface, surf);
853 *surface = &surf->IDirectDrawSurface7_iface;
854 ddraw_surface7_AddRef(*surface);
855 wined3d_mutex_unlock();
857 return DD_OK;
861 /* Next, look at the attachment chain */
862 surf = head_surface;
864 while( (surf = surf->next_attached) )
866 TRACE("Surface %p, caps %#x, %#x, %#x, %#x.\n", surf,
867 surf->surface_desc.ddsCaps.dwCaps,
868 surf->surface_desc.ddsCaps.dwCaps2,
869 surf->surface_desc.ddsCaps.dwCaps3,
870 surf->surface_desc.ddsCaps.u1.dwCaps4);
872 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
873 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
875 TRACE("head_surface %p, returning surface %p.\n", head_surface, surf);
876 *surface = &surf->IDirectDrawSurface7_iface;
877 ddraw_surface7_AddRef(*surface);
878 wined3d_mutex_unlock();
879 return DD_OK;
883 TRACE("head_surface %p, didn't find a valid surface.\n", head_surface);
885 wined3d_mutex_unlock();
887 *surface = NULL;
888 return DDERR_NOTFOUND;
891 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
892 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
894 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
895 struct ddraw_surface *attachment_impl;
896 IDirectDrawSurface7 *attachment7;
897 HRESULT hr;
899 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
901 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
902 caps, &attachment7);
903 if (FAILED(hr))
905 *attachment = NULL;
906 return hr;
908 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
909 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
910 ddraw_surface4_AddRef(*attachment);
911 ddraw_surface7_Release(attachment7);
913 return hr;
916 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
917 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
919 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
920 struct ddraw_surface *attachment_impl;
921 IDirectDrawSurface7 *attachment7;
922 DDSCAPS2 caps2;
923 HRESULT hr;
925 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
927 caps2.dwCaps = caps->dwCaps;
928 caps2.dwCaps2 = 0;
929 caps2.dwCaps3 = 0;
930 caps2.u1.dwCaps4 = 0;
932 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
933 &caps2, &attachment7);
934 if (FAILED(hr))
936 *attachment = NULL;
937 return hr;
939 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
940 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
941 ddraw_surface3_AddRef(*attachment);
942 ddraw_surface7_Release(attachment7);
944 return hr;
947 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
948 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
950 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
951 struct ddraw_surface *attachment_impl;
952 IDirectDrawSurface7 *attachment7;
953 DDSCAPS2 caps2;
954 HRESULT hr;
956 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
958 caps2.dwCaps = caps->dwCaps;
959 caps2.dwCaps2 = 0;
960 caps2.dwCaps3 = 0;
961 caps2.u1.dwCaps4 = 0;
963 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
964 &caps2, &attachment7);
965 if (FAILED(hr))
967 *attachment = NULL;
968 return hr;
970 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
971 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
972 ddraw_surface2_AddRef(*attachment);
973 ddraw_surface7_Release(attachment7);
975 return hr;
978 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
979 DDSCAPS *caps, IDirectDrawSurface **attachment)
981 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
982 struct ddraw_surface *attachment_impl;
983 IDirectDrawSurface7 *attachment7;
984 DDSCAPS2 caps2;
985 HRESULT hr;
987 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
989 caps2.dwCaps = caps->dwCaps;
990 caps2.dwCaps2 = 0;
991 caps2.dwCaps3 = 0;
992 caps2.u1.dwCaps4 = 0;
994 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
995 &caps2, &attachment7);
996 if (FAILED(hr))
998 *attachment = NULL;
999 return hr;
1001 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
1002 *attachment = &attachment_impl->IDirectDrawSurface_iface;
1003 ddraw_surface1_AddRef(*attachment);
1004 ddraw_surface7_Release(attachment7);
1006 return hr;
1009 /*****************************************************************************
1010 * IDirectDrawSurface7::Lock
1012 * Locks the surface and returns a pointer to the surface's memory
1014 * Params:
1015 * Rect: Rectangle to lock. If NULL, the whole surface is locked
1016 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
1017 * Flags: Locking flags, e.g Read only or write only
1018 * h: An event handle that's not used and must be NULL
1020 * Returns:
1021 * DD_OK on success
1022 * DDERR_INVALIDPARAMS if DDSD is NULL
1024 *****************************************************************************/
1025 static HRESULT surface_lock(struct ddraw_surface *surface,
1026 RECT *rect, DDSURFACEDESC2 *surface_desc, unsigned int surface_desc_size,
1027 DWORD flags, HANDLE h)
1029 struct wined3d_map_desc map_desc;
1030 unsigned int wined3d_flags;
1031 struct wined3d_box box;
1032 HRESULT hr = DD_OK;
1034 TRACE("surface %p, rect %s, surface_desc %p, surface_desc_size %u, flags %#x, h %p.\n",
1035 surface, wine_dbgstr_rect(rect), surface_desc, surface_desc_size, flags, h);
1037 /* surface->surface_desc.dwWidth and dwHeight are changeable, thus lock */
1038 wined3d_mutex_lock();
1040 /* Should I check for the handle to be NULL?
1042 * The DDLOCK flags and the D3DLOCK flags are equal
1043 * for the supported values. The others are ignored by WineD3D
1046 /* Windows zeroes this if the rect is invalid */
1047 surface_desc->lpSurface = NULL;
1049 if (rect)
1051 if ((rect->left < 0) || (rect->top < 0)
1052 || (rect->left > rect->right) || (rect->right > surface->surface_desc.dwWidth)
1053 || (rect->top > rect->bottom) || (rect->bottom > surface->surface_desc.dwHeight))
1055 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
1056 wined3d_mutex_unlock();
1057 return DDERR_INVALIDPARAMS;
1059 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
1062 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1063 hr = ddraw_surface_update_frontbuffer(surface, rect, TRUE, 0);
1064 if (SUCCEEDED(hr))
1066 wined3d_flags = wined3dmapflags_from_ddrawmapflags(flags);
1067 hr = wined3d_resource_map(wined3d_texture_get_resource
1068 (ddraw_surface_get_default_texture(surface, wined3d_flags & WINED3D_MAP_WRITE ? DDRAW_SURFACE_RW
1069 : DDRAW_SURFACE_READ)), surface->sub_resource_idx, &map_desc, rect ? &box : NULL, wined3d_flags);
1071 if (FAILED(hr))
1073 wined3d_mutex_unlock();
1074 switch(hr)
1076 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
1077 * specific error. But since wined3d returns that error in this only occasion,
1078 * keep d3d8 and d3d9 free from the return value override. There are many different
1079 * places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it is much easier
1080 * to do it in one place in ddraw.
1082 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
1083 default: return hr;
1087 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1089 if (flags & DDLOCK_READONLY)
1090 SetRectEmpty(&surface->ddraw->primary_lock);
1091 else if (rect)
1092 surface->ddraw->primary_lock = *rect;
1093 else
1094 SetRect(&surface->ddraw->primary_lock, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
1097 /* Windows does not set DDSD_LPSURFACE on locked surfaces. */
1098 DD_STRUCT_COPY_BYSIZE_(surface_desc, &surface->surface_desc, surface_desc_size, surface->surface_desc.dwSize);
1099 surface_desc->lpSurface = map_desc.data;
1101 TRACE("locked surface returning description :\n");
1102 if (TRACE_ON(ddraw))
1103 DDRAW_dump_surface_desc(surface_desc);
1105 wined3d_mutex_unlock();
1107 return DD_OK;
1110 static BOOL surface_validate_lock_desc(struct ddraw_surface *surface,
1111 const DDSURFACEDESC *desc, unsigned int *size)
1113 if (!desc)
1114 return FALSE;
1116 if (desc->dwSize == sizeof(DDSURFACEDESC) || desc->dwSize == sizeof(DDSURFACEDESC2))
1118 *size = desc->dwSize;
1119 return TRUE;
1122 if (surface->version == 7
1123 && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE
1124 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
1126 if (desc->dwSize >= sizeof(DDSURFACEDESC2))
1127 *size = sizeof(DDSURFACEDESC2);
1128 else
1129 *size = sizeof(DDSURFACEDESC);
1130 return TRUE;
1133 WARN("Invalid structure size %u.\n", desc->dwSize);
1134 return FALSE;
1137 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1138 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1140 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1141 unsigned int surface_desc_size;
1143 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1144 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1146 if (!surface_validate_lock_desc(surface, (DDSURFACEDESC *)surface_desc, &surface_desc_size))
1147 return DDERR_INVALIDPARAMS;
1149 if (ddraw_surface_is_lost(surface))
1151 WARN("Surface is lost.\n");
1152 return DDERR_SURFACELOST;
1155 return surface_lock(surface, rect, surface_desc, surface_desc_size, flags, h);
1158 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1159 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1161 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1162 unsigned int surface_desc_size;
1164 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1165 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1167 if (!surface_validate_lock_desc(surface, (DDSURFACEDESC *)surface_desc, &surface_desc_size))
1168 return DDERR_INVALIDPARAMS;
1170 if (ddraw_surface_is_lost(surface))
1172 WARN("Surface is lost.\n");
1173 return DDERR_SURFACELOST;
1176 return surface_lock(surface, rect, surface_desc, surface_desc_size, flags, h);
1179 static HRESULT ddraw_surface_lock_ddsd(struct ddraw_surface *surface, RECT *rect,
1180 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1182 unsigned int surface_desc_size;
1183 DDSURFACEDESC2 surface_desc2;
1184 HRESULT hr;
1186 if (!surface_validate_lock_desc(surface, surface_desc, &surface_desc_size))
1187 return DDERR_INVALIDPARAMS;
1189 if (ddraw_surface_is_lost(surface))
1191 WARN("Surface is lost.\n");
1192 return DDERR_SURFACELOST;
1195 surface_desc2.dwSize = surface_desc->dwSize;
1196 surface_desc2.dwFlags = 0;
1197 hr = surface_lock(surface, rect, &surface_desc2, surface_desc_size, flags, h);
1198 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1199 surface_desc->dwSize = surface_desc2.dwSize;
1200 return hr;
1203 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1204 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1206 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1208 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1209 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1211 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1214 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1215 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1217 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1219 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1220 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1222 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1225 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1226 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1228 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1230 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1231 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1233 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1236 /* FRAPS hooks IDirectDrawSurface::Unlock and expects the version 1 method to be called when the
1237 * game uses later interfaces. */
1238 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1240 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1241 HRESULT hr;
1243 TRACE("iface %p, data %p.\n", iface, data);
1245 wined3d_mutex_lock();
1246 hr = wined3d_resource_unmap(wined3d_texture_get_resource
1247 (ddraw_surface_get_default_texture(surface, 0)), surface->sub_resource_idx);
1248 if (SUCCEEDED(hr) && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1249 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE, 0);
1250 wined3d_mutex_unlock();
1252 return hr;
1255 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *rect)
1257 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1259 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1261 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, NULL);
1264 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *rect)
1266 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1268 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1270 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, NULL);
1273 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1275 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1277 TRACE("iface %p, data %p.\n", iface, data);
1279 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, data);
1282 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1284 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1286 TRACE("iface %p, data %p.\n", iface, data);
1288 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, data);
1291 static unsigned int ddraw_swap_interval_from_flags(DWORD flags)
1293 if (flags & DDFLIP_NOVSYNC)
1294 return 0;
1296 switch (flags & (DDFLIP_INTERVAL2 | DDFLIP_INTERVAL3 | DDFLIP_INTERVAL4))
1298 case DDFLIP_INTERVAL2:
1299 return 2;
1300 case DDFLIP_INTERVAL3:
1301 return 3;
1302 case DDFLIP_INTERVAL4:
1303 return 4;
1304 default:
1305 return 1;
1309 /* FRAPS hooks IDirectDrawSurface::Flip and expects the version 1 method to be called when the
1310 * game uses later interfaces. */
1311 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Flip(IDirectDrawSurface *iface,
1312 IDirectDrawSurface *src, DWORD flags)
1314 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
1315 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
1316 struct ddraw_texture *dst_ddraw_texture, *src_ddraw_texture;
1317 struct wined3d_rendertarget_view *tmp_rtv, *src_rtv, *rtv;
1318 DDSCAPS caps = {DDSCAPS_FLIP};
1319 struct wined3d_texture *texture, *draw_texture;
1320 IDirectDrawSurface *current;
1321 void *texture_memory;
1322 HRESULT hr;
1324 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1326 if (src == iface || !(dst_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1327 return DDERR_NOTFLIPPABLE;
1329 if (ddraw_surface_is_lost(dst_impl))
1330 return DDERR_SURFACELOST;
1332 wined3d_mutex_lock();
1334 if ((dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1335 && !(dst_impl->ddraw->cooperative_level & DDSCL_EXCLUSIVE))
1337 WARN("Not in exclusive mode.\n");
1338 wined3d_mutex_unlock();
1339 return DDERR_NOEXCLUSIVEMODE;
1342 tmp_rtv = ddraw_surface_get_rendertarget_view(dst_impl);
1343 if (dst_impl->sub_resource_idx)
1344 ERR("Invalid sub-resource index %u on surface %p.\n", dst_impl->sub_resource_idx, dst_impl);
1345 texture = dst_impl->wined3d_texture;
1346 rtv = wined3d_device_context_get_rendertarget_view(dst_impl->ddraw->immediate_context, 0);
1347 dst_ddraw_texture = wined3d_texture_get_parent(dst_impl->wined3d_texture);
1348 texture_memory = dst_ddraw_texture->texture_memory;
1349 draw_texture = dst_impl->draw_texture;
1351 if (src_impl)
1353 for (current = iface; current != src;)
1355 if (FAILED(hr = ddraw_surface1_GetAttachedSurface(current, &caps, &current)))
1357 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1358 wined3d_mutex_unlock();
1359 return DDERR_NOTFLIPPABLE;
1361 ddraw_surface1_Release(current);
1362 if (current == iface)
1364 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1365 wined3d_mutex_unlock();
1366 return DDERR_NOTFLIPPABLE;
1370 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1371 if (rtv == dst_impl->wined3d_rtv)
1372 wined3d_device_context_set_rendertarget_views(dst_impl->ddraw->immediate_context, 0, 1, &src_rtv, FALSE);
1373 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1374 dst_impl->wined3d_rtv = src_rtv;
1375 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1376 if (src_impl->draw_texture)
1377 wined3d_texture_set_sub_resource_parent(src_impl->draw_texture, 0, dst_impl);
1378 src_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1379 dst_ddraw_texture->texture_memory = src_ddraw_texture->texture_memory;
1380 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), dst_ddraw_texture);
1381 if (src_impl->draw_texture)
1382 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->draw_texture), dst_ddraw_texture);
1383 dst_ddraw_texture = src_ddraw_texture;
1384 if (src_impl->sub_resource_idx)
1385 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1386 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1387 dst_impl->draw_texture = src_impl->draw_texture;
1389 else
1391 for (current = iface;;)
1393 if (FAILED(hr = ddraw_surface1_GetAttachedSurface(current, &caps, &current)))
1395 ERR("Can't find a flip target\n");
1396 wined3d_mutex_unlock();
1397 return DDERR_NOTFLIPPABLE; /* Unchecked */
1399 ddraw_surface1_Release(current);
1400 if (current == iface)
1402 dst_impl = impl_from_IDirectDrawSurface(iface);
1403 break;
1406 src_impl = impl_from_IDirectDrawSurface(current);
1407 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1408 if (rtv == dst_impl->wined3d_rtv)
1409 wined3d_device_context_set_rendertarget_views(dst_impl->ddraw->immediate_context,
1410 0, 1, &src_rtv, FALSE);
1411 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1412 dst_impl->wined3d_rtv = src_rtv;
1413 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1414 if (src_impl->draw_texture)
1415 wined3d_texture_set_sub_resource_parent(src_impl->draw_texture, 0, dst_impl);
1416 src_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1417 dst_ddraw_texture->texture_memory = src_ddraw_texture->texture_memory;
1418 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), dst_ddraw_texture);
1419 if (src_impl->draw_texture)
1420 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->draw_texture), dst_ddraw_texture);
1421 dst_ddraw_texture = src_ddraw_texture;
1422 if (src_impl->sub_resource_idx)
1423 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1424 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1425 dst_impl->draw_texture = src_impl->draw_texture;
1426 dst_impl = src_impl;
1430 /* We don't have to worry about potential texture bindings, since
1431 * flippable surfaces can never be textures. */
1432 if (rtv == src_impl->wined3d_rtv)
1433 wined3d_device_context_set_rendertarget_views(dst_impl->ddraw->immediate_context, 0, 1, &tmp_rtv, FALSE);
1434 wined3d_rendertarget_view_set_parent(tmp_rtv, src_impl);
1435 src_impl->wined3d_rtv = tmp_rtv;
1436 wined3d_texture_set_sub_resource_parent(texture, 0, src_impl);
1437 if (draw_texture)
1438 wined3d_texture_set_sub_resource_parent(draw_texture, 0, src_impl);
1439 dst_ddraw_texture->texture_memory = texture_memory;
1440 wined3d_resource_set_parent(wined3d_texture_get_resource(texture), dst_ddraw_texture);
1441 if (draw_texture)
1442 wined3d_resource_set_parent(wined3d_texture_get_resource(draw_texture), dst_ddraw_texture);
1443 src_impl->wined3d_texture = texture;
1444 src_impl->draw_texture = draw_texture;
1446 if (flags & ~(DDFLIP_NOVSYNC | DDFLIP_INTERVAL2 | DDFLIP_INTERVAL3 | DDFLIP_INTERVAL4))
1448 static UINT once;
1449 if (!once++)
1450 FIXME("Ignoring flags %#x.\n", flags);
1451 else
1452 WARN("Ignoring flags %#x.\n", flags);
1455 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1456 hr = ddraw_surface_update_frontbuffer(dst_impl, NULL, FALSE, ddraw_swap_interval_from_flags(flags));
1457 else
1458 hr = DD_OK;
1460 wined3d_mutex_unlock();
1462 return hr;
1465 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Flip(IDirectDrawSurface7 *iface,
1466 IDirectDrawSurface7 *src, DWORD flags)
1468 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1469 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src);
1471 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1473 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1474 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1477 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Flip(IDirectDrawSurface4 *iface,
1478 IDirectDrawSurface4 *src, DWORD flags)
1480 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1481 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
1483 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1485 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1486 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1489 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Flip(IDirectDrawSurface3 *iface,
1490 IDirectDrawSurface3 *src, DWORD flags)
1492 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1493 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src);
1495 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1497 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1498 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1501 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Flip(IDirectDrawSurface2 *iface,
1502 IDirectDrawSurface2 *src, DWORD flags)
1504 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1505 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src);
1507 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1509 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1510 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1513 static HRESULT ddraw_surface_blt(struct ddraw_surface *dst_surface, const RECT *dst_rect,
1514 struct ddraw_surface *src_surface, const RECT *src_rect, DWORD flags, DWORD fill_colour,
1515 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1517 struct ddraw *ddraw = dst_surface->ddraw;
1518 struct wined3d_device *wined3d_device = ddraw->wined3d_device;
1519 struct wined3d_color colour;
1520 DWORD wined3d_flags;
1522 if (flags & DDBLT_COLORFILL)
1524 wined3d_flags = WINED3DCLEAR_TARGET;
1525 if (!(flags & DDBLT_ASYNC))
1526 wined3d_flags |= WINED3DCLEAR_SYNCHRONOUS;
1528 if (!wined3d_colour_from_ddraw_colour(&dst_surface->surface_desc.u4.ddpfPixelFormat,
1529 dst_surface->palette, fill_colour, &colour))
1530 return DDERR_INVALIDPARAMS;
1532 wined3d_device_apply_stateblock(wined3d_device, ddraw->state);
1533 ddraw_surface_get_draw_texture(dst_surface, dst_rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE);
1534 return wined3d_device_context_clear_rendertarget_view(ddraw->immediate_context,
1535 ddraw_surface_get_rendertarget_view(dst_surface),
1536 dst_rect, wined3d_flags, &colour, 0.0f, 0);
1539 if (flags & DDBLT_DEPTHFILL)
1541 wined3d_flags = WINED3DCLEAR_ZBUFFER;
1542 if (!(flags & DDBLT_ASYNC))
1543 wined3d_flags |= WINED3DCLEAR_SYNCHRONOUS;
1545 if (!wined3d_colour_from_ddraw_colour(&dst_surface->surface_desc.u4.ddpfPixelFormat,
1546 dst_surface->palette, fill_colour, &colour))
1547 return DDERR_INVALIDPARAMS;
1549 wined3d_device_apply_stateblock(wined3d_device, ddraw->state);
1550 ddraw_surface_get_draw_texture(dst_surface, dst_rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE);
1551 return wined3d_device_context_clear_rendertarget_view(ddraw->immediate_context,
1552 ddraw_surface_get_rendertarget_view(dst_surface),
1553 dst_rect, wined3d_flags, NULL, colour.r, 0);
1556 wined3d_flags = flags & ~DDBLT_ASYNC;
1557 if (wined3d_flags & ~WINED3D_BLT_MASK)
1559 FIXME("Unhandled flags %#x.\n", flags);
1560 return E_NOTIMPL;
1563 if (!(flags & DDBLT_ASYNC))
1564 wined3d_flags |= WINED3D_BLT_SYNCHRONOUS;
1566 return wined3d_device_context_blt(ddraw->immediate_context,
1567 ddraw_surface_get_any_texture(dst_surface, DDRAW_SURFACE_RW), dst_surface->sub_resource_idx, dst_rect,
1568 ddraw_surface_get_any_texture(src_surface, DDRAW_SURFACE_READ), src_surface->sub_resource_idx, src_rect,
1569 wined3d_flags, fx, filter);
1572 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1573 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags, DWORD fill_colour,
1574 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1576 RECT src_rect, dst_rect;
1577 float scale_x, scale_y;
1578 const RECT *clip_rect;
1579 UINT clip_list_size;
1580 RGNDATA *clip_list;
1581 HRESULT hr = DD_OK;
1582 UINT i;
1584 if (!dst_rect_in)
1585 SetRect(&dst_rect, 0, 0, dst_surface->surface_desc.dwWidth,
1586 dst_surface->surface_desc.dwHeight);
1587 else
1588 dst_rect = *dst_rect_in;
1590 if (IsRectEmpty(&dst_rect))
1591 return DDERR_INVALIDRECT;
1593 if (src_surface)
1595 if (!src_rect_in)
1596 SetRect(&src_rect, 0, 0, src_surface->surface_desc.dwWidth,
1597 src_surface->surface_desc.dwHeight);
1598 else
1599 src_rect = *src_rect_in;
1601 if (IsRectEmpty(&src_rect))
1602 return DDERR_INVALIDRECT;
1604 else
1606 SetRectEmpty(&src_rect);
1609 if (!dst_surface->clipper)
1611 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1612 hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect, TRUE, 0);
1613 if (SUCCEEDED(hr))
1614 hr = ddraw_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, flags, fill_colour, fx, filter);
1615 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
1616 hr = ddraw_surface_update_frontbuffer(dst_surface, &dst_rect, FALSE, 0);
1618 return hr;
1621 if (!ddraw_clipper_is_valid(dst_surface->clipper))
1623 FIXME("Attempting to blit with an invalid clipper.\n");
1624 return DDERR_INVALIDPARAMS;
1627 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1628 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1630 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1631 &dst_rect, NULL, &clip_list_size)))
1633 WARN("Failed to get clip list size, hr %#x.\n", hr);
1634 return hr;
1637 if (!(clip_list = heap_alloc(clip_list_size)))
1639 WARN("Failed to allocate clip list.\n");
1640 return E_OUTOFMEMORY;
1643 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1644 &dst_rect, clip_list, &clip_list_size)))
1646 WARN("Failed to get clip list, hr %#x.\n", hr);
1647 heap_free(clip_list);
1648 return hr;
1651 clip_rect = (RECT *)clip_list->Buffer;
1652 for (i = 0; i < clip_list->rdh.nCount; ++i)
1654 RECT src_rect_clipped = src_rect;
1656 if (src_surface)
1658 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1659 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1660 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1661 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1663 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1665 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE, 0)))
1666 break;
1670 if (FAILED(hr = ddraw_surface_blt(dst_surface, &clip_rect[i],
1671 src_surface, &src_rect_clipped, flags, fill_colour, fx, filter)))
1672 break;
1674 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1676 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE, 0)))
1677 break;
1681 heap_free(clip_list);
1682 return hr;
1685 /* FRAPS hooks IDirectDrawSurface::Blt and expects the version 1 method to be called when the
1686 * game uses later interfaces. */
1687 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1688 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1690 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
1691 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1692 struct wined3d_blt_fx wined3d_fx = {0};
1693 DWORD unsupported_flags;
1694 DWORD fill_colour = 0;
1695 HRESULT hr = DD_OK;
1696 DDBLTFX rop_fx;
1698 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1699 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1701 unsupported_flags = DDBLT_ALPHADEST
1702 | DDBLT_ALPHADESTCONSTOVERRIDE
1703 | DDBLT_ALPHADESTNEG
1704 | DDBLT_ALPHADESTSURFACEOVERRIDE
1705 | DDBLT_ALPHAEDGEBLEND
1706 | DDBLT_ALPHASRC
1707 | DDBLT_ALPHASRCCONSTOVERRIDE
1708 | DDBLT_ALPHASRCNEG
1709 | DDBLT_ALPHASRCSURFACEOVERRIDE
1710 | DDBLT_ZBUFFER
1711 | DDBLT_ZBUFFERDESTCONSTOVERRIDE
1712 | DDBLT_ZBUFFERDESTOVERRIDE
1713 | DDBLT_ZBUFFERSRCCONSTOVERRIDE
1714 | DDBLT_ZBUFFERSRCOVERRIDE;
1715 if (flags & unsupported_flags)
1717 WARN("Ignoring unsupported flags %#x.\n", flags & unsupported_flags);
1718 flags &= ~unsupported_flags;
1721 if ((flags & DDBLT_KEYSRCOVERRIDE) && (!fx || flags & DDBLT_KEYSRC))
1723 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1724 return DDERR_INVALIDPARAMS;
1727 if ((flags & DDBLT_KEYDESTOVERRIDE) && (!fx || flags & DDBLT_KEYDEST))
1729 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1730 return DDERR_INVALIDPARAMS;
1733 if (flags & DDBLT_DDROPS)
1735 FIXME("DDBLT_DDROPS not implemented.\n");
1736 if (fx)
1737 FIXME(" rop %#x, pattern %p.\n", fx->dwDDROP, fx->u5.lpDDSPattern);
1738 return DDERR_NORASTEROPHW;
1741 wined3d_mutex_lock();
1743 if (flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1745 if (flags & DDBLT_ROP)
1747 wined3d_mutex_unlock();
1748 WARN("DDBLT_ROP used with DDBLT_COLORFILL or DDBLT_DEPTHFILL, returning DDERR_INVALIDPARAMS.\n");
1749 return DDERR_INVALIDPARAMS;
1751 if (src_impl)
1753 wined3d_mutex_unlock();
1754 WARN("Depth or colorfill is not compatible with source surfaces, returning DDERR_INVALIDPARAMS\n");
1755 return DDERR_INVALIDPARAMS;
1757 if (!fx)
1759 wined3d_mutex_unlock();
1760 WARN("Depth or colorfill used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1761 return DDERR_INVALIDPARAMS;
1764 if ((flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) == (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1765 flags &= ~DDBLT_DEPTHFILL;
1767 if ((dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_COLORFILL))
1769 wined3d_mutex_unlock();
1770 WARN("DDBLT_COLORFILL used on a depth buffer, returning DDERR_INVALIDPARAMS.\n");
1771 return DDERR_INVALIDPARAMS;
1773 if (!(dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_DEPTHFILL))
1775 wined3d_mutex_unlock();
1776 WARN("DDBLT_DEPTHFILL used on a color buffer, returning DDERR_INVALIDPARAMS.\n");
1777 return DDERR_INVALIDPARAMS;
1781 if (flags & DDBLT_ROP)
1783 if (!fx)
1785 wined3d_mutex_unlock();
1786 WARN("DDBLT_ROP used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1787 return DDERR_INVALIDPARAMS;
1790 if (src_impl && src_rect
1791 && ((ULONG)src_rect->left >= src_rect->right || src_rect->right > src_impl->surface_desc.dwWidth
1792 || (ULONG)src_rect->top >= src_rect->bottom || src_rect->bottom > src_impl->surface_desc.dwHeight))
1794 wined3d_mutex_unlock();
1795 WARN("Invalid source rectangle.\n");
1796 return DDERR_INVALIDRECT;
1799 flags &= ~DDBLT_ROP;
1800 switch (fx->dwROP)
1802 case SRCCOPY:
1803 break;
1805 case WHITENESS:
1806 case BLACKNESS:
1807 rop_fx = *fx;
1809 if (fx->dwROP == WHITENESS)
1810 rop_fx.u5.dwFillColor = 0xffffffff;
1811 else
1812 rop_fx.u5.dwFillColor = 0;
1814 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1815 flags |= DDBLT_DEPTHFILL;
1816 else
1817 flags |= DDBLT_COLORFILL;
1819 fx = &rop_fx;
1820 break;
1822 default:
1823 wined3d_mutex_unlock();
1824 WARN("Unsupported ROP %#x used, returning DDERR_NORASTEROPHW.\n", fx->dwROP);
1825 return DDERR_NORASTEROPHW;
1829 if (!(flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) && !src_impl)
1831 WARN("No source surface.\n");
1832 wined3d_mutex_unlock();
1833 return DDERR_INVALIDPARAMS;
1836 if (flags & DDBLT_KEYSRC && (!src_impl || !(src_impl->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1838 WARN("DDBLT_KEYSRC blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1839 wined3d_mutex_unlock();
1840 return DDERR_INVALIDPARAMS;
1842 if (flags & DDBLT_KEYDEST && !(dst_impl->surface_desc.dwFlags & DDSD_CKDESTBLT))
1844 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1845 wined3d_mutex_unlock();
1846 return DDERR_INVALIDPARAMS;
1849 if (fx)
1851 wined3d_fx.fx = fx->dwDDFX;
1852 fill_colour = fx->u5.dwFillColor;
1853 wined3d_fx.dst_color_key.color_space_low_value = fx->ddckDestColorkey.dwColorSpaceLowValue;
1854 wined3d_fx.dst_color_key.color_space_high_value = fx->ddckDestColorkey.dwColorSpaceHighValue;
1855 wined3d_fx.src_color_key.color_space_low_value = fx->ddckSrcColorkey.dwColorSpaceLowValue;
1856 wined3d_fx.src_color_key.color_space_high_value = fx->ddckSrcColorkey.dwColorSpaceHighValue;
1859 hr = ddraw_surface_blt_clipped(dst_impl, dst_rect, src_impl,
1860 src_rect, flags, fill_colour, fx ? &wined3d_fx : NULL, WINED3D_TEXF_LINEAR);
1862 wined3d_mutex_unlock();
1863 switch(hr)
1865 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1866 default: return hr;
1870 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *dst_rect,
1871 IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1873 struct ddraw_surface *dst = impl_from_IDirectDrawSurface7(iface);
1874 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface7(src_surface);
1876 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1877 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1879 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1880 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1883 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1884 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1886 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1887 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1889 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1890 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1892 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1893 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1896 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1897 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1899 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1900 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1902 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1903 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1905 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1906 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1909 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1910 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1912 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1913 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1915 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1916 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1918 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1919 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1922 /*****************************************************************************
1923 * IDirectDrawSurface7::AddAttachedSurface
1925 * Attaches a surface to another surface. How the surface attachments work
1926 * is not totally understood yet, and this method is prone to problems.
1927 * The surface that is attached is AddRef-ed.
1929 * Tests with complex surfaces suggest that the surface attachments form a
1930 * tree, but no method to test this has been found yet.
1932 * The attachment list consists of a first surface (first_attached) and
1933 * for each surface a pointer to the next attached surface (next_attached).
1934 * For the first surface, and a surface that has no attachments
1935 * first_attached points to the surface itself. A surface that has
1936 * no successors in the chain has next_attached set to NULL.
1938 * Newly attached surfaces are attached right after the root surface.
1939 * If a surface is attached to a complex surface compound, it's attached to
1940 * the surface that the app requested, not the complex root. See
1941 * GetAttachedSurface for a description how surfaces are found.
1943 * This is how the current implementation works, and it was coded by looking
1944 * at the needs of the applications.
1946 * So far only Z-Buffer attachments are tested, and they are activated in
1947 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1948 * Back buffers should work in 2D mode, but they are not tested(They can be
1949 * attached in older iface versions). Rendering to the front buffer and
1950 * switching between that and double buffering is not yet implemented in
1951 * WineD3D, so for 3D it might have unexpected results.
1953 * ddraw_surface_attach_surface is the real thing,
1954 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1955 * performs additional checks. Version 7 of this interface is much more restrictive
1956 * than its predecessors.
1958 * Params:
1959 * Attach: Surface to attach to iface
1961 * Returns:
1962 * DD_OK on success
1963 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1965 *****************************************************************************/
1966 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1968 TRACE("surface %p, attachment %p.\n", This, Surf);
1970 if(Surf == This)
1971 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1973 wined3d_mutex_lock();
1975 /* Check if the surface is already attached somewhere */
1976 if (Surf->next_attached || Surf->first_attached != Surf)
1978 /* TODO: Test for the structure of the manual attachment. Is it a
1979 * chain or a list? What happens if one surface is attached to 2
1980 * different surfaces? */
1981 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1982 Surf, Surf->next_attached, Surf->first_attached);
1984 wined3d_mutex_unlock();
1985 return DDERR_SURFACEALREADYATTACHED;
1988 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1989 Surf->next_attached = This->next_attached;
1990 Surf->first_attached = This->first_attached;
1991 This->next_attached = Surf;
1993 /* Check if the WineD3D depth stencil needs updating */
1994 if (This->ddraw->d3ddevice)
1995 d3d_device_update_depth_stencil(This->ddraw->d3ddevice);
1997 wined3d_mutex_unlock();
1999 return DD_OK;
2002 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
2004 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
2005 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
2006 HRESULT hr;
2008 TRACE("iface %p, attachment %p.\n", iface, attachment);
2010 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
2011 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
2014 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
2015 attachment_impl->surface_desc.ddsCaps.dwCaps);
2016 return DDERR_CANNOTATTACHSURFACE;
2019 hr = ddraw_surface_attach_surface(This, attachment_impl);
2020 if (FAILED(hr))
2022 return hr;
2024 attachment_impl->attached_iface = (IUnknown *)attachment;
2025 IUnknown_AddRef(attachment_impl->attached_iface);
2026 return hr;
2029 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
2031 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2032 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
2033 HRESULT hr;
2035 TRACE("iface %p, attachment %p.\n", iface, attachment);
2037 /* Tests suggest that
2038 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
2039 * -> offscreen plain surfaces can be attached to primaries
2040 * -> primaries can be attached to offscreen plain surfaces
2041 * -> z buffers can be attached to primaries */
2042 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
2043 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
2045 /* Sizes have to match */
2046 if (attachment_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
2047 || attachment_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
2049 WARN("Surface sizes do not match.\n");
2050 return DDERR_CANNOTATTACHSURFACE;
2053 else if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
2054 || !(attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER)))
2056 WARN("Invalid attachment combination.\n");
2057 return DDERR_CANNOTATTACHSURFACE;
2060 if (FAILED(hr = ddraw_surface_attach_surface(surface, attachment_impl)))
2061 return hr;
2063 attachment_impl->attached_iface = (IUnknown *)attachment;
2064 IUnknown_AddRef(attachment_impl->attached_iface);
2065 return hr;
2068 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
2070 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2071 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2072 HRESULT hr;
2074 TRACE("iface %p, attachment %p.\n", iface, attachment);
2076 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2077 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2078 return hr;
2080 attachment_impl->attached_iface = (IUnknown *)attachment;
2081 IUnknown_AddRef(attachment_impl->attached_iface);
2082 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2083 return hr;
2086 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
2088 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2089 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2090 HRESULT hr;
2092 TRACE("iface %p, attachment %p.\n", iface, attachment);
2094 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2095 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2096 return hr;
2098 attachment_impl->attached_iface = (IUnknown *)attachment;
2099 IUnknown_AddRef(attachment_impl->attached_iface);
2100 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2101 return hr;
2104 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
2106 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2107 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2108 HRESULT hr;
2110 TRACE("iface %p, attachment %p.\n", iface, attachment);
2112 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2113 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2114 return hr;
2116 attachment_impl->attached_iface = (IUnknown *)attachment;
2117 IUnknown_AddRef(attachment_impl->attached_iface);
2118 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2119 return hr;
2122 /*****************************************************************************
2123 * IDirectDrawSurface7::DeleteAttachedSurface
2125 * Removes a surface from the attachment chain. The surface's refcount
2126 * is decreased by one after it has been removed
2128 * Params:
2129 * Flags: Some flags, not used by this implementation
2130 * Attach: Surface to detach
2132 * Returns:
2133 * DD_OK on success
2134 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
2136 *****************************************************************************/
2137 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
2138 struct ddraw_surface *attachment, IUnknown *detach_iface)
2140 struct wined3d_rendertarget_view *dsv;
2141 struct ddraw_surface *prev = surface;
2143 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
2145 wined3d_mutex_lock();
2146 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
2148 wined3d_mutex_unlock();
2149 return DDERR_CANNOTDETACHSURFACE;
2152 if (attachment->attached_iface != detach_iface)
2154 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
2155 wined3d_mutex_unlock();
2156 return DDERR_SURFACENOTATTACHED;
2159 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
2160 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2162 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2163 /* FIXME: we should probably also subtract from dwMipMapCount of this
2164 * and all parent surfaces */
2167 /* Find the predecessor of the detached surface */
2168 while (prev->next_attached != attachment)
2170 if (!(prev = prev->next_attached))
2172 ERR("Failed to find predecessor of %p.\n", attachment);
2173 wined3d_mutex_unlock();
2174 return DDERR_SURFACENOTATTACHED;
2178 /* Unchain the surface */
2179 prev->next_attached = attachment->next_attached;
2180 attachment->next_attached = NULL;
2181 attachment->first_attached = attachment;
2183 /* Check if the wined3d depth stencil needs updating. Note that we don't
2184 * just call d3d_device_update_depth_stencil() here since it uses
2185 * QueryInterface(). Some applications, SCP - Containment Breach in
2186 * particular, modify the QueryInterface() pointer in the surface vtbl
2187 * but don't cleanup properly after the relevant dll is unloaded. */
2188 dsv = wined3d_device_context_get_depth_stencil_view(surface->ddraw->immediate_context);
2189 if (attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER && dsv == attachment->wined3d_rtv)
2190 wined3d_device_context_set_depth_stencil_view(surface->ddraw->immediate_context, NULL);
2191 wined3d_mutex_unlock();
2193 /* Set attached_iface to NULL before releasing it, the surface may go
2194 * away. */
2195 attachment->attached_iface = NULL;
2196 IUnknown_Release(detach_iface);
2198 return DD_OK;
2201 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
2202 DWORD flags, IDirectDrawSurface7 *attachment)
2204 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2205 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
2207 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2209 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2212 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
2213 DWORD flags, IDirectDrawSurface4 *attachment)
2215 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2216 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
2218 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2220 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2223 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
2224 DWORD flags, IDirectDrawSurface3 *attachment)
2226 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2227 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2229 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2231 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2234 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
2235 DWORD flags, IDirectDrawSurface2 *attachment)
2237 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2238 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2240 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2242 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2245 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
2246 DWORD flags, IDirectDrawSurface *attachment)
2248 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2249 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2251 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2253 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2256 /*****************************************************************************
2257 * IDirectDrawSurface7::AddOverlayDirtyRect
2259 * "This method is not currently implemented"
2261 * Params:
2262 * Rect: ?
2264 * Returns:
2265 * DDERR_UNSUPPORTED
2267 *****************************************************************************/
2268 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
2270 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
2272 return DDERR_UNSUPPORTED; /* unchecked */
2275 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
2277 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2279 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2281 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2284 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
2286 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2288 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2290 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2293 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
2295 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2297 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2299 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2302 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
2304 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2306 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2308 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2311 /*****************************************************************************
2312 * IDirectDrawSurface7::GetDC
2314 * Returns a GDI device context for the surface
2316 * Params:
2317 * hdc: Address of a HDC variable to store the dc to
2319 * Returns:
2320 * DD_OK on success
2321 * DDERR_INVALIDPARAMS if hdc is NULL
2323 *****************************************************************************/
2324 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *dc)
2326 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2327 HRESULT hr = DD_OK;
2329 TRACE("iface %p, dc %p.\n", iface, dc);
2331 if (!dc)
2332 return DDERR_INVALIDPARAMS;
2334 wined3d_mutex_lock();
2335 if (surface->dc)
2336 hr = DDERR_DCALREADYCREATED;
2337 else if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2338 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE, 0);
2339 if (SUCCEEDED(hr))
2340 hr = wined3d_texture_get_dc(ddraw_surface_get_default_texture(surface, DDRAW_SURFACE_RW), surface->sub_resource_idx, dc);
2342 if (SUCCEEDED(hr))
2344 surface->dc = *dc;
2346 if (format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
2348 const struct ddraw_palette *palette;
2350 if (surface->palette)
2351 palette = surface->palette;
2352 else if (surface->ddraw->primary)
2353 palette = surface->ddraw->primary->palette;
2354 else
2355 palette = NULL;
2357 if (palette)
2358 wined3d_palette_apply_to_dc(palette->wined3d_palette, *dc);
2362 wined3d_mutex_unlock();
2363 switch (hr)
2365 /* Some, but not all errors set *dc to NULL. E.g. DCALREADYCREATED
2366 * does not touch *dc. */
2367 case WINED3DERR_INVALIDCALL:
2368 *dc = NULL;
2369 return DDERR_CANTCREATEDC;
2371 default:
2372 return hr;
2376 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
2378 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2380 TRACE("iface %p, dc %p.\n", iface, dc);
2382 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2385 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
2387 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2389 TRACE("iface %p, dc %p.\n", iface, dc);
2391 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2394 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2396 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2398 TRACE("iface %p, dc %p.\n", iface, dc);
2400 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2403 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2405 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2407 TRACE("iface %p, dc %p.\n", iface, dc);
2409 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2412 /*****************************************************************************
2413 * IDirectDrawSurface7::ReleaseDC
2415 * Releases the DC that was constructed with GetDC
2417 * Params:
2418 * hdc: HDC to release
2420 * Returns:
2421 * DD_OK on success, error code otherwise.
2423 *****************************************************************************/
2424 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2426 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2427 HRESULT hr;
2429 TRACE("iface %p, dc %p.\n", iface, hdc);
2431 wined3d_mutex_lock();
2432 if (!surface->dc)
2434 hr = DDERR_NODC;
2436 else if (SUCCEEDED(hr = wined3d_texture_release_dc(ddraw_surface_get_default_texture(surface, 0),
2437 surface->sub_resource_idx, hdc)))
2439 surface->dc = NULL;
2440 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2441 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE, 0);
2443 wined3d_mutex_unlock();
2446 return hr;
2449 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2451 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2453 TRACE("iface %p, dc %p.\n", iface, dc);
2455 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2458 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2460 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2462 TRACE("iface %p, dc %p.\n", iface, dc);
2464 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2467 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2469 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2471 TRACE("iface %p, dc %p.\n", iface, dc);
2473 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2476 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2478 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2480 TRACE("iface %p, dc %p.\n", iface, dc);
2482 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2485 /*****************************************************************************
2486 * IDirectDrawSurface7::GetCaps
2488 * Returns the surface's caps
2490 * Params:
2491 * Caps: Address to write the caps to
2493 * Returns:
2494 * DD_OK on success
2495 * DDERR_INVALIDPARAMS if Caps is NULL
2497 *****************************************************************************/
2498 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2500 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2502 TRACE("iface %p, caps %p.\n", iface, Caps);
2504 if(!Caps)
2505 return DDERR_INVALIDPARAMS;
2507 *Caps = surface->surface_desc.ddsCaps;
2509 return DD_OK;
2512 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2514 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2516 TRACE("iface %p, caps %p.\n", iface, caps);
2518 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2521 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2523 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2524 DDSCAPS2 caps2;
2525 HRESULT hr;
2527 TRACE("iface %p, caps %p.\n", iface, caps);
2529 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2530 if (FAILED(hr)) return hr;
2532 caps->dwCaps = caps2.dwCaps;
2533 return hr;
2536 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2538 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2539 DDSCAPS2 caps2;
2540 HRESULT hr;
2542 TRACE("iface %p, caps %p.\n", iface, caps);
2544 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2545 if (FAILED(hr)) return hr;
2547 caps->dwCaps = caps2.dwCaps;
2548 return hr;
2551 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2553 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2554 DDSCAPS2 caps2;
2555 HRESULT hr;
2557 TRACE("iface %p, caps %p.\n", iface, caps);
2559 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2560 if (FAILED(hr)) return hr;
2562 caps->dwCaps = caps2.dwCaps;
2563 return hr;
2566 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD priority)
2568 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2569 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2570 HRESULT hr;
2571 struct wined3d_resource *resource;
2573 TRACE("iface %p, priority %u.\n", iface, priority);
2575 wined3d_mutex_lock();
2576 /* No need to check for offscreen plain surfaces or mipmap sublevels. SetPriority
2577 * calls on such surfaces segfault on Windows. */
2578 if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed))
2580 WARN("Called on non-managed texture returning DDERR_INVALIDPARAMS.\n");
2581 hr = DDERR_INVALIDPARAMS;
2583 else
2585 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2586 wined3d_resource_set_priority(resource, priority);
2587 if (surface->draw_texture)
2588 wined3d_resource_set_priority(wined3d_texture_get_resource(surface->draw_texture), priority);
2590 hr = DD_OK;
2592 wined3d_mutex_unlock();
2594 return hr;
2597 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *priority)
2599 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2600 const struct wined3d_resource *resource;
2601 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2602 HRESULT hr;
2604 TRACE("iface %p, priority %p.\n", iface, priority);
2606 wined3d_mutex_lock();
2607 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN)
2609 WARN("Called on offscreenplain surface, returning DDERR_INVALIDOBJECT.\n");
2610 hr = DDERR_INVALIDOBJECT;
2612 else if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed) || !surface->is_complex_root)
2614 WARN("Called on non-managed texture or non-root surface, returning DDERR_INVALIDPARAMS.\n");
2615 hr = DDERR_INVALIDPARAMS;
2617 else
2619 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2620 *priority = wined3d_resource_get_priority(resource);
2621 hr = DD_OK;
2623 wined3d_mutex_unlock();
2625 return hr;
2628 /*****************************************************************************
2629 * IDirectDrawSurface7::SetPrivateData
2631 * Stores some data in the surface that is intended for the application's
2632 * use.
2634 * Params:
2635 * tag: GUID that identifies the data
2636 * Data: Pointer to the private data
2637 * Size: Size of the private data
2638 * Flags: Some flags
2640 * Returns:
2641 * D3D_OK on success, error code otherwise.
2643 *****************************************************************************/
2644 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2645 REFGUID tag, void *data, DWORD size, DWORD flags)
2647 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2648 HRESULT hr;
2650 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2651 iface, debugstr_guid(tag), data, size, flags);
2653 if (!data)
2655 WARN("data is NULL, returning DDERR_INVALIDPARAMS.\n");
2656 return DDERR_INVALIDPARAMS;
2659 wined3d_mutex_lock();
2660 hr = wined3d_private_store_set_private_data(&surface->private_store, tag, data, size, flags);
2661 wined3d_mutex_unlock();
2662 return hr_ddraw_from_wined3d(hr);
2665 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2666 REFGUID tag, void *data, DWORD size, DWORD flags)
2668 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2670 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2671 iface, debugstr_guid(tag), data, size, flags);
2673 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2676 /*****************************************************************************
2677 * IDirectDrawSurface7::GetPrivateData
2679 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2681 * Params:
2682 * tag: GUID of the data to return
2683 * Data: Address where to write the data to
2684 * Size: Size of the buffer at Data
2686 * Returns:
2687 * DD_OK on success
2688 * DDERR_INVALIDPARAMS if Data is NULL
2690 *****************************************************************************/
2691 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *data, DWORD *size)
2693 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2694 const struct wined3d_private_data *stored_data;
2695 HRESULT hr;
2697 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2698 iface, debugstr_guid(tag), data, size);
2700 wined3d_mutex_lock();
2701 stored_data = wined3d_private_store_get_private_data(&surface->private_store, tag);
2702 if (!stored_data)
2704 hr = DDERR_NOTFOUND;
2705 goto done;
2707 if (!size)
2709 hr = DDERR_INVALIDPARAMS;
2710 goto done;
2712 if (*size < stored_data->size)
2714 *size = stored_data->size;
2715 hr = DDERR_MOREDATA;
2716 goto done;
2718 if (!data)
2720 hr = DDERR_INVALIDPARAMS;
2721 goto done;
2724 *size = stored_data->size;
2725 memcpy(data, stored_data->content.data, stored_data->size);
2726 hr = DD_OK;
2728 done:
2729 wined3d_mutex_unlock();
2730 return hr;
2733 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2735 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2737 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2738 iface, debugstr_guid(tag), data, size);
2740 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2743 /*****************************************************************************
2744 * IDirectDrawSurface7::FreePrivateData
2746 * Frees private data stored in the surface
2748 * Params:
2749 * tag: Tag of the data to free
2751 * Returns:
2752 * D3D_OK on success, error code otherwise.
2754 *****************************************************************************/
2755 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2757 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2758 struct wined3d_private_data *entry;
2760 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2762 wined3d_mutex_lock();
2763 entry = wined3d_private_store_get_private_data(&surface->private_store, tag);
2764 if (!entry)
2766 wined3d_mutex_unlock();
2767 return DDERR_NOTFOUND;
2770 wined3d_private_store_free_private_data(&surface->private_store, entry);
2771 wined3d_mutex_unlock();
2773 return DD_OK;
2776 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2778 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2780 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2782 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2785 /*****************************************************************************
2786 * IDirectDrawSurface7::PageLock
2788 * Prevents a sysmem surface from being paged out
2790 * Params:
2791 * Flags: Not used, must be 0(unchecked)
2793 * Returns:
2794 * DD_OK, because it's a stub
2796 *****************************************************************************/
2797 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2799 TRACE("iface %p, flags %#x.\n", iface, Flags);
2801 /* This is Windows memory management related - we don't need this */
2802 return DD_OK;
2805 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2807 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2809 TRACE("iface %p, flags %#x.\n", iface, flags);
2811 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2814 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2816 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2818 TRACE("iface %p, flags %#x.\n", iface, flags);
2820 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2823 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2825 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2827 TRACE("iface %p, flags %#x.\n", iface, flags);
2829 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2832 /*****************************************************************************
2833 * IDirectDrawSurface7::PageUnlock
2835 * Allows a sysmem surface to be paged out
2837 * Params:
2838 * Flags: Not used, must be 0(unchecked)
2840 * Returns:
2841 * DD_OK, because it's a stub
2843 *****************************************************************************/
2844 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2846 TRACE("iface %p, flags %#x.\n", iface, Flags);
2848 return DD_OK;
2851 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2853 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2855 TRACE("iface %p, flags %#x.\n", iface, flags);
2857 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2860 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2862 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2864 TRACE("iface %p, flags %#x.\n", iface, flags);
2866 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2869 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2871 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2873 TRACE("iface %p, flags %#x.\n", iface, flags);
2875 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2878 /*****************************************************************************
2879 * IDirectDrawSurface7::BltBatch
2881 * An unimplemented function
2883 * Params:
2886 * Returns:
2887 * DDERR_UNSUPPORTED
2889 *****************************************************************************/
2890 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2892 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2894 /* MSDN: "not currently implemented" */
2895 return DDERR_UNSUPPORTED;
2898 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2900 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2902 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2904 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2907 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2909 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2911 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2913 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2916 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2918 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2920 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2922 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2925 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2927 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2929 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2931 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2934 /*****************************************************************************
2935 * IDirectDrawSurface7::EnumAttachedSurfaces
2937 * Enumerates all surfaces attached to this surface
2939 * Params:
2940 * context: Pointer to pass unmodified to the callback
2941 * cb: Callback function to call for each surface
2943 * Returns:
2944 * DD_OK on success
2945 * DDERR_INVALIDPARAMS if cb is NULL
2947 *****************************************************************************/
2948 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2949 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2951 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2952 struct ddraw_surface *surf;
2953 DDSURFACEDESC2 desc;
2954 int i;
2956 /* Attached surfaces aren't handled in WineD3D */
2957 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2959 if(!cb)
2960 return DDERR_INVALIDPARAMS;
2962 wined3d_mutex_lock();
2964 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2966 surf = surface->complex_array[i];
2967 if(!surf) break;
2969 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2970 desc = surf->surface_desc;
2971 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2972 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2974 wined3d_mutex_unlock();
2975 return DD_OK;
2979 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2981 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2982 desc = surf->surface_desc;
2983 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2984 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2986 wined3d_mutex_unlock();
2987 return DD_OK;
2991 TRACE(" end of enumeration.\n");
2993 wined3d_mutex_unlock();
2995 return DD_OK;
2998 struct callback_info2
3000 LPDDENUMSURFACESCALLBACK2 callback;
3001 void *context;
3004 struct callback_info
3006 LPDDENUMSURFACESCALLBACK callback;
3007 void *context;
3010 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
3012 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3013 const struct callback_info2 *info = context;
3015 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3016 ddraw_surface7_Release(surface);
3018 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
3021 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
3023 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3024 const struct callback_info *info = context;
3026 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
3027 ddraw_surface7_Release(surface);
3029 /* FIXME: Check surface_test.dwSize */
3030 return info->callback(&surface_impl->IDirectDrawSurface_iface,
3031 (DDSURFACEDESC *)surface_desc, info->context);
3034 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
3035 void *context, LPDDENUMSURFACESCALLBACK2 callback)
3037 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3038 struct callback_info2 info;
3040 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3042 info.callback = callback;
3043 info.context = context;
3045 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3046 &info, EnumCallback2);
3049 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
3050 void *context, LPDDENUMSURFACESCALLBACK callback)
3052 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3053 struct callback_info info;
3055 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3057 info.callback = callback;
3058 info.context = context;
3060 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3061 &info, EnumCallback);
3064 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
3065 void *context, LPDDENUMSURFACESCALLBACK callback)
3067 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3068 struct callback_info info;
3070 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3072 info.callback = callback;
3073 info.context = context;
3075 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3076 &info, EnumCallback);
3079 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
3080 void *context, LPDDENUMSURFACESCALLBACK callback)
3082 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3083 struct callback_info info;
3085 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3087 info.callback = callback;
3088 info.context = context;
3090 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3091 &info, EnumCallback);
3094 /*****************************************************************************
3095 * IDirectDrawSurface7::EnumOverlayZOrders
3097 * "Enumerates the overlay surfaces on the specified destination"
3099 * Params:
3100 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
3101 * context: context to pass back to the callback
3102 * cb: callback function to call for each enumerated surface
3104 * Returns:
3105 * DD_OK, because it's a stub
3107 *****************************************************************************/
3108 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
3109 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
3111 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
3113 return DD_OK;
3116 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
3117 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3119 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3120 struct callback_info2 info;
3122 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3124 info.callback = callback;
3125 info.context = context;
3127 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3128 flags, &info, EnumCallback2);
3131 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
3132 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3134 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3135 struct callback_info info;
3137 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3139 info.callback = callback;
3140 info.context = context;
3142 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3143 flags, &info, EnumCallback);
3146 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
3147 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3149 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3150 struct callback_info info;
3152 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3154 info.callback = callback;
3155 info.context = context;
3157 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3158 flags, &info, EnumCallback);
3161 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
3162 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3164 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3165 struct callback_info info;
3167 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3169 info.callback = callback;
3170 info.context = context;
3172 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3173 flags, &info, EnumCallback);
3176 /*****************************************************************************
3177 * IDirectDrawSurface7::GetBltStatus
3179 * Returns the blitting status
3181 * Params:
3182 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
3184 *****************************************************************************/
3185 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3187 TRACE("iface %p, flags %#x.\n", iface, Flags);
3189 switch (Flags)
3191 case WINEDDGBS_CANBLT:
3192 case WINEDDGBS_ISBLTDONE:
3193 return DD_OK;
3195 default:
3196 return DDERR_INVALIDPARAMS;
3200 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
3202 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3204 TRACE("iface %p, flags %#x.\n", iface, flags);
3206 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3209 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
3211 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3213 TRACE("iface %p, flags %#x.\n", iface, flags);
3215 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3218 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
3220 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3222 TRACE("iface %p, flags %#x.\n", iface, flags);
3224 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3227 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
3229 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3231 TRACE("iface %p, flags %#x.\n", iface, flags);
3233 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3236 /*****************************************************************************
3237 * IDirectDrawSurface7::GetColorKey
3239 * Returns the color key assigned to the surface
3241 * Params:
3242 * Flags: Some flags
3243 * CKey: Address to store the key to
3245 * Returns:
3246 * DD_OK on success
3247 * DDERR_INVALIDPARAMS if CKey is NULL
3249 *****************************************************************************/
3250 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
3252 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3254 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
3256 if(!CKey)
3257 return DDERR_INVALIDPARAMS;
3259 wined3d_mutex_lock();
3261 switch (Flags)
3263 case DDCKEY_DESTBLT:
3264 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
3266 wined3d_mutex_unlock();
3267 return DDERR_NOCOLORKEY;
3269 *CKey = This->surface_desc.ddckCKDestBlt;
3270 break;
3272 case DDCKEY_DESTOVERLAY:
3273 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
3275 wined3d_mutex_unlock();
3276 return DDERR_NOCOLORKEY;
3278 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
3279 break;
3281 case DDCKEY_SRCBLT:
3282 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
3284 wined3d_mutex_unlock();
3285 return DDERR_NOCOLORKEY;
3287 *CKey = This->surface_desc.ddckCKSrcBlt;
3288 break;
3290 case DDCKEY_SRCOVERLAY:
3291 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
3293 wined3d_mutex_unlock();
3294 return DDERR_NOCOLORKEY;
3296 *CKey = This->surface_desc.ddckCKSrcOverlay;
3297 break;
3299 default:
3300 wined3d_mutex_unlock();
3301 return DDERR_INVALIDPARAMS;
3304 wined3d_mutex_unlock();
3306 return DD_OK;
3309 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
3311 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3313 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3315 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3318 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
3320 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3322 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3324 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3327 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
3329 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3331 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3333 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3336 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
3338 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3340 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3342 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3345 /*****************************************************************************
3346 * IDirectDrawSurface7::GetFlipStatus
3348 * Returns the flipping status of the surface
3350 * Params:
3351 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
3353 *****************************************************************************/
3354 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3356 TRACE("iface %p, flags %#x.\n", iface, Flags);
3358 /* XXX: DDERR_INVALIDSURFACETYPE */
3360 switch (Flags)
3362 case WINEDDGFS_CANFLIP:
3363 case WINEDDGFS_ISFLIPDONE:
3364 return DD_OK;
3366 default:
3367 return DDERR_INVALIDPARAMS;
3371 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
3373 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3375 TRACE("iface %p, flags %#x.\n", iface, flags);
3377 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3380 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
3382 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3384 TRACE("iface %p, flags %#x.\n", iface, flags);
3386 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3389 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
3391 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3393 TRACE("iface %p, flags %#x.\n", iface, flags);
3395 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3398 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
3400 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3402 TRACE("iface %p, flags %#x.\n", iface, flags);
3404 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3407 /*****************************************************************************
3408 * IDirectDrawSurface7::GetOverlayPosition
3410 * Returns the display coordinates of a visible and active overlay surface
3412 * Params:
3416 * Returns:
3417 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3418 *****************************************************************************/
3419 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *x, LONG *y)
3421 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3422 HRESULT hr;
3424 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3426 wined3d_mutex_lock();
3427 hr = wined3d_texture_get_overlay_position(surface->wined3d_texture,
3428 surface->sub_resource_idx, x, y);
3429 wined3d_mutex_unlock();
3431 return hr;
3434 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3436 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3438 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3440 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3443 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3445 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3447 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3449 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3452 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3454 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3456 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3458 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3461 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3463 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3465 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3467 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3470 /*****************************************************************************
3471 * IDirectDrawSurface7::GetPixelFormat
3473 * Returns the pixel format of the Surface
3475 * Params:
3476 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3477 * format should be written
3479 * Returns:
3480 * DD_OK on success
3481 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3483 *****************************************************************************/
3484 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3486 /* What is DDERR_INVALIDSURFACETYPE for here? */
3487 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3489 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3491 if(!PixelFormat)
3492 return DDERR_INVALIDPARAMS;
3494 wined3d_mutex_lock();
3495 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3496 wined3d_mutex_unlock();
3498 return DD_OK;
3501 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3503 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3505 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3507 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3510 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3512 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3514 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3516 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3519 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3521 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3523 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3525 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3528 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3530 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3532 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3534 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3537 /*****************************************************************************
3538 * IDirectDrawSurface7::GetSurfaceDesc
3540 * Returns the description of this surface
3542 * Params:
3543 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3544 * surface desc
3546 * Returns:
3547 * DD_OK on success
3548 * DDERR_INVALIDPARAMS if DDSD is NULL
3550 *****************************************************************************/
3551 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3553 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3555 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3557 if(!DDSD)
3558 return DDERR_INVALIDPARAMS;
3560 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3562 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3563 return DDERR_INVALIDPARAMS;
3566 wined3d_mutex_lock();
3567 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3568 TRACE("Returning surface desc:\n");
3569 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3570 wined3d_mutex_unlock();
3572 return DD_OK;
3575 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3577 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3579 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3581 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3584 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3586 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3588 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3590 if (!surface_desc) return DDERR_INVALIDPARAMS;
3592 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3594 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3595 return DDERR_INVALIDPARAMS;
3598 wined3d_mutex_lock();
3599 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3600 TRACE("Returning surface desc:\n");
3601 if (TRACE_ON(ddraw))
3603 /* DDRAW_dump_surface_desc handles the smaller size */
3604 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3606 wined3d_mutex_unlock();
3608 return DD_OK;
3611 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3613 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3615 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3617 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3620 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3622 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3624 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3626 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3629 /*****************************************************************************
3630 * IDirectDrawSurface7::Initialize
3632 * Initializes the surface. This is a no-op in Wine
3634 * Params:
3635 * DD: Pointer to an DirectDraw interface
3636 * DDSD: Surface description for initialization
3638 * Returns:
3639 * DDERR_ALREADYINITIALIZED
3641 *****************************************************************************/
3642 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3643 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3645 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3647 return DDERR_ALREADYINITIALIZED;
3650 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3651 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3653 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3655 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3657 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3658 ddraw, surface_desc);
3661 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3662 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3664 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3665 DDSURFACEDESC2 surface_desc2;
3667 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3669 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3670 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3671 ddraw, surface_desc ? &surface_desc2 : NULL);
3674 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3675 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3677 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3678 DDSURFACEDESC2 surface_desc2;
3680 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3682 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3683 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3684 ddraw, surface_desc ? &surface_desc2 : NULL);
3687 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3688 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3690 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3691 DDSURFACEDESC2 surface_desc2;
3693 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3695 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3696 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3697 ddraw, surface_desc ? &surface_desc2 : NULL);
3700 /*****************************************************************************
3701 * IDirect3DTexture1::Initialize
3703 * The sdk says it's not implemented
3705 * Params:
3708 * Returns
3709 * DDERR_UNSUPPORTED
3711 *****************************************************************************/
3712 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3713 IDirect3DDevice *device, IDirectDrawSurface *surface)
3715 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3717 return DDERR_UNSUPPORTED; /* Unchecked */
3720 /*****************************************************************************
3721 * IDirectDrawSurface7::IsLost
3723 * Checks if the surface is lost
3725 * Returns:
3726 * DD_OK, if the surface is usable
3727 * DDERR_ISLOST if the surface is lost
3729 *****************************************************************************/
3730 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3732 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3734 TRACE("iface %p.\n", iface);
3736 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3739 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3741 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3743 TRACE("iface %p.\n", iface);
3745 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3748 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3750 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3752 TRACE("iface %p.\n", iface);
3754 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3757 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3759 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3761 TRACE("iface %p.\n", iface);
3763 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3766 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3768 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3770 TRACE("iface %p.\n", iface);
3772 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3775 /*****************************************************************************
3776 * IDirectDrawSurface7::Restore
3778 * Restores a lost surface. This makes the surface usable again, but
3779 * doesn't reload its old contents
3781 * Returns:
3782 * DD_OK on success, error code otherwise.
3784 *****************************************************************************/
3785 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3787 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3788 unsigned int i;
3790 TRACE("iface %p.\n", iface);
3792 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3794 struct wined3d_swapchain *swapchain = surface->ddraw->wined3d_swapchain;
3795 struct wined3d_sub_resource_desc wined3d_desc;
3796 struct wined3d_display_mode mode;
3797 HRESULT hr;
3799 if (FAILED(hr = wined3d_swapchain_get_display_mode(swapchain, &mode, NULL)))
3801 WARN("Failed to get display mode, hr %#x.\n", hr);
3802 return hr;
3805 if (FAILED(hr = wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, 0, &wined3d_desc)))
3807 WARN("Failed to get resource desc, hr %#x.\n", hr);
3808 return hr;
3811 if (mode.width != wined3d_desc.width || mode.height != wined3d_desc.height)
3813 WARN("Display mode dimensions %ux%u don't match surface dimensions %ux%u.\n",
3814 mode.width, mode.height, wined3d_desc.width, wined3d_desc.height);
3815 return DDERR_WRONGMODE;
3818 if (mode.format_id != wined3d_desc.format)
3820 WARN("Display mode format %#x doesn't match surface format %#x.\n",
3821 mode.format_id, wined3d_desc.format);
3822 return DDERR_WRONGMODE;
3826 if (!ddraw_surface_can_be_lost(surface))
3827 return DD_OK;
3828 ddraw_update_lost_surfaces(surface->ddraw);
3829 if (surface->ddraw->device_state == DDRAW_DEVICE_STATE_LOST)
3830 return DDERR_WRONGMODE;
3832 surface->is_lost = FALSE;
3833 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
3835 if (surface->complex_array[i])
3836 surface->complex_array[i]->is_lost = FALSE;
3839 return DD_OK;
3842 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3844 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3846 TRACE("iface %p.\n", iface);
3848 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3851 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3853 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3855 TRACE("iface %p.\n", iface);
3857 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3860 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3862 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3864 TRACE("iface %p.\n", iface);
3866 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3869 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3871 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3873 TRACE("iface %p.\n", iface);
3875 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3878 /*****************************************************************************
3879 * IDirectDrawSurface7::SetOverlayPosition
3881 * Changes the display coordinates of an overlay surface
3883 * Params:
3884 * X:
3885 * Y:
3887 * Returns:
3888 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3889 *****************************************************************************/
3890 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG x, LONG y)
3892 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3893 HRESULT hr;
3895 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3897 wined3d_mutex_lock();
3898 hr = wined3d_texture_set_overlay_position(surface->wined3d_texture,
3899 surface->sub_resource_idx, x, y);
3900 wined3d_mutex_unlock();
3902 return hr;
3905 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3907 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3909 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3911 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3914 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3916 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3918 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3920 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3923 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3925 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3927 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3929 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3932 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3934 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3936 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3938 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3941 /*****************************************************************************
3942 * IDirectDrawSurface7::UpdateOverlay
3944 * Modifies the attributes of an overlay surface.
3946 * Params:
3947 * SrcRect: The section of the source being used for the overlay
3948 * DstSurface: Address of the surface that is overlaid
3949 * DstRect: Place of the overlay
3950 * Flags: some DDOVER_* flags
3952 * Returns:
3953 * DDERR_UNSUPPORTED, because we don't support overlays
3955 *****************************************************************************/
3956 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *src_rect,
3957 IDirectDrawSurface7 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3959 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3960 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(dst_surface);
3961 struct wined3d_texture *dst_wined3d_texture = NULL;
3962 unsigned int dst_sub_resource_idx = 0;
3963 HRESULT hr;
3965 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3966 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3968 if (fx)
3969 FIXME("Ignoring fx %p.\n", fx);
3971 wined3d_mutex_lock();
3972 if (dst_impl)
3974 dst_wined3d_texture = dst_impl->wined3d_texture;
3975 dst_sub_resource_idx = dst_impl->sub_resource_idx;
3977 hr = wined3d_texture_update_overlay(src_impl->wined3d_texture, src_impl->sub_resource_idx,
3978 src_rect, dst_wined3d_texture, dst_sub_resource_idx, dst_rect, flags);
3979 wined3d_mutex_unlock();
3981 return hr_ddraw_from_wined3d(hr);
3984 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3985 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3987 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3988 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3990 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3991 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3993 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3994 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3997 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3998 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4000 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
4001 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
4003 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4004 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4006 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4007 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4010 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
4011 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4013 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
4014 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
4016 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4017 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4019 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4020 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4023 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
4024 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4026 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
4027 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
4029 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4030 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4032 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4033 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4036 /*****************************************************************************
4037 * IDirectDrawSurface7::UpdateOverlayDisplay
4039 * The DX7 sdk says that it's not implemented
4041 * Params:
4042 * Flags: ?
4044 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
4046 *****************************************************************************/
4047 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
4049 TRACE("iface %p, flags %#x.\n", iface, Flags);
4051 return DDERR_UNSUPPORTED;
4054 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
4056 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4058 TRACE("iface %p, flags %#x.\n", iface, flags);
4060 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4063 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
4065 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4067 TRACE("iface %p, flags %#x.\n", iface, flags);
4069 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4072 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
4074 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4076 TRACE("iface %p, flags %#x.\n", iface, flags);
4078 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4081 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
4083 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4085 TRACE("iface %p, flags %#x.\n", iface, flags);
4087 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4090 /*****************************************************************************
4091 * IDirectDrawSurface7::UpdateOverlayZOrder
4093 * Sets an overlay's Z order
4095 * Params:
4096 * Flags: DDOVERZ_* flags
4097 * DDSRef: Defines the relative position in the overlay chain
4099 * Returns:
4100 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
4102 *****************************************************************************/
4103 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
4104 DWORD flags, IDirectDrawSurface7 *reference)
4106 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4108 FIXME("iface %p, flags %#x, reference %p stub!\n", iface, flags, reference);
4110 wined3d_mutex_lock();
4111 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OVERLAY))
4113 WARN("Not an overlay surface.\n");
4114 wined3d_mutex_unlock();
4115 return DDERR_NOTAOVERLAYSURFACE;
4117 wined3d_mutex_unlock();
4119 return DD_OK;
4122 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
4123 DWORD flags, IDirectDrawSurface4 *reference)
4125 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4126 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
4128 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4130 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4131 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4134 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
4135 DWORD flags, IDirectDrawSurface3 *reference)
4137 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4138 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
4140 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4142 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4143 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4146 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
4147 DWORD flags, IDirectDrawSurface2 *reference)
4149 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4150 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
4152 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4154 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4155 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4158 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
4159 DWORD flags, IDirectDrawSurface *reference)
4161 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4162 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
4164 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4166 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4167 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4170 /*****************************************************************************
4171 * IDirectDrawSurface7::GetDDInterface
4173 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
4174 * surface belongs to
4176 * Params:
4177 * DD: Address to write the interface pointer to
4179 * Returns:
4180 * DD_OK on success
4181 * DDERR_INVALIDPARAMS if DD is NULL
4183 *****************************************************************************/
4184 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
4186 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4188 TRACE("iface %p, ddraw %p.\n", iface, DD);
4190 if(!DD)
4191 return DDERR_INVALIDPARAMS;
4193 switch(This->version)
4195 case 7:
4196 *DD = &This->ddraw->IDirectDraw7_iface;
4197 break;
4199 case 4:
4200 *DD = &This->ddraw->IDirectDraw4_iface;
4201 break;
4203 case 2:
4204 *DD = &This->ddraw->IDirectDraw2_iface;
4205 break;
4207 case 1:
4208 *DD = &This->ddraw->IDirectDraw_iface;
4209 break;
4212 IUnknown_AddRef((IUnknown *)*DD);
4214 return DD_OK;
4217 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
4219 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4221 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4223 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4226 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
4228 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4230 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4232 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4235 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
4237 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4239 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4241 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4244 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
4246 TRACE("iface %p.\n", iface);
4248 return DD_OK;
4251 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
4253 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4255 TRACE("iface %p.\n", iface);
4257 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
4260 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
4262 TRACE("iface %p, value %p.\n", iface, pValue);
4264 *pValue = 0;
4266 return DD_OK;
4269 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
4271 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4273 TRACE("iface %p, value %p.\n", iface, pValue);
4275 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
4278 /*****************************************************************************
4279 * IDirectDrawSurface7::SetLOD
4281 * Sets the level of detail of a texture
4283 * Params:
4284 * MaxLOD: LOD to set
4286 * Returns:
4287 * DD_OK on success
4288 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4290 *****************************************************************************/
4291 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
4293 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4294 HRESULT hr;
4296 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
4298 wined3d_mutex_lock();
4299 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4301 wined3d_mutex_unlock();
4302 return DDERR_INVALIDOBJECT;
4305 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
4306 if (SUCCEEDED(hr) && surface->draw_texture)
4307 hr = wined3d_texture_set_lod(surface->draw_texture, MaxLOD);
4308 wined3d_mutex_unlock();
4310 return hr;
4313 /*****************************************************************************
4314 * IDirectDrawSurface7::GetLOD
4316 * Returns the level of detail of a Direct3D texture
4318 * Params:
4319 * MaxLOD: Address to write the LOD to
4321 * Returns:
4322 * DD_OK on success
4323 * DDERR_INVALIDPARAMS if MaxLOD is NULL
4324 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4326 *****************************************************************************/
4327 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4329 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4331 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4333 if(!MaxLOD)
4334 return DDERR_INVALIDPARAMS;
4336 wined3d_mutex_lock();
4337 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4339 wined3d_mutex_unlock();
4340 return DDERR_INVALIDOBJECT;
4343 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4344 wined3d_mutex_unlock();
4346 return DD_OK;
4349 /*****************************************************************************
4350 * IDirectDrawSurface7::BltFast
4352 * Performs a fast Blit.
4354 * Params:
4355 * dstx: The x coordinate to blit to on the destination
4356 * dsty: The y coordinate to blit to on the destination
4357 * Source: The source surface
4358 * rsrc: The source rectangle
4359 * trans: Type of transfer. Some DDBLTFAST_* flags
4361 * Returns:
4362 * DD_OK on success, error code otherwise.
4364 *****************************************************************************/
4365 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltFast(IDirectDrawSurface7 *iface,
4366 DWORD dst_x, DWORD dst_y, IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD trans)
4368 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
4369 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src_surface);
4370 DWORD flags = WINED3D_BLT_SYNCHRONOUS;
4371 DWORD src_w, src_h, dst_w, dst_h;
4372 HRESULT hr = DD_OK;
4373 RECT dst_rect, s;
4375 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4376 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), trans);
4378 dst_w = dst_impl->surface_desc.dwWidth;
4379 dst_h = dst_impl->surface_desc.dwHeight;
4381 if (!src_rect)
4383 SetRect(&s, 0, 0, src_impl->surface_desc.dwWidth, src_impl->surface_desc.dwHeight);
4384 src_rect = &s;
4387 src_w = src_rect->right - src_rect->left;
4388 src_h = src_rect->bottom - src_rect->top;
4389 if (src_w > dst_w || dst_x > dst_w - src_w
4390 || src_h > dst_h || dst_y > dst_h - src_h)
4392 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4393 return DDERR_INVALIDRECT;
4396 SetRect(&dst_rect, dst_x, dst_y, dst_x + src_w, dst_y + src_h);
4397 if (trans & DDBLTFAST_SRCCOLORKEY)
4398 flags |= WINED3D_BLT_SRC_CKEY;
4399 if (trans & DDBLTFAST_DESTCOLORKEY)
4400 flags |= WINED3D_BLT_DST_CKEY;
4401 if (trans & DDBLTFAST_WAIT)
4402 flags |= WINED3D_BLT_WAIT;
4403 if (trans & DDBLTFAST_DONOTWAIT)
4404 flags |= WINED3D_BLT_DO_NOT_WAIT;
4406 wined3d_mutex_lock();
4407 if (dst_impl->clipper)
4409 wined3d_mutex_unlock();
4410 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4411 return DDERR_BLTFASTCANTCLIP;
4414 if (src_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4415 hr = ddraw_surface_update_frontbuffer(src_impl, src_rect, TRUE, 0);
4416 if (SUCCEEDED(hr))
4417 hr = wined3d_device_context_blt(dst_impl->ddraw->immediate_context,
4418 ddraw_surface_get_any_texture(dst_impl, DDRAW_SURFACE_RW), dst_impl->sub_resource_idx, &dst_rect,
4419 ddraw_surface_get_any_texture(src_impl,DDRAW_SURFACE_READ), src_impl->sub_resource_idx, src_rect,
4420 flags, NULL, WINED3D_TEXF_POINT);
4421 if (SUCCEEDED(hr) && (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4422 hr = ddraw_surface_update_frontbuffer(dst_impl, &dst_rect, FALSE, 0);
4423 wined3d_mutex_unlock();
4425 switch(hr)
4427 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4428 default: return hr;
4432 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4433 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4435 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4436 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4438 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4439 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4441 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4442 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4445 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4446 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4448 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4449 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4451 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4452 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4454 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4455 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4458 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4459 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4461 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4462 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4464 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4465 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4467 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4468 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4471 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4472 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4474 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4475 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4477 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4478 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4480 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4481 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4484 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **clipper)
4486 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4488 TRACE("iface %p, clipper %p.\n", iface, clipper);
4490 if (!clipper)
4491 return DDERR_INVALIDPARAMS;
4493 wined3d_mutex_lock();
4494 if (!surface->clipper)
4496 wined3d_mutex_unlock();
4497 *clipper = NULL;
4498 return DDERR_NOCLIPPERATTACHED;
4501 *clipper = &surface->clipper->IDirectDrawClipper_iface;
4502 if (ddraw_clipper_is_valid(surface->clipper))
4503 IDirectDrawClipper_AddRef(*clipper);
4504 wined3d_mutex_unlock();
4506 return DD_OK;
4509 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4511 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4513 TRACE("iface %p, clipper %p.\n", iface, clipper);
4515 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4518 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4520 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4522 TRACE("iface %p, clipper %p.\n", iface, clipper);
4524 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4527 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4529 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4531 TRACE("iface %p, clipper %p.\n", iface, clipper);
4533 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4536 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4538 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4540 TRACE("iface %p, clipper %p.\n", iface, clipper);
4542 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4545 /*****************************************************************************
4546 * IDirectDrawSurface7::SetClipper
4548 * Sets a clipper for the surface
4550 * Params:
4551 * Clipper: IDirectDrawClipper interface of the clipper to set
4553 * Returns:
4554 * DD_OK on success
4556 *****************************************************************************/
4557 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4558 IDirectDrawClipper *iclipper)
4560 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4561 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4562 struct ddraw_clipper *old_clipper = This->clipper;
4563 HWND clipWindow;
4565 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4567 wined3d_mutex_lock();
4568 if (clipper == This->clipper)
4570 wined3d_mutex_unlock();
4571 return DD_OK;
4574 This->clipper = clipper;
4576 if (clipper != NULL)
4577 IDirectDrawClipper_AddRef(iclipper);
4578 if (old_clipper && ddraw_clipper_is_valid(old_clipper))
4579 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4581 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4583 clipWindow = NULL;
4584 if(clipper) {
4585 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4588 if (clipWindow)
4590 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4591 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4593 else
4595 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4596 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4600 wined3d_mutex_unlock();
4602 return DD_OK;
4605 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4607 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4609 TRACE("iface %p, clipper %p.\n", iface, clipper);
4611 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4614 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4616 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4618 TRACE("iface %p, clipper %p.\n", iface, clipper);
4620 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4623 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4625 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4627 TRACE("iface %p, clipper %p.\n", iface, clipper);
4629 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4632 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4634 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4636 TRACE("iface %p, clipper %p.\n", iface, clipper);
4638 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4641 /*****************************************************************************
4642 * IDirectDrawSurface7::SetSurfaceDesc
4644 * Sets the surface description. It can override the pixel format, the surface
4645 * memory, ...
4646 * It's not really tested.
4648 * Params:
4649 * DDSD: Pointer to the new surface description to set
4650 * Flags: Some flags
4652 * Returns:
4653 * DD_OK on success
4654 * DDERR_INVALIDPARAMS if DDSD is NULL
4656 *****************************************************************************/
4657 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4659 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4660 HRESULT hr;
4661 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4662 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4663 enum wined3d_format_id format_id;
4664 UINT pitch, width, height;
4666 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4668 if (!DDSD)
4670 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4671 return DDERR_INVALIDPARAMS;
4673 if (Flags)
4675 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4676 return DDERR_INVALIDPARAMS;
4678 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4679 || surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4680 || surface->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4682 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4683 return DDERR_INVALIDSURFACETYPE;
4686 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4687 * for PIXELFORMAT to work */
4688 if (DDSD->dwFlags & ~allowed_flags)
4690 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4691 return DDERR_INVALIDPARAMS;
4693 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4695 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4696 return DDERR_INVALIDPARAMS;
4698 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4700 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4701 return DDERR_INVALIDCAPS;
4703 if (DDSD->dwFlags & DDSD_WIDTH)
4705 if (!(DDSD->dwFlags & DDSD_PITCH))
4707 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4708 return DDERR_INVALIDPARAMS;
4710 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4712 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4713 DDSD->u1.lPitch, DDSD->dwWidth);
4714 return DDERR_INVALIDPARAMS;
4716 if (DDSD->dwWidth != surface->surface_desc.dwWidth)
4717 TRACE("Surface width changed from %u to %u.\n", surface->surface_desc.dwWidth, DDSD->dwWidth);
4718 if (DDSD->u1.lPitch != surface->surface_desc.u1.lPitch)
4719 TRACE("Surface pitch changed from %u to %u.\n", surface->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4720 pitch = DDSD->u1.lPitch;
4721 width = DDSD->dwWidth;
4723 else if (DDSD->dwFlags & DDSD_PITCH)
4725 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4726 return DDERR_INVALIDPARAMS;
4728 else
4730 pitch = surface->surface_desc.u1.lPitch;
4731 width = surface->surface_desc.dwWidth;
4734 if (DDSD->dwFlags & DDSD_HEIGHT)
4736 if (!DDSD->dwHeight)
4738 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4739 return DDERR_INVALIDPARAMS;
4741 if (DDSD->dwHeight != surface->surface_desc.dwHeight)
4742 TRACE("Surface height changed from %u to %u.\n", surface->surface_desc.dwHeight, DDSD->dwHeight);
4743 height = DDSD->dwHeight;
4745 else
4747 height = surface->surface_desc.dwHeight;
4750 wined3d_mutex_lock();
4751 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4753 enum wined3d_format_id current_format_id;
4754 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4756 if (format_id == WINED3DFMT_UNKNOWN)
4758 ERR("Requested to set an unknown pixelformat\n");
4759 wined3d_mutex_unlock();
4760 return DDERR_INVALIDPARAMS;
4762 current_format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4763 if (format_id != current_format_id)
4764 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4766 else
4768 format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4771 if (FAILED(hr = wined3d_texture_update_desc(surface->wined3d_texture, surface->sub_resource_idx,
4772 width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4774 WARN("Failed to update surface desc, hr %#x.\n", hr);
4775 wined3d_mutex_unlock();
4776 return hr_ddraw_from_wined3d(hr);
4779 if (surface->draw_texture && FAILED(hr = wined3d_texture_update_desc(surface->draw_texture,
4780 surface->sub_resource_idx, width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, NULL, 0)))
4782 ERR("Failed to update surface desc for draw_texture, hr %#x.\n", hr);
4783 wined3d_mutex_unlock();
4784 return hr_ddraw_from_wined3d(hr);
4787 if (DDSD->dwFlags & DDSD_WIDTH)
4788 surface->surface_desc.dwWidth = width;
4789 if (DDSD->dwFlags & DDSD_PITCH)
4790 surface->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4791 if (DDSD->dwFlags & DDSD_HEIGHT)
4792 surface->surface_desc.dwHeight = height;
4793 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4794 surface->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4796 wined3d_mutex_unlock();
4798 return DD_OK;
4801 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4802 DDSURFACEDESC2 *surface_desc, DWORD flags)
4804 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4806 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4808 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4809 surface_desc, flags);
4812 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4813 DDSURFACEDESC *surface_desc, DWORD flags)
4815 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4816 DDSURFACEDESC2 surface_desc2;
4818 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4820 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4821 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4822 surface_desc ? &surface_desc2 : NULL, flags);
4825 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4827 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4828 struct ddraw_palette *palette_impl;
4829 HRESULT hr = DD_OK;
4831 TRACE("iface %p, palette %p.\n", iface, palette);
4833 if (!palette)
4834 return DDERR_INVALIDPARAMS;
4835 if (ddraw_surface_is_lost(surface))
4837 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4838 return DDERR_SURFACELOST;
4841 wined3d_mutex_lock();
4842 if ((palette_impl = surface->palette))
4844 *palette = &palette_impl->IDirectDrawPalette_iface;
4845 IDirectDrawPalette_AddRef(*palette);
4847 else
4849 *palette = NULL;
4850 hr = DDERR_NOPALETTEATTACHED;
4852 wined3d_mutex_unlock();
4854 return hr;
4857 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4859 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4861 TRACE("iface %p, palette %p.\n", iface, palette);
4863 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4866 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4868 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4870 TRACE("iface %p, palette %p.\n", iface, palette);
4872 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4875 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4877 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4879 TRACE("iface %p, palette %p.\n", iface, palette);
4881 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4884 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4886 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4888 TRACE("iface %p, palette %p.\n", iface, palette);
4890 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4893 static HRESULT ddraw_surface_set_wined3d_textures_colour_key(struct ddraw_surface *surface, DWORD flags,
4894 struct wined3d_color_key *color_key)
4896 HRESULT hr;
4898 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags, color_key);
4899 if (surface->draw_texture && SUCCEEDED(hr))
4900 hr = wined3d_texture_set_color_key(surface->draw_texture, flags, color_key);
4902 return hr;
4905 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4907 DDCOLORKEY fixed_color_key;
4908 HRESULT hr = WINED3D_OK;
4910 if (flags & DDCKEY_COLORSPACE)
4912 if (color_key && color_key->dwColorSpaceLowValue != color_key->dwColorSpaceHighValue)
4914 WARN("Range color keys are not supported, returning DDERR_NOCOLORKEYHW.\n");
4915 return DDERR_NOCOLORKEYHW;
4917 flags &= ~DDCKEY_COLORSPACE;
4920 wined3d_mutex_lock();
4922 if (color_key)
4924 fixed_color_key.dwColorSpaceLowValue = fixed_color_key.dwColorSpaceHighValue = color_key->dwColorSpaceLowValue;
4925 switch (flags & ~DDCKEY_COLORSPACE)
4927 case DDCKEY_DESTBLT:
4928 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4929 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4930 break;
4932 case DDCKEY_DESTOVERLAY:
4933 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4934 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4935 break;
4937 case DDCKEY_SRCOVERLAY:
4938 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4939 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4940 break;
4942 case DDCKEY_SRCBLT:
4943 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4944 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4945 break;
4947 default:
4948 wined3d_mutex_unlock();
4949 return DDERR_INVALIDPARAMS;
4952 else
4954 switch (flags & ~DDCKEY_COLORSPACE)
4956 case DDCKEY_DESTBLT:
4957 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4958 break;
4960 case DDCKEY_DESTOVERLAY:
4961 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4962 break;
4964 case DDCKEY_SRCOVERLAY:
4965 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4966 break;
4968 case DDCKEY_SRCBLT:
4969 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4970 break;
4972 default:
4973 wined3d_mutex_unlock();
4974 return DDERR_INVALIDPARAMS;
4978 if (surface->is_complex_root)
4979 hr = ddraw_surface_set_wined3d_textures_colour_key(surface, flags,
4980 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4982 wined3d_mutex_unlock();
4984 return hr_ddraw_from_wined3d(hr);
4987 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4989 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4991 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4993 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4994 return DDERR_NOTONMIPMAPSUBLEVEL;
4996 return ddraw_surface_set_color_key(surface, flags, color_key);
4999 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
5001 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(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_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
5010 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(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_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
5019 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(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_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
5028 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5030 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5032 return ddraw_surface_set_color_key(surface, flags, color_key);
5035 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
5037 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
5039 TRACE("iface %p, palette %p.\n", iface, palette);
5041 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
5042 return DDERR_NOTONMIPMAPSUBLEVEL;
5043 if (ddraw_surface_is_lost(surface))
5045 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5046 return DDERR_SURFACELOST;
5049 return ddraw_surface_set_palette(surface, palette);
5052 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
5054 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
5056 TRACE("iface %p, palette %p.\n", iface, palette);
5058 if (ddraw_surface_is_lost(surface))
5060 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5061 return DDERR_SURFACELOST;
5064 return ddraw_surface_set_palette(surface, palette);
5067 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
5069 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
5071 TRACE("iface %p, palette %p.\n", iface, palette);
5073 if (ddraw_surface_is_lost(surface))
5075 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5076 return DDERR_SURFACELOST;
5079 return ddraw_surface_set_palette(surface, palette);
5082 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
5084 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
5086 TRACE("iface %p, palette %p.\n", iface, palette);
5088 if (ddraw_surface_is_lost(surface))
5090 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5091 return DDERR_SURFACELOST;
5094 return ddraw_surface_set_palette(surface, palette);
5097 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
5099 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5101 TRACE("iface %p, palette %p.\n", iface, palette);
5103 if (ddraw_surface_is_lost(surface))
5105 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5106 return DDERR_SURFACELOST;
5109 return ddraw_surface_set_palette(surface, palette);
5112 /**********************************************************
5113 * IDirectDrawGammaControl::GetGammaRamp
5115 * Returns the current gamma ramp for a surface
5117 * Params:
5118 * flags: Ignored
5119 * gamma_ramp: Address to write the ramp to
5121 * Returns:
5122 * DD_OK on success
5123 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5125 **********************************************************/
5126 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
5127 DWORD flags, DDGAMMARAMP *gamma_ramp)
5129 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5131 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5133 if (!gamma_ramp)
5135 WARN("Invalid gamma_ramp passed.\n");
5136 return DDERR_INVALIDPARAMS;
5139 wined3d_mutex_lock();
5140 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5142 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5143 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
5145 else
5147 ERR("Not implemented for non-primary surfaces.\n");
5149 wined3d_mutex_unlock();
5151 return DD_OK;
5154 /**********************************************************
5155 * IDirectDrawGammaControl::SetGammaRamp
5157 * Sets the red, green and blue gamma ramps for
5159 * Params:
5160 * flags: Can be DDSGR_CALIBRATE to request calibration
5161 * gamma_ramp: Structure containing the new gamma ramp
5163 * Returns:
5164 * DD_OK on success
5165 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5167 **********************************************************/
5168 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
5169 DWORD flags, DDGAMMARAMP *gamma_ramp)
5171 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5173 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5175 if (!gamma_ramp)
5177 WARN("Invalid gamma_ramp passed.\n");
5178 return DDERR_INVALIDPARAMS;
5181 wined3d_mutex_lock();
5182 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5184 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5185 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
5186 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
5188 else
5190 ERR("Not implemented for non-primary surfaces.\n");
5192 wined3d_mutex_unlock();
5194 return DD_OK;
5197 /*****************************************************************************
5198 * IDirect3DTexture2::PaletteChanged
5200 * Informs the texture about a palette change
5202 * Params:
5203 * start: Start index of the change
5204 * count: The number of changed entries
5206 * Returns
5207 * D3D_OK, because it's a stub
5209 *****************************************************************************/
5210 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
5212 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
5214 return D3D_OK;
5217 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
5219 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5221 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
5223 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
5226 /*****************************************************************************
5227 * IDirect3DTexture::Unload
5229 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
5232 * Returns:
5233 * DDERR_UNSUPPORTED
5235 *****************************************************************************/
5236 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
5238 WARN("iface %p. Not implemented.\n", iface);
5240 return DDERR_UNSUPPORTED;
5243 /*****************************************************************************
5244 * IDirect3DTexture2::GetHandle
5246 * Returns handle for the texture.
5248 * Params:
5249 * device: Device this handle is assigned to
5250 * handle: Address to store the handle at.
5252 * Returns:
5253 * D3D_OK
5255 *****************************************************************************/
5256 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
5257 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
5259 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
5260 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
5262 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5264 wined3d_mutex_lock();
5266 if (!surface->Handle)
5268 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
5269 if (h == DDRAW_INVALID_HANDLE)
5271 ERR("Failed to allocate a texture handle.\n");
5272 wined3d_mutex_unlock();
5273 return DDERR_OUTOFMEMORY;
5276 surface->Handle = h + 1;
5279 TRACE("Returning handle %08x.\n", surface->Handle);
5280 *handle = surface->Handle;
5282 wined3d_mutex_unlock();
5284 return D3D_OK;
5287 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
5288 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
5290 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5291 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
5293 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5295 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
5296 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
5299 /*****************************************************************************
5300 * get_sub_mimaplevel
5302 * Helper function that returns the next mipmap level
5304 * tex_ptr: Surface of which to return the next level
5306 *****************************************************************************/
5307 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
5309 /* Now go down the mipmap chain to the next surface */
5310 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, {0} };
5311 IDirectDrawSurface7 *next_level;
5312 HRESULT hr;
5314 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
5315 if (FAILED(hr)) return NULL;
5317 ddraw_surface7_Release(next_level);
5319 return impl_from_IDirectDrawSurface7(next_level);
5322 /*****************************************************************************
5323 * IDirect3DTexture2::Load
5325 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5327 * This function isn't relayed to WineD3D because the whole interface is
5328 * implemented in DDraw only. For speed improvements an implementation which
5329 * takes OpenGL more into account could be placed into WineD3D.
5331 * Params:
5332 * src_texture: Address of the texture to load
5334 * Returns:
5335 * D3D_OK on success
5336 * D3DERR_TEXTURE_LOAD_FAILED.
5338 *****************************************************************************/
5339 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5341 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5342 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5343 struct wined3d_resource *dst_resource, *src_resource;
5344 HRESULT hr;
5346 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5348 if (src_surface == dst_surface)
5350 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5351 return D3D_OK;
5354 wined3d_mutex_lock();
5356 dst_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(dst_surface, DDRAW_SURFACE_WRITE));
5357 src_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(src_surface, DDRAW_SURFACE_READ));
5359 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5360 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5361 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5363 ERR("Trying to load surfaces with different mip-map counts.\n");
5366 for (;;)
5368 struct ddraw_palette *dst_pal, *src_pal;
5369 DDSURFACEDESC *src_desc, *dst_desc;
5371 TRACE("Copying surface %p to surface %p.\n", src_surface, dst_surface);
5373 /* Suppress the ALLOCONLOAD flag */
5374 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5376 /* Get the palettes */
5377 dst_pal = dst_surface->palette;
5378 src_pal = src_surface->palette;
5380 if (src_pal)
5382 PALETTEENTRY palent[256];
5384 if (!dst_pal)
5386 wined3d_mutex_unlock();
5387 return DDERR_NOPALETTEATTACHED;
5389 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5390 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5393 /* Copy one surface on the other */
5394 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5395 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5397 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5399 /* Should also check for same pixel format, u1.lPitch, ... */
5400 ERR("Error in surface sizes.\n");
5401 wined3d_mutex_unlock();
5402 return D3DERR_TEXTURE_LOAD_FAILED;
5404 else
5406 struct wined3d_map_desc src_map_desc, dst_map_desc;
5408 /* Copy the src blit color key if the source has one, don't erase
5409 * the destination's ckey if the source has none */
5410 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5412 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5413 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5416 if (FAILED(hr = wined3d_resource_map(src_resource,
5417 src_surface->sub_resource_idx, &src_map_desc, NULL, WINED3D_MAP_READ)))
5419 ERR("Failed to lock source surface, hr %#x.\n", hr);
5420 wined3d_mutex_unlock();
5421 return D3DERR_TEXTURE_LOAD_FAILED;
5424 if (FAILED(hr = wined3d_resource_map(dst_resource,
5425 dst_surface->sub_resource_idx, &dst_map_desc, NULL, WINED3D_MAP_WRITE)))
5427 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5428 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5429 wined3d_mutex_unlock();
5430 return D3DERR_TEXTURE_LOAD_FAILED;
5433 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5434 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5435 else
5436 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5438 wined3d_resource_unmap(dst_resource, dst_surface->sub_resource_idx);
5439 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5442 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5443 src_surface = get_sub_mimaplevel(src_surface);
5444 else
5445 src_surface = NULL;
5447 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5448 dst_surface = get_sub_mimaplevel(dst_surface);
5449 else
5450 dst_surface = NULL;
5452 if (!src_surface || !dst_surface)
5454 if (src_surface != dst_surface)
5455 ERR("Loading surface with different mipmap structure.\n");
5456 break;
5460 wined3d_mutex_unlock();
5462 return hr;
5465 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5467 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5468 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5470 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5472 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5473 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5476 /*****************************************************************************
5477 * The VTable
5478 *****************************************************************************/
5480 /* Some windowed mode wrappers expect this vtbl to be writable. */
5481 static struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5483 /* IUnknown */
5484 ddraw_surface7_QueryInterface,
5485 ddraw_surface7_AddRef,
5486 ddraw_surface7_Release,
5487 /* IDirectDrawSurface */
5488 ddraw_surface7_AddAttachedSurface,
5489 ddraw_surface7_AddOverlayDirtyRect,
5490 ddraw_surface7_Blt,
5491 ddraw_surface7_BltBatch,
5492 ddraw_surface7_BltFast,
5493 ddraw_surface7_DeleteAttachedSurface,
5494 ddraw_surface7_EnumAttachedSurfaces,
5495 ddraw_surface7_EnumOverlayZOrders,
5496 ddraw_surface7_Flip,
5497 ddraw_surface7_GetAttachedSurface,
5498 ddraw_surface7_GetBltStatus,
5499 ddraw_surface7_GetCaps,
5500 ddraw_surface7_GetClipper,
5501 ddraw_surface7_GetColorKey,
5502 ddraw_surface7_GetDC,
5503 ddraw_surface7_GetFlipStatus,
5504 ddraw_surface7_GetOverlayPosition,
5505 ddraw_surface7_GetPalette,
5506 ddraw_surface7_GetPixelFormat,
5507 ddraw_surface7_GetSurfaceDesc,
5508 ddraw_surface7_Initialize,
5509 ddraw_surface7_IsLost,
5510 ddraw_surface7_Lock,
5511 ddraw_surface7_ReleaseDC,
5512 ddraw_surface7_Restore,
5513 ddraw_surface7_SetClipper,
5514 ddraw_surface7_SetColorKey,
5515 ddraw_surface7_SetOverlayPosition,
5516 ddraw_surface7_SetPalette,
5517 ddraw_surface7_Unlock,
5518 ddraw_surface7_UpdateOverlay,
5519 ddraw_surface7_UpdateOverlayDisplay,
5520 ddraw_surface7_UpdateOverlayZOrder,
5521 /* IDirectDrawSurface2 */
5522 ddraw_surface7_GetDDInterface,
5523 ddraw_surface7_PageLock,
5524 ddraw_surface7_PageUnlock,
5525 /* IDirectDrawSurface3 */
5526 ddraw_surface7_SetSurfaceDesc,
5527 /* IDirectDrawSurface4 */
5528 ddraw_surface7_SetPrivateData,
5529 ddraw_surface7_GetPrivateData,
5530 ddraw_surface7_FreePrivateData,
5531 ddraw_surface7_GetUniquenessValue,
5532 ddraw_surface7_ChangeUniquenessValue,
5533 /* IDirectDrawSurface7 */
5534 ddraw_surface7_SetPriority,
5535 ddraw_surface7_GetPriority,
5536 ddraw_surface7_SetLOD,
5537 ddraw_surface7_GetLOD,
5540 /* Some windowed mode wrappers expect this vtbl to be writable. */
5541 static struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5543 /* IUnknown */
5544 ddraw_surface4_QueryInterface,
5545 ddraw_surface4_AddRef,
5546 ddraw_surface4_Release,
5547 /* IDirectDrawSurface */
5548 ddraw_surface4_AddAttachedSurface,
5549 ddraw_surface4_AddOverlayDirtyRect,
5550 ddraw_surface4_Blt,
5551 ddraw_surface4_BltBatch,
5552 ddraw_surface4_BltFast,
5553 ddraw_surface4_DeleteAttachedSurface,
5554 ddraw_surface4_EnumAttachedSurfaces,
5555 ddraw_surface4_EnumOverlayZOrders,
5556 ddraw_surface4_Flip,
5557 ddraw_surface4_GetAttachedSurface,
5558 ddraw_surface4_GetBltStatus,
5559 ddraw_surface4_GetCaps,
5560 ddraw_surface4_GetClipper,
5561 ddraw_surface4_GetColorKey,
5562 ddraw_surface4_GetDC,
5563 ddraw_surface4_GetFlipStatus,
5564 ddraw_surface4_GetOverlayPosition,
5565 ddraw_surface4_GetPalette,
5566 ddraw_surface4_GetPixelFormat,
5567 ddraw_surface4_GetSurfaceDesc,
5568 ddraw_surface4_Initialize,
5569 ddraw_surface4_IsLost,
5570 ddraw_surface4_Lock,
5571 ddraw_surface4_ReleaseDC,
5572 ddraw_surface4_Restore,
5573 ddraw_surface4_SetClipper,
5574 ddraw_surface4_SetColorKey,
5575 ddraw_surface4_SetOverlayPosition,
5576 ddraw_surface4_SetPalette,
5577 ddraw_surface4_Unlock,
5578 ddraw_surface4_UpdateOverlay,
5579 ddraw_surface4_UpdateOverlayDisplay,
5580 ddraw_surface4_UpdateOverlayZOrder,
5581 /* IDirectDrawSurface2 */
5582 ddraw_surface4_GetDDInterface,
5583 ddraw_surface4_PageLock,
5584 ddraw_surface4_PageUnlock,
5585 /* IDirectDrawSurface3 */
5586 ddraw_surface4_SetSurfaceDesc,
5587 /* IDirectDrawSurface4 */
5588 ddraw_surface4_SetPrivateData,
5589 ddraw_surface4_GetPrivateData,
5590 ddraw_surface4_FreePrivateData,
5591 ddraw_surface4_GetUniquenessValue,
5592 ddraw_surface4_ChangeUniquenessValue,
5595 /* Some windowed mode wrappers expect this vtbl to be writable. */
5596 static struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5598 /* IUnknown */
5599 ddraw_surface3_QueryInterface,
5600 ddraw_surface3_AddRef,
5601 ddraw_surface3_Release,
5602 /* IDirectDrawSurface */
5603 ddraw_surface3_AddAttachedSurface,
5604 ddraw_surface3_AddOverlayDirtyRect,
5605 ddraw_surface3_Blt,
5606 ddraw_surface3_BltBatch,
5607 ddraw_surface3_BltFast,
5608 ddraw_surface3_DeleteAttachedSurface,
5609 ddraw_surface3_EnumAttachedSurfaces,
5610 ddraw_surface3_EnumOverlayZOrders,
5611 ddraw_surface3_Flip,
5612 ddraw_surface3_GetAttachedSurface,
5613 ddraw_surface3_GetBltStatus,
5614 ddraw_surface3_GetCaps,
5615 ddraw_surface3_GetClipper,
5616 ddraw_surface3_GetColorKey,
5617 ddraw_surface3_GetDC,
5618 ddraw_surface3_GetFlipStatus,
5619 ddraw_surface3_GetOverlayPosition,
5620 ddraw_surface3_GetPalette,
5621 ddraw_surface3_GetPixelFormat,
5622 ddraw_surface3_GetSurfaceDesc,
5623 ddraw_surface3_Initialize,
5624 ddraw_surface3_IsLost,
5625 ddraw_surface3_Lock,
5626 ddraw_surface3_ReleaseDC,
5627 ddraw_surface3_Restore,
5628 ddraw_surface3_SetClipper,
5629 ddraw_surface3_SetColorKey,
5630 ddraw_surface3_SetOverlayPosition,
5631 ddraw_surface3_SetPalette,
5632 ddraw_surface3_Unlock,
5633 ddraw_surface3_UpdateOverlay,
5634 ddraw_surface3_UpdateOverlayDisplay,
5635 ddraw_surface3_UpdateOverlayZOrder,
5636 /* IDirectDrawSurface2 */
5637 ddraw_surface3_GetDDInterface,
5638 ddraw_surface3_PageLock,
5639 ddraw_surface3_PageUnlock,
5640 /* IDirectDrawSurface3 */
5641 ddraw_surface3_SetSurfaceDesc,
5644 /* Some windowed mode wrappers expect this vtbl to be writable. */
5645 static struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5647 /* IUnknown */
5648 ddraw_surface2_QueryInterface,
5649 ddraw_surface2_AddRef,
5650 ddraw_surface2_Release,
5651 /* IDirectDrawSurface */
5652 ddraw_surface2_AddAttachedSurface,
5653 ddraw_surface2_AddOverlayDirtyRect,
5654 ddraw_surface2_Blt,
5655 ddraw_surface2_BltBatch,
5656 ddraw_surface2_BltFast,
5657 ddraw_surface2_DeleteAttachedSurface,
5658 ddraw_surface2_EnumAttachedSurfaces,
5659 ddraw_surface2_EnumOverlayZOrders,
5660 ddraw_surface2_Flip,
5661 ddraw_surface2_GetAttachedSurface,
5662 ddraw_surface2_GetBltStatus,
5663 ddraw_surface2_GetCaps,
5664 ddraw_surface2_GetClipper,
5665 ddraw_surface2_GetColorKey,
5666 ddraw_surface2_GetDC,
5667 ddraw_surface2_GetFlipStatus,
5668 ddraw_surface2_GetOverlayPosition,
5669 ddraw_surface2_GetPalette,
5670 ddraw_surface2_GetPixelFormat,
5671 ddraw_surface2_GetSurfaceDesc,
5672 ddraw_surface2_Initialize,
5673 ddraw_surface2_IsLost,
5674 ddraw_surface2_Lock,
5675 ddraw_surface2_ReleaseDC,
5676 ddraw_surface2_Restore,
5677 ddraw_surface2_SetClipper,
5678 ddraw_surface2_SetColorKey,
5679 ddraw_surface2_SetOverlayPosition,
5680 ddraw_surface2_SetPalette,
5681 ddraw_surface2_Unlock,
5682 ddraw_surface2_UpdateOverlay,
5683 ddraw_surface2_UpdateOverlayDisplay,
5684 ddraw_surface2_UpdateOverlayZOrder,
5685 /* IDirectDrawSurface2 */
5686 ddraw_surface2_GetDDInterface,
5687 ddraw_surface2_PageLock,
5688 ddraw_surface2_PageUnlock,
5691 /* Bad Mojo Redux expects this vtbl to be writable. */
5692 static struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5694 /* IUnknown */
5695 ddraw_surface1_QueryInterface,
5696 ddraw_surface1_AddRef,
5697 ddraw_surface1_Release,
5698 /* IDirectDrawSurface */
5699 ddraw_surface1_AddAttachedSurface,
5700 ddraw_surface1_AddOverlayDirtyRect,
5701 ddraw_surface1_Blt,
5702 ddraw_surface1_BltBatch,
5703 ddraw_surface1_BltFast,
5704 ddraw_surface1_DeleteAttachedSurface,
5705 ddraw_surface1_EnumAttachedSurfaces,
5706 ddraw_surface1_EnumOverlayZOrders,
5707 ddraw_surface1_Flip,
5708 ddraw_surface1_GetAttachedSurface,
5709 ddraw_surface1_GetBltStatus,
5710 ddraw_surface1_GetCaps,
5711 ddraw_surface1_GetClipper,
5712 ddraw_surface1_GetColorKey,
5713 ddraw_surface1_GetDC,
5714 ddraw_surface1_GetFlipStatus,
5715 ddraw_surface1_GetOverlayPosition,
5716 ddraw_surface1_GetPalette,
5717 ddraw_surface1_GetPixelFormat,
5718 ddraw_surface1_GetSurfaceDesc,
5719 ddraw_surface1_Initialize,
5720 ddraw_surface1_IsLost,
5721 ddraw_surface1_Lock,
5722 ddraw_surface1_ReleaseDC,
5723 ddraw_surface1_Restore,
5724 ddraw_surface1_SetClipper,
5725 ddraw_surface1_SetColorKey,
5726 ddraw_surface1_SetOverlayPosition,
5727 ddraw_surface1_SetPalette,
5728 ddraw_surface1_Unlock,
5729 ddraw_surface1_UpdateOverlay,
5730 ddraw_surface1_UpdateOverlayDisplay,
5731 ddraw_surface1_UpdateOverlayZOrder,
5734 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5736 ddraw_gamma_control_QueryInterface,
5737 ddraw_gamma_control_AddRef,
5738 ddraw_gamma_control_Release,
5739 ddraw_gamma_control_GetGammaRamp,
5740 ddraw_gamma_control_SetGammaRamp,
5743 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5745 d3d_texture2_QueryInterface,
5746 d3d_texture2_AddRef,
5747 d3d_texture2_Release,
5748 d3d_texture2_GetHandle,
5749 d3d_texture2_PaletteChanged,
5750 d3d_texture2_Load,
5753 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5755 d3d_texture1_QueryInterface,
5756 d3d_texture1_AddRef,
5757 d3d_texture1_Release,
5758 d3d_texture1_Initialize,
5759 d3d_texture1_GetHandle,
5760 d3d_texture1_PaletteChanged,
5761 d3d_texture1_Load,
5762 d3d_texture1_Unload,
5765 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5766 * IDirectDrawSurface interface to ddraw methods. */
5767 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5769 if (!iface) return NULL;
5770 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5772 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5773 if (FAILED(hr))
5775 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5776 return NULL;
5778 IDirectDrawSurface7_Release(iface);
5780 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5783 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5785 if (!iface) return NULL;
5786 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5788 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5789 if (FAILED(hr))
5791 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5792 return NULL;
5794 IDirectDrawSurface4_Release(iface);
5796 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5799 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5801 if (!iface) return NULL;
5802 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5804 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5805 if (FAILED(hr))
5807 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5808 return NULL;
5810 IDirectDrawSurface3_Release(iface);
5812 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5815 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5817 if (!iface) return NULL;
5818 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5820 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5821 if (FAILED(hr))
5823 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5824 return NULL;
5826 IDirectDrawSurface2_Release(iface);
5828 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5831 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5833 if (!iface) return NULL;
5834 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5836 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5837 if (FAILED(hr))
5839 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5840 return NULL;
5842 IDirectDrawSurface_Release(iface);
5844 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5847 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5849 if (!iface) return NULL;
5850 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5851 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5854 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5856 if (!iface) return NULL;
5857 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5858 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5861 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5863 struct ddraw_surface *surface = parent;
5865 TRACE("surface %p.\n", surface);
5867 /* This shouldn't happen, ddraw_surface_release_iface() should prevent the
5868 * surface from being destroyed in this case. */
5869 if (surface->first_attached != surface)
5870 ERR("Surface is still attached to surface %p.\n", surface->first_attached);
5872 while (surface->next_attached)
5873 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5874 surface->next_attached, surface->next_attached->attached_iface)))
5875 ERR("DeleteAttachedSurface failed.\n");
5877 /* Having a texture handle set implies that the device still exists. */
5878 if (surface->Handle)
5879 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5881 /* Reduce the ddraw surface count. */
5882 list_remove(&surface->surface_list_entry);
5884 if (surface->clipper && ddraw_clipper_is_valid(surface->clipper))
5885 IDirectDrawClipper_Release(&surface->clipper->IDirectDrawClipper_iface);
5887 if (surface == surface->ddraw->primary)
5889 surface->ddraw->primary = NULL;
5890 surface->ddraw->gdi_surface = NULL;
5893 wined3d_private_store_cleanup(&surface->private_store);
5895 if (surface->draw_texture)
5896 wined3d_texture_decref(surface->wined3d_texture);
5898 heap_free(surface);
5901 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5903 ddraw_surface_wined3d_object_destroyed,
5906 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5908 struct ddraw_texture *texture = parent;
5910 TRACE("texture %p, texture_memory %p.\n", texture, texture->texture_memory);
5912 heap_free(texture->texture_memory);
5913 heap_free(parent);
5916 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5918 ddraw_texture_wined3d_object_destroyed,
5921 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5923 return DD_OK;
5926 static HRESULT ddraw_surface_reserve_memory(struct wined3d_texture *wined3d_texture)
5928 static const unsigned int extra_size = 0x10000;
5930 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
5931 struct wined3d_resource_desc resource_desc;
5932 struct wined3d_sub_resource_desc desc;
5933 unsigned int pitch, slice_pitch;
5934 unsigned int sub_resource_idx;
5935 HRESULT hr = WINED3D_OK;
5936 unsigned int offset;
5938 wined3d_resource_get_desc(wined3d_texture_get_resource(wined3d_texture), &resource_desc);
5939 if (!(texture->texture_memory = heap_alloc_zero(resource_desc.size + extra_size)))
5941 ERR("Out of memory.\n");
5942 return E_OUTOFMEMORY;
5944 TRACE("texture->texture_memory %p.\n", texture->texture_memory);
5946 offset = 0;
5947 sub_resource_idx = 0;
5948 while (wined3d_texture_get_sub_resource_desc(wined3d_texture, sub_resource_idx, &desc)
5949 == WINED3D_OK)
5951 wined3d_texture_get_pitch(wined3d_texture, sub_resource_idx, &pitch, &slice_pitch);
5953 if (FAILED(hr = wined3d_texture_update_desc(wined3d_texture, sub_resource_idx,
5954 desc.width, desc.height, resource_desc.format,
5955 desc.multisample_type, desc.multisample_quality,
5956 (BYTE *)texture->texture_memory + offset, pitch)))
5958 heap_free(texture->texture_memory);
5959 texture->texture_memory = NULL;
5960 break;
5962 ++sub_resource_idx;
5963 offset += desc.size;
5965 return hr;
5968 static HRESULT ddraw_surface_create_wined3d_texture(DDSURFACEDESC2 *desc, struct wined3d_device *wined3d_device,
5969 const struct wined3d_resource_desc *wined3d_desc, unsigned int layers, unsigned int levels,
5970 struct ddraw_texture *texture, struct wined3d_texture **wined3d_texture)
5972 struct wined3d_resource_desc draw_texture_desc;
5973 struct wined3d_texture *draw_texture;
5974 struct ddraw_surface *parent;
5975 unsigned int bind_flags;
5976 unsigned int i;
5977 HRESULT hr;
5979 bind_flags = 0;
5980 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5981 || (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5982 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
5984 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5985 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
5986 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5987 bind_flags |= WINED3D_BIND_RENDER_TARGET;
5989 if (!bind_flags || (wined3d_desc->access & WINED3D_RESOURCE_ACCESS_GPU && !(bind_flags & ~wined3d_desc->bind_flags)))
5990 goto no_draw_texture;
5992 draw_texture_desc = *wined3d_desc;
5993 draw_texture_desc.bind_flags = bind_flags;
5994 draw_texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5996 if (FAILED(hr = wined3d_texture_create(wined3d_device, &draw_texture_desc, layers,
5997 levels, 0, NULL, texture, &ddraw_texture_wined3d_parent_ops, &draw_texture)))
5999 WARN("Failed to create draw texture, hr %#x.\n", hr);
6000 goto no_draw_texture;
6002 wined3d_texture_decref(draw_texture);
6004 /* Some applications assume surfaces will always be mapped at the same
6005 * address. Some of those also assume that this address is valid even when
6006 * the surface isn't mapped, and that updates done this way will be
6007 * visible on the screen. The game Nox is such an application,
6008 * Commandos: Behind Enemy Lines is another. Setting
6009 * WINED3D_TEXTURE_CREATE_GET_DC_LENIENT will ensure this. */
6010 if (FAILED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6011 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, NULL, &ddraw_null_wined3d_parent_ops,
6012 wined3d_texture)))
6014 parent = wined3d_texture_get_sub_resource_parent(draw_texture, 0);
6015 if (texture->version == 7)
6016 IDirectDrawSurface7_Release(&parent->IDirectDrawSurface7_iface);
6017 else if (texture->version == 4)
6018 IDirectDrawSurface4_Release(&parent->IDirectDrawSurface4_iface);
6019 else
6020 IDirectDrawSurface_Release(&parent->IDirectDrawSurface_iface);
6021 return hr;
6023 wined3d_resource_set_parent(wined3d_texture_get_resource(*wined3d_texture), texture);
6024 for (i = 0; i < layers * levels; ++i)
6026 parent = wined3d_texture_get_sub_resource_parent(draw_texture, i);
6027 assert(parent->wined3d_texture == draw_texture);
6028 parent->draw_texture = draw_texture;
6029 parent->wined3d_texture = *wined3d_texture;
6030 wined3d_texture_set_sub_resource_parent(*wined3d_texture, i, parent);
6031 wined3d_texture_incref(*wined3d_texture);
6033 wined3d_texture_decref(*wined3d_texture);
6034 TRACE("Surface %p, created draw_texture %p, wined3d_texture %p.\n",
6035 wined3d_texture_get_sub_resource_parent(draw_texture, 0), draw_texture, wined3d_texture);
6036 return D3D_OK;
6038 no_draw_texture:
6039 if (SUCCEEDED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6040 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, texture, &ddraw_texture_wined3d_parent_ops,
6041 wined3d_texture)))
6042 wined3d_texture_decref(*wined3d_texture);
6043 return hr;
6046 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
6047 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
6049 struct wined3d_sub_resource_desc wined3d_mip_desc;
6050 struct ddraw_surface *root, *mip, **attach;
6051 struct wined3d_resource_desc wined3d_desc;
6052 DDPIXELFORMAT wined3d_display_mode_format;
6053 struct wined3d_texture *wined3d_texture;
6054 struct wined3d_display_mode mode;
6055 DDSURFACEDESC2 *desc, *mip_desc;
6056 struct ddraw_texture *texture;
6057 BOOL sysmem_fallback = FALSE;
6058 unsigned int layers = 1;
6059 unsigned int pitch = 0;
6060 BOOL reserve_memory;
6061 UINT levels, i, j;
6062 HRESULT hr;
6064 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
6065 ddraw, surface_desc, surface, outer_unknown, version);
6066 if (TRACE_ON(ddraw))
6068 TRACE("Requesting surface desc:\n");
6069 DDRAW_dump_surface_desc(surface_desc);
6072 if (outer_unknown)
6073 return CLASS_E_NOAGGREGATION;
6075 if (!surface)
6076 return E_POINTER;
6078 if (!(texture = heap_alloc(sizeof(*texture))))
6079 return E_OUTOFMEMORY;
6081 texture->texture_memory = NULL;
6082 texture->version = version;
6083 texture->surface_desc = *surface_desc;
6084 desc = &texture->surface_desc;
6086 /* Ensure DDSD_CAPS is always set. */
6087 desc->dwFlags |= DDSD_CAPS;
6089 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6091 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->u5.dwBackBufferCount)
6093 WARN("Tried to create a flippable surface without any back buffers.\n");
6094 heap_free(texture);
6095 return DDERR_INVALIDCAPS;
6098 if (!(desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX))
6100 WARN("Tried to create a flippable surface without DDSCAPS_COMPLEX.\n");
6101 heap_free(texture);
6102 return DDERR_INVALIDCAPS;
6105 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6107 WARN("Tried to create a flippable cubemap.\n");
6108 heap_free(texture);
6109 return DDERR_INVALIDPARAMS;
6112 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6114 FIXME("Flippable textures not implemented.\n");
6115 heap_free(texture);
6116 return DDERR_INVALIDCAPS;
6119 else
6121 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6123 WARN("Tried to specify a back buffer count for a non-flippable surface.\n");
6124 hr = desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP ? DDERR_INVALIDPARAMS : DDERR_INVALIDCAPS;
6125 heap_free(texture);
6126 return hr;
6130 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6132 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6134 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
6135 heap_free(texture);
6136 return DDERR_INVALIDCAPS;
6139 if ((desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX) && !(desc->ddsCaps.dwCaps & DDSCAPS_FLIP))
6141 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
6142 heap_free(texture);
6143 return DDERR_INVALIDCAPS;
6146 if ((desc->ddsCaps.dwCaps & DDSCAPS_FLIP) && !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
6148 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
6149 heap_free(texture);
6150 return DDERR_NOEXCLUSIVEMODE;
6154 /* This is a special case in ddrawex, but not allowed in ddraw. */
6155 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6156 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6158 WARN("Tried to create a surface in both system and video memory.\n");
6159 heap_free(texture);
6160 return DDERR_INVALIDCAPS;
6163 if ((desc->ddsCaps.dwCaps & (DDSCAPS_ALLOCONLOAD | DDSCAPS_MIPMAP))
6164 && !(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6166 WARN("Caps %#x require DDSCAPS_TEXTURE.\n", desc->ddsCaps.dwCaps);
6167 heap_free(texture);
6168 return DDERR_INVALIDCAPS;
6171 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
6172 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
6174 WARN("Cube map faces requested without cube map flag.\n");
6175 heap_free(texture);
6176 return DDERR_INVALIDCAPS;
6179 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6180 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
6182 WARN("Cube map without faces requested.\n");
6183 heap_free(texture);
6184 return DDERR_INVALIDPARAMS;
6187 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6188 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
6189 FIXME("Partial cube maps not implemented.\n");
6191 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6193 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6195 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
6196 heap_free(texture);
6197 return DDERR_INVALIDCAPS;
6199 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6201 WARN("DDSCAPS2_TEXTUREMANAGE used with DDSCAPS_VIDEOMEMORY "
6202 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
6203 heap_free(texture);
6204 return DDERR_INVALIDCAPS;
6208 if (desc->ddsCaps.dwCaps & DDSCAPS_WRITEONLY
6209 && !(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6211 WARN("DDSCAPS_WRITEONLY used without DDSCAPS2_TEXTUREMANAGE, returning DDERR_INVALIDCAPS.\n");
6212 heap_free(texture);
6213 return DDERR_INVALIDCAPS;
6216 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
6218 ERR("Failed to get display mode, hr %#x.\n", hr);
6219 heap_free(texture);
6220 return hr_ddraw_from_wined3d(hr);
6223 wined3d_display_mode_format.dwSize = sizeof(wined3d_display_mode_format);
6224 ddrawformat_from_wined3dformat(&wined3d_display_mode_format, mode.format_id);
6226 /* No pixelformat given? Use the current screen format. */
6227 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
6229 desc->dwFlags |= DDSD_PIXELFORMAT;
6230 desc->u4.ddpfPixelFormat = wined3d_display_mode_format;
6233 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6234 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
6235 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
6237 WARN("Unsupported / unknown pixelformat.\n");
6238 heap_free(texture);
6239 return DDERR_INVALIDPIXELFORMAT;
6242 /* No width or no height? Use the screen size. */
6243 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
6245 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
6247 WARN("No width / height specified.\n");
6248 heap_free(texture);
6249 return DDERR_INVALIDPARAMS;
6252 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
6253 desc->dwWidth = mode.width;
6254 desc->dwHeight = mode.height;
6257 if (!desc->dwWidth || !desc->dwHeight)
6259 heap_free(texture);
6260 return DDERR_INVALIDPARAMS;
6263 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6264 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
6266 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6268 /* The first surface is a front buffer, the back buffers are created
6269 * afterwards. */
6270 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
6271 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
6273 struct wined3d_swapchain_desc swapchain_desc;
6275 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
6276 swapchain_desc.backbuffer_width = mode.width;
6277 swapchain_desc.backbuffer_height = mode.height;
6278 swapchain_desc.backbuffer_format = mode.format_id;
6280 if (ddraw->d3ddevice)
6282 if (ddraw->d3ddevice->recording)
6283 wined3d_stateblock_decref(ddraw->d3ddevice->recording);
6284 ddraw->d3ddevice->recording = NULL;
6285 ddraw->d3ddevice->update_state = ddraw->d3ddevice->state;
6287 wined3d_stateblock_reset(ddraw->state);
6289 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
6290 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
6292 ERR("Failed to reset device.\n");
6293 heap_free(texture);
6294 return hr_ddraw_from_wined3d(hr);
6297 wined3d_stateblock_set_render_state(ddraw->state, WINED3D_RS_ZENABLE,
6298 !!swapchain_desc.enable_auto_depth_stencil);
6302 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
6303 wined3d_desc.multisample_quality = 0;
6304 wined3d_desc.usage = 0;
6305 wined3d_desc.bind_flags = 0;
6306 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6307 wined3d_desc.width = desc->dwWidth;
6308 wined3d_desc.height = desc->dwHeight;
6309 wined3d_desc.depth = 1;
6310 wined3d_desc.size = 0;
6312 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
6314 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
6315 /* Do not fail surface creation, only fail 3D device creation. */
6318 /* Mipmap count fixes */
6319 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6321 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
6323 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
6325 /* Mipmap count is given, should not be 0. */
6326 if (!desc->u2.dwMipMapCount)
6328 heap_free(texture);
6329 return DDERR_INVALIDPARAMS;
6332 else
6334 /* Undocumented feature: Create sublevels until either the
6335 * width or the height is 1. */
6336 if (version == 7)
6337 desc->u2.dwMipMapCount = wined3d_log2i(max(desc->dwWidth, desc->dwHeight)) + 1;
6338 else
6339 desc->u2.dwMipMapCount = wined3d_log2i(min(desc->dwWidth, desc->dwHeight)) + 1;
6342 else
6344 desc->u2.dwMipMapCount = 1;
6347 desc->dwFlags |= DDSD_MIPMAPCOUNT;
6348 levels = desc->u2.dwMipMapCount;
6350 else
6352 levels = 1;
6355 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
6357 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6359 unsigned int bind_flags = 0;
6360 DWORD usage = 0;
6362 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6364 usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6365 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6367 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6369 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6372 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6373 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6374 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6375 bind_flags |= WINED3D_BIND_RENDER_TARGET;
6377 if (!(ddraw->flags & DDRAW_NO3D) && SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d,
6378 ddraw->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL, mode.format_id,
6379 usage, bind_flags, WINED3D_RTYPE_TEXTURE_2D, wined3d_desc.format)))
6381 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
6383 else
6385 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6386 sysmem_fallback = TRUE;
6389 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6391 /* Tests show surfaces without memory flags get these flags added
6392 * right after creation. */
6393 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
6397 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6398 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6400 WARN("System memory overlays are not allowed.\n");
6401 heap_free(texture);
6402 return DDERR_NOOVERLAYHW;
6405 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
6407 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_CPU
6408 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6410 else
6412 if (!(ddraw->flags & DDRAW_NO3D))
6414 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6415 wined3d_desc.bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6416 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6417 wined3d_desc.bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6418 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6419 wined3d_desc.bind_flags |= WINED3D_BIND_RENDER_TARGET;
6422 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6424 wined3d_desc.bind_flags &= ~WINED3D_BIND_RENDER_TARGET;
6425 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU
6426 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6427 /* Managed textures have the system memory flag set. */
6428 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6430 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
6432 /* Videomemory adds localvidmem. This is mutually exclusive with
6433 * systemmemory and texturemanage. */
6434 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
6435 /* Dynamic resources can't be written by the GPU. */
6436 if (!(wined3d_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
6437 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
6441 if (desc->dwFlags & DDSD_LPSURFACE)
6443 if (wined3d_desc.access & WINED3D_RESOURCE_ACCESS_GPU)
6445 WARN("User memory surfaces should not be GPU accessible.\n");
6446 heap_free(texture);
6447 return DDERR_INVALIDCAPS;
6450 if (version < 4)
6452 WARN("User memory surfaces not supported before version 4.\n");
6453 heap_free(texture);
6454 return DDERR_INVALIDPARAMS;
6457 if (!desc->lpSurface)
6459 WARN("NULL surface memory pointer specified.\n");
6460 heap_free(texture);
6461 return DDERR_INVALIDPARAMS;
6464 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6466 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
6468 WARN("Pitch specified on a compressed user memory surface.\n");
6469 heap_free(texture);
6470 return DDERR_INVALIDPARAMS;
6473 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
6475 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
6476 heap_free(texture);
6477 return DDERR_INVALIDPARAMS;
6480 if ((desc->dwFlags & DDSD_LINEARSIZE)
6481 && desc->u1.dwLinearSize < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6482 wined3d_desc.format, wined3d_desc.width) * ((desc->dwHeight + 3) / 4))
6484 WARN("Invalid linear size %u specified.\n", desc->u1.dwLinearSize);
6485 heap_free(texture);
6486 return DDERR_INVALIDPARAMS;
6489 else
6491 if (!(desc->dwFlags & DDSD_PITCH))
6493 WARN("User memory surfaces should explicitly specify the pitch.\n");
6494 heap_free(texture);
6495 return DDERR_INVALIDPARAMS;
6498 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6499 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
6501 WARN("Invalid pitch %u specified.\n", desc->u1.lPitch);
6502 heap_free(texture);
6503 return DDERR_INVALIDPARAMS;
6506 pitch = desc->u1.lPitch;
6510 if (((desc->dwFlags & DDSD_CKDESTOVERLAY)
6511 && desc->u3.ddckCKDestOverlay.dwColorSpaceLowValue != desc->u3.ddckCKDestOverlay.dwColorSpaceHighValue)
6512 || ((desc->dwFlags & DDSD_CKDESTBLT)
6513 && desc->ddckCKDestBlt.dwColorSpaceLowValue != desc->ddckCKDestBlt.dwColorSpaceHighValue)
6514 || ((desc->dwFlags & DDSD_CKSRCOVERLAY)
6515 && desc->ddckCKSrcOverlay.dwColorSpaceLowValue != desc->ddckCKSrcOverlay.dwColorSpaceHighValue)
6516 || ((desc->dwFlags & DDSD_CKSRCBLT)
6517 && desc->ddckCKSrcBlt.dwColorSpaceLowValue != desc->ddckCKSrcBlt.dwColorSpaceHighValue))
6519 WARN("Range color keys not supported, returning DDERR_NOCOLORKEYHW.\n");
6520 heap_free(texture);
6521 return DDERR_NOCOLORKEYHW;
6524 if ((ddraw->flags & DDRAW_NO3D) && (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
6526 WARN("Video memory surfaces not supported without 3D support.\n");
6527 heap_free(texture);
6528 return DDERR_NODIRECTDRAWHW;
6531 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
6532 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
6534 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
6535 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
6537 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6539 wined3d_desc.usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6540 layers = 6;
6543 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, layers, levels,
6544 texture, &wined3d_texture)))
6546 WARN("Failed to create wined3d texture, hr %#x.\n", hr);
6547 heap_free(texture);
6548 return hr_ddraw_from_wined3d(hr);
6551 root = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6553 root->is_complex_root = TRUE;
6554 root->sysmem_fallback = sysmem_fallback;
6555 texture->root = root;
6556 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6558 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6559 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTOVERLAY,
6560 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6561 if (desc->dwFlags & DDSD_CKDESTBLT)
6562 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTBLT,
6563 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6564 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6565 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCOVERLAY,
6566 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6567 if (desc->dwFlags & DDSD_CKSRCBLT)
6568 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCBLT,
6569 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6571 for (i = 0; i < layers; ++i)
6573 attach = &root->complex_array[layers - 1 - i];
6575 for (j = 0; j < levels; ++j)
6577 mip = wined3d_texture_get_sub_resource_parent(wined3d_texture, i * levels + j);
6578 mip->sysmem_fallback = sysmem_fallback;
6579 mip_desc = &mip->surface_desc;
6580 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6581 mip_desc->u2.dwMipMapCount = levels - j;
6583 if (j)
6585 wined3d_texture_get_sub_resource_desc(wined3d_texture, i * levels + j, &wined3d_mip_desc);
6586 mip_desc->dwWidth = wined3d_mip_desc.width;
6587 mip_desc->dwHeight = wined3d_mip_desc.height;
6589 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6591 else
6593 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6596 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6598 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6600 switch (i)
6602 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6603 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6604 break;
6605 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6606 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6607 break;
6608 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6609 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6610 break;
6611 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6612 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6613 break;
6614 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6615 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6616 break;
6617 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6618 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6619 break;
6624 if (mip == root)
6625 continue;
6627 *attach = mip;
6628 attach = &mip->complex_array[0];
6632 if ((desc->dwFlags & DDSD_LPSURFACE) && FAILED(hr = wined3d_texture_update_desc(wined3d_texture, 0,
6633 wined3d_desc.width, wined3d_desc.height, wined3d_desc.format,
6634 WINED3D_MULTISAMPLE_NONE, 0, desc->lpSurface, pitch)))
6636 ERR("Failed to set surface memory, hr %#x.\n", hr);
6637 goto fail;
6640 reserve_memory = !(desc->dwFlags & DDSD_LPSURFACE)
6641 && desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY
6642 && wined3d_display_mode_format.u1.dwRGBBitCount <= 16;
6644 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture)))
6646 ERR("Failed to reserve surface memory, hr %#x.\n", hr);
6647 goto fail;
6650 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6652 unsigned int count = desc->u5.dwBackBufferCount;
6653 struct ddraw_surface *last = root;
6655 attach = &last->complex_array[0];
6656 for (i = 0; i < count; ++i)
6658 if (!(texture = heap_alloc(sizeof(*texture))))
6660 hr = E_OUTOFMEMORY;
6661 goto fail;
6664 texture->texture_memory = NULL;
6665 texture->version = version;
6666 texture->surface_desc = root->surface_desc;
6667 desc = &texture->surface_desc;
6669 /* Only one surface in the flipping chain is a back buffer, one is
6670 * a front buffer, the others are just flippable surfaces. */
6671 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6672 | DDSCAPS_BACKBUFFER);
6673 if (!i)
6674 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6675 desc->u5.dwBackBufferCount = 0;
6677 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, 1, 1,
6678 texture, &wined3d_texture)))
6680 heap_free(texture);
6681 hr = hr_ddraw_from_wined3d(hr);
6682 goto fail;
6685 last = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6686 last->sysmem_fallback = sysmem_fallback;
6687 texture->root = last;
6688 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6690 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6691 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6692 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6693 if (desc->dwFlags & DDSD_CKDESTBLT)
6694 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6695 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6696 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6697 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6698 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6699 if (desc->dwFlags & DDSD_CKSRCBLT)
6700 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6701 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6703 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture)))
6705 hr = hr_ddraw_from_wined3d(hr);
6706 goto fail;
6708 *attach = last;
6709 attach = &last->complex_array[0];
6711 *attach = root;
6714 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6716 ddraw->primary = root;
6717 ddraw->gdi_surface = root->wined3d_texture;
6719 *surface = root;
6721 return DD_OK;
6723 fail:
6724 if (version == 7)
6725 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6726 else if (version == 4)
6727 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6728 else
6729 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6731 return hr;
6734 void ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw,
6735 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
6736 const struct wined3d_parent_ops **parent_ops)
6738 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
6739 unsigned int texture_level, row_pitch, slice_pitch;
6740 DDSURFACEDESC2 *desc = &surface->surface_desc;
6741 unsigned int version = texture->version;
6743 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6744 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6745 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6746 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6747 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6748 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6749 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6750 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6751 surface->iface_count = 1;
6752 surface->version = version;
6753 surface->ddraw = ddraw;
6755 if (version == 7)
6757 surface->ref7 = 1;
6758 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6760 else if (version == 4)
6762 surface->ref4 = 1;
6763 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6765 else
6767 surface->ref1 = 1;
6768 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6771 *desc = texture->surface_desc;
6772 surface->first_attached = surface;
6774 texture_level = desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP ? sub_resource_idx % desc->u2.dwMipMapCount : 0;
6775 wined3d_texture_get_pitch(wined3d_texture, texture_level, &row_pitch, &slice_pitch);
6776 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6778 if (desc->dwFlags & DDSD_LPSURFACE)
6779 desc->u1.dwLinearSize = ~0u;
6780 else
6781 desc->u1.dwLinearSize = slice_pitch;
6782 desc->dwFlags |= DDSD_LINEARSIZE;
6783 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6785 else
6787 if (!(desc->dwFlags & DDSD_LPSURFACE))
6788 desc->u1.lPitch = row_pitch;
6789 desc->dwFlags |= DDSD_PITCH;
6790 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6792 desc->lpSurface = NULL;
6794 wined3d_texture_incref(surface->wined3d_texture = wined3d_texture);
6795 surface->sub_resource_idx = sub_resource_idx;
6796 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6797 surface->texture_location = DDRAW_SURFACE_LOCATION_DEFAULT;
6799 wined3d_private_store_init(&surface->private_store);
6802 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
6804 struct ddraw_surface *surface = parent;
6806 /* If the surface reference count drops to zero, we release our reference
6807 * to the view, but don't clear the pointer yet, in case e.g. a
6808 * GetRenderTarget() call brings the surface back before the view is
6809 * actually destroyed. When the view is destroyed, we need to clear the
6810 * pointer, or a subsequent surface AddRef() would reference it again.
6812 * This is safe because as long as the view still has a reference to the
6813 * texture, the surface is also still alive, and we're called before the
6814 * view releases that reference. */
6815 surface->wined3d_rtv = NULL;
6818 static const struct wined3d_parent_ops ddraw_view_wined3d_parent_ops =
6820 view_wined3d_object_destroyed,
6823 struct wined3d_rendertarget_view *ddraw_surface_get_rendertarget_view(struct ddraw_surface *surface)
6825 struct wined3d_texture *wined3d_texture;
6826 HRESULT hr;
6828 if (surface->wined3d_rtv)
6829 return surface->wined3d_rtv;
6831 wined3d_texture = surface->draw_texture ? surface->draw_texture : surface->wined3d_texture;
6832 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(wined3d_texture,
6833 surface->sub_resource_idx, surface, &ddraw_view_wined3d_parent_ops, &surface->wined3d_rtv)))
6835 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
6836 return NULL;
6839 return surface->wined3d_rtv;