l3codeca.acm: Avoid mpg123 functions with suffix.
[wine.git] / dlls / ddraw / surface.c
blob421ce68fa1ca23c4ebaeac6bc5e240ff259b17a2
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 %#lx.\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 %#lx.\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 %lu.\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 %lu.\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 %lu.\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 %lu.\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 %lu.\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 %lu.\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 %lu.\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: %lu 4: %lu 3: %lu 2: %lu 1: %lu.\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 %lu.\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 %lu.\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 %lu.\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 %lu.\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 %lu.\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 %lu.\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 %lu.\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 %#lx, %#lx, %#lx, %#lx.\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 %#lx, %#lx, %#lx, %#lx.\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 %#lx, %#lx, %#lx, %#lx.\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 %#lx, 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 %lu.\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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx.\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 %#lx.\n", flags);
1454 else
1455 WARN("Ignoring flags %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 DWORD 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 %#lx.\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 %#lx.\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 %#lx, 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 %#lx.\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 %#lx, 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 %#lx 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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx.\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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx, 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 %lu.\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 %lu, flags %#lx.\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 %lu, flags %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %lu, flags %#lx.\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 %lu, flags %#lx.\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 %lu, flags %#lx.\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 %lu, flags %#lx.\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 %lu, flags %#lx.\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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx, 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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 %#lx.\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 *surface_desc)
3556 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3558 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3560 if (!surface_desc) return DDERR_INVALIDPARAMS;
3562 if (surface_desc->dwSize != sizeof(DDSURFACEDESC2))
3564 WARN("Incorrect struct size %lu.\n", surface_desc->dwSize);
3565 return DDERR_INVALIDPARAMS;
3568 wined3d_mutex_lock();
3569 DD_STRUCT_COPY_BYSIZE(surface_desc, &surface->surface_desc);
3570 TRACE("Returning surface desc:\n");
3571 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(surface_desc);
3572 wined3d_mutex_unlock();
3574 return DD_OK;
3577 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3579 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3581 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3583 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3586 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3588 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3590 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3592 if (!surface_desc) return DDERR_INVALIDPARAMS;
3594 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3596 WARN("Incorrect structure size %lu.\n", surface_desc->dwSize);
3597 return DDERR_INVALIDPARAMS;
3600 wined3d_mutex_lock();
3601 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3602 TRACE("Returning surface desc:\n");
3603 if (TRACE_ON(ddraw))
3605 /* DDRAW_dump_surface_desc handles the smaller size */
3606 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3608 wined3d_mutex_unlock();
3610 return DD_OK;
3613 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3615 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3617 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3619 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3622 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3624 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3626 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3628 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3631 /*****************************************************************************
3632 * IDirectDrawSurface7::Initialize
3634 * Initializes the surface. This is a no-op in Wine
3636 * Params:
3637 * DD: Pointer to an DirectDraw interface
3638 * DDSD: Surface description for initialization
3640 * Returns:
3641 * DDERR_ALREADYINITIALIZED
3643 *****************************************************************************/
3644 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3645 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3647 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3649 return DDERR_ALREADYINITIALIZED;
3652 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3653 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3655 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3657 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3659 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3660 ddraw, surface_desc);
3663 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3664 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3666 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3667 DDSURFACEDESC2 surface_desc2;
3669 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3671 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3672 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3673 ddraw, surface_desc ? &surface_desc2 : NULL);
3676 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3677 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3679 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3680 DDSURFACEDESC2 surface_desc2;
3682 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3684 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3685 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3686 ddraw, surface_desc ? &surface_desc2 : NULL);
3689 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3690 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3692 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3693 DDSURFACEDESC2 surface_desc2;
3695 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3697 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3698 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3699 ddraw, surface_desc ? &surface_desc2 : NULL);
3702 /*****************************************************************************
3703 * IDirect3DTexture1::Initialize
3705 * The sdk says it's not implemented
3707 * Params:
3710 * Returns
3711 * DDERR_UNSUPPORTED
3713 *****************************************************************************/
3714 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3715 IDirect3DDevice *device, IDirectDrawSurface *surface)
3717 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3719 return DDERR_UNSUPPORTED; /* Unchecked */
3722 /*****************************************************************************
3723 * IDirectDrawSurface7::IsLost
3725 * Checks if the surface is lost
3727 * Returns:
3728 * DD_OK, if the surface is usable
3729 * DDERR_ISLOST if the surface is lost
3731 *****************************************************************************/
3732 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3734 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3736 TRACE("iface %p.\n", iface);
3738 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3741 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3743 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3745 TRACE("iface %p.\n", iface);
3747 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3750 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3752 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3754 TRACE("iface %p.\n", iface);
3756 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3759 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3761 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3763 TRACE("iface %p.\n", iface);
3765 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3768 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3770 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3772 TRACE("iface %p.\n", iface);
3774 return ddraw_surface_is_lost(surface) ? DDERR_SURFACELOST : DD_OK;
3777 /*****************************************************************************
3778 * IDirectDrawSurface7::Restore
3780 * Restores a lost surface. This makes the surface usable again, but
3781 * doesn't reload its old contents
3783 * Returns:
3784 * DD_OK on success, error code otherwise.
3786 *****************************************************************************/
3787 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3789 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3790 unsigned int i;
3792 TRACE("iface %p.\n", iface);
3794 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3796 struct wined3d_swapchain *swapchain = surface->ddraw->wined3d_swapchain;
3797 struct wined3d_sub_resource_desc wined3d_desc;
3798 struct wined3d_display_mode mode;
3799 HRESULT hr;
3801 if (FAILED(hr = wined3d_swapchain_get_display_mode(swapchain, &mode, NULL)))
3803 WARN("Failed to get display mode, hr %#lx.\n", hr);
3804 return hr;
3807 if (FAILED(hr = wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, 0, &wined3d_desc)))
3809 WARN("Failed to get resource desc, hr %#lx.\n", hr);
3810 return hr;
3813 if (mode.width != wined3d_desc.width || mode.height != wined3d_desc.height)
3815 WARN("Display mode dimensions %ux%u don't match surface dimensions %ux%u.\n",
3816 mode.width, mode.height, wined3d_desc.width, wined3d_desc.height);
3817 return DDERR_WRONGMODE;
3820 if (mode.format_id != wined3d_desc.format)
3822 WARN("Display mode format %#x doesn't match surface format %#x.\n",
3823 mode.format_id, wined3d_desc.format);
3824 return DDERR_WRONGMODE;
3828 if (!ddraw_surface_can_be_lost(surface))
3829 return DD_OK;
3830 ddraw_update_lost_surfaces(surface->ddraw);
3831 if (surface->ddraw->device_state == DDRAW_DEVICE_STATE_LOST)
3832 return DDERR_WRONGMODE;
3834 surface->is_lost = FALSE;
3835 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
3837 if (surface->complex_array[i])
3838 surface->complex_array[i]->is_lost = FALSE;
3841 return DD_OK;
3844 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3846 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3848 TRACE("iface %p.\n", iface);
3850 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3853 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3855 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3857 TRACE("iface %p.\n", iface);
3859 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3862 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3864 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3866 TRACE("iface %p.\n", iface);
3868 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3871 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3873 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3875 TRACE("iface %p.\n", iface);
3877 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3880 /*****************************************************************************
3881 * IDirectDrawSurface7::SetOverlayPosition
3883 * Changes the display coordinates of an overlay surface
3885 * Params:
3886 * X:
3887 * Y:
3889 * Returns:
3890 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3891 *****************************************************************************/
3892 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG x, LONG y)
3894 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3895 HRESULT hr;
3897 TRACE("iface %p, x %ld, y %ld.\n", iface, x, y);
3899 wined3d_mutex_lock();
3900 hr = wined3d_texture_set_overlay_position(surface->wined3d_texture,
3901 surface->sub_resource_idx, x, y);
3902 wined3d_mutex_unlock();
3904 return hr;
3907 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3909 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3911 TRACE("iface %p, x %ld, y %ld.\n", iface, x, y);
3913 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3916 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3918 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3920 TRACE("iface %p, x %ld, y %ld.\n", iface, x, y);
3922 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3925 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3927 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3929 TRACE("iface %p, x %ld, y %ld.\n", iface, x, y);
3931 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3934 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3936 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3938 TRACE("iface %p, x %ld, y %ld.\n", iface, x, y);
3940 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3943 /*****************************************************************************
3944 * IDirectDrawSurface7::UpdateOverlay
3946 * Modifies the attributes of an overlay surface.
3948 * Params:
3949 * SrcRect: The section of the source being used for the overlay
3950 * DstSurface: Address of the surface that is overlaid
3951 * DstRect: Place of the overlay
3952 * Flags: some DDOVER_* flags
3954 * Returns:
3955 * DDERR_UNSUPPORTED, because we don't support overlays
3957 *****************************************************************************/
3958 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *src_rect,
3959 IDirectDrawSurface7 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3961 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3962 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(dst_surface);
3963 struct wined3d_texture *dst_wined3d_texture = NULL;
3964 unsigned int dst_sub_resource_idx = 0;
3965 HRESULT hr;
3967 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#lx, fx %p.\n",
3968 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3970 if (fx)
3971 FIXME("Ignoring fx %p.\n", fx);
3973 wined3d_mutex_lock();
3974 if (dst_impl)
3976 dst_wined3d_texture = dst_impl->wined3d_texture;
3977 dst_sub_resource_idx = dst_impl->sub_resource_idx;
3979 hr = wined3d_texture_update_overlay(src_impl->wined3d_texture, src_impl->sub_resource_idx,
3980 src_rect, dst_wined3d_texture, dst_sub_resource_idx, dst_rect, flags);
3981 wined3d_mutex_unlock();
3983 return hr_ddraw_from_wined3d(hr);
3986 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3987 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3989 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3990 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3992 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#lx, fx %p.\n",
3993 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3995 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3996 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3999 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
4000 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4002 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
4003 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
4005 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#lx, fx %p.\n",
4006 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4008 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4009 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4012 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
4013 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4015 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
4016 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
4018 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#lx, fx %p.\n",
4019 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4021 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4022 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4025 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
4026 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
4028 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
4029 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
4031 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#lx, fx %p.\n",
4032 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
4034 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
4035 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
4038 /*****************************************************************************
4039 * IDirectDrawSurface7::UpdateOverlayDisplay
4041 * The DX7 sdk says that it's not implemented
4043 * Params:
4044 * Flags: ?
4046 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
4048 *****************************************************************************/
4049 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
4051 TRACE("iface %p, flags %#lx.\n", iface, Flags);
4053 return DDERR_UNSUPPORTED;
4056 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
4058 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4060 TRACE("iface %p, flags %#lx.\n", iface, flags);
4062 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4065 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
4067 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4069 TRACE("iface %p, flags %#lx.\n", iface, flags);
4071 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4074 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
4076 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4078 TRACE("iface %p, flags %#lx.\n", iface, flags);
4080 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4083 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
4085 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4087 TRACE("iface %p, flags %#lx.\n", iface, flags);
4089 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
4092 /*****************************************************************************
4093 * IDirectDrawSurface7::UpdateOverlayZOrder
4095 * Sets an overlay's Z order
4097 * Params:
4098 * Flags: DDOVERZ_* flags
4099 * DDSRef: Defines the relative position in the overlay chain
4101 * Returns:
4102 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
4104 *****************************************************************************/
4105 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
4106 DWORD flags, IDirectDrawSurface7 *reference)
4108 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4110 FIXME("iface %p, flags %#lx, reference %p stub!\n", iface, flags, reference);
4112 wined3d_mutex_lock();
4113 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OVERLAY))
4115 WARN("Not an overlay surface.\n");
4116 wined3d_mutex_unlock();
4117 return DDERR_NOTAOVERLAYSURFACE;
4119 wined3d_mutex_unlock();
4121 return DD_OK;
4124 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
4125 DWORD flags, IDirectDrawSurface4 *reference)
4127 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4128 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
4130 TRACE("iface %p, flags %#lx, reference %p.\n", iface, flags, reference);
4132 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4133 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4136 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
4137 DWORD flags, IDirectDrawSurface3 *reference)
4139 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4140 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
4142 TRACE("iface %p, flags %#lx, reference %p.\n", iface, flags, reference);
4144 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4145 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4148 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
4149 DWORD flags, IDirectDrawSurface2 *reference)
4151 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4152 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
4154 TRACE("iface %p, flags %#lx, reference %p.\n", iface, flags, reference);
4156 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4157 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4160 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
4161 DWORD flags, IDirectDrawSurface *reference)
4163 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4164 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
4166 TRACE("iface %p, flags %#lx, reference %p.\n", iface, flags, reference);
4168 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
4169 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
4172 /*****************************************************************************
4173 * IDirectDrawSurface7::GetDDInterface
4175 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
4176 * surface belongs to
4178 * Params:
4179 * DD: Address to write the interface pointer to
4181 * Returns:
4182 * DD_OK on success
4183 * DDERR_INVALIDPARAMS if DD is NULL
4185 *****************************************************************************/
4186 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
4188 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4190 TRACE("iface %p, ddraw %p.\n", iface, DD);
4192 if(!DD)
4193 return DDERR_INVALIDPARAMS;
4195 switch(This->version)
4197 case 7:
4198 *DD = &This->ddraw->IDirectDraw7_iface;
4199 break;
4201 case 4:
4202 *DD = &This->ddraw->IDirectDraw4_iface;
4203 break;
4205 case 2:
4206 *DD = &This->ddraw->IDirectDraw2_iface;
4207 break;
4209 case 1:
4210 *DD = &This->ddraw->IDirectDraw_iface;
4211 break;
4214 IUnknown_AddRef((IUnknown *)*DD);
4216 return DD_OK;
4219 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
4221 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4223 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4225 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4228 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
4230 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4232 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4234 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4237 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
4239 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4241 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4243 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4246 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
4248 TRACE("iface %p.\n", iface);
4250 return DD_OK;
4253 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
4255 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4257 TRACE("iface %p.\n", iface);
4259 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
4262 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
4264 TRACE("iface %p, value %p.\n", iface, pValue);
4266 *pValue = 0;
4268 return DD_OK;
4271 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
4273 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4275 TRACE("iface %p, value %p.\n", iface, pValue);
4277 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
4280 /*****************************************************************************
4281 * IDirectDrawSurface7::SetLOD
4283 * Sets the level of detail of a texture
4285 * Params:
4286 * MaxLOD: LOD to set
4288 * Returns:
4289 * DD_OK on success
4290 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4292 *****************************************************************************/
4293 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
4295 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4296 HRESULT hr;
4298 TRACE("iface %p, lod %lu.\n", iface, MaxLOD);
4300 wined3d_mutex_lock();
4301 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4303 wined3d_mutex_unlock();
4304 return DDERR_INVALIDOBJECT;
4307 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
4308 if (SUCCEEDED(hr) && surface->draw_texture)
4309 hr = wined3d_texture_set_lod(surface->draw_texture, MaxLOD);
4310 wined3d_mutex_unlock();
4312 return hr;
4315 /*****************************************************************************
4316 * IDirectDrawSurface7::GetLOD
4318 * Returns the level of detail of a Direct3D texture
4320 * Params:
4321 * MaxLOD: Address to write the LOD to
4323 * Returns:
4324 * DD_OK on success
4325 * DDERR_INVALIDPARAMS if MaxLOD is NULL
4326 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4328 *****************************************************************************/
4329 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4331 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4333 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4335 if(!MaxLOD)
4336 return DDERR_INVALIDPARAMS;
4338 wined3d_mutex_lock();
4339 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4341 wined3d_mutex_unlock();
4342 return DDERR_INVALIDOBJECT;
4345 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4346 wined3d_mutex_unlock();
4348 return DD_OK;
4351 /*****************************************************************************
4352 * IDirectDrawSurface7::BltFast
4354 * Performs a fast Blit.
4356 * Params:
4357 * dstx: The x coordinate to blit to on the destination
4358 * dsty: The y coordinate to blit to on the destination
4359 * Source: The source surface
4360 * rsrc: The source rectangle
4361 * trans: Type of transfer. Some DDBLTFAST_* flags
4363 * Returns:
4364 * DD_OK on success, error code otherwise.
4366 *****************************************************************************/
4367 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltFast(IDirectDrawSurface7 *iface,
4368 DWORD dst_x, DWORD dst_y, IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD trans)
4370 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
4371 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src_surface);
4372 DWORD flags = WINED3D_BLT_SYNCHRONOUS;
4373 DWORD src_w, src_h, dst_w, dst_h;
4374 HRESULT hr = DD_OK;
4375 RECT dst_rect, s;
4377 TRACE("iface %p, dst_x %lu, dst_y %lu, src_surface %p, src_rect %s, flags %#lx.\n",
4378 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), trans);
4380 dst_w = dst_impl->surface_desc.dwWidth;
4381 dst_h = dst_impl->surface_desc.dwHeight;
4383 if (!src_rect)
4385 SetRect(&s, 0, 0, src_impl->surface_desc.dwWidth, src_impl->surface_desc.dwHeight);
4386 src_rect = &s;
4389 src_w = src_rect->right - src_rect->left;
4390 src_h = src_rect->bottom - src_rect->top;
4391 if (src_w > dst_w || dst_x > dst_w - src_w
4392 || src_h > dst_h || dst_y > dst_h - src_h)
4394 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4395 return DDERR_INVALIDRECT;
4398 SetRect(&dst_rect, dst_x, dst_y, dst_x + src_w, dst_y + src_h);
4399 if (trans & DDBLTFAST_SRCCOLORKEY)
4400 flags |= WINED3D_BLT_SRC_CKEY;
4401 if (trans & DDBLTFAST_DESTCOLORKEY)
4402 flags |= WINED3D_BLT_DST_CKEY;
4403 if (trans & DDBLTFAST_WAIT)
4404 flags |= WINED3D_BLT_WAIT;
4405 if (trans & DDBLTFAST_DONOTWAIT)
4406 flags |= WINED3D_BLT_DO_NOT_WAIT;
4408 wined3d_mutex_lock();
4409 if (dst_impl->clipper)
4411 wined3d_mutex_unlock();
4412 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4413 return DDERR_BLTFASTCANTCLIP;
4416 if (src_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4417 hr = ddraw_surface_update_frontbuffer(src_impl, src_rect, TRUE, 0);
4418 if (SUCCEEDED(hr))
4419 hr = wined3d_device_context_blt(dst_impl->ddraw->immediate_context,
4420 ddraw_surface_get_any_texture(dst_impl, DDRAW_SURFACE_RW), dst_impl->sub_resource_idx, &dst_rect,
4421 ddraw_surface_get_any_texture(src_impl,DDRAW_SURFACE_READ), src_impl->sub_resource_idx, src_rect,
4422 flags, NULL, WINED3D_TEXF_POINT);
4423 if (SUCCEEDED(hr) && (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4424 hr = ddraw_surface_update_frontbuffer(dst_impl, &dst_rect, FALSE, 0);
4425 wined3d_mutex_unlock();
4427 switch(hr)
4429 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4430 default: return hr;
4434 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4435 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4437 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4438 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4440 TRACE("iface %p, dst_x %lu, dst_y %lu, src_surface %p, src_rect %s, flags %#lx.\n",
4441 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4443 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4444 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4447 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4448 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4450 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4451 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4453 TRACE("iface %p, dst_x %lu, dst_y %lu, src_surface %p, src_rect %s, flags %#lx.\n",
4454 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4456 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4457 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4460 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4461 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4463 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4464 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4466 TRACE("iface %p, dst_x %lu, dst_y %lu, src_surface %p, src_rect %s, flags %#lx.\n",
4467 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4469 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4470 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4473 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4474 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4476 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4477 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4479 TRACE("iface %p, dst_x %lu, dst_y %lu, src_surface %p, src_rect %s, flags %#lx.\n",
4480 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4482 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4483 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4486 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **clipper)
4488 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4490 TRACE("iface %p, clipper %p.\n", iface, clipper);
4492 if (!clipper)
4493 return DDERR_INVALIDPARAMS;
4495 wined3d_mutex_lock();
4496 if (!surface->clipper)
4498 wined3d_mutex_unlock();
4499 *clipper = NULL;
4500 return DDERR_NOCLIPPERATTACHED;
4503 *clipper = &surface->clipper->IDirectDrawClipper_iface;
4504 if (ddraw_clipper_is_valid(surface->clipper))
4505 IDirectDrawClipper_AddRef(*clipper);
4506 wined3d_mutex_unlock();
4508 return DD_OK;
4511 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4513 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4515 TRACE("iface %p, clipper %p.\n", iface, clipper);
4517 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4520 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4522 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4524 TRACE("iface %p, clipper %p.\n", iface, clipper);
4526 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4529 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4531 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4533 TRACE("iface %p, clipper %p.\n", iface, clipper);
4535 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4538 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4540 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4542 TRACE("iface %p, clipper %p.\n", iface, clipper);
4544 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4547 /*****************************************************************************
4548 * IDirectDrawSurface7::SetClipper
4550 * Sets a clipper for the surface
4552 * Params:
4553 * Clipper: IDirectDrawClipper interface of the clipper to set
4555 * Returns:
4556 * DD_OK on success
4558 *****************************************************************************/
4559 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4560 IDirectDrawClipper *iclipper)
4562 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4563 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4564 struct ddraw_clipper *old_clipper = This->clipper;
4565 HWND clipWindow;
4567 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4569 wined3d_mutex_lock();
4570 if (clipper == This->clipper)
4572 wined3d_mutex_unlock();
4573 return DD_OK;
4576 This->clipper = clipper;
4578 if (clipper != NULL)
4579 IDirectDrawClipper_AddRef(iclipper);
4580 if (old_clipper && ddraw_clipper_is_valid(old_clipper))
4581 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4583 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4585 clipWindow = NULL;
4586 if(clipper) {
4587 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4590 if (clipWindow)
4592 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4593 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4595 else
4597 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4598 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4602 wined3d_mutex_unlock();
4604 return DD_OK;
4607 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4609 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4611 TRACE("iface %p, clipper %p.\n", iface, clipper);
4613 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4616 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4618 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4620 TRACE("iface %p, clipper %p.\n", iface, clipper);
4622 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4625 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4627 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4629 TRACE("iface %p, clipper %p.\n", iface, clipper);
4631 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4634 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4636 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4638 TRACE("iface %p, clipper %p.\n", iface, clipper);
4640 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4643 /*****************************************************************************
4644 * IDirectDrawSurface7::SetSurfaceDesc
4646 * Sets the surface description. It can override the pixel format, the surface
4647 * memory, ...
4648 * It's not really tested.
4650 * Params:
4651 * DDSD: Pointer to the new surface description to set
4652 * Flags: Some flags
4654 * Returns:
4655 * DD_OK on success
4656 * DDERR_INVALIDPARAMS if DDSD is NULL
4658 *****************************************************************************/
4659 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4661 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4662 HRESULT hr;
4663 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4664 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4665 enum wined3d_format_id format_id;
4666 UINT pitch, width, height;
4668 TRACE("iface %p, surface_desc %p, flags %#lx.\n", iface, DDSD, Flags);
4670 if (!DDSD)
4672 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4673 return DDERR_INVALIDPARAMS;
4675 if (Flags)
4677 WARN("Flags is %lx, returning DDERR_INVALIDPARAMS\n", Flags);
4678 return DDERR_INVALIDPARAMS;
4680 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4681 || surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4682 || surface->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4684 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4685 return DDERR_INVALIDSURFACETYPE;
4688 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4689 * for PIXELFORMAT to work */
4690 if (DDSD->dwFlags & ~allowed_flags)
4692 WARN("Invalid flags %#lx set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4693 return DDERR_INVALIDPARAMS;
4695 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4697 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4698 return DDERR_INVALIDPARAMS;
4700 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4702 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4703 return DDERR_INVALIDCAPS;
4705 if (DDSD->dwFlags & DDSD_WIDTH)
4707 if (!(DDSD->dwFlags & DDSD_PITCH))
4709 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4710 return DDERR_INVALIDPARAMS;
4712 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4714 WARN("Pitch is %ld, width is %lu, returning DDERR_INVALIDPARAMS.\n",
4715 DDSD->u1.lPitch, DDSD->dwWidth);
4716 return DDERR_INVALIDPARAMS;
4718 if (DDSD->dwWidth != surface->surface_desc.dwWidth)
4719 TRACE("Surface width changed from %lu to %lu.\n", surface->surface_desc.dwWidth, DDSD->dwWidth);
4720 if (DDSD->u1.lPitch != surface->surface_desc.u1.lPitch)
4721 TRACE("Surface pitch changed from %lu to %lu.\n", surface->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4722 pitch = DDSD->u1.lPitch;
4723 width = DDSD->dwWidth;
4725 else if (DDSD->dwFlags & DDSD_PITCH)
4727 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4728 return DDERR_INVALIDPARAMS;
4730 else
4732 pitch = surface->surface_desc.u1.lPitch;
4733 width = surface->surface_desc.dwWidth;
4736 if (DDSD->dwFlags & DDSD_HEIGHT)
4738 if (!DDSD->dwHeight)
4740 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4741 return DDERR_INVALIDPARAMS;
4743 if (DDSD->dwHeight != surface->surface_desc.dwHeight)
4744 TRACE("Surface height changed from %lu to %lu.\n", surface->surface_desc.dwHeight, DDSD->dwHeight);
4745 height = DDSD->dwHeight;
4747 else
4749 height = surface->surface_desc.dwHeight;
4752 wined3d_mutex_lock();
4753 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4755 enum wined3d_format_id current_format_id;
4756 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4758 if (format_id == WINED3DFMT_UNKNOWN)
4760 ERR("Requested to set an unknown pixelformat\n");
4761 wined3d_mutex_unlock();
4762 return DDERR_INVALIDPARAMS;
4764 current_format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4765 if (format_id != current_format_id)
4766 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4768 else
4770 format_id = wined3dformat_from_ddrawformat(&surface->surface_desc.u4.ddpfPixelFormat);
4773 if (FAILED(hr = wined3d_texture_update_desc(surface->wined3d_texture, surface->sub_resource_idx,
4774 width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4776 WARN("Failed to update surface desc, hr %#lx.\n", hr);
4777 wined3d_mutex_unlock();
4778 return hr_ddraw_from_wined3d(hr);
4781 if (surface->draw_texture && FAILED(hr = wined3d_texture_update_desc(surface->draw_texture,
4782 surface->sub_resource_idx, width, height, format_id, WINED3D_MULTISAMPLE_NONE, 0, NULL, 0)))
4784 ERR("Failed to update surface desc for draw_texture, hr %#lx.\n", hr);
4785 wined3d_mutex_unlock();
4786 return hr_ddraw_from_wined3d(hr);
4789 if (DDSD->dwFlags & DDSD_WIDTH)
4790 surface->surface_desc.dwWidth = width;
4791 if (DDSD->dwFlags & DDSD_PITCH)
4792 surface->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4793 if (DDSD->dwFlags & DDSD_HEIGHT)
4794 surface->surface_desc.dwHeight = height;
4795 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4796 surface->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4798 wined3d_mutex_unlock();
4800 return DD_OK;
4803 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4804 DDSURFACEDESC2 *surface_desc, DWORD flags)
4806 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4808 TRACE("iface %p, surface_desc %p, flags %#lx.\n", iface, surface_desc, flags);
4810 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4811 surface_desc, flags);
4814 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4815 DDSURFACEDESC *surface_desc, DWORD flags)
4817 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4818 DDSURFACEDESC2 surface_desc2;
4820 TRACE("iface %p, surface_desc %p, flags %#lx.\n", iface, surface_desc, flags);
4822 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4823 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4824 surface_desc ? &surface_desc2 : NULL, flags);
4827 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4829 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4830 struct ddraw_palette *palette_impl;
4831 HRESULT hr = DD_OK;
4833 TRACE("iface %p, palette %p.\n", iface, palette);
4835 if (!palette)
4836 return DDERR_INVALIDPARAMS;
4837 if (ddraw_surface_is_lost(surface))
4839 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4840 return DDERR_SURFACELOST;
4843 wined3d_mutex_lock();
4844 if ((palette_impl = surface->palette))
4846 *palette = &palette_impl->IDirectDrawPalette_iface;
4847 IDirectDrawPalette_AddRef(*palette);
4849 else
4851 *palette = NULL;
4852 hr = DDERR_NOPALETTEATTACHED;
4854 wined3d_mutex_unlock();
4856 return hr;
4859 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4861 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4863 TRACE("iface %p, palette %p.\n", iface, palette);
4865 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4868 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4870 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4872 TRACE("iface %p, palette %p.\n", iface, palette);
4874 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4877 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4879 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4881 TRACE("iface %p, palette %p.\n", iface, palette);
4883 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4886 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4888 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4890 TRACE("iface %p, palette %p.\n", iface, palette);
4892 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4895 static HRESULT ddraw_surface_set_wined3d_textures_colour_key(struct ddraw_surface *surface, DWORD flags,
4896 struct wined3d_color_key *color_key)
4898 HRESULT hr;
4900 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags, color_key);
4901 if (surface->draw_texture && SUCCEEDED(hr))
4902 hr = wined3d_texture_set_color_key(surface->draw_texture, flags, color_key);
4904 return hr;
4907 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4909 DDCOLORKEY fixed_color_key;
4910 HRESULT hr = WINED3D_OK;
4912 if (flags & DDCKEY_COLORSPACE)
4914 if (color_key && color_key->dwColorSpaceLowValue != color_key->dwColorSpaceHighValue)
4916 WARN("Range color keys are not supported, returning DDERR_NOCOLORKEYHW.\n");
4917 return DDERR_NOCOLORKEYHW;
4919 flags &= ~DDCKEY_COLORSPACE;
4922 wined3d_mutex_lock();
4924 if (color_key)
4926 fixed_color_key.dwColorSpaceLowValue = fixed_color_key.dwColorSpaceHighValue = color_key->dwColorSpaceLowValue;
4927 switch (flags & ~DDCKEY_COLORSPACE)
4929 case DDCKEY_DESTBLT:
4930 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4931 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4932 break;
4934 case DDCKEY_DESTOVERLAY:
4935 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4936 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4937 break;
4939 case DDCKEY_SRCOVERLAY:
4940 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4941 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4942 break;
4944 case DDCKEY_SRCBLT:
4945 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4946 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4947 break;
4949 default:
4950 wined3d_mutex_unlock();
4951 return DDERR_INVALIDPARAMS;
4954 else
4956 switch (flags & ~DDCKEY_COLORSPACE)
4958 case DDCKEY_DESTBLT:
4959 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4960 break;
4962 case DDCKEY_DESTOVERLAY:
4963 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4964 break;
4966 case DDCKEY_SRCOVERLAY:
4967 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4968 break;
4970 case DDCKEY_SRCBLT:
4971 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4972 break;
4974 default:
4975 wined3d_mutex_unlock();
4976 return DDERR_INVALIDPARAMS;
4980 if (surface->is_complex_root)
4981 hr = ddraw_surface_set_wined3d_textures_colour_key(surface, flags,
4982 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4984 wined3d_mutex_unlock();
4986 return hr_ddraw_from_wined3d(hr);
4989 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4991 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4993 TRACE("iface %p, flags %#lx, color_key %p.\n", iface, flags, color_key);
4995 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4996 return DDERR_NOTONMIPMAPSUBLEVEL;
4998 return ddraw_surface_set_color_key(surface, flags, color_key);
5001 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
5003 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
5005 TRACE("iface %p, flags %#lx, color_key %p.\n", iface, flags, color_key);
5007 return ddraw_surface_set_color_key(surface, flags, color_key);
5010 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
5012 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
5014 TRACE("iface %p, flags %#lx, color_key %p.\n", iface, flags, color_key);
5016 return ddraw_surface_set_color_key(surface, flags, color_key);
5019 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
5021 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
5023 TRACE("iface %p, flags %#lx, color_key %p.\n", iface, flags, color_key);
5025 return ddraw_surface_set_color_key(surface, flags, color_key);
5028 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
5030 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5032 TRACE("iface %p, flags %#lx, color_key %p.\n", iface, flags, color_key);
5034 return ddraw_surface_set_color_key(surface, flags, color_key);
5037 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
5039 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
5041 TRACE("iface %p, palette %p.\n", iface, palette);
5043 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
5044 return DDERR_NOTONMIPMAPSUBLEVEL;
5045 if (ddraw_surface_is_lost(surface))
5047 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5048 return DDERR_SURFACELOST;
5051 return ddraw_surface_set_palette(surface, palette);
5054 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
5056 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
5058 TRACE("iface %p, palette %p.\n", iface, palette);
5060 if (ddraw_surface_is_lost(surface))
5062 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5063 return DDERR_SURFACELOST;
5066 return ddraw_surface_set_palette(surface, palette);
5069 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
5071 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
5073 TRACE("iface %p, palette %p.\n", iface, palette);
5075 if (ddraw_surface_is_lost(surface))
5077 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5078 return DDERR_SURFACELOST;
5081 return ddraw_surface_set_palette(surface, palette);
5084 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
5086 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
5088 TRACE("iface %p, palette %p.\n", iface, palette);
5090 if (ddraw_surface_is_lost(surface))
5092 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5093 return DDERR_SURFACELOST;
5096 return ddraw_surface_set_palette(surface, palette);
5099 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
5101 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
5103 TRACE("iface %p, palette %p.\n", iface, palette);
5105 if (ddraw_surface_is_lost(surface))
5107 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
5108 return DDERR_SURFACELOST;
5111 return ddraw_surface_set_palette(surface, palette);
5114 /**********************************************************
5115 * IDirectDrawGammaControl::GetGammaRamp
5117 * Returns the current gamma ramp for a surface
5119 * Params:
5120 * flags: Ignored
5121 * gamma_ramp: Address to write the ramp to
5123 * Returns:
5124 * DD_OK on success
5125 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5127 **********************************************************/
5128 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
5129 DWORD flags, DDGAMMARAMP *gamma_ramp)
5131 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5133 TRACE("iface %p, flags %#lx, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5135 if (!gamma_ramp)
5137 WARN("Invalid gamma_ramp passed.\n");
5138 return DDERR_INVALIDPARAMS;
5141 wined3d_mutex_lock();
5142 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5144 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5145 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
5147 else
5149 ERR("Not implemented for non-primary surfaces.\n");
5151 wined3d_mutex_unlock();
5153 return DD_OK;
5156 /**********************************************************
5157 * IDirectDrawGammaControl::SetGammaRamp
5159 * Sets the red, green and blue gamma ramps for
5161 * Params:
5162 * flags: Can be DDSGR_CALIBRATE to request calibration
5163 * gamma_ramp: Structure containing the new gamma ramp
5165 * Returns:
5166 * DD_OK on success
5167 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
5169 **********************************************************/
5170 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
5171 DWORD flags, DDGAMMARAMP *gamma_ramp)
5173 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
5175 TRACE("iface %p, flags %#lx, gamma_ramp %p.\n", iface, flags, gamma_ramp);
5177 if (!gamma_ramp)
5179 WARN("Invalid gamma_ramp passed.\n");
5180 return DDERR_INVALIDPARAMS;
5183 wined3d_mutex_lock();
5184 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5186 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
5187 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
5188 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
5190 else
5192 ERR("Not implemented for non-primary surfaces.\n");
5194 wined3d_mutex_unlock();
5196 return DD_OK;
5199 /*****************************************************************************
5200 * IDirect3DTexture2::PaletteChanged
5202 * Informs the texture about a palette change
5204 * Params:
5205 * start: Start index of the change
5206 * count: The number of changed entries
5208 * Returns
5209 * D3D_OK, because it's a stub
5211 *****************************************************************************/
5212 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
5214 FIXME("iface %p, start %lu, count %lu stub!\n", iface, start, count);
5216 return D3D_OK;
5219 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
5221 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5223 TRACE("iface %p, start %lu, count %lu.\n", iface, start, count);
5225 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
5228 /*****************************************************************************
5229 * IDirect3DTexture::Unload
5231 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
5234 * Returns:
5235 * DDERR_UNSUPPORTED
5237 *****************************************************************************/
5238 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
5240 WARN("iface %p. Not implemented.\n", iface);
5242 return DDERR_UNSUPPORTED;
5245 /*****************************************************************************
5246 * IDirect3DTexture2::GetHandle
5248 * Returns handle for the texture.
5250 * Params:
5251 * device: Device this handle is assigned to
5252 * handle: Address to store the handle at.
5254 * Returns:
5255 * D3D_OK
5257 *****************************************************************************/
5258 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
5259 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
5261 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
5262 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
5264 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5266 wined3d_mutex_lock();
5268 if (!surface->Handle)
5270 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
5271 if (h == DDRAW_INVALID_HANDLE)
5273 ERR("Failed to allocate a texture handle.\n");
5274 wined3d_mutex_unlock();
5275 return DDERR_OUTOFMEMORY;
5278 surface->Handle = h + 1;
5281 TRACE("Returning handle %08lx.\n", surface->Handle);
5282 *handle = surface->Handle;
5284 wined3d_mutex_unlock();
5286 return D3D_OK;
5289 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
5290 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
5292 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5293 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
5295 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5297 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
5298 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
5301 /*****************************************************************************
5302 * get_sub_mimaplevel
5304 * Helper function that returns the next mipmap level
5306 * tex_ptr: Surface of which to return the next level
5308 *****************************************************************************/
5309 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
5311 /* Now go down the mipmap chain to the next surface */
5312 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, {0} };
5313 IDirectDrawSurface7 *next_level;
5314 HRESULT hr;
5316 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
5317 if (FAILED(hr)) return NULL;
5319 ddraw_surface7_Release(next_level);
5321 return impl_from_IDirectDrawSurface7(next_level);
5324 /*****************************************************************************
5325 * IDirect3DTexture2::Load
5327 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5329 * This function isn't relayed to WineD3D because the whole interface is
5330 * implemented in DDraw only. For speed improvements an implementation which
5331 * takes OpenGL more into account could be placed into WineD3D.
5333 * Params:
5334 * src_texture: Address of the texture to load
5336 * Returns:
5337 * D3D_OK on success
5338 * D3DERR_TEXTURE_LOAD_FAILED.
5340 *****************************************************************************/
5341 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5343 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5344 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5345 struct wined3d_resource *dst_resource, *src_resource;
5346 HRESULT hr;
5348 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5350 if (src_surface == dst_surface)
5352 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5353 return D3D_OK;
5356 wined3d_mutex_lock();
5358 dst_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(dst_surface, DDRAW_SURFACE_WRITE));
5359 src_resource = wined3d_texture_get_resource(ddraw_surface_get_default_texture(src_surface, DDRAW_SURFACE_READ));
5361 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5362 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5363 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5365 ERR("Trying to load surfaces with different mip-map counts.\n");
5368 for (;;)
5370 struct ddraw_palette *dst_pal, *src_pal;
5371 DDSURFACEDESC *src_desc, *dst_desc;
5373 TRACE("Copying surface %p to surface %p.\n", src_surface, dst_surface);
5375 /* Suppress the ALLOCONLOAD flag */
5376 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5378 /* Get the palettes */
5379 dst_pal = dst_surface->palette;
5380 src_pal = src_surface->palette;
5382 if (src_pal)
5384 PALETTEENTRY palent[256];
5386 if (!dst_pal)
5388 wined3d_mutex_unlock();
5389 return DDERR_NOPALETTEATTACHED;
5391 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5392 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5395 /* Copy one surface on the other */
5396 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5397 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5399 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5401 /* Should also check for same pixel format, u1.lPitch, ... */
5402 ERR("Error in surface sizes.\n");
5403 wined3d_mutex_unlock();
5404 return D3DERR_TEXTURE_LOAD_FAILED;
5406 else
5408 struct wined3d_map_desc src_map_desc, dst_map_desc;
5410 /* Copy the src blit color key if the source has one, don't erase
5411 * the destination's ckey if the source has none */
5412 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5414 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5415 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5418 if (FAILED(hr = wined3d_resource_map(src_resource,
5419 src_surface->sub_resource_idx, &src_map_desc, NULL, WINED3D_MAP_READ)))
5421 ERR("Failed to lock source surface, hr %#lx.\n", hr);
5422 wined3d_mutex_unlock();
5423 return D3DERR_TEXTURE_LOAD_FAILED;
5426 if (FAILED(hr = wined3d_resource_map(dst_resource,
5427 dst_surface->sub_resource_idx, &dst_map_desc, NULL, WINED3D_MAP_WRITE)))
5429 ERR("Failed to lock destination surface, hr %#lx.\n", hr);
5430 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5431 wined3d_mutex_unlock();
5432 return D3DERR_TEXTURE_LOAD_FAILED;
5435 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5436 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5437 else
5438 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5440 wined3d_resource_unmap(dst_resource, dst_surface->sub_resource_idx);
5441 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5444 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5445 src_surface = get_sub_mimaplevel(src_surface);
5446 else
5447 src_surface = NULL;
5449 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5450 dst_surface = get_sub_mimaplevel(dst_surface);
5451 else
5452 dst_surface = NULL;
5454 if (!src_surface || !dst_surface)
5456 if (src_surface != dst_surface)
5457 ERR("Loading surface with different mipmap structure.\n");
5458 break;
5462 wined3d_mutex_unlock();
5464 return hr;
5467 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5469 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5470 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5472 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5474 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5475 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5478 /*****************************************************************************
5479 * The VTable
5480 *****************************************************************************/
5482 /* Some windowed mode wrappers expect this vtbl to be writable. */
5483 static struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5485 /* IUnknown */
5486 ddraw_surface7_QueryInterface,
5487 ddraw_surface7_AddRef,
5488 ddraw_surface7_Release,
5489 /* IDirectDrawSurface */
5490 ddraw_surface7_AddAttachedSurface,
5491 ddraw_surface7_AddOverlayDirtyRect,
5492 ddraw_surface7_Blt,
5493 ddraw_surface7_BltBatch,
5494 ddraw_surface7_BltFast,
5495 ddraw_surface7_DeleteAttachedSurface,
5496 ddraw_surface7_EnumAttachedSurfaces,
5497 ddraw_surface7_EnumOverlayZOrders,
5498 ddraw_surface7_Flip,
5499 ddraw_surface7_GetAttachedSurface,
5500 ddraw_surface7_GetBltStatus,
5501 ddraw_surface7_GetCaps,
5502 ddraw_surface7_GetClipper,
5503 ddraw_surface7_GetColorKey,
5504 ddraw_surface7_GetDC,
5505 ddraw_surface7_GetFlipStatus,
5506 ddraw_surface7_GetOverlayPosition,
5507 ddraw_surface7_GetPalette,
5508 ddraw_surface7_GetPixelFormat,
5509 ddraw_surface7_GetSurfaceDesc,
5510 ddraw_surface7_Initialize,
5511 ddraw_surface7_IsLost,
5512 ddraw_surface7_Lock,
5513 ddraw_surface7_ReleaseDC,
5514 ddraw_surface7_Restore,
5515 ddraw_surface7_SetClipper,
5516 ddraw_surface7_SetColorKey,
5517 ddraw_surface7_SetOverlayPosition,
5518 ddraw_surface7_SetPalette,
5519 ddraw_surface7_Unlock,
5520 ddraw_surface7_UpdateOverlay,
5521 ddraw_surface7_UpdateOverlayDisplay,
5522 ddraw_surface7_UpdateOverlayZOrder,
5523 /* IDirectDrawSurface2 */
5524 ddraw_surface7_GetDDInterface,
5525 ddraw_surface7_PageLock,
5526 ddraw_surface7_PageUnlock,
5527 /* IDirectDrawSurface3 */
5528 ddraw_surface7_SetSurfaceDesc,
5529 /* IDirectDrawSurface4 */
5530 ddraw_surface7_SetPrivateData,
5531 ddraw_surface7_GetPrivateData,
5532 ddraw_surface7_FreePrivateData,
5533 ddraw_surface7_GetUniquenessValue,
5534 ddraw_surface7_ChangeUniquenessValue,
5535 /* IDirectDrawSurface7 */
5536 ddraw_surface7_SetPriority,
5537 ddraw_surface7_GetPriority,
5538 ddraw_surface7_SetLOD,
5539 ddraw_surface7_GetLOD,
5542 /* Some windowed mode wrappers expect this vtbl to be writable. */
5543 static struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5545 /* IUnknown */
5546 ddraw_surface4_QueryInterface,
5547 ddraw_surface4_AddRef,
5548 ddraw_surface4_Release,
5549 /* IDirectDrawSurface */
5550 ddraw_surface4_AddAttachedSurface,
5551 ddraw_surface4_AddOverlayDirtyRect,
5552 ddraw_surface4_Blt,
5553 ddraw_surface4_BltBatch,
5554 ddraw_surface4_BltFast,
5555 ddraw_surface4_DeleteAttachedSurface,
5556 ddraw_surface4_EnumAttachedSurfaces,
5557 ddraw_surface4_EnumOverlayZOrders,
5558 ddraw_surface4_Flip,
5559 ddraw_surface4_GetAttachedSurface,
5560 ddraw_surface4_GetBltStatus,
5561 ddraw_surface4_GetCaps,
5562 ddraw_surface4_GetClipper,
5563 ddraw_surface4_GetColorKey,
5564 ddraw_surface4_GetDC,
5565 ddraw_surface4_GetFlipStatus,
5566 ddraw_surface4_GetOverlayPosition,
5567 ddraw_surface4_GetPalette,
5568 ddraw_surface4_GetPixelFormat,
5569 ddraw_surface4_GetSurfaceDesc,
5570 ddraw_surface4_Initialize,
5571 ddraw_surface4_IsLost,
5572 ddraw_surface4_Lock,
5573 ddraw_surface4_ReleaseDC,
5574 ddraw_surface4_Restore,
5575 ddraw_surface4_SetClipper,
5576 ddraw_surface4_SetColorKey,
5577 ddraw_surface4_SetOverlayPosition,
5578 ddraw_surface4_SetPalette,
5579 ddraw_surface4_Unlock,
5580 ddraw_surface4_UpdateOverlay,
5581 ddraw_surface4_UpdateOverlayDisplay,
5582 ddraw_surface4_UpdateOverlayZOrder,
5583 /* IDirectDrawSurface2 */
5584 ddraw_surface4_GetDDInterface,
5585 ddraw_surface4_PageLock,
5586 ddraw_surface4_PageUnlock,
5587 /* IDirectDrawSurface3 */
5588 ddraw_surface4_SetSurfaceDesc,
5589 /* IDirectDrawSurface4 */
5590 ddraw_surface4_SetPrivateData,
5591 ddraw_surface4_GetPrivateData,
5592 ddraw_surface4_FreePrivateData,
5593 ddraw_surface4_GetUniquenessValue,
5594 ddraw_surface4_ChangeUniquenessValue,
5597 /* Some windowed mode wrappers expect this vtbl to be writable. */
5598 static struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5600 /* IUnknown */
5601 ddraw_surface3_QueryInterface,
5602 ddraw_surface3_AddRef,
5603 ddraw_surface3_Release,
5604 /* IDirectDrawSurface */
5605 ddraw_surface3_AddAttachedSurface,
5606 ddraw_surface3_AddOverlayDirtyRect,
5607 ddraw_surface3_Blt,
5608 ddraw_surface3_BltBatch,
5609 ddraw_surface3_BltFast,
5610 ddraw_surface3_DeleteAttachedSurface,
5611 ddraw_surface3_EnumAttachedSurfaces,
5612 ddraw_surface3_EnumOverlayZOrders,
5613 ddraw_surface3_Flip,
5614 ddraw_surface3_GetAttachedSurface,
5615 ddraw_surface3_GetBltStatus,
5616 ddraw_surface3_GetCaps,
5617 ddraw_surface3_GetClipper,
5618 ddraw_surface3_GetColorKey,
5619 ddraw_surface3_GetDC,
5620 ddraw_surface3_GetFlipStatus,
5621 ddraw_surface3_GetOverlayPosition,
5622 ddraw_surface3_GetPalette,
5623 ddraw_surface3_GetPixelFormat,
5624 ddraw_surface3_GetSurfaceDesc,
5625 ddraw_surface3_Initialize,
5626 ddraw_surface3_IsLost,
5627 ddraw_surface3_Lock,
5628 ddraw_surface3_ReleaseDC,
5629 ddraw_surface3_Restore,
5630 ddraw_surface3_SetClipper,
5631 ddraw_surface3_SetColorKey,
5632 ddraw_surface3_SetOverlayPosition,
5633 ddraw_surface3_SetPalette,
5634 ddraw_surface3_Unlock,
5635 ddraw_surface3_UpdateOverlay,
5636 ddraw_surface3_UpdateOverlayDisplay,
5637 ddraw_surface3_UpdateOverlayZOrder,
5638 /* IDirectDrawSurface2 */
5639 ddraw_surface3_GetDDInterface,
5640 ddraw_surface3_PageLock,
5641 ddraw_surface3_PageUnlock,
5642 /* IDirectDrawSurface3 */
5643 ddraw_surface3_SetSurfaceDesc,
5646 /* Some windowed mode wrappers expect this vtbl to be writable. */
5647 static struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5649 /* IUnknown */
5650 ddraw_surface2_QueryInterface,
5651 ddraw_surface2_AddRef,
5652 ddraw_surface2_Release,
5653 /* IDirectDrawSurface */
5654 ddraw_surface2_AddAttachedSurface,
5655 ddraw_surface2_AddOverlayDirtyRect,
5656 ddraw_surface2_Blt,
5657 ddraw_surface2_BltBatch,
5658 ddraw_surface2_BltFast,
5659 ddraw_surface2_DeleteAttachedSurface,
5660 ddraw_surface2_EnumAttachedSurfaces,
5661 ddraw_surface2_EnumOverlayZOrders,
5662 ddraw_surface2_Flip,
5663 ddraw_surface2_GetAttachedSurface,
5664 ddraw_surface2_GetBltStatus,
5665 ddraw_surface2_GetCaps,
5666 ddraw_surface2_GetClipper,
5667 ddraw_surface2_GetColorKey,
5668 ddraw_surface2_GetDC,
5669 ddraw_surface2_GetFlipStatus,
5670 ddraw_surface2_GetOverlayPosition,
5671 ddraw_surface2_GetPalette,
5672 ddraw_surface2_GetPixelFormat,
5673 ddraw_surface2_GetSurfaceDesc,
5674 ddraw_surface2_Initialize,
5675 ddraw_surface2_IsLost,
5676 ddraw_surface2_Lock,
5677 ddraw_surface2_ReleaseDC,
5678 ddraw_surface2_Restore,
5679 ddraw_surface2_SetClipper,
5680 ddraw_surface2_SetColorKey,
5681 ddraw_surface2_SetOverlayPosition,
5682 ddraw_surface2_SetPalette,
5683 ddraw_surface2_Unlock,
5684 ddraw_surface2_UpdateOverlay,
5685 ddraw_surface2_UpdateOverlayDisplay,
5686 ddraw_surface2_UpdateOverlayZOrder,
5687 /* IDirectDrawSurface2 */
5688 ddraw_surface2_GetDDInterface,
5689 ddraw_surface2_PageLock,
5690 ddraw_surface2_PageUnlock,
5693 /* Bad Mojo Redux expects this vtbl to be writable. */
5694 static struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5696 /* IUnknown */
5697 ddraw_surface1_QueryInterface,
5698 ddraw_surface1_AddRef,
5699 ddraw_surface1_Release,
5700 /* IDirectDrawSurface */
5701 ddraw_surface1_AddAttachedSurface,
5702 ddraw_surface1_AddOverlayDirtyRect,
5703 ddraw_surface1_Blt,
5704 ddraw_surface1_BltBatch,
5705 ddraw_surface1_BltFast,
5706 ddraw_surface1_DeleteAttachedSurface,
5707 ddraw_surface1_EnumAttachedSurfaces,
5708 ddraw_surface1_EnumOverlayZOrders,
5709 ddraw_surface1_Flip,
5710 ddraw_surface1_GetAttachedSurface,
5711 ddraw_surface1_GetBltStatus,
5712 ddraw_surface1_GetCaps,
5713 ddraw_surface1_GetClipper,
5714 ddraw_surface1_GetColorKey,
5715 ddraw_surface1_GetDC,
5716 ddraw_surface1_GetFlipStatus,
5717 ddraw_surface1_GetOverlayPosition,
5718 ddraw_surface1_GetPalette,
5719 ddraw_surface1_GetPixelFormat,
5720 ddraw_surface1_GetSurfaceDesc,
5721 ddraw_surface1_Initialize,
5722 ddraw_surface1_IsLost,
5723 ddraw_surface1_Lock,
5724 ddraw_surface1_ReleaseDC,
5725 ddraw_surface1_Restore,
5726 ddraw_surface1_SetClipper,
5727 ddraw_surface1_SetColorKey,
5728 ddraw_surface1_SetOverlayPosition,
5729 ddraw_surface1_SetPalette,
5730 ddraw_surface1_Unlock,
5731 ddraw_surface1_UpdateOverlay,
5732 ddraw_surface1_UpdateOverlayDisplay,
5733 ddraw_surface1_UpdateOverlayZOrder,
5736 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5738 ddraw_gamma_control_QueryInterface,
5739 ddraw_gamma_control_AddRef,
5740 ddraw_gamma_control_Release,
5741 ddraw_gamma_control_GetGammaRamp,
5742 ddraw_gamma_control_SetGammaRamp,
5745 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5747 d3d_texture2_QueryInterface,
5748 d3d_texture2_AddRef,
5749 d3d_texture2_Release,
5750 d3d_texture2_GetHandle,
5751 d3d_texture2_PaletteChanged,
5752 d3d_texture2_Load,
5755 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5757 d3d_texture1_QueryInterface,
5758 d3d_texture1_AddRef,
5759 d3d_texture1_Release,
5760 d3d_texture1_Initialize,
5761 d3d_texture1_GetHandle,
5762 d3d_texture1_PaletteChanged,
5763 d3d_texture1_Load,
5764 d3d_texture1_Unload,
5767 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5768 * IDirectDrawSurface interface to ddraw methods. */
5769 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5771 if (!iface) return NULL;
5772 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5774 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5775 if (FAILED(hr))
5777 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5778 return NULL;
5780 IDirectDrawSurface7_Release(iface);
5782 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5785 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5787 if (!iface) return NULL;
5788 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5790 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5791 if (FAILED(hr))
5793 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5794 return NULL;
5796 IDirectDrawSurface4_Release(iface);
5798 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5801 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5803 if (!iface) return NULL;
5804 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5806 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5807 if (FAILED(hr))
5809 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5810 return NULL;
5812 IDirectDrawSurface3_Release(iface);
5814 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5817 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5819 if (!iface) return NULL;
5820 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5822 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5823 if (FAILED(hr))
5825 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5826 return NULL;
5828 IDirectDrawSurface2_Release(iface);
5830 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5833 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5835 if (!iface) return NULL;
5836 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5838 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5839 if (FAILED(hr))
5841 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5842 return NULL;
5844 IDirectDrawSurface_Release(iface);
5846 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5849 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5851 if (!iface) return NULL;
5852 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5853 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5856 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5858 if (!iface) return NULL;
5859 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5860 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5863 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5865 struct ddraw_surface *surface = parent;
5867 TRACE("surface %p.\n", surface);
5869 /* This shouldn't happen, ddraw_surface_release_iface() should prevent the
5870 * surface from being destroyed in this case. */
5871 if (surface->first_attached != surface)
5872 ERR("Surface is still attached to surface %p.\n", surface->first_attached);
5874 while (surface->next_attached)
5875 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5876 surface->next_attached, surface->next_attached->attached_iface)))
5877 ERR("DeleteAttachedSurface failed.\n");
5879 /* Having a texture handle set implies that the device still exists. */
5880 if (surface->Handle)
5881 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5883 /* Reduce the ddraw surface count. */
5884 list_remove(&surface->surface_list_entry);
5886 if (surface->clipper && ddraw_clipper_is_valid(surface->clipper))
5887 IDirectDrawClipper_Release(&surface->clipper->IDirectDrawClipper_iface);
5889 if (surface == surface->ddraw->primary)
5891 surface->ddraw->primary = NULL;
5892 surface->ddraw->gdi_surface = NULL;
5895 wined3d_private_store_cleanup(&surface->private_store);
5897 if (surface->draw_texture)
5898 wined3d_texture_decref(surface->wined3d_texture);
5900 heap_free(surface);
5903 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5905 ddraw_surface_wined3d_object_destroyed,
5908 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5910 struct ddraw_texture *texture = parent;
5912 TRACE("texture %p, texture_memory %p.\n", texture, texture->texture_memory);
5914 heap_free(texture->texture_memory);
5915 heap_free(parent);
5918 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5920 ddraw_texture_wined3d_object_destroyed,
5923 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5925 return DD_OK;
5928 static HRESULT ddraw_surface_reserve_memory(struct wined3d_texture *wined3d_texture,
5929 unsigned int sub_resource_count)
5931 static const unsigned int extra_size = 0x10000;
5933 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
5934 struct wined3d_resource_desc resource_desc;
5935 struct wined3d_sub_resource_desc desc;
5936 unsigned int pitch, slice_pitch;
5937 HRESULT hr = WINED3D_OK;
5938 unsigned int offset, i;
5940 wined3d_resource_get_desc(wined3d_texture_get_resource(wined3d_texture), &resource_desc);
5941 if (!(texture->texture_memory = heap_alloc_zero(resource_desc.size + extra_size)))
5943 ERR("Out of memory.\n");
5944 return E_OUTOFMEMORY;
5946 TRACE("texture->texture_memory %p.\n", texture->texture_memory);
5948 offset = 0;
5949 for (i = 0; i < sub_resource_count; ++i)
5951 if (FAILED(hr = wined3d_texture_get_sub_resource_desc(wined3d_texture, i, &desc)))
5953 ERR("Subresource %u not found.\n", i);
5954 heap_free(texture->texture_memory);
5955 texture->texture_memory = NULL;
5956 return hr;
5958 wined3d_texture_get_pitch(wined3d_texture, i, &pitch, &slice_pitch);
5960 if (FAILED(hr = wined3d_texture_update_desc(wined3d_texture, i,
5961 desc.width, desc.height, resource_desc.format,
5962 desc.multisample_type, desc.multisample_quality,
5963 (BYTE *)texture->texture_memory + offset, pitch)))
5965 heap_free(texture->texture_memory);
5966 texture->texture_memory = NULL;
5967 break;
5969 offset += desc.size;
5971 return hr;
5974 static HRESULT ddraw_surface_create_wined3d_texture(DDSURFACEDESC2 *desc, struct wined3d_device *wined3d_device,
5975 const struct wined3d_resource_desc *wined3d_desc, unsigned int layers, unsigned int levels,
5976 struct ddraw_texture *texture, struct wined3d_texture **wined3d_texture)
5978 struct wined3d_resource_desc draw_texture_desc;
5979 struct wined3d_texture *draw_texture;
5980 struct ddraw_surface *parent;
5981 unsigned int bind_flags;
5982 unsigned int i;
5983 HRESULT hr;
5985 bind_flags = 0;
5986 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5987 || (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5988 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
5990 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5991 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
5992 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5993 bind_flags |= WINED3D_BIND_RENDER_TARGET;
5995 if (!bind_flags || (wined3d_desc->access & WINED3D_RESOURCE_ACCESS_GPU && !(bind_flags & ~wined3d_desc->bind_flags)))
5996 goto no_draw_texture;
5998 draw_texture_desc = *wined3d_desc;
5999 draw_texture_desc.bind_flags = bind_flags;
6000 draw_texture_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
6002 if (FAILED(hr = wined3d_texture_create(wined3d_device, &draw_texture_desc, layers,
6003 levels, 0, NULL, texture, &ddraw_texture_wined3d_parent_ops, &draw_texture)))
6005 WARN("Failed to create draw texture, hr %#lx.\n", hr);
6006 goto no_draw_texture;
6008 wined3d_texture_decref(draw_texture);
6010 /* Some applications assume surfaces will always be mapped at the same
6011 * address. Some of those also assume that this address is valid even when
6012 * the surface isn't mapped, and that updates done this way will be
6013 * visible on the screen. The game Nox is such an application,
6014 * Commandos: Behind Enemy Lines is another. Setting
6015 * WINED3D_TEXTURE_CREATE_GET_DC_LENIENT will ensure this. */
6016 if (FAILED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6017 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, NULL, &ddraw_null_wined3d_parent_ops,
6018 wined3d_texture)))
6020 parent = wined3d_texture_get_sub_resource_parent(draw_texture, 0);
6021 if (texture->version == 7)
6022 IDirectDrawSurface7_Release(&parent->IDirectDrawSurface7_iface);
6023 else if (texture->version == 4)
6024 IDirectDrawSurface4_Release(&parent->IDirectDrawSurface4_iface);
6025 else
6026 IDirectDrawSurface_Release(&parent->IDirectDrawSurface_iface);
6027 return hr;
6029 wined3d_resource_set_parent(wined3d_texture_get_resource(*wined3d_texture), texture);
6030 for (i = 0; i < layers * levels; ++i)
6032 parent = wined3d_texture_get_sub_resource_parent(draw_texture, i);
6033 assert(parent->wined3d_texture == draw_texture);
6034 parent->draw_texture = draw_texture;
6035 parent->wined3d_texture = *wined3d_texture;
6036 wined3d_texture_set_sub_resource_parent(*wined3d_texture, i, parent);
6037 wined3d_texture_incref(*wined3d_texture);
6039 wined3d_texture_decref(*wined3d_texture);
6040 TRACE("Surface %p, created draw_texture %p, wined3d_texture %p.\n",
6041 wined3d_texture_get_sub_resource_parent(draw_texture, 0), draw_texture, wined3d_texture);
6042 return D3D_OK;
6044 no_draw_texture:
6045 if (SUCCEEDED(hr = wined3d_texture_create(wined3d_device, wined3d_desc, layers, levels,
6046 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, texture, &ddraw_texture_wined3d_parent_ops,
6047 wined3d_texture)))
6048 wined3d_texture_decref(*wined3d_texture);
6049 return hr;
6052 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
6053 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
6055 struct wined3d_sub_resource_desc wined3d_mip_desc;
6056 struct ddraw_surface *root, *mip, **attach;
6057 struct wined3d_resource_desc wined3d_desc;
6058 DDPIXELFORMAT wined3d_display_mode_format;
6059 struct wined3d_texture *wined3d_texture;
6060 struct wined3d_display_mode mode;
6061 DDSURFACEDESC2 *desc, *mip_desc;
6062 struct ddraw_texture *texture;
6063 BOOL sysmem_fallback = FALSE;
6064 unsigned int layers = 1;
6065 unsigned int pitch = 0;
6066 BOOL reserve_memory;
6067 UINT levels, i, j;
6068 HRESULT hr;
6070 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
6071 ddraw, surface_desc, surface, outer_unknown, version);
6072 if (TRACE_ON(ddraw))
6074 TRACE("Requesting surface desc:\n");
6075 DDRAW_dump_surface_desc(surface_desc);
6078 if (outer_unknown)
6079 return CLASS_E_NOAGGREGATION;
6081 if (!surface)
6082 return E_POINTER;
6084 if (!(texture = heap_alloc(sizeof(*texture))))
6085 return E_OUTOFMEMORY;
6087 texture->texture_memory = NULL;
6088 texture->version = version;
6089 texture->surface_desc = *surface_desc;
6090 desc = &texture->surface_desc;
6092 /* Ensure DDSD_CAPS is always set. */
6093 desc->dwFlags |= DDSD_CAPS;
6095 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6097 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->u5.dwBackBufferCount)
6099 WARN("Tried to create a flippable surface without any back buffers.\n");
6100 heap_free(texture);
6101 return DDERR_INVALIDCAPS;
6104 if (!(desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX))
6106 WARN("Tried to create a flippable surface without DDSCAPS_COMPLEX.\n");
6107 heap_free(texture);
6108 return DDERR_INVALIDCAPS;
6111 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6113 WARN("Tried to create a flippable cubemap.\n");
6114 heap_free(texture);
6115 return DDERR_INVALIDPARAMS;
6118 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6120 FIXME("Flippable textures not implemented.\n");
6121 heap_free(texture);
6122 return DDERR_INVALIDCAPS;
6125 else
6127 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6129 WARN("Tried to specify a back buffer count for a non-flippable surface.\n");
6130 hr = desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP ? DDERR_INVALIDPARAMS : DDERR_INVALIDCAPS;
6131 heap_free(texture);
6132 return hr;
6136 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6138 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6140 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
6141 heap_free(texture);
6142 return DDERR_INVALIDCAPS;
6145 if ((desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX) && !(desc->ddsCaps.dwCaps & DDSCAPS_FLIP))
6147 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
6148 heap_free(texture);
6149 return DDERR_INVALIDCAPS;
6152 if ((desc->ddsCaps.dwCaps & DDSCAPS_FLIP) && !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
6154 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
6155 heap_free(texture);
6156 return DDERR_NOEXCLUSIVEMODE;
6160 /* This is a special case in ddrawex, but not allowed in ddraw. */
6161 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6162 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6164 WARN("Tried to create a surface in both system and video memory.\n");
6165 heap_free(texture);
6166 return DDERR_INVALIDCAPS;
6169 if ((desc->ddsCaps.dwCaps & (DDSCAPS_ALLOCONLOAD | DDSCAPS_MIPMAP))
6170 && !(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6172 WARN("Caps %#lx require DDSCAPS_TEXTURE.\n", desc->ddsCaps.dwCaps);
6173 heap_free(texture);
6174 return DDERR_INVALIDCAPS;
6177 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
6178 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
6180 WARN("Cube map faces requested without cube map flag.\n");
6181 heap_free(texture);
6182 return DDERR_INVALIDCAPS;
6185 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6186 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
6188 WARN("Cube map without faces requested.\n");
6189 heap_free(texture);
6190 return DDERR_INVALIDPARAMS;
6193 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6194 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
6195 FIXME("Partial cube maps not implemented.\n");
6197 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6199 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6201 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
6202 heap_free(texture);
6203 return DDERR_INVALIDCAPS;
6205 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
6207 WARN("DDSCAPS2_TEXTUREMANAGE used with DDSCAPS_VIDEOMEMORY "
6208 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
6209 heap_free(texture);
6210 return DDERR_INVALIDCAPS;
6214 if (desc->ddsCaps.dwCaps & DDSCAPS_WRITEONLY
6215 && !(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6217 WARN("DDSCAPS_WRITEONLY used without DDSCAPS2_TEXTUREMANAGE, returning DDERR_INVALIDCAPS.\n");
6218 heap_free(texture);
6219 return DDERR_INVALIDCAPS;
6222 if (FAILED(hr = wined3d_output_get_display_mode(ddraw->wined3d_output, &mode, NULL)))
6224 ERR("Failed to get display mode, hr %#lx.\n", hr);
6225 heap_free(texture);
6226 return hr_ddraw_from_wined3d(hr);
6229 wined3d_display_mode_format.dwSize = sizeof(wined3d_display_mode_format);
6230 ddrawformat_from_wined3dformat(&wined3d_display_mode_format, mode.format_id);
6232 /* No pixelformat given? Use the current screen format. */
6233 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
6235 desc->dwFlags |= DDSD_PIXELFORMAT;
6236 desc->u4.ddpfPixelFormat = wined3d_display_mode_format;
6239 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6240 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
6241 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
6243 WARN("Unsupported / unknown pixelformat.\n");
6244 heap_free(texture);
6245 return DDERR_INVALIDPIXELFORMAT;
6248 /* No width or no height? Use the screen size. */
6249 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
6251 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
6253 WARN("No width / height specified.\n");
6254 heap_free(texture);
6255 return DDERR_INVALIDPARAMS;
6258 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
6259 desc->dwWidth = mode.width;
6260 desc->dwHeight = mode.height;
6263 if (!desc->dwWidth || !desc->dwHeight)
6265 heap_free(texture);
6266 return DDERR_INVALIDPARAMS;
6269 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
6270 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
6272 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6274 /* The first surface is a front buffer, the back buffers are created
6275 * afterwards. */
6276 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
6277 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
6279 struct wined3d_swapchain_desc swapchain_desc;
6281 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
6282 swapchain_desc.backbuffer_width = mode.width;
6283 swapchain_desc.backbuffer_height = mode.height;
6284 swapchain_desc.backbuffer_format = mode.format_id;
6286 if (ddraw->d3ddevice)
6288 if (ddraw->d3ddevice->recording)
6289 wined3d_stateblock_decref(ddraw->d3ddevice->recording);
6290 ddraw->d3ddevice->recording = NULL;
6291 ddraw->d3ddevice->update_state = ddraw->d3ddevice->state;
6293 wined3d_stateblock_reset(ddraw->state);
6295 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
6296 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
6298 ERR("Failed to reset device.\n");
6299 heap_free(texture);
6300 return hr_ddraw_from_wined3d(hr);
6303 wined3d_stateblock_set_render_state(ddraw->state, WINED3D_RS_ZENABLE,
6304 !!swapchain_desc.enable_auto_depth_stencil);
6308 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
6309 wined3d_desc.multisample_quality = 0;
6310 wined3d_desc.usage = 0;
6311 wined3d_desc.bind_flags = 0;
6312 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6313 wined3d_desc.width = desc->dwWidth;
6314 wined3d_desc.height = desc->dwHeight;
6315 wined3d_desc.depth = 1;
6316 wined3d_desc.size = 0;
6318 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
6320 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
6321 /* Do not fail surface creation, only fail 3D device creation. */
6324 /* Mipmap count fixes */
6325 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6327 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
6329 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
6331 /* Mipmap count is given, should not be 0. */
6332 if (!desc->u2.dwMipMapCount)
6334 heap_free(texture);
6335 return DDERR_INVALIDPARAMS;
6338 else
6340 /* Undocumented feature: Create sublevels until either the
6341 * width or the height is 1. */
6342 if (version == 7)
6343 desc->u2.dwMipMapCount = wined3d_log2i(max(desc->dwWidth, desc->dwHeight)) + 1;
6344 else
6345 desc->u2.dwMipMapCount = wined3d_log2i(min(desc->dwWidth, desc->dwHeight)) + 1;
6348 else
6350 desc->u2.dwMipMapCount = 1;
6353 desc->dwFlags |= DDSD_MIPMAPCOUNT;
6354 levels = desc->u2.dwMipMapCount;
6356 else
6358 levels = 1;
6361 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
6363 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
6365 unsigned int bind_flags = 0;
6366 DWORD usage = 0;
6368 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6370 usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6371 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6373 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6375 bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6378 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6379 bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6380 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6381 bind_flags |= WINED3D_BIND_RENDER_TARGET;
6383 if (!(ddraw->flags & DDRAW_NO3D) && SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d,
6384 ddraw->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL, mode.format_id,
6385 usage, bind_flags, WINED3D_RTYPE_TEXTURE_2D, wined3d_desc.format)))
6387 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
6389 else
6391 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6392 sysmem_fallback = TRUE;
6395 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
6397 /* Tests show surfaces without memory flags get these flags added
6398 * right after creation. */
6399 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
6403 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6404 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
6406 WARN("System memory overlays are not allowed.\n");
6407 heap_free(texture);
6408 return DDERR_NOOVERLAYHW;
6411 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
6413 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_CPU
6414 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6416 else
6418 if (!(ddraw->flags & DDRAW_NO3D))
6420 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6421 wined3d_desc.bind_flags |= WINED3D_BIND_SHADER_RESOURCE;
6422 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6423 wined3d_desc.bind_flags |= WINED3D_BIND_DEPTH_STENCIL;
6424 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6425 wined3d_desc.bind_flags |= WINED3D_BIND_RENDER_TARGET;
6428 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6430 wined3d_desc.bind_flags &= ~WINED3D_BIND_RENDER_TARGET;
6431 wined3d_desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU
6432 | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
6433 /* Managed textures have the system memory flag set. */
6434 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6436 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
6438 /* Videomemory adds localvidmem. This is mutually exclusive with
6439 * systemmemory and texturemanage. */
6440 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
6441 /* Dynamic resources can't be written by the GPU. */
6442 if (!(wined3d_desc.bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)))
6443 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
6447 if (desc->dwFlags & DDSD_LPSURFACE)
6449 if (wined3d_desc.access & WINED3D_RESOURCE_ACCESS_GPU)
6451 WARN("User memory surfaces should not be GPU accessible.\n");
6452 heap_free(texture);
6453 return DDERR_INVALIDCAPS;
6456 if (version < 4)
6458 WARN("User memory surfaces not supported before version 4.\n");
6459 heap_free(texture);
6460 return DDERR_INVALIDPARAMS;
6463 if (!desc->lpSurface)
6465 WARN("NULL surface memory pointer specified.\n");
6466 heap_free(texture);
6467 return DDERR_INVALIDPARAMS;
6470 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6472 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
6474 WARN("Pitch specified on a compressed user memory surface.\n");
6475 heap_free(texture);
6476 return DDERR_INVALIDPARAMS;
6479 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
6481 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
6482 heap_free(texture);
6483 return DDERR_INVALIDPARAMS;
6486 if ((desc->dwFlags & DDSD_LINEARSIZE)
6487 && desc->u1.dwLinearSize < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6488 wined3d_desc.format, wined3d_desc.width) * ((desc->dwHeight + 3) / 4))
6490 WARN("Invalid linear size %lu specified.\n", desc->u1.dwLinearSize);
6491 heap_free(texture);
6492 return DDERR_INVALIDPARAMS;
6495 else
6497 if (!(desc->dwFlags & DDSD_PITCH))
6499 WARN("User memory surfaces should explicitly specify the pitch.\n");
6500 heap_free(texture);
6501 return DDERR_INVALIDPARAMS;
6504 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d_adapter,
6505 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
6507 WARN("Invalid pitch %lu specified.\n", desc->u1.lPitch);
6508 heap_free(texture);
6509 return DDERR_INVALIDPARAMS;
6512 pitch = desc->u1.lPitch;
6516 if (((desc->dwFlags & DDSD_CKDESTOVERLAY)
6517 && desc->u3.ddckCKDestOverlay.dwColorSpaceLowValue != desc->u3.ddckCKDestOverlay.dwColorSpaceHighValue)
6518 || ((desc->dwFlags & DDSD_CKDESTBLT)
6519 && desc->ddckCKDestBlt.dwColorSpaceLowValue != desc->ddckCKDestBlt.dwColorSpaceHighValue)
6520 || ((desc->dwFlags & DDSD_CKSRCOVERLAY)
6521 && desc->ddckCKSrcOverlay.dwColorSpaceLowValue != desc->ddckCKSrcOverlay.dwColorSpaceHighValue)
6522 || ((desc->dwFlags & DDSD_CKSRCBLT)
6523 && desc->ddckCKSrcBlt.dwColorSpaceLowValue != desc->ddckCKSrcBlt.dwColorSpaceHighValue))
6525 WARN("Range color keys not supported, returning DDERR_NOCOLORKEYHW.\n");
6526 heap_free(texture);
6527 return DDERR_NOCOLORKEYHW;
6530 if ((ddraw->flags & DDRAW_NO3D) && (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
6532 WARN("Video memory surfaces not supported without 3D support.\n");
6533 heap_free(texture);
6534 return DDERR_NODIRECTDRAWHW;
6537 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
6538 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
6540 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
6541 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
6543 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6545 wined3d_desc.usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6546 layers = 6;
6549 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, layers, levels,
6550 texture, &wined3d_texture)))
6552 WARN("Failed to create wined3d texture, hr %#lx.\n", hr);
6553 heap_free(texture);
6554 return hr_ddraw_from_wined3d(hr);
6557 root = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6559 root->is_complex_root = TRUE;
6560 root->sysmem_fallback = sysmem_fallback;
6561 texture->root = root;
6562 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6564 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6565 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTOVERLAY,
6566 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6567 if (desc->dwFlags & DDSD_CKDESTBLT)
6568 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_DESTBLT,
6569 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6570 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6571 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCOVERLAY,
6572 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6573 if (desc->dwFlags & DDSD_CKSRCBLT)
6574 ddraw_surface_set_wined3d_textures_colour_key(root, DDCKEY_SRCBLT,
6575 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6577 for (i = 0; i < layers; ++i)
6579 attach = &root->complex_array[layers - 1 - i];
6581 for (j = 0; j < levels; ++j)
6583 mip = wined3d_texture_get_sub_resource_parent(wined3d_texture, i * levels + j);
6584 mip->sysmem_fallback = sysmem_fallback;
6585 mip_desc = &mip->surface_desc;
6586 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
6587 mip_desc->u2.dwMipMapCount = levels - j;
6589 if (j)
6591 wined3d_texture_get_sub_resource_desc(wined3d_texture, i * levels + j, &wined3d_mip_desc);
6592 mip_desc->dwWidth = wined3d_mip_desc.width;
6593 mip_desc->dwHeight = wined3d_mip_desc.height;
6595 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6597 else
6599 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6602 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6604 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6606 switch (i)
6608 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6609 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6610 break;
6611 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6612 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6613 break;
6614 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6615 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6616 break;
6617 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6618 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6619 break;
6620 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6621 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6622 break;
6623 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6624 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6625 break;
6630 if (mip == root)
6631 continue;
6633 *attach = mip;
6634 attach = &mip->complex_array[0];
6638 if ((desc->dwFlags & DDSD_LPSURFACE) && FAILED(hr = wined3d_texture_update_desc(wined3d_texture, 0,
6639 wined3d_desc.width, wined3d_desc.height, wined3d_desc.format,
6640 WINED3D_MULTISAMPLE_NONE, 0, desc->lpSurface, pitch)))
6642 ERR("Failed to set surface memory, hr %#lx.\n", hr);
6643 goto fail;
6646 reserve_memory = !(desc->dwFlags & DDSD_LPSURFACE)
6647 && desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY
6648 && wined3d_display_mode_format.u1.dwRGBBitCount <= 16;
6650 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture, layers * levels)))
6652 ERR("Failed to reserve surface memory, hr %#lx.\n", hr);
6653 goto fail;
6656 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6658 unsigned int count = desc->u5.dwBackBufferCount;
6659 struct ddraw_surface *last = root;
6661 attach = &last->complex_array[0];
6662 for (i = 0; i < count; ++i)
6664 if (!(texture = heap_alloc(sizeof(*texture))))
6666 hr = E_OUTOFMEMORY;
6667 goto fail;
6670 texture->texture_memory = NULL;
6671 texture->version = version;
6672 texture->surface_desc = root->surface_desc;
6673 desc = &texture->surface_desc;
6675 /* Only one surface in the flipping chain is a back buffer, one is
6676 * a front buffer, the others are just flippable surfaces. */
6677 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6678 | DDSCAPS_BACKBUFFER);
6679 if (!i)
6680 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6681 desc->u5.dwBackBufferCount = 0;
6683 if (FAILED(hr = ddraw_surface_create_wined3d_texture(desc, ddraw->wined3d_device, &wined3d_desc, 1, 1,
6684 texture, &wined3d_texture)))
6686 heap_free(texture);
6687 hr = hr_ddraw_from_wined3d(hr);
6688 goto fail;
6691 last = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6692 last->sysmem_fallback = sysmem_fallback;
6693 texture->root = last;
6694 wined3d_device_incref(texture->wined3d_device = ddraw->wined3d_device);
6696 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6697 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6698 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6699 if (desc->dwFlags & DDSD_CKDESTBLT)
6700 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6701 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6702 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6703 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6704 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6705 if (desc->dwFlags & DDSD_CKSRCBLT)
6706 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6707 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6709 if (reserve_memory && FAILED(hr = ddraw_surface_reserve_memory(wined3d_texture, 1)))
6711 hr = hr_ddraw_from_wined3d(hr);
6712 goto fail;
6714 *attach = last;
6715 attach = &last->complex_array[0];
6717 *attach = root;
6720 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6722 ddraw->primary = root;
6723 ddraw->gdi_surface = root->wined3d_texture;
6725 *surface = root;
6727 return DD_OK;
6729 fail:
6730 if (version == 7)
6731 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6732 else if (version == 4)
6733 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6734 else
6735 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6737 return hr;
6740 void ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw,
6741 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
6742 const struct wined3d_parent_ops **parent_ops)
6744 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
6745 unsigned int texture_level, row_pitch, slice_pitch;
6746 DDSURFACEDESC2 *desc = &surface->surface_desc;
6747 unsigned int version = texture->version;
6749 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6750 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6751 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6752 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6753 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6754 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6755 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6756 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6757 surface->iface_count = 1;
6758 surface->version = version;
6759 surface->ddraw = ddraw;
6761 if (version == 7)
6763 surface->ref7 = 1;
6764 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6766 else if (version == 4)
6768 surface->ref4 = 1;
6769 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6771 else
6773 surface->ref1 = 1;
6774 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6777 *desc = texture->surface_desc;
6778 surface->first_attached = surface;
6780 texture_level = desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP ? sub_resource_idx % desc->u2.dwMipMapCount : 0;
6781 wined3d_texture_get_pitch(wined3d_texture, texture_level, &row_pitch, &slice_pitch);
6782 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6784 if (desc->dwFlags & DDSD_LPSURFACE)
6785 desc->u1.dwLinearSize = ~0u;
6786 else
6787 desc->u1.dwLinearSize = slice_pitch;
6788 desc->dwFlags |= DDSD_LINEARSIZE;
6789 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6791 else
6793 if (!(desc->dwFlags & DDSD_LPSURFACE))
6794 desc->u1.lPitch = row_pitch;
6795 desc->dwFlags |= DDSD_PITCH;
6796 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6798 desc->lpSurface = NULL;
6800 wined3d_texture_incref(surface->wined3d_texture = wined3d_texture);
6801 surface->sub_resource_idx = sub_resource_idx;
6802 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6803 surface->texture_location = DDRAW_SURFACE_LOCATION_DEFAULT;
6805 wined3d_private_store_init(&surface->private_store);
6808 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
6810 struct ddraw_surface *surface = parent;
6812 /* If the surface reference count drops to zero, we release our reference
6813 * to the view, but don't clear the pointer yet, in case e.g. a
6814 * GetRenderTarget() call brings the surface back before the view is
6815 * actually destroyed. When the view is destroyed, we need to clear the
6816 * pointer, or a subsequent surface AddRef() would reference it again.
6818 * This is safe because as long as the view still has a reference to the
6819 * texture, the surface is also still alive, and we're called before the
6820 * view releases that reference. */
6821 surface->wined3d_rtv = NULL;
6824 static const struct wined3d_parent_ops ddraw_view_wined3d_parent_ops =
6826 view_wined3d_object_destroyed,
6829 struct wined3d_rendertarget_view *ddraw_surface_get_rendertarget_view(struct ddraw_surface *surface)
6831 struct wined3d_texture *wined3d_texture;
6832 HRESULT hr;
6834 if (surface->wined3d_rtv)
6835 return surface->wined3d_rtv;
6837 wined3d_texture = surface->draw_texture ? surface->draw_texture : surface->wined3d_texture;
6838 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(wined3d_texture,
6839 surface->sub_resource_idx, surface, &ddraw_view_wined3d_parent_ops, &surface->wined3d_rtv)))
6841 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
6842 return NULL;
6845 return surface->wined3d_rtv;