wined3d: Use wined3d_uint32_compare() in compare_sig().
[wine.git] / dlls / ddraw / surface.c
blob65e24b0bbcfbc5dd7cdc51a818940eb980007a4e
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 TRACE("surface %p, rect %s, read %#x, swap_interval %u.\n",
68 surface, wine_dbgstr_rect(rect), read, swap_interval);
70 if (ddraw->flags & DDRAW_SWAPPED && !read)
72 ddraw->flags &= ~DDRAW_SWAPPED;
73 rect = NULL;
76 if (!rect)
78 SetRect(&r, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
79 rect = &r;
82 x = rect->left;
83 y = rect->top;
84 w = rect->right - rect->left;
85 h = rect->bottom - rect->top;
87 if (w <= 0 || h <= 0)
88 return DD_OK;
90 /* The interaction between ddraw and GDI drawing is not all that well
91 * documented, and somewhat arcane. In ddraw exclusive mode, GDI draws
92 * seemingly go to the *original* frontbuffer/primary surface, while ddraw
93 * draws/flips go to the *current* frontbuffer surface. The bottom line is
94 * that if the current frontbuffer is not the GDI frontbuffer, and there's
95 * e.g. a popup window in front of the ddraw swapchain window, we can't
96 * use wined3d_swapchain_present() to get the ddraw contents to the screen
97 * while in exclusive mode, since it would get obscured by the popup
98 * window. On the other hand, if the current frontbuffer *is* the GDI
99 * frontbuffer, that's what's supposed to happen; the popup should obscure
100 * (part of) the ddraw swapchain window.
102 * This affects the "Deer Hunter" demo, which uses a popup window and GDI
103 * draws to draw part of the user interface. See also the "fswindow"
104 * sample is the DirectX 7 SDK. */
105 if (ddraw->swapchain_window && (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE)
106 || ddraw->swapchain_window == GetForegroundWindow() || ddraw_gdi_is_front(ddraw)))
108 /* Nothing to do, we control the frontbuffer, or at least the parts we
109 * care about. */
110 if (read)
111 return DD_OK;
113 if (swap_interval)
114 dst_texture = wined3d_swapchain_get_back_buffer(ddraw->wined3d_swapchain, 0);
115 else
116 dst_texture = ddraw->wined3d_frontbuffer;
118 if (SUCCEEDED(hr = wined3d_device_context_blt(ddraw->immediate_context, dst_texture, 0, rect,
119 ddraw_surface_get_any_texture(surface, DDRAW_SURFACE_READ), surface->sub_resource_idx, rect, 0,
120 NULL, WINED3D_TEXF_POINT)) && swap_interval)
122 hr = wined3d_swapchain_present(ddraw->wined3d_swapchain, rect, rect, NULL, swap_interval, 0);
123 ddraw->flags |= DDRAW_SWAPPED;
125 return hr;
128 wined3d_texture = ddraw_surface_get_default_texture(surface, read ? (rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE)
129 : DDRAW_SURFACE_READ);
131 if (FAILED(hr = wined3d_texture_get_dc(wined3d_texture, surface->sub_resource_idx, &surface_dc)))
133 ERR("Failed to get surface DC, hr %#x.\n", hr);
134 return hr;
136 if (surface->palette)
137 wined3d_palette_apply_to_dc(surface->palette->wined3d_palette, surface_dc);
139 if (!(screen_dc = GetDC(NULL)))
141 wined3d_texture_release_dc(wined3d_texture, surface->sub_resource_idx, surface_dc);
142 ERR("Failed to get screen DC.\n");
143 return E_FAIL;
146 if (read)
147 ret = BitBlt(surface_dc, x, y, w, h,
148 screen_dc, x, y, SRCCOPY);
149 else
150 ret = BitBlt(screen_dc, x, y, w, h,
151 surface_dc, x, y, SRCCOPY);
153 ReleaseDC(NULL, screen_dc);
154 wined3d_texture_release_dc(wined3d_texture, surface->sub_resource_idx, surface_dc);
156 if (!ret)
158 ERR("Failed to blit to/from screen.\n");
159 return E_FAIL;
162 return DD_OK;
165 /*****************************************************************************
166 * IUnknown parts follow
167 *****************************************************************************/
169 /*****************************************************************************
170 * IDirectDrawSurface7::QueryInterface
172 * A normal QueryInterface implementation. For QueryInterface rules
173 * see ddraw.c, IDirectDraw7::QueryInterface. This method
174 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
175 * in all versions, the IDirectDrawGammaControl interface and it can
176 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
178 * Params:
179 * riid: The interface id queried for
180 * obj: Address to write the pointer to
182 * Returns:
183 * S_OK on success
184 * E_NOINTERFACE if the requested interface wasn't found
186 *****************************************************************************/
187 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
189 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
191 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
193 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
194 *obj = NULL;
196 if(!riid)
197 return DDERR_INVALIDPARAMS;
199 if (IsEqualGUID(riid, &IID_IDirectDrawSurface7))
201 IDirectDrawSurface7_AddRef(iface);
202 *obj = iface;
203 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
204 return S_OK;
207 if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
209 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
210 *obj = &This->IDirectDrawSurface4_iface;
211 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
212 return S_OK;
215 if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
217 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
218 *obj = &This->IDirectDrawSurface3_iface;
219 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
220 return S_OK;
223 if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
225 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
226 *obj = &This->IDirectDrawSurface2_iface;
227 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
228 return S_OK;
231 if (IsEqualGUID(riid, &IID_IDirectDrawSurface)
232 || IsEqualGUID(riid, &IID_IUnknown))
234 IDirectDrawSurface_AddRef(&This->IDirectDrawSurface_iface);
235 *obj = &This->IDirectDrawSurface_iface;
236 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
237 return S_OK;
240 if (IsEqualGUID(riid, &IID_IDirectDrawGammaControl))
242 IDirectDrawGammaControl_AddRef(&This->IDirectDrawGammaControl_iface);
243 *obj = &This->IDirectDrawGammaControl_iface;
244 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
245 return S_OK;
248 if (IsEqualGUID(riid, &IID_IDirectDrawColorControl))
250 WARN("Color control not implemented.\n");
251 *obj = NULL;
252 return E_NOINTERFACE;
255 if (This->version != 7)
257 if (IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D)
258 || IsEqualGUID(riid, &IID_IDirect3DHALDevice)
259 || IsEqualGUID(riid, &IID_IDirect3DRGBDevice))
261 wined3d_mutex_lock();
262 if (!This->device1)
264 HRESULT hr;
266 if (FAILED(hr = d3d_device_create(This->ddraw, riid, This, (IUnknown *)&This->IDirectDrawSurface_iface,
267 1, &This->device1, (IUnknown *)&This->IDirectDrawSurface_iface)))
269 This->device1 = NULL;
270 wined3d_mutex_unlock();
271 WARN("Failed to create device, hr %#x.\n", hr);
272 return hr;
275 wined3d_mutex_unlock();
277 IDirect3DDevice_AddRef(&This->device1->IDirect3DDevice_iface);
278 *obj = &This->device1->IDirect3DDevice_iface;
279 return S_OK;
282 if (IsEqualGUID(&IID_IDirect3DTexture2, riid))
284 IDirect3DTexture2_AddRef(&This->IDirect3DTexture2_iface);
285 *obj = &This->IDirect3DTexture2_iface;
286 return S_OK;
289 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
291 IDirect3DTexture2_AddRef(&This->IDirect3DTexture_iface);
292 *obj = &This->IDirect3DTexture_iface;
293 return S_OK;
297 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
299 if (This->version != 7)
300 return E_INVALIDARG;
302 return E_NOINTERFACE;
305 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
307 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
309 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
311 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
314 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
316 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
318 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
320 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
323 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
325 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
327 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
329 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
332 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
334 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
336 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
338 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
341 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
342 REFIID riid, void **object)
344 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
346 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
348 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
351 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
353 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
355 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
357 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
360 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
362 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
364 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
366 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
369 static void ddraw_surface_add_iface(struct ddraw_surface *surface)
371 ULONG iface_count = InterlockedIncrement(&surface->iface_count);
372 TRACE("%p increasing iface count to %u.\n", surface, iface_count);
374 if (iface_count == 1)
376 if (surface->ifaceToRelease)
377 IUnknown_AddRef(surface->ifaceToRelease);
378 wined3d_mutex_lock();
379 if (surface->wined3d_rtv)
380 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
381 wined3d_texture_incref(surface->draw_texture ? surface->draw_texture : surface->wined3d_texture);
382 wined3d_mutex_unlock();
386 /*****************************************************************************
387 * IDirectDrawSurface7::AddRef
389 * A normal addref implementation
391 * Returns:
392 * The new refcount
394 *****************************************************************************/
395 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
397 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
398 ULONG refcount = InterlockedIncrement(&This->ref7);
400 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
402 if (refcount == 1)
404 ddraw_surface_add_iface(This);
407 return refcount;
410 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
412 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
413 ULONG refcount = InterlockedIncrement(&This->ref4);
415 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
417 if (refcount == 1)
419 ddraw_surface_add_iface(This);
422 return refcount;
425 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
427 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
428 ULONG refcount = InterlockedIncrement(&This->ref3);
430 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
432 if (refcount == 1)
434 ddraw_surface_add_iface(This);
437 return refcount;
440 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
442 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
443 ULONG refcount = InterlockedIncrement(&This->ref2);
445 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
447 if (refcount == 1)
449 ddraw_surface_add_iface(This);
452 return refcount;
455 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
457 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
458 ULONG refcount = InterlockedIncrement(&This->ref1);
460 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
462 if (refcount == 1)
464 ddraw_surface_add_iface(This);
467 return refcount;
470 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
472 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
473 ULONG refcount = InterlockedIncrement(&This->gamma_count);
475 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
477 if (refcount == 1)
479 ddraw_surface_add_iface(This);
482 return refcount;
485 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
487 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
489 TRACE("iface %p.\n", iface);
491 return IUnknown_AddRef(surface->texture_outer);
494 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
496 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
498 TRACE("iface %p.\n", iface);
500 return IUnknown_AddRef(surface->texture_outer);
503 static HRESULT ddraw_surface_set_palette(struct ddraw_surface *surface, IDirectDrawPalette *palette)
505 struct ddraw_palette *palette_impl = unsafe_impl_from_IDirectDrawPalette(palette);
506 struct ddraw_palette *prev;
508 TRACE("iface %p, palette %p.\n", surface, palette);
510 if (palette_impl && palette_impl->flags & DDPCAPS_ALPHA
511 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
513 WARN("Alpha palette set on non-texture surface, returning DDERR_INVALIDSURFACETYPE.\n");
514 return DDERR_INVALIDSURFACETYPE;
517 if (!format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
518 return DDERR_INVALIDPIXELFORMAT;
520 wined3d_mutex_lock();
522 prev = surface->palette;
523 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
525 if (prev)
526 prev->flags &= ~DDPCAPS_PRIMARYSURFACE;
527 if (palette_impl)
528 palette_impl->flags |= DDPCAPS_PRIMARYSURFACE;
529 wined3d_swapchain_set_palette(surface->ddraw->wined3d_swapchain,
530 palette_impl ? palette_impl->wined3d_palette : NULL);
531 ddraw_surface_update_frontbuffer(surface, NULL, FALSE, 0);
533 if (palette_impl)
534 IDirectDrawPalette_AddRef(&palette_impl->IDirectDrawPalette_iface);
535 if (prev)
536 IDirectDrawPalette_Release(&prev->IDirectDrawPalette_iface);
537 surface->palette = palette_impl;
539 wined3d_mutex_unlock();
541 return DD_OK;
544 static void ddraw_surface_cleanup(struct ddraw_surface *surface)
546 struct ddraw_surface *surf;
547 UINT i;
549 TRACE("surface %p.\n", surface);
551 /* The refcount test shows that the palette is detached when the surface
552 * is destroyed. */
553 ddraw_surface_set_palette(surface, NULL);
555 /* Loop through all complex attached surfaces and destroy them.
557 * Yet again, only the root can have more than one complexly attached
558 * surface, all the others have a total of one. */
559 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
561 if (!surface->complex_array[i])
562 break;
564 surf = surface->complex_array[i];
565 surface->complex_array[i] = NULL;
566 if (!surf->is_complex_root)
568 struct ddraw_texture *texture = wined3d_texture_get_parent(surf->wined3d_texture);
569 struct wined3d_device *wined3d_device = texture->wined3d_device;
570 struct ddraw_surface *root = texture->root;
572 ddraw_surface_cleanup(surf);
574 if (surf == root)
575 wined3d_device_decref(wined3d_device);
579 if (surface->device1)
580 IUnknown_Release(&surface->device1->IUnknown_inner);
582 if (surface->iface_count > 1)
584 /* This can happen when a complex surface is destroyed, because the
585 * 2nd surface was addref()ed when the app called
586 * GetAttachedSurface(). */
587 WARN("Destroying surface %p with refcounts 7: %u 4: %u 3: %u 2: %u 1: %u.\n",
588 surface, surface->ref7, surface->ref4, surface->ref3, surface->ref2, surface->ref1);
591 if (surface->wined3d_rtv)
592 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
593 wined3d_texture_decref(surface->draw_texture ? surface->draw_texture : surface->wined3d_texture);
596 static ULONG ddraw_surface_release_iface(struct ddraw_surface *This)
598 ULONG iface_count;
600 /* Prevent the surface from being destroyed if it's still attached to
601 * another surface. It will be destroyed when the root is destroyed. */
602 if (This->iface_count == 1 && This->attached_iface)
603 IUnknown_AddRef(This->attached_iface);
604 iface_count = InterlockedDecrement(&This->iface_count);
606 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
608 if (iface_count == 0)
610 struct ddraw_texture *texture = wined3d_texture_get_parent(This->wined3d_texture);
611 struct wined3d_device *wined3d_device = texture->wined3d_device;
612 IUnknown *release_iface = This->ifaceToRelease;
614 /* Complex attached surfaces are destroyed implicitly when the root is released */
615 wined3d_mutex_lock();
616 if(!This->is_complex_root)
618 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
619 wined3d_mutex_unlock();
620 return iface_count;
622 ddraw_surface_cleanup(This);
623 wined3d_mutex_unlock();
625 if (release_iface)
626 IUnknown_Release(release_iface);
627 /* Release the device only after anything that may reference it (the
628 * wined3d texture and rendertarget view in particular) is released. */
629 wined3d_device_decref(wined3d_device);
632 return iface_count;
635 /*****************************************************************************
636 * IDirectDrawSurface7::Release
638 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
639 * surface is destroyed.
641 * Destroying the surface is a bit tricky. For the connection between
642 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
643 * It has a nice graph explaining the connection.
645 * What happens here is basically this:
646 * When a surface is destroyed, its WineD3DSurface is released,
647 * and the refcount of the DirectDraw interface is reduced by 1. If it has
648 * complex surfaces attached to it, then these surfaces are destroyed too,
649 * regardless of their refcount. If any surface being destroyed has another
650 * surface attached to it (with a "soft" attachment, not complex), then
651 * this surface is detached with DeleteAttachedSurface.
653 * When the surface is a texture, the WineD3DTexture is released.
654 * If the surface is the Direct3D render target, then the D3D
655 * capabilities of the WineD3DDevice are uninitialized, which causes the
656 * swapchain to be released.
658 * When a complex sublevel falls to ref zero, then this is ignored.
660 * Returns:
661 * The new refcount
663 *****************************************************************************/
664 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
666 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
667 ULONG refcount = InterlockedDecrement(&This->ref7);
669 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
671 if (refcount == 0)
673 ddraw_surface_release_iface(This);
676 return refcount;
679 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
681 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
682 ULONG refcount = InterlockedDecrement(&This->ref4);
684 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
686 if (refcount == 0)
688 ddraw_surface_release_iface(This);
691 return refcount;
694 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
696 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
697 ULONG refcount = InterlockedDecrement(&This->ref3);
699 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
701 if (refcount == 0)
703 ddraw_surface_release_iface(This);
706 return refcount;
709 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
711 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
712 ULONG refcount = InterlockedDecrement(&This->ref2);
714 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
716 if (refcount == 0)
718 ddraw_surface_release_iface(This);
721 return refcount;
724 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
726 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
727 ULONG refcount = InterlockedDecrement(&This->ref1);
729 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
731 if (refcount == 0)
733 ddraw_surface_release_iface(This);
736 return refcount;
739 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
741 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
742 ULONG refcount = InterlockedDecrement(&This->gamma_count);
744 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
746 if (refcount == 0)
748 ddraw_surface_release_iface(This);
751 return refcount;
754 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
756 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
758 TRACE("iface %p.\n", iface);
760 return IUnknown_Release(surface->texture_outer);
763 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
765 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
767 TRACE("iface %p.\n", iface);
769 return IUnknown_Release(surface->texture_outer);
772 /*****************************************************************************
773 * IDirectDrawSurface7::GetAttachedSurface
775 * Returns an attached surface with the requested caps. Surface attachment
776 * and complex surfaces are not clearly described by the MSDN or sdk,
777 * so this method is tricky and likely to contain problems.
778 * This implementation searches the complex list first, then the
779 * attachment chain.
781 * The chains are searched from This down to the last surface in the chain,
782 * not from the first element in the chain. The first surface found is
783 * returned. The MSDN says that this method fails if more than one surface
784 * matches the caps, but it is not sure if that is right. The attachment
785 * structure may not even allow two matching surfaces.
787 * The found surface is AddRef-ed before it is returned.
789 * Params:
790 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
791 * Surface: Address to store the found surface
793 * Returns:
794 * DD_OK on success
795 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
796 * DDERR_NOTFOUND if no surface was found
798 *****************************************************************************/
799 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
800 DDSCAPS2 *caps, IDirectDrawSurface7 **surface)
802 struct ddraw_surface *head_surface = impl_from_IDirectDrawSurface7(iface);
803 struct ddraw_surface *surf;
804 DDSCAPS2 our_caps;
805 int i;
807 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, surface);
809 if (ddraw_surface_is_lost(head_surface))
811 WARN("Surface %p is lost.\n", head_surface);
813 *surface = NULL;
814 return DDERR_SURFACELOST;
817 wined3d_mutex_lock();
819 if(head_surface->version < 7)
821 /* Earlier dx apps put garbage into these members, clear them */
822 our_caps.dwCaps = caps->dwCaps;
823 our_caps.dwCaps2 = 0;
824 our_caps.dwCaps3 = 0;
825 our_caps.u1.dwCaps4 = 0;
827 else
829 our_caps = *caps;
832 TRACE("head_surface %p, looking for caps %#x, %#x, %#x, %#x.\n", head_surface, our_caps.dwCaps,
833 our_caps.dwCaps2, our_caps.dwCaps3, our_caps.u1.dwCaps4); /* FIXME: Better debugging */
835 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
837 surf = head_surface->complex_array[i];
838 if(!surf) break;
840 TRACE("Surface %p, caps %#x, %#x, %#x, %#x.\n", surf,
841 surf->surface_desc.ddsCaps.dwCaps,
842 surf->surface_desc.ddsCaps.dwCaps2,
843 surf->surface_desc.ddsCaps.dwCaps3,
844 surf->surface_desc.ddsCaps.u1.dwCaps4);
846 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
847 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
849 /* MSDN: "This method fails if more than one surface is attached
850 * that matches the capabilities requested."
852 * Not sure how to test this.
855 TRACE("head_surface %p, returning surface %p.\n", head_surface, surf);
856 *surface = &surf->IDirectDrawSurface7_iface;
857 ddraw_surface7_AddRef(*surface);
858 wined3d_mutex_unlock();
860 return DD_OK;
864 /* Next, look at the attachment chain */
865 surf = head_surface;
867 while( (surf = surf->next_attached) )
869 TRACE("Surface %p, caps %#x, %#x, %#x, %#x.\n", surf,
870 surf->surface_desc.ddsCaps.dwCaps,
871 surf->surface_desc.ddsCaps.dwCaps2,
872 surf->surface_desc.ddsCaps.dwCaps3,
873 surf->surface_desc.ddsCaps.u1.dwCaps4);
875 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
876 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
878 TRACE("head_surface %p, returning surface %p.\n", head_surface, surf);
879 *surface = &surf->IDirectDrawSurface7_iface;
880 ddraw_surface7_AddRef(*surface);
881 wined3d_mutex_unlock();
882 return DD_OK;
886 TRACE("head_surface %p, didn't find a valid surface.\n", head_surface);
888 wined3d_mutex_unlock();
890 *surface = NULL;
891 return DDERR_NOTFOUND;
894 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
895 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
897 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
898 struct ddraw_surface *attachment_impl;
899 IDirectDrawSurface7 *attachment7;
900 HRESULT hr;
902 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
904 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
905 caps, &attachment7);
906 if (FAILED(hr))
908 *attachment = NULL;
909 return hr;
911 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
912 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
913 ddraw_surface4_AddRef(*attachment);
914 ddraw_surface7_Release(attachment7);
916 return hr;
919 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
920 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
922 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
923 struct ddraw_surface *attachment_impl;
924 IDirectDrawSurface7 *attachment7;
925 DDSCAPS2 caps2;
926 HRESULT hr;
928 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
930 caps2.dwCaps = caps->dwCaps;
931 caps2.dwCaps2 = 0;
932 caps2.dwCaps3 = 0;
933 caps2.u1.dwCaps4 = 0;
935 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
936 &caps2, &attachment7);
937 if (FAILED(hr))
939 *attachment = NULL;
940 return hr;
942 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
943 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
944 ddraw_surface3_AddRef(*attachment);
945 ddraw_surface7_Release(attachment7);
947 return hr;
950 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
951 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
953 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
954 struct ddraw_surface *attachment_impl;
955 IDirectDrawSurface7 *attachment7;
956 DDSCAPS2 caps2;
957 HRESULT hr;
959 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
961 caps2.dwCaps = caps->dwCaps;
962 caps2.dwCaps2 = 0;
963 caps2.dwCaps3 = 0;
964 caps2.u1.dwCaps4 = 0;
966 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
967 &caps2, &attachment7);
968 if (FAILED(hr))
970 *attachment = NULL;
971 return hr;
973 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
974 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
975 ddraw_surface2_AddRef(*attachment);
976 ddraw_surface7_Release(attachment7);
978 return hr;
981 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
982 DDSCAPS *caps, IDirectDrawSurface **attachment)
984 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
985 struct ddraw_surface *attachment_impl;
986 IDirectDrawSurface7 *attachment7;
987 DDSCAPS2 caps2;
988 HRESULT hr;
990 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
992 caps2.dwCaps = caps->dwCaps;
993 caps2.dwCaps2 = 0;
994 caps2.dwCaps3 = 0;
995 caps2.u1.dwCaps4 = 0;
997 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
998 &caps2, &attachment7);
999 if (FAILED(hr))
1001 *attachment = NULL;
1002 return hr;
1004 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
1005 *attachment = &attachment_impl->IDirectDrawSurface_iface;
1006 ddraw_surface1_AddRef(*attachment);
1007 ddraw_surface7_Release(attachment7);
1009 return hr;
1012 /*****************************************************************************
1013 * IDirectDrawSurface7::Lock
1015 * Locks the surface and returns a pointer to the surface's memory
1017 * Params:
1018 * Rect: Rectangle to lock. If NULL, the whole surface is locked
1019 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
1020 * Flags: Locking flags, e.g Read only or write only
1021 * h: An event handle that's not used and must be NULL
1023 * Returns:
1024 * DD_OK on success
1025 * DDERR_INVALIDPARAMS if DDSD is NULL
1027 *****************************************************************************/
1028 static HRESULT surface_lock(struct ddraw_surface *surface,
1029 RECT *rect, DDSURFACEDESC2 *surface_desc, unsigned int surface_desc_size,
1030 DWORD flags, HANDLE h)
1032 struct wined3d_map_desc map_desc;
1033 unsigned int wined3d_flags;
1034 struct wined3d_box box;
1035 HRESULT hr = DD_OK;
1037 TRACE("surface %p, rect %s, surface_desc %p, surface_desc_size %u, flags %#x, h %p.\n",
1038 surface, wine_dbgstr_rect(rect), surface_desc, surface_desc_size, flags, h);
1040 /* surface->surface_desc.dwWidth and dwHeight are changeable, thus lock */
1041 wined3d_mutex_lock();
1043 /* Should I check for the handle to be NULL?
1045 * The DDLOCK flags and the D3DLOCK flags are equal
1046 * for the supported values. The others are ignored by WineD3D
1049 /* Windows zeroes this if the rect is invalid */
1050 surface_desc->lpSurface = NULL;
1052 if (rect)
1054 if ((rect->left < 0) || (rect->top < 0)
1055 || (rect->left > rect->right) || (rect->right > surface->surface_desc.dwWidth)
1056 || (rect->top > rect->bottom) || (rect->bottom > surface->surface_desc.dwHeight))
1058 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
1059 wined3d_mutex_unlock();
1060 return DDERR_INVALIDPARAMS;
1062 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
1065 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1066 hr = ddraw_surface_update_frontbuffer(surface, rect, TRUE, 0);
1067 if (SUCCEEDED(hr))
1069 wined3d_flags = wined3dmapflags_from_ddrawmapflags(flags);
1070 hr = wined3d_resource_map(wined3d_texture_get_resource
1071 (ddraw_surface_get_default_texture(surface, wined3d_flags & WINED3D_MAP_WRITE ? DDRAW_SURFACE_RW
1072 : DDRAW_SURFACE_READ)), surface->sub_resource_idx, &map_desc, rect ? &box : NULL, wined3d_flags);
1074 if (FAILED(hr))
1076 wined3d_mutex_unlock();
1077 switch(hr)
1079 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
1080 * specific error. But since wined3d returns that error in this only occasion,
1081 * keep d3d8 and d3d9 free from the return value override. There are many different
1082 * places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it is much easier
1083 * to do it in one place in ddraw.
1085 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
1086 default: return hr;
1090 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1092 if (flags & DDLOCK_READONLY)
1093 SetRectEmpty(&surface->ddraw->primary_lock);
1094 else if (rect)
1095 surface->ddraw->primary_lock = *rect;
1096 else
1097 SetRect(&surface->ddraw->primary_lock, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
1100 /* Windows does not set DDSD_LPSURFACE on locked surfaces. */
1101 DD_STRUCT_COPY_BYSIZE_(surface_desc, &surface->surface_desc, surface_desc_size, surface->surface_desc.dwSize);
1102 surface_desc->lpSurface = map_desc.data;
1104 TRACE("locked surface returning description :\n");
1105 if (TRACE_ON(ddraw))
1106 DDRAW_dump_surface_desc(surface_desc);
1108 wined3d_mutex_unlock();
1110 return DD_OK;
1113 static BOOL surface_validate_lock_desc(struct ddraw_surface *surface,
1114 const DDSURFACEDESC *desc, unsigned int *size)
1116 if (!desc)
1117 return FALSE;
1119 if (desc->dwSize == sizeof(DDSURFACEDESC) || desc->dwSize == sizeof(DDSURFACEDESC2))
1121 *size = desc->dwSize;
1122 return TRUE;
1125 if (surface->version == 7
1126 && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE
1127 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
1129 if (desc->dwSize >= sizeof(DDSURFACEDESC2))
1130 *size = sizeof(DDSURFACEDESC2);
1131 else
1132 *size = sizeof(DDSURFACEDESC);
1133 return TRUE;
1136 WARN("Invalid structure size %u.\n", desc->dwSize);
1137 return FALSE;
1140 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1141 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1143 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1144 unsigned int surface_desc_size;
1146 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1147 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1149 if (!surface_validate_lock_desc(surface, (DDSURFACEDESC *)surface_desc, &surface_desc_size))
1150 return DDERR_INVALIDPARAMS;
1152 if (ddraw_surface_is_lost(surface))
1154 WARN("Surface is lost.\n");
1155 return DDERR_SURFACELOST;
1158 return surface_lock(surface, rect, surface_desc, surface_desc_size, flags, h);
1161 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1162 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1164 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1165 unsigned int surface_desc_size;
1167 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1168 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1170 if (!surface_validate_lock_desc(surface, (DDSURFACEDESC *)surface_desc, &surface_desc_size))
1171 return DDERR_INVALIDPARAMS;
1173 if (ddraw_surface_is_lost(surface))
1175 WARN("Surface is lost.\n");
1176 return DDERR_SURFACELOST;
1179 return surface_lock(surface, rect, surface_desc, surface_desc_size, flags, h);
1182 static HRESULT ddraw_surface_lock_ddsd(struct ddraw_surface *surface, RECT *rect,
1183 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1185 unsigned int surface_desc_size;
1186 DDSURFACEDESC2 surface_desc2;
1187 HRESULT hr;
1189 if (!surface_validate_lock_desc(surface, surface_desc, &surface_desc_size))
1190 return DDERR_INVALIDPARAMS;
1192 if (ddraw_surface_is_lost(surface))
1194 WARN("Surface is lost.\n");
1195 return DDERR_SURFACELOST;
1198 surface_desc2.dwSize = surface_desc->dwSize;
1199 surface_desc2.dwFlags = 0;
1200 hr = surface_lock(surface, rect, &surface_desc2, surface_desc_size, flags, h);
1201 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1202 surface_desc->dwSize = surface_desc2.dwSize;
1203 return hr;
1206 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1207 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1209 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1211 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1212 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1214 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1217 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1218 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1220 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1222 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1223 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1225 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1228 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1229 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1231 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1233 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1234 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1236 return ddraw_surface_lock_ddsd(surface, rect, surface_desc, flags, h);
1239 /* FRAPS hooks IDirectDrawSurface::Unlock and expects the version 1 method to be called when the
1240 * game uses later interfaces. */
1241 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1243 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1244 HRESULT hr;
1246 TRACE("iface %p, data %p.\n", iface, data);
1248 wined3d_mutex_lock();
1249 hr = wined3d_resource_unmap(wined3d_texture_get_resource
1250 (ddraw_surface_get_default_texture(surface, 0)), surface->sub_resource_idx);
1251 if (SUCCEEDED(hr) && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1252 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE, 0);
1253 wined3d_mutex_unlock();
1255 return hr;
1258 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *rect)
1260 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1262 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1264 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, NULL);
1267 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *rect)
1269 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1271 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1273 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, NULL);
1276 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1278 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1280 TRACE("iface %p, data %p.\n", iface, data);
1282 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, data);
1285 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1287 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1289 TRACE("iface %p, data %p.\n", iface, data);
1291 return ddraw_surface1_Unlock(&surface->IDirectDrawSurface_iface, data);
1294 static unsigned int ddraw_swap_interval_from_flags(DWORD flags)
1296 if (flags & DDFLIP_NOVSYNC)
1297 return 0;
1299 switch (flags & (DDFLIP_INTERVAL2 | DDFLIP_INTERVAL3 | DDFLIP_INTERVAL4))
1301 case DDFLIP_INTERVAL2:
1302 return 2;
1303 case DDFLIP_INTERVAL3:
1304 return 3;
1305 case DDFLIP_INTERVAL4:
1306 return 4;
1307 default:
1308 return 1;
1312 /* FRAPS hooks IDirectDrawSurface::Flip and expects the version 1 method to be called when the
1313 * game uses later interfaces. */
1314 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Flip(IDirectDrawSurface *iface,
1315 IDirectDrawSurface *src, DWORD flags)
1317 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
1318 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
1319 struct ddraw_texture *dst_ddraw_texture, *src_ddraw_texture;
1320 struct wined3d_rendertarget_view *tmp_rtv, *src_rtv, *rtv;
1321 DDSCAPS caps = {DDSCAPS_FLIP};
1322 struct wined3d_texture *texture, *draw_texture;
1323 IDirectDrawSurface *current;
1324 void *texture_memory;
1325 HRESULT hr;
1327 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1329 if (src == iface || !(dst_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1330 return DDERR_NOTFLIPPABLE;
1332 if (ddraw_surface_is_lost(dst_impl))
1333 return DDERR_SURFACELOST;
1335 wined3d_mutex_lock();
1337 if ((dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1338 && !(dst_impl->ddraw->cooperative_level & DDSCL_EXCLUSIVE))
1340 WARN("Not in exclusive mode.\n");
1341 wined3d_mutex_unlock();
1342 return DDERR_NOEXCLUSIVEMODE;
1345 tmp_rtv = ddraw_surface_get_rendertarget_view(dst_impl);
1346 if (dst_impl->sub_resource_idx)
1347 ERR("Invalid sub-resource index %u on surface %p.\n", dst_impl->sub_resource_idx, dst_impl);
1348 texture = dst_impl->wined3d_texture;
1349 rtv = wined3d_device_context_get_rendertarget_view(dst_impl->ddraw->immediate_context, 0);
1350 dst_ddraw_texture = wined3d_texture_get_parent(dst_impl->wined3d_texture);
1351 texture_memory = dst_ddraw_texture->texture_memory;
1352 draw_texture = dst_impl->draw_texture;
1354 if (src_impl)
1356 for (current = iface; current != src;)
1358 if (FAILED(hr = ddraw_surface1_GetAttachedSurface(current, &caps, &current)))
1360 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1361 wined3d_mutex_unlock();
1362 return DDERR_NOTFLIPPABLE;
1364 ddraw_surface1_Release(current);
1365 if (current == iface)
1367 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1368 wined3d_mutex_unlock();
1369 return DDERR_NOTFLIPPABLE;
1373 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1374 if (rtv == dst_impl->wined3d_rtv)
1375 wined3d_device_context_set_rendertarget_views(dst_impl->ddraw->immediate_context, 0, 1, &src_rtv, FALSE);
1376 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1377 dst_impl->wined3d_rtv = src_rtv;
1378 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1379 if (src_impl->draw_texture)
1380 wined3d_texture_set_sub_resource_parent(src_impl->draw_texture, 0, dst_impl);
1381 src_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1382 dst_ddraw_texture->texture_memory = src_ddraw_texture->texture_memory;
1383 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), dst_ddraw_texture);
1384 if (src_impl->draw_texture)
1385 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->draw_texture), dst_ddraw_texture);
1386 dst_ddraw_texture = src_ddraw_texture;
1387 if (src_impl->sub_resource_idx)
1388 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1389 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1390 dst_impl->draw_texture = src_impl->draw_texture;
1392 else
1394 for (current = iface;;)
1396 if (FAILED(hr = ddraw_surface1_GetAttachedSurface(current, &caps, &current)))
1398 ERR("Can't find a flip target\n");
1399 wined3d_mutex_unlock();
1400 return DDERR_NOTFLIPPABLE; /* Unchecked */
1402 ddraw_surface1_Release(current);
1403 if (current == iface)
1405 dst_impl = impl_from_IDirectDrawSurface(iface);
1406 break;
1409 src_impl = impl_from_IDirectDrawSurface(current);
1410 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1411 if (rtv == dst_impl->wined3d_rtv)
1412 wined3d_device_context_set_rendertarget_views(dst_impl->ddraw->immediate_context,
1413 0, 1, &src_rtv, FALSE);
1414 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1415 dst_impl->wined3d_rtv = src_rtv;
1416 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1417 if (src_impl->draw_texture)
1418 wined3d_texture_set_sub_resource_parent(src_impl->draw_texture, 0, dst_impl);
1419 src_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1420 dst_ddraw_texture->texture_memory = src_ddraw_texture->texture_memory;
1421 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), dst_ddraw_texture);
1422 if (src_impl->draw_texture)
1423 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->draw_texture), dst_ddraw_texture);
1424 dst_ddraw_texture = src_ddraw_texture;
1425 if (src_impl->sub_resource_idx)
1426 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1427 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1428 dst_impl->draw_texture = src_impl->draw_texture;
1429 dst_impl = src_impl;
1433 /* We don't have to worry about potential texture bindings, since
1434 * flippable surfaces can never be textures. */
1435 if (rtv == src_impl->wined3d_rtv)
1436 wined3d_device_context_set_rendertarget_views(dst_impl->ddraw->immediate_context, 0, 1, &tmp_rtv, FALSE);
1437 wined3d_rendertarget_view_set_parent(tmp_rtv, src_impl);
1438 src_impl->wined3d_rtv = tmp_rtv;
1439 wined3d_texture_set_sub_resource_parent(texture, 0, src_impl);
1440 if (draw_texture)
1441 wined3d_texture_set_sub_resource_parent(draw_texture, 0, src_impl);
1442 dst_ddraw_texture->texture_memory = texture_memory;
1443 wined3d_resource_set_parent(wined3d_texture_get_resource(texture), dst_ddraw_texture);
1444 if (draw_texture)
1445 wined3d_resource_set_parent(wined3d_texture_get_resource(draw_texture), dst_ddraw_texture);
1446 src_impl->wined3d_texture = texture;
1447 src_impl->draw_texture = draw_texture;
1449 if (flags & ~(DDFLIP_NOVSYNC | DDFLIP_INTERVAL2 | DDFLIP_INTERVAL3 | DDFLIP_INTERVAL4))
1451 static UINT once;
1452 if (!once++)
1453 FIXME("Ignoring flags %#x.\n", flags);
1454 else
1455 WARN("Ignoring flags %#x.\n", flags);
1458 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1459 hr = ddraw_surface_update_frontbuffer(dst_impl, NULL, FALSE, ddraw_swap_interval_from_flags(flags));
1460 else
1461 hr = DD_OK;
1463 wined3d_mutex_unlock();
1465 return hr;
1468 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Flip(IDirectDrawSurface7 *iface,
1469 IDirectDrawSurface7 *src, DWORD flags)
1471 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1472 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src);
1474 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1476 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1477 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1480 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Flip(IDirectDrawSurface4 *iface,
1481 IDirectDrawSurface4 *src, DWORD flags)
1483 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1484 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
1486 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1488 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1489 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1492 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Flip(IDirectDrawSurface3 *iface,
1493 IDirectDrawSurface3 *src, DWORD flags)
1495 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1496 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src);
1498 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1500 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1501 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1504 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Flip(IDirectDrawSurface2 *iface,
1505 IDirectDrawSurface2 *src, DWORD flags)
1507 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1508 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src);
1510 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1512 return ddraw_surface1_Flip(&surface->IDirectDrawSurface_iface,
1513 src_impl ? &src_impl->IDirectDrawSurface_iface : NULL, flags);
1516 static HRESULT ddraw_surface_blt(struct ddraw_surface *dst_surface, const RECT *dst_rect,
1517 struct ddraw_surface *src_surface, const RECT *src_rect, DWORD flags, DWORD fill_colour,
1518 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1520 struct ddraw *ddraw = dst_surface->ddraw;
1521 struct wined3d_device *wined3d_device = ddraw->wined3d_device;
1522 struct wined3d_color colour;
1523 DWORD wined3d_flags;
1525 if (flags & DDBLT_COLORFILL)
1527 wined3d_flags = WINED3DCLEAR_TARGET;
1528 if (!(flags & DDBLT_ASYNC))
1529 wined3d_flags |= WINED3DCLEAR_SYNCHRONOUS;
1531 if (!wined3d_colour_from_ddraw_colour(&dst_surface->surface_desc.u4.ddpfPixelFormat,
1532 dst_surface->palette, fill_colour, &colour))
1533 return DDERR_INVALIDPARAMS;
1535 wined3d_device_apply_stateblock(wined3d_device, ddraw->state);
1536 ddraw_surface_get_draw_texture(dst_surface, dst_rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE);
1537 return wined3d_device_context_clear_rendertarget_view(ddraw->immediate_context,
1538 ddraw_surface_get_rendertarget_view(dst_surface),
1539 dst_rect, wined3d_flags, &colour, 0.0f, 0);
1542 if (flags & DDBLT_DEPTHFILL)
1544 wined3d_flags = WINED3DCLEAR_ZBUFFER;
1545 if (!(flags & DDBLT_ASYNC))
1546 wined3d_flags |= WINED3DCLEAR_SYNCHRONOUS;
1548 if (!wined3d_colour_from_ddraw_colour(&dst_surface->surface_desc.u4.ddpfPixelFormat,
1549 dst_surface->palette, fill_colour, &colour))
1550 return DDERR_INVALIDPARAMS;
1552 wined3d_device_apply_stateblock(wined3d_device, ddraw->state);
1553 ddraw_surface_get_draw_texture(dst_surface, dst_rect ? DDRAW_SURFACE_RW : DDRAW_SURFACE_WRITE);
1554 return wined3d_device_context_clear_rendertarget_view(ddraw->immediate_context,
1555 ddraw_surface_get_rendertarget_view(dst_surface),
1556 dst_rect, wined3d_flags, NULL, colour.r, 0);
1559 wined3d_flags = flags & ~DDBLT_ASYNC;
1560 if (wined3d_flags & ~WINED3D_BLT_MASK)
1562 FIXME("Unhandled flags %#x.\n", flags);
1563 return E_NOTIMPL;
1566 if (!(flags & DDBLT_ASYNC))
1567 wined3d_flags |= WINED3D_BLT_SYNCHRONOUS;
1569 return wined3d_device_context_blt(ddraw->immediate_context,
1570 ddraw_surface_get_any_texture(dst_surface, DDRAW_SURFACE_RW), dst_surface->sub_resource_idx, dst_rect,
1571 ddraw_surface_get_any_texture(src_surface, DDRAW_SURFACE_READ), src_surface->sub_resource_idx, src_rect,
1572 wined3d_flags, fx, filter);
1575 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1576 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags, DWORD fill_colour,
1577 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1579 RECT src_rect, dst_rect;
1580 float scale_x, scale_y;
1581 const RECT *clip_rect;
1582 UINT clip_list_size;
1583 RGNDATA *clip_list;
1584 HRESULT hr = DD_OK;
1585 UINT i;
1587 if (!dst_rect_in)
1588 SetRect(&dst_rect, 0, 0, dst_surface->surface_desc.dwWidth,
1589 dst_surface->surface_desc.dwHeight);
1590 else
1591 dst_rect = *dst_rect_in;
1593 if (IsRectEmpty(&dst_rect))
1594 return DDERR_INVALIDRECT;
1596 if (src_surface)
1598 if (!src_rect_in)
1599 SetRect(&src_rect, 0, 0, src_surface->surface_desc.dwWidth,
1600 src_surface->surface_desc.dwHeight);
1601 else
1602 src_rect = *src_rect_in;
1604 if (IsRectEmpty(&src_rect))
1605 return DDERR_INVALIDRECT;
1607 else
1609 SetRectEmpty(&src_rect);
1612 if (!dst_surface->clipper)
1614 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1615 hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect, TRUE, 0);
1616 if (SUCCEEDED(hr))
1617 hr = ddraw_surface_blt(dst_surface, &dst_rect, src_surface, &src_rect, flags, fill_colour, fx, filter);
1618 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
1619 hr = ddraw_surface_update_frontbuffer(dst_surface, &dst_rect, FALSE, 0);
1621 return hr;
1624 if (!ddraw_clipper_is_valid(dst_surface->clipper))
1626 FIXME("Attempting to blit with an invalid clipper.\n");
1627 return DDERR_INVALIDPARAMS;
1630 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1631 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1633 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1634 &dst_rect, NULL, &clip_list_size)))
1636 WARN("Failed to get clip list size, hr %#x.\n", hr);
1637 return hr;
1640 if (!(clip_list = heap_alloc(clip_list_size)))
1642 WARN("Failed to allocate clip list.\n");
1643 return E_OUTOFMEMORY;
1646 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1647 &dst_rect, clip_list, &clip_list_size)))
1649 WARN("Failed to get clip list, hr %#x.\n", hr);
1650 heap_free(clip_list);
1651 return hr;
1654 clip_rect = (RECT *)clip_list->Buffer;
1655 for (i = 0; i < clip_list->rdh.nCount; ++i)
1657 RECT src_rect_clipped = src_rect;
1659 if (src_surface)
1661 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1662 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1663 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1664 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1666 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1668 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE, 0)))
1669 break;
1673 if (FAILED(hr = ddraw_surface_blt(dst_surface, &clip_rect[i],
1674 src_surface, &src_rect_clipped, flags, fill_colour, fx, filter)))
1675 break;
1677 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1679 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE, 0)))
1680 break;
1684 heap_free(clip_list);
1685 return hr;
1688 /* FRAPS hooks IDirectDrawSurface::Blt and expects the version 1 method to be called when the
1689 * game uses later interfaces. */
1690 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1691 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1693 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
1694 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1695 struct wined3d_blt_fx wined3d_fx = {0};
1696 DWORD unsupported_flags;
1697 DWORD fill_colour = 0;
1698 HRESULT hr = DD_OK;
1699 DDBLTFX rop_fx;
1701 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1702 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1704 unsupported_flags = DDBLT_ALPHADEST
1705 | DDBLT_ALPHADESTCONSTOVERRIDE
1706 | DDBLT_ALPHADESTNEG
1707 | DDBLT_ALPHADESTSURFACEOVERRIDE
1708 | DDBLT_ALPHAEDGEBLEND
1709 | DDBLT_ALPHASRC
1710 | DDBLT_ALPHASRCCONSTOVERRIDE
1711 | DDBLT_ALPHASRCNEG
1712 | DDBLT_ALPHASRCSURFACEOVERRIDE
1713 | DDBLT_ZBUFFER
1714 | DDBLT_ZBUFFERDESTCONSTOVERRIDE
1715 | DDBLT_ZBUFFERDESTOVERRIDE
1716 | DDBLT_ZBUFFERSRCCONSTOVERRIDE
1717 | DDBLT_ZBUFFERSRCOVERRIDE;
1718 if (flags & unsupported_flags)
1720 WARN("Ignoring unsupported flags %#x.\n", flags & unsupported_flags);
1721 flags &= ~unsupported_flags;
1724 if ((flags & DDBLT_KEYSRCOVERRIDE) && (!fx || flags & DDBLT_KEYSRC))
1726 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1727 return DDERR_INVALIDPARAMS;
1730 if ((flags & DDBLT_KEYDESTOVERRIDE) && (!fx || flags & DDBLT_KEYDEST))
1732 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1733 return DDERR_INVALIDPARAMS;
1736 if (flags & DDBLT_DDROPS)
1738 FIXME("DDBLT_DDROPS not implemented.\n");
1739 if (fx)
1740 FIXME(" rop %#x, pattern %p.\n", fx->dwDDROP, fx->u5.lpDDSPattern);
1741 return DDERR_NORASTEROPHW;
1744 wined3d_mutex_lock();
1746 if (flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1748 if (flags & DDBLT_ROP)
1750 wined3d_mutex_unlock();
1751 WARN("DDBLT_ROP used with DDBLT_COLORFILL or DDBLT_DEPTHFILL, returning DDERR_INVALIDPARAMS.\n");
1752 return DDERR_INVALIDPARAMS;
1754 if (src_impl)
1756 wined3d_mutex_unlock();
1757 WARN("Depth or colorfill is not compatible with source surfaces, returning DDERR_INVALIDPARAMS\n");
1758 return DDERR_INVALIDPARAMS;
1760 if (!fx)
1762 wined3d_mutex_unlock();
1763 WARN("Depth or colorfill used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1764 return DDERR_INVALIDPARAMS;
1767 if ((flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) == (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1768 flags &= ~DDBLT_DEPTHFILL;
1770 if ((dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_COLORFILL))
1772 wined3d_mutex_unlock();
1773 WARN("DDBLT_COLORFILL used on a depth buffer, returning DDERR_INVALIDPARAMS.\n");
1774 return DDERR_INVALIDPARAMS;
1776 if (!(dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_DEPTHFILL))
1778 wined3d_mutex_unlock();
1779 WARN("DDBLT_DEPTHFILL used on a color buffer, returning DDERR_INVALIDPARAMS.\n");
1780 return DDERR_INVALIDPARAMS;
1784 if (flags & DDBLT_ROP)
1786 if (!fx)
1788 wined3d_mutex_unlock();
1789 WARN("DDBLT_ROP used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1790 return DDERR_INVALIDPARAMS;
1793 if (src_impl && src_rect
1794 && ((ULONG)src_rect->left >= src_rect->right || src_rect->right > src_impl->surface_desc.dwWidth
1795 || (ULONG)src_rect->top >= src_rect->bottom || src_rect->bottom > src_impl->surface_desc.dwHeight))
1797 wined3d_mutex_unlock();
1798 WARN("Invalid source rectangle.\n");
1799 return DDERR_INVALIDRECT;
1802 flags &= ~DDBLT_ROP;
1803 switch (fx->dwROP)
1805 case SRCCOPY:
1806 break;
1808 case WHITENESS:
1809 case BLACKNESS:
1810 rop_fx = *fx;
1812 if (fx->dwROP == WHITENESS)
1813 rop_fx.u5.dwFillColor = 0xffffffff;
1814 else
1815 rop_fx.u5.dwFillColor = 0;
1817 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1818 flags |= DDBLT_DEPTHFILL;
1819 else
1820 flags |= DDBLT_COLORFILL;
1822 fx = &rop_fx;
1823 break;
1825 default:
1826 wined3d_mutex_unlock();
1827 WARN("Unsupported ROP %#x used, returning DDERR_NORASTEROPHW.\n", fx->dwROP);
1828 return DDERR_NORASTEROPHW;
1832 if (!(flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) && !src_impl)
1834 WARN("No source surface.\n");
1835 wined3d_mutex_unlock();
1836 return DDERR_INVALIDPARAMS;
1839 if (flags & DDBLT_KEYSRC && (!src_impl || !(src_impl->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1841 WARN("DDBLT_KEYSRC blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1842 wined3d_mutex_unlock();
1843 return DDERR_INVALIDPARAMS;
1845 if (flags & DDBLT_KEYDEST && !(dst_impl->surface_desc.dwFlags & DDSD_CKDESTBLT))
1847 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1848 wined3d_mutex_unlock();
1849 return DDERR_INVALIDPARAMS;
1852 if (fx)
1854 wined3d_fx.fx = fx->dwDDFX;
1855 fill_colour = fx->u5.dwFillColor;
1856 wined3d_fx.dst_color_key.color_space_low_value = fx->ddckDestColorkey.dwColorSpaceLowValue;
1857 wined3d_fx.dst_color_key.color_space_high_value = fx->ddckDestColorkey.dwColorSpaceHighValue;
1858 wined3d_fx.src_color_key.color_space_low_value = fx->ddckSrcColorkey.dwColorSpaceLowValue;
1859 wined3d_fx.src_color_key.color_space_high_value = fx->ddckSrcColorkey.dwColorSpaceHighValue;
1862 hr = ddraw_surface_blt_clipped(dst_impl, dst_rect, src_impl,
1863 src_rect, flags, fill_colour, fx ? &wined3d_fx : NULL, WINED3D_TEXF_LINEAR);
1865 wined3d_mutex_unlock();
1866 switch(hr)
1868 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1869 default: return hr;
1873 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *dst_rect,
1874 IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1876 struct ddraw_surface *dst = impl_from_IDirectDrawSurface7(iface);
1877 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface7(src_surface);
1879 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1880 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1882 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1883 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1886 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1887 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1889 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1890 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1892 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1893 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1895 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1896 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1899 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1900 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1902 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1903 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1905 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1906 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1908 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1909 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1912 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1913 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1915 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1916 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1918 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1919 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1921 return ddraw_surface1_Blt(&dst->IDirectDrawSurface_iface, dst_rect,
1922 src ? &src->IDirectDrawSurface_iface : NULL, src_rect, flags, fx);
1925 /*****************************************************************************
1926 * IDirectDrawSurface7::AddAttachedSurface
1928 * Attaches a surface to another surface. How the surface attachments work
1929 * is not totally understood yet, and this method is prone to problems.
1930 * The surface that is attached is AddRef-ed.
1932 * Tests with complex surfaces suggest that the surface attachments form a
1933 * tree, but no method to test this has been found yet.
1935 * The attachment list consists of a first surface (first_attached) and
1936 * for each surface a pointer to the next attached surface (next_attached).
1937 * For the first surface, and a surface that has no attachments
1938 * first_attached points to the surface itself. A surface that has
1939 * no successors in the chain has next_attached set to NULL.
1941 * Newly attached surfaces are attached right after the root surface.
1942 * If a surface is attached to a complex surface compound, it's attached to
1943 * the surface that the app requested, not the complex root. See
1944 * GetAttachedSurface for a description how surfaces are found.
1946 * This is how the current implementation works, and it was coded by looking
1947 * at the needs of the applications.
1949 * So far only Z-Buffer attachments are tested, and they are activated in
1950 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1951 * Back buffers should work in 2D mode, but they are not tested(They can be
1952 * attached in older iface versions). Rendering to the front buffer and
1953 * switching between that and double buffering is not yet implemented in
1954 * WineD3D, so for 3D it might have unexpected results.
1956 * ddraw_surface_attach_surface is the real thing,
1957 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1958 * performs additional checks. Version 7 of this interface is much more restrictive
1959 * than its predecessors.
1961 * Params:
1962 * Attach: Surface to attach to iface
1964 * Returns:
1965 * DD_OK on success
1966 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1968 *****************************************************************************/
1969 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1971 TRACE("surface %p, attachment %p.\n", This, Surf);
1973 if(Surf == This)
1974 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1976 wined3d_mutex_lock();
1978 /* Check if the surface is already attached somewhere */
1979 if (Surf->next_attached || Surf->first_attached != Surf)
1981 /* TODO: Test for the structure of the manual attachment. Is it a
1982 * chain or a list? What happens if one surface is attached to 2
1983 * different surfaces? */
1984 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1985 Surf, Surf->next_attached, Surf->first_attached);
1987 wined3d_mutex_unlock();
1988 return DDERR_SURFACEALREADYATTACHED;
1991 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1992 Surf->next_attached = This->next_attached;
1993 Surf->first_attached = This->first_attached;
1994 This->next_attached = Surf;
1996 /* Check if the WineD3D depth stencil needs updating */
1997 if (This->ddraw->d3ddevice)
1998 d3d_device_update_depth_stencil(This->ddraw->d3ddevice);
2000 wined3d_mutex_unlock();
2002 return DD_OK;
2005 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
2007 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
2008 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
2009 HRESULT hr;
2011 TRACE("iface %p, attachment %p.\n", iface, attachment);
2013 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
2014 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
2017 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
2018 attachment_impl->surface_desc.ddsCaps.dwCaps);
2019 return DDERR_CANNOTATTACHSURFACE;
2022 hr = ddraw_surface_attach_surface(This, attachment_impl);
2023 if (FAILED(hr))
2025 return hr;
2027 attachment_impl->attached_iface = (IUnknown *)attachment;
2028 IUnknown_AddRef(attachment_impl->attached_iface);
2029 return hr;
2032 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
2034 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2035 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
2036 HRESULT hr;
2038 TRACE("iface %p, attachment %p.\n", iface, attachment);
2040 /* Tests suggest that
2041 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
2042 * -> offscreen plain surfaces can be attached to primaries
2043 * -> primaries can be attached to offscreen plain surfaces
2044 * -> z buffers can be attached to primaries */
2045 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
2046 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
2048 /* Sizes have to match */
2049 if (attachment_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
2050 || attachment_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
2052 WARN("Surface sizes do not match.\n");
2053 return DDERR_CANNOTATTACHSURFACE;
2056 else if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
2057 || !(attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER)))
2059 WARN("Invalid attachment combination.\n");
2060 return DDERR_CANNOTATTACHSURFACE;
2063 if (FAILED(hr = ddraw_surface_attach_surface(surface, attachment_impl)))
2064 return hr;
2066 attachment_impl->attached_iface = (IUnknown *)attachment;
2067 IUnknown_AddRef(attachment_impl->attached_iface);
2068 return hr;
2071 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
2073 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2074 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2075 HRESULT hr;
2077 TRACE("iface %p, attachment %p.\n", iface, attachment);
2079 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2080 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2081 return hr;
2083 attachment_impl->attached_iface = (IUnknown *)attachment;
2084 IUnknown_AddRef(attachment_impl->attached_iface);
2085 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2086 return hr;
2089 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
2091 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2092 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2093 HRESULT hr;
2095 TRACE("iface %p, attachment %p.\n", iface, attachment);
2097 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2098 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2099 return hr;
2101 attachment_impl->attached_iface = (IUnknown *)attachment;
2102 IUnknown_AddRef(attachment_impl->attached_iface);
2103 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2104 return hr;
2107 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
2109 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2110 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2111 HRESULT hr;
2113 TRACE("iface %p, attachment %p.\n", iface, attachment);
2115 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
2116 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
2117 return hr;
2119 attachment_impl->attached_iface = (IUnknown *)attachment;
2120 IUnknown_AddRef(attachment_impl->attached_iface);
2121 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
2122 return hr;
2125 /*****************************************************************************
2126 * IDirectDrawSurface7::DeleteAttachedSurface
2128 * Removes a surface from the attachment chain. The surface's refcount
2129 * is decreased by one after it has been removed
2131 * Params:
2132 * Flags: Some flags, not used by this implementation
2133 * Attach: Surface to detach
2135 * Returns:
2136 * DD_OK on success
2137 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
2139 *****************************************************************************/
2140 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
2141 struct ddraw_surface *attachment, IUnknown *detach_iface)
2143 struct wined3d_rendertarget_view *dsv;
2144 struct ddraw_surface *prev = surface;
2146 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
2148 wined3d_mutex_lock();
2149 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
2151 wined3d_mutex_unlock();
2152 return DDERR_CANNOTDETACHSURFACE;
2155 if (attachment->attached_iface != detach_iface)
2157 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
2158 wined3d_mutex_unlock();
2159 return DDERR_SURFACENOTATTACHED;
2162 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
2163 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2165 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2166 /* FIXME: we should probably also subtract from dwMipMapCount of this
2167 * and all parent surfaces */
2170 /* Find the predecessor of the detached surface */
2171 while (prev->next_attached != attachment)
2173 if (!(prev = prev->next_attached))
2175 ERR("Failed to find predecessor of %p.\n", attachment);
2176 wined3d_mutex_unlock();
2177 return DDERR_SURFACENOTATTACHED;
2181 /* Unchain the surface */
2182 prev->next_attached = attachment->next_attached;
2183 attachment->next_attached = NULL;
2184 attachment->first_attached = attachment;
2186 /* Check if the wined3d depth stencil needs updating. Note that we don't
2187 * just call d3d_device_update_depth_stencil() here since it uses
2188 * QueryInterface(). Some applications, SCP - Containment Breach in
2189 * particular, modify the QueryInterface() pointer in the surface vtbl
2190 * but don't cleanup properly after the relevant dll is unloaded. */
2191 dsv = wined3d_device_context_get_depth_stencil_view(surface->ddraw->immediate_context);
2192 if (attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER && dsv == attachment->wined3d_rtv)
2193 wined3d_device_context_set_depth_stencil_view(surface->ddraw->immediate_context, NULL);
2194 wined3d_mutex_unlock();
2196 /* Set attached_iface to NULL before releasing it, the surface may go
2197 * away. */
2198 attachment->attached_iface = NULL;
2199 IUnknown_Release(detach_iface);
2201 return DD_OK;
2204 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
2205 DWORD flags, IDirectDrawSurface7 *attachment)
2207 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2208 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
2210 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2212 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2215 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
2216 DWORD flags, IDirectDrawSurface4 *attachment)
2218 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2219 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
2221 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2223 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2226 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
2227 DWORD flags, IDirectDrawSurface3 *attachment)
2229 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2230 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2232 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2234 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2237 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
2238 DWORD flags, IDirectDrawSurface2 *attachment)
2240 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2241 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2243 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2245 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2248 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
2249 DWORD flags, IDirectDrawSurface *attachment)
2251 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2252 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2254 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2256 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2259 /*****************************************************************************
2260 * IDirectDrawSurface7::AddOverlayDirtyRect
2262 * "This method is not currently implemented"
2264 * Params:
2265 * Rect: ?
2267 * Returns:
2268 * DDERR_UNSUPPORTED
2270 *****************************************************************************/
2271 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
2273 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
2275 return DDERR_UNSUPPORTED; /* unchecked */
2278 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
2280 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2282 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2284 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2287 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
2289 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2291 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2293 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2296 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
2298 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2300 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2302 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2305 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
2307 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2309 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2311 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2314 /*****************************************************************************
2315 * IDirectDrawSurface7::GetDC
2317 * Returns a GDI device context for the surface
2319 * Params:
2320 * hdc: Address of a HDC variable to store the dc to
2322 * Returns:
2323 * DD_OK on success
2324 * DDERR_INVALIDPARAMS if hdc is NULL
2326 *****************************************************************************/
2327 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *dc)
2329 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2330 HRESULT hr = DD_OK;
2332 TRACE("iface %p, dc %p.\n", iface, dc);
2334 if (!dc)
2335 return DDERR_INVALIDPARAMS;
2337 wined3d_mutex_lock();
2338 if (surface->dc)
2339 hr = DDERR_DCALREADYCREATED;
2340 else if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2341 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE, 0);
2342 if (SUCCEEDED(hr))
2343 hr = wined3d_texture_get_dc(ddraw_surface_get_default_texture(surface, DDRAW_SURFACE_RW), surface->sub_resource_idx, dc);
2345 if (SUCCEEDED(hr))
2347 surface->dc = *dc;
2349 if (format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
2351 const struct ddraw_palette *palette;
2353 if (surface->palette)
2354 palette = surface->palette;
2355 else if (surface->ddraw->primary)
2356 palette = surface->ddraw->primary->palette;
2357 else
2358 palette = NULL;
2360 if (palette)
2361 wined3d_palette_apply_to_dc(palette->wined3d_palette, *dc);
2365 wined3d_mutex_unlock();
2366 switch (hr)
2368 /* Some, but not all errors set *dc to NULL. E.g. DCALREADYCREATED
2369 * does not touch *dc. */
2370 case WINED3DERR_INVALIDCALL:
2371 *dc = NULL;
2372 return DDERR_CANTCREATEDC;
2374 default:
2375 return hr;
2379 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
2381 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2383 TRACE("iface %p, dc %p.\n", iface, dc);
2385 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2388 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
2390 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2392 TRACE("iface %p, dc %p.\n", iface, dc);
2394 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2397 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2399 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2401 TRACE("iface %p, dc %p.\n", iface, dc);
2403 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2406 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2408 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2410 TRACE("iface %p, dc %p.\n", iface, dc);
2412 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2415 /*****************************************************************************
2416 * IDirectDrawSurface7::ReleaseDC
2418 * Releases the DC that was constructed with GetDC
2420 * Params:
2421 * hdc: HDC to release
2423 * Returns:
2424 * DD_OK on success, error code otherwise.
2426 *****************************************************************************/
2427 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2429 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2430 HRESULT hr;
2432 TRACE("iface %p, dc %p.\n", iface, hdc);
2434 wined3d_mutex_lock();
2435 if (!surface->dc)
2437 hr = DDERR_NODC;
2439 else if (SUCCEEDED(hr = wined3d_texture_release_dc(ddraw_surface_get_default_texture(surface, 0),
2440 surface->sub_resource_idx, hdc)))
2442 surface->dc = NULL;
2443 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2444 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE, 0);
2446 wined3d_mutex_unlock();
2449 return hr;
2452 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2454 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2456 TRACE("iface %p, dc %p.\n", iface, dc);
2458 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2461 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2463 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2465 TRACE("iface %p, dc %p.\n", iface, dc);
2467 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2470 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2472 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2474 TRACE("iface %p, dc %p.\n", iface, dc);
2476 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2479 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2481 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2483 TRACE("iface %p, dc %p.\n", iface, dc);
2485 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2488 /*****************************************************************************
2489 * IDirectDrawSurface7::GetCaps
2491 * Returns the surface's caps
2493 * Params:
2494 * Caps: Address to write the caps to
2496 * Returns:
2497 * DD_OK on success
2498 * DDERR_INVALIDPARAMS if Caps is NULL
2500 *****************************************************************************/
2501 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2503 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2505 TRACE("iface %p, caps %p.\n", iface, Caps);
2507 if(!Caps)
2508 return DDERR_INVALIDPARAMS;
2510 *Caps = surface->surface_desc.ddsCaps;
2512 return DD_OK;
2515 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2517 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2519 TRACE("iface %p, caps %p.\n", iface, caps);
2521 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2524 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2526 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2527 DDSCAPS2 caps2;
2528 HRESULT hr;
2530 TRACE("iface %p, caps %p.\n", iface, caps);
2532 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2533 if (FAILED(hr)) return hr;
2535 caps->dwCaps = caps2.dwCaps;
2536 return hr;
2539 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2541 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2542 DDSCAPS2 caps2;
2543 HRESULT hr;
2545 TRACE("iface %p, caps %p.\n", iface, caps);
2547 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2548 if (FAILED(hr)) return hr;
2550 caps->dwCaps = caps2.dwCaps;
2551 return hr;
2554 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2556 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2557 DDSCAPS2 caps2;
2558 HRESULT hr;
2560 TRACE("iface %p, caps %p.\n", iface, caps);
2562 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2563 if (FAILED(hr)) return hr;
2565 caps->dwCaps = caps2.dwCaps;
2566 return hr;
2569 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD priority)
2571 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2572 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2573 HRESULT hr;
2574 struct wined3d_resource *resource;
2576 TRACE("iface %p, priority %u.\n", iface, priority);
2578 wined3d_mutex_lock();
2579 /* No need to check for offscreen plain surfaces or mipmap sublevels. SetPriority
2580 * calls on such surfaces segfault on Windows. */
2581 if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed))
2583 WARN("Called on non-managed texture returning DDERR_INVALIDPARAMS.\n");
2584 hr = DDERR_INVALIDPARAMS;
2586 else
2588 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2589 wined3d_resource_set_priority(resource, priority);
2590 if (surface->draw_texture)
2591 wined3d_resource_set_priority(wined3d_texture_get_resource(surface->draw_texture), priority);
2593 hr = DD_OK;
2595 wined3d_mutex_unlock();
2597 return hr;
2600 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *priority)
2602 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2603 const struct wined3d_resource *resource;
2604 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2605 HRESULT hr;
2607 TRACE("iface %p, priority %p.\n", iface, priority);
2609 wined3d_mutex_lock();
2610 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN)
2612 WARN("Called on offscreenplain surface, returning DDERR_INVALIDOBJECT.\n");
2613 hr = DDERR_INVALIDOBJECT;
2615 else if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed) || !surface->is_complex_root)
2617 WARN("Called on non-managed texture or non-root surface, returning DDERR_INVALIDPARAMS.\n");
2618 hr = DDERR_INVALIDPARAMS;
2620 else
2622 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2623 *priority = wined3d_resource_get_priority(resource);
2624 hr = DD_OK;
2626 wined3d_mutex_unlock();
2628 return hr;
2631 /*****************************************************************************
2632 * IDirectDrawSurface7::SetPrivateData
2634 * Stores some data in the surface that is intended for the application's
2635 * use.
2637 * Params:
2638 * tag: GUID that identifies the data
2639 * Data: Pointer to the private data
2640 * Size: Size of the private data
2641 * Flags: Some flags
2643 * Returns:
2644 * D3D_OK on success, error code otherwise.
2646 *****************************************************************************/
2647 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2648 REFGUID tag, void *data, DWORD size, DWORD flags)
2650 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2651 HRESULT hr;
2653 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2654 iface, debugstr_guid(tag), data, size, flags);
2656 if (!data)
2658 WARN("data is NULL, returning DDERR_INVALIDPARAMS.\n");
2659 return DDERR_INVALIDPARAMS;
2662 wined3d_mutex_lock();
2663 hr = wined3d_private_store_set_private_data(&surface->private_store, tag, data, size, flags);
2664 wined3d_mutex_unlock();
2665 return hr_ddraw_from_wined3d(hr);
2668 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2669 REFGUID tag, void *data, DWORD size, DWORD flags)
2671 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2673 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2674 iface, debugstr_guid(tag), data, size, flags);
2676 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2679 /*****************************************************************************
2680 * IDirectDrawSurface7::GetPrivateData
2682 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2684 * Params:
2685 * tag: GUID of the data to return
2686 * Data: Address where to write the data to
2687 * Size: Size of the buffer at Data
2689 * Returns:
2690 * DD_OK on success
2691 * DDERR_INVALIDPARAMS if Data is NULL
2693 *****************************************************************************/
2694 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *data, DWORD *size)
2696 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2697 const struct wined3d_private_data *stored_data;
2698 HRESULT hr;
2700 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2701 iface, debugstr_guid(tag), data, size);
2703 wined3d_mutex_lock();
2704 stored_data = wined3d_private_store_get_private_data(&surface->private_store, tag);
2705 if (!stored_data)
2707 hr = DDERR_NOTFOUND;
2708 goto done;
2710 if (!size)
2712 hr = DDERR_INVALIDPARAMS;
2713 goto done;
2715 if (*size < stored_data->size)
2717 *size = stored_data->size;
2718 hr = DDERR_MOREDATA;
2719 goto done;
2721 if (!data)
2723 hr = DDERR_INVALIDPARAMS;
2724 goto done;
2727 *size = stored_data->size;
2728 memcpy(data, stored_data->content.data, stored_data->size);
2729 hr = DD_OK;
2731 done:
2732 wined3d_mutex_unlock();
2733 return hr;
2736 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2738 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2740 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2741 iface, debugstr_guid(tag), data, size);
2743 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2746 /*****************************************************************************
2747 * IDirectDrawSurface7::FreePrivateData
2749 * Frees private data stored in the surface
2751 * Params:
2752 * tag: Tag of the data to free
2754 * Returns:
2755 * D3D_OK on success, error code otherwise.
2757 *****************************************************************************/
2758 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2760 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2761 struct wined3d_private_data *entry;
2763 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2765 wined3d_mutex_lock();
2766 entry = wined3d_private_store_get_private_data(&surface->private_store, tag);
2767 if (!entry)
2769 wined3d_mutex_unlock();
2770 return DDERR_NOTFOUND;
2773 wined3d_private_store_free_private_data(&surface->private_store, entry);
2774 wined3d_mutex_unlock();
2776 return DD_OK;
2779 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2781 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2783 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2785 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2788 /*****************************************************************************
2789 * IDirectDrawSurface7::PageLock
2791 * Prevents a sysmem surface from being paged out
2793 * Params:
2794 * Flags: Not used, must be 0(unchecked)
2796 * Returns:
2797 * DD_OK, because it's a stub
2799 *****************************************************************************/
2800 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2802 TRACE("iface %p, flags %#x.\n", iface, Flags);
2804 /* This is Windows memory management related - we don't need this */
2805 return DD_OK;
2808 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2810 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2812 TRACE("iface %p, flags %#x.\n", iface, flags);
2814 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2817 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2819 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2821 TRACE("iface %p, flags %#x.\n", iface, flags);
2823 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2826 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2828 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2830 TRACE("iface %p, flags %#x.\n", iface, flags);
2832 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2835 /*****************************************************************************
2836 * IDirectDrawSurface7::PageUnlock
2838 * Allows a sysmem surface to be paged out
2840 * Params:
2841 * Flags: Not used, must be 0(unchecked)
2843 * Returns:
2844 * DD_OK, because it's a stub
2846 *****************************************************************************/
2847 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2849 TRACE("iface %p, flags %#x.\n", iface, Flags);
2851 return DD_OK;
2854 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2856 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2858 TRACE("iface %p, flags %#x.\n", iface, flags);
2860 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2863 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2865 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2867 TRACE("iface %p, flags %#x.\n", iface, flags);
2869 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2872 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2874 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2876 TRACE("iface %p, flags %#x.\n", iface, flags);
2878 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2881 /*****************************************************************************
2882 * IDirectDrawSurface7::BltBatch
2884 * An unimplemented function
2886 * Params:
2889 * Returns:
2890 * DDERR_UNSUPPORTED
2892 *****************************************************************************/
2893 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2895 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2897 /* MSDN: "not currently implemented" */
2898 return DDERR_UNSUPPORTED;
2901 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2903 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2905 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2907 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2910 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2912 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2914 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2916 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2919 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2921 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2923 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2925 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2928 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2930 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2932 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2934 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2937 /*****************************************************************************
2938 * IDirectDrawSurface7::EnumAttachedSurfaces
2940 * Enumerates all surfaces attached to this surface
2942 * Params:
2943 * context: Pointer to pass unmodified to the callback
2944 * cb: Callback function to call for each surface
2946 * Returns:
2947 * DD_OK on success
2948 * DDERR_INVALIDPARAMS if cb is NULL
2950 *****************************************************************************/
2951 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2952 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2954 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2955 struct ddraw_surface *surf;
2956 DDSURFACEDESC2 desc;
2957 int i;
2959 /* Attached surfaces aren't handled in WineD3D */
2960 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2962 if(!cb)
2963 return DDERR_INVALIDPARAMS;
2965 wined3d_mutex_lock();
2967 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2969 surf = surface->complex_array[i];
2970 if(!surf) break;
2972 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2973 desc = surf->surface_desc;
2974 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2975 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2977 wined3d_mutex_unlock();
2978 return DD_OK;
2982 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2984 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2985 desc = surf->surface_desc;
2986 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2987 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2989 wined3d_mutex_unlock();
2990 return DD_OK;
2994 TRACE(" end of enumeration.\n");
2996 wined3d_mutex_unlock();
2998 return DD_OK;
3001 struct callback_info2
3003 LPDDENUMSURFACESCALLBACK2 callback;
3004 void *context;
3007 struct callback_info
3009 LPDDENUMSURFACESCALLBACK callback;
3010 void *context;
3013 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
3015 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3016 const struct callback_info2 *info = context;
3018 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
3019 ddraw_surface7_Release(surface);
3021 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
3024 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
3026 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
3027 const struct callback_info *info = context;
3029 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
3030 ddraw_surface7_Release(surface);
3032 /* FIXME: Check surface_test.dwSize */
3033 return info->callback(&surface_impl->IDirectDrawSurface_iface,
3034 (DDSURFACEDESC *)surface_desc, info->context);
3037 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
3038 void *context, LPDDENUMSURFACESCALLBACK2 callback)
3040 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3041 struct callback_info2 info;
3043 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3045 info.callback = callback;
3046 info.context = context;
3048 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3049 &info, EnumCallback2);
3052 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
3053 void *context, LPDDENUMSURFACESCALLBACK callback)
3055 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3056 struct callback_info info;
3058 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3060 info.callback = callback;
3061 info.context = context;
3063 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3064 &info, EnumCallback);
3067 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
3068 void *context, LPDDENUMSURFACESCALLBACK callback)
3070 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3071 struct callback_info info;
3073 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3075 info.callback = callback;
3076 info.context = context;
3078 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3079 &info, EnumCallback);
3082 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
3083 void *context, LPDDENUMSURFACESCALLBACK callback)
3085 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3086 struct callback_info info;
3088 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
3090 info.callback = callback;
3091 info.context = context;
3093 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
3094 &info, EnumCallback);
3097 /*****************************************************************************
3098 * IDirectDrawSurface7::EnumOverlayZOrders
3100 * "Enumerates the overlay surfaces on the specified destination"
3102 * Params:
3103 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
3104 * context: context to pass back to the callback
3105 * cb: callback function to call for each enumerated surface
3107 * Returns:
3108 * DD_OK, because it's a stub
3110 *****************************************************************************/
3111 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
3112 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
3114 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
3116 return DD_OK;
3119 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
3120 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
3122 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3123 struct callback_info2 info;
3125 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3127 info.callback = callback;
3128 info.context = context;
3130 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3131 flags, &info, EnumCallback2);
3134 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
3135 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3137 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3138 struct callback_info info;
3140 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3142 info.callback = callback;
3143 info.context = context;
3145 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3146 flags, &info, EnumCallback);
3149 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
3150 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3152 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3153 struct callback_info info;
3155 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3157 info.callback = callback;
3158 info.context = context;
3160 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3161 flags, &info, EnumCallback);
3164 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
3165 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
3167 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3168 struct callback_info info;
3170 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
3172 info.callback = callback;
3173 info.context = context;
3175 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
3176 flags, &info, EnumCallback);
3179 /*****************************************************************************
3180 * IDirectDrawSurface7::GetBltStatus
3182 * Returns the blitting status
3184 * Params:
3185 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
3187 *****************************************************************************/
3188 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3190 TRACE("iface %p, flags %#x.\n", iface, Flags);
3192 switch (Flags)
3194 case WINEDDGBS_CANBLT:
3195 case WINEDDGBS_ISBLTDONE:
3196 return DD_OK;
3198 default:
3199 return DDERR_INVALIDPARAMS;
3203 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
3205 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3207 TRACE("iface %p, flags %#x.\n", iface, flags);
3209 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3212 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
3214 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3216 TRACE("iface %p, flags %#x.\n", iface, flags);
3218 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3221 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
3223 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3225 TRACE("iface %p, flags %#x.\n", iface, flags);
3227 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3230 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
3232 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3234 TRACE("iface %p, flags %#x.\n", iface, flags);
3236 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3239 /*****************************************************************************
3240 * IDirectDrawSurface7::GetColorKey
3242 * Returns the color key assigned to the surface
3244 * Params:
3245 * Flags: Some flags
3246 * CKey: Address to store the key to
3248 * Returns:
3249 * DD_OK on success
3250 * DDERR_INVALIDPARAMS if CKey is NULL
3252 *****************************************************************************/
3253 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
3255 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3257 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
3259 if(!CKey)
3260 return DDERR_INVALIDPARAMS;
3262 wined3d_mutex_lock();
3264 switch (Flags)
3266 case DDCKEY_DESTBLT:
3267 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
3269 wined3d_mutex_unlock();
3270 return DDERR_NOCOLORKEY;
3272 *CKey = This->surface_desc.ddckCKDestBlt;
3273 break;
3275 case DDCKEY_DESTOVERLAY:
3276 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
3278 wined3d_mutex_unlock();
3279 return DDERR_NOCOLORKEY;
3281 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
3282 break;
3284 case DDCKEY_SRCBLT:
3285 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
3287 wined3d_mutex_unlock();
3288 return DDERR_NOCOLORKEY;
3290 *CKey = This->surface_desc.ddckCKSrcBlt;
3291 break;
3293 case DDCKEY_SRCOVERLAY:
3294 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
3296 wined3d_mutex_unlock();
3297 return DDERR_NOCOLORKEY;
3299 *CKey = This->surface_desc.ddckCKSrcOverlay;
3300 break;
3302 default:
3303 wined3d_mutex_unlock();
3304 return DDERR_INVALIDPARAMS;
3307 wined3d_mutex_unlock();
3309 return DD_OK;
3312 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
3314 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3316 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3318 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3321 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
3323 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3325 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3327 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3330 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
3332 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3334 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3336 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3339 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
3341 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3343 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3345 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3348 /*****************************************************************************
3349 * IDirectDrawSurface7::GetFlipStatus
3351 * Returns the flipping status of the surface
3353 * Params:
3354 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
3356 *****************************************************************************/
3357 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3359 TRACE("iface %p, flags %#x.\n", iface, Flags);
3361 /* XXX: DDERR_INVALIDSURFACETYPE */
3363 switch (Flags)
3365 case WINEDDGFS_CANFLIP:
3366 case WINEDDGFS_ISFLIPDONE:
3367 return DD_OK;
3369 default:
3370 return DDERR_INVALIDPARAMS;
3374 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
3376 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3378 TRACE("iface %p, flags %#x.\n", iface, flags);
3380 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3383 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
3385 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3387 TRACE("iface %p, flags %#x.\n", iface, flags);
3389 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3392 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
3394 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3396 TRACE("iface %p, flags %#x.\n", iface, flags);
3398 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3401 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
3403 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3405 TRACE("iface %p, flags %#x.\n", iface, flags);
3407 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3410 /*****************************************************************************
3411 * IDirectDrawSurface7::GetOverlayPosition
3413 * Returns the display coordinates of a visible and active overlay surface
3415 * Params:
3419 * Returns:
3420 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3421 *****************************************************************************/
3422 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *x, LONG *y)
3424 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3425 HRESULT hr;
3427 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3429 wined3d_mutex_lock();
3430 hr = wined3d_texture_get_overlay_position(surface->wined3d_texture,
3431 surface->sub_resource_idx, x, y);
3432 wined3d_mutex_unlock();
3434 return hr;
3437 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3439 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3441 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3443 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3446 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3448 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3450 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3452 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3455 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3457 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3459 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3461 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3464 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3466 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3468 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3470 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3473 /*****************************************************************************
3474 * IDirectDrawSurface7::GetPixelFormat
3476 * Returns the pixel format of the Surface
3478 * Params:
3479 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3480 * format should be written
3482 * Returns:
3483 * DD_OK on success
3484 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3486 *****************************************************************************/
3487 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3489 /* What is DDERR_INVALIDSURFACETYPE for here? */
3490 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3492 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3494 if(!PixelFormat)
3495 return DDERR_INVALIDPARAMS;
3497 wined3d_mutex_lock();
3498 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3499 wined3d_mutex_unlock();
3501 return DD_OK;
3504 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3506 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3508 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3510 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3513 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3515 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3517 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3519 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3522 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3524 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3526 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3528 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3531 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3533 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3535 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3537 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3540 /*****************************************************************************
3541 * IDirectDrawSurface7::GetSurfaceDesc
3543 * Returns the description of this surface
3545 * Params:
3546 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3547 * surface desc
3549 * Returns:
3550 * DD_OK on success
3551 * DDERR_INVALIDPARAMS if DDSD is NULL
3553 *****************************************************************************/
3554 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3556 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3558 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3560 if(!DDSD)
3561 return DDERR_INVALIDPARAMS;
3563 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3565 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3566 return DDERR_INVALIDPARAMS;
3569 wined3d_mutex_lock();
3570 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3571 TRACE("Returning surface desc:\n");
3572 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3573 wined3d_mutex_unlock();
3575 return DD_OK;
3578 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3580 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3582 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3584 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3587 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3589 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3591 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3593 if (!surface_desc) return DDERR_INVALIDPARAMS;
3595 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3597 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3598 return DDERR_INVALIDPARAMS;
3601 wined3d_mutex_lock();
3602 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3603 TRACE("Returning surface desc:\n");
3604 if (TRACE_ON(ddraw))
3606 /* DDRAW_dump_surface_desc handles the smaller size */
3607 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3609 wined3d_mutex_unlock();
3611 return DD_OK;
3614 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3616 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3618 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3620 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3623 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3625 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3627 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3629 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3632 /*****************************************************************************
3633 * IDirectDrawSurface7::Initialize
3635 * Initializes the surface. This is a no-op in Wine
3637 * Params:
3638 * DD: Pointer to an DirectDraw interface
3639 * DDSD: Surface description for initialization
3641 * Returns:
3642 * DDERR_ALREADYINITIALIZED
3644 *****************************************************************************/
3645 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3646 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3648 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3650 return DDERR_ALREADYINITIALIZED;
3653 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3654 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3656 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3658 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3660 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3661 ddraw, surface_desc);
3664 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3665 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3667 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3668 DDSURFACEDESC2 surface_desc2;
3670 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3672 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3673 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3674 ddraw, surface_desc ? &surface_desc2 : NULL);
3677 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3678 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3680 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3681 DDSURFACEDESC2 surface_desc2;
3683 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3685 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3686 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3687 ddraw, surface_desc ? &surface_desc2 : NULL);
3690 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3691 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3693 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3694 DDSURFACEDESC2 surface_desc2;
3696 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3698 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3699 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3700 ddraw, surface_desc ? &surface_desc2 : NULL);
3703 /*****************************************************************************
3704 * IDirect3DTexture1::Initialize
3706 * The sdk says it's not implemented
3708 * Params:
3711 * Returns
3712 * DDERR_UNSUPPORTED
3714 *****************************************************************************/
3715 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3716 IDirect3DDevice *device, IDirectDrawSurface *surface)
3718 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3720 return DDERR_UNSUPPORTED; /* Unchecked */
3723 /*****************************************************************************
3724 * IDirectDrawSurface7::IsLost
3726 * Checks if the surface is lost
3728 * Returns:
3729 * DD_OK, if the surface is usable
3730 * DDERR_ISLOST if the surface is lost
3732 *****************************************************************************/
3733 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3735 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3737 TRACE("iface %p.\n", iface);
3739 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3742 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3744 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3746 TRACE("iface %p.\n", iface);
3748 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3751 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3753 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3755 TRACE("iface %p.\n", iface);
3757 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3760 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3762 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3764 TRACE("iface %p.\n", iface);
3766 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3769 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3771 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3773 TRACE("iface %p.\n", iface);
3775 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3778 /*****************************************************************************
3779 * IDirectDrawSurface7::Restore
3781 * Restores a lost surface. This makes the surface usable again, but
3782 * doesn't reload its old contents
3784 * Returns:
3785 * DD_OK on success, error code otherwise.
3787 *****************************************************************************/
3788 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3790 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3791 unsigned int i;
3793 TRACE("iface %p.\n", iface);
3795 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3797 struct wined3d_swapchain *swapchain = surface->ddraw->wined3d_swapchain;
3798 struct wined3d_sub_resource_desc wined3d_desc;
3799 struct wined3d_display_mode mode;
3800 HRESULT hr;
3802 if (FAILED(hr = wined3d_swapchain_get_display_mode(swapchain, &mode, NULL)))
3804 WARN("Failed to get display mode, hr %#x.\n", hr);
3805 return hr;
3808 if (FAILED(hr = wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, 0, &wined3d_desc)))
3810 WARN("Failed to get resource desc, hr %#x.\n", hr);
3811 return hr;
3814 if (mode.width != wined3d_desc.width || mode.height != wined3d_desc.height)
3816 WARN("Display mode dimensions %ux%u don't match surface dimensions %ux%u.\n",
3817 mode.width, mode.height, wined3d_desc.width, wined3d_desc.height);
3818 return DDERR_WRONGMODE;
3821 if (mode.format_id != wined3d_desc.format)
3823 WARN("Display mode format %#x doesn't match surface format %#x.\n",
3824 mode.format_id, wined3d_desc.format);
3825 return DDERR_WRONGMODE;
3829 if (!ddraw_surface_can_be_lost(surface))
3830 return DD_OK;
3831 ddraw_update_lost_surfaces(surface->ddraw);
3832 if (surface->ddraw->device_state == DDRAW_DEVICE_STATE_LOST)
3833 return DDERR_WRONGMODE;
3835 surface->is_lost = FALSE;
3836 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
3838 if (surface->complex_array[i])
3839 surface->complex_array[i]->is_lost = FALSE;
3842 return DD_OK;
3845 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3847 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3849 TRACE("iface %p.\n", iface);
3851 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3854 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3856 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3858 TRACE("iface %p.\n", iface);
3860 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3863 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3865 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3867 TRACE("iface %p.\n", iface);
3869 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3872 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3874 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3876 TRACE("iface %p.\n", iface);
3878 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3881 /*****************************************************************************
3882 * IDirectDrawSurface7::SetOverlayPosition
3884 * Changes the display coordinates of an overlay surface
3886 * Params:
3887 * X:
3888 * Y:
3890 * Returns:
3891 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3892 *****************************************************************************/
3893 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG x, LONG y)
3895 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3896 HRESULT hr;
3898 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3900 wined3d_mutex_lock();
3901 hr = wined3d_texture_set_overlay_position(surface->wined3d_texture,
3902 surface->sub_resource_idx, x, y);
3903 wined3d_mutex_unlock();
3905 return hr;
3908 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3910 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3912 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3914 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3917 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3919 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3921 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3923 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3926 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3928 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3930 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3932 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3935 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3937 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3939 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3941 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3944 /*****************************************************************************
3945 * IDirectDrawSurface7::UpdateOverlay
3947 * Modifies the attributes of an overlay surface.
3949 * Params:
3950 * SrcRect: The section of the source being used for the overlay
3951 * DstSurface: Address of the surface that is overlaid
3952 * DstRect: Place of the overlay
3953 * Flags: some DDOVER_* flags
3955 * Returns:
3956 * DDERR_UNSUPPORTED, because we don't support overlays
3958 *****************************************************************************/
3959 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *src_rect,
3960 IDirectDrawSurface7 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3962 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3963 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(dst_surface);
3964 struct wined3d_texture *dst_wined3d_texture = NULL;
3965 unsigned int dst_sub_resource_idx = 0;
3966 HRESULT hr;
3968 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3969 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3971 if (fx)
3972 FIXME("Ignoring fx %p.\n", fx);
3974 wined3d_mutex_lock();
3975 if (dst_impl)
3977 dst_wined3d_texture = dst_impl->wined3d_texture;
3978 dst_sub_resource_idx = dst_impl->sub_resource_idx;
3980 hr = wined3d_texture_update_overlay(src_impl->wined3d_texture, src_impl->sub_resource_idx,
3981 src_rect, dst_wined3d_texture, dst_sub_resource_idx, dst_rect, flags);
3982 wined3d_mutex_unlock();
3984 return hr_ddraw_from_wined3d(hr);
3987 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3988 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3990 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3991 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3993 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3994 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3996 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3997 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4000 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
4001 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4003 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
4004 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
4006 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4007 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4009 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4010 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4013 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
4014 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4016 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
4017 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
4019 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4020 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4022 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4023 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4026 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
4027 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4029 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
4030 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
4032 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
4033 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4035 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4036 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4039 /*****************************************************************************
4040 * IDirectDrawSurface7::UpdateOverlayDisplay
4042 * The DX7 sdk says that it's not implemented
4044 * Params:
4045 * Flags: ?
4047 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
4049 *****************************************************************************/
4050 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
4052 TRACE("iface %p, flags %#x.\n", iface, Flags);
4054 return DDERR_UNSUPPORTED;
4057 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
4059 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4061 TRACE("iface %p, flags %#x.\n", iface, flags);
4063 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4066 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
4068 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4070 TRACE("iface %p, flags %#x.\n", iface, flags);
4072 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4075 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
4077 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4079 TRACE("iface %p, flags %#x.\n", iface, flags);
4081 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4084 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
4086 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4088 TRACE("iface %p, flags %#x.\n", iface, flags);
4090 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4093 /*****************************************************************************
4094 * IDirectDrawSurface7::UpdateOverlayZOrder
4096 * Sets an overlay's Z order
4098 * Params:
4099 * Flags: DDOVERZ_* flags
4100 * DDSRef: Defines the relative position in the overlay chain
4102 * Returns:
4103 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
4105 *****************************************************************************/
4106 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
4107 DWORD flags, IDirectDrawSurface7 *reference)
4109 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4111 FIXME("iface %p, flags %#x, reference %p stub!\n", iface, flags, reference);
4113 wined3d_mutex_lock();
4114 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OVERLAY))
4116 WARN("Not an overlay surface.\n");
4117 wined3d_mutex_unlock();
4118 return DDERR_NOTAOVERLAYSURFACE;
4120 wined3d_mutex_unlock();
4122 return DD_OK;
4125 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
4126 DWORD flags, IDirectDrawSurface4 *reference)
4128 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4129 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
4131 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4133 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4134 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4137 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
4138 DWORD flags, IDirectDrawSurface3 *reference)
4140 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4141 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
4143 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4145 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4146 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4149 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
4150 DWORD flags, IDirectDrawSurface2 *reference)
4152 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4153 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
4155 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4157 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4158 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4161 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
4162 DWORD flags, IDirectDrawSurface *reference)
4164 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4165 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
4167 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
4169 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4170 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4173 /*****************************************************************************
4174 * IDirectDrawSurface7::GetDDInterface
4176 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
4177 * surface belongs to
4179 * Params:
4180 * DD: Address to write the interface pointer to
4182 * Returns:
4183 * DD_OK on success
4184 * DDERR_INVALIDPARAMS if DD is NULL
4186 *****************************************************************************/
4187 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
4189 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4191 TRACE("iface %p, ddraw %p.\n", iface, DD);
4193 if(!DD)
4194 return DDERR_INVALIDPARAMS;
4196 switch(This->version)
4198 case 7:
4199 *DD = &This->ddraw->IDirectDraw7_iface;
4200 break;
4202 case 4:
4203 *DD = &This->ddraw->IDirectDraw4_iface;
4204 break;
4206 case 2:
4207 *DD = &This->ddraw->IDirectDraw2_iface;
4208 break;
4210 case 1:
4211 *DD = &This->ddraw->IDirectDraw_iface;
4212 break;
4215 IUnknown_AddRef((IUnknown *)*DD);
4217 return DD_OK;
4220 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
4222 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4224 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4226 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4229 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
4231 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4233 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4235 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4238 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
4240 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4242 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4244 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4247 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
4249 TRACE("iface %p.\n", iface);
4251 return DD_OK;
4254 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
4256 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4258 TRACE("iface %p.\n", iface);
4260 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
4263 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
4265 TRACE("iface %p, value %p.\n", iface, pValue);
4267 *pValue = 0;
4269 return DD_OK;
4272 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
4274 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4276 TRACE("iface %p, value %p.\n", iface, pValue);
4278 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
4281 /*****************************************************************************
4282 * IDirectDrawSurface7::SetLOD
4284 * Sets the level of detail of a texture
4286 * Params:
4287 * MaxLOD: LOD to set
4289 * Returns:
4290 * DD_OK on success
4291 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4293 *****************************************************************************/
4294 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
4296 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4297 HRESULT hr;
4299 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
4301 wined3d_mutex_lock();
4302 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4304 wined3d_mutex_unlock();
4305 return DDERR_INVALIDOBJECT;
4308 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
4309 if (SUCCEEDED(hr) && surface->draw_texture)
4310 hr = wined3d_texture_set_lod(surface->draw_texture, MaxLOD);
4311 wined3d_mutex_unlock();
4313 return hr;
4316 /*****************************************************************************
4317 * IDirectDrawSurface7::GetLOD
4319 * Returns the level of detail of a Direct3D texture
4321 * Params:
4322 * MaxLOD: Address to write the LOD to
4324 * Returns:
4325 * DD_OK on success
4326 * DDERR_INVALIDPARAMS if MaxLOD is NULL
4327 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4329 *****************************************************************************/
4330 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4332 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4334 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4336 if(!MaxLOD)
4337 return DDERR_INVALIDPARAMS;
4339 wined3d_mutex_lock();
4340 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4342 wined3d_mutex_unlock();
4343 return DDERR_INVALIDOBJECT;
4346 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4347 wined3d_mutex_unlock();
4349 return DD_OK;
4352 /*****************************************************************************
4353 * IDirectDrawSurface7::BltFast
4355 * Performs a fast Blit.
4357 * Params:
4358 * dstx: The x coordinate to blit to on the destination
4359 * dsty: The y coordinate to blit to on the destination
4360 * Source: The source surface
4361 * rsrc: The source rectangle
4362 * trans: Type of transfer. Some DDBLTFAST_* flags
4364 * Returns:
4365 * DD_OK on success, error code otherwise.
4367 *****************************************************************************/
4368 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltFast(IDirectDrawSurface7 *iface,
4369 DWORD dst_x, DWORD dst_y, IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD trans)
4371 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
4372 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src_surface);
4373 DWORD flags = WINED3D_BLT_SYNCHRONOUS;
4374 DWORD src_w, src_h, dst_w, dst_h;
4375 HRESULT hr = DD_OK;
4376 RECT dst_rect, s;
4378 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4379 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), trans);
4381 dst_w = dst_impl->surface_desc.dwWidth;
4382 dst_h = dst_impl->surface_desc.dwHeight;
4384 if (!src_rect)
4386 SetRect(&s, 0, 0, src_impl->surface_desc.dwWidth, src_impl->surface_desc.dwHeight);
4387 src_rect = &s;
4390 src_w = src_rect->right - src_rect->left;
4391 src_h = src_rect->bottom - src_rect->top;
4392 if (src_w > dst_w || dst_x > dst_w - src_w
4393 || src_h > dst_h || dst_y > dst_h - src_h)
4395 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4396 return DDERR_INVALIDRECT;
4399 SetRect(&dst_rect, dst_x, dst_y, dst_x + src_w, dst_y + src_h);
4400 if (trans & DDBLTFAST_SRCCOLORKEY)
4401 flags |= WINED3D_BLT_SRC_CKEY;
4402 if (trans & DDBLTFAST_DESTCOLORKEY)
4403 flags |= WINED3D_BLT_DST_CKEY;
4404 if (trans & DDBLTFAST_WAIT)
4405 flags |= WINED3D_BLT_WAIT;
4406 if (trans & DDBLTFAST_DONOTWAIT)
4407 flags |= WINED3D_BLT_DO_NOT_WAIT;
4409 wined3d_mutex_lock();
4410 if (dst_impl->clipper)
4412 wined3d_mutex_unlock();
4413 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4414 return DDERR_BLTFASTCANTCLIP;
4417 if (src_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4418 hr = ddraw_surface_update_frontbuffer(src_impl, src_rect, TRUE, 0);
4419 if (SUCCEEDED(hr))
4420 hr = wined3d_device_context_blt(dst_impl->ddraw->immediate_context,
4421 ddraw_surface_get_any_texture(dst_impl, DDRAW_SURFACE_RW), dst_impl->sub_resource_idx, &dst_rect,
4422 ddraw_surface_get_any_texture(src_impl,DDRAW_SURFACE_READ), src_impl->sub_resource_idx, src_rect,
4423 flags, NULL, WINED3D_TEXF_POINT);
4424 if (SUCCEEDED(hr) && (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4425 hr = ddraw_surface_update_frontbuffer(dst_impl, &dst_rect, FALSE, 0);
4426 wined3d_mutex_unlock();
4428 switch(hr)
4430 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4431 default: return hr;
4435 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4436 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4438 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4439 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4441 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4442 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4444 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4445 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4448 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4449 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4451 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4452 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4454 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4455 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4457 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4458 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4461 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4462 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4464 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4465 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4467 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4468 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4470 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4471 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4474 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4475 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4477 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4478 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4480 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4481 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4483 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4484 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4487 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **clipper)
4489 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4491 TRACE("iface %p, clipper %p.\n", iface, clipper);
4493 if (!clipper)
4494 return DDERR_INVALIDPARAMS;
4496 wined3d_mutex_lock();
4497 if (!surface->clipper)
4499 wined3d_mutex_unlock();
4500 *clipper = NULL;
4501 return DDERR_NOCLIPPERATTACHED;
4504 *clipper = &surface->clipper->IDirectDrawClipper_iface;
4505 if (ddraw_clipper_is_valid(surface->clipper))
4506 IDirectDrawClipper_AddRef(*clipper);
4507 wined3d_mutex_unlock();
4509 return DD_OK;
4512 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4514 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4516 TRACE("iface %p, clipper %p.\n", iface, clipper);
4518 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4521 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4523 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4525 TRACE("iface %p, clipper %p.\n", iface, clipper);
4527 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4530 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4532 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4534 TRACE("iface %p, clipper %p.\n", iface, clipper);
4536 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4539 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4541 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4543 TRACE("iface %p, clipper %p.\n", iface, clipper);
4545 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4548 /*****************************************************************************
4549 * IDirectDrawSurface7::SetClipper
4551 * Sets a clipper for the surface
4553 * Params:
4554 * Clipper: IDirectDrawClipper interface of the clipper to set
4556 * Returns:
4557 * DD_OK on success
4559 *****************************************************************************/
4560 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4561 IDirectDrawClipper *iclipper)
4563 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4564 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4565 struct ddraw_clipper *old_clipper = This->clipper;
4566 HWND clipWindow;
4568 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4570 wined3d_mutex_lock();
4571 if (clipper == This->clipper)
4573 wined3d_mutex_unlock();
4574 return DD_OK;
4577 This->clipper = clipper;
4579 if (clipper != NULL)
4580 IDirectDrawClipper_AddRef(iclipper);
4581 if (old_clipper && ddraw_clipper_is_valid(old_clipper))
4582 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4584 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4586 clipWindow = NULL;
4587 if(clipper) {
4588 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4591 if (clipWindow)
4593 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4594 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4596 else
4598 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4599 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4603 wined3d_mutex_unlock();
4605 return DD_OK;
4608 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4610 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4612 TRACE("iface %p, clipper %p.\n", iface, clipper);
4614 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4617 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4619 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4621 TRACE("iface %p, clipper %p.\n", iface, clipper);
4623 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4626 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4628 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4630 TRACE("iface %p, clipper %p.\n", iface, clipper);
4632 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4635 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4637 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4639 TRACE("iface %p, clipper %p.\n", iface, clipper);
4641 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4644 /*****************************************************************************
4645 * IDirectDrawSurface7::SetSurfaceDesc
4647 * Sets the surface description. It can override the pixel format, the surface
4648 * memory, ...
4649 * It's not really tested.
4651 * Params:
4652 * DDSD: Pointer to the new surface description to set
4653 * Flags: Some flags
4655 * Returns:
4656 * DD_OK on success
4657 * DDERR_INVALIDPARAMS if DDSD is NULL
4659 *****************************************************************************/
4660 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4662 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4663 HRESULT hr;
4664 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4665 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4666 enum wined3d_format_id format_id;
4667 UINT pitch, width, height;
4669 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4671 if (!DDSD)
4673 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4674 return DDERR_INVALIDPARAMS;
4676 if (Flags)
4678 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4679 return DDERR_INVALIDPARAMS;
4681 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4682 || surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4683 || surface->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4685 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4686 return DDERR_INVALIDSURFACETYPE;
4689 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4690 * for PIXELFORMAT to work */
4691 if (DDSD->dwFlags & ~allowed_flags)
4693 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4694 return DDERR_INVALIDPARAMS;
4696 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4698 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4699 return DDERR_INVALIDPARAMS;
4701 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4703 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4704 return DDERR_INVALIDCAPS;
4706 if (DDSD->dwFlags & DDSD_WIDTH)
4708 if (!(DDSD->dwFlags & DDSD_PITCH))
4710 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4711 return DDERR_INVALIDPARAMS;
4713 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4715 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4716 DDSD->u1.lPitch, DDSD->dwWidth);
4717 return DDERR_INVALIDPARAMS;
4719 if (DDSD->dwWidth != surface->surface_desc.dwWidth)
4720 TRACE("Surface width changed from %u to %u.\n", surface->surface_desc.dwWidth, DDSD->dwWidth);
4721 if (DDSD->u1.lPitch != surface->surface_desc.u1.lPitch)
4722 TRACE("Surface pitch changed from %u to %u.\n", surface->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4723 pitch = DDSD->u1.lPitch;
4724 width = DDSD->dwWidth;
4726 else if (DDSD->dwFlags & DDSD_PITCH)
4728 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4729 return DDERR_INVALIDPARAMS;
4731 else
4733 pitch = surface->surface_desc.u1.lPitch;
4734 width = surface->surface_desc.dwWidth;
4737 if (DDSD->dwFlags & DDSD_HEIGHT)
4739 if (!DDSD->dwHeight)
4741 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4742 return DDERR_INVALIDPARAMS;
4744 if (DDSD->dwHeight != surface->surface_desc.dwHeight)
4745 TRACE("Surface height changed from %u to %u.\n", surface->surface_desc.dwHeight, DDSD->dwHeight);
4746 height = DDSD->dwHeight;
4748 else
4750 height = surface->surface_desc.dwHeight;
4753 wined3d_mutex_lock();
4754 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4756 enum wined3d_format_id current_format_id;
4757 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4759 if (format_id == WINED3DFMT_UNKNOWN)
4761 ERR("Requested to set an unknown pixelformat\n");
4762 wined3d_mutex_unlock();
4763 return DDERR_INVALIDPARAMS;
4765 current_format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4766 if (format_id != current_format_id)
4767 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4769 else
4771 format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4774 if (FAILED(hr = wined3d_texture_update_desc(surface->wined3d_texture, surface->sub_resource_idx,
4775 width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4777 WARN("Failed to update surface desc, hr %#x.\n", hr);
4778 wined3d_mutex_unlock();
4779 return hr_ddraw_from_wined3d(hr);
4782 if (surface->draw_texture && FAILED(hr = wined3d_texture_update_desc(surface->draw_texture,
4783 surface->sub_resource_idx, width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, NULL, 0)))
4785 ERR("Failed to update surface desc for draw_texture, hr %#x.\n", hr);
4786 wined3d_mutex_unlock();
4787 return hr_ddraw_from_wined3d(hr);
4790 if (DDSD->dwFlags & DDSD_WIDTH)
4791 surface->surface_desc.dwWidth = width;
4792 if (DDSD->dwFlags & DDSD_PITCH)
4793 surface->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4794 if (DDSD->dwFlags & DDSD_HEIGHT)
4795 surface->surface_desc.dwHeight = height;
4796 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4797 surface->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4799 wined3d_mutex_unlock();
4801 return DD_OK;
4804 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4805 DDSURFACEDESC2 *surface_desc, DWORD flags)
4807 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4809 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4811 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4812 surface_desc, flags);
4815 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4816 DDSURFACEDESC *surface_desc, DWORD flags)
4818 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4819 DDSURFACEDESC2 surface_desc2;
4821 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4823 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4824 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4825 surface_desc ? &surface_desc2 : NULL, flags);
4828 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4830 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4831 struct ddraw_palette *palette_impl;
4832 HRESULT hr = DD_OK;
4834 TRACE("iface %p, palette %p.\n", iface, palette);
4836 if (!palette)
4837 return DDERR_INVALIDPARAMS;
4838 if (ddraw_surface_is_lost(surface))
4840 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4841 return DDERR_SURFACELOST;
4844 wined3d_mutex_lock();
4845 if ((palette_impl = surface->palette))
4847 *palette = &palette_impl->IDirectDrawPalette_iface;
4848 IDirectDrawPalette_AddRef(*palette);
4850 else
4852 *palette = NULL;
4853 hr = DDERR_NOPALETTEATTACHED;
4855 wined3d_mutex_unlock();
4857 return hr;
4860 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4862 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4864 TRACE("iface %p, palette %p.\n", iface, palette);
4866 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4869 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4871 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4873 TRACE("iface %p, palette %p.\n", iface, palette);
4875 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4878 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4880 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4882 TRACE("iface %p, palette %p.\n", iface, palette);
4884 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4887 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4889 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4891 TRACE("iface %p, palette %p.\n", iface, palette);
4893 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4896 static HRESULT ddraw_surface_set_wined3d_textures_colour_key(struct ddraw_surface *surface, DWORD flags,
4897 struct wined3d_color_key *color_key)
4899 HRESULT hr;
4901 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags, color_key);
4902 if (surface->draw_texture && SUCCEEDED(hr))
4903 hr = wined3d_texture_set_color_key(surface->draw_texture, flags, color_key);
4905 return hr;
4908 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4910 DDCOLORKEY fixed_color_key;
4911 HRESULT hr = WINED3D_OK;
4913 if (flags & DDCKEY_COLORSPACE)
4915 if (color_key && color_key->dwColorSpaceLowValue != color_key->dwColorSpaceHighValue)
4917 WARN("Range color keys are not supported, returning DDERR_NOCOLORKEYHW.\n");
4918 return DDERR_NOCOLORKEYHW;
4920 flags &= ~DDCKEY_COLORSPACE;
4923 wined3d_mutex_lock();
4925 if (color_key)
4927 fixed_color_key.dwColorSpaceLowValue = fixed_color_key.dwColorSpaceHighValue = color_key->dwColorSpaceLowValue;
4928 switch (flags & ~DDCKEY_COLORSPACE)
4930 case DDCKEY_DESTBLT:
4931 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4932 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4933 break;
4935 case DDCKEY_DESTOVERLAY:
4936 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4937 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4938 break;
4940 case DDCKEY_SRCOVERLAY:
4941 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4942 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4943 break;
4945 case DDCKEY_SRCBLT:
4946 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4947 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4948 break;
4950 default:
4951 wined3d_mutex_unlock();
4952 return DDERR_INVALIDPARAMS;
4955 else
4957 switch (flags & ~DDCKEY_COLORSPACE)
4959 case DDCKEY_DESTBLT:
4960 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4961 break;
4963 case DDCKEY_DESTOVERLAY:
4964 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4965 break;
4967 case DDCKEY_SRCOVERLAY:
4968 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4969 break;
4971 case DDCKEY_SRCBLT:
4972 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4973 break;
4975 default:
4976 wined3d_mutex_unlock();
4977 return DDERR_INVALIDPARAMS;
4981 if (surface->is_complex_root)
4982 hr = ddraw_surface_set_wined3d_textures_colour_key(surface, flags,
4983 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4985 wined3d_mutex_unlock();
4987 return hr_ddraw_from_wined3d(hr);
4990 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4992 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4994 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4996 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4997 return DDERR_NOTONMIPMAPSUBLEVEL;
4999 return ddraw_surface_set_color_key(surface, flags, color_key);
5002 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
5004 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
5006 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5008 return ddraw_surface_set_color_key(surface, flags, color_key);
5011 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
5013 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
5015 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5017 return ddraw_surface_set_color_key(surface, flags, color_key);
5020 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
5022 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
5024 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5026 return ddraw_surface_set_color_key(surface, flags, color_key);
5029 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
5031 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5033 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
5035 return ddraw_surface_set_color_key(surface, flags, color_key);
5038 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
5040 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
5042 TRACE("iface %p, palette %p.\n", iface, palette);
5044 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
5045 return DDERR_NOTONMIPMAPSUBLEVEL;
5046 if (ddraw_surface_is_lost(surface))
5048 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5049 return DDERR_SURFACELOST;
5052 return ddraw_surface_set_palette(surface, palette);
5055 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
5057 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
5059 TRACE("iface %p, palette %p.\n", iface, palette);
5061 if (ddraw_surface_is_lost(surface))
5063 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5064 return DDERR_SURFACELOST;
5067 return ddraw_surface_set_palette(surface, palette);
5070 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
5072 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
5074 TRACE("iface %p, palette %p.\n", iface, palette);
5076 if (ddraw_surface_is_lost(surface))
5078 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5079 return DDERR_SURFACELOST;
5082 return ddraw_surface_set_palette(surface, palette);
5085 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
5087 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
5089 TRACE("iface %p, palette %p.\n", iface, palette);
5091 if (ddraw_surface_is_lost(surface))
5093 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5094 return DDERR_SURFACELOST;
5097 return ddraw_surface_set_palette(surface, palette);
5100 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
5102 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5104 TRACE("iface %p, palette %p.\n", iface, palette);
5106 if (ddraw_surface_is_lost(surface))
5108 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5109 return DDERR_SURFACELOST;
5112 return ddraw_surface_set_palette(surface, palette);
5115 /**********************************************************
5116 * IDirectDrawGammaControl::GetGammaRamp
5118 * Returns the current gamma ramp for a surface
5120 * Params:
5121 * flags: Ignored
5122 * gamma_ramp: Address to write the ramp to
5124 * Returns:
5125 * DD_OK on success
5126 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5128 **********************************************************/
5129 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
5130 DWORD flags, DDGAMMARAMP *gamma_ramp)
5132 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5134 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5136 if (!gamma_ramp)
5138 WARN("Invalid gamma_ramp passed.\n");
5139 return DDERR_INVALIDPARAMS;
5142 wined3d_mutex_lock();
5143 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5145 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5146 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
5148 else
5150 ERR("Not implemented for non-primary surfaces.\n");
5152 wined3d_mutex_unlock();
5154 return DD_OK;
5157 /**********************************************************
5158 * IDirectDrawGammaControl::SetGammaRamp
5160 * Sets the red, green and blue gamma ramps for
5162 * Params:
5163 * flags: Can be DDSGR_CALIBRATE to request calibration
5164 * gamma_ramp: Structure containing the new gamma ramp
5166 * Returns:
5167 * DD_OK on success
5168 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5170 **********************************************************/
5171 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
5172 DWORD flags, DDGAMMARAMP *gamma_ramp)
5174 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5176 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5178 if (!gamma_ramp)
5180 WARN("Invalid gamma_ramp passed.\n");
5181 return DDERR_INVALIDPARAMS;
5184 wined3d_mutex_lock();
5185 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5187 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5188 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
5189 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
5191 else
5193 ERR("Not implemented for non-primary surfaces.\n");
5195 wined3d_mutex_unlock();
5197 return DD_OK;
5200 /*****************************************************************************
5201 * IDirect3DTexture2::PaletteChanged
5203 * Informs the texture about a palette change
5205 * Params:
5206 * start: Start index of the change
5207 * count: The number of changed entries
5209 * Returns
5210 * D3D_OK, because it's a stub
5212 *****************************************************************************/
5213 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
5215 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
5217 return D3D_OK;
5220 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
5222 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5224 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
5226 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
5229 /*****************************************************************************
5230 * IDirect3DTexture::Unload
5232 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
5235 * Returns:
5236 * DDERR_UNSUPPORTED
5238 *****************************************************************************/
5239 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
5241 WARN("iface %p. Not implemented.\n", iface);
5243 return DDERR_UNSUPPORTED;
5246 /*****************************************************************************
5247 * IDirect3DTexture2::GetHandle
5249 * Returns handle for the texture.
5251 * Params:
5252 * device: Device this handle is assigned to
5253 * handle: Address to store the handle at.
5255 * Returns:
5256 * D3D_OK
5258 *****************************************************************************/
5259 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
5260 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
5262 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
5263 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
5265 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5267 wined3d_mutex_lock();
5269 if (!surface->Handle)
5271 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
5272 if (h == DDRAW_INVALID_HANDLE)
5274 ERR("Failed to allocate a texture handle.\n");
5275 wined3d_mutex_unlock();
5276 return DDERR_OUTOFMEMORY;
5279 surface->Handle = h + 1;
5282 TRACE("Returning handle %08x.\n", surface->Handle);
5283 *handle = surface->Handle;
5285 wined3d_mutex_unlock();
5287 return D3D_OK;
5290 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
5291 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
5293 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5294 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
5296 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5298 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
5299 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
5302 /*****************************************************************************
5303 * get_sub_mimaplevel
5305 * Helper function that returns the next mipmap level
5307 * tex_ptr: Surface of which to return the next level
5309 *****************************************************************************/
5310 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
5312 /* Now go down the mipmap chain to the next surface */
5313 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, {0} };
5314 IDirectDrawSurface7 *next_level;
5315 HRESULT hr;
5317 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
5318 if (FAILED(hr)) return NULL;
5320 ddraw_surface7_Release(next_level);
5322 return impl_from_IDirectDrawSurface7(next_level);
5325 /*****************************************************************************
5326 * IDirect3DTexture2::Load
5328 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5330 * This function isn't relayed to WineD3D because the whole interface is
5331 * implemented in DDraw only. For speed improvements an implementation which
5332 * takes OpenGL more into account could be placed into WineD3D.
5334 * Params:
5335 * src_texture: Address of the texture to load
5337 * Returns:
5338 * D3D_OK on success
5339 * D3DERR_TEXTURE_LOAD_FAILED.
5341 *****************************************************************************/
5342 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5344 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5345 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5346 struct wined3d_resource *dst_resource, *src_resource;
5347 HRESULT hr;
5349 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5351 if (src_surface == dst_surface)
5353 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5354 return D3D_OK;
5357 wined3d_mutex_lock();
5359 dst_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(dst_surface, DDRAW_SURFACE_WRITE));
5360 src_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(src_surface, DDRAW_SURFACE_READ));
5362 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5363 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5364 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5366 ERR("Trying to load surfaces with different mip-map counts.\n");
5369 for (;;)
5371 struct ddraw_palette *dst_pal, *src_pal;
5372 DDSURFACEDESC *src_desc, *dst_desc;
5374 TRACE("Copying surface %p to surface %p.\n", src_surface, dst_surface);
5376 /* Suppress the ALLOCONLOAD flag */
5377 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5379 /* Get the palettes */
5380 dst_pal = dst_surface->palette;
5381 src_pal = src_surface->palette;
5383 if (src_pal)
5385 PALETTEENTRY palent[256];
5387 if (!dst_pal)
5389 wined3d_mutex_unlock();
5390 return DDERR_NOPALETTEATTACHED;
5392 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5393 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5396 /* Copy one surface on the other */
5397 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5398 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5400 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5402 /* Should also check for same pixel format, u1.lPitch, ... */
5403 ERR("Error in surface sizes.\n");
5404 wined3d_mutex_unlock();
5405 return D3DERR_TEXTURE_LOAD_FAILED;
5407 else
5409 struct wined3d_map_desc src_map_desc, dst_map_desc;
5411 /* Copy the src blit color key if the source has one, don't erase
5412 * the destination's ckey if the source has none */
5413 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5415 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5416 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5419 if (FAILED(hr = wined3d_resource_map(src_resource,
5420 src_surface->sub_resource_idx, &src_map_desc, NULL, WINED3D_MAP_READ)))
5422 ERR("Failed to lock source surface, hr %#x.\n", hr);
5423 wined3d_mutex_unlock();
5424 return D3DERR_TEXTURE_LOAD_FAILED;
5427 if (FAILED(hr = wined3d_resource_map(dst_resource,
5428 dst_surface->sub_resource_idx, &dst_map_desc, NULL, WINED3D_MAP_WRITE)))
5430 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5431 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5432 wined3d_mutex_unlock();
5433 return D3DERR_TEXTURE_LOAD_FAILED;
5436 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5437 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5438 else
5439 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5441 wined3d_resource_unmap(dst_resource, dst_surface->sub_resource_idx);
5442 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5445 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5446 src_surface = get_sub_mimaplevel(src_surface);
5447 else
5448 src_surface = NULL;
5450 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5451 dst_surface = get_sub_mimaplevel(dst_surface);
5452 else
5453 dst_surface = NULL;
5455 if (!src_surface || !dst_surface)
5457 if (src_surface != dst_surface)
5458 ERR("Loading surface with different mipmap structure.\n");
5459 break;
5463 wined3d_mutex_unlock();
5465 return hr;
5468 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5470 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5471 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5473 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5475 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5476 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5479 /*****************************************************************************
5480 * The VTable
5481 *****************************************************************************/
5483 /* Some windowed mode wrappers expect this vtbl to be writable. */
5484 static struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5486 /* IUnknown */
5487 ddraw_surface7_QueryInterface,
5488 ddraw_surface7_AddRef,
5489 ddraw_surface7_Release,
5490 /* IDirectDrawSurface */
5491 ddraw_surface7_AddAttachedSurface,
5492 ddraw_surface7_AddOverlayDirtyRect,
5493 ddraw_surface7_Blt,
5494 ddraw_surface7_BltBatch,
5495 ddraw_surface7_BltFast,
5496 ddraw_surface7_DeleteAttachedSurface,
5497 ddraw_surface7_EnumAttachedSurfaces,
5498 ddraw_surface7_EnumOverlayZOrders,
5499 ddraw_surface7_Flip,
5500 ddraw_surface7_GetAttachedSurface,
5501 ddraw_surface7_GetBltStatus,
5502 ddraw_surface7_GetCaps,
5503 ddraw_surface7_GetClipper,
5504 ddraw_surface7_GetColorKey,
5505 ddraw_surface7_GetDC,
5506 ddraw_surface7_GetFlipStatus,
5507 ddraw_surface7_GetOverlayPosition,
5508 ddraw_surface7_GetPalette,
5509 ddraw_surface7_GetPixelFormat,
5510 ddraw_surface7_GetSurfaceDesc,
5511 ddraw_surface7_Initialize,
5512 ddraw_surface7_IsLost,
5513 ddraw_surface7_Lock,
5514 ddraw_surface7_ReleaseDC,
5515 ddraw_surface7_Restore,
5516 ddraw_surface7_SetClipper,
5517 ddraw_surface7_SetColorKey,
5518 ddraw_surface7_SetOverlayPosition,
5519 ddraw_surface7_SetPalette,
5520 ddraw_surface7_Unlock,
5521 ddraw_surface7_UpdateOverlay,
5522 ddraw_surface7_UpdateOverlayDisplay,
5523 ddraw_surface7_UpdateOverlayZOrder,
5524 /* IDirectDrawSurface2 */
5525 ddraw_surface7_GetDDInterface,
5526 ddraw_surface7_PageLock,
5527 ddraw_surface7_PageUnlock,
5528 /* IDirectDrawSurface3 */
5529 ddraw_surface7_SetSurfaceDesc,
5530 /* IDirectDrawSurface4 */
5531 ddraw_surface7_SetPrivateData,
5532 ddraw_surface7_GetPrivateData,
5533 ddraw_surface7_FreePrivateData,
5534 ddraw_surface7_GetUniquenessValue,
5535 ddraw_surface7_ChangeUniquenessValue,
5536 /* IDirectDrawSurface7 */
5537 ddraw_surface7_SetPriority,
5538 ddraw_surface7_GetPriority,
5539 ddraw_surface7_SetLOD,
5540 ddraw_surface7_GetLOD,
5543 /* Some windowed mode wrappers expect this vtbl to be writable. */
5544 static struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5546 /* IUnknown */
5547 ddraw_surface4_QueryInterface,
5548 ddraw_surface4_AddRef,
5549 ddraw_surface4_Release,
5550 /* IDirectDrawSurface */
5551 ddraw_surface4_AddAttachedSurface,
5552 ddraw_surface4_AddOverlayDirtyRect,
5553 ddraw_surface4_Blt,
5554 ddraw_surface4_BltBatch,
5555 ddraw_surface4_BltFast,
5556 ddraw_surface4_DeleteAttachedSurface,
5557 ddraw_surface4_EnumAttachedSurfaces,
5558 ddraw_surface4_EnumOverlayZOrders,
5559 ddraw_surface4_Flip,
5560 ddraw_surface4_GetAttachedSurface,
5561 ddraw_surface4_GetBltStatus,
5562 ddraw_surface4_GetCaps,
5563 ddraw_surface4_GetClipper,
5564 ddraw_surface4_GetColorKey,
5565 ddraw_surface4_GetDC,
5566 ddraw_surface4_GetFlipStatus,
5567 ddraw_surface4_GetOverlayPosition,
5568 ddraw_surface4_GetPalette,
5569 ddraw_surface4_GetPixelFormat,
5570 ddraw_surface4_GetSurfaceDesc,
5571 ddraw_surface4_Initialize,
5572 ddraw_surface4_IsLost,
5573 ddraw_surface4_Lock,
5574 ddraw_surface4_ReleaseDC,
5575 ddraw_surface4_Restore,
5576 ddraw_surface4_SetClipper,
5577 ddraw_surface4_SetColorKey,
5578 ddraw_surface4_SetOverlayPosition,
5579 ddraw_surface4_SetPalette,
5580 ddraw_surface4_Unlock,
5581 ddraw_surface4_UpdateOverlay,
5582 ddraw_surface4_UpdateOverlayDisplay,
5583 ddraw_surface4_UpdateOverlayZOrder,
5584 /* IDirectDrawSurface2 */
5585 ddraw_surface4_GetDDInterface,
5586 ddraw_surface4_PageLock,
5587 ddraw_surface4_PageUnlock,
5588 /* IDirectDrawSurface3 */
5589 ddraw_surface4_SetSurfaceDesc,
5590 /* IDirectDrawSurface4 */
5591 ddraw_surface4_SetPrivateData,
5592 ddraw_surface4_GetPrivateData,
5593 ddraw_surface4_FreePrivateData,
5594 ddraw_surface4_GetUniquenessValue,
5595 ddraw_surface4_ChangeUniquenessValue,
5598 /* Some windowed mode wrappers expect this vtbl to be writable. */
5599 static struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5601 /* IUnknown */
5602 ddraw_surface3_QueryInterface,
5603 ddraw_surface3_AddRef,
5604 ddraw_surface3_Release,
5605 /* IDirectDrawSurface */
5606 ddraw_surface3_AddAttachedSurface,
5607 ddraw_surface3_AddOverlayDirtyRect,
5608 ddraw_surface3_Blt,
5609 ddraw_surface3_BltBatch,
5610 ddraw_surface3_BltFast,
5611 ddraw_surface3_DeleteAttachedSurface,
5612 ddraw_surface3_EnumAttachedSurfaces,
5613 ddraw_surface3_EnumOverlayZOrders,
5614 ddraw_surface3_Flip,
5615 ddraw_surface3_GetAttachedSurface,
5616 ddraw_surface3_GetBltStatus,
5617 ddraw_surface3_GetCaps,
5618 ddraw_surface3_GetClipper,
5619 ddraw_surface3_GetColorKey,
5620 ddraw_surface3_GetDC,
5621 ddraw_surface3_GetFlipStatus,
5622 ddraw_surface3_GetOverlayPosition,
5623 ddraw_surface3_GetPalette,
5624 ddraw_surface3_GetPixelFormat,
5625 ddraw_surface3_GetSurfaceDesc,
5626 ddraw_surface3_Initialize,
5627 ddraw_surface3_IsLost,
5628 ddraw_surface3_Lock,
5629 ddraw_surface3_ReleaseDC,
5630 ddraw_surface3_Restore,
5631 ddraw_surface3_SetClipper,
5632 ddraw_surface3_SetColorKey,
5633 ddraw_surface3_SetOverlayPosition,
5634 ddraw_surface3_SetPalette,
5635 ddraw_surface3_Unlock,
5636 ddraw_surface3_UpdateOverlay,
5637 ddraw_surface3_UpdateOverlayDisplay,
5638 ddraw_surface3_UpdateOverlayZOrder,
5639 /* IDirectDrawSurface2 */
5640 ddraw_surface3_GetDDInterface,
5641 ddraw_surface3_PageLock,
5642 ddraw_surface3_PageUnlock,
5643 /* IDirectDrawSurface3 */
5644 ddraw_surface3_SetSurfaceDesc,
5647 /* Some windowed mode wrappers expect this vtbl to be writable. */
5648 static struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5650 /* IUnknown */
5651 ddraw_surface2_QueryInterface,
5652 ddraw_surface2_AddRef,
5653 ddraw_surface2_Release,
5654 /* IDirectDrawSurface */
5655 ddraw_surface2_AddAttachedSurface,
5656 ddraw_surface2_AddOverlayDirtyRect,
5657 ddraw_surface2_Blt,
5658 ddraw_surface2_BltBatch,
5659 ddraw_surface2_BltFast,
5660 ddraw_surface2_DeleteAttachedSurface,
5661 ddraw_surface2_EnumAttachedSurfaces,
5662 ddraw_surface2_EnumOverlayZOrders,
5663 ddraw_surface2_Flip,
5664 ddraw_surface2_GetAttachedSurface,
5665 ddraw_surface2_GetBltStatus,
5666 ddraw_surface2_GetCaps,
5667 ddraw_surface2_GetClipper,
5668 ddraw_surface2_GetColorKey,
5669 ddraw_surface2_GetDC,
5670 ddraw_surface2_GetFlipStatus,
5671 ddraw_surface2_GetOverlayPosition,
5672 ddraw_surface2_GetPalette,
5673 ddraw_surface2_GetPixelFormat,
5674 ddraw_surface2_GetSurfaceDesc,
5675 ddraw_surface2_Initialize,
5676 ddraw_surface2_IsLost,
5677 ddraw_surface2_Lock,
5678 ddraw_surface2_ReleaseDC,
5679 ddraw_surface2_Restore,
5680 ddraw_surface2_SetClipper,
5681 ddraw_surface2_SetColorKey,
5682 ddraw_surface2_SetOverlayPosition,
5683 ddraw_surface2_SetPalette,
5684 ddraw_surface2_Unlock,
5685 ddraw_surface2_UpdateOverlay,
5686 ddraw_surface2_UpdateOverlayDisplay,
5687 ddraw_surface2_UpdateOverlayZOrder,
5688 /* IDirectDrawSurface2 */
5689 ddraw_surface2_GetDDInterface,
5690 ddraw_surface2_PageLock,
5691 ddraw_surface2_PageUnlock,
5694 /* Bad Mojo Redux expects this vtbl to be writable. */
5695 static struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5697 /* IUnknown */
5698 ddraw_surface1_QueryInterface,
5699 ddraw_surface1_AddRef,
5700 ddraw_surface1_Release,
5701 /* IDirectDrawSurface */
5702 ddraw_surface1_AddAttachedSurface,
5703 ddraw_surface1_AddOverlayDirtyRect,
5704 ddraw_surface1_Blt,
5705 ddraw_surface1_BltBatch,
5706 ddraw_surface1_BltFast,
5707 ddraw_surface1_DeleteAttachedSurface,
5708 ddraw_surface1_EnumAttachedSurfaces,
5709 ddraw_surface1_EnumOverlayZOrders,
5710 ddraw_surface1_Flip,
5711 ddraw_surface1_GetAttachedSurface,
5712 ddraw_surface1_GetBltStatus,
5713 ddraw_surface1_GetCaps,
5714 ddraw_surface1_GetClipper,
5715 ddraw_surface1_GetColorKey,
5716 ddraw_surface1_GetDC,
5717 ddraw_surface1_GetFlipStatus,
5718 ddraw_surface1_GetOverlayPosition,
5719 ddraw_surface1_GetPalette,
5720 ddraw_surface1_GetPixelFormat,
5721 ddraw_surface1_GetSurfaceDesc,
5722 ddraw_surface1_Initialize,
5723 ddraw_surface1_IsLost,
5724 ddraw_surface1_Lock,
5725 ddraw_surface1_ReleaseDC,
5726 ddraw_surface1_Restore,
5727 ddraw_surface1_SetClipper,
5728 ddraw_surface1_SetColorKey,
5729 ddraw_surface1_SetOverlayPosition,
5730 ddraw_surface1_SetPalette,
5731 ddraw_surface1_Unlock,
5732 ddraw_surface1_UpdateOverlay,
5733 ddraw_surface1_UpdateOverlayDisplay,
5734 ddraw_surface1_UpdateOverlayZOrder,
5737 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5739 ddraw_gamma_control_QueryInterface,
5740 ddraw_gamma_control_AddRef,
5741 ddraw_gamma_control_Release,
5742 ddraw_gamma_control_GetGammaRamp,
5743 ddraw_gamma_control_SetGammaRamp,
5746 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5748 d3d_texture2_QueryInterface,
5749 d3d_texture2_AddRef,
5750 d3d_texture2_Release,
5751 d3d_texture2_GetHandle,
5752 d3d_texture2_PaletteChanged,
5753 d3d_texture2_Load,
5756 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5758 d3d_texture1_QueryInterface,
5759 d3d_texture1_AddRef,
5760 d3d_texture1_Release,
5761 d3d_texture1_Initialize,
5762 d3d_texture1_GetHandle,
5763 d3d_texture1_PaletteChanged,
5764 d3d_texture1_Load,
5765 d3d_texture1_Unload,
5768 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5769 * IDirectDrawSurface interface to ddraw methods. */
5770 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5772 if (!iface) return NULL;
5773 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5775 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5776 if (FAILED(hr))
5778 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5779 return NULL;
5781 IDirectDrawSurface7_Release(iface);
5783 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5786 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5788 if (!iface) return NULL;
5789 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5791 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5792 if (FAILED(hr))
5794 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5795 return NULL;
5797 IDirectDrawSurface4_Release(iface);
5799 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5802 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5804 if (!iface) return NULL;
5805 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5807 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5808 if (FAILED(hr))
5810 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5811 return NULL;
5813 IDirectDrawSurface3_Release(iface);
5815 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5818 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5820 if (!iface) return NULL;
5821 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5823 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5824 if (FAILED(hr))
5826 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5827 return NULL;
5829 IDirectDrawSurface2_Release(iface);
5831 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5834 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5836 if (!iface) return NULL;
5837 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5839 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5840 if (FAILED(hr))
5842 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5843 return NULL;
5845 IDirectDrawSurface_Release(iface);
5847 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5850 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5852 if (!iface) return NULL;
5853 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5854 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5857 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5859 if (!iface) return NULL;
5860 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5861 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5864 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5866 struct ddraw_surface *surface = parent;
5868 TRACE("surface %p.\n", surface);
5870 /* This shouldn't happen, ddraw_surface_release_iface() should prevent the
5871 * surface from being destroyed in this case. */
5872 if (surface->first_attached != surface)
5873 ERR("Surface is still attached to surface %p.\n", surface->first_attached);
5875 while (surface->next_attached)
5876 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5877 surface->next_attached, surface->next_attached->attached_iface)))
5878 ERR("DeleteAttachedSurface failed.\n");
5880 /* Having a texture handle set implies that the device still exists. */
5881 if (surface->Handle)
5882 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5884 /* Reduce the ddraw surface count. */
5885 list_remove(&surface->surface_list_entry);
5887 if (surface->clipper && ddraw_clipper_is_valid(surface->clipper))
5888 IDirectDrawClipper_Release(&surface->clipper->IDirectDrawClipper_iface);
5890 if (surface == surface->ddraw->primary)
5892 surface->ddraw->primary = NULL;
5893 surface->ddraw->gdi_surface = NULL;
5896 wined3d_private_store_cleanup(&surface->private_store);
5898 if (surface->draw_texture)
5899 wined3d_texture_decref(surface->wined3d_texture);
5901 heap_free(surface);
5904 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5906 ddraw_surface_wined3d_object_destroyed,
5909 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5911 struct ddraw_texture *texture = parent;
5913 TRACE("texture %p, texture_memory %p.\n", texture, texture->texture_memory);
5915 heap_free(texture->texture_memory);
5916 heap_free(parent);
5919 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5921 ddraw_texture_wined3d_object_destroyed,
5924 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5926 return DD_OK;
5929 static HRESULT ddraw_surface_reserve_memory(struct wined3d_texture *wined3d_texture,
5930 unsigned int sub_resource_count)
5932 static const unsigned int extra_size = 0x10000;
5934 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
5935 struct wined3d_resource_desc resource_desc;
5936 struct wined3d_sub_resource_desc desc;
5937 unsigned int pitch, slice_pitch;
5938 HRESULT hr = WINED3D_OK;
5939 unsigned int offset, i;
5941 wined3d_resource_get_desc(wined3d_texture_get_resource(wined3d_texture), &resource_desc);
5942 if (!(texture->texture_memory = heap_alloc_zero(resource_desc.size + extra_size)))
5944 ERR("Out of memory.\n");
5945 return E_OUTOFMEMORY;
5947 TRACE("texture->texture_memory %p.\n", texture->texture_memory);
5949 offset = 0;
5950 for (i = 0; i < sub_resource_count; ++i)
5952 if (FAILED(hr = wined3d_texture_get_sub_resource_desc(wined3d_texture, i, &desc)))
5954 ERR("Subresource %u not found.\n", i);
5955 heap_free(texture->texture_memory);
5956 texture->texture_memory = NULL;
5957 return hr;
5959 wined3d_texture_get_pitch(wined3d_texture, i, &pitch, &slice_pitch);
5961 if (FAILED(hr = wined3d_texture_update_desc(wined3d_texture, i,
5962 desc.width, desc.height, resource_desc.format,
5963 desc.multisample_type, desc.multisample_quality,
5964 (BYTE *)texture->texture_memory + offset, pitch)))
5966 heap_free(texture->texture_memory);
5967 texture->texture_memory = NULL;
5968 break;
5970 offset += desc.size;
5972 return hr;
5975 static HRESULT ddraw_surface_create_wined3d_texture(DDSURFACEDESC2 *desc, struct wined3d_device *wined3d_device,
5976 const struct wined3d_resource_desc *wined3d_desc, unsigned int layers, unsigned int levels,
5977 struct ddraw_texture *texture, struct wined3d_texture **wined3d_texture)
5979 struct wined3d_resource_desc draw_texture_desc;
5980 struct wined3d_texture *draw_texture;
5981 struct ddraw_surface *parent;
5982 unsigned int bind_flags;
5983 unsigned int i;
5984 HRESULT hr;
5986 bind_flags = 0;
5987 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5988 || (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5989 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
5991 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5992 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
5993 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5994 bind_flags |= WINED3D_BIND_RENDER_TARGET;
5996 if (!bind_flags || (wined3d_desc->access & WINED3D_RESOURCE_ACCESS_GPU && !(bind_flags & ~wined3d_desc->bind_flags)))
5997 goto no_draw_texture;
5999 draw_texture_desc = *wined3d_desc;
6000 draw_texture_desc.bind_flags = bind_flags;
6001 draw_texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
6003 if (FAILED(hr = wined3d_texture_create(wined3d_device, &draw_texture_desc, layers,
6004 levels, 0, NULL, texture, &ddraw_texture_wined3d_parent_ops, &draw_texture)))
6006 WARN("Failed to create draw texture, hr %#x.\n", hr);
6007 goto no_draw_texture;
6009 wined3d_texture_decref(draw_texture);
6011 /* Some applications assume surfaces will always be mapped at the same
6012 * address. Some of those also assume that this address is valid even when
6013 * the surface isn't mapped, and that updates done this way will be
6014 * visible on the screen. The game Nox is such an application,
6015 * Commandos: Behind Enemy Lines is another. Setting
6016 * WINED3D_TEXTURE_CREATE_GET_DC_LENIENT will ensure this. */
6017 if (FAILED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6018 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, NULL, &ddraw_null_wined3d_parent_ops,
6019 wined3d_texture)))
6021 parent = wined3d_texture_get_sub_resource_parent(draw_texture, 0);
6022 if (texture->version == 7)
6023 IDirectDrawSurface7_Release(&parent->IDirectDrawSurface7_iface);
6024 else if (texture->version == 4)
6025 IDirectDrawSurface4_Release(&parent->IDirectDrawSurface4_iface);
6026 else
6027 IDirectDrawSurface_Release(&parent->IDirectDrawSurface_iface);
6028 return hr;
6030 wined3d_resource_set_parent(wined3d_texture_get_resource(*wined3d_texture), texture);
6031 for (i = 0; i < layers * levels; ++i)
6033 parent = wined3d_texture_get_sub_resource_parent(draw_texture, i);
6034 assert(parent->wined3d_texture == draw_texture);
6035 parent->draw_texture = draw_texture;
6036 parent->wined3d_texture = *wined3d_texture;
6037 wined3d_texture_set_sub_resource_parent(*wined3d_texture, i, parent);
6038 wined3d_texture_incref(*wined3d_texture);
6040 wined3d_texture_decref(*wined3d_texture);
6041 TRACE("Surface %p, created draw_texture %p, wined3d_texture %p.\n",
6042 wined3d_texture_get_sub_resource_parent(draw_texture, 0), draw_texture, wined3d_texture);
6043 return D3D_OK;
6045 no_draw_texture:
6046 if (SUCCEEDED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6047 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, texture, &ddraw_texture_wined3d_parent_ops,
6048 wined3d_texture)))
6049 wined3d_texture_decref(*wined3d_texture);
6050 return hr;
6053 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
6054 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
6056 struct wined3d_sub_resource_desc wined3d_mip_desc;
6057 struct ddraw_surface *root, *mip, **attach;
6058 struct wined3d_resource_desc wined3d_desc;
6059 DDPIXELFORMAT wined3d_display_mode_format;
6060 struct wined3d_texture *wined3d_texture;
6061 struct wined3d_display_mode mode;
6062 DDSURFACEDESC2 *desc, *mip_desc;
6063 struct ddraw_texture *texture;
6064 BOOL sysmem_fallback = FALSE;
6065 unsigned int layers = 1;
6066 unsigned int pitch = 0;
6067 BOOL reserve_memory;
6068 UINT levels, i, j;
6069 HRESULT hr;
6071 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
6072 ddraw, surface_desc, surface, outer_unknown, version);
6073 if (TRACE_ON(ddraw))
6075 TRACE("Requesting surface desc:\n");
6076 DDRAW_dump_surface_desc(surface_desc);
6079 if (outer_unknown)
6080 return CLASS_E_NOAGGREGATION;
6082 if (!surface)
6083 return E_POINTER;
6085 if (!(texture = heap_alloc(sizeof(*texture))))
6086 return E_OUTOFMEMORY;
6088 texture->texture_memory = NULL;
6089 texture->version = version;
6090 texture->surface_desc = *surface_desc;
6091 desc = &texture->surface_desc;
6093 /* Ensure DDSD_CAPS is always set. */
6094 desc->dwFlags |= DDSD_CAPS;
6096 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6098 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->u5.dwBackBufferCount)
6100 WARN("Tried to create a flippable surface without any back buffers.\n");
6101 heap_free(texture);
6102 return DDERR_INVALIDCAPS;
6105 if (!(desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX))
6107 WARN("Tried to create a flippable surface without DDSCAPS_COMPLEX.\n");
6108 heap_free(texture);
6109 return DDERR_INVALIDCAPS;
6112 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6114 WARN("Tried to create a flippable cubemap.\n");
6115 heap_free(texture);
6116 return DDERR_INVALIDPARAMS;
6119 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6121 FIXME("Flippable textures not implemented.\n");
6122 heap_free(texture);
6123 return DDERR_INVALIDCAPS;
6126 else
6128 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6130 WARN("Tried to specify a back buffer count for a non-flippable surface.\n");
6131 hr = desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP ? DDERR_INVALIDPARAMS : DDERR_INVALIDCAPS;
6132 heap_free(texture);
6133 return hr;
6137 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6139 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6141 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
6142 heap_free(texture);
6143 return DDERR_INVALIDCAPS;
6146 if ((desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX) && !(desc->ddsCaps.dwCaps & DDSCAPS_FLIP))
6148 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
6149 heap_free(texture);
6150 return DDERR_INVALIDCAPS;
6153 if ((desc->ddsCaps.dwCaps & DDSCAPS_FLIP) && !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
6155 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
6156 heap_free(texture);
6157 return DDERR_NOEXCLUSIVEMODE;
6161 /* This is a special case in ddrawex, but not allowed in ddraw. */
6162 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6163 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6165 WARN("Tried to create a surface in both system and video memory.\n");
6166 heap_free(texture);
6167 return DDERR_INVALIDCAPS;
6170 if ((desc->ddsCaps.dwCaps & (DDSCAPS_ALLOCONLOAD | DDSCAPS_MIPMAP))
6171 && !(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6173 WARN("Caps %#x require DDSCAPS_TEXTURE.\n", desc->ddsCaps.dwCaps);
6174 heap_free(texture);
6175 return DDERR_INVALIDCAPS;
6178 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
6179 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
6181 WARN("Cube map faces requested without cube map flag.\n");
6182 heap_free(texture);
6183 return DDERR_INVALIDCAPS;
6186 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6187 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
6189 WARN("Cube map without faces requested.\n");
6190 heap_free(texture);
6191 return DDERR_INVALIDPARAMS;
6194 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6195 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
6196 FIXME("Partial cube maps not implemented.\n");
6198 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6200 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6202 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
6203 heap_free(texture);
6204 return DDERR_INVALIDCAPS;
6206 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6208 WARN("DDSCAPS2_TEXTUREMANAGE used with DDSCAPS_VIDEOMEMORY "
6209 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
6210 heap_free(texture);
6211 return DDERR_INVALIDCAPS;
6215 if (desc->ddsCaps.dwCaps & DDSCAPS_WRITEONLY
6216 && !(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6218 WARN("DDSCAPS_WRITEONLY used without DDSCAPS2_TEXTUREMANAGE, returning DDERR_INVALIDCAPS.\n");
6219 heap_free(texture);
6220 return DDERR_INVALIDCAPS;
6223 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
6225 ERR("Failed to get display mode, hr %#x.\n", hr);
6226 heap_free(texture);
6227 return hr_ddraw_from_wined3d(hr);
6230 wined3d_display_mode_format.dwSize = sizeof(wined3d_display_mode_format);
6231 ddrawformat_from_wined3dformat(&wined3d_display_mode_format, mode.format_id);
6233 /* No pixelformat given? Use the current screen format. */
6234 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
6236 desc->dwFlags |= DDSD_PIXELFORMAT;
6237 desc->u4.ddpfPixelFormat = wined3d_display_mode_format;
6240 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6241 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
6242 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
6244 WARN("Unsupported / unknown pixelformat.\n");
6245 heap_free(texture);
6246 return DDERR_INVALIDPIXELFORMAT;
6249 /* No width or no height? Use the screen size. */
6250 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
6252 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
6254 WARN("No width / height specified.\n");
6255 heap_free(texture);
6256 return DDERR_INVALIDPARAMS;
6259 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
6260 desc->dwWidth = mode.width;
6261 desc->dwHeight = mode.height;
6264 if (!desc->dwWidth || !desc->dwHeight)
6266 heap_free(texture);
6267 return DDERR_INVALIDPARAMS;
6270 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6271 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
6273 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6275 /* The first surface is a front buffer, the back buffers are created
6276 * afterwards. */
6277 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
6278 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
6280 struct wined3d_swapchain_desc swapchain_desc;
6282 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
6283 swapchain_desc.backbuffer_width = mode.width;
6284 swapchain_desc.backbuffer_height = mode.height;
6285 swapchain_desc.backbuffer_format = mode.format_id;
6287 if (ddraw->d3ddevice)
6289 if (ddraw->d3ddevice->recording)
6290 wined3d_stateblock_decref(ddraw->d3ddevice->recording);
6291 ddraw->d3ddevice->recording = NULL;
6292 ddraw->d3ddevice->update_state = ddraw->d3ddevice->state;
6294 wined3d_stateblock_reset(ddraw->state);
6296 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
6297 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
6299 ERR("Failed to reset device.\n");
6300 heap_free(texture);
6301 return hr_ddraw_from_wined3d(hr);
6304 wined3d_stateblock_set_render_state(ddraw->state, WINED3D_RS_ZENABLE,
6305 !!swapchain_desc.enable_auto_depth_stencil);
6309 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
6310 wined3d_desc.multisample_quality = 0;
6311 wined3d_desc.usage = 0;
6312 wined3d_desc.bind_flags = 0;
6313 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6314 wined3d_desc.width = desc->dwWidth;
6315 wined3d_desc.height = desc->dwHeight;
6316 wined3d_desc.depth = 1;
6317 wined3d_desc.size = 0;
6319 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
6321 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
6322 /* Do not fail surface creation, only fail 3D device creation. */
6325 /* Mipmap count fixes */
6326 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6328 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
6330 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
6332 /* Mipmap count is given, should not be 0. */
6333 if (!desc->u2.dwMipMapCount)
6335 heap_free(texture);
6336 return DDERR_INVALIDPARAMS;
6339 else
6341 /* Undocumented feature: Create sublevels until either the
6342 * width or the height is 1. */
6343 if (version == 7)
6344 desc->u2.dwMipMapCount = wined3d_log2i(max(desc->dwWidth, desc->dwHeight)) + 1;
6345 else
6346 desc->u2.dwMipMapCount = wined3d_log2i(min(desc->dwWidth, desc->dwHeight)) + 1;
6349 else
6351 desc->u2.dwMipMapCount = 1;
6354 desc->dwFlags |= DDSD_MIPMAPCOUNT;
6355 levels = desc->u2.dwMipMapCount;
6357 else
6359 levels = 1;
6362 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
6364 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6366 unsigned int bind_flags = 0;
6367 DWORD usage = 0;
6369 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6371 usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6372 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6374 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6376 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6379 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6380 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6381 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6382 bind_flags |= WINED3D_BIND_RENDER_TARGET;
6384 if (!(ddraw->flags & DDRAW_NO3D) && SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d,
6385 ddraw->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL, mode.format_id,
6386 usage, bind_flags, WINED3D_RTYPE_TEXTURE_2D, wined3d_desc.format)))
6388 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
6390 else
6392 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6393 sysmem_fallback = TRUE;
6396 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6398 /* Tests show surfaces without memory flags get these flags added
6399 * right after creation. */
6400 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
6404 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6405 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6407 WARN("System memory overlays are not allowed.\n");
6408 heap_free(texture);
6409 return DDERR_NOOVERLAYHW;
6412 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
6414 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_CPU
6415 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6417 else
6419 if (!(ddraw->flags & DDRAW_NO3D))
6421 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6422 wined3d_desc.bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6423 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6424 wined3d_desc.bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6425 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6426 wined3d_desc.bind_flags |= WINED3D_BIND_RENDER_TARGET;
6429 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6431 wined3d_desc.bind_flags &= ~WINED3D_BIND_RENDER_TARGET;
6432 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU
6433 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6434 /* Managed textures have the system memory flag set. */
6435 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6437 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
6439 /* Videomemory adds localvidmem. This is mutually exclusive with
6440 * systemmemory and texturemanage. */
6441 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
6442 /* Dynamic resources can't be written by the GPU. */
6443 if (!(wined3d_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
6444 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
6448 if (desc->dwFlags & DDSD_LPSURFACE)
6450 if (wined3d_desc.access & WINED3D_RESOURCE_ACCESS_GPU)
6452 WARN("User memory surfaces should not be GPU accessible.\n");
6453 heap_free(texture);
6454 return DDERR_INVALIDCAPS;
6457 if (version < 4)
6459 WARN("User memory surfaces not supported before version 4.\n");
6460 heap_free(texture);
6461 return DDERR_INVALIDPARAMS;
6464 if (!desc->lpSurface)
6466 WARN("NULL surface memory pointer specified.\n");
6467 heap_free(texture);
6468 return DDERR_INVALIDPARAMS;
6471 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6473 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
6475 WARN("Pitch specified on a compressed user memory surface.\n");
6476 heap_free(texture);
6477 return DDERR_INVALIDPARAMS;
6480 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
6482 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
6483 heap_free(texture);
6484 return DDERR_INVALIDPARAMS;
6487 if ((desc->dwFlags & DDSD_LINEARSIZE)
6488 && desc->u1.dwLinearSize < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6489 wined3d_desc.format, wined3d_desc.width) * ((desc->dwHeight + 3) / 4))
6491 WARN("Invalid linear size %u specified.\n", desc->u1.dwLinearSize);
6492 heap_free(texture);
6493 return DDERR_INVALIDPARAMS;
6496 else
6498 if (!(desc->dwFlags & DDSD_PITCH))
6500 WARN("User memory surfaces should explicitly specify the pitch.\n");
6501 heap_free(texture);
6502 return DDERR_INVALIDPARAMS;
6505 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6506 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
6508 WARN("Invalid pitch %u specified.\n", desc->u1.lPitch);
6509 heap_free(texture);
6510 return DDERR_INVALIDPARAMS;
6513 pitch = desc->u1.lPitch;
6517 if (((desc->dwFlags & DDSD_CKDESTOVERLAY)
6518 && desc->u3.ddckCKDestOverlay.dwColorSpaceLowValue != desc->u3.ddckCKDestOverlay.dwColorSpaceHighValue)
6519 || ((desc->dwFlags & DDSD_CKDESTBLT)
6520 && desc->ddckCKDestBlt.dwColorSpaceLowValue != desc->ddckCKDestBlt.dwColorSpaceHighValue)
6521 || ((desc->dwFlags & DDSD_CKSRCOVERLAY)
6522 && desc->ddckCKSrcOverlay.dwColorSpaceLowValue != desc->ddckCKSrcOverlay.dwColorSpaceHighValue)
6523 || ((desc->dwFlags & DDSD_CKSRCBLT)
6524 && desc->ddckCKSrcBlt.dwColorSpaceLowValue != desc->ddckCKSrcBlt.dwColorSpaceHighValue))
6526 WARN("Range color keys not supported, returning DDERR_NOCOLORKEYHW.\n");
6527 heap_free(texture);
6528 return DDERR_NOCOLORKEYHW;
6531 if ((ddraw->flags & DDRAW_NO3D) && (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
6533 WARN("Video memory surfaces not supported without 3D support.\n");
6534 heap_free(texture);
6535 return DDERR_NODIRECTDRAWHW;
6538 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
6539 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
6541 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
6542 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
6544 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6546 wined3d_desc.usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6547 layers = 6;
6550 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, layers, levels,
6551 texture, &wined3d_texture)))
6553 WARN("Failed to create wined3d texture, hr %#x.\n", hr);
6554 heap_free(texture);
6555 return hr_ddraw_from_wined3d(hr);
6558 root = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6560 root->is_complex_root = TRUE;
6561 root->sysmem_fallback = sysmem_fallback;
6562 texture->root = root;
6563 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6565 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6566 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTOVERLAY,
6567 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6568 if (desc->dwFlags & DDSD_CKDESTBLT)
6569 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTBLT,
6570 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6571 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6572 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCOVERLAY,
6573 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6574 if (desc->dwFlags & DDSD_CKSRCBLT)
6575 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCBLT,
6576 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6578 for (i = 0; i < layers; ++i)
6580 attach = &root->complex_array[layers - 1 - i];
6582 for (j = 0; j < levels; ++j)
6584 mip = wined3d_texture_get_sub_resource_parent(wined3d_texture, i * levels + j);
6585 mip->sysmem_fallback = sysmem_fallback;
6586 mip_desc = &mip->surface_desc;
6587 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6588 mip_desc->u2.dwMipMapCount = levels - j;
6590 if (j)
6592 wined3d_texture_get_sub_resource_desc(wined3d_texture, i * levels + j, &wined3d_mip_desc);
6593 mip_desc->dwWidth = wined3d_mip_desc.width;
6594 mip_desc->dwHeight = wined3d_mip_desc.height;
6596 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6598 else
6600 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6603 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6605 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6607 switch (i)
6609 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6610 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6611 break;
6612 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6613 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6614 break;
6615 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6616 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6617 break;
6618 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6619 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6620 break;
6621 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6622 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6623 break;
6624 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6625 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6626 break;
6631 if (mip == root)
6632 continue;
6634 *attach = mip;
6635 attach = &mip->complex_array[0];
6639 if ((desc->dwFlags & DDSD_LPSURFACE) && FAILED(hr = wined3d_texture_update_desc(wined3d_texture, 0,
6640 wined3d_desc.width, wined3d_desc.height, wined3d_desc.format,
6641 WINED3D_MULTISAMPLE_NONE, 0, desc->lpSurface, pitch)))
6643 ERR("Failed to set surface memory, hr %#x.\n", hr);
6644 goto fail;
6647 reserve_memory = !(desc->dwFlags & DDSD_LPSURFACE)
6648 && desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY
6649 && wined3d_display_mode_format.u1.dwRGBBitCount <= 16;
6651 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture, layers * levels)))
6653 ERR("Failed to reserve surface memory, hr %#x.\n", hr);
6654 goto fail;
6657 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6659 unsigned int count = desc->u5.dwBackBufferCount;
6660 struct ddraw_surface *last = root;
6662 attach = &last->complex_array[0];
6663 for (i = 0; i < count; ++i)
6665 if (!(texture = heap_alloc(sizeof(*texture))))
6667 hr = E_OUTOFMEMORY;
6668 goto fail;
6671 texture->texture_memory = NULL;
6672 texture->version = version;
6673 texture->surface_desc = root->surface_desc;
6674 desc = &texture->surface_desc;
6676 /* Only one surface in the flipping chain is a back buffer, one is
6677 * a front buffer, the others are just flippable surfaces. */
6678 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6679 | DDSCAPS_BACKBUFFER);
6680 if (!i)
6681 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6682 desc->u5.dwBackBufferCount = 0;
6684 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, 1, 1,
6685 texture, &wined3d_texture)))
6687 heap_free(texture);
6688 hr = hr_ddraw_from_wined3d(hr);
6689 goto fail;
6692 last = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6693 last->sysmem_fallback = sysmem_fallback;
6694 texture->root = last;
6695 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6697 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6698 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6699 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6700 if (desc->dwFlags & DDSD_CKDESTBLT)
6701 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6702 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6703 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6704 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6705 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6706 if (desc->dwFlags & DDSD_CKSRCBLT)
6707 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6708 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6710 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture, 1)))
6712 hr = hr_ddraw_from_wined3d(hr);
6713 goto fail;
6715 *attach = last;
6716 attach = &last->complex_array[0];
6718 *attach = root;
6721 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6723 ddraw->primary = root;
6724 ddraw->gdi_surface = root->wined3d_texture;
6726 *surface = root;
6728 return DD_OK;
6730 fail:
6731 if (version == 7)
6732 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6733 else if (version == 4)
6734 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6735 else
6736 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6738 return hr;
6741 void ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw,
6742 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
6743 const struct wined3d_parent_ops **parent_ops)
6745 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
6746 unsigned int texture_level, row_pitch, slice_pitch;
6747 DDSURFACEDESC2 *desc = &surface->surface_desc;
6748 unsigned int version = texture->version;
6750 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6751 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6752 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6753 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6754 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6755 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6756 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6757 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6758 surface->iface_count = 1;
6759 surface->version = version;
6760 surface->ddraw = ddraw;
6762 if (version == 7)
6764 surface->ref7 = 1;
6765 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6767 else if (version == 4)
6769 surface->ref4 = 1;
6770 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6772 else
6774 surface->ref1 = 1;
6775 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6778 *desc = texture->surface_desc;
6779 surface->first_attached = surface;
6781 texture_level = desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP ? sub_resource_idx % desc->u2.dwMipMapCount : 0;
6782 wined3d_texture_get_pitch(wined3d_texture, texture_level, &row_pitch, &slice_pitch);
6783 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6785 if (desc->dwFlags & DDSD_LPSURFACE)
6786 desc->u1.dwLinearSize = ~0u;
6787 else
6788 desc->u1.dwLinearSize = slice_pitch;
6789 desc->dwFlags |= DDSD_LINEARSIZE;
6790 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6792 else
6794 if (!(desc->dwFlags & DDSD_LPSURFACE))
6795 desc->u1.lPitch = row_pitch;
6796 desc->dwFlags |= DDSD_PITCH;
6797 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6799 desc->lpSurface = NULL;
6801 wined3d_texture_incref(surface->wined3d_texture = wined3d_texture);
6802 surface->sub_resource_idx = sub_resource_idx;
6803 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6804 surface->texture_location = DDRAW_SURFACE_LOCATION_DEFAULT;
6806 wined3d_private_store_init(&surface->private_store);
6809 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
6811 struct ddraw_surface *surface = parent;
6813 /* If the surface reference count drops to zero, we release our reference
6814 * to the view, but don't clear the pointer yet, in case e.g. a
6815 * GetRenderTarget() call brings the surface back before the view is
6816 * actually destroyed. When the view is destroyed, we need to clear the
6817 * pointer, or a subsequent surface AddRef() would reference it again.
6819 * This is safe because as long as the view still has a reference to the
6820 * texture, the surface is also still alive, and we're called before the
6821 * view releases that reference. */
6822 surface->wined3d_rtv = NULL;
6825 static const struct wined3d_parent_ops ddraw_view_wined3d_parent_ops =
6827 view_wined3d_object_destroyed,
6830 struct wined3d_rendertarget_view *ddraw_surface_get_rendertarget_view(struct ddraw_surface *surface)
6832 struct wined3d_texture *wined3d_texture;
6833 HRESULT hr;
6835 if (surface->wined3d_rtv)
6836 return surface->wined3d_rtv;
6838 wined3d_texture = surface->draw_texture ? surface->draw_texture : surface->wined3d_texture;
6839 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(wined3d_texture,
6840 surface->sub_resource_idx, surface, &ddraw_view_wined3d_parent_ops, &surface->wined3d_rtv)))
6842 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
6843 return NULL;
6846 return surface->wined3d_rtv;