ddraw: Allow DDSCAPS_FLIP without DDSCAPS_PRIMARYSURFACE.
[wine.git] / dlls / ddraw / surface.c
bloba1bbced33d4b477c8a8ec307630a319cc6242a84
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 "config.h"
25 #include "wine/port.h"
27 #include "ddraw_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
31 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface);
32 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface);
34 static inline struct ddraw_surface *impl_from_IDirectDrawGammaControl(IDirectDrawGammaControl *iface)
36 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawGammaControl_iface);
39 /* This is slow, of course. Also, in case of locks, we can't prevent other
40 * applications from drawing to the screen while we've locked the frontbuffer.
41 * We'd like to do this in wined3d instead, but for that to work wined3d needs
42 * to support windowless rendering first. */
43 HRESULT ddraw_surface_update_frontbuffer(struct ddraw_surface *surface, const RECT *rect, BOOL read)
45 HDC surface_dc, screen_dc;
46 int x, y, w, h;
47 HRESULT hr;
48 BOOL ret;
49 RECT r;
51 if (!rect)
53 SetRect(&r, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
54 rect = &r;
57 x = rect->left;
58 y = rect->top;
59 w = rect->right - rect->left;
60 h = rect->bottom - rect->top;
62 if (w <= 0 || h <= 0)
63 return DD_OK;
65 if (surface->ddraw->swapchain_window)
67 /* Nothing to do, we control the frontbuffer, or at least the parts we
68 * care about. */
69 if (read)
70 return DD_OK;
72 return wined3d_texture_blt(surface->ddraw->wined3d_frontbuffer, 0, rect,
73 surface->wined3d_texture, surface->sub_resource_idx, rect, 0, NULL, WINED3D_TEXF_POINT);
76 if (FAILED(hr = wined3d_texture_get_dc(surface->wined3d_texture, surface->sub_resource_idx, &surface_dc)))
78 ERR("Failed to get surface DC, hr %#x.\n", hr);
79 return hr;
81 if (surface->palette)
82 wined3d_palette_apply_to_dc(surface->palette->wineD3DPalette, surface_dc);
84 if (!(screen_dc = GetDC(NULL)))
86 wined3d_texture_release_dc(surface->wined3d_texture, surface->sub_resource_idx, surface_dc);
87 ERR("Failed to get screen DC.\n");
88 return E_FAIL;
91 if (read)
92 ret = BitBlt(surface_dc, x, y, w, h,
93 screen_dc, x, y, SRCCOPY);
94 else
95 ret = BitBlt(screen_dc, x, y, w, h,
96 surface_dc, x, y, SRCCOPY);
98 ReleaseDC(NULL, screen_dc);
99 wined3d_texture_release_dc(surface->wined3d_texture, surface->sub_resource_idx, surface_dc);
101 if (!ret)
103 ERR("Failed to blit to/from screen.\n");
104 return E_FAIL;
107 return DD_OK;
110 /*****************************************************************************
111 * IUnknown parts follow
112 *****************************************************************************/
114 /*****************************************************************************
115 * IDirectDrawSurface7::QueryInterface
117 * A normal QueryInterface implementation. For QueryInterface rules
118 * see ddraw.c, IDirectDraw7::QueryInterface. This method
119 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
120 * in all versions, the IDirectDrawGammaControl interface and it can
121 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
123 * Params:
124 * riid: The interface id queried for
125 * obj: Address to write the pointer to
127 * Returns:
128 * S_OK on success
129 * E_NOINTERFACE if the requested interface wasn't found
131 *****************************************************************************/
132 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
134 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
136 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
138 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
139 *obj = NULL;
141 if(!riid)
142 return DDERR_INVALIDPARAMS;
144 if (IsEqualGUID(riid, &IID_IDirectDrawSurface7))
146 IDirectDrawSurface7_AddRef(iface);
147 *obj = iface;
148 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
149 return S_OK;
152 if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
154 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
155 *obj = &This->IDirectDrawSurface4_iface;
156 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
157 return S_OK;
160 if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
162 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
163 *obj = &This->IDirectDrawSurface3_iface;
164 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
165 return S_OK;
168 if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
170 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
171 *obj = &This->IDirectDrawSurface2_iface;
172 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
173 return S_OK;
176 if (IsEqualGUID(riid, &IID_IDirectDrawSurface)
177 || IsEqualGUID(riid, &IID_IUnknown))
179 IDirectDrawSurface_AddRef(&This->IDirectDrawSurface_iface);
180 *obj = &This->IDirectDrawSurface_iface;
181 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
182 return S_OK;
185 if (IsEqualGUID(riid, &IID_IDirectDrawGammaControl))
187 IDirectDrawGammaControl_AddRef(&This->IDirectDrawGammaControl_iface);
188 *obj = &This->IDirectDrawGammaControl_iface;
189 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
190 return S_OK;
193 if (IsEqualGUID(riid, &IID_IDirectDrawColorControl))
195 WARN("Color control not implemented.\n");
196 *obj = NULL;
197 return E_NOINTERFACE;
200 if (This->version != 7)
202 if (IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D)
203 || IsEqualGUID(riid, &IID_IDirect3DHALDevice)
204 || IsEqualGUID(riid, &IID_IDirect3DRGBDevice))
206 wined3d_mutex_lock();
207 if (!This->device1)
209 HRESULT hr;
211 if (FAILED(hr = d3d_device_create(This->ddraw, This, (IUnknown *)&This->IDirectDrawSurface_iface,
212 1, &This->device1, (IUnknown *)&This->IDirectDrawSurface_iface)))
214 This->device1 = NULL;
215 wined3d_mutex_unlock();
216 WARN("Failed to create device, hr %#x.\n", hr);
217 return hr;
220 wined3d_mutex_unlock();
222 IDirect3DDevice_AddRef(&This->device1->IDirect3DDevice_iface);
223 *obj = &This->device1->IDirect3DDevice_iface;
224 return S_OK;
227 if (IsEqualGUID(&IID_IDirect3DTexture2, riid))
229 IDirect3DTexture2_AddRef(&This->IDirect3DTexture2_iface);
230 *obj = &This->IDirect3DTexture2_iface;
231 return S_OK;
234 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
236 IDirect3DTexture2_AddRef(&This->IDirect3DTexture_iface);
237 *obj = &This->IDirect3DTexture_iface;
238 return S_OK;
242 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
244 if (This->version != 7)
245 return E_INVALIDARG;
247 return E_NOINTERFACE;
250 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
252 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
254 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
256 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
259 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
261 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
263 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
265 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
268 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
270 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
272 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
274 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
277 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
279 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
281 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
283 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
286 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
287 REFIID riid, void **object)
289 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
291 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
293 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
296 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
298 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
300 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
302 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
305 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
307 struct ddraw_surface *surface = impl_from_IDirect3DTexture(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 void ddraw_surface_add_iface(struct ddraw_surface *surface)
316 ULONG iface_count = InterlockedIncrement(&surface->iface_count);
317 TRACE("%p increasing iface count to %u.\n", surface, iface_count);
319 if (iface_count == 1)
321 if (surface->ifaceToRelease)
322 IUnknown_AddRef(surface->ifaceToRelease);
323 wined3d_mutex_lock();
324 if (surface->wined3d_rtv)
325 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
326 wined3d_texture_incref(surface->wined3d_texture);
327 wined3d_mutex_unlock();
331 /*****************************************************************************
332 * IDirectDrawSurface7::AddRef
334 * A normal addref implementation
336 * Returns:
337 * The new refcount
339 *****************************************************************************/
340 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
342 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
343 ULONG refcount = InterlockedIncrement(&This->ref7);
345 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
347 if (refcount == 1)
349 ddraw_surface_add_iface(This);
352 return refcount;
355 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
357 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
358 ULONG refcount = InterlockedIncrement(&This->ref4);
360 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
362 if (refcount == 1)
364 ddraw_surface_add_iface(This);
367 return refcount;
370 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
372 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
373 ULONG refcount = InterlockedIncrement(&This->ref3);
375 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
377 if (refcount == 1)
379 ddraw_surface_add_iface(This);
382 return refcount;
385 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
387 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
388 ULONG refcount = InterlockedIncrement(&This->ref2);
390 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
392 if (refcount == 1)
394 ddraw_surface_add_iface(This);
397 return refcount;
400 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
402 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
403 ULONG refcount = InterlockedIncrement(&This->ref1);
405 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
407 if (refcount == 1)
409 ddraw_surface_add_iface(This);
412 return refcount;
415 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
417 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
418 ULONG refcount = InterlockedIncrement(&This->gamma_count);
420 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
422 if (refcount == 1)
424 ddraw_surface_add_iface(This);
427 return refcount;
430 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
432 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
434 TRACE("iface %p.\n", iface);
436 return IUnknown_AddRef(surface->texture_outer);
439 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
441 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
443 TRACE("iface %p.\n", iface);
445 return IUnknown_AddRef(surface->texture_outer);
448 static HRESULT ddraw_surface_set_palette(struct ddraw_surface *surface, IDirectDrawPalette *palette)
450 struct ddraw_palette *palette_impl = unsafe_impl_from_IDirectDrawPalette(palette);
451 struct ddraw_palette *prev;
453 TRACE("iface %p, palette %p.\n", surface, palette);
455 if (palette_impl && palette_impl->flags & DDPCAPS_ALPHA
456 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
458 WARN("Alpha palette set on non-texture surface, returning DDERR_INVALIDSURFACETYPE.\n");
459 return DDERR_INVALIDSURFACETYPE;
462 if (!format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
463 return DDERR_INVALIDPIXELFORMAT;
465 wined3d_mutex_lock();
467 prev = surface->palette;
468 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
470 if (prev)
471 prev->flags &= ~DDPCAPS_PRIMARYSURFACE;
472 if (palette_impl)
473 palette_impl->flags |= DDPCAPS_PRIMARYSURFACE;
474 wined3d_swapchain_set_palette(surface->ddraw->wined3d_swapchain,
475 palette_impl ? palette_impl->wineD3DPalette : NULL);
476 ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
478 if (palette_impl)
479 IDirectDrawPalette_AddRef(&palette_impl->IDirectDrawPalette_iface);
480 if (prev)
481 IDirectDrawPalette_Release(&prev->IDirectDrawPalette_iface);
482 surface->palette = palette_impl;
484 wined3d_mutex_unlock();
486 return DD_OK;
489 static void ddraw_surface_cleanup(struct ddraw_surface *surface)
491 struct ddraw_surface *surf;
492 UINT i;
494 TRACE("surface %p.\n", surface);
496 /* The refcount test shows that the palette is detached when the surface
497 * is destroyed. */
498 ddraw_surface_set_palette(surface, NULL);
500 /* Loop through all complex attached surfaces and destroy them.
502 * Yet again, only the root can have more than one complexly attached
503 * surface, all the others have a total of one. */
504 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
506 if (!surface->complex_array[i])
507 break;
509 surf = surface->complex_array[i];
510 surface->complex_array[i] = NULL;
511 if (!surf->is_complex_root)
512 ddraw_surface_cleanup(surf);
515 if (surface->device1)
516 IUnknown_Release(&surface->device1->IUnknown_inner);
518 if (surface->iface_count > 1)
520 /* This can happen when a complex surface is destroyed, because the
521 * 2nd surface was addref()ed when the app called
522 * GetAttachedSurface(). */
523 WARN("Destroying surface %p with refcounts 7: %u 4: %u 3: %u 2: %u 1: %u.\n",
524 surface, surface->ref7, surface->ref4, surface->ref3, surface->ref2, surface->ref1);
527 if (surface->wined3d_rtv)
528 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
529 wined3d_texture_decref(surface->wined3d_texture);
532 ULONG ddraw_surface_release_iface(struct ddraw_surface *This)
534 ULONG iface_count;
536 /* Prevent the surface from being destroyed if it's still attached to
537 * another surface. It will be destroyed when the root is destroyed. */
538 if (This->iface_count == 1 && This->attached_iface)
539 IUnknown_AddRef(This->attached_iface);
540 iface_count = InterlockedDecrement(&This->iface_count);
542 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
544 if (iface_count == 0)
546 IUnknown *release_iface = This->ifaceToRelease;
548 /* Complex attached surfaces are destroyed implicitly when the root is released */
549 wined3d_mutex_lock();
550 if(!This->is_complex_root)
552 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
553 wined3d_mutex_unlock();
554 return iface_count;
556 ddraw_surface_cleanup(This);
557 wined3d_mutex_unlock();
559 if (release_iface)
560 IUnknown_Release(release_iface);
563 return iface_count;
566 /*****************************************************************************
567 * IDirectDrawSurface7::Release
569 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
570 * surface is destroyed.
572 * Destroying the surface is a bit tricky. For the connection between
573 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
574 * It has a nice graph explaining the connection.
576 * What happens here is basically this:
577 * When a surface is destroyed, its WineD3DSurface is released,
578 * and the refcount of the DirectDraw interface is reduced by 1. If it has
579 * complex surfaces attached to it, then these surfaces are destroyed too,
580 * regardless of their refcount. If any surface being destroyed has another
581 * surface attached to it (with a "soft" attachment, not complex), then
582 * this surface is detached with DeleteAttachedSurface.
584 * When the surface is a texture, the WineD3DTexture is released.
585 * If the surface is the Direct3D render target, then the D3D
586 * capabilities of the WineD3DDevice are uninitialized, which causes the
587 * swapchain to be released.
589 * When a complex sublevel falls to ref zero, then this is ignored.
591 * Returns:
592 * The new refcount
594 *****************************************************************************/
595 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
597 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
598 ULONG refcount = InterlockedDecrement(&This->ref7);
600 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
602 if (refcount == 0)
604 ddraw_surface_release_iface(This);
607 return refcount;
610 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
612 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
613 ULONG refcount = InterlockedDecrement(&This->ref4);
615 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
617 if (refcount == 0)
619 ddraw_surface_release_iface(This);
622 return refcount;
625 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
627 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
628 ULONG refcount = InterlockedDecrement(&This->ref3);
630 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
632 if (refcount == 0)
634 ddraw_surface_release_iface(This);
637 return refcount;
640 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
642 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
643 ULONG refcount = InterlockedDecrement(&This->ref2);
645 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
647 if (refcount == 0)
649 ddraw_surface_release_iface(This);
652 return refcount;
655 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
657 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
658 ULONG refcount = InterlockedDecrement(&This->ref1);
660 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
662 if (refcount == 0)
664 ddraw_surface_release_iface(This);
667 return refcount;
670 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
672 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
673 ULONG refcount = InterlockedDecrement(&This->gamma_count);
675 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
677 if (refcount == 0)
679 ddraw_surface_release_iface(This);
682 return refcount;
685 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
687 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
689 TRACE("iface %p.\n", iface);
691 return IUnknown_Release(surface->texture_outer);
694 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
696 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
698 TRACE("iface %p.\n", iface);
700 return IUnknown_Release(surface->texture_outer);
703 /*****************************************************************************
704 * IDirectDrawSurface7::GetAttachedSurface
706 * Returns an attached surface with the requested caps. Surface attachment
707 * and complex surfaces are not clearly described by the MSDN or sdk,
708 * so this method is tricky and likely to contain problems.
709 * This implementation searches the complex list first, then the
710 * attachment chain.
712 * The chains are searched from This down to the last surface in the chain,
713 * not from the first element in the chain. The first surface found is
714 * returned. The MSDN says that this method fails if more than one surface
715 * matches the caps, but it is not sure if that is right. The attachment
716 * structure may not even allow two matching surfaces.
718 * The found surface is AddRef-ed before it is returned.
720 * Params:
721 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
722 * Surface: Address to store the found surface
724 * Returns:
725 * DD_OK on success
726 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
727 * DDERR_NOTFOUND if no surface was found
729 *****************************************************************************/
730 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
731 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
733 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
734 struct ddraw_surface *surf;
735 DDSCAPS2 our_caps;
736 int i;
738 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
740 wined3d_mutex_lock();
742 if(This->version < 7)
744 /* Earlier dx apps put garbage into these members, clear them */
745 our_caps.dwCaps = Caps->dwCaps;
746 our_caps.dwCaps2 = 0;
747 our_caps.dwCaps3 = 0;
748 our_caps.u1.dwCaps4 = 0;
750 else
752 our_caps = *Caps;
755 TRACE("(%p): Looking for caps: %x,%x,%x,%x\n", This, our_caps.dwCaps, our_caps.dwCaps2, our_caps.dwCaps3, our_caps.u1.dwCaps4); /* FIXME: Better debugging */
757 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
759 surf = This->complex_array[i];
760 if(!surf) break;
762 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
763 surf->surface_desc.ddsCaps.dwCaps,
764 surf->surface_desc.ddsCaps.dwCaps2,
765 surf->surface_desc.ddsCaps.dwCaps3,
766 surf->surface_desc.ddsCaps.u1.dwCaps4);
768 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
769 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
771 /* MSDN: "This method fails if more than one surface is attached
772 * that matches the capabilities requested."
774 * Not sure how to test this.
777 TRACE("(%p): Returning surface %p\n", This, surf);
778 *Surface = &surf->IDirectDrawSurface7_iface;
779 ddraw_surface7_AddRef(*Surface);
780 wined3d_mutex_unlock();
782 return DD_OK;
786 /* Next, look at the attachment chain */
787 surf = This;
789 while( (surf = surf->next_attached) )
791 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
792 surf->surface_desc.ddsCaps.dwCaps,
793 surf->surface_desc.ddsCaps.dwCaps2,
794 surf->surface_desc.ddsCaps.dwCaps3,
795 surf->surface_desc.ddsCaps.u1.dwCaps4);
797 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
798 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
800 TRACE("(%p): Returning surface %p\n", This, surf);
801 *Surface = &surf->IDirectDrawSurface7_iface;
802 ddraw_surface7_AddRef(*Surface);
803 wined3d_mutex_unlock();
804 return DD_OK;
808 TRACE("(%p) Didn't find a valid surface\n", This);
810 wined3d_mutex_unlock();
812 *Surface = NULL;
813 return DDERR_NOTFOUND;
816 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
817 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
819 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
820 struct ddraw_surface *attachment_impl;
821 IDirectDrawSurface7 *attachment7;
822 HRESULT hr;
824 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
826 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
827 caps, &attachment7);
828 if (FAILED(hr))
830 *attachment = NULL;
831 return hr;
833 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
834 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
835 ddraw_surface4_AddRef(*attachment);
836 ddraw_surface7_Release(attachment7);
838 return hr;
841 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
842 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
844 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
845 struct ddraw_surface *attachment_impl;
846 IDirectDrawSurface7 *attachment7;
847 DDSCAPS2 caps2;
848 HRESULT hr;
850 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
852 caps2.dwCaps = caps->dwCaps;
853 caps2.dwCaps2 = 0;
854 caps2.dwCaps3 = 0;
855 caps2.u1.dwCaps4 = 0;
857 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
858 &caps2, &attachment7);
859 if (FAILED(hr))
861 *attachment = NULL;
862 return hr;
864 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
865 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
866 ddraw_surface3_AddRef(*attachment);
867 ddraw_surface7_Release(attachment7);
869 return hr;
872 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
873 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
875 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
876 struct ddraw_surface *attachment_impl;
877 IDirectDrawSurface7 *attachment7;
878 DDSCAPS2 caps2;
879 HRESULT hr;
881 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
883 caps2.dwCaps = caps->dwCaps;
884 caps2.dwCaps2 = 0;
885 caps2.dwCaps3 = 0;
886 caps2.u1.dwCaps4 = 0;
888 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
889 &caps2, &attachment7);
890 if (FAILED(hr))
892 *attachment = NULL;
893 return hr;
895 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
896 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
897 ddraw_surface2_AddRef(*attachment);
898 ddraw_surface7_Release(attachment7);
900 return hr;
903 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
904 DDSCAPS *caps, IDirectDrawSurface **attachment)
906 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
907 struct ddraw_surface *attachment_impl;
908 IDirectDrawSurface7 *attachment7;
909 DDSCAPS2 caps2;
910 HRESULT hr;
912 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
914 caps2.dwCaps = caps->dwCaps;
915 caps2.dwCaps2 = 0;
916 caps2.dwCaps3 = 0;
917 caps2.u1.dwCaps4 = 0;
919 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
920 &caps2, &attachment7);
921 if (FAILED(hr))
923 *attachment = NULL;
924 return hr;
926 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
927 *attachment = &attachment_impl->IDirectDrawSurface_iface;
928 ddraw_surface1_AddRef(*attachment);
929 ddraw_surface7_Release(attachment7);
931 return hr;
934 /*****************************************************************************
935 * IDirectDrawSurface7::Lock
937 * Locks the surface and returns a pointer to the surface's memory
939 * Params:
940 * Rect: Rectangle to lock. If NULL, the whole surface is locked
941 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
942 * Flags: Locking flags, e.g Read only or write only
943 * h: An event handle that's not used and must be NULL
945 * Returns:
946 * DD_OK on success
947 * DDERR_INVALIDPARAMS if DDSD is NULL
948 * For more details, see IWineD3DSurface::LockRect
950 *****************************************************************************/
951 static HRESULT surface_lock(struct ddraw_surface *surface,
952 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
954 struct wined3d_box box;
955 struct wined3d_map_desc map_desc;
956 HRESULT hr = DD_OK;
958 TRACE("surface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
959 surface, wine_dbgstr_rect(rect), surface_desc, flags, h);
961 /* surface->surface_desc.dwWidth and dwHeight are changeable, thus lock */
962 wined3d_mutex_lock();
964 /* Should I check for the handle to be NULL?
966 * The DDLOCK flags and the D3DLOCK flags are equal
967 * for the supported values. The others are ignored by WineD3D
970 /* Windows zeroes this if the rect is invalid */
971 surface_desc->lpSurface = NULL;
973 if (rect)
975 if ((rect->left < 0) || (rect->top < 0)
976 || (rect->left > rect->right) || (rect->right > surface->surface_desc.dwWidth)
977 || (rect->top > rect->bottom) || (rect->bottom > surface->surface_desc.dwHeight))
979 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
980 wined3d_mutex_unlock();
981 return DDERR_INVALIDPARAMS;
983 box.left = rect->left;
984 box.top = rect->top;
985 box.right = rect->right;
986 box.bottom = rect->bottom;
987 box.front = 0;
988 box.back = 1;
991 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
992 hr = ddraw_surface_update_frontbuffer(surface, rect, TRUE);
993 if (SUCCEEDED(hr))
994 hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture),
995 surface->sub_resource_idx, &map_desc, rect ? &box : NULL, flags);
996 if (FAILED(hr))
998 wined3d_mutex_unlock();
999 switch(hr)
1001 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
1002 * specific error. But since IWineD3DSurface::LockRect returns that error in this
1003 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
1004 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
1005 * is much easier to do it in one place in ddraw
1007 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
1008 default: return hr;
1012 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1014 if (flags & DDLOCK_READONLY)
1015 memset(&surface->ddraw->primary_lock, 0, sizeof(surface->ddraw->primary_lock));
1016 else if (rect)
1017 surface->ddraw->primary_lock = *rect;
1018 else
1019 SetRect(&surface->ddraw->primary_lock, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
1022 /* Windows does not set DDSD_LPSURFACE on locked surfaces. */
1023 DD_STRUCT_COPY_BYSIZE(surface_desc, &surface->surface_desc);
1024 surface_desc->lpSurface = map_desc.data;
1026 TRACE("locked surface returning description :\n");
1027 if (TRACE_ON(ddraw))
1028 DDRAW_dump_surface_desc(surface_desc);
1030 wined3d_mutex_unlock();
1032 return DD_OK;
1035 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1036 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1038 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1040 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1041 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1043 if (!surface_desc) return DDERR_INVALIDPARAMS;
1044 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1045 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1047 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1048 return DDERR_INVALIDPARAMS;
1050 return surface_lock(surface, rect, surface_desc, flags, h);
1053 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1054 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1056 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1058 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1059 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1061 if (!surface_desc) return DDERR_INVALIDPARAMS;
1062 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1063 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1065 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1066 return DDERR_INVALIDPARAMS;
1068 return surface_lock(surface, rect, surface_desc, flags, h);
1071 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1072 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1074 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1075 DDSURFACEDESC2 surface_desc2;
1076 HRESULT hr;
1078 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1079 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1081 if (!surface_desc) return DDERR_INVALIDPARAMS;
1082 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1083 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1085 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1086 return DDERR_INVALIDPARAMS;
1089 surface_desc2.dwSize = surface_desc->dwSize;
1090 surface_desc2.dwFlags = 0;
1091 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1092 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1093 surface_desc->dwSize = surface_desc2.dwSize;
1094 return hr;
1097 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1098 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1100 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1101 DDSURFACEDESC2 surface_desc2;
1102 HRESULT hr;
1104 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1105 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1107 if (!surface_desc) return DDERR_INVALIDPARAMS;
1108 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1109 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1111 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1112 return DDERR_INVALIDPARAMS;
1115 surface_desc2.dwSize = surface_desc->dwSize;
1116 surface_desc2.dwFlags = 0;
1117 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1118 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1119 surface_desc->dwSize = surface_desc2.dwSize;
1120 return hr;
1123 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1124 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1126 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1127 DDSURFACEDESC2 surface_desc2;
1128 HRESULT hr;
1129 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1130 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1132 if (!surface_desc) return DDERR_INVALIDPARAMS;
1133 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1134 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1136 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1137 return DDERR_INVALIDPARAMS;
1140 surface_desc2.dwSize = surface_desc->dwSize;
1141 surface_desc2.dwFlags = 0;
1142 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1143 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1144 surface_desc->dwSize = surface_desc2.dwSize;
1145 return hr;
1148 /*****************************************************************************
1149 * IDirectDrawSurface7::Unlock
1151 * Unlocks an locked surface
1153 * Params:
1154 * Rect: Not used by this implementation
1156 * Returns:
1157 * D3D_OK on success
1158 * For more details, see IWineD3DSurface::UnlockRect
1160 *****************************************************************************/
1161 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
1163 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1164 HRESULT hr;
1166 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1168 wined3d_mutex_lock();
1169 hr = wined3d_resource_unmap(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx);
1170 if (SUCCEEDED(hr) && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1171 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE);
1172 wined3d_mutex_unlock();
1174 return hr;
1177 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1179 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1181 TRACE("iface %p, rect %p.\n", iface, pRect);
1183 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, pRect);
1186 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1188 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1190 TRACE("iface %p, data %p.\n", iface, data);
1192 /* data might not be the LPRECT of later versions, so drop it. */
1193 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1196 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1198 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1200 TRACE("iface %p, data %p.\n", iface, data);
1202 /* data might not be the LPRECT of later versions, so drop it. */
1203 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1206 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1208 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1210 TRACE("iface %p, data %p.\n", iface, data);
1212 /* data might not be the LPRECT of later versions, so drop it. */
1213 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1216 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Flip(IDirectDrawSurface7 *iface,
1217 IDirectDrawSurface7 *src, DWORD flags)
1219 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
1220 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src);
1221 struct wined3d_rendertarget_view *tmp_rtv, *src_rtv, *rtv;
1222 struct ddraw_texture *ddraw_texture, *prev_ddraw_texture;
1223 DDSCAPS2 caps = {DDSCAPS_FLIP, 0, 0, {0}};
1224 struct wined3d_texture *texture;
1225 IDirectDrawSurface7 *current;
1226 HRESULT hr;
1228 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1230 if (src == iface || !(dst_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1231 return DDERR_NOTFLIPPABLE;
1233 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
1234 return DDERR_SURFACELOST;
1236 wined3d_mutex_lock();
1238 if (!(dst_impl->ddraw->cooperative_level & DDSCL_EXCLUSIVE))
1240 WARN("Not in exclusive mode.\n");
1241 wined3d_mutex_unlock();
1242 return DDERR_NOEXCLUSIVEMODE;
1245 tmp_rtv = ddraw_surface_get_rendertarget_view(dst_impl);
1246 if (dst_impl->sub_resource_idx)
1247 ERR("Invalid sub-resource index %u on surface %p.\n", dst_impl->sub_resource_idx, dst_impl);
1248 texture = dst_impl->wined3d_texture;
1249 rtv = wined3d_device_get_rendertarget_view(dst_impl->ddraw->wined3d_device, 0);
1250 ddraw_texture = wined3d_texture_get_parent(dst_impl->wined3d_texture);
1252 if (src_impl)
1254 for (current = iface; current != src;)
1256 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1258 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1259 wined3d_mutex_unlock();
1260 return DDERR_NOTFLIPPABLE;
1262 ddraw_surface7_Release(current);
1263 if (current == iface)
1265 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1266 wined3d_mutex_unlock();
1267 return DDERR_NOTFLIPPABLE;
1271 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1272 if (rtv == dst_impl->wined3d_rtv)
1273 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1274 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1275 dst_impl->wined3d_rtv = src_rtv;
1276 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1277 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1278 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1279 if (src_impl->sub_resource_idx)
1280 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1281 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1282 ddraw_texture = prev_ddraw_texture;
1284 else
1286 for (current = iface;;)
1288 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1290 ERR("Can't find a flip target\n");
1291 wined3d_mutex_unlock();
1292 return DDERR_NOTFLIPPABLE; /* Unchecked */
1294 ddraw_surface7_Release(current);
1295 if (current == iface)
1297 dst_impl = impl_from_IDirectDrawSurface7(iface);
1298 break;
1301 src_impl = impl_from_IDirectDrawSurface7(current);
1302 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1303 if (rtv == dst_impl->wined3d_rtv)
1304 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1305 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1306 dst_impl->wined3d_rtv = src_rtv;
1307 wined3d_texture_set_sub_resource_parent(src_impl->wined3d_texture, 0, dst_impl);
1308 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1309 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1310 ddraw_texture = prev_ddraw_texture;
1311 if (src_impl->sub_resource_idx)
1312 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1313 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1314 dst_impl = src_impl;
1318 /* We don't have to worry about potential texture bindings, since
1319 * flippable surfaces can never be textures. */
1320 if (rtv == src_impl->wined3d_rtv)
1321 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, tmp_rtv, FALSE);
1322 wined3d_rendertarget_view_set_parent(tmp_rtv, src_impl);
1323 src_impl->wined3d_rtv = tmp_rtv;
1324 wined3d_texture_set_sub_resource_parent(texture, 0, src_impl);
1325 wined3d_resource_set_parent(wined3d_texture_get_resource(texture), ddraw_texture);
1326 src_impl->wined3d_texture = texture;
1328 if (flags)
1330 static UINT once;
1331 if (!once++)
1332 FIXME("Ignoring flags %#x.\n", flags);
1333 else
1334 WARN("Ignoring flags %#x.\n", flags);
1337 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1338 hr = ddraw_surface_update_frontbuffer(dst_impl, NULL, FALSE);
1339 else
1340 hr = DD_OK;
1342 wined3d_mutex_unlock();
1344 return hr;
1347 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Flip(IDirectDrawSurface4 *iface,
1348 IDirectDrawSurface4 *src, DWORD flags)
1350 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1351 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
1353 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1355 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1356 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, flags);
1359 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Flip(IDirectDrawSurface3 *iface,
1360 IDirectDrawSurface3 *src, DWORD flags)
1362 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1363 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src);
1365 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1367 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1368 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, flags);
1371 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Flip(IDirectDrawSurface2 *iface,
1372 IDirectDrawSurface2 *src, DWORD flags)
1374 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1375 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src);
1377 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1379 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1380 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, flags);
1383 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Flip(IDirectDrawSurface *iface,
1384 IDirectDrawSurface *src, DWORD flags)
1386 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1387 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src);
1389 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1391 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1392 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, flags);
1395 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1396 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags,
1397 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1399 struct wined3d_texture *wined3d_src_texture;
1400 unsigned int src_sub_resource_idx;
1401 RECT src_rect, dst_rect;
1402 float scale_x, scale_y;
1403 const RECT *clip_rect;
1404 UINT clip_list_size;
1405 RGNDATA *clip_list;
1406 HRESULT hr = DD_OK;
1407 UINT i;
1409 if (!dst_rect_in)
1410 SetRect(&dst_rect, 0, 0, dst_surface->surface_desc.dwWidth,
1411 dst_surface->surface_desc.dwHeight);
1412 else
1413 dst_rect = *dst_rect_in;
1415 if (IsRectEmpty(&dst_rect))
1416 return DDERR_INVALIDRECT;
1418 if (src_surface)
1420 if (!src_rect_in)
1421 SetRect(&src_rect, 0, 0, src_surface->surface_desc.dwWidth,
1422 src_surface->surface_desc.dwHeight);
1423 else
1424 src_rect = *src_rect_in;
1426 if (IsRectEmpty(&src_rect))
1427 return DDERR_INVALIDRECT;
1429 wined3d_src_texture = src_surface->wined3d_texture;
1430 src_sub_resource_idx = src_surface->sub_resource_idx;
1432 else
1434 SetRectEmpty(&src_rect);
1435 wined3d_src_texture = NULL;
1436 src_sub_resource_idx = 0;
1439 if (!dst_surface->clipper)
1441 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1442 hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect, TRUE);
1443 if (SUCCEEDED(hr))
1444 hr = wined3d_texture_blt(dst_surface->wined3d_texture, dst_surface->sub_resource_idx, &dst_rect,
1445 wined3d_src_texture, src_sub_resource_idx, &src_rect, flags, fx, filter);
1446 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
1447 hr = ddraw_surface_update_frontbuffer(dst_surface, &dst_rect, FALSE);
1449 return hr;
1452 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1453 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1455 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1456 &dst_rect, NULL, &clip_list_size)))
1458 WARN("Failed to get clip list size, hr %#x.\n", hr);
1459 return hr;
1462 if (!(clip_list = HeapAlloc(GetProcessHeap(), 0, clip_list_size)))
1464 WARN("Failed to allocate clip list.\n");
1465 return E_OUTOFMEMORY;
1468 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1469 &dst_rect, clip_list, &clip_list_size)))
1471 WARN("Failed to get clip list, hr %#x.\n", hr);
1472 HeapFree(GetProcessHeap(), 0, clip_list);
1473 return hr;
1476 clip_rect = (RECT *)clip_list->Buffer;
1477 for (i = 0; i < clip_list->rdh.nCount; ++i)
1479 RECT src_rect_clipped = src_rect;
1481 if (src_surface)
1483 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1484 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1485 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1486 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1488 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1490 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE)))
1491 break;
1495 if (FAILED(hr = wined3d_texture_blt(dst_surface->wined3d_texture, dst_surface->sub_resource_idx,
1496 &clip_rect[i], wined3d_src_texture, src_sub_resource_idx, &src_rect_clipped, flags, fx, filter)))
1497 break;
1499 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1501 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE)))
1502 break;
1506 HeapFree(GetProcessHeap(), 0, clip_list);
1507 return hr;
1510 /*****************************************************************************
1511 * IDirectDrawSurface7::Blt
1513 * Performs a blit on the surface
1515 * Params:
1516 * DestRect: Destination rectangle, can be NULL
1517 * SrcSurface: Source surface, can be NULL
1518 * SrcRect: Source rectangle, can be NULL
1519 * Flags: Blt flags
1520 * DDBltFx: Some extended blt parameters, connected to the flags
1522 * Returns:
1523 * D3D_OK on success
1524 * See IWineD3DSurface::Blt for more details
1526 *****************************************************************************/
1527 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *dst_rect,
1528 IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1530 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
1531 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src_surface);
1532 struct wined3d_blt_fx wined3d_fx;
1533 HRESULT hr = DD_OK;
1534 DDBLTFX rop_fx;
1536 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1537 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1539 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1540 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1542 if ((flags & DDBLT_KEYSRCOVERRIDE) && (!fx || flags & DDBLT_KEYSRC))
1544 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1545 return DDERR_INVALIDPARAMS;
1548 if ((flags & DDBLT_KEYDESTOVERRIDE) && (!fx || flags & DDBLT_KEYDEST))
1550 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1551 return DDERR_INVALIDPARAMS;
1554 if (flags & DDBLT_DDROPS)
1556 FIXME("DDBLT_DDROPS not implemented.\n");
1557 if (fx)
1558 FIXME(" rop %#x, pattern %p.\n", fx->dwDDROP, fx->u5.lpDDSPattern);
1559 return DDERR_NORASTEROPHW;
1562 wined3d_mutex_lock();
1564 if (flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1566 if (flags & DDBLT_ROP)
1568 wined3d_mutex_unlock();
1569 WARN("DDBLT_ROP used with DDBLT_COLORFILL or DDBLT_DEPTHFILL, returning DDERR_INVALIDPARAMS.\n");
1570 return DDERR_INVALIDPARAMS;
1572 if (src_impl)
1574 wined3d_mutex_unlock();
1575 WARN("Depth or colorfill is not compatible with source surfaces, returning DDERR_INVALIDPARAMS\n");
1576 return DDERR_INVALIDPARAMS;
1578 if (!fx)
1580 wined3d_mutex_unlock();
1581 WARN("Depth or colorfill used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1582 return DDERR_INVALIDPARAMS;
1585 if ((flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) == (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1586 flags &= ~DDBLT_DEPTHFILL;
1588 if ((dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_COLORFILL))
1590 wined3d_mutex_unlock();
1591 WARN("DDBLT_COLORFILL used on a depth buffer, returning DDERR_INVALIDPARAMS.\n");
1592 return DDERR_INVALIDPARAMS;
1594 if (!(dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (flags & DDBLT_DEPTHFILL))
1596 wined3d_mutex_unlock();
1597 WARN("DDBLT_DEPTHFILL used on a color buffer, returning DDERR_INVALIDPARAMS.\n");
1598 return DDERR_INVALIDPARAMS;
1602 if (flags & DDBLT_ROP)
1604 if (!fx)
1606 wined3d_mutex_unlock();
1607 WARN("DDBLT_ROP used with NULL fx, returning DDERR_INVALIDPARAMS.\n");
1608 return DDERR_INVALIDPARAMS;
1611 flags &= ~DDBLT_ROP;
1612 switch (fx->dwROP)
1614 case SRCCOPY:
1615 break;
1617 case WHITENESS:
1618 case BLACKNESS:
1619 rop_fx = *fx;
1621 if (fx->dwROP == WHITENESS)
1622 rop_fx.u5.dwFillColor = 0xffffffff;
1623 else
1624 rop_fx.u5.dwFillColor = 0;
1626 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1627 flags |= DDBLT_DEPTHFILL;
1628 else
1629 flags |= DDBLT_COLORFILL;
1631 fx = &rop_fx;
1632 break;
1634 default:
1635 wined3d_mutex_unlock();
1636 WARN("Unsupported ROP %#x used, returning DDERR_NORASTEROPHW.\n", fx->dwROP);
1637 return DDERR_NORASTEROPHW;
1641 if (flags & DDBLT_KEYSRC && (!src_impl || !(src_impl->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1643 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1644 wined3d_mutex_unlock();
1645 return DDERR_INVALIDPARAMS;
1648 if (flags & ~WINED3D_BLT_MASK)
1650 wined3d_mutex_unlock();
1651 FIXME("Unhandled flags %#x.\n", flags);
1652 return E_NOTIMPL;
1655 if (fx)
1657 wined3d_fx.fx = fx->dwDDFX;
1658 wined3d_fx.fill_color = fx->u5.dwFillColor;
1659 wined3d_fx.dst_color_key.color_space_low_value = fx->ddckDestColorkey.dwColorSpaceLowValue;
1660 wined3d_fx.dst_color_key.color_space_high_value = fx->ddckDestColorkey.dwColorSpaceHighValue;
1661 wined3d_fx.src_color_key.color_space_low_value = fx->ddckSrcColorkey.dwColorSpaceLowValue;
1662 wined3d_fx.src_color_key.color_space_high_value = fx->ddckSrcColorkey.dwColorSpaceHighValue;
1665 hr = ddraw_surface_blt_clipped(dst_impl, dst_rect, src_impl,
1666 src_rect, flags, fx ? &wined3d_fx : NULL, WINED3D_TEXF_LINEAR);
1668 wined3d_mutex_unlock();
1669 switch(hr)
1671 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1672 default: return hr;
1676 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1677 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1679 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1680 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1682 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1683 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1685 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1686 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1689 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1690 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1692 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1693 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1695 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1696 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1698 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1699 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1702 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1703 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1705 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1706 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1708 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1709 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1711 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1712 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1715 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1716 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1718 struct ddraw_surface *dst = impl_from_IDirectDrawSurface(iface);
1719 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1721 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1722 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1724 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1725 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1728 /*****************************************************************************
1729 * IDirectDrawSurface7::AddAttachedSurface
1731 * Attaches a surface to another surface. How the surface attachments work
1732 * is not totally understood yet, and this method is prone to problems.
1733 * The surface that is attached is AddRef-ed.
1735 * Tests with complex surfaces suggest that the surface attachments form a
1736 * tree, but no method to test this has been found yet.
1738 * The attachment list consists of a first surface (first_attached) and
1739 * for each surface a pointer to the next attached surface (next_attached).
1740 * For the first surface, and a surface that has no attachments
1741 * first_attached points to the surface itself. A surface that has
1742 * no successors in the chain has next_attached set to NULL.
1744 * Newly attached surfaces are attached right after the root surface.
1745 * If a surface is attached to a complex surface compound, it's attached to
1746 * the surface that the app requested, not the complex root. See
1747 * GetAttachedSurface for a description how surfaces are found.
1749 * This is how the current implementation works, and it was coded by looking
1750 * at the needs of the applications.
1752 * So far only Z-Buffer attachments are tested, and they are activated in
1753 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1754 * Back buffers should work in 2D mode, but they are not tested(They can be
1755 * attached in older iface versions). Rendering to the front buffer and
1756 * switching between that and double buffering is not yet implemented in
1757 * WineD3D, so for 3D it might have unexpected results.
1759 * ddraw_surface_attach_surface is the real thing,
1760 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1761 * performs additional checks. Version 7 of this interface is much more restrictive
1762 * than its predecessors.
1764 * Params:
1765 * Attach: Surface to attach to iface
1767 * Returns:
1768 * DD_OK on success
1769 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1771 *****************************************************************************/
1772 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1774 TRACE("surface %p, attachment %p.\n", This, Surf);
1776 if(Surf == This)
1777 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1779 wined3d_mutex_lock();
1781 /* Check if the surface is already attached somewhere */
1782 if (Surf->next_attached || Surf->first_attached != Surf)
1784 /* TODO: Test for the structure of the manual attachment. Is it a
1785 * chain or a list? What happens if one surface is attached to 2
1786 * different surfaces? */
1787 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1788 Surf, Surf->next_attached, Surf->first_attached);
1790 wined3d_mutex_unlock();
1791 return DDERR_SURFACEALREADYATTACHED;
1794 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1795 Surf->next_attached = This->next_attached;
1796 Surf->first_attached = This->first_attached;
1797 This->next_attached = Surf;
1799 /* Check if the WineD3D depth stencil needs updating */
1800 if (This->ddraw->d3ddevice)
1801 d3d_device_update_depth_stencil(This->ddraw->d3ddevice);
1803 wined3d_mutex_unlock();
1805 return DD_OK;
1808 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
1810 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
1811 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1812 HRESULT hr;
1814 TRACE("iface %p, attachment %p.\n", iface, attachment);
1816 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1817 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1820 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1821 attachment_impl->surface_desc.ddsCaps.dwCaps);
1822 return DDERR_CANNOTATTACHSURFACE;
1825 hr = ddraw_surface_attach_surface(This, attachment_impl);
1826 if (FAILED(hr))
1828 return hr;
1830 attachment_impl->attached_iface = (IUnknown *)attachment;
1831 IUnknown_AddRef(attachment_impl->attached_iface);
1832 return hr;
1835 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1837 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1838 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1839 HRESULT hr;
1841 TRACE("iface %p, attachment %p.\n", iface, attachment);
1843 /* Tests suggest that
1844 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1845 * -> offscreen plain surfaces can be attached to primaries
1846 * -> primaries can be attached to offscreen plain surfaces
1847 * -> z buffers can be attached to primaries */
1848 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1849 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1851 /* Sizes have to match */
1852 if (attachment_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
1853 || attachment_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
1855 WARN("Surface sizes do not match.\n");
1856 return DDERR_CANNOTATTACHSURFACE;
1859 else if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
1860 || !(attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER)))
1862 WARN("Invalid attachment combination.\n");
1863 return DDERR_CANNOTATTACHSURFACE;
1866 if (FAILED(hr = ddraw_surface_attach_surface(surface, attachment_impl)))
1867 return hr;
1869 attachment_impl->attached_iface = (IUnknown *)attachment;
1870 IUnknown_AddRef(attachment_impl->attached_iface);
1871 return hr;
1874 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1876 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1877 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1878 HRESULT hr;
1880 TRACE("iface %p, attachment %p.\n", iface, attachment);
1882 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1883 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1884 return hr;
1886 attachment_impl->attached_iface = (IUnknown *)attachment;
1887 IUnknown_AddRef(attachment_impl->attached_iface);
1888 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1889 return hr;
1892 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1894 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1895 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1896 HRESULT hr;
1898 TRACE("iface %p, attachment %p.\n", iface, attachment);
1900 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1901 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1902 return hr;
1904 attachment_impl->attached_iface = (IUnknown *)attachment;
1905 IUnknown_AddRef(attachment_impl->attached_iface);
1906 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1907 return hr;
1910 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1912 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1913 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1914 HRESULT hr;
1916 TRACE("iface %p, attachment %p.\n", iface, attachment);
1918 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1919 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1920 return hr;
1922 attachment_impl->attached_iface = (IUnknown *)attachment;
1923 IUnknown_AddRef(attachment_impl->attached_iface);
1924 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1925 return hr;
1928 /*****************************************************************************
1929 * IDirectDrawSurface7::DeleteAttachedSurface
1931 * Removes a surface from the attachment chain. The surface's refcount
1932 * is decreased by one after it has been removed
1934 * Params:
1935 * Flags: Some flags, not used by this implementation
1936 * Attach: Surface to detach
1938 * Returns:
1939 * DD_OK on success
1940 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1942 *****************************************************************************/
1943 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
1944 struct ddraw_surface *attachment, IUnknown *detach_iface)
1946 struct ddraw_surface *prev = surface;
1948 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
1950 wined3d_mutex_lock();
1951 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
1953 wined3d_mutex_unlock();
1954 return DDERR_CANNOTDETACHSURFACE;
1957 if (attachment->attached_iface != detach_iface)
1959 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
1960 wined3d_mutex_unlock();
1961 return DDERR_SURFACENOTATTACHED;
1964 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1965 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1967 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1968 /* FIXME: we should probably also subtract from dwMipMapCount of this
1969 * and all parent surfaces */
1972 /* Find the predecessor of the detached surface */
1973 while (prev->next_attached != attachment)
1975 if (!(prev = prev->next_attached))
1977 ERR("Failed to find predecessor of %p.\n", attachment);
1978 wined3d_mutex_unlock();
1979 return DDERR_SURFACENOTATTACHED;
1983 /* Unchain the surface */
1984 prev->next_attached = attachment->next_attached;
1985 attachment->next_attached = NULL;
1986 attachment->first_attached = attachment;
1988 /* Check if the wined3d depth stencil needs updating. Note that we don't
1989 * just call d3d_device_update_depth_stencil() here since it uses
1990 * QueryInterface(). Some applications, SCP - Containment Breach in
1991 * particular, modify the QueryInterface() pointer in the surface vtbl
1992 * but don't cleanup properly after the relevant dll is unloaded. */
1993 if (attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER
1994 && wined3d_device_get_depth_stencil_view(surface->ddraw->wined3d_device) == surface->wined3d_rtv)
1995 wined3d_device_set_depth_stencil_view(surface->ddraw->wined3d_device, NULL);
1996 wined3d_mutex_unlock();
1998 /* Set attached_iface to NULL before releasing it, the surface may go
1999 * away. */
2000 attachment->attached_iface = NULL;
2001 IUnknown_Release(detach_iface);
2003 return DD_OK;
2006 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
2007 DWORD flags, IDirectDrawSurface7 *attachment)
2009 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2010 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
2012 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2014 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2017 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
2018 DWORD flags, IDirectDrawSurface4 *attachment)
2020 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2021 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
2023 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2025 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2028 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
2029 DWORD flags, IDirectDrawSurface3 *attachment)
2031 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2032 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2034 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2036 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2039 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
2040 DWORD flags, IDirectDrawSurface2 *attachment)
2042 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2043 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2045 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2047 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2050 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
2051 DWORD flags, IDirectDrawSurface *attachment)
2053 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2054 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2056 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2058 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2061 /*****************************************************************************
2062 * IDirectDrawSurface7::AddOverlayDirtyRect
2064 * "This method is not currently implemented"
2066 * Params:
2067 * Rect: ?
2069 * Returns:
2070 * DDERR_UNSUPPORTED
2072 *****************************************************************************/
2073 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
2075 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
2077 return DDERR_UNSUPPORTED; /* unchecked */
2080 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
2082 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2084 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2086 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2089 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
2091 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2093 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2095 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2098 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
2100 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2102 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2104 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2107 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
2109 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2111 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2113 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2116 /*****************************************************************************
2117 * IDirectDrawSurface7::GetDC
2119 * Returns a GDI device context for the surface
2121 * Params:
2122 * hdc: Address of a HDC variable to store the dc to
2124 * Returns:
2125 * DD_OK on success
2126 * DDERR_INVALIDPARAMS if hdc is NULL
2127 * For details, see IWineD3DSurface::GetDC
2129 *****************************************************************************/
2130 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *dc)
2132 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2133 HRESULT hr = DD_OK;
2135 TRACE("iface %p, dc %p.\n", iface, dc);
2137 if (!dc)
2138 return DDERR_INVALIDPARAMS;
2140 wined3d_mutex_lock();
2141 if (surface->dc)
2142 hr = DDERR_DCALREADYCREATED;
2143 else if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2144 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE);
2145 if (SUCCEEDED(hr))
2146 hr = wined3d_texture_get_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
2148 if (SUCCEEDED(hr))
2150 surface->dc = *dc;
2152 if (format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
2154 const struct ddraw_palette *palette;
2156 if (surface->palette)
2157 palette = surface->palette;
2158 else if (surface->ddraw->primary)
2159 palette = surface->ddraw->primary->palette;
2160 else
2161 palette = NULL;
2163 if (palette)
2164 wined3d_palette_apply_to_dc(palette->wineD3DPalette, *dc);
2168 wined3d_mutex_unlock();
2169 switch (hr)
2171 /* Some, but not all errors set *dc to NULL. E.g. DCALREADYCREATED
2172 * does not touch *dc. */
2173 case WINED3DERR_INVALIDCALL:
2174 *dc = NULL;
2175 return DDERR_CANTCREATEDC;
2177 default:
2178 return hr;
2182 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
2184 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2186 TRACE("iface %p, dc %p.\n", iface, dc);
2188 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2191 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
2193 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2195 TRACE("iface %p, dc %p.\n", iface, dc);
2197 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2200 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2202 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2204 TRACE("iface %p, dc %p.\n", iface, dc);
2206 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2209 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2211 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2213 TRACE("iface %p, dc %p.\n", iface, dc);
2215 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2218 /*****************************************************************************
2219 * IDirectDrawSurface7::ReleaseDC
2221 * Releases the DC that was constructed with GetDC
2223 * Params:
2224 * hdc: HDC to release
2226 * Returns:
2227 * DD_OK on success
2228 * For more details, see IWineD3DSurface::ReleaseDC
2230 *****************************************************************************/
2231 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2233 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2234 HRESULT hr;
2236 TRACE("iface %p, dc %p.\n", iface, hdc);
2238 wined3d_mutex_lock();
2239 if (!surface->dc)
2241 hr = DDERR_NODC;
2243 else if (SUCCEEDED(hr = wined3d_texture_release_dc(surface->wined3d_texture, surface->sub_resource_idx, hdc)))
2245 surface->dc = NULL;
2246 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2247 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
2249 wined3d_mutex_unlock();
2252 return hr;
2255 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2257 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2259 TRACE("iface %p, dc %p.\n", iface, dc);
2261 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2264 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2266 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2268 TRACE("iface %p, dc %p.\n", iface, dc);
2270 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2273 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2275 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2277 TRACE("iface %p, dc %p.\n", iface, dc);
2279 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2282 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2284 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2286 TRACE("iface %p, dc %p.\n", iface, dc);
2288 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2291 /*****************************************************************************
2292 * IDirectDrawSurface7::GetCaps
2294 * Returns the surface's caps
2296 * Params:
2297 * Caps: Address to write the caps to
2299 * Returns:
2300 * DD_OK on success
2301 * DDERR_INVALIDPARAMS if Caps is NULL
2303 *****************************************************************************/
2304 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2306 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2308 TRACE("iface %p, caps %p.\n", iface, Caps);
2310 if(!Caps)
2311 return DDERR_INVALIDPARAMS;
2313 *Caps = surface->surface_desc.ddsCaps;
2315 return DD_OK;
2318 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2320 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2322 TRACE("iface %p, caps %p.\n", iface, caps);
2324 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2327 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2329 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2330 DDSCAPS2 caps2;
2331 HRESULT hr;
2333 TRACE("iface %p, caps %p.\n", iface, caps);
2335 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2336 if (FAILED(hr)) return hr;
2338 caps->dwCaps = caps2.dwCaps;
2339 return hr;
2342 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2344 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2345 DDSCAPS2 caps2;
2346 HRESULT hr;
2348 TRACE("iface %p, caps %p.\n", iface, caps);
2350 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2351 if (FAILED(hr)) return hr;
2353 caps->dwCaps = caps2.dwCaps;
2354 return hr;
2357 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2359 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2360 DDSCAPS2 caps2;
2361 HRESULT hr;
2363 TRACE("iface %p, caps %p.\n", iface, caps);
2365 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2366 if (FAILED(hr)) return hr;
2368 caps->dwCaps = caps2.dwCaps;
2369 return hr;
2372 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD priority)
2374 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2375 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2376 HRESULT hr;
2377 struct wined3d_resource *resource;
2379 TRACE("iface %p, priority %u.\n", iface, priority);
2381 wined3d_mutex_lock();
2382 /* No need to check for offscreen plain surfaces or mipmap sublevels. SetPriority
2383 * calls on such surfaces segfault on Windows. */
2384 if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed))
2386 WARN("Called on non-managed texture returning DDERR_INVALIDPARAMS.\n");
2387 hr = DDERR_INVALIDPARAMS;
2389 else
2391 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2392 wined3d_resource_set_priority(resource, priority);
2393 hr = DD_OK;
2395 wined3d_mutex_unlock();
2397 return hr;
2400 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *priority)
2402 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2403 const struct wined3d_resource *resource;
2404 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2405 HRESULT hr;
2407 TRACE("iface %p, priority %p.\n", iface, priority);
2409 wined3d_mutex_lock();
2410 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN)
2412 WARN("Called on offscreenplain surface, returning DDERR_INVALIDOBJECT.\n");
2413 hr = DDERR_INVALIDOBJECT;
2415 else if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed) || !surface->is_complex_root)
2417 WARN("Called on non-managed texture or non-root surface, returning DDERR_INVALIDPARAMS.\n");
2418 hr = DDERR_INVALIDPARAMS;
2420 else
2422 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2423 *priority = wined3d_resource_get_priority(resource);
2424 hr = DD_OK;
2426 wined3d_mutex_unlock();
2428 return hr;
2431 /*****************************************************************************
2432 * IDirectDrawSurface7::SetPrivateData
2434 * Stores some data in the surface that is intended for the application's
2435 * use.
2437 * Params:
2438 * tag: GUID that identifies the data
2439 * Data: Pointer to the private data
2440 * Size: Size of the private data
2441 * Flags: Some flags
2443 * Returns:
2444 * D3D_OK on success
2445 * For more details, see IWineD3DSurface::SetPrivateData
2447 *****************************************************************************/
2448 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2449 REFGUID tag, void *data, DWORD size, DWORD flags)
2451 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2452 HRESULT hr;
2454 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2455 iface, debugstr_guid(tag), data, size, flags);
2457 if (!data)
2459 WARN("data is NULL, returning DDERR_INVALIDPARAMS.\n");
2460 return DDERR_INVALIDPARAMS;
2463 wined3d_mutex_lock();
2464 hr = wined3d_private_store_set_private_data(&surface->private_store, tag, data, size, flags);
2465 wined3d_mutex_unlock();
2466 return hr_ddraw_from_wined3d(hr);
2469 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2470 REFGUID tag, void *data, DWORD size, DWORD flags)
2472 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2474 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2475 iface, debugstr_guid(tag), data, size, flags);
2477 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2480 /*****************************************************************************
2481 * IDirectDrawSurface7::GetPrivateData
2483 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2485 * Params:
2486 * tag: GUID of the data to return
2487 * Data: Address where to write the data to
2488 * Size: Size of the buffer at Data
2490 * Returns:
2491 * DD_OK on success
2492 * DDERR_INVALIDPARAMS if Data is NULL
2493 * For more details, see IWineD3DSurface::GetPrivateData
2495 *****************************************************************************/
2496 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *data, DWORD *size)
2498 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2499 const struct wined3d_private_data *stored_data;
2500 HRESULT hr;
2502 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2503 iface, debugstr_guid(tag), data, size);
2505 wined3d_mutex_lock();
2506 stored_data = wined3d_private_store_get_private_data(&surface->private_store, tag);
2507 if (!stored_data)
2509 hr = DDERR_NOTFOUND;
2510 goto done;
2512 if (!size)
2514 hr = DDERR_INVALIDPARAMS;
2515 goto done;
2517 if (*size < stored_data->size)
2519 *size = stored_data->size;
2520 hr = DDERR_MOREDATA;
2521 goto done;
2523 if (!data)
2525 hr = DDERR_INVALIDPARAMS;
2526 goto done;
2529 *size = stored_data->size;
2530 memcpy(data, stored_data->content.data, stored_data->size);
2531 hr = DD_OK;
2533 done:
2534 wined3d_mutex_unlock();
2535 return hr;
2538 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2540 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2542 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2543 iface, debugstr_guid(tag), data, size);
2545 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2548 /*****************************************************************************
2549 * IDirectDrawSurface7::FreePrivateData
2551 * Frees private data stored in the surface
2553 * Params:
2554 * tag: Tag of the data to free
2556 * Returns:
2557 * D3D_OK on success
2558 * For more details, see IWineD3DSurface::FreePrivateData
2560 *****************************************************************************/
2561 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2563 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2564 struct wined3d_private_data *entry;
2566 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2568 wined3d_mutex_lock();
2569 entry = wined3d_private_store_get_private_data(&surface->private_store, tag);
2570 if (!entry)
2572 wined3d_mutex_unlock();
2573 return DDERR_NOTFOUND;
2576 wined3d_private_store_free_private_data(&surface->private_store, entry);
2577 wined3d_mutex_unlock();
2579 return DD_OK;
2582 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2584 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2586 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2588 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2591 /*****************************************************************************
2592 * IDirectDrawSurface7::PageLock
2594 * Prevents a sysmem surface from being paged out
2596 * Params:
2597 * Flags: Not used, must be 0(unchecked)
2599 * Returns:
2600 * DD_OK, because it's a stub
2602 *****************************************************************************/
2603 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2605 TRACE("iface %p, flags %#x.\n", iface, Flags);
2607 /* This is Windows memory management related - we don't need this */
2608 return DD_OK;
2611 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2613 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2615 TRACE("iface %p, flags %#x.\n", iface, flags);
2617 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2620 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2622 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2624 TRACE("iface %p, flags %#x.\n", iface, flags);
2626 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2629 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2631 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2633 TRACE("iface %p, flags %#x.\n", iface, flags);
2635 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2638 /*****************************************************************************
2639 * IDirectDrawSurface7::PageUnlock
2641 * Allows a sysmem surface to be paged out
2643 * Params:
2644 * Flags: Not used, must be 0(unchecked)
2646 * Returns:
2647 * DD_OK, because it's a stub
2649 *****************************************************************************/
2650 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2652 TRACE("iface %p, flags %#x.\n", iface, Flags);
2654 return DD_OK;
2657 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2659 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2661 TRACE("iface %p, flags %#x.\n", iface, flags);
2663 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2666 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2668 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2670 TRACE("iface %p, flags %#x.\n", iface, flags);
2672 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2675 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2677 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2679 TRACE("iface %p, flags %#x.\n", iface, flags);
2681 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2684 /*****************************************************************************
2685 * IDirectDrawSurface7::BltBatch
2687 * An unimplemented function
2689 * Params:
2692 * Returns:
2693 * DDERR_UNSUPPORTED
2695 *****************************************************************************/
2696 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2698 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2700 /* MSDN: "not currently implemented" */
2701 return DDERR_UNSUPPORTED;
2704 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2706 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2708 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2710 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2713 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2715 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2717 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2719 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2722 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2724 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2726 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2728 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2731 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2733 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2735 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2737 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2740 /*****************************************************************************
2741 * IDirectDrawSurface7::EnumAttachedSurfaces
2743 * Enumerates all surfaces attached to this surface
2745 * Params:
2746 * context: Pointer to pass unmodified to the callback
2747 * cb: Callback function to call for each surface
2749 * Returns:
2750 * DD_OK on success
2751 * DDERR_INVALIDPARAMS if cb is NULL
2753 *****************************************************************************/
2754 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2755 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2757 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2758 struct ddraw_surface *surf;
2759 DDSURFACEDESC2 desc;
2760 int i;
2762 /* Attached surfaces aren't handled in WineD3D */
2763 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2765 if(!cb)
2766 return DDERR_INVALIDPARAMS;
2768 wined3d_mutex_lock();
2770 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2772 surf = surface->complex_array[i];
2773 if(!surf) break;
2775 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2776 desc = surf->surface_desc;
2777 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2778 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2780 wined3d_mutex_unlock();
2781 return DD_OK;
2785 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2787 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2788 desc = surf->surface_desc;
2789 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2790 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2792 wined3d_mutex_unlock();
2793 return DD_OK;
2797 TRACE(" end of enumeration.\n");
2799 wined3d_mutex_unlock();
2801 return DD_OK;
2804 struct callback_info2
2806 LPDDENUMSURFACESCALLBACK2 callback;
2807 void *context;
2810 struct callback_info
2812 LPDDENUMSURFACESCALLBACK callback;
2813 void *context;
2816 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2818 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2819 const struct callback_info2 *info = context;
2821 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
2822 ddraw_surface7_Release(surface);
2824 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
2827 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2829 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2830 const struct callback_info *info = context;
2832 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
2833 ddraw_surface7_Release(surface);
2835 /* FIXME: Check surface_test.dwSize */
2836 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2837 (DDSURFACEDESC *)surface_desc, info->context);
2840 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2841 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2843 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2844 struct callback_info2 info;
2846 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2848 info.callback = callback;
2849 info.context = context;
2851 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2852 &info, EnumCallback2);
2855 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2856 void *context, LPDDENUMSURFACESCALLBACK callback)
2858 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2859 struct callback_info info;
2861 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2863 info.callback = callback;
2864 info.context = context;
2866 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2867 &info, EnumCallback);
2870 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2871 void *context, LPDDENUMSURFACESCALLBACK callback)
2873 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2874 struct callback_info info;
2876 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2878 info.callback = callback;
2879 info.context = context;
2881 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2882 &info, EnumCallback);
2885 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2886 void *context, LPDDENUMSURFACESCALLBACK callback)
2888 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2889 struct callback_info info;
2891 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2893 info.callback = callback;
2894 info.context = context;
2896 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2897 &info, EnumCallback);
2900 /*****************************************************************************
2901 * IDirectDrawSurface7::EnumOverlayZOrders
2903 * "Enumerates the overlay surfaces on the specified destination"
2905 * Params:
2906 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2907 * context: context to pass back to the callback
2908 * cb: callback function to call for each enumerated surface
2910 * Returns:
2911 * DD_OK, because it's a stub
2913 *****************************************************************************/
2914 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2915 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2917 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2919 return DD_OK;
2922 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2923 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2925 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2926 struct callback_info2 info;
2928 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2930 info.callback = callback;
2931 info.context = context;
2933 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2934 flags, &info, EnumCallback2);
2937 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2938 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2940 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2941 struct callback_info info;
2943 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2945 info.callback = callback;
2946 info.context = context;
2948 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2949 flags, &info, EnumCallback);
2952 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2953 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2955 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2956 struct callback_info info;
2958 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2960 info.callback = callback;
2961 info.context = context;
2963 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2964 flags, &info, EnumCallback);
2967 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2968 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2970 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2971 struct callback_info info;
2973 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2975 info.callback = callback;
2976 info.context = context;
2978 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2979 flags, &info, EnumCallback);
2982 /*****************************************************************************
2983 * IDirectDrawSurface7::GetBltStatus
2985 * Returns the blitting status
2987 * Params:
2988 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2990 * Returns:
2991 * See IWineD3DSurface::Blt
2993 *****************************************************************************/
2994 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2996 TRACE("iface %p, flags %#x.\n", iface, Flags);
2998 switch (Flags)
3000 case WINEDDGBS_CANBLT:
3001 case WINEDDGBS_ISBLTDONE:
3002 return DD_OK;
3004 default:
3005 return DDERR_INVALIDPARAMS;
3009 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
3011 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3013 TRACE("iface %p, flags %#x.\n", iface, flags);
3015 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3018 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
3020 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3022 TRACE("iface %p, flags %#x.\n", iface, flags);
3024 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3027 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
3029 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3031 TRACE("iface %p, flags %#x.\n", iface, flags);
3033 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3036 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
3038 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3040 TRACE("iface %p, flags %#x.\n", iface, flags);
3042 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3045 /*****************************************************************************
3046 * IDirectDrawSurface7::GetColorKey
3048 * Returns the color key assigned to the surface
3050 * Params:
3051 * Flags: Some flags
3052 * CKey: Address to store the key to
3054 * Returns:
3055 * DD_OK on success
3056 * DDERR_INVALIDPARAMS if CKey is NULL
3058 *****************************************************************************/
3059 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
3061 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3063 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
3065 if(!CKey)
3066 return DDERR_INVALIDPARAMS;
3068 wined3d_mutex_lock();
3070 switch (Flags)
3072 case DDCKEY_DESTBLT:
3073 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
3075 wined3d_mutex_unlock();
3076 return DDERR_NOCOLORKEY;
3078 *CKey = This->surface_desc.ddckCKDestBlt;
3079 break;
3081 case DDCKEY_DESTOVERLAY:
3082 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
3084 wined3d_mutex_unlock();
3085 return DDERR_NOCOLORKEY;
3087 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
3088 break;
3090 case DDCKEY_SRCBLT:
3091 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
3093 wined3d_mutex_unlock();
3094 return DDERR_NOCOLORKEY;
3096 *CKey = This->surface_desc.ddckCKSrcBlt;
3097 break;
3099 case DDCKEY_SRCOVERLAY:
3100 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
3102 wined3d_mutex_unlock();
3103 return DDERR_NOCOLORKEY;
3105 *CKey = This->surface_desc.ddckCKSrcOverlay;
3106 break;
3108 default:
3109 wined3d_mutex_unlock();
3110 return DDERR_INVALIDPARAMS;
3113 wined3d_mutex_unlock();
3115 return DD_OK;
3118 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
3120 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3122 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3124 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3127 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
3129 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3131 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3133 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3136 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
3138 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3140 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3142 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3145 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
3147 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3149 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3151 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3154 /*****************************************************************************
3155 * IDirectDrawSurface7::GetFlipStatus
3157 * Returns the flipping status of the surface
3159 * Params:
3160 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
3162 * Returns:
3163 * See IWineD3DSurface::GetFlipStatus
3165 *****************************************************************************/
3166 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3168 TRACE("iface %p, flags %#x.\n", iface, Flags);
3170 /* XXX: DDERR_INVALIDSURFACETYPE */
3172 switch (Flags)
3174 case WINEDDGFS_CANFLIP:
3175 case WINEDDGFS_ISFLIPDONE:
3176 return DD_OK;
3178 default:
3179 return DDERR_INVALIDPARAMS;
3183 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
3185 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3187 TRACE("iface %p, flags %#x.\n", iface, flags);
3189 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3192 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
3194 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3196 TRACE("iface %p, flags %#x.\n", iface, flags);
3198 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3201 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
3203 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3205 TRACE("iface %p, flags %#x.\n", iface, flags);
3207 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3210 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
3212 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3214 TRACE("iface %p, flags %#x.\n", iface, flags);
3216 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3219 /*****************************************************************************
3220 * IDirectDrawSurface7::GetOverlayPosition
3222 * Returns the display coordinates of a visible and active overlay surface
3224 * Params:
3228 * Returns:
3229 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3230 *****************************************************************************/
3231 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *x, LONG *y)
3233 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3234 HRESULT hr;
3236 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3238 wined3d_mutex_lock();
3239 hr = wined3d_texture_get_overlay_position(surface->wined3d_texture,
3240 surface->sub_resource_idx, x, y);
3241 wined3d_mutex_unlock();
3243 return hr;
3246 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3248 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3250 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3252 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3255 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3257 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3259 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3261 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3264 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3266 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3268 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3270 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3273 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3275 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3277 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3279 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3282 /*****************************************************************************
3283 * IDirectDrawSurface7::GetPixelFormat
3285 * Returns the pixel format of the Surface
3287 * Params:
3288 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3289 * format should be written
3291 * Returns:
3292 * DD_OK on success
3293 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3295 *****************************************************************************/
3296 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3298 /* What is DDERR_INVALIDSURFACETYPE for here? */
3299 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3301 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3303 if(!PixelFormat)
3304 return DDERR_INVALIDPARAMS;
3306 wined3d_mutex_lock();
3307 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3308 wined3d_mutex_unlock();
3310 return DD_OK;
3313 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3315 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3317 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3319 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3322 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3324 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3326 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3328 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3331 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3333 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3335 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3337 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3340 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3342 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3344 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3346 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3349 /*****************************************************************************
3350 * IDirectDrawSurface7::GetSurfaceDesc
3352 * Returns the description of this surface
3354 * Params:
3355 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3356 * surface desc
3358 * Returns:
3359 * DD_OK on success
3360 * DDERR_INVALIDPARAMS if DDSD is NULL
3362 *****************************************************************************/
3363 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3365 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3367 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3369 if(!DDSD)
3370 return DDERR_INVALIDPARAMS;
3372 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3374 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3375 return DDERR_INVALIDPARAMS;
3378 wined3d_mutex_lock();
3379 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3380 TRACE("Returning surface desc:\n");
3381 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3382 wined3d_mutex_unlock();
3384 return DD_OK;
3387 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3389 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3391 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3393 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3396 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3398 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3400 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3402 if (!surface_desc) return DDERR_INVALIDPARAMS;
3404 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3406 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3407 return DDERR_INVALIDPARAMS;
3410 wined3d_mutex_lock();
3411 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3412 TRACE("Returning surface desc:\n");
3413 if (TRACE_ON(ddraw))
3415 /* DDRAW_dump_surface_desc handles the smaller size */
3416 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3418 wined3d_mutex_unlock();
3420 return DD_OK;
3423 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3425 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3427 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3429 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3432 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3434 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3436 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3438 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3441 /*****************************************************************************
3442 * IDirectDrawSurface7::Initialize
3444 * Initializes the surface. This is a no-op in Wine
3446 * Params:
3447 * DD: Pointer to an DirectDraw interface
3448 * DDSD: Surface description for initialization
3450 * Returns:
3451 * DDERR_ALREADYINITIALIZED
3453 *****************************************************************************/
3454 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3455 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3457 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3459 return DDERR_ALREADYINITIALIZED;
3462 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3463 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3465 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3467 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3469 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3470 ddraw, surface_desc);
3473 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3474 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3476 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3477 DDSURFACEDESC2 surface_desc2;
3479 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3481 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3482 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3483 ddraw, surface_desc ? &surface_desc2 : NULL);
3486 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3487 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3489 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3490 DDSURFACEDESC2 surface_desc2;
3492 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3494 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3495 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3496 ddraw, surface_desc ? &surface_desc2 : NULL);
3499 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3500 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3502 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3503 DDSURFACEDESC2 surface_desc2;
3505 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3507 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3508 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3509 ddraw, surface_desc ? &surface_desc2 : NULL);
3512 /*****************************************************************************
3513 * IDirect3DTexture1::Initialize
3515 * The sdk says it's not implemented
3517 * Params:
3520 * Returns
3521 * DDERR_UNSUPPORTED
3523 *****************************************************************************/
3524 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3525 IDirect3DDevice *device, IDirectDrawSurface *surface)
3527 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3529 return DDERR_UNSUPPORTED; /* Unchecked */
3532 /*****************************************************************************
3533 * IDirectDrawSurface7::IsLost
3535 * Checks if the surface is lost
3537 * Returns:
3538 * DD_OK, if the surface is usable
3539 * DDERR_ISLOST if the surface is lost
3540 * See IWineD3DSurface::IsLost for more details
3542 *****************************************************************************/
3543 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3545 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3547 TRACE("iface %p.\n", iface);
3549 if (surface->ddraw->device_state != DDRAW_DEVICE_STATE_OK || surface->is_lost)
3550 return DDERR_SURFACELOST;
3552 return DD_OK;
3555 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3557 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3559 TRACE("iface %p.\n", iface);
3561 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3564 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3566 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3568 TRACE("iface %p.\n", iface);
3570 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3573 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3575 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3577 TRACE("iface %p.\n", iface);
3579 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3582 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3584 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3586 TRACE("iface %p.\n", iface);
3588 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3591 /*****************************************************************************
3592 * IDirectDrawSurface7::Restore
3594 * Restores a lost surface. This makes the surface usable again, but
3595 * doesn't reload its old contents
3597 * Returns:
3598 * DD_OK on success
3599 * See IWineD3DSurface::Restore for more details
3601 *****************************************************************************/
3602 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3604 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3606 TRACE("iface %p.\n", iface);
3608 ddraw_update_lost_surfaces(surface->ddraw);
3609 surface->is_lost = FALSE;
3611 return DD_OK;
3614 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3616 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3618 TRACE("iface %p.\n", iface);
3620 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3623 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3625 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3627 TRACE("iface %p.\n", iface);
3629 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3632 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3634 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3636 TRACE("iface %p.\n", iface);
3638 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3641 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3643 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3645 TRACE("iface %p.\n", iface);
3647 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3650 /*****************************************************************************
3651 * IDirectDrawSurface7::SetOverlayPosition
3653 * Changes the display coordinates of an overlay surface
3655 * Params:
3656 * X:
3657 * Y:
3659 * Returns:
3660 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3661 *****************************************************************************/
3662 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG x, LONG y)
3664 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3665 HRESULT hr;
3667 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3669 wined3d_mutex_lock();
3670 hr = wined3d_texture_set_overlay_position(surface->wined3d_texture,
3671 surface->sub_resource_idx, x, y);
3672 wined3d_mutex_unlock();
3674 return hr;
3677 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3679 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3681 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3683 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3686 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3688 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3690 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3692 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3695 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3697 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3699 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3701 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3704 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3706 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3708 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3710 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3713 /*****************************************************************************
3714 * IDirectDrawSurface7::UpdateOverlay
3716 * Modifies the attributes of an overlay surface.
3718 * Params:
3719 * SrcRect: The section of the source being used for the overlay
3720 * DstSurface: Address of the surface that is overlaid
3721 * DstRect: Place of the overlay
3722 * Flags: some DDOVER_* flags
3724 * Returns:
3725 * DDERR_UNSUPPORTED, because we don't support overlays
3727 *****************************************************************************/
3728 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *src_rect,
3729 IDirectDrawSurface7 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3731 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3732 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(dst_surface);
3733 struct wined3d_texture *dst_wined3d_texture = NULL;
3734 unsigned int dst_sub_resource_idx = 0;
3735 HRESULT hr;
3737 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3738 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3740 if (fx)
3741 FIXME("Ignoring fx %p.\n", fx);
3743 wined3d_mutex_lock();
3744 if (dst_impl)
3746 dst_wined3d_texture = dst_impl->wined3d_texture;
3747 dst_sub_resource_idx = dst_impl->sub_resource_idx;
3749 hr = wined3d_texture_update_overlay(src_impl->wined3d_texture, src_impl->sub_resource_idx,
3750 src_rect, dst_wined3d_texture, dst_sub_resource_idx, dst_rect, flags);
3751 wined3d_mutex_unlock();
3753 switch (hr)
3755 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3756 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3757 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3758 default:
3759 return hr;
3763 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3764 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3766 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3767 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3769 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3770 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3772 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3773 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3776 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3777 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3779 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
3780 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3782 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3783 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3785 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3786 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3789 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3790 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3792 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
3793 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3795 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3796 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3798 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3799 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3802 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3803 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3805 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
3806 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3808 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3809 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3811 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3812 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3815 /*****************************************************************************
3816 * IDirectDrawSurface7::UpdateOverlayDisplay
3818 * The DX7 sdk says that it's not implemented
3820 * Params:
3821 * Flags: ?
3823 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3825 *****************************************************************************/
3826 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3828 TRACE("iface %p, flags %#x.\n", iface, Flags);
3830 return DDERR_UNSUPPORTED;
3833 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3835 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3837 TRACE("iface %p, flags %#x.\n", iface, flags);
3839 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3842 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3844 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3846 TRACE("iface %p, flags %#x.\n", iface, flags);
3848 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3851 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3853 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3855 TRACE("iface %p, flags %#x.\n", iface, flags);
3857 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3860 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3862 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3864 TRACE("iface %p, flags %#x.\n", iface, flags);
3866 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3869 /*****************************************************************************
3870 * IDirectDrawSurface7::UpdateOverlayZOrder
3872 * Sets an overlay's Z order
3874 * Params:
3875 * Flags: DDOVERZ_* flags
3876 * DDSRef: Defines the relative position in the overlay chain
3878 * Returns:
3879 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3881 *****************************************************************************/
3882 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3883 DWORD flags, IDirectDrawSurface7 *reference)
3885 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3887 FIXME("iface %p, flags %#x, reference %p stub!\n", iface, flags, reference);
3889 wined3d_mutex_lock();
3890 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OVERLAY))
3892 WARN("Not an overlay surface.\n");
3893 wined3d_mutex_unlock();
3894 return DDERR_NOTAOVERLAYSURFACE;
3896 wined3d_mutex_unlock();
3898 return DD_OK;
3901 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3902 DWORD flags, IDirectDrawSurface4 *reference)
3904 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3905 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3907 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3909 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3910 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3913 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3914 DWORD flags, IDirectDrawSurface3 *reference)
3916 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3917 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3919 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3921 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3922 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3925 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3926 DWORD flags, IDirectDrawSurface2 *reference)
3928 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3929 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3931 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3933 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3934 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3937 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3938 DWORD flags, IDirectDrawSurface *reference)
3940 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3941 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3943 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3945 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3946 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3949 /*****************************************************************************
3950 * IDirectDrawSurface7::GetDDInterface
3952 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3953 * surface belongs to
3955 * Params:
3956 * DD: Address to write the interface pointer to
3958 * Returns:
3959 * DD_OK on success
3960 * DDERR_INVALIDPARAMS if DD is NULL
3962 *****************************************************************************/
3963 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3965 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3967 TRACE("iface %p, ddraw %p.\n", iface, DD);
3969 if(!DD)
3970 return DDERR_INVALIDPARAMS;
3972 switch(This->version)
3974 case 7:
3975 *DD = &This->ddraw->IDirectDraw7_iface;
3976 break;
3978 case 4:
3979 *DD = &This->ddraw->IDirectDraw4_iface;
3980 break;
3982 case 2:
3983 *DD = &This->ddraw->IDirectDraw2_iface;
3984 break;
3986 case 1:
3987 *DD = &This->ddraw->IDirectDraw_iface;
3988 break;
3991 IUnknown_AddRef((IUnknown *)*DD);
3993 return DD_OK;
3996 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3998 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4000 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4002 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4005 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
4007 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4009 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4011 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4014 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
4016 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4018 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
4020 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
4023 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
4025 TRACE("iface %p.\n", iface);
4027 return DD_OK;
4030 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
4032 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4034 TRACE("iface %p.\n", iface);
4036 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
4039 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
4041 TRACE("iface %p, value %p.\n", iface, pValue);
4043 *pValue = 0;
4045 return DD_OK;
4048 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
4050 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4052 TRACE("iface %p, value %p.\n", iface, pValue);
4054 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
4057 /*****************************************************************************
4058 * IDirectDrawSurface7::SetLOD
4060 * Sets the level of detail of a texture
4062 * Params:
4063 * MaxLOD: LOD to set
4065 * Returns:
4066 * DD_OK on success
4067 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4069 *****************************************************************************/
4070 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
4072 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4073 HRESULT hr;
4075 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
4077 wined3d_mutex_lock();
4078 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4080 wined3d_mutex_unlock();
4081 return DDERR_INVALIDOBJECT;
4084 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
4085 wined3d_mutex_unlock();
4087 return hr;
4090 /*****************************************************************************
4091 * IDirectDrawSurface7::GetLOD
4093 * Returns the level of detail of a Direct3D texture
4095 * Params:
4096 * MaxLOD: Address to write the LOD to
4098 * Returns:
4099 * DD_OK on success
4100 * DDERR_INVALIDPARAMS if MaxLOD is NULL
4101 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4103 *****************************************************************************/
4104 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4106 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4108 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4110 if(!MaxLOD)
4111 return DDERR_INVALIDPARAMS;
4113 wined3d_mutex_lock();
4114 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4116 wined3d_mutex_unlock();
4117 return DDERR_INVALIDOBJECT;
4120 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4121 wined3d_mutex_unlock();
4123 return DD_OK;
4126 /*****************************************************************************
4127 * IDirectDrawSurface7::BltFast
4129 * Performs a fast Blit.
4131 * Params:
4132 * dstx: The x coordinate to blit to on the destination
4133 * dsty: The y coordinate to blit to on the destination
4134 * Source: The source surface
4135 * rsrc: The source rectangle
4136 * trans: Type of transfer. Some DDBLTFAST_* flags
4138 * Returns:
4139 * DD_OK on success
4140 * For more details, see IWineD3DSurface::BltFast
4142 *****************************************************************************/
4143 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltFast(IDirectDrawSurface7 *iface,
4144 DWORD dst_x, DWORD dst_y, IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD trans)
4146 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
4147 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src_surface);
4148 DWORD src_w, src_h, dst_w, dst_h;
4149 HRESULT hr = DD_OK;
4150 RECT dst_rect, s;
4151 DWORD flags = 0;
4153 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4154 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), trans);
4156 dst_w = dst_impl->surface_desc.dwWidth;
4157 dst_h = dst_impl->surface_desc.dwHeight;
4159 if (!src_rect)
4161 SetRect(&s, 0, 0, src_impl->surface_desc.dwWidth, src_impl->surface_desc.dwHeight);
4162 src_rect = &s;
4165 src_w = src_rect->right - src_rect->left;
4166 src_h = src_rect->bottom - src_rect->top;
4167 if (src_w > dst_w || dst_x > dst_w - src_w
4168 || src_h > dst_h || dst_y > dst_h - src_h)
4170 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4171 return DDERR_INVALIDRECT;
4174 SetRect(&dst_rect, dst_x, dst_y, dst_x + src_w, dst_y + src_h);
4175 if (trans & DDBLTFAST_SRCCOLORKEY)
4176 flags |= WINED3D_BLT_SRC_CKEY;
4177 if (trans & DDBLTFAST_DESTCOLORKEY)
4178 flags |= WINED3D_BLT_DST_CKEY;
4179 if (trans & DDBLTFAST_WAIT)
4180 flags |= WINED3D_BLT_WAIT;
4181 if (trans & DDBLTFAST_DONOTWAIT)
4182 flags |= WINED3D_BLT_DO_NOT_WAIT;
4184 wined3d_mutex_lock();
4185 if (dst_impl->clipper)
4187 wined3d_mutex_unlock();
4188 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4189 return DDERR_BLTFASTCANTCLIP;
4192 if (src_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4193 hr = ddraw_surface_update_frontbuffer(src_impl, src_rect, TRUE);
4194 if (SUCCEEDED(hr))
4195 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
4196 src_impl->wined3d_texture, src_impl->sub_resource_idx, src_rect, flags, NULL, WINED3D_TEXF_POINT);
4197 if (SUCCEEDED(hr) && (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4198 hr = ddraw_surface_update_frontbuffer(dst_impl, &dst_rect, FALSE);
4199 wined3d_mutex_unlock();
4201 switch(hr)
4203 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4204 default: return hr;
4208 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4209 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4211 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4212 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4214 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4215 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4217 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4218 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4221 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4222 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4224 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4225 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4227 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4228 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4230 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4231 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4234 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4235 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4237 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4238 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4240 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4241 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4243 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4244 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4247 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4248 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4250 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4251 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4253 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4254 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4256 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4257 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4260 /*****************************************************************************
4261 * IDirectDrawSurface7::GetClipper
4263 * Returns the IDirectDrawClipper interface of the clipper assigned to this
4264 * surface
4266 * Params:
4267 * Clipper: Address to store the interface pointer at
4269 * Returns:
4270 * DD_OK on success
4271 * DDERR_INVALIDPARAMS if Clipper is NULL
4272 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
4274 *****************************************************************************/
4275 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
4277 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4279 TRACE("iface %p, clipper %p.\n", iface, Clipper);
4281 if (!Clipper)
4282 return DDERR_INVALIDPARAMS;
4284 wined3d_mutex_lock();
4285 if (!surface->clipper)
4287 wined3d_mutex_unlock();
4288 return DDERR_NOCLIPPERATTACHED;
4291 *Clipper = (IDirectDrawClipper *)surface->clipper;
4292 IDirectDrawClipper_AddRef(*Clipper);
4293 wined3d_mutex_unlock();
4295 return DD_OK;
4298 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4300 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4302 TRACE("iface %p, clipper %p.\n", iface, clipper);
4304 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4307 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4309 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4311 TRACE("iface %p, clipper %p.\n", iface, clipper);
4313 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4316 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4318 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4320 TRACE("iface %p, clipper %p.\n", iface, clipper);
4322 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4325 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4327 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4329 TRACE("iface %p, clipper %p.\n", iface, clipper);
4331 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4334 /*****************************************************************************
4335 * IDirectDrawSurface7::SetClipper
4337 * Sets a clipper for the surface
4339 * Params:
4340 * Clipper: IDirectDrawClipper interface of the clipper to set
4342 * Returns:
4343 * DD_OK on success
4345 *****************************************************************************/
4346 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4347 IDirectDrawClipper *iclipper)
4349 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4350 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4351 struct ddraw_clipper *old_clipper = This->clipper;
4352 HWND clipWindow;
4354 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4356 wined3d_mutex_lock();
4357 if (clipper == This->clipper)
4359 wined3d_mutex_unlock();
4360 return DD_OK;
4363 This->clipper = clipper;
4365 if (clipper != NULL)
4366 IDirectDrawClipper_AddRef(iclipper);
4367 if (old_clipper)
4368 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4370 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4372 clipWindow = NULL;
4373 if(clipper) {
4374 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4377 if (clipWindow)
4379 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4380 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4382 else
4384 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4385 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4389 wined3d_mutex_unlock();
4391 return DD_OK;
4394 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4396 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4398 TRACE("iface %p, clipper %p.\n", iface, clipper);
4400 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4403 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4405 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4407 TRACE("iface %p, clipper %p.\n", iface, clipper);
4409 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4412 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4414 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4416 TRACE("iface %p, clipper %p.\n", iface, clipper);
4418 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4421 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4423 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4425 TRACE("iface %p, clipper %p.\n", iface, clipper);
4427 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4430 /*****************************************************************************
4431 * IDirectDrawSurface7::SetSurfaceDesc
4433 * Sets the surface description. It can override the pixel format, the surface
4434 * memory, ...
4435 * It's not really tested.
4437 * Params:
4438 * DDSD: Pointer to the new surface description to set
4439 * Flags: Some flags
4441 * Returns:
4442 * DD_OK on success
4443 * DDERR_INVALIDPARAMS if DDSD is NULL
4445 *****************************************************************************/
4446 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4448 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4449 HRESULT hr;
4450 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4451 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4452 enum wined3d_format_id format_id;
4453 UINT pitch, width, height;
4455 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4457 if (!DDSD)
4459 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4460 return DDERR_INVALIDPARAMS;
4462 if (Flags)
4464 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4465 return DDERR_INVALIDPARAMS;
4467 if (!(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4468 || This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4469 || This->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4471 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4472 return DDERR_INVALIDSURFACETYPE;
4475 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4476 * for PIXELFORMAT to work */
4477 if (DDSD->dwFlags & ~allowed_flags)
4479 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4480 return DDERR_INVALIDPARAMS;
4482 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4484 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4485 return DDERR_INVALIDPARAMS;
4487 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4489 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4490 return DDERR_INVALIDCAPS;
4492 if (DDSD->dwFlags & DDSD_WIDTH)
4494 if (!(DDSD->dwFlags & DDSD_PITCH))
4496 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4497 return DDERR_INVALIDPARAMS;
4499 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4501 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4502 DDSD->u1.lPitch, DDSD->dwWidth);
4503 return DDERR_INVALIDPARAMS;
4505 if (DDSD->dwWidth != This->surface_desc.dwWidth)
4506 TRACE("Surface width changed from %u to %u.\n", This->surface_desc.dwWidth, DDSD->dwWidth);
4507 if (DDSD->u1.lPitch != This->surface_desc.u1.lPitch)
4508 TRACE("Surface pitch changed from %u to %u.\n", This->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4509 pitch = DDSD->u1.lPitch;
4510 width = DDSD->dwWidth;
4512 else if (DDSD->dwFlags & DDSD_PITCH)
4514 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4515 return DDERR_INVALIDPARAMS;
4517 else
4519 pitch = This->surface_desc.u1.lPitch;
4520 width = This->surface_desc.dwWidth;
4523 if (DDSD->dwFlags & DDSD_HEIGHT)
4525 if (!DDSD->dwHeight)
4527 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4528 return DDERR_INVALIDPARAMS;
4530 if (DDSD->dwHeight != This->surface_desc.dwHeight)
4531 TRACE("Surface height changed from %u to %u.\n", This->surface_desc.dwHeight, DDSD->dwHeight);
4532 height = DDSD->dwHeight;
4534 else
4536 height = This->surface_desc.dwHeight;
4539 wined3d_mutex_lock();
4540 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4542 enum wined3d_format_id current_format_id;
4543 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4545 if (format_id == WINED3DFMT_UNKNOWN)
4547 ERR("Requested to set an unknown pixelformat\n");
4548 wined3d_mutex_unlock();
4549 return DDERR_INVALIDPARAMS;
4551 current_format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4552 if (format_id != current_format_id)
4553 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4555 else
4557 format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4560 if (FAILED(hr = wined3d_texture_update_desc(This->wined3d_texture, width, height,
4561 format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4563 WARN("Failed to update surface desc, hr %#x.\n", hr);
4564 wined3d_mutex_unlock();
4565 return hr_ddraw_from_wined3d(hr);
4568 if (DDSD->dwFlags & DDSD_WIDTH)
4569 This->surface_desc.dwWidth = width;
4570 if (DDSD->dwFlags & DDSD_PITCH)
4571 This->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4572 if (DDSD->dwFlags & DDSD_HEIGHT)
4573 This->surface_desc.dwHeight = height;
4574 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4575 This->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4577 wined3d_mutex_unlock();
4579 return DD_OK;
4582 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4583 DDSURFACEDESC2 *surface_desc, DWORD flags)
4585 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4587 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4589 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4590 surface_desc, flags);
4593 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4594 DDSURFACEDESC *surface_desc, DWORD flags)
4596 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4597 DDSURFACEDESC2 surface_desc2;
4599 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4601 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4602 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4603 surface_desc ? &surface_desc2 : NULL, flags);
4606 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4608 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4609 struct ddraw_palette *palette_impl;
4610 HRESULT hr = DD_OK;
4612 TRACE("iface %p, palette %p.\n", iface, palette);
4614 if (!palette)
4615 return DDERR_INVALIDPARAMS;
4616 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4618 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4619 return DDERR_SURFACELOST;
4622 wined3d_mutex_lock();
4623 if ((palette_impl = surface->palette))
4625 *palette = &palette_impl->IDirectDrawPalette_iface;
4626 IDirectDrawPalette_AddRef(*palette);
4628 else
4630 *palette = NULL;
4631 hr = DDERR_NOPALETTEATTACHED;
4633 wined3d_mutex_unlock();
4635 return hr;
4638 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4640 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4642 TRACE("iface %p, palette %p.\n", iface, palette);
4644 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4647 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4649 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4651 TRACE("iface %p, palette %p.\n", iface, palette);
4653 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4656 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4658 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4660 TRACE("iface %p, palette %p.\n", iface, palette);
4662 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4665 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4667 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4669 TRACE("iface %p, palette %p.\n", iface, palette);
4671 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4674 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4676 DDCOLORKEY fixed_color_key;
4677 HRESULT hr = WINED3D_OK;
4679 if (flags & DDCKEY_COLORSPACE)
4681 if (color_key && color_key->dwColorSpaceLowValue != color_key->dwColorSpaceHighValue)
4683 WARN("Range color keys are not supported, returning DDERR_NOCOLORKEYHW.\n");
4684 return DDERR_NOCOLORKEYHW;
4686 flags &= ~DDCKEY_COLORSPACE;
4689 wined3d_mutex_lock();
4691 if (color_key)
4693 fixed_color_key.dwColorSpaceLowValue = fixed_color_key.dwColorSpaceHighValue = color_key->dwColorSpaceLowValue;
4694 switch (flags & ~DDCKEY_COLORSPACE)
4696 case DDCKEY_DESTBLT:
4697 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4698 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4699 break;
4701 case DDCKEY_DESTOVERLAY:
4702 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4703 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4704 break;
4706 case DDCKEY_SRCOVERLAY:
4707 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4708 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4709 break;
4711 case DDCKEY_SRCBLT:
4712 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4713 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4714 break;
4716 default:
4717 wined3d_mutex_unlock();
4718 return DDERR_INVALIDPARAMS;
4721 else
4723 switch (flags & ~DDCKEY_COLORSPACE)
4725 case DDCKEY_DESTBLT:
4726 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4727 break;
4729 case DDCKEY_DESTOVERLAY:
4730 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4731 break;
4733 case DDCKEY_SRCOVERLAY:
4734 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4735 break;
4737 case DDCKEY_SRCBLT:
4738 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4739 break;
4741 default:
4742 wined3d_mutex_unlock();
4743 return DDERR_INVALIDPARAMS;
4747 if (surface->is_complex_root)
4748 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags,
4749 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4751 wined3d_mutex_unlock();
4753 return hr_ddraw_from_wined3d(hr);
4756 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4758 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4760 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4762 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4763 return DDERR_NOTONMIPMAPSUBLEVEL;
4765 return ddraw_surface_set_color_key(surface, flags, color_key);
4768 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4770 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4772 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4774 return ddraw_surface_set_color_key(surface, flags, color_key);
4777 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4779 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4781 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4783 return ddraw_surface_set_color_key(surface, flags, color_key);
4786 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4788 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4790 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4792 return ddraw_surface_set_color_key(surface, flags, color_key);
4795 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4797 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4799 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4801 return ddraw_surface_set_color_key(surface, flags, color_key);
4804 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
4806 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4808 TRACE("iface %p, palette %p.\n", iface, palette);
4810 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4811 return DDERR_NOTONMIPMAPSUBLEVEL;
4812 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4814 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4815 return DDERR_SURFACELOST;
4818 return ddraw_surface_set_palette(surface, palette);
4821 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4823 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4825 TRACE("iface %p, palette %p.\n", iface, palette);
4827 if (IDirectDrawSurface4_IsLost(iface) == DDERR_SURFACELOST)
4829 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4830 return DDERR_SURFACELOST;
4833 return ddraw_surface_set_palette(surface, palette);
4836 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4838 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4840 TRACE("iface %p, palette %p.\n", iface, palette);
4842 if (IDirectDrawSurface3_IsLost(iface) == DDERR_SURFACELOST)
4844 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4845 return DDERR_SURFACELOST;
4848 return ddraw_surface_set_palette(surface, palette);
4851 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4853 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4855 TRACE("iface %p, palette %p.\n", iface, palette);
4857 if (IDirectDrawSurface2_IsLost(iface) == DDERR_SURFACELOST)
4859 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4860 return DDERR_SURFACELOST;
4863 return ddraw_surface_set_palette(surface, palette);
4866 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4868 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4870 TRACE("iface %p, palette %p.\n", iface, palette);
4872 if (IDirectDrawSurface_IsLost(iface) == DDERR_SURFACELOST)
4874 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4875 return DDERR_SURFACELOST;
4878 return ddraw_surface_set_palette(surface, palette);
4881 /**********************************************************
4882 * IDirectDrawGammaControl::GetGammaRamp
4884 * Returns the current gamma ramp for a surface
4886 * Params:
4887 * flags: Ignored
4888 * gamma_ramp: Address to write the ramp to
4890 * Returns:
4891 * DD_OK on success
4892 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4894 **********************************************************/
4895 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4896 DWORD flags, DDGAMMARAMP *gamma_ramp)
4898 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4900 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4902 if (!gamma_ramp)
4904 WARN("Invalid gamma_ramp passed.\n");
4905 return DDERR_INVALIDPARAMS;
4908 wined3d_mutex_lock();
4909 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4911 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4912 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
4914 else
4916 ERR("Not implemented for non-primary surfaces.\n");
4918 wined3d_mutex_unlock();
4920 return DD_OK;
4923 /**********************************************************
4924 * IDirectDrawGammaControl::SetGammaRamp
4926 * Sets the red, green and blue gamma ramps for
4928 * Params:
4929 * flags: Can be DDSGR_CALIBRATE to request calibration
4930 * gamma_ramp: Structure containing the new gamma ramp
4932 * Returns:
4933 * DD_OK on success
4934 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4936 **********************************************************/
4937 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4938 DWORD flags, DDGAMMARAMP *gamma_ramp)
4940 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4942 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4944 if (!gamma_ramp)
4946 WARN("Invalid gamma_ramp passed.\n");
4947 return DDERR_INVALIDPARAMS;
4950 wined3d_mutex_lock();
4951 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4953 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4954 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
4955 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
4957 else
4959 ERR("Not implemented for non-primary surfaces.\n");
4961 wined3d_mutex_unlock();
4963 return DD_OK;
4966 /*****************************************************************************
4967 * IDirect3DTexture2::PaletteChanged
4969 * Informs the texture about a palette change
4971 * Params:
4972 * start: Start index of the change
4973 * count: The number of changed entries
4975 * Returns
4976 * D3D_OK, because it's a stub
4978 *****************************************************************************/
4979 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4981 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4983 return D3D_OK;
4986 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4988 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
4990 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4992 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
4995 /*****************************************************************************
4996 * IDirect3DTexture::Unload
4998 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
5001 * Returns:
5002 * DDERR_UNSUPPORTED
5004 *****************************************************************************/
5005 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
5007 WARN("iface %p. Not implemented.\n", iface);
5009 return DDERR_UNSUPPORTED;
5012 /*****************************************************************************
5013 * IDirect3DTexture2::GetHandle
5015 * Returns handle for the texture. At the moment, the interface
5016 * to the IWineD3DTexture is used.
5018 * Params:
5019 * device: Device this handle is assigned to
5020 * handle: Address to store the handle at.
5022 * Returns:
5023 * D3D_OK
5025 *****************************************************************************/
5026 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
5027 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
5029 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
5030 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
5032 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5034 wined3d_mutex_lock();
5036 if (!surface->Handle)
5038 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
5039 if (h == DDRAW_INVALID_HANDLE)
5041 ERR("Failed to allocate a texture handle.\n");
5042 wined3d_mutex_unlock();
5043 return DDERR_OUTOFMEMORY;
5046 surface->Handle = h + 1;
5049 TRACE("Returning handle %08x.\n", surface->Handle);
5050 *handle = surface->Handle;
5052 wined3d_mutex_unlock();
5054 return D3D_OK;
5057 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
5058 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
5060 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5061 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
5063 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5065 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
5066 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
5069 /*****************************************************************************
5070 * get_sub_mimaplevel
5072 * Helper function that returns the next mipmap level
5074 * tex_ptr: Surface of which to return the next level
5076 *****************************************************************************/
5077 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
5079 /* Now go down the mipmap chain to the next surface */
5080 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, {0} };
5081 IDirectDrawSurface7 *next_level;
5082 HRESULT hr;
5084 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
5085 if (FAILED(hr)) return NULL;
5087 ddraw_surface7_Release(next_level);
5089 return impl_from_IDirectDrawSurface7(next_level);
5092 /*****************************************************************************
5093 * IDirect3DTexture2::Load
5095 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5097 * This function isn't relayed to WineD3D because the whole interface is
5098 * implemented in DDraw only. For speed improvements an implementation which
5099 * takes OpenGL more into account could be placed into WineD3D.
5101 * Params:
5102 * src_texture: Address of the texture to load
5104 * Returns:
5105 * D3D_OK on success
5106 * D3DERR_TEXTURE_LOAD_FAILED.
5108 *****************************************************************************/
5109 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5111 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5112 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5113 struct wined3d_resource *dst_resource, *src_resource;
5114 HRESULT hr;
5116 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5118 if (src_surface == dst_surface)
5120 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5121 return D3D_OK;
5124 wined3d_mutex_lock();
5126 dst_resource = wined3d_texture_get_resource(dst_surface->wined3d_texture);
5127 src_resource = wined3d_texture_get_resource(src_surface->wined3d_texture);
5129 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5130 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5131 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5133 ERR("Trying to load surfaces with different mip-map counts.\n");
5136 for (;;)
5138 struct ddraw_palette *dst_pal, *src_pal;
5139 DDSURFACEDESC *src_desc, *dst_desc;
5141 TRACE("Copying surface %p to surface %p.\n", src_surface, dst_surface);
5143 /* Suppress the ALLOCONLOAD flag */
5144 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5146 /* Get the palettes */
5147 dst_pal = dst_surface->palette;
5148 src_pal = src_surface->palette;
5150 if (src_pal)
5152 PALETTEENTRY palent[256];
5154 if (!dst_pal)
5156 wined3d_mutex_unlock();
5157 return DDERR_NOPALETTEATTACHED;
5159 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5160 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5163 /* Copy one surface on the other */
5164 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5165 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5167 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5169 /* Should also check for same pixel format, u1.lPitch, ... */
5170 ERR("Error in surface sizes.\n");
5171 wined3d_mutex_unlock();
5172 return D3DERR_TEXTURE_LOAD_FAILED;
5174 else
5176 struct wined3d_map_desc src_map_desc, dst_map_desc;
5178 /* Copy the src blit color key if the source has one, don't erase
5179 * the destination's ckey if the source has none */
5180 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5182 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5183 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5186 if (FAILED(hr = wined3d_resource_map(src_resource,
5187 src_surface->sub_resource_idx, &src_map_desc, NULL, 0)))
5189 ERR("Failed to lock source surface, hr %#x.\n", hr);
5190 wined3d_mutex_unlock();
5191 return D3DERR_TEXTURE_LOAD_FAILED;
5194 if (FAILED(hr = wined3d_resource_map(dst_resource,
5195 dst_surface->sub_resource_idx, &dst_map_desc, NULL, 0)))
5197 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5198 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5199 wined3d_mutex_unlock();
5200 return D3DERR_TEXTURE_LOAD_FAILED;
5203 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5204 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5205 else
5206 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5208 wined3d_resource_unmap(dst_resource, dst_surface->sub_resource_idx);
5209 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5212 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5213 src_surface = get_sub_mimaplevel(src_surface);
5214 else
5215 src_surface = NULL;
5217 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5218 dst_surface = get_sub_mimaplevel(dst_surface);
5219 else
5220 dst_surface = NULL;
5222 if (!src_surface || !dst_surface)
5224 if (src_surface != dst_surface)
5225 ERR("Loading surface with different mipmap structure.\n");
5226 break;
5230 wined3d_mutex_unlock();
5232 return hr;
5235 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5237 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5238 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5240 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5242 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5243 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5246 /*****************************************************************************
5247 * The VTable
5248 *****************************************************************************/
5250 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5252 /* IUnknown */
5253 ddraw_surface7_QueryInterface,
5254 ddraw_surface7_AddRef,
5255 ddraw_surface7_Release,
5256 /* IDirectDrawSurface */
5257 ddraw_surface7_AddAttachedSurface,
5258 ddraw_surface7_AddOverlayDirtyRect,
5259 ddraw_surface7_Blt,
5260 ddraw_surface7_BltBatch,
5261 ddraw_surface7_BltFast,
5262 ddraw_surface7_DeleteAttachedSurface,
5263 ddraw_surface7_EnumAttachedSurfaces,
5264 ddraw_surface7_EnumOverlayZOrders,
5265 ddraw_surface7_Flip,
5266 ddraw_surface7_GetAttachedSurface,
5267 ddraw_surface7_GetBltStatus,
5268 ddraw_surface7_GetCaps,
5269 ddraw_surface7_GetClipper,
5270 ddraw_surface7_GetColorKey,
5271 ddraw_surface7_GetDC,
5272 ddraw_surface7_GetFlipStatus,
5273 ddraw_surface7_GetOverlayPosition,
5274 ddraw_surface7_GetPalette,
5275 ddraw_surface7_GetPixelFormat,
5276 ddraw_surface7_GetSurfaceDesc,
5277 ddraw_surface7_Initialize,
5278 ddraw_surface7_IsLost,
5279 ddraw_surface7_Lock,
5280 ddraw_surface7_ReleaseDC,
5281 ddraw_surface7_Restore,
5282 ddraw_surface7_SetClipper,
5283 ddraw_surface7_SetColorKey,
5284 ddraw_surface7_SetOverlayPosition,
5285 ddraw_surface7_SetPalette,
5286 ddraw_surface7_Unlock,
5287 ddraw_surface7_UpdateOverlay,
5288 ddraw_surface7_UpdateOverlayDisplay,
5289 ddraw_surface7_UpdateOverlayZOrder,
5290 /* IDirectDrawSurface2 */
5291 ddraw_surface7_GetDDInterface,
5292 ddraw_surface7_PageLock,
5293 ddraw_surface7_PageUnlock,
5294 /* IDirectDrawSurface3 */
5295 ddraw_surface7_SetSurfaceDesc,
5296 /* IDirectDrawSurface4 */
5297 ddraw_surface7_SetPrivateData,
5298 ddraw_surface7_GetPrivateData,
5299 ddraw_surface7_FreePrivateData,
5300 ddraw_surface7_GetUniquenessValue,
5301 ddraw_surface7_ChangeUniquenessValue,
5302 /* IDirectDrawSurface7 */
5303 ddraw_surface7_SetPriority,
5304 ddraw_surface7_GetPriority,
5305 ddraw_surface7_SetLOD,
5306 ddraw_surface7_GetLOD,
5309 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5311 /* IUnknown */
5312 ddraw_surface4_QueryInterface,
5313 ddraw_surface4_AddRef,
5314 ddraw_surface4_Release,
5315 /* IDirectDrawSurface */
5316 ddraw_surface4_AddAttachedSurface,
5317 ddraw_surface4_AddOverlayDirtyRect,
5318 ddraw_surface4_Blt,
5319 ddraw_surface4_BltBatch,
5320 ddraw_surface4_BltFast,
5321 ddraw_surface4_DeleteAttachedSurface,
5322 ddraw_surface4_EnumAttachedSurfaces,
5323 ddraw_surface4_EnumOverlayZOrders,
5324 ddraw_surface4_Flip,
5325 ddraw_surface4_GetAttachedSurface,
5326 ddraw_surface4_GetBltStatus,
5327 ddraw_surface4_GetCaps,
5328 ddraw_surface4_GetClipper,
5329 ddraw_surface4_GetColorKey,
5330 ddraw_surface4_GetDC,
5331 ddraw_surface4_GetFlipStatus,
5332 ddraw_surface4_GetOverlayPosition,
5333 ddraw_surface4_GetPalette,
5334 ddraw_surface4_GetPixelFormat,
5335 ddraw_surface4_GetSurfaceDesc,
5336 ddraw_surface4_Initialize,
5337 ddraw_surface4_IsLost,
5338 ddraw_surface4_Lock,
5339 ddraw_surface4_ReleaseDC,
5340 ddraw_surface4_Restore,
5341 ddraw_surface4_SetClipper,
5342 ddraw_surface4_SetColorKey,
5343 ddraw_surface4_SetOverlayPosition,
5344 ddraw_surface4_SetPalette,
5345 ddraw_surface4_Unlock,
5346 ddraw_surface4_UpdateOverlay,
5347 ddraw_surface4_UpdateOverlayDisplay,
5348 ddraw_surface4_UpdateOverlayZOrder,
5349 /* IDirectDrawSurface2 */
5350 ddraw_surface4_GetDDInterface,
5351 ddraw_surface4_PageLock,
5352 ddraw_surface4_PageUnlock,
5353 /* IDirectDrawSurface3 */
5354 ddraw_surface4_SetSurfaceDesc,
5355 /* IDirectDrawSurface4 */
5356 ddraw_surface4_SetPrivateData,
5357 ddraw_surface4_GetPrivateData,
5358 ddraw_surface4_FreePrivateData,
5359 ddraw_surface4_GetUniquenessValue,
5360 ddraw_surface4_ChangeUniquenessValue,
5363 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5365 /* IUnknown */
5366 ddraw_surface3_QueryInterface,
5367 ddraw_surface3_AddRef,
5368 ddraw_surface3_Release,
5369 /* IDirectDrawSurface */
5370 ddraw_surface3_AddAttachedSurface,
5371 ddraw_surface3_AddOverlayDirtyRect,
5372 ddraw_surface3_Blt,
5373 ddraw_surface3_BltBatch,
5374 ddraw_surface3_BltFast,
5375 ddraw_surface3_DeleteAttachedSurface,
5376 ddraw_surface3_EnumAttachedSurfaces,
5377 ddraw_surface3_EnumOverlayZOrders,
5378 ddraw_surface3_Flip,
5379 ddraw_surface3_GetAttachedSurface,
5380 ddraw_surface3_GetBltStatus,
5381 ddraw_surface3_GetCaps,
5382 ddraw_surface3_GetClipper,
5383 ddraw_surface3_GetColorKey,
5384 ddraw_surface3_GetDC,
5385 ddraw_surface3_GetFlipStatus,
5386 ddraw_surface3_GetOverlayPosition,
5387 ddraw_surface3_GetPalette,
5388 ddraw_surface3_GetPixelFormat,
5389 ddraw_surface3_GetSurfaceDesc,
5390 ddraw_surface3_Initialize,
5391 ddraw_surface3_IsLost,
5392 ddraw_surface3_Lock,
5393 ddraw_surface3_ReleaseDC,
5394 ddraw_surface3_Restore,
5395 ddraw_surface3_SetClipper,
5396 ddraw_surface3_SetColorKey,
5397 ddraw_surface3_SetOverlayPosition,
5398 ddraw_surface3_SetPalette,
5399 ddraw_surface3_Unlock,
5400 ddraw_surface3_UpdateOverlay,
5401 ddraw_surface3_UpdateOverlayDisplay,
5402 ddraw_surface3_UpdateOverlayZOrder,
5403 /* IDirectDrawSurface2 */
5404 ddraw_surface3_GetDDInterface,
5405 ddraw_surface3_PageLock,
5406 ddraw_surface3_PageUnlock,
5407 /* IDirectDrawSurface3 */
5408 ddraw_surface3_SetSurfaceDesc,
5411 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5413 /* IUnknown */
5414 ddraw_surface2_QueryInterface,
5415 ddraw_surface2_AddRef,
5416 ddraw_surface2_Release,
5417 /* IDirectDrawSurface */
5418 ddraw_surface2_AddAttachedSurface,
5419 ddraw_surface2_AddOverlayDirtyRect,
5420 ddraw_surface2_Blt,
5421 ddraw_surface2_BltBatch,
5422 ddraw_surface2_BltFast,
5423 ddraw_surface2_DeleteAttachedSurface,
5424 ddraw_surface2_EnumAttachedSurfaces,
5425 ddraw_surface2_EnumOverlayZOrders,
5426 ddraw_surface2_Flip,
5427 ddraw_surface2_GetAttachedSurface,
5428 ddraw_surface2_GetBltStatus,
5429 ddraw_surface2_GetCaps,
5430 ddraw_surface2_GetClipper,
5431 ddraw_surface2_GetColorKey,
5432 ddraw_surface2_GetDC,
5433 ddraw_surface2_GetFlipStatus,
5434 ddraw_surface2_GetOverlayPosition,
5435 ddraw_surface2_GetPalette,
5436 ddraw_surface2_GetPixelFormat,
5437 ddraw_surface2_GetSurfaceDesc,
5438 ddraw_surface2_Initialize,
5439 ddraw_surface2_IsLost,
5440 ddraw_surface2_Lock,
5441 ddraw_surface2_ReleaseDC,
5442 ddraw_surface2_Restore,
5443 ddraw_surface2_SetClipper,
5444 ddraw_surface2_SetColorKey,
5445 ddraw_surface2_SetOverlayPosition,
5446 ddraw_surface2_SetPalette,
5447 ddraw_surface2_Unlock,
5448 ddraw_surface2_UpdateOverlay,
5449 ddraw_surface2_UpdateOverlayDisplay,
5450 ddraw_surface2_UpdateOverlayZOrder,
5451 /* IDirectDrawSurface2 */
5452 ddraw_surface2_GetDDInterface,
5453 ddraw_surface2_PageLock,
5454 ddraw_surface2_PageUnlock,
5457 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5459 /* IUnknown */
5460 ddraw_surface1_QueryInterface,
5461 ddraw_surface1_AddRef,
5462 ddraw_surface1_Release,
5463 /* IDirectDrawSurface */
5464 ddraw_surface1_AddAttachedSurface,
5465 ddraw_surface1_AddOverlayDirtyRect,
5466 ddraw_surface1_Blt,
5467 ddraw_surface1_BltBatch,
5468 ddraw_surface1_BltFast,
5469 ddraw_surface1_DeleteAttachedSurface,
5470 ddraw_surface1_EnumAttachedSurfaces,
5471 ddraw_surface1_EnumOverlayZOrders,
5472 ddraw_surface1_Flip,
5473 ddraw_surface1_GetAttachedSurface,
5474 ddraw_surface1_GetBltStatus,
5475 ddraw_surface1_GetCaps,
5476 ddraw_surface1_GetClipper,
5477 ddraw_surface1_GetColorKey,
5478 ddraw_surface1_GetDC,
5479 ddraw_surface1_GetFlipStatus,
5480 ddraw_surface1_GetOverlayPosition,
5481 ddraw_surface1_GetPalette,
5482 ddraw_surface1_GetPixelFormat,
5483 ddraw_surface1_GetSurfaceDesc,
5484 ddraw_surface1_Initialize,
5485 ddraw_surface1_IsLost,
5486 ddraw_surface1_Lock,
5487 ddraw_surface1_ReleaseDC,
5488 ddraw_surface1_Restore,
5489 ddraw_surface1_SetClipper,
5490 ddraw_surface1_SetColorKey,
5491 ddraw_surface1_SetOverlayPosition,
5492 ddraw_surface1_SetPalette,
5493 ddraw_surface1_Unlock,
5494 ddraw_surface1_UpdateOverlay,
5495 ddraw_surface1_UpdateOverlayDisplay,
5496 ddraw_surface1_UpdateOverlayZOrder,
5499 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5501 ddraw_gamma_control_QueryInterface,
5502 ddraw_gamma_control_AddRef,
5503 ddraw_gamma_control_Release,
5504 ddraw_gamma_control_GetGammaRamp,
5505 ddraw_gamma_control_SetGammaRamp,
5508 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5510 d3d_texture2_QueryInterface,
5511 d3d_texture2_AddRef,
5512 d3d_texture2_Release,
5513 d3d_texture2_GetHandle,
5514 d3d_texture2_PaletteChanged,
5515 d3d_texture2_Load,
5518 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5520 d3d_texture1_QueryInterface,
5521 d3d_texture1_AddRef,
5522 d3d_texture1_Release,
5523 d3d_texture1_Initialize,
5524 d3d_texture1_GetHandle,
5525 d3d_texture1_PaletteChanged,
5526 d3d_texture1_Load,
5527 d3d_texture1_Unload,
5530 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5531 * IDirectDrawSurface interface to ddraw methods. */
5532 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5534 if (!iface) return NULL;
5535 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5537 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5538 if (FAILED(hr))
5540 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5541 return NULL;
5543 IDirectDrawSurface7_Release(iface);
5545 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5548 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5550 if (!iface) return NULL;
5551 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5553 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5554 if (FAILED(hr))
5556 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5557 return NULL;
5559 IDirectDrawSurface4_Release(iface);
5561 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5564 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5566 if (!iface) return NULL;
5567 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5569 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5570 if (FAILED(hr))
5572 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5573 return NULL;
5575 IDirectDrawSurface3_Release(iface);
5577 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5580 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5582 if (!iface) return NULL;
5583 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5585 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5586 if (FAILED(hr))
5588 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5589 return NULL;
5591 IDirectDrawSurface2_Release(iface);
5593 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5596 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5598 if (!iface) return NULL;
5599 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5601 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5602 if (FAILED(hr))
5604 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5605 return NULL;
5607 IDirectDrawSurface_Release(iface);
5609 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5612 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5614 if (!iface) return NULL;
5615 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5616 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5619 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5621 if (!iface) return NULL;
5622 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5623 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5626 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5628 struct ddraw_surface *surface = parent;
5630 TRACE("surface %p.\n", surface);
5632 /* This shouldn't happen, ddraw_surface_release_iface() should prevent the
5633 * surface from being destroyed in this case. */
5634 if (surface->first_attached != surface)
5635 ERR("Surface is still attached to surface %p.\n", surface->first_attached);
5637 while (surface->next_attached)
5638 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5639 surface->next_attached, surface->next_attached->attached_iface)))
5640 ERR("DeleteAttachedSurface failed.\n");
5642 /* Having a texture handle set implies that the device still exists. */
5643 if (surface->Handle)
5644 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5646 /* Reduce the ddraw surface count. */
5647 list_remove(&surface->surface_list_entry);
5649 if (surface->clipper)
5650 IDirectDrawClipper_Release(&surface->clipper->IDirectDrawClipper_iface);
5652 if (surface == surface->ddraw->primary)
5653 surface->ddraw->primary = NULL;
5655 wined3d_private_store_cleanup(&surface->private_store);
5657 HeapFree(GetProcessHeap(), 0, surface);
5660 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5662 ddraw_surface_wined3d_object_destroyed,
5665 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5667 TRACE("parent %p.\n", parent);
5669 HeapFree(GetProcessHeap(), 0, parent);
5672 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5674 ddraw_texture_wined3d_object_destroyed,
5677 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5679 return DD_OK;
5682 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
5683 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
5685 struct wined3d_sub_resource_desc wined3d_mip_desc;
5686 struct ddraw_surface *root, *mip, **attach;
5687 struct wined3d_resource_desc wined3d_desc;
5688 struct wined3d_texture *wined3d_texture;
5689 struct wined3d_display_mode mode;
5690 DDSURFACEDESC2 *desc, *mip_desc;
5691 struct ddraw_texture *texture;
5692 unsigned int layers = 1;
5693 unsigned int pitch = 0;
5694 UINT levels, i, j;
5695 HRESULT hr;
5697 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
5698 ddraw, surface_desc, surface, outer_unknown, version);
5699 if (TRACE_ON(ddraw))
5701 TRACE("Requesting surface desc:\n");
5702 DDRAW_dump_surface_desc(surface_desc);
5705 if (outer_unknown)
5706 return CLASS_E_NOAGGREGATION;
5708 if (!surface)
5709 return E_POINTER;
5711 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
5712 return E_OUTOFMEMORY;
5714 texture->version = version;
5715 texture->surface_desc = *surface_desc;
5716 desc = &texture->surface_desc;
5718 /* Ensure DDSD_CAPS is always set. */
5719 desc->dwFlags |= DDSD_CAPS;
5721 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
5723 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->u5.dwBackBufferCount)
5725 WARN("Tried to create a flippable surface without any back buffers.\n");
5726 HeapFree(GetProcessHeap(), 0, texture);
5727 return DDERR_INVALIDCAPS;
5730 if (!(desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX))
5732 WARN("Tried to create a flippable surface without DDSCAPS_COMPLEX.\n");
5733 HeapFree(GetProcessHeap(), 0, texture);
5734 return DDERR_INVALIDCAPS;
5737 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5739 FIXME("Flippable textures not implemented.\n");
5740 HeapFree(GetProcessHeap(), 0, texture);
5741 return DDERR_INVALIDCAPS;
5744 else
5746 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
5748 WARN("Tried to specify a back buffer count for a non-flippable surface.\n");
5749 HeapFree(GetProcessHeap(), 0, texture);
5750 return DDERR_INVALIDCAPS;
5754 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5756 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5758 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
5759 HeapFree(GetProcessHeap(), 0, texture);
5760 return DDERR_INVALIDCAPS;
5763 if ((desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX) && !(desc->ddsCaps.dwCaps & DDSCAPS_FLIP))
5765 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
5766 HeapFree(GetProcessHeap(), 0, texture);
5767 return DDERR_INVALIDCAPS;
5770 if ((desc->ddsCaps.dwCaps & DDSCAPS_FLIP) && !(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
5772 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
5773 HeapFree(GetProcessHeap(), 0, texture);
5774 return DDERR_NOEXCLUSIVEMODE;
5778 /* This is a special case in ddrawex, but not allowed in ddraw. */
5779 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5780 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5782 WARN("Tried to create a surface in both system and video memory.\n");
5783 HeapFree(GetProcessHeap(), 0, texture);
5784 return DDERR_INVALIDCAPS;
5787 if ((desc->ddsCaps.dwCaps & (DDSCAPS_ALLOCONLOAD | DDSCAPS_MIPMAP))
5788 && !(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5790 WARN("Caps %#x require DDSCAPS_TEXTURE.\n", desc->ddsCaps.dwCaps);
5791 HeapFree(GetProcessHeap(), 0, texture);
5792 return DDERR_INVALIDCAPS;
5795 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
5796 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
5798 WARN("Cube map faces requested without cube map flag.\n");
5799 HeapFree(GetProcessHeap(), 0, texture);
5800 return DDERR_INVALIDCAPS;
5803 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5804 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
5806 WARN("Cube map without faces requested.\n");
5807 HeapFree(GetProcessHeap(), 0, texture);
5808 return DDERR_INVALIDPARAMS;
5811 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5812 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
5813 FIXME("Partial cube maps not implemented.\n");
5815 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
5817 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5819 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
5820 HeapFree(GetProcessHeap(), 0, texture);
5821 return DDERR_INVALIDCAPS;
5823 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5825 WARN("DDSCAPS2_TEXTUREMANAGE used width DDSCAPS_VIDEOMEMORY "
5826 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
5827 HeapFree(GetProcessHeap(), 0, texture);
5828 return DDERR_INVALIDCAPS;
5832 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
5834 ERR("Failed to get display mode, hr %#x.\n", hr);
5835 HeapFree(GetProcessHeap(), 0, texture);
5836 return hr_ddraw_from_wined3d(hr);
5839 /* No pixelformat given? Use the current screen format. */
5840 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
5842 desc->dwFlags |= DDSD_PIXELFORMAT;
5843 desc->u4.ddpfPixelFormat.dwSize = sizeof(desc->u4.ddpfPixelFormat);
5844 ddrawformat_from_wined3dformat(&desc->u4.ddpfPixelFormat, mode.format_id);
5847 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5848 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
5849 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
5851 WARN("Unsupported / unknown pixelformat.\n");
5852 HeapFree(GetProcessHeap(), 0, texture);
5853 return DDERR_INVALIDPIXELFORMAT;
5856 /* No width or no height? Use the screen size. */
5857 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
5859 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
5861 WARN("No width / height specified.\n");
5862 HeapFree(GetProcessHeap(), 0, texture);
5863 return DDERR_INVALIDPARAMS;
5866 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
5867 desc->dwWidth = mode.width;
5868 desc->dwHeight = mode.height;
5871 if (!desc->dwWidth || !desc->dwHeight)
5873 HeapFree(GetProcessHeap(), 0, texture);
5874 return DDERR_INVALIDPARAMS;
5877 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
5878 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
5880 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5882 /* The first surface is a front buffer, the back buffers are created
5883 * afterwards. */
5884 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5885 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
5887 struct wined3d_swapchain_desc swapchain_desc;
5889 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
5890 swapchain_desc.backbuffer_width = mode.width;
5891 swapchain_desc.backbuffer_height = mode.height;
5892 swapchain_desc.backbuffer_format = mode.format_id;
5894 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
5895 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
5897 ERR("Failed to reset device.\n");
5898 HeapFree(GetProcessHeap(), 0, texture);
5899 return hr_ddraw_from_wined3d(hr);
5904 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5905 wined3d_desc.multisample_quality = 0;
5906 wined3d_desc.usage = 0;
5907 wined3d_desc.pool = WINED3D_POOL_DEFAULT;
5908 wined3d_desc.width = desc->dwWidth;
5909 wined3d_desc.height = desc->dwHeight;
5910 wined3d_desc.depth = 1;
5911 wined3d_desc.size = 0;
5913 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
5915 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
5916 /* Do not fail surface creation, only fail 3D device creation. */
5919 /* Mipmap count fixes */
5920 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5922 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
5924 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
5926 /* Mipmap count is given, should not be 0. */
5927 if (!desc->u2.dwMipMapCount)
5929 HeapFree(GetProcessHeap(), 0, texture);
5930 return DDERR_INVALIDPARAMS;
5933 else
5935 /* Undocumented feature: Create sublevels until either the
5936 * width or the height is 1. */
5937 if (version == 7)
5938 desc->u2.dwMipMapCount = wined3d_log2i(max(desc->dwWidth, desc->dwHeight)) + 1;
5939 else
5940 desc->u2.dwMipMapCount = wined3d_log2i(min(desc->dwWidth, desc->dwHeight)) + 1;
5943 else
5945 desc->u2.dwMipMapCount = 1;
5948 desc->dwFlags |= DDSD_MIPMAPCOUNT;
5949 levels = desc->u2.dwMipMapCount;
5951 else
5953 levels = 1;
5956 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
5958 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
5960 DWORD usage = 0;
5962 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5963 usage |= WINED3DUSAGE_LEGACY_CUBEMAP | WINED3DUSAGE_TEXTURE;
5964 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5965 usage |= WINED3DUSAGE_TEXTURE;
5967 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5968 usage = WINED3DUSAGE_DEPTHSTENCIL;
5969 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5970 usage = WINED3DUSAGE_RENDERTARGET;
5972 if (SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
5973 WINED3D_DEVICE_TYPE_HAL, mode.format_id, usage, WINED3D_RTYPE_TEXTURE_2D, wined3d_desc.format)))
5974 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
5975 else
5976 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5978 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5980 /* Tests show surfaces without memory flags get these flags added
5981 * right after creation. */
5982 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5986 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5987 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5989 WARN("System memory overlays are not allowed.\n");
5990 HeapFree(GetProcessHeap(), 0, texture);
5991 return DDERR_NOOVERLAYHW;
5994 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5996 wined3d_desc.pool = WINED3D_POOL_SYSTEM_MEM;
5998 else
6000 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
6001 wined3d_desc.usage |= WINED3DUSAGE_TEXTURE;
6002 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
6003 wined3d_desc.usage |= WINED3DUSAGE_DEPTHSTENCIL;
6004 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
6005 wined3d_desc.usage |= WINED3DUSAGE_RENDERTARGET;
6007 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
6009 wined3d_desc.pool = WINED3D_POOL_MANAGED;
6010 /* Managed textures have the system memory flag set. */
6011 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
6013 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
6015 /* Videomemory adds localvidmem. This is mutually exclusive with
6016 * systemmemory and texturemanage. */
6017 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
6018 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
6022 if (desc->dwFlags & DDSD_LPSURFACE)
6024 if (wined3d_desc.pool != WINED3D_POOL_SYSTEM_MEM)
6026 WARN("User memory surfaces should be in the system memory pool.\n");
6027 HeapFree(GetProcessHeap(), 0, texture);
6028 return DDERR_INVALIDCAPS;
6031 if (version < 4)
6033 WARN("User memory surfaces not supported before version 4.\n");
6034 HeapFree(GetProcessHeap(), 0, texture);
6035 return DDERR_INVALIDPARAMS;
6038 if (!desc->lpSurface)
6040 WARN("NULL surface memory pointer specified.\n");
6041 HeapFree(GetProcessHeap(), 0, texture);
6042 return DDERR_INVALIDPARAMS;
6045 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6047 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
6049 WARN("Pitch specified on a compressed user memory surface.\n");
6050 HeapFree(GetProcessHeap(), 0, texture);
6051 return DDERR_INVALIDPARAMS;
6054 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
6056 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
6057 HeapFree(GetProcessHeap(), 0, texture);
6058 return DDERR_INVALIDPARAMS;
6061 if ((desc->dwFlags & DDSD_LINEARSIZE)
6062 && desc->u1.dwLinearSize < wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
6063 wined3d_desc.format, wined3d_desc.width) * ((desc->dwHeight + 3) / 4))
6065 WARN("Invalid linear size %u specified.\n", desc->u1.dwLinearSize);
6066 HeapFree(GetProcessHeap(), 0, texture);
6067 return DDERR_INVALIDPARAMS;
6070 else
6072 if (!(desc->dwFlags & DDSD_PITCH))
6074 WARN("User memory surfaces should explicitly specify the pitch.\n");
6075 HeapFree(GetProcessHeap(), 0, texture);
6076 return DDERR_INVALIDPARAMS;
6079 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
6080 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
6082 WARN("Invalid pitch %u specified.\n", desc->u1.lPitch);
6083 HeapFree(GetProcessHeap(), 0, texture);
6084 return DDERR_INVALIDPARAMS;
6087 pitch = desc->u1.lPitch;
6091 if (((desc->dwFlags & DDSD_CKDESTOVERLAY)
6092 && desc->u3.ddckCKDestOverlay.dwColorSpaceLowValue != desc->u3.ddckCKDestOverlay.dwColorSpaceHighValue)
6093 || ((desc->dwFlags & DDSD_CKDESTBLT)
6094 && desc->ddckCKDestBlt.dwColorSpaceLowValue != desc->ddckCKDestBlt.dwColorSpaceHighValue)
6095 || ((desc->dwFlags & DDSD_CKSRCOVERLAY)
6096 && desc->ddckCKSrcOverlay.dwColorSpaceLowValue != desc->ddckCKSrcOverlay.dwColorSpaceHighValue)
6097 || ((desc->dwFlags & DDSD_CKSRCBLT)
6098 && desc->ddckCKSrcBlt.dwColorSpaceLowValue != desc->ddckCKSrcBlt.dwColorSpaceHighValue))
6100 WARN("Range color keys not supported, returning DDERR_NOCOLORKEYHW.\n");
6101 HeapFree(GetProcessHeap(), 0, texture);
6102 return DDERR_NOCOLORKEYHW;
6105 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
6106 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
6108 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
6109 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
6111 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6113 wined3d_desc.usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6114 layers = 6;
6117 /* Some applications assume surfaces will always be mapped at the same
6118 * address. Some of those also assume that this address is valid even when
6119 * the surface isn't mapped, and that updates done this way will be
6120 * visible on the screen. The game Nox is such an application,
6121 * Commandos: Behind Enemy Lines is another. Setting
6122 * WINED3D_TEXTURE_CREATE_GET_DC_LENIENT will ensure this. */
6123 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, levels,
6124 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, texture,
6125 &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
6127 WARN("Failed to create wined3d texture, hr %#x.\n", hr);
6128 HeapFree(GetProcessHeap(), 0, texture);
6129 return hr_ddraw_from_wined3d(hr);
6132 root = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6133 wined3d_texture_decref(wined3d_texture);
6134 root->is_complex_root = TRUE;
6135 texture->root = root;
6137 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6138 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6139 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6140 if (desc->dwFlags & DDSD_CKDESTBLT)
6141 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6142 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6143 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6144 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6145 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6146 if (desc->dwFlags & DDSD_CKSRCBLT)
6147 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6148 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6150 for (i = 0; i < layers; ++i)
6152 attach = &root->complex_array[layers - 1 - i];
6154 for (j = 0; j < levels; ++j)
6156 mip = wined3d_texture_get_sub_resource_parent(wined3d_texture, i * levels + j);
6157 mip_desc = &mip->surface_desc;
6159 if (j)
6161 wined3d_texture_get_sub_resource_desc(wined3d_texture, i * levels + j, &wined3d_mip_desc);
6162 mip_desc->dwWidth = wined3d_mip_desc.width;
6163 mip_desc->dwHeight = wined3d_mip_desc.height;
6165 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6167 else
6169 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6172 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6174 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6176 switch (i)
6178 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6179 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6180 break;
6181 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6182 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6183 break;
6184 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6185 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6186 break;
6187 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6188 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6189 break;
6190 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6191 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6192 break;
6193 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6194 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6195 break;
6200 if (mip == root)
6201 continue;
6203 *attach = mip;
6204 attach = &mip->complex_array[0];
6208 if ((desc->dwFlags & DDSD_LPSURFACE) && FAILED(hr = wined3d_texture_update_desc(wined3d_texture,
6209 wined3d_desc.width, wined3d_desc.height, wined3d_desc.format,
6210 WINED3D_MULTISAMPLE_NONE, 0, desc->lpSurface, pitch)))
6212 ERR("Failed to set surface memory, hr %#x.\n", hr);
6213 goto fail;
6216 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6218 unsigned int count = desc->u5.dwBackBufferCount;
6219 struct ddraw_surface *last = root;
6221 attach = &last->complex_array[0];
6222 for (i = 0; i < count; ++i)
6224 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
6226 hr = E_OUTOFMEMORY;
6227 goto fail;
6230 texture->version = version;
6231 texture->surface_desc = root->surface_desc;
6232 desc = &texture->surface_desc;
6234 /* Only one surface in the flipping chain is a back buffer, one is
6235 * a front buffer, the others are just flippable surfaces. */
6236 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6237 | DDSCAPS_BACKBUFFER);
6238 if (!i)
6239 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6240 desc->u5.dwBackBufferCount = 0;
6242 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, 1,
6243 WINED3D_TEXTURE_CREATE_GET_DC_LENIENT, NULL, texture,
6244 &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
6246 HeapFree(GetProcessHeap(), 0, texture);
6247 hr = hr_ddraw_from_wined3d(hr);
6248 goto fail;
6251 last = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
6252 wined3d_texture_decref(wined3d_texture);
6253 texture->root = last;
6255 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6256 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6257 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6258 if (desc->dwFlags & DDSD_CKDESTBLT)
6259 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6260 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6261 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6262 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6263 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6264 if (desc->dwFlags & DDSD_CKSRCBLT)
6265 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6266 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6268 *attach = last;
6269 attach = &last->complex_array[0];
6271 *attach = root;
6274 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6275 ddraw->primary = root;
6276 *surface = root;
6278 return DD_OK;
6280 fail:
6281 if (version == 7)
6282 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6283 else if (version == 4)
6284 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6285 else
6286 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6288 return hr;
6291 void ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw,
6292 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
6293 const struct wined3d_parent_ops **parent_ops)
6295 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
6296 unsigned int texture_level, row_pitch, slice_pitch;
6297 DDSURFACEDESC2 *desc = &surface->surface_desc;
6298 unsigned int version = texture->version;
6300 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6301 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6302 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6303 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6304 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6305 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6306 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6307 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6308 surface->iface_count = 1;
6309 surface->version = version;
6310 surface->ddraw = ddraw;
6312 if (version == 7)
6314 surface->ref7 = 1;
6315 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6317 else if (version == 4)
6319 surface->ref4 = 1;
6320 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6322 else
6324 surface->ref1 = 1;
6325 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6328 *desc = texture->surface_desc;
6329 surface->first_attached = surface;
6331 texture_level = desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP ? sub_resource_idx % desc->u2.dwMipMapCount : 0;
6332 wined3d_texture_get_pitch(wined3d_texture, texture_level, &row_pitch, &slice_pitch);
6333 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6335 if (desc->dwFlags & DDSD_LPSURFACE)
6336 desc->u1.dwLinearSize = ~0u;
6337 else
6338 desc->u1.dwLinearSize = slice_pitch;
6339 desc->dwFlags |= DDSD_LINEARSIZE;
6340 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6342 else
6344 if (!(desc->dwFlags & DDSD_LPSURFACE))
6345 desc->u1.lPitch = row_pitch;
6346 desc->dwFlags |= DDSD_PITCH;
6347 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6349 desc->lpSurface = NULL;
6351 wined3d_texture_incref(surface->wined3d_texture = wined3d_texture);
6352 surface->sub_resource_idx = sub_resource_idx;
6353 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6355 wined3d_private_store_init(&surface->private_store);
6358 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
6360 struct ddraw_surface *surface = parent;
6362 /* If the surface reference count drops to zero, we release our reference
6363 * to the view, but don't clear the pointer yet, in case e.g. a
6364 * GetRenderTarget() call brings the surface back before the view is
6365 * actually destroyed. When the view is destroyed, we need to clear the
6366 * pointer, or a subsequent surface AddRef() would reference it again.
6368 * This is safe because as long as the view still has a reference to the
6369 * texture, the surface is also still alive, and we're called before the
6370 * view releases that reference. */
6371 surface->wined3d_rtv = NULL;
6374 static const struct wined3d_parent_ops ddraw_view_wined3d_parent_ops =
6376 view_wined3d_object_destroyed,
6379 struct wined3d_rendertarget_view *ddraw_surface_get_rendertarget_view(struct ddraw_surface *surface)
6381 HRESULT hr;
6383 if (surface->wined3d_rtv)
6384 return surface->wined3d_rtv;
6386 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
6387 surface->sub_resource_idx, surface, &ddraw_view_wined3d_parent_ops, &surface->wined3d_rtv)))
6389 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
6390 return NULL;
6393 return surface->wined3d_rtv;