wined3d: Get rid of the unused _WINEDDOVERLAYFX structure.
[wine.git] / dlls / ddraw / surface.c
blobdc6ad253624b67bed1a55184439aa885fc7ef480
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 = InterlockedDecrement(&This->iface_count);
535 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
537 if (iface_count == 0)
539 IUnknown *release_iface = This->ifaceToRelease;
541 /* Complex attached surfaces are destroyed implicitly when the root is released */
542 wined3d_mutex_lock();
543 if(!This->is_complex_root)
545 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
546 wined3d_mutex_unlock();
547 return iface_count;
549 ddraw_surface_cleanup(This);
550 wined3d_mutex_unlock();
552 if (release_iface)
553 IUnknown_Release(release_iface);
556 return iface_count;
559 /*****************************************************************************
560 * IDirectDrawSurface7::Release
562 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
563 * surface is destroyed.
565 * Destroying the surface is a bit tricky. For the connection between
566 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
567 * It has a nice graph explaining the connection.
569 * What happens here is basically this:
570 * When a surface is destroyed, its WineD3DSurface is released,
571 * and the refcount of the DirectDraw interface is reduced by 1. If it has
572 * complex surfaces attached to it, then these surfaces are destroyed too,
573 * regardless of their refcount. If any surface being destroyed has another
574 * surface attached to it (with a "soft" attachment, not complex), then
575 * this surface is detached with DeleteAttachedSurface.
577 * When the surface is a texture, the WineD3DTexture is released.
578 * If the surface is the Direct3D render target, then the D3D
579 * capabilities of the WineD3DDevice are uninitialized, which causes the
580 * swapchain to be released.
582 * When a complex sublevel falls to ref zero, then this is ignored.
584 * Returns:
585 * The new refcount
587 *****************************************************************************/
588 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
590 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
591 ULONG refcount = InterlockedDecrement(&This->ref7);
593 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
595 if (refcount == 0)
597 ddraw_surface_release_iface(This);
600 return refcount;
603 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
605 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
606 ULONG refcount = InterlockedDecrement(&This->ref4);
608 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
610 if (refcount == 0)
612 ddraw_surface_release_iface(This);
615 return refcount;
618 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
620 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
621 ULONG refcount = InterlockedDecrement(&This->ref3);
623 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
625 if (refcount == 0)
627 ddraw_surface_release_iface(This);
630 return refcount;
633 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
635 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
636 ULONG refcount = InterlockedDecrement(&This->ref2);
638 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
640 if (refcount == 0)
642 ddraw_surface_release_iface(This);
645 return refcount;
648 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
650 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
651 ULONG refcount = InterlockedDecrement(&This->ref1);
653 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
655 if (refcount == 0)
657 ddraw_surface_release_iface(This);
660 return refcount;
663 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
665 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
666 ULONG refcount = InterlockedDecrement(&This->gamma_count);
668 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
670 if (refcount == 0)
672 ddraw_surface_release_iface(This);
675 return refcount;
678 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
680 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
682 TRACE("iface %p.\n", iface);
684 return IUnknown_Release(surface->texture_outer);
687 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
689 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
691 TRACE("iface %p.\n", iface);
693 return IUnknown_Release(surface->texture_outer);
696 /*****************************************************************************
697 * IDirectDrawSurface7::GetAttachedSurface
699 * Returns an attached surface with the requested caps. Surface attachment
700 * and complex surfaces are not clearly described by the MSDN or sdk,
701 * so this method is tricky and likely to contain problems.
702 * This implementation searches the complex list first, then the
703 * attachment chain.
705 * The chains are searched from This down to the last surface in the chain,
706 * not from the first element in the chain. The first surface found is
707 * returned. The MSDN says that this method fails if more than one surface
708 * matches the caps, but it is not sure if that is right. The attachment
709 * structure may not even allow two matching surfaces.
711 * The found surface is AddRef-ed before it is returned.
713 * Params:
714 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
715 * Surface: Address to store the found surface
717 * Returns:
718 * DD_OK on success
719 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
720 * DDERR_NOTFOUND if no surface was found
722 *****************************************************************************/
723 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
724 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
726 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
727 struct ddraw_surface *surf;
728 DDSCAPS2 our_caps;
729 int i;
731 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
733 wined3d_mutex_lock();
735 if(This->version < 7)
737 /* Earlier dx apps put garbage into these members, clear them */
738 our_caps.dwCaps = Caps->dwCaps;
739 our_caps.dwCaps2 = 0;
740 our_caps.dwCaps3 = 0;
741 our_caps.u1.dwCaps4 = 0;
743 else
745 our_caps = *Caps;
748 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 */
750 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
752 surf = This->complex_array[i];
753 if(!surf) break;
755 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
756 surf->surface_desc.ddsCaps.dwCaps,
757 surf->surface_desc.ddsCaps.dwCaps2,
758 surf->surface_desc.ddsCaps.dwCaps3,
759 surf->surface_desc.ddsCaps.u1.dwCaps4);
761 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
762 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
764 /* MSDN: "This method fails if more than one surface is attached
765 * that matches the capabilities requested."
767 * Not sure how to test this.
770 TRACE("(%p): Returning surface %p\n", This, surf);
771 *Surface = &surf->IDirectDrawSurface7_iface;
772 ddraw_surface7_AddRef(*Surface);
773 wined3d_mutex_unlock();
775 return DD_OK;
779 /* Next, look at the attachment chain */
780 surf = This;
782 while( (surf = surf->next_attached) )
784 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
785 surf->surface_desc.ddsCaps.dwCaps,
786 surf->surface_desc.ddsCaps.dwCaps2,
787 surf->surface_desc.ddsCaps.dwCaps3,
788 surf->surface_desc.ddsCaps.u1.dwCaps4);
790 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
791 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
793 TRACE("(%p): Returning surface %p\n", This, surf);
794 *Surface = &surf->IDirectDrawSurface7_iface;
795 ddraw_surface7_AddRef(*Surface);
796 wined3d_mutex_unlock();
797 return DD_OK;
801 TRACE("(%p) Didn't find a valid surface\n", This);
803 wined3d_mutex_unlock();
805 *Surface = NULL;
806 return DDERR_NOTFOUND;
809 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
810 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
812 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
813 struct ddraw_surface *attachment_impl;
814 IDirectDrawSurface7 *attachment7;
815 HRESULT hr;
817 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
819 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
820 caps, &attachment7);
821 if (FAILED(hr))
823 *attachment = NULL;
824 return hr;
826 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
827 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
828 ddraw_surface4_AddRef(*attachment);
829 ddraw_surface7_Release(attachment7);
831 return hr;
834 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
835 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
837 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
838 struct ddraw_surface *attachment_impl;
839 IDirectDrawSurface7 *attachment7;
840 DDSCAPS2 caps2;
841 HRESULT hr;
843 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
845 caps2.dwCaps = caps->dwCaps;
846 caps2.dwCaps2 = 0;
847 caps2.dwCaps3 = 0;
848 caps2.u1.dwCaps4 = 0;
850 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
851 &caps2, &attachment7);
852 if (FAILED(hr))
854 *attachment = NULL;
855 return hr;
857 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
858 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
859 ddraw_surface3_AddRef(*attachment);
860 ddraw_surface7_Release(attachment7);
862 return hr;
865 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
866 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
868 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
869 struct ddraw_surface *attachment_impl;
870 IDirectDrawSurface7 *attachment7;
871 DDSCAPS2 caps2;
872 HRESULT hr;
874 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
876 caps2.dwCaps = caps->dwCaps;
877 caps2.dwCaps2 = 0;
878 caps2.dwCaps3 = 0;
879 caps2.u1.dwCaps4 = 0;
881 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
882 &caps2, &attachment7);
883 if (FAILED(hr))
885 *attachment = NULL;
886 return hr;
888 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
889 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
890 ddraw_surface2_AddRef(*attachment);
891 ddraw_surface7_Release(attachment7);
893 return hr;
896 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
897 DDSCAPS *caps, IDirectDrawSurface **attachment)
899 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
900 struct ddraw_surface *attachment_impl;
901 IDirectDrawSurface7 *attachment7;
902 DDSCAPS2 caps2;
903 HRESULT hr;
905 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
907 caps2.dwCaps = caps->dwCaps;
908 caps2.dwCaps2 = 0;
909 caps2.dwCaps3 = 0;
910 caps2.u1.dwCaps4 = 0;
912 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
913 &caps2, &attachment7);
914 if (FAILED(hr))
916 *attachment = NULL;
917 return hr;
919 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
920 *attachment = &attachment_impl->IDirectDrawSurface_iface;
921 ddraw_surface1_AddRef(*attachment);
922 ddraw_surface7_Release(attachment7);
924 return hr;
927 /*****************************************************************************
928 * IDirectDrawSurface7::Lock
930 * Locks the surface and returns a pointer to the surface's memory
932 * Params:
933 * Rect: Rectangle to lock. If NULL, the whole surface is locked
934 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
935 * Flags: Locking flags, e.g Read only or write only
936 * h: An event handle that's not used and must be NULL
938 * Returns:
939 * DD_OK on success
940 * DDERR_INVALIDPARAMS if DDSD is NULL
941 * For more details, see IWineD3DSurface::LockRect
943 *****************************************************************************/
944 static HRESULT surface_lock(struct ddraw_surface *surface,
945 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
947 struct wined3d_box box;
948 struct wined3d_map_desc map_desc;
949 HRESULT hr = DD_OK;
951 TRACE("surface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
952 surface, wine_dbgstr_rect(rect), surface_desc, flags, h);
954 /* surface->surface_desc.dwWidth and dwHeight are changeable, thus lock */
955 wined3d_mutex_lock();
957 /* Should I check for the handle to be NULL?
959 * The DDLOCK flags and the D3DLOCK flags are equal
960 * for the supported values. The others are ignored by WineD3D
963 /* Windows zeroes this if the rect is invalid */
964 surface_desc->lpSurface = NULL;
966 if (rect)
968 if ((rect->left < 0) || (rect->top < 0)
969 || (rect->left > rect->right) || (rect->right > surface->surface_desc.dwWidth)
970 || (rect->top > rect->bottom) || (rect->bottom > surface->surface_desc.dwHeight))
972 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
973 wined3d_mutex_unlock();
974 return DDERR_INVALIDPARAMS;
976 box.left = rect->left;
977 box.top = rect->top;
978 box.right = rect->right;
979 box.bottom = rect->bottom;
980 box.front = 0;
981 box.back = 1;
984 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
985 hr = ddraw_surface_update_frontbuffer(surface, rect, TRUE);
986 if (SUCCEEDED(hr))
987 hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture),
988 surface->sub_resource_idx, &map_desc, rect ? &box : NULL, flags);
989 if (FAILED(hr))
991 wined3d_mutex_unlock();
992 switch(hr)
994 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
995 * specific error. But since IWineD3DSurface::LockRect returns that error in this
996 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
997 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
998 * is much easier to do it in one place in ddraw
1000 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
1001 default: return hr;
1005 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1007 if (flags & DDLOCK_READONLY)
1008 memset(&surface->ddraw->primary_lock, 0, sizeof(surface->ddraw->primary_lock));
1009 else if (rect)
1010 surface->ddraw->primary_lock = *rect;
1011 else
1012 SetRect(&surface->ddraw->primary_lock, 0, 0, surface->surface_desc.dwWidth, surface->surface_desc.dwHeight);
1015 /* Windows does not set DDSD_LPSURFACE on locked surfaces. */
1016 DD_STRUCT_COPY_BYSIZE(surface_desc, &surface->surface_desc);
1017 surface_desc->lpSurface = map_desc.data;
1019 TRACE("locked surface returning description :\n");
1020 if (TRACE_ON(ddraw))
1021 DDRAW_dump_surface_desc(surface_desc);
1023 wined3d_mutex_unlock();
1025 return DD_OK;
1028 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1029 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1031 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1033 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1034 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1036 if (!surface_desc) return DDERR_INVALIDPARAMS;
1037 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1038 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1040 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1041 return DDERR_INVALIDPARAMS;
1043 return surface_lock(surface, rect, surface_desc, flags, h);
1046 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1047 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1049 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1051 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1052 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1054 if (!surface_desc) return DDERR_INVALIDPARAMS;
1055 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1056 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1058 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1059 return DDERR_INVALIDPARAMS;
1061 return surface_lock(surface, rect, surface_desc, flags, h);
1064 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1065 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1067 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1068 DDSURFACEDESC2 surface_desc2;
1069 HRESULT hr;
1071 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1072 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1074 if (!surface_desc) return DDERR_INVALIDPARAMS;
1075 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1076 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1078 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1079 return DDERR_INVALIDPARAMS;
1082 surface_desc2.dwSize = surface_desc->dwSize;
1083 surface_desc2.dwFlags = 0;
1084 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1085 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1086 surface_desc->dwSize = surface_desc2.dwSize;
1087 return hr;
1090 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1091 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1093 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1094 DDSURFACEDESC2 surface_desc2;
1095 HRESULT hr;
1097 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1098 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1100 if (!surface_desc) return DDERR_INVALIDPARAMS;
1101 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1102 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1104 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1105 return DDERR_INVALIDPARAMS;
1108 surface_desc2.dwSize = surface_desc->dwSize;
1109 surface_desc2.dwFlags = 0;
1110 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1111 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1112 surface_desc->dwSize = surface_desc2.dwSize;
1113 return hr;
1116 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1117 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1119 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1120 DDSURFACEDESC2 surface_desc2;
1121 HRESULT hr;
1122 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1123 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1125 if (!surface_desc) return DDERR_INVALIDPARAMS;
1126 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1127 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1129 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1130 return DDERR_INVALIDPARAMS;
1133 surface_desc2.dwSize = surface_desc->dwSize;
1134 surface_desc2.dwFlags = 0;
1135 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1136 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1137 surface_desc->dwSize = surface_desc2.dwSize;
1138 return hr;
1141 /*****************************************************************************
1142 * IDirectDrawSurface7::Unlock
1144 * Unlocks an locked surface
1146 * Params:
1147 * Rect: Not used by this implementation
1149 * Returns:
1150 * D3D_OK on success
1151 * For more details, see IWineD3DSurface::UnlockRect
1153 *****************************************************************************/
1154 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
1156 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1157 HRESULT hr;
1159 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1161 wined3d_mutex_lock();
1162 hr = wined3d_resource_unmap(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx);
1163 if (SUCCEEDED(hr) && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1164 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE);
1165 wined3d_mutex_unlock();
1167 return hr;
1170 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1172 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1174 TRACE("iface %p, rect %p.\n", iface, pRect);
1176 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, pRect);
1179 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1181 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1183 TRACE("iface %p, data %p.\n", iface, data);
1185 /* data might not be the LPRECT of later versions, so drop it. */
1186 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1189 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1191 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1193 TRACE("iface %p, data %p.\n", iface, data);
1195 /* data might not be the LPRECT of later versions, so drop it. */
1196 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1199 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1201 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1203 TRACE("iface %p, data %p.\n", iface, data);
1205 /* data might not be the LPRECT of later versions, so drop it. */
1206 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1209 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Flip(IDirectDrawSurface7 *iface,
1210 IDirectDrawSurface7 *src, DWORD flags)
1212 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
1213 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src);
1214 struct wined3d_rendertarget_view *tmp_rtv, *src_rtv, *rtv;
1215 struct ddraw_texture *ddraw_texture, *prev_ddraw_texture;
1216 DDSCAPS2 caps = {DDSCAPS_FLIP, 0, 0, {0}};
1217 struct wined3d_texture *texture;
1218 IDirectDrawSurface7 *current;
1219 HRESULT hr;
1221 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1223 if (src == iface || !(dst_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1224 return DDERR_NOTFLIPPABLE;
1226 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
1227 return DDERR_SURFACELOST;
1229 wined3d_mutex_lock();
1231 if (!(dst_impl->ddraw->cooperative_level & DDSCL_EXCLUSIVE))
1233 WARN("Not in exclusive mode.\n");
1234 wined3d_mutex_unlock();
1235 return DDERR_NOEXCLUSIVEMODE;
1238 tmp_rtv = ddraw_surface_get_rendertarget_view(dst_impl);
1239 if (dst_impl->sub_resource_idx)
1240 ERR("Invalid sub-resource index %u on surface %p.\n", dst_impl->sub_resource_idx, dst_impl);
1241 texture = dst_impl->wined3d_texture;
1242 rtv = wined3d_device_get_rendertarget_view(dst_impl->ddraw->wined3d_device, 0);
1243 ddraw_texture = wined3d_texture_get_parent(dst_impl->wined3d_texture);
1245 if (src_impl)
1247 for (current = iface; current != src;)
1249 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1251 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1252 wined3d_mutex_unlock();
1253 return DDERR_NOTFLIPPABLE;
1255 ddraw_surface7_Release(current);
1256 if (current == iface)
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;
1264 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1265 if (rtv == dst_impl->wined3d_rtv)
1266 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1267 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1268 dst_impl->wined3d_rtv = src_rtv;
1269 wined3d_resource_set_parent(wined3d_texture_get_sub_resource(src_impl->wined3d_texture, 0), dst_impl);
1270 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1271 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1272 if (src_impl->sub_resource_idx)
1273 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1274 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1275 ddraw_texture = prev_ddraw_texture;
1277 else
1279 for (current = iface;;)
1281 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1283 ERR("Can't find a flip target\n");
1284 wined3d_mutex_unlock();
1285 return DDERR_NOTFLIPPABLE; /* Unchecked */
1287 ddraw_surface7_Release(current);
1288 if (current == iface)
1290 dst_impl = impl_from_IDirectDrawSurface7(iface);
1291 break;
1294 src_impl = impl_from_IDirectDrawSurface7(current);
1295 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1296 if (rtv == dst_impl->wined3d_rtv)
1297 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1298 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1299 dst_impl->wined3d_rtv = src_rtv;
1300 wined3d_resource_set_parent(wined3d_texture_get_sub_resource(src_impl->wined3d_texture, 0), dst_impl);
1301 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1302 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1303 ddraw_texture = prev_ddraw_texture;
1304 if (src_impl->sub_resource_idx)
1305 ERR("Invalid sub-resource index %u on surface %p.\n", src_impl->sub_resource_idx, src_impl);
1306 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1307 dst_impl = src_impl;
1311 /* We don't have to worry about potential texture bindings, since
1312 * flippable surfaces can never be textures. */
1313 if (rtv == src_impl->wined3d_rtv)
1314 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, tmp_rtv, FALSE);
1315 wined3d_rendertarget_view_set_parent(tmp_rtv, src_impl);
1316 src_impl->wined3d_rtv = tmp_rtv;
1317 wined3d_resource_set_parent(wined3d_texture_get_sub_resource(texture, 0), src_impl);
1318 wined3d_resource_set_parent(wined3d_texture_get_resource(texture), ddraw_texture);
1319 src_impl->wined3d_texture = texture;
1321 if (flags)
1323 static UINT once;
1324 if (!once++)
1325 FIXME("Ignoring flags %#x.\n", flags);
1326 else
1327 WARN("Ignoring flags %#x.\n", flags);
1330 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1331 hr = ddraw_surface_update_frontbuffer(dst_impl, NULL, FALSE);
1332 else
1333 hr = DD_OK;
1335 wined3d_mutex_unlock();
1337 return hr;
1340 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Flip(IDirectDrawSurface4 *iface,
1341 IDirectDrawSurface4 *dst, DWORD flags)
1343 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1344 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1346 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1348 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1349 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1352 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Flip(IDirectDrawSurface3 *iface,
1353 IDirectDrawSurface3 *dst, DWORD flags)
1355 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1356 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1358 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1360 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1361 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1364 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Flip(IDirectDrawSurface2 *iface,
1365 IDirectDrawSurface2 *dst, DWORD flags)
1367 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1368 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1370 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1372 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1373 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1376 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Flip(IDirectDrawSurface *iface,
1377 IDirectDrawSurface *dst, DWORD flags)
1379 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1380 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1382 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1384 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1385 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1388 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1389 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags,
1390 const WINEDDBLTFX *fx, enum wined3d_texture_filter_type filter)
1392 struct wined3d_texture *wined3d_src_texture;
1393 unsigned int src_sub_resource_idx;
1394 RECT src_rect, dst_rect;
1395 float scale_x, scale_y;
1396 const RECT *clip_rect;
1397 UINT clip_list_size;
1398 RGNDATA *clip_list;
1399 HRESULT hr = DD_OK;
1400 UINT i;
1402 if (!dst_rect_in)
1404 dst_rect.left = 0;
1405 dst_rect.top = 0;
1406 dst_rect.right = dst_surface->surface_desc.dwWidth;
1407 dst_rect.bottom = dst_surface->surface_desc.dwHeight;
1409 else
1411 dst_rect = *dst_rect_in;
1414 if (IsRectEmpty(&dst_rect))
1415 return DDERR_INVALIDRECT;
1417 if (src_surface)
1419 if (!src_rect_in)
1421 src_rect.left = 0;
1422 src_rect.top = 0;
1423 src_rect.right = src_surface->surface_desc.dwWidth;
1424 src_rect.bottom = src_surface->surface_desc.dwHeight;
1426 else
1428 src_rect = *src_rect_in;
1431 if (IsRectEmpty(&src_rect))
1432 return DDERR_INVALIDRECT;
1434 wined3d_src_texture = src_surface->wined3d_texture;
1435 src_sub_resource_idx = src_surface->sub_resource_idx;
1437 else
1439 SetRect(&src_rect, 0, 0, 0, 0);
1440 wined3d_src_texture = NULL;
1441 src_sub_resource_idx = 0;
1444 if (!dst_surface->clipper)
1446 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1447 hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect, TRUE);
1448 if (SUCCEEDED(hr))
1449 hr = wined3d_texture_blt(dst_surface->wined3d_texture, dst_surface->sub_resource_idx, &dst_rect,
1450 wined3d_src_texture, src_sub_resource_idx, &src_rect, flags, fx, filter);
1451 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
1452 hr = ddraw_surface_update_frontbuffer(dst_surface, &dst_rect, FALSE);
1454 return hr;
1457 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1458 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1460 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1461 &dst_rect, NULL, &clip_list_size)))
1463 WARN("Failed to get clip list size, hr %#x.\n", hr);
1464 return hr;
1467 if (!(clip_list = HeapAlloc(GetProcessHeap(), 0, clip_list_size)))
1469 WARN("Failed to allocate clip list.\n");
1470 return E_OUTOFMEMORY;
1473 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1474 &dst_rect, clip_list, &clip_list_size)))
1476 WARN("Failed to get clip list, hr %#x.\n", hr);
1477 HeapFree(GetProcessHeap(), 0, clip_list);
1478 return hr;
1481 clip_rect = (RECT *)clip_list->Buffer;
1482 for (i = 0; i < clip_list->rdh.nCount; ++i)
1484 RECT src_rect_clipped = src_rect;
1486 if (src_surface)
1488 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1489 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1490 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1491 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1493 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1495 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE)))
1496 break;
1500 if (FAILED(hr = wined3d_texture_blt(dst_surface->wined3d_texture, dst_surface->sub_resource_idx,
1501 &clip_rect[i], wined3d_src_texture, src_sub_resource_idx, &src_rect_clipped, flags, fx, filter)))
1502 break;
1504 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1506 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE)))
1507 break;
1511 HeapFree(GetProcessHeap(), 0, clip_list);
1512 return hr;
1515 /*****************************************************************************
1516 * IDirectDrawSurface7::Blt
1518 * Performs a blit on the surface
1520 * Params:
1521 * DestRect: Destination rectangle, can be NULL
1522 * SrcSurface: Source surface, can be NULL
1523 * SrcRect: Source rectangle, can be NULL
1524 * Flags: Blt flags
1525 * DDBltFx: Some extended blt parameters, connected to the flags
1527 * Returns:
1528 * D3D_OK on success
1529 * See IWineD3DSurface::Blt for more details
1531 *****************************************************************************/
1532 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1533 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1535 struct ddraw_surface *dst_surface = impl_from_IDirectDrawSurface7(iface);
1536 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1537 HRESULT hr = DD_OK;
1538 DDBLTFX rop_fx;
1540 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1541 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1543 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1544 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1546 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1547 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1548 return DDERR_INVALIDPARAMS;
1551 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1552 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1553 return DDERR_INVALIDPARAMS;
1556 wined3d_mutex_lock();
1558 if (Flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1560 if (Flags & DDBLT_ROP)
1562 wined3d_mutex_unlock();
1563 WARN("DDBLT_ROP used with DDBLT_COLORFILL or DDBLT_DEPTHFILL, returning DDERR_INVALIDPARAMS.\n");
1564 return DDERR_INVALIDPARAMS;
1566 if (src_surface)
1568 wined3d_mutex_unlock();
1569 WARN("Depth or colorfill is not compatible with source surfaces, returning DDERR_INVALIDPARAMS\n");
1570 return DDERR_INVALIDPARAMS;
1572 if (!DDBltFx)
1574 wined3d_mutex_unlock();
1575 WARN("Depth or colorfill used with DDBltFx = NULL, returning DDERR_INVALIDPARAMS.\n");
1576 return DDERR_INVALIDPARAMS;
1579 if ((Flags & (DDBLT_COLORFILL | DDBLT_DEPTHFILL)) == (DDBLT_COLORFILL | DDBLT_DEPTHFILL))
1580 Flags &= ~DDBLT_DEPTHFILL;
1582 if ((dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (Flags & DDBLT_COLORFILL))
1584 wined3d_mutex_unlock();
1585 WARN("DDBLT_COLORFILL used on a depth buffer, returning DDERR_INVALIDPARAMS.\n");
1586 return DDERR_INVALIDPARAMS;
1588 if (!(dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER) && (Flags & DDBLT_DEPTHFILL))
1590 wined3d_mutex_unlock();
1591 WARN("DDBLT_DEPTHFILL used on a color buffer, returning DDERR_INVALIDPARAMS.\n");
1592 return DDERR_INVALIDPARAMS;
1596 if (Flags & DDBLT_ROP)
1598 if (!DDBltFx)
1600 wined3d_mutex_unlock();
1601 WARN("DDBLT_ROP used with DDBltFx = NULL, returning DDERR_INVALIDPARAMS.\n");
1602 return DDERR_INVALIDPARAMS;
1605 Flags &= ~DDBLT_ROP;
1606 switch (DDBltFx->dwROP)
1608 case SRCCOPY:
1609 break;
1611 case WHITENESS:
1612 case BLACKNESS:
1613 rop_fx = *DDBltFx;
1615 if (DDBltFx->dwROP == WHITENESS)
1616 rop_fx.u5.dwFillColor = 0xffffffff;
1617 else
1618 rop_fx.u5.dwFillColor = 0;
1620 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
1621 Flags |= DDBLT_DEPTHFILL;
1622 else
1623 Flags |= DDBLT_COLORFILL;
1625 DDBltFx = &rop_fx;
1626 break;
1628 default:
1629 wined3d_mutex_unlock();
1630 WARN("Unsupported ROP %#x used, returning DDERR_NORASTEROPHW.\n", DDBltFx->dwROP);
1631 return DDERR_NORASTEROPHW;
1635 if (Flags & DDBLT_KEYSRC && (!src_surface || !(src_surface->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1637 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1638 wined3d_mutex_unlock();
1639 return DDERR_INVALIDPARAMS;
1642 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1643 * does, copy the struct, and replace the ddraw surfaces with the wined3d
1644 * surfaces. So far no blitting operations using surfaces in the bltfx
1645 * struct are supported anyway. */
1646 hr = ddraw_surface_blt_clipped(dst_surface, DestRect, src_surface, SrcRect,
1647 Flags, (WINEDDBLTFX *)DDBltFx, WINED3D_TEXF_LINEAR);
1649 wined3d_mutex_unlock();
1650 switch(hr)
1652 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1653 default: return hr;
1657 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1658 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1660 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1661 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1663 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1664 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1666 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1667 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1670 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1671 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1673 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1674 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1676 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1677 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1679 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1680 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1683 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1684 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1686 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1687 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1689 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1690 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1692 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1693 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1696 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1697 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1699 struct ddraw_surface *dst = impl_from_IDirectDrawSurface(iface);
1700 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1702 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1703 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1705 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1706 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1709 /*****************************************************************************
1710 * IDirectDrawSurface7::AddAttachedSurface
1712 * Attaches a surface to another surface. How the surface attachments work
1713 * is not totally understood yet, and this method is prone to problems.
1714 * The surface that is attached is AddRef-ed.
1716 * Tests with complex surfaces suggest that the surface attachments form a
1717 * tree, but no method to test this has been found yet.
1719 * The attachment list consists of a first surface (first_attached) and
1720 * for each surface a pointer to the next attached surface (next_attached).
1721 * For the first surface, and a surface that has no attachments
1722 * first_attached points to the surface itself. A surface that has
1723 * no successors in the chain has next_attached set to NULL.
1725 * Newly attached surfaces are attached right after the root surface.
1726 * If a surface is attached to a complex surface compound, it's attached to
1727 * the surface that the app requested, not the complex root. See
1728 * GetAttachedSurface for a description how surfaces are found.
1730 * This is how the current implementation works, and it was coded by looking
1731 * at the needs of the applications.
1733 * So far only Z-Buffer attachments are tested, and they are activated in
1734 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1735 * Back buffers should work in 2D mode, but they are not tested(They can be
1736 * attached in older iface versions). Rendering to the front buffer and
1737 * switching between that and double buffering is not yet implemented in
1738 * WineD3D, so for 3D it might have unexpected results.
1740 * ddraw_surface_attach_surface is the real thing,
1741 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1742 * performs additional checks. Version 7 of this interface is much more restrictive
1743 * than its predecessors.
1745 * Params:
1746 * Attach: Surface to attach to iface
1748 * Returns:
1749 * DD_OK on success
1750 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1752 *****************************************************************************/
1753 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1755 TRACE("surface %p, attachment %p.\n", This, Surf);
1757 if(Surf == This)
1758 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1760 wined3d_mutex_lock();
1762 /* Check if the surface is already attached somewhere */
1763 if (Surf->next_attached || Surf->first_attached != Surf)
1765 /* TODO: Test for the structure of the manual attachment. Is it a
1766 * chain or a list? What happens if one surface is attached to 2
1767 * different surfaces? */
1768 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1769 Surf, Surf->next_attached, Surf->first_attached);
1771 wined3d_mutex_unlock();
1772 return DDERR_SURFACEALREADYATTACHED;
1775 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1776 Surf->next_attached = This->next_attached;
1777 Surf->first_attached = This->first_attached;
1778 This->next_attached = Surf;
1780 /* Check if the WineD3D depth stencil needs updating */
1781 if (This->ddraw->d3ddevice)
1782 d3d_device_update_depth_stencil(This->ddraw->d3ddevice);
1784 wined3d_mutex_unlock();
1786 return DD_OK;
1789 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
1791 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
1792 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1793 HRESULT hr;
1795 TRACE("iface %p, attachment %p.\n", iface, attachment);
1797 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1798 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1801 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1802 attachment_impl->surface_desc.ddsCaps.dwCaps);
1803 return DDERR_CANNOTATTACHSURFACE;
1806 hr = ddraw_surface_attach_surface(This, attachment_impl);
1807 if (FAILED(hr))
1809 return hr;
1811 attachment_impl->attached_iface = (IUnknown *)attachment;
1812 IUnknown_AddRef(attachment_impl->attached_iface);
1813 return hr;
1816 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1818 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1819 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1820 HRESULT hr;
1822 TRACE("iface %p, attachment %p.\n", iface, attachment);
1824 /* Tests suggest that
1825 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1826 * -> offscreen plain surfaces can be attached to primaries
1827 * -> primaries can be attached to offscreen plain surfaces
1828 * -> z buffers can be attached to primaries */
1829 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1830 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1832 /* Sizes have to match */
1833 if (attachment_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
1834 || attachment_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
1836 WARN("Surface sizes do not match.\n");
1837 return DDERR_CANNOTATTACHSURFACE;
1840 else if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
1841 || !(attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER)))
1843 WARN("Invalid attachment combination.\n");
1844 return DDERR_CANNOTATTACHSURFACE;
1847 if (FAILED(hr = ddraw_surface_attach_surface(surface, attachment_impl)))
1848 return hr;
1850 attachment_impl->attached_iface = (IUnknown *)attachment;
1851 IUnknown_AddRef(attachment_impl->attached_iface);
1852 return hr;
1855 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1857 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1858 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1859 HRESULT hr;
1861 TRACE("iface %p, attachment %p.\n", iface, attachment);
1863 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1864 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1865 return hr;
1867 attachment_impl->attached_iface = (IUnknown *)attachment;
1868 IUnknown_AddRef(attachment_impl->attached_iface);
1869 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1870 return hr;
1873 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1875 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1876 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1877 HRESULT hr;
1879 TRACE("iface %p, attachment %p.\n", iface, attachment);
1881 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1882 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1883 return hr;
1885 attachment_impl->attached_iface = (IUnknown *)attachment;
1886 IUnknown_AddRef(attachment_impl->attached_iface);
1887 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1888 return hr;
1891 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1893 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1894 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1895 HRESULT hr;
1897 TRACE("iface %p, attachment %p.\n", iface, attachment);
1899 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1900 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1901 return hr;
1903 attachment_impl->attached_iface = (IUnknown *)attachment;
1904 IUnknown_AddRef(attachment_impl->attached_iface);
1905 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1906 return hr;
1909 /*****************************************************************************
1910 * IDirectDrawSurface7::DeleteAttachedSurface
1912 * Removes a surface from the attachment chain. The surface's refcount
1913 * is decreased by one after it has been removed
1915 * Params:
1916 * Flags: Some flags, not used by this implementation
1917 * Attach: Surface to detach
1919 * Returns:
1920 * DD_OK on success
1921 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1923 *****************************************************************************/
1924 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
1925 struct ddraw_surface *attachment, IUnknown *detach_iface)
1927 struct ddraw_surface *prev = surface;
1929 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
1931 wined3d_mutex_lock();
1932 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
1934 wined3d_mutex_unlock();
1935 return DDERR_CANNOTDETACHSURFACE;
1938 if (attachment->attached_iface != detach_iface)
1940 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
1941 wined3d_mutex_unlock();
1942 return DDERR_SURFACENOTATTACHED;
1945 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1946 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1948 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1949 /* FIXME: we should probably also subtract from dwMipMapCount of this
1950 * and all parent surfaces */
1953 /* Find the predecessor of the detached surface */
1954 while (prev)
1956 if (prev->next_attached == attachment)
1957 break;
1958 prev = prev->next_attached;
1961 /* There must be a surface, otherwise there's a bug */
1962 assert(prev);
1964 /* Unchain the surface */
1965 prev->next_attached = attachment->next_attached;
1966 attachment->next_attached = NULL;
1967 attachment->first_attached = attachment;
1969 /* Check if the wined3d depth stencil needs updating. */
1970 if (surface->ddraw->d3ddevice)
1971 d3d_device_update_depth_stencil(surface->ddraw->d3ddevice);
1972 wined3d_mutex_unlock();
1974 /* Set attached_iface to NULL before releasing it, the surface may go
1975 * away. */
1976 attachment->attached_iface = NULL;
1977 IUnknown_Release(detach_iface);
1979 return DD_OK;
1982 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1983 DWORD flags, IDirectDrawSurface7 *attachment)
1985 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1986 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1988 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1990 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1993 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1994 DWORD flags, IDirectDrawSurface4 *attachment)
1996 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1997 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1999 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2001 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2004 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
2005 DWORD flags, IDirectDrawSurface3 *attachment)
2007 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2008 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
2010 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2012 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2015 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
2016 DWORD flags, IDirectDrawSurface2 *attachment)
2018 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2019 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
2021 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2023 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2026 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
2027 DWORD flags, IDirectDrawSurface *attachment)
2029 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2030 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
2032 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
2034 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
2037 /*****************************************************************************
2038 * IDirectDrawSurface7::AddOverlayDirtyRect
2040 * "This method is not currently implemented"
2042 * Params:
2043 * Rect: ?
2045 * Returns:
2046 * DDERR_UNSUPPORTED
2048 *****************************************************************************/
2049 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
2051 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
2053 return DDERR_UNSUPPORTED; /* unchecked */
2056 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
2058 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2060 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2062 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2065 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
2067 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2069 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2071 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2074 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
2076 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2078 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2080 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2083 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
2085 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2087 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
2089 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
2092 /*****************************************************************************
2093 * IDirectDrawSurface7::GetDC
2095 * Returns a GDI device context for the surface
2097 * Params:
2098 * hdc: Address of a HDC variable to store the dc to
2100 * Returns:
2101 * DD_OK on success
2102 * DDERR_INVALIDPARAMS if hdc is NULL
2103 * For details, see IWineD3DSurface::GetDC
2105 *****************************************************************************/
2106 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *dc)
2108 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2109 HRESULT hr = DD_OK;
2111 TRACE("iface %p, dc %p.\n", iface, dc);
2113 if (!dc)
2114 return DDERR_INVALIDPARAMS;
2116 wined3d_mutex_lock();
2117 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2118 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE);
2119 if (SUCCEEDED(hr))
2120 hr = wined3d_texture_get_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
2122 if (SUCCEEDED(hr))
2124 surface->dc = *dc;
2126 if (format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
2128 const struct ddraw_palette *palette;
2130 if (surface->palette)
2131 palette = surface->palette;
2132 else if (surface->ddraw->primary)
2133 palette = surface->ddraw->primary->palette;
2134 else
2135 palette = NULL;
2137 if (palette)
2138 wined3d_palette_apply_to_dc(palette->wineD3DPalette, *dc);
2142 wined3d_mutex_unlock();
2143 switch (hr)
2145 /* Some, but not all errors set *dc to NULL. E.g. DCALREADYCREATED
2146 * does not touch *dc. */
2147 case WINED3DERR_INVALIDCALL:
2148 *dc = NULL;
2149 return DDERR_INVALIDPARAMS;
2151 default:
2152 return hr;
2156 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
2158 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2160 TRACE("iface %p, dc %p.\n", iface, dc);
2162 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2165 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
2167 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2169 TRACE("iface %p, dc %p.\n", iface, dc);
2171 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2174 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2176 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2178 TRACE("iface %p, dc %p.\n", iface, dc);
2180 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2183 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2185 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2187 TRACE("iface %p, dc %p.\n", iface, dc);
2189 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2192 /*****************************************************************************
2193 * IDirectDrawSurface7::ReleaseDC
2195 * Releases the DC that was constructed with GetDC
2197 * Params:
2198 * hdc: HDC to release
2200 * Returns:
2201 * DD_OK on success
2202 * For more details, see IWineD3DSurface::ReleaseDC
2204 *****************************************************************************/
2205 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2207 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2208 HRESULT hr;
2210 TRACE("iface %p, dc %p.\n", iface, hdc);
2212 wined3d_mutex_lock();
2213 if (SUCCEEDED(hr = wined3d_texture_release_dc(surface->wined3d_texture, surface->sub_resource_idx, hdc)))
2215 surface->dc = NULL;
2216 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2217 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
2219 wined3d_mutex_unlock();
2222 return hr;
2225 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2227 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2229 TRACE("iface %p, dc %p.\n", iface, dc);
2231 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2234 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2236 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2238 TRACE("iface %p, dc %p.\n", iface, dc);
2240 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2243 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2245 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2247 TRACE("iface %p, dc %p.\n", iface, dc);
2249 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2252 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2254 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2256 TRACE("iface %p, dc %p.\n", iface, dc);
2258 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2261 /*****************************************************************************
2262 * IDirectDrawSurface7::GetCaps
2264 * Returns the surface's caps
2266 * Params:
2267 * Caps: Address to write the caps to
2269 * Returns:
2270 * DD_OK on success
2271 * DDERR_INVALIDPARAMS if Caps is NULL
2273 *****************************************************************************/
2274 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2276 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2278 TRACE("iface %p, caps %p.\n", iface, Caps);
2280 if(!Caps)
2281 return DDERR_INVALIDPARAMS;
2283 *Caps = surface->surface_desc.ddsCaps;
2285 return DD_OK;
2288 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2290 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2292 TRACE("iface %p, caps %p.\n", iface, caps);
2294 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2297 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2299 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2300 DDSCAPS2 caps2;
2301 HRESULT hr;
2303 TRACE("iface %p, caps %p.\n", iface, caps);
2305 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2306 if (FAILED(hr)) return hr;
2308 caps->dwCaps = caps2.dwCaps;
2309 return hr;
2312 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2314 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2315 DDSCAPS2 caps2;
2316 HRESULT hr;
2318 TRACE("iface %p, caps %p.\n", iface, caps);
2320 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2321 if (FAILED(hr)) return hr;
2323 caps->dwCaps = caps2.dwCaps;
2324 return hr;
2327 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2329 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(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_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD priority)
2344 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2345 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2346 HRESULT hr;
2347 struct wined3d_resource *resource;
2349 TRACE("iface %p, priority %u.\n", iface, priority);
2351 wined3d_mutex_lock();
2352 /* No need to check for offscreen plain surfaces or mipmap sublevels. SetPriority
2353 * calls on such surfaces segfault on Windows. */
2354 if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed))
2356 WARN("Called on non-managed texture returning DDERR_INVALIDPARAMS.\n");
2357 hr = DDERR_INVALIDPARAMS;
2359 else
2361 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2362 wined3d_resource_set_priority(resource, priority);
2363 hr = DD_OK;
2365 wined3d_mutex_unlock();
2367 return hr;
2370 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *priority)
2372 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2373 const struct wined3d_resource *resource;
2374 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2375 HRESULT hr;
2377 TRACE("iface %p, priority %p.\n", iface, priority);
2379 wined3d_mutex_lock();
2380 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN)
2382 WARN("Called on offscreenplain surface, returning DDERR_INVALIDOBJECT.\n");
2383 hr = DDERR_INVALIDOBJECT;
2385 else if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed) || !surface->is_complex_root)
2387 WARN("Called on non-managed texture or non-root surface, returning DDERR_INVALIDPARAMS.\n");
2388 hr = DDERR_INVALIDPARAMS;
2390 else
2392 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2393 *priority = wined3d_resource_get_priority(resource);
2394 hr = DD_OK;
2396 wined3d_mutex_unlock();
2398 return hr;
2401 /*****************************************************************************
2402 * IDirectDrawSurface7::SetPrivateData
2404 * Stores some data in the surface that is intended for the application's
2405 * use.
2407 * Params:
2408 * tag: GUID that identifies the data
2409 * Data: Pointer to the private data
2410 * Size: Size of the private data
2411 * Flags: Some flags
2413 * Returns:
2414 * D3D_OK on success
2415 * For more details, see IWineD3DSurface::SetPrivateData
2417 *****************************************************************************/
2418 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2419 REFGUID tag, void *data, DWORD size, DWORD flags)
2421 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2422 HRESULT hr;
2424 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2425 iface, debugstr_guid(tag), data, size, flags);
2427 if (!data)
2429 WARN("data is NULL, returning DDERR_INVALIDPARAMS.\n");
2430 return DDERR_INVALIDPARAMS;
2433 wined3d_mutex_lock();
2434 hr = wined3d_private_store_set_private_data(&surface->private_store, tag, data, size, flags);
2435 wined3d_mutex_unlock();
2436 return hr_ddraw_from_wined3d(hr);
2439 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2440 REFGUID tag, void *data, DWORD size, DWORD flags)
2442 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2444 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2445 iface, debugstr_guid(tag), data, size, flags);
2447 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2450 /*****************************************************************************
2451 * IDirectDrawSurface7::GetPrivateData
2453 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2455 * Params:
2456 * tag: GUID of the data to return
2457 * Data: Address where to write the data to
2458 * Size: Size of the buffer at Data
2460 * Returns:
2461 * DD_OK on success
2462 * DDERR_INVALIDPARAMS if Data is NULL
2463 * For more details, see IWineD3DSurface::GetPrivateData
2465 *****************************************************************************/
2466 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *data, DWORD *size)
2468 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2469 const struct wined3d_private_data *stored_data;
2470 HRESULT hr;
2472 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2473 iface, debugstr_guid(tag), data, size);
2475 wined3d_mutex_lock();
2476 stored_data = wined3d_private_store_get_private_data(&surface->private_store, tag);
2477 if (!stored_data)
2479 hr = DDERR_NOTFOUND;
2480 goto done;
2482 if (!size)
2484 hr = DDERR_INVALIDPARAMS;
2485 goto done;
2487 if (*size < stored_data->size)
2489 *size = stored_data->size;
2490 hr = DDERR_MOREDATA;
2491 goto done;
2493 if (!data)
2495 hr = DDERR_INVALIDPARAMS;
2496 goto done;
2499 *size = stored_data->size;
2500 memcpy(data, stored_data->content.data, stored_data->size);
2501 hr = DD_OK;
2503 done:
2504 wined3d_mutex_unlock();
2505 return hr;
2508 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2510 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2512 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2513 iface, debugstr_guid(tag), data, size);
2515 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2518 /*****************************************************************************
2519 * IDirectDrawSurface7::FreePrivateData
2521 * Frees private data stored in the surface
2523 * Params:
2524 * tag: Tag of the data to free
2526 * Returns:
2527 * D3D_OK on success
2528 * For more details, see IWineD3DSurface::FreePrivateData
2530 *****************************************************************************/
2531 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2533 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2534 struct wined3d_private_data *entry;
2536 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2538 wined3d_mutex_lock();
2539 entry = wined3d_private_store_get_private_data(&surface->private_store, tag);
2540 if (!entry)
2542 wined3d_mutex_unlock();
2543 return DDERR_NOTFOUND;
2546 wined3d_private_store_free_private_data(&surface->private_store, entry);
2547 wined3d_mutex_unlock();
2549 return DD_OK;
2552 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2554 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2556 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2558 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2561 /*****************************************************************************
2562 * IDirectDrawSurface7::PageLock
2564 * Prevents a sysmem surface from being paged out
2566 * Params:
2567 * Flags: Not used, must be 0(unchecked)
2569 * Returns:
2570 * DD_OK, because it's a stub
2572 *****************************************************************************/
2573 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2575 TRACE("iface %p, flags %#x.\n", iface, Flags);
2577 /* This is Windows memory management related - we don't need this */
2578 return DD_OK;
2581 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2583 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2585 TRACE("iface %p, flags %#x.\n", iface, flags);
2587 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2590 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2592 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2594 TRACE("iface %p, flags %#x.\n", iface, flags);
2596 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2599 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2601 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2603 TRACE("iface %p, flags %#x.\n", iface, flags);
2605 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2608 /*****************************************************************************
2609 * IDirectDrawSurface7::PageUnlock
2611 * Allows a sysmem surface to be paged out
2613 * Params:
2614 * Flags: Not used, must be 0(unchecked)
2616 * Returns:
2617 * DD_OK, because it's a stub
2619 *****************************************************************************/
2620 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2622 TRACE("iface %p, flags %#x.\n", iface, Flags);
2624 return DD_OK;
2627 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2629 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2631 TRACE("iface %p, flags %#x.\n", iface, flags);
2633 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2636 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2638 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2640 TRACE("iface %p, flags %#x.\n", iface, flags);
2642 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2645 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2647 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2649 TRACE("iface %p, flags %#x.\n", iface, flags);
2651 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2654 /*****************************************************************************
2655 * IDirectDrawSurface7::BltBatch
2657 * An unimplemented function
2659 * Params:
2662 * Returns:
2663 * DDERR_UNSUPPORTED
2665 *****************************************************************************/
2666 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2668 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2670 /* MSDN: "not currently implemented" */
2671 return DDERR_UNSUPPORTED;
2674 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2676 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2678 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2680 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2683 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2685 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2687 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2689 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2692 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2694 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2696 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2698 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2701 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2703 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2705 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2707 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2710 /*****************************************************************************
2711 * IDirectDrawSurface7::EnumAttachedSurfaces
2713 * Enumerates all surfaces attached to this surface
2715 * Params:
2716 * context: Pointer to pass unmodified to the callback
2717 * cb: Callback function to call for each surface
2719 * Returns:
2720 * DD_OK on success
2721 * DDERR_INVALIDPARAMS if cb is NULL
2723 *****************************************************************************/
2724 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2725 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2727 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2728 struct ddraw_surface *surf;
2729 DDSURFACEDESC2 desc;
2730 int i;
2732 /* Attached surfaces aren't handled in WineD3D */
2733 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2735 if(!cb)
2736 return DDERR_INVALIDPARAMS;
2738 wined3d_mutex_lock();
2740 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2742 surf = surface->complex_array[i];
2743 if(!surf) break;
2745 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2746 desc = surf->surface_desc;
2747 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2748 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2750 wined3d_mutex_unlock();
2751 return DD_OK;
2755 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2757 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2758 desc = surf->surface_desc;
2759 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2760 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2762 wined3d_mutex_unlock();
2763 return DD_OK;
2767 TRACE(" end of enumeration.\n");
2769 wined3d_mutex_unlock();
2771 return DD_OK;
2774 struct callback_info2
2776 LPDDENUMSURFACESCALLBACK2 callback;
2777 void *context;
2780 struct callback_info
2782 LPDDENUMSURFACESCALLBACK callback;
2783 void *context;
2786 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2788 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2789 const struct callback_info2 *info = context;
2791 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
2792 ddraw_surface7_Release(surface);
2794 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
2797 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2799 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2800 const struct callback_info *info = context;
2802 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
2803 ddraw_surface7_Release(surface);
2805 /* FIXME: Check surface_test.dwSize */
2806 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2807 (DDSURFACEDESC *)surface_desc, info->context);
2810 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2811 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2813 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2814 struct callback_info2 info;
2816 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2818 info.callback = callback;
2819 info.context = context;
2821 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2822 &info, EnumCallback2);
2825 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2826 void *context, LPDDENUMSURFACESCALLBACK callback)
2828 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2829 struct callback_info info;
2831 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2833 info.callback = callback;
2834 info.context = context;
2836 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2837 &info, EnumCallback);
2840 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2841 void *context, LPDDENUMSURFACESCALLBACK callback)
2843 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2844 struct callback_info 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, EnumCallback);
2855 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2856 void *context, LPDDENUMSURFACESCALLBACK callback)
2858 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(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 /*****************************************************************************
2871 * IDirectDrawSurface7::EnumOverlayZOrders
2873 * "Enumerates the overlay surfaces on the specified destination"
2875 * Params:
2876 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2877 * context: context to pass back to the callback
2878 * cb: callback function to call for each enumerated surface
2880 * Returns:
2881 * DD_OK, because it's a stub
2883 *****************************************************************************/
2884 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2885 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2887 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2889 return DD_OK;
2892 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2893 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2895 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2896 struct callback_info2 info;
2898 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2900 info.callback = callback;
2901 info.context = context;
2903 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2904 flags, &info, EnumCallback2);
2907 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2908 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2910 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2911 struct callback_info info;
2913 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2915 info.callback = callback;
2916 info.context = context;
2918 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2919 flags, &info, EnumCallback);
2922 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2923 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2925 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2926 struct callback_info 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, EnumCallback);
2937 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2938 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2940 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(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 /*****************************************************************************
2953 * IDirectDrawSurface7::GetBltStatus
2955 * Returns the blitting status
2957 * Params:
2958 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2960 * Returns:
2961 * See IWineD3DSurface::Blt
2963 *****************************************************************************/
2964 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2966 TRACE("iface %p, flags %#x.\n", iface, Flags);
2968 switch (Flags)
2970 case WINEDDGBS_CANBLT:
2971 case WINEDDGBS_ISBLTDONE:
2972 return DD_OK;
2974 default:
2975 return DDERR_INVALIDPARAMS;
2979 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2981 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2983 TRACE("iface %p, flags %#x.\n", iface, flags);
2985 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2988 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2990 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2992 TRACE("iface %p, flags %#x.\n", iface, flags);
2994 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2997 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2999 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3001 TRACE("iface %p, flags %#x.\n", iface, flags);
3003 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3006 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
3008 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3010 TRACE("iface %p, flags %#x.\n", iface, flags);
3012 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
3015 /*****************************************************************************
3016 * IDirectDrawSurface7::GetColorKey
3018 * Returns the color key assigned to the surface
3020 * Params:
3021 * Flags: Some flags
3022 * CKey: Address to store the key to
3024 * Returns:
3025 * DD_OK on success
3026 * DDERR_INVALIDPARAMS if CKey is NULL
3028 *****************************************************************************/
3029 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
3031 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3033 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
3035 if(!CKey)
3036 return DDERR_INVALIDPARAMS;
3038 wined3d_mutex_lock();
3040 switch (Flags)
3042 case DDCKEY_DESTBLT:
3043 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
3045 wined3d_mutex_unlock();
3046 return DDERR_NOCOLORKEY;
3048 *CKey = This->surface_desc.ddckCKDestBlt;
3049 break;
3051 case DDCKEY_DESTOVERLAY:
3052 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
3054 wined3d_mutex_unlock();
3055 return DDERR_NOCOLORKEY;
3057 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
3058 break;
3060 case DDCKEY_SRCBLT:
3061 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
3063 wined3d_mutex_unlock();
3064 return DDERR_NOCOLORKEY;
3066 *CKey = This->surface_desc.ddckCKSrcBlt;
3067 break;
3069 case DDCKEY_SRCOVERLAY:
3070 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
3072 wined3d_mutex_unlock();
3073 return DDERR_NOCOLORKEY;
3075 *CKey = This->surface_desc.ddckCKSrcOverlay;
3076 break;
3078 default:
3079 wined3d_mutex_unlock();
3080 return DDERR_INVALIDPARAMS;
3083 wined3d_mutex_unlock();
3085 return DD_OK;
3088 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
3090 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3092 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3094 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3097 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
3099 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3101 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3103 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3106 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
3108 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3110 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3112 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3115 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
3117 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3119 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3121 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3124 /*****************************************************************************
3125 * IDirectDrawSurface7::GetFlipStatus
3127 * Returns the flipping status of the surface
3129 * Params:
3130 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
3132 * Returns:
3133 * See IWineD3DSurface::GetFlipStatus
3135 *****************************************************************************/
3136 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3138 TRACE("iface %p, flags %#x.\n", iface, Flags);
3140 /* XXX: DDERR_INVALIDSURFACETYPE */
3142 switch (Flags)
3144 case WINEDDGFS_CANFLIP:
3145 case WINEDDGFS_ISFLIPDONE:
3146 return DD_OK;
3148 default:
3149 return DDERR_INVALIDPARAMS;
3153 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
3155 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3157 TRACE("iface %p, flags %#x.\n", iface, flags);
3159 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3162 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
3164 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3166 TRACE("iface %p, flags %#x.\n", iface, flags);
3168 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3171 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
3173 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3175 TRACE("iface %p, flags %#x.\n", iface, flags);
3177 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3180 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
3182 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3184 TRACE("iface %p, flags %#x.\n", iface, flags);
3186 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3189 /*****************************************************************************
3190 * IDirectDrawSurface7::GetOverlayPosition
3192 * Returns the display coordinates of a visible and active overlay surface
3194 * Params:
3198 * Returns:
3199 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3200 *****************************************************************************/
3201 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *x, LONG *y)
3203 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3204 HRESULT hr;
3206 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3208 wined3d_mutex_lock();
3209 hr = wined3d_texture_get_overlay_position(surface->wined3d_texture,
3210 surface->sub_resource_idx, x, y);
3211 wined3d_mutex_unlock();
3213 return hr;
3216 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3218 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3220 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3222 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3225 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3227 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3229 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3231 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3234 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3236 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3238 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3240 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3243 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3245 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3247 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3249 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3252 /*****************************************************************************
3253 * IDirectDrawSurface7::GetPixelFormat
3255 * Returns the pixel format of the Surface
3257 * Params:
3258 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3259 * format should be written
3261 * Returns:
3262 * DD_OK on success
3263 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3265 *****************************************************************************/
3266 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3268 /* What is DDERR_INVALIDSURFACETYPE for here? */
3269 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3271 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3273 if(!PixelFormat)
3274 return DDERR_INVALIDPARAMS;
3276 wined3d_mutex_lock();
3277 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3278 wined3d_mutex_unlock();
3280 return DD_OK;
3283 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3285 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3287 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3289 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3292 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3294 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3296 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3298 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3301 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3303 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3305 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3307 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3310 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3312 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3314 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3316 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3319 /*****************************************************************************
3320 * IDirectDrawSurface7::GetSurfaceDesc
3322 * Returns the description of this surface
3324 * Params:
3325 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3326 * surface desc
3328 * Returns:
3329 * DD_OK on success
3330 * DDERR_INVALIDPARAMS if DDSD is NULL
3332 *****************************************************************************/
3333 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3335 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3337 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3339 if(!DDSD)
3340 return DDERR_INVALIDPARAMS;
3342 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3344 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3345 return DDERR_INVALIDPARAMS;
3348 wined3d_mutex_lock();
3349 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3350 TRACE("Returning surface desc:\n");
3351 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3352 wined3d_mutex_unlock();
3354 return DD_OK;
3357 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3359 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3361 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3363 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3366 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3368 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3370 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3372 if (!surface_desc) return DDERR_INVALIDPARAMS;
3374 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3376 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3377 return DDERR_INVALIDPARAMS;
3380 wined3d_mutex_lock();
3381 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3382 TRACE("Returning surface desc:\n");
3383 if (TRACE_ON(ddraw))
3385 /* DDRAW_dump_surface_desc handles the smaller size */
3386 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3388 wined3d_mutex_unlock();
3390 return DD_OK;
3393 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3395 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3397 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3399 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3402 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3404 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3406 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3408 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3411 /*****************************************************************************
3412 * IDirectDrawSurface7::Initialize
3414 * Initializes the surface. This is a no-op in Wine
3416 * Params:
3417 * DD: Pointer to an DirectDraw interface
3418 * DDSD: Surface description for initialization
3420 * Returns:
3421 * DDERR_ALREADYINITIALIZED
3423 *****************************************************************************/
3424 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3425 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3427 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3429 return DDERR_ALREADYINITIALIZED;
3432 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3433 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3435 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3437 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3439 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3440 ddraw, surface_desc);
3443 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3444 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3446 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3447 DDSURFACEDESC2 surface_desc2;
3449 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3451 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3452 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3453 ddraw, surface_desc ? &surface_desc2 : NULL);
3456 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3457 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3459 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3460 DDSURFACEDESC2 surface_desc2;
3462 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3464 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3465 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3466 ddraw, surface_desc ? &surface_desc2 : NULL);
3469 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3470 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3472 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3473 DDSURFACEDESC2 surface_desc2;
3475 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3477 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3478 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3479 ddraw, surface_desc ? &surface_desc2 : NULL);
3482 /*****************************************************************************
3483 * IDirect3DTexture1::Initialize
3485 * The sdk says it's not implemented
3487 * Params:
3490 * Returns
3491 * DDERR_UNSUPPORTED
3493 *****************************************************************************/
3494 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3495 IDirect3DDevice *device, IDirectDrawSurface *surface)
3497 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3499 return DDERR_UNSUPPORTED; /* Unchecked */
3502 /*****************************************************************************
3503 * IDirectDrawSurface7::IsLost
3505 * Checks if the surface is lost
3507 * Returns:
3508 * DD_OK, if the surface is usable
3509 * DDERR_ISLOST if the surface is lost
3510 * See IWineD3DSurface::IsLost for more details
3512 *****************************************************************************/
3513 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3515 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3517 TRACE("iface %p.\n", iface);
3519 if (surface->ddraw->device_state != DDRAW_DEVICE_STATE_OK || surface->is_lost)
3520 return DDERR_SURFACELOST;
3522 return DD_OK;
3525 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3527 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3529 TRACE("iface %p.\n", iface);
3531 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3534 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3536 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3538 TRACE("iface %p.\n", iface);
3540 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3543 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3545 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3547 TRACE("iface %p.\n", iface);
3549 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3552 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3554 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3556 TRACE("iface %p.\n", iface);
3558 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3561 /*****************************************************************************
3562 * IDirectDrawSurface7::Restore
3564 * Restores a lost surface. This makes the surface usable again, but
3565 * doesn't reload its old contents
3567 * Returns:
3568 * DD_OK on success
3569 * See IWineD3DSurface::Restore for more details
3571 *****************************************************************************/
3572 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3574 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3576 TRACE("iface %p.\n", iface);
3578 ddraw_update_lost_surfaces(surface->ddraw);
3579 surface->is_lost = FALSE;
3581 return DD_OK;
3584 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3586 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3588 TRACE("iface %p.\n", iface);
3590 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3593 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3595 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3597 TRACE("iface %p.\n", iface);
3599 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3602 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3604 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3606 TRACE("iface %p.\n", iface);
3608 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3611 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3613 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3615 TRACE("iface %p.\n", iface);
3617 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3620 /*****************************************************************************
3621 * IDirectDrawSurface7::SetOverlayPosition
3623 * Changes the display coordinates of an overlay surface
3625 * Params:
3626 * X:
3627 * Y:
3629 * Returns:
3630 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3631 *****************************************************************************/
3632 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG x, LONG y)
3634 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3635 HRESULT hr;
3637 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3639 wined3d_mutex_lock();
3640 hr = wined3d_texture_set_overlay_position(surface->wined3d_texture,
3641 surface->sub_resource_idx, x, y);
3642 wined3d_mutex_unlock();
3644 return hr;
3647 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3649 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3651 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3653 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3656 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3658 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3660 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3662 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3665 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3667 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3669 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3671 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3674 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3676 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3678 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3680 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3683 /*****************************************************************************
3684 * IDirectDrawSurface7::UpdateOverlay
3686 * Modifies the attributes of an overlay surface.
3688 * Params:
3689 * SrcRect: The section of the source being used for the overlay
3690 * DstSurface: Address of the surface that is overlaid
3691 * DstRect: Place of the overlay
3692 * Flags: some DDOVER_* flags
3694 * Returns:
3695 * DDERR_UNSUPPORTED, because we don't support overlays
3697 *****************************************************************************/
3698 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *src_rect,
3699 IDirectDrawSurface7 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3701 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3702 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(dst_surface);
3703 struct wined3d_texture *dst_wined3d_texture = NULL;
3704 unsigned int dst_sub_resource_idx = 0;
3705 HRESULT hr;
3707 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3708 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3710 if (fx)
3711 FIXME("Ignoring fx %p.\n", fx);
3713 wined3d_mutex_lock();
3714 if (dst_impl)
3716 dst_wined3d_texture = dst_impl->wined3d_texture;
3717 dst_sub_resource_idx = dst_impl->sub_resource_idx;
3719 hr = wined3d_texture_update_overlay(src_impl->wined3d_texture, src_impl->sub_resource_idx,
3720 src_rect, dst_wined3d_texture, dst_sub_resource_idx, dst_rect, flags);
3721 wined3d_mutex_unlock();
3723 switch (hr)
3725 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3726 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3727 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3728 default:
3729 return hr;
3733 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3734 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3736 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3737 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3739 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3740 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3742 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3743 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3746 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3747 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3749 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
3750 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3752 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3753 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3755 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3756 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3759 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3760 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3762 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
3763 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3765 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3766 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3768 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3769 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3772 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3773 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3775 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
3776 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3778 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3779 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3781 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3782 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3785 /*****************************************************************************
3786 * IDirectDrawSurface7::UpdateOverlayDisplay
3788 * The DX7 sdk says that it's not implemented
3790 * Params:
3791 * Flags: ?
3793 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3795 *****************************************************************************/
3796 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3798 TRACE("iface %p, flags %#x.\n", iface, Flags);
3800 return DDERR_UNSUPPORTED;
3803 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3805 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3807 TRACE("iface %p, flags %#x.\n", iface, flags);
3809 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3812 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3814 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3816 TRACE("iface %p, flags %#x.\n", iface, flags);
3818 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3821 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3823 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3825 TRACE("iface %p, flags %#x.\n", iface, flags);
3827 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3830 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3832 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3834 TRACE("iface %p, flags %#x.\n", iface, flags);
3836 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3839 /*****************************************************************************
3840 * IDirectDrawSurface7::UpdateOverlayZOrder
3842 * Sets an overlay's Z order
3844 * Params:
3845 * Flags: DDOVERZ_* flags
3846 * DDSRef: Defines the relative position in the overlay chain
3848 * Returns:
3849 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3851 *****************************************************************************/
3852 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3853 DWORD flags, IDirectDrawSurface7 *reference)
3855 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3857 FIXME("iface %p, flags %#x, reference %p stub!\n", iface, flags, reference);
3859 wined3d_mutex_lock();
3860 if (!(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OVERLAY))
3862 WARN("Not an overlay surface.\n");
3863 wined3d_mutex_unlock();
3864 return DDERR_NOTAOVERLAYSURFACE;
3866 wined3d_mutex_unlock();
3868 return DD_OK;
3871 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3872 DWORD flags, IDirectDrawSurface4 *reference)
3874 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3875 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3877 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3879 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3880 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3883 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3884 DWORD flags, IDirectDrawSurface3 *reference)
3886 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3887 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3889 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3891 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3892 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3895 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3896 DWORD flags, IDirectDrawSurface2 *reference)
3898 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3899 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3901 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3903 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3904 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3907 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3908 DWORD flags, IDirectDrawSurface *reference)
3910 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3911 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3913 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3915 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3916 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3919 /*****************************************************************************
3920 * IDirectDrawSurface7::GetDDInterface
3922 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3923 * surface belongs to
3925 * Params:
3926 * DD: Address to write the interface pointer to
3928 * Returns:
3929 * DD_OK on success
3930 * DDERR_INVALIDPARAMS if DD is NULL
3932 *****************************************************************************/
3933 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3935 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3937 TRACE("iface %p, ddraw %p.\n", iface, DD);
3939 if(!DD)
3940 return DDERR_INVALIDPARAMS;
3942 switch(This->version)
3944 case 7:
3945 *DD = &This->ddraw->IDirectDraw7_iface;
3946 break;
3948 case 4:
3949 *DD = &This->ddraw->IDirectDraw4_iface;
3950 break;
3952 case 2:
3953 *DD = &This->ddraw->IDirectDraw2_iface;
3954 break;
3956 case 1:
3957 *DD = &This->ddraw->IDirectDraw_iface;
3958 break;
3961 IUnknown_AddRef((IUnknown *)*DD);
3963 return DD_OK;
3966 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3968 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3970 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3972 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3975 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3977 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3979 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3981 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3984 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3986 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3988 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3990 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3993 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3995 TRACE("iface %p.\n", iface);
3997 return DD_OK;
4000 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
4002 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4004 TRACE("iface %p.\n", iface);
4006 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
4009 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
4011 TRACE("iface %p, value %p.\n", iface, pValue);
4013 *pValue = 0;
4015 return DD_OK;
4018 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
4020 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4022 TRACE("iface %p, value %p.\n", iface, pValue);
4024 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
4027 /*****************************************************************************
4028 * IDirectDrawSurface7::SetLOD
4030 * Sets the level of detail of a texture
4032 * Params:
4033 * MaxLOD: LOD to set
4035 * Returns:
4036 * DD_OK on success
4037 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4039 *****************************************************************************/
4040 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
4042 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4043 HRESULT hr;
4045 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
4047 wined3d_mutex_lock();
4048 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4050 wined3d_mutex_unlock();
4051 return DDERR_INVALIDOBJECT;
4054 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
4055 wined3d_mutex_unlock();
4057 return hr;
4060 /*****************************************************************************
4061 * IDirectDrawSurface7::GetLOD
4063 * Returns the level of detail of a Direct3D texture
4065 * Params:
4066 * MaxLOD: Address to write the LOD to
4068 * Returns:
4069 * DD_OK on success
4070 * DDERR_INVALIDPARAMS if MaxLOD is NULL
4071 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4073 *****************************************************************************/
4074 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4076 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4078 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4080 if(!MaxLOD)
4081 return DDERR_INVALIDPARAMS;
4083 wined3d_mutex_lock();
4084 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4086 wined3d_mutex_unlock();
4087 return DDERR_INVALIDOBJECT;
4090 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4091 wined3d_mutex_unlock();
4093 return DD_OK;
4096 /*****************************************************************************
4097 * IDirectDrawSurface7::BltFast
4099 * Performs a fast Blit.
4101 * Params:
4102 * dstx: The x coordinate to blit to on the destination
4103 * dsty: The y coordinate to blit to on the destination
4104 * Source: The source surface
4105 * rsrc: The source rectangle
4106 * trans: Type of transfer. Some DDBLTFAST_* flags
4108 * Returns:
4109 * DD_OK on success
4110 * For more details, see IWineD3DSurface::BltFast
4112 *****************************************************************************/
4113 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface7_BltFast(IDirectDrawSurface7 *iface,
4114 DWORD dst_x, DWORD dst_y, IDirectDrawSurface7 *src_surface, RECT *src_rect, DWORD trans)
4116 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
4117 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src_surface);
4118 DWORD src_w, src_h, dst_w, dst_h;
4119 HRESULT hr = DD_OK;
4120 RECT dst_rect, s;
4121 DWORD flags = 0;
4123 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4124 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), trans);
4126 dst_w = dst_impl->surface_desc.dwWidth;
4127 dst_h = dst_impl->surface_desc.dwHeight;
4129 if (!src_rect)
4131 SetRect(&s, 0, 0, src_impl->surface_desc.dwWidth, src_impl->surface_desc.dwHeight);
4132 src_rect = &s;
4135 src_w = src_rect->right - src_rect->left;
4136 src_h = src_rect->bottom - src_rect->top;
4137 if (src_w > dst_w || dst_x > dst_w - src_w
4138 || src_h > dst_h || dst_y > dst_h - src_h)
4140 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4141 return DDERR_INVALIDRECT;
4144 SetRect(&dst_rect, dst_x, dst_y, dst_x + src_w, dst_y + src_h);
4145 if (trans & DDBLTFAST_SRCCOLORKEY)
4146 flags |= WINEDDBLT_KEYSRC;
4147 if (trans & DDBLTFAST_DESTCOLORKEY)
4148 flags |= WINEDDBLT_KEYDEST;
4149 if (trans & DDBLTFAST_WAIT)
4150 flags |= WINEDDBLT_WAIT;
4151 if (trans & DDBLTFAST_DONOTWAIT)
4152 flags |= WINEDDBLT_DONOTWAIT;
4154 wined3d_mutex_lock();
4155 if (dst_impl->clipper)
4157 wined3d_mutex_unlock();
4158 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4159 return DDERR_BLTFASTCANTCLIP;
4162 if (src_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4163 hr = ddraw_surface_update_frontbuffer(src_impl, src_rect, TRUE);
4164 if (SUCCEEDED(hr))
4165 hr = wined3d_texture_blt(dst_impl->wined3d_texture, dst_impl->sub_resource_idx, &dst_rect,
4166 src_impl->wined3d_texture, src_impl->sub_resource_idx, src_rect, flags, NULL, WINED3D_TEXF_POINT);
4167 if (SUCCEEDED(hr) && (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4168 hr = ddraw_surface_update_frontbuffer(dst_impl, &dst_rect, FALSE);
4169 wined3d_mutex_unlock();
4171 switch(hr)
4173 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4174 default: return hr;
4178 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4179 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4181 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4182 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4184 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4185 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4187 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4188 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4191 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4192 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4194 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4195 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4197 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4198 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4200 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4201 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4204 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4205 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4207 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4208 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4210 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4211 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4213 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4214 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4217 static HRESULT WINAPI DECLSPEC_HOTPATCH ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4218 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4220 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4221 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4223 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4224 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4226 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4227 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4230 /*****************************************************************************
4231 * IDirectDrawSurface7::GetClipper
4233 * Returns the IDirectDrawClipper interface of the clipper assigned to this
4234 * surface
4236 * Params:
4237 * Clipper: Address to store the interface pointer at
4239 * Returns:
4240 * DD_OK on success
4241 * DDERR_INVALIDPARAMS if Clipper is NULL
4242 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
4244 *****************************************************************************/
4245 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
4247 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4249 TRACE("iface %p, clipper %p.\n", iface, Clipper);
4251 if (!Clipper)
4252 return DDERR_INVALIDPARAMS;
4254 wined3d_mutex_lock();
4255 if (!surface->clipper)
4257 wined3d_mutex_unlock();
4258 return DDERR_NOCLIPPERATTACHED;
4261 *Clipper = (IDirectDrawClipper *)surface->clipper;
4262 IDirectDrawClipper_AddRef(*Clipper);
4263 wined3d_mutex_unlock();
4265 return DD_OK;
4268 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4270 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4272 TRACE("iface %p, clipper %p.\n", iface, clipper);
4274 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4277 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4279 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4281 TRACE("iface %p, clipper %p.\n", iface, clipper);
4283 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4286 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4288 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4290 TRACE("iface %p, clipper %p.\n", iface, clipper);
4292 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4295 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4297 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4299 TRACE("iface %p, clipper %p.\n", iface, clipper);
4301 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4304 /*****************************************************************************
4305 * IDirectDrawSurface7::SetClipper
4307 * Sets a clipper for the surface
4309 * Params:
4310 * Clipper: IDirectDrawClipper interface of the clipper to set
4312 * Returns:
4313 * DD_OK on success
4315 *****************************************************************************/
4316 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4317 IDirectDrawClipper *iclipper)
4319 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4320 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4321 struct ddraw_clipper *old_clipper = This->clipper;
4322 HWND clipWindow;
4324 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4326 wined3d_mutex_lock();
4327 if (clipper == This->clipper)
4329 wined3d_mutex_unlock();
4330 return DD_OK;
4333 This->clipper = clipper;
4335 if (clipper != NULL)
4336 IDirectDrawClipper_AddRef(iclipper);
4337 if (old_clipper)
4338 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4340 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4342 clipWindow = NULL;
4343 if(clipper) {
4344 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4347 if (clipWindow)
4349 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4350 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4352 else
4354 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4355 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4359 wined3d_mutex_unlock();
4361 return DD_OK;
4364 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4366 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4368 TRACE("iface %p, clipper %p.\n", iface, clipper);
4370 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4373 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4375 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4377 TRACE("iface %p, clipper %p.\n", iface, clipper);
4379 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4382 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4384 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4386 TRACE("iface %p, clipper %p.\n", iface, clipper);
4388 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4391 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4393 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4395 TRACE("iface %p, clipper %p.\n", iface, clipper);
4397 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4400 /*****************************************************************************
4401 * IDirectDrawSurface7::SetSurfaceDesc
4403 * Sets the surface description. It can override the pixel format, the surface
4404 * memory, ...
4405 * It's not really tested.
4407 * Params:
4408 * DDSD: Pointer to the new surface description to set
4409 * Flags: Some flags
4411 * Returns:
4412 * DD_OK on success
4413 * DDERR_INVALIDPARAMS if DDSD is NULL
4415 *****************************************************************************/
4416 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4418 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4419 HRESULT hr;
4420 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4421 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4422 enum wined3d_format_id format_id;
4423 UINT pitch, width, height;
4425 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4427 if (!DDSD)
4429 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4430 return DDERR_INVALIDPARAMS;
4432 if (Flags)
4434 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4435 return DDERR_INVALIDPARAMS;
4437 if (!(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4438 || This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4439 || This->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4441 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4442 return DDERR_INVALIDSURFACETYPE;
4445 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4446 * for PIXELFORMAT to work */
4447 if (DDSD->dwFlags & ~allowed_flags)
4449 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4450 return DDERR_INVALIDPARAMS;
4452 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4454 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4455 return DDERR_INVALIDPARAMS;
4457 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4459 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4460 return DDERR_INVALIDCAPS;
4462 if (DDSD->dwFlags & DDSD_WIDTH)
4464 if (!(DDSD->dwFlags & DDSD_PITCH))
4466 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4467 return DDERR_INVALIDPARAMS;
4469 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4471 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4472 DDSD->u1.lPitch, DDSD->dwWidth);
4473 return DDERR_INVALIDPARAMS;
4475 if (DDSD->dwWidth != This->surface_desc.dwWidth)
4476 TRACE("Surface width changed from %u to %u.\n", This->surface_desc.dwWidth, DDSD->dwWidth);
4477 if (DDSD->u1.lPitch != This->surface_desc.u1.lPitch)
4478 TRACE("Surface pitch changed from %u to %u.\n", This->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4479 pitch = DDSD->u1.lPitch;
4480 width = DDSD->dwWidth;
4482 else if (DDSD->dwFlags & DDSD_PITCH)
4484 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4485 return DDERR_INVALIDPARAMS;
4487 else
4489 pitch = This->surface_desc.u1.lPitch;
4490 width = This->surface_desc.dwWidth;
4493 if (DDSD->dwFlags & DDSD_HEIGHT)
4495 if (!DDSD->dwHeight)
4497 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4498 return DDERR_INVALIDPARAMS;
4500 if (DDSD->dwHeight != This->surface_desc.dwHeight)
4501 TRACE("Surface height changed from %u to %u.\n", This->surface_desc.dwHeight, DDSD->dwHeight);
4502 height = DDSD->dwHeight;
4504 else
4506 height = This->surface_desc.dwHeight;
4509 wined3d_mutex_lock();
4510 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4512 enum wined3d_format_id current_format_id;
4513 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4515 if (format_id == WINED3DFMT_UNKNOWN)
4517 ERR("Requested to set an unknown pixelformat\n");
4518 wined3d_mutex_unlock();
4519 return DDERR_INVALIDPARAMS;
4521 current_format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4522 if (format_id != current_format_id)
4523 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4525 else
4527 format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4530 if (FAILED(hr = wined3d_texture_update_desc(This->wined3d_texture, width, height,
4531 format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4533 WARN("Failed to update surface desc, hr %#x.\n", hr);
4534 wined3d_mutex_unlock();
4535 return hr_ddraw_from_wined3d(hr);
4538 if (DDSD->dwFlags & DDSD_WIDTH)
4539 This->surface_desc.dwWidth = width;
4540 if (DDSD->dwFlags & DDSD_PITCH)
4541 This->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4542 if (DDSD->dwFlags & DDSD_HEIGHT)
4543 This->surface_desc.dwHeight = height;
4544 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4545 This->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4547 wined3d_mutex_unlock();
4549 return DD_OK;
4552 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4553 DDSURFACEDESC2 *surface_desc, DWORD flags)
4555 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4557 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4559 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4560 surface_desc, flags);
4563 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4564 DDSURFACEDESC *surface_desc, DWORD flags)
4566 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4567 DDSURFACEDESC2 surface_desc2;
4569 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4571 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4572 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4573 surface_desc ? &surface_desc2 : NULL, flags);
4576 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4578 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4579 struct ddraw_palette *palette_impl;
4580 HRESULT hr = DD_OK;
4582 TRACE("iface %p, palette %p.\n", iface, palette);
4584 if (!palette)
4585 return DDERR_INVALIDPARAMS;
4586 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4588 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4589 return DDERR_SURFACELOST;
4592 wined3d_mutex_lock();
4593 if ((palette_impl = surface->palette))
4595 *palette = &palette_impl->IDirectDrawPalette_iface;
4596 IDirectDrawPalette_AddRef(*palette);
4598 else
4600 *palette = NULL;
4601 hr = DDERR_NOPALETTEATTACHED;
4603 wined3d_mutex_unlock();
4605 return hr;
4608 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4610 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4612 TRACE("iface %p, palette %p.\n", iface, palette);
4614 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4617 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4619 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4621 TRACE("iface %p, palette %p.\n", iface, palette);
4623 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4626 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4628 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4630 TRACE("iface %p, palette %p.\n", iface, palette);
4632 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4635 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4637 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4639 TRACE("iface %p, palette %p.\n", iface, palette);
4641 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4644 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4646 DDCOLORKEY fixed_color_key;
4647 HRESULT hr = WINED3D_OK;
4649 if (flags & DDCKEY_COLORSPACE)
4651 if (color_key && color_key->dwColorSpaceLowValue != color_key->dwColorSpaceHighValue)
4653 WARN("Range color keys are not supported, returning DDERR_NOCOLORKEYHW.\n");
4654 return DDERR_NOCOLORKEYHW;
4656 flags &= ~DDCKEY_COLORSPACE;
4659 wined3d_mutex_lock();
4661 if (color_key)
4663 fixed_color_key.dwColorSpaceLowValue = fixed_color_key.dwColorSpaceHighValue = color_key->dwColorSpaceLowValue;
4664 switch (flags & ~DDCKEY_COLORSPACE)
4666 case DDCKEY_DESTBLT:
4667 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4668 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4669 break;
4671 case DDCKEY_DESTOVERLAY:
4672 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4673 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4674 break;
4676 case DDCKEY_SRCOVERLAY:
4677 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4678 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4679 break;
4681 case DDCKEY_SRCBLT:
4682 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4683 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4684 break;
4686 default:
4687 wined3d_mutex_unlock();
4688 return DDERR_INVALIDPARAMS;
4691 else
4693 switch (flags & ~DDCKEY_COLORSPACE)
4695 case DDCKEY_DESTBLT:
4696 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4697 break;
4699 case DDCKEY_DESTOVERLAY:
4700 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4701 break;
4703 case DDCKEY_SRCOVERLAY:
4704 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4705 break;
4707 case DDCKEY_SRCBLT:
4708 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4709 break;
4711 default:
4712 wined3d_mutex_unlock();
4713 return DDERR_INVALIDPARAMS;
4717 if (surface->is_complex_root)
4718 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags,
4719 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4721 wined3d_mutex_unlock();
4723 return hr_ddraw_from_wined3d(hr);
4726 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4728 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4730 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4732 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4733 return DDERR_NOTONMIPMAPSUBLEVEL;
4735 return ddraw_surface_set_color_key(surface, flags, color_key);
4738 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4740 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4742 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4744 return ddraw_surface_set_color_key(surface, flags, color_key);
4747 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4749 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4751 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4753 return ddraw_surface_set_color_key(surface, flags, color_key);
4756 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4758 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4760 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4762 return ddraw_surface_set_color_key(surface, flags, color_key);
4765 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4767 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4769 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4771 return ddraw_surface_set_color_key(surface, flags, color_key);
4774 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
4776 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4778 TRACE("iface %p, palette %p.\n", iface, palette);
4780 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4781 return DDERR_NOTONMIPMAPSUBLEVEL;
4782 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4784 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4785 return DDERR_SURFACELOST;
4788 return ddraw_surface_set_palette(surface, palette);
4791 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4793 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4795 TRACE("iface %p, palette %p.\n", iface, palette);
4797 if (IDirectDrawSurface4_IsLost(iface) == DDERR_SURFACELOST)
4799 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4800 return DDERR_SURFACELOST;
4803 return ddraw_surface_set_palette(surface, palette);
4806 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4808 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4810 TRACE("iface %p, palette %p.\n", iface, palette);
4812 if (IDirectDrawSurface3_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_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4823 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4825 TRACE("iface %p, palette %p.\n", iface, palette);
4827 if (IDirectDrawSurface2_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_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4838 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4840 TRACE("iface %p, palette %p.\n", iface, palette);
4842 if (IDirectDrawSurface_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 /**********************************************************
4852 * IDirectDrawGammaControl::GetGammaRamp
4854 * Returns the current gamma ramp for a surface
4856 * Params:
4857 * flags: Ignored
4858 * gamma_ramp: Address to write the ramp to
4860 * Returns:
4861 * DD_OK on success
4862 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4864 **********************************************************/
4865 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4866 DWORD flags, DDGAMMARAMP *gamma_ramp)
4868 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4870 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4872 if (!gamma_ramp)
4874 WARN("Invalid gamma_ramp passed.\n");
4875 return DDERR_INVALIDPARAMS;
4878 wined3d_mutex_lock();
4879 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4881 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4882 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
4884 else
4886 ERR("Not implemented for non-primary surfaces.\n");
4888 wined3d_mutex_unlock();
4890 return DD_OK;
4893 /**********************************************************
4894 * IDirectDrawGammaControl::SetGammaRamp
4896 * Sets the red, green and blue gamma ramps for
4898 * Params:
4899 * flags: Can be DDSGR_CALIBRATE to request calibration
4900 * gamma_ramp: Structure containing the new gamma ramp
4902 * Returns:
4903 * DD_OK on success
4904 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4906 **********************************************************/
4907 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4908 DWORD flags, DDGAMMARAMP *gamma_ramp)
4910 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4912 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4914 if (!gamma_ramp)
4916 WARN("Invalid gamma_ramp passed.\n");
4917 return DDERR_INVALIDPARAMS;
4920 wined3d_mutex_lock();
4921 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4923 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4924 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
4925 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
4927 else
4929 ERR("Not implemented for non-primary surfaces.\n");
4931 wined3d_mutex_unlock();
4933 return DD_OK;
4936 /*****************************************************************************
4937 * IDirect3DTexture2::PaletteChanged
4939 * Informs the texture about a palette change
4941 * Params:
4942 * start: Start index of the change
4943 * count: The number of changed entries
4945 * Returns
4946 * D3D_OK, because it's a stub
4948 *****************************************************************************/
4949 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4951 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4953 return D3D_OK;
4956 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4958 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
4960 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4962 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
4965 /*****************************************************************************
4966 * IDirect3DTexture::Unload
4968 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4971 * Returns:
4972 * DDERR_UNSUPPORTED
4974 *****************************************************************************/
4975 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4977 WARN("iface %p. Not implemented.\n", iface);
4979 return DDERR_UNSUPPORTED;
4982 /*****************************************************************************
4983 * IDirect3DTexture2::GetHandle
4985 * Returns handle for the texture. At the moment, the interface
4986 * to the IWineD3DTexture is used.
4988 * Params:
4989 * device: Device this handle is assigned to
4990 * handle: Address to store the handle at.
4992 * Returns:
4993 * D3D_OK
4995 *****************************************************************************/
4996 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4997 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4999 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
5000 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
5002 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5004 wined3d_mutex_lock();
5006 if (!surface->Handle)
5008 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
5009 if (h == DDRAW_INVALID_HANDLE)
5011 ERR("Failed to allocate a texture handle.\n");
5012 wined3d_mutex_unlock();
5013 return DDERR_OUTOFMEMORY;
5016 surface->Handle = h + 1;
5019 TRACE("Returning handle %08x.\n", surface->Handle);
5020 *handle = surface->Handle;
5022 wined3d_mutex_unlock();
5024 return D3D_OK;
5027 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
5028 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
5030 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
5031 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
5033 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
5035 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
5036 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
5039 /*****************************************************************************
5040 * get_sub_mimaplevel
5042 * Helper function that returns the next mipmap level
5044 * tex_ptr: Surface of which to return the next level
5046 *****************************************************************************/
5047 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
5049 /* Now go down the mipmap chain to the next surface */
5050 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, {0} };
5051 IDirectDrawSurface7 *next_level;
5052 HRESULT hr;
5054 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
5055 if (FAILED(hr)) return NULL;
5057 ddraw_surface7_Release(next_level);
5059 return impl_from_IDirectDrawSurface7(next_level);
5062 /*****************************************************************************
5063 * IDirect3DTexture2::Load
5065 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5067 * This function isn't relayed to WineD3D because the whole interface is
5068 * implemented in DDraw only. For speed improvements an implementation which
5069 * takes OpenGL more into account could be placed into WineD3D.
5071 * Params:
5072 * src_texture: Address of the texture to load
5074 * Returns:
5075 * D3D_OK on success
5076 * D3DERR_TEXTURE_LOAD_FAILED.
5078 *****************************************************************************/
5079 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5081 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5082 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5083 struct wined3d_resource *dst_resource, *src_resource;
5084 HRESULT hr;
5086 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5088 if (src_surface == dst_surface)
5090 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5091 return D3D_OK;
5094 wined3d_mutex_lock();
5096 dst_resource = wined3d_texture_get_resource(dst_surface->wined3d_texture);
5097 src_resource = wined3d_texture_get_resource(src_surface->wined3d_texture);
5099 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5100 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5101 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5103 ERR("Trying to load surfaces with different mip-map counts.\n");
5106 for (;;)
5108 struct ddraw_palette *dst_pal, *src_pal;
5109 DDSURFACEDESC *src_desc, *dst_desc;
5111 TRACE("Copying surface %p to surface %p.\n", src_surface, dst_surface);
5113 /* Suppress the ALLOCONLOAD flag */
5114 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5116 /* Get the palettes */
5117 dst_pal = dst_surface->palette;
5118 src_pal = src_surface->palette;
5120 if (src_pal)
5122 PALETTEENTRY palent[256];
5124 if (!dst_pal)
5126 wined3d_mutex_unlock();
5127 return DDERR_NOPALETTEATTACHED;
5129 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5130 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5133 /* Copy one surface on the other */
5134 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5135 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5137 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5139 /* Should also check for same pixel format, u1.lPitch, ... */
5140 ERR("Error in surface sizes.\n");
5141 wined3d_mutex_unlock();
5142 return D3DERR_TEXTURE_LOAD_FAILED;
5144 else
5146 struct wined3d_map_desc src_map_desc, dst_map_desc;
5148 /* Copy the src blit color key if the source has one, don't erase
5149 * the destination's ckey if the source has none */
5150 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5152 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5153 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5156 if (FAILED(hr = wined3d_resource_map(src_resource,
5157 src_surface->sub_resource_idx, &src_map_desc, NULL, 0)))
5159 ERR("Failed to lock source surface, hr %#x.\n", hr);
5160 wined3d_mutex_unlock();
5161 return D3DERR_TEXTURE_LOAD_FAILED;
5164 if (FAILED(hr = wined3d_resource_map(dst_resource,
5165 dst_surface->sub_resource_idx, &dst_map_desc, NULL, 0)))
5167 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5168 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5169 wined3d_mutex_unlock();
5170 return D3DERR_TEXTURE_LOAD_FAILED;
5173 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5174 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5175 else
5176 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5178 wined3d_resource_unmap(dst_resource, dst_surface->sub_resource_idx);
5179 wined3d_resource_unmap(src_resource, src_surface->sub_resource_idx);
5182 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5183 src_surface = get_sub_mimaplevel(src_surface);
5184 else
5185 src_surface = NULL;
5187 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5188 dst_surface = get_sub_mimaplevel(dst_surface);
5189 else
5190 dst_surface = NULL;
5192 if (!src_surface || !dst_surface)
5194 if (src_surface != dst_surface)
5195 ERR("Loading surface with different mipmap structure.\n");
5196 break;
5200 wined3d_mutex_unlock();
5202 return hr;
5205 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5207 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5208 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5210 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5212 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5213 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5216 /*****************************************************************************
5217 * The VTable
5218 *****************************************************************************/
5220 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5222 /* IUnknown */
5223 ddraw_surface7_QueryInterface,
5224 ddraw_surface7_AddRef,
5225 ddraw_surface7_Release,
5226 /* IDirectDrawSurface */
5227 ddraw_surface7_AddAttachedSurface,
5228 ddraw_surface7_AddOverlayDirtyRect,
5229 ddraw_surface7_Blt,
5230 ddraw_surface7_BltBatch,
5231 ddraw_surface7_BltFast,
5232 ddraw_surface7_DeleteAttachedSurface,
5233 ddraw_surface7_EnumAttachedSurfaces,
5234 ddraw_surface7_EnumOverlayZOrders,
5235 ddraw_surface7_Flip,
5236 ddraw_surface7_GetAttachedSurface,
5237 ddraw_surface7_GetBltStatus,
5238 ddraw_surface7_GetCaps,
5239 ddraw_surface7_GetClipper,
5240 ddraw_surface7_GetColorKey,
5241 ddraw_surface7_GetDC,
5242 ddraw_surface7_GetFlipStatus,
5243 ddraw_surface7_GetOverlayPosition,
5244 ddraw_surface7_GetPalette,
5245 ddraw_surface7_GetPixelFormat,
5246 ddraw_surface7_GetSurfaceDesc,
5247 ddraw_surface7_Initialize,
5248 ddraw_surface7_IsLost,
5249 ddraw_surface7_Lock,
5250 ddraw_surface7_ReleaseDC,
5251 ddraw_surface7_Restore,
5252 ddraw_surface7_SetClipper,
5253 ddraw_surface7_SetColorKey,
5254 ddraw_surface7_SetOverlayPosition,
5255 ddraw_surface7_SetPalette,
5256 ddraw_surface7_Unlock,
5257 ddraw_surface7_UpdateOverlay,
5258 ddraw_surface7_UpdateOverlayDisplay,
5259 ddraw_surface7_UpdateOverlayZOrder,
5260 /* IDirectDrawSurface2 */
5261 ddraw_surface7_GetDDInterface,
5262 ddraw_surface7_PageLock,
5263 ddraw_surface7_PageUnlock,
5264 /* IDirectDrawSurface3 */
5265 ddraw_surface7_SetSurfaceDesc,
5266 /* IDirectDrawSurface4 */
5267 ddraw_surface7_SetPrivateData,
5268 ddraw_surface7_GetPrivateData,
5269 ddraw_surface7_FreePrivateData,
5270 ddraw_surface7_GetUniquenessValue,
5271 ddraw_surface7_ChangeUniquenessValue,
5272 /* IDirectDrawSurface7 */
5273 ddraw_surface7_SetPriority,
5274 ddraw_surface7_GetPriority,
5275 ddraw_surface7_SetLOD,
5276 ddraw_surface7_GetLOD,
5279 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5281 /* IUnknown */
5282 ddraw_surface4_QueryInterface,
5283 ddraw_surface4_AddRef,
5284 ddraw_surface4_Release,
5285 /* IDirectDrawSurface */
5286 ddraw_surface4_AddAttachedSurface,
5287 ddraw_surface4_AddOverlayDirtyRect,
5288 ddraw_surface4_Blt,
5289 ddraw_surface4_BltBatch,
5290 ddraw_surface4_BltFast,
5291 ddraw_surface4_DeleteAttachedSurface,
5292 ddraw_surface4_EnumAttachedSurfaces,
5293 ddraw_surface4_EnumOverlayZOrders,
5294 ddraw_surface4_Flip,
5295 ddraw_surface4_GetAttachedSurface,
5296 ddraw_surface4_GetBltStatus,
5297 ddraw_surface4_GetCaps,
5298 ddraw_surface4_GetClipper,
5299 ddraw_surface4_GetColorKey,
5300 ddraw_surface4_GetDC,
5301 ddraw_surface4_GetFlipStatus,
5302 ddraw_surface4_GetOverlayPosition,
5303 ddraw_surface4_GetPalette,
5304 ddraw_surface4_GetPixelFormat,
5305 ddraw_surface4_GetSurfaceDesc,
5306 ddraw_surface4_Initialize,
5307 ddraw_surface4_IsLost,
5308 ddraw_surface4_Lock,
5309 ddraw_surface4_ReleaseDC,
5310 ddraw_surface4_Restore,
5311 ddraw_surface4_SetClipper,
5312 ddraw_surface4_SetColorKey,
5313 ddraw_surface4_SetOverlayPosition,
5314 ddraw_surface4_SetPalette,
5315 ddraw_surface4_Unlock,
5316 ddraw_surface4_UpdateOverlay,
5317 ddraw_surface4_UpdateOverlayDisplay,
5318 ddraw_surface4_UpdateOverlayZOrder,
5319 /* IDirectDrawSurface2 */
5320 ddraw_surface4_GetDDInterface,
5321 ddraw_surface4_PageLock,
5322 ddraw_surface4_PageUnlock,
5323 /* IDirectDrawSurface3 */
5324 ddraw_surface4_SetSurfaceDesc,
5325 /* IDirectDrawSurface4 */
5326 ddraw_surface4_SetPrivateData,
5327 ddraw_surface4_GetPrivateData,
5328 ddraw_surface4_FreePrivateData,
5329 ddraw_surface4_GetUniquenessValue,
5330 ddraw_surface4_ChangeUniquenessValue,
5333 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5335 /* IUnknown */
5336 ddraw_surface3_QueryInterface,
5337 ddraw_surface3_AddRef,
5338 ddraw_surface3_Release,
5339 /* IDirectDrawSurface */
5340 ddraw_surface3_AddAttachedSurface,
5341 ddraw_surface3_AddOverlayDirtyRect,
5342 ddraw_surface3_Blt,
5343 ddraw_surface3_BltBatch,
5344 ddraw_surface3_BltFast,
5345 ddraw_surface3_DeleteAttachedSurface,
5346 ddraw_surface3_EnumAttachedSurfaces,
5347 ddraw_surface3_EnumOverlayZOrders,
5348 ddraw_surface3_Flip,
5349 ddraw_surface3_GetAttachedSurface,
5350 ddraw_surface3_GetBltStatus,
5351 ddraw_surface3_GetCaps,
5352 ddraw_surface3_GetClipper,
5353 ddraw_surface3_GetColorKey,
5354 ddraw_surface3_GetDC,
5355 ddraw_surface3_GetFlipStatus,
5356 ddraw_surface3_GetOverlayPosition,
5357 ddraw_surface3_GetPalette,
5358 ddraw_surface3_GetPixelFormat,
5359 ddraw_surface3_GetSurfaceDesc,
5360 ddraw_surface3_Initialize,
5361 ddraw_surface3_IsLost,
5362 ddraw_surface3_Lock,
5363 ddraw_surface3_ReleaseDC,
5364 ddraw_surface3_Restore,
5365 ddraw_surface3_SetClipper,
5366 ddraw_surface3_SetColorKey,
5367 ddraw_surface3_SetOverlayPosition,
5368 ddraw_surface3_SetPalette,
5369 ddraw_surface3_Unlock,
5370 ddraw_surface3_UpdateOverlay,
5371 ddraw_surface3_UpdateOverlayDisplay,
5372 ddraw_surface3_UpdateOverlayZOrder,
5373 /* IDirectDrawSurface2 */
5374 ddraw_surface3_GetDDInterface,
5375 ddraw_surface3_PageLock,
5376 ddraw_surface3_PageUnlock,
5377 /* IDirectDrawSurface3 */
5378 ddraw_surface3_SetSurfaceDesc,
5381 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5383 /* IUnknown */
5384 ddraw_surface2_QueryInterface,
5385 ddraw_surface2_AddRef,
5386 ddraw_surface2_Release,
5387 /* IDirectDrawSurface */
5388 ddraw_surface2_AddAttachedSurface,
5389 ddraw_surface2_AddOverlayDirtyRect,
5390 ddraw_surface2_Blt,
5391 ddraw_surface2_BltBatch,
5392 ddraw_surface2_BltFast,
5393 ddraw_surface2_DeleteAttachedSurface,
5394 ddraw_surface2_EnumAttachedSurfaces,
5395 ddraw_surface2_EnumOverlayZOrders,
5396 ddraw_surface2_Flip,
5397 ddraw_surface2_GetAttachedSurface,
5398 ddraw_surface2_GetBltStatus,
5399 ddraw_surface2_GetCaps,
5400 ddraw_surface2_GetClipper,
5401 ddraw_surface2_GetColorKey,
5402 ddraw_surface2_GetDC,
5403 ddraw_surface2_GetFlipStatus,
5404 ddraw_surface2_GetOverlayPosition,
5405 ddraw_surface2_GetPalette,
5406 ddraw_surface2_GetPixelFormat,
5407 ddraw_surface2_GetSurfaceDesc,
5408 ddraw_surface2_Initialize,
5409 ddraw_surface2_IsLost,
5410 ddraw_surface2_Lock,
5411 ddraw_surface2_ReleaseDC,
5412 ddraw_surface2_Restore,
5413 ddraw_surface2_SetClipper,
5414 ddraw_surface2_SetColorKey,
5415 ddraw_surface2_SetOverlayPosition,
5416 ddraw_surface2_SetPalette,
5417 ddraw_surface2_Unlock,
5418 ddraw_surface2_UpdateOverlay,
5419 ddraw_surface2_UpdateOverlayDisplay,
5420 ddraw_surface2_UpdateOverlayZOrder,
5421 /* IDirectDrawSurface2 */
5422 ddraw_surface2_GetDDInterface,
5423 ddraw_surface2_PageLock,
5424 ddraw_surface2_PageUnlock,
5427 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5429 /* IUnknown */
5430 ddraw_surface1_QueryInterface,
5431 ddraw_surface1_AddRef,
5432 ddraw_surface1_Release,
5433 /* IDirectDrawSurface */
5434 ddraw_surface1_AddAttachedSurface,
5435 ddraw_surface1_AddOverlayDirtyRect,
5436 ddraw_surface1_Blt,
5437 ddraw_surface1_BltBatch,
5438 ddraw_surface1_BltFast,
5439 ddraw_surface1_DeleteAttachedSurface,
5440 ddraw_surface1_EnumAttachedSurfaces,
5441 ddraw_surface1_EnumOverlayZOrders,
5442 ddraw_surface1_Flip,
5443 ddraw_surface1_GetAttachedSurface,
5444 ddraw_surface1_GetBltStatus,
5445 ddraw_surface1_GetCaps,
5446 ddraw_surface1_GetClipper,
5447 ddraw_surface1_GetColorKey,
5448 ddraw_surface1_GetDC,
5449 ddraw_surface1_GetFlipStatus,
5450 ddraw_surface1_GetOverlayPosition,
5451 ddraw_surface1_GetPalette,
5452 ddraw_surface1_GetPixelFormat,
5453 ddraw_surface1_GetSurfaceDesc,
5454 ddraw_surface1_Initialize,
5455 ddraw_surface1_IsLost,
5456 ddraw_surface1_Lock,
5457 ddraw_surface1_ReleaseDC,
5458 ddraw_surface1_Restore,
5459 ddraw_surface1_SetClipper,
5460 ddraw_surface1_SetColorKey,
5461 ddraw_surface1_SetOverlayPosition,
5462 ddraw_surface1_SetPalette,
5463 ddraw_surface1_Unlock,
5464 ddraw_surface1_UpdateOverlay,
5465 ddraw_surface1_UpdateOverlayDisplay,
5466 ddraw_surface1_UpdateOverlayZOrder,
5469 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5471 ddraw_gamma_control_QueryInterface,
5472 ddraw_gamma_control_AddRef,
5473 ddraw_gamma_control_Release,
5474 ddraw_gamma_control_GetGammaRamp,
5475 ddraw_gamma_control_SetGammaRamp,
5478 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5480 d3d_texture2_QueryInterface,
5481 d3d_texture2_AddRef,
5482 d3d_texture2_Release,
5483 d3d_texture2_GetHandle,
5484 d3d_texture2_PaletteChanged,
5485 d3d_texture2_Load,
5488 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5490 d3d_texture1_QueryInterface,
5491 d3d_texture1_AddRef,
5492 d3d_texture1_Release,
5493 d3d_texture1_Initialize,
5494 d3d_texture1_GetHandle,
5495 d3d_texture1_PaletteChanged,
5496 d3d_texture1_Load,
5497 d3d_texture1_Unload,
5500 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5501 * IDirectDrawSurface interface to ddraw methods. */
5502 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5504 if (!iface) return NULL;
5505 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5507 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5508 if (FAILED(hr))
5510 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5511 return NULL;
5513 IDirectDrawSurface7_Release(iface);
5515 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5518 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5520 if (!iface) return NULL;
5521 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5523 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5524 if (FAILED(hr))
5526 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5527 return NULL;
5529 IDirectDrawSurface4_Release(iface);
5531 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5534 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5536 if (!iface) return NULL;
5537 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5539 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5540 if (FAILED(hr))
5542 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5543 return NULL;
5545 IDirectDrawSurface3_Release(iface);
5547 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5550 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5552 if (!iface) return NULL;
5553 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5555 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5556 if (FAILED(hr))
5558 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5559 return NULL;
5561 IDirectDrawSurface2_Release(iface);
5563 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5566 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5568 if (!iface) return NULL;
5569 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5571 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5572 if (FAILED(hr))
5574 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5575 return NULL;
5577 IDirectDrawSurface_Release(iface);
5579 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5582 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5584 if (!iface) return NULL;
5585 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5586 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5589 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5591 if (!iface) return NULL;
5592 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5593 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5596 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5598 struct ddraw_surface *surface = parent;
5600 TRACE("surface %p.\n", surface);
5602 /* Check for attached surfaces and detach them. */
5603 if (surface->first_attached != surface)
5605 /* Well, this shouldn't happen: The surface being attached is
5606 * referenced in AddAttachedSurface(), so it shouldn't be released
5607 * until DeleteAttachedSurface() is called, because the refcount is
5608 * held. It looks like the application released it often enough to
5609 * force this. */
5610 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
5612 /* The refcount will drop to -1 here */
5613 if (FAILED(ddraw_surface_delete_attached_surface(surface->first_attached, surface, surface->attached_iface)))
5614 ERR("DeleteAttachedSurface failed.\n");
5617 while (surface->next_attached)
5618 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5619 surface->next_attached, surface->next_attached->attached_iface)))
5620 ERR("DeleteAttachedSurface failed.\n");
5622 /* Having a texture handle set implies that the device still exists. */
5623 if (surface->Handle)
5624 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5626 /* Reduce the ddraw surface count. */
5627 list_remove(&surface->surface_list_entry);
5629 if (surface->clipper)
5630 IDirectDrawClipper_Release(&surface->clipper->IDirectDrawClipper_iface);
5632 if (surface == surface->ddraw->primary)
5633 surface->ddraw->primary = NULL;
5635 wined3d_private_store_cleanup(&surface->private_store);
5637 HeapFree(GetProcessHeap(), 0, surface);
5640 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5642 ddraw_surface_wined3d_object_destroyed,
5645 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5647 TRACE("parent %p.\n", parent);
5649 HeapFree(GetProcessHeap(), 0, parent);
5652 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5654 ddraw_texture_wined3d_object_destroyed,
5657 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5659 return DD_OK;
5662 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
5663 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
5665 struct wined3d_resource_desc wined3d_desc, wined3d_mip_desc;
5666 struct ddraw_surface *root, *mip, **attach;
5667 struct wined3d_texture *wined3d_texture;
5668 struct wined3d_resource *resource;
5669 struct wined3d_display_mode mode;
5670 DDSURFACEDESC2 *desc, *mip_desc;
5671 struct ddraw_texture *texture;
5672 unsigned int layers = 1;
5673 unsigned int pitch = 0;
5674 UINT levels, i, j;
5675 HRESULT hr;
5677 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
5678 ddraw, surface_desc, surface, outer_unknown, version);
5679 if (TRACE_ON(ddraw))
5681 TRACE("Requesting surface desc:\n");
5682 DDRAW_dump_surface_desc(surface_desc);
5685 if (outer_unknown)
5686 return CLASS_E_NOAGGREGATION;
5688 if (!surface)
5689 return E_POINTER;
5691 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
5692 return E_OUTOFMEMORY;
5694 texture->version = version;
5695 texture->surface_desc = *surface_desc;
5696 desc = &texture->surface_desc;
5698 /* Ensure DDSD_CAPS is always set. */
5699 desc->dwFlags |= DDSD_CAPS;
5701 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5703 DWORD flippable = desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_COMPLEX);
5705 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5707 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
5708 HeapFree(GetProcessHeap(), 0, texture);
5709 return DDERR_INVALIDCAPS;
5712 if (flippable)
5714 if (flippable != (DDSCAPS_FLIP | DDSCAPS_COMPLEX))
5716 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
5717 HeapFree(GetProcessHeap(), 0, texture);
5718 return DDERR_INVALIDCAPS;
5721 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->u5.dwBackBufferCount)
5723 WARN("Tried to create a flippable primary surface without any back buffers.\n");
5724 HeapFree(GetProcessHeap(), 0, texture);
5725 return DDERR_INVALIDCAPS;
5728 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
5730 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
5731 HeapFree(GetProcessHeap(), 0, texture);
5732 return DDERR_NOEXCLUSIVEMODE;
5737 /* This is a special case in ddrawex, but not allowed in ddraw. */
5738 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5739 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5741 WARN("Tried to create a surface in both system and video memory.\n");
5742 HeapFree(GetProcessHeap(), 0, texture);
5743 return DDERR_INVALIDCAPS;
5746 if ((desc->ddsCaps.dwCaps & (DDSCAPS_ALLOCONLOAD | DDSCAPS_MIPMAP))
5747 && !(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5749 WARN("Caps %#x require DDSCAPS_TEXTURE.\n", desc->ddsCaps.dwCaps);
5750 HeapFree(GetProcessHeap(), 0, texture);
5751 return DDERR_INVALIDCAPS;
5754 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
5755 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
5757 WARN("Cube map faces requested without cube map flag.\n");
5758 HeapFree(GetProcessHeap(), 0, texture);
5759 return DDERR_INVALIDCAPS;
5762 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5763 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
5765 WARN("Cube map without faces requested.\n");
5766 HeapFree(GetProcessHeap(), 0, texture);
5767 return DDERR_INVALIDPARAMS;
5770 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5771 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
5772 FIXME("Partial cube maps not implemented.\n");
5774 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
5776 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5778 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
5779 HeapFree(GetProcessHeap(), 0, texture);
5780 return DDERR_INVALIDCAPS;
5782 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5784 WARN("DDSCAPS2_TEXTUREMANAGE used width DDSCAPS_VIDEOMEMORY "
5785 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
5786 HeapFree(GetProcessHeap(), 0, texture);
5787 return DDERR_INVALIDCAPS;
5791 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
5793 ERR("Failed to get display mode, hr %#x.\n", hr);
5794 HeapFree(GetProcessHeap(), 0, texture);
5795 return hr_ddraw_from_wined3d(hr);
5798 /* No pixelformat given? Use the current screen format. */
5799 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
5801 desc->dwFlags |= DDSD_PIXELFORMAT;
5802 desc->u4.ddpfPixelFormat.dwSize = sizeof(desc->u4.ddpfPixelFormat);
5803 ddrawformat_from_wined3dformat(&desc->u4.ddpfPixelFormat, mode.format_id);
5806 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5807 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
5808 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
5810 WARN("Unsupported / unknown pixelformat.\n");
5811 HeapFree(GetProcessHeap(), 0, texture);
5812 return DDERR_INVALIDPIXELFORMAT;
5815 /* No width or no height? Use the screen size. */
5816 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
5818 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
5820 WARN("No width / height specified.\n");
5821 HeapFree(GetProcessHeap(), 0, texture);
5822 return DDERR_INVALIDPARAMS;
5825 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
5826 desc->dwWidth = mode.width;
5827 desc->dwHeight = mode.height;
5830 if (!desc->dwWidth || !desc->dwHeight)
5832 HeapFree(GetProcessHeap(), 0, texture);
5833 return DDERR_INVALIDPARAMS;
5836 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5838 /* The first surface is a front buffer, the back buffers are created
5839 * afterwards. */
5840 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
5841 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
5842 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5843 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
5845 struct wined3d_swapchain_desc swapchain_desc;
5847 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
5848 swapchain_desc.backbuffer_width = mode.width;
5849 swapchain_desc.backbuffer_height = mode.height;
5850 swapchain_desc.backbuffer_format = mode.format_id;
5852 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
5853 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
5855 ERR("Failed to reset device.\n");
5856 HeapFree(GetProcessHeap(), 0, texture);
5857 return hr_ddraw_from_wined3d(hr);
5862 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5863 wined3d_desc.multisample_quality = 0;
5864 wined3d_desc.usage = 0;
5865 wined3d_desc.pool = WINED3D_POOL_DEFAULT;
5866 wined3d_desc.width = desc->dwWidth;
5867 wined3d_desc.height = desc->dwHeight;
5868 wined3d_desc.depth = 1;
5869 wined3d_desc.size = 0;
5871 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
5873 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
5874 /* Do not fail surface creation, only fail 3D device creation. */
5877 /* Mipmap count fixes */
5878 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5880 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
5882 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
5884 /* Mipmap count is given, should not be 0. */
5885 if (!desc->u2.dwMipMapCount)
5887 HeapFree(GetProcessHeap(), 0, texture);
5888 return DDERR_INVALIDPARAMS;
5891 else
5893 /* Undocumented feature: Create sublevels until either the
5894 * width or the height is 1. */
5895 if (version == 7)
5896 desc->u2.dwMipMapCount = wined3d_log2i(max(desc->dwWidth, desc->dwHeight)) + 1;
5897 else
5898 desc->u2.dwMipMapCount = wined3d_log2i(min(desc->dwWidth, desc->dwHeight)) + 1;
5901 else
5903 desc->u2.dwMipMapCount = 1;
5906 desc->dwFlags |= DDSD_MIPMAPCOUNT;
5907 levels = desc->u2.dwMipMapCount;
5909 else
5911 levels = 1;
5914 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
5916 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
5918 enum wined3d_resource_type rtype;
5919 DWORD usage = 0;
5921 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5923 usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
5924 rtype = WINED3D_RTYPE_TEXTURE_2D;
5926 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5927 rtype = WINED3D_RTYPE_TEXTURE_2D;
5928 else
5929 rtype = WINED3D_RTYPE_SURFACE;
5931 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5932 usage = WINED3DUSAGE_DEPTHSTENCIL;
5933 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5934 usage = WINED3DUSAGE_RENDERTARGET;
5936 if (SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
5937 WINED3D_DEVICE_TYPE_HAL, mode.format_id, usage, rtype, wined3d_desc.format)))
5938 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
5939 else
5940 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5942 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5944 /* Tests show surfaces without memory flags get these flags added
5945 * right after creation. */
5946 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5950 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5951 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5953 WARN("System memory overlays are not allowed.\n");
5954 HeapFree(GetProcessHeap(), 0, texture);
5955 return DDERR_NOOVERLAYHW;
5958 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5960 wined3d_desc.pool = WINED3D_POOL_SYSTEM_MEM;
5962 else
5964 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5965 wined3d_desc.usage |= WINED3DUSAGE_TEXTURE;
5966 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5967 wined3d_desc.usage |= WINED3DUSAGE_DEPTHSTENCIL;
5968 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5969 wined3d_desc.usage |= WINED3DUSAGE_RENDERTARGET;
5971 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
5973 wined3d_desc.pool = WINED3D_POOL_MANAGED;
5974 /* Managed textures have the system memory flag set. */
5975 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5977 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5979 /* Videomemory adds localvidmem. This is mutually exclusive with
5980 * systemmemory and texturemanage. */
5981 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
5982 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
5986 if (desc->dwFlags & DDSD_LPSURFACE)
5988 if (wined3d_desc.pool != WINED3D_POOL_SYSTEM_MEM)
5990 WARN("User memory surfaces should be in the system memory pool.\n");
5991 HeapFree(GetProcessHeap(), 0, texture);
5992 return DDERR_INVALIDCAPS;
5995 if (version < 4)
5997 WARN("User memory surfaces not supported before version 4.\n");
5998 HeapFree(GetProcessHeap(), 0, texture);
5999 return DDERR_INVALIDPARAMS;
6002 if (!desc->lpSurface)
6004 WARN("NULL surface memory pointer specified.\n");
6005 HeapFree(GetProcessHeap(), 0, texture);
6006 return DDERR_INVALIDPARAMS;
6009 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6011 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
6013 WARN("Pitch specified on a compressed user memory surface.\n");
6014 HeapFree(GetProcessHeap(), 0, texture);
6015 return DDERR_INVALIDPARAMS;
6018 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
6020 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
6021 HeapFree(GetProcessHeap(), 0, texture);
6022 return DDERR_INVALIDPARAMS;
6025 if ((desc->dwFlags & DDSD_LINEARSIZE)
6026 && desc->u1.dwLinearSize < wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
6027 wined3d_desc.format, wined3d_desc.width) * ((desc->dwHeight + 3) / 4))
6029 WARN("Invalid linear size %u specified.\n", desc->u1.dwLinearSize);
6030 HeapFree(GetProcessHeap(), 0, texture);
6031 return DDERR_INVALIDPARAMS;
6034 else
6036 if (!(desc->dwFlags & DDSD_PITCH))
6038 WARN("User memory surfaces should explicitly specify the pitch.\n");
6039 HeapFree(GetProcessHeap(), 0, texture);
6040 return DDERR_INVALIDPARAMS;
6043 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
6044 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
6046 WARN("Invalid pitch %u specified.\n", desc->u1.lPitch);
6047 HeapFree(GetProcessHeap(), 0, texture);
6048 return DDERR_INVALIDPARAMS;
6051 pitch = desc->u1.lPitch;
6055 if (((desc->dwFlags & DDSD_CKDESTOVERLAY)
6056 && desc->u3.ddckCKDestOverlay.dwColorSpaceLowValue != desc->u3.ddckCKDestOverlay.dwColorSpaceHighValue)
6057 || ((desc->dwFlags & DDSD_CKDESTBLT)
6058 && desc->ddckCKDestBlt.dwColorSpaceLowValue != desc->ddckCKDestBlt.dwColorSpaceHighValue)
6059 || ((desc->dwFlags & DDSD_CKSRCOVERLAY)
6060 && desc->ddckCKSrcOverlay.dwColorSpaceLowValue != desc->ddckCKSrcOverlay.dwColorSpaceHighValue)
6061 || ((desc->dwFlags & DDSD_CKSRCBLT)
6062 && desc->ddckCKSrcBlt.dwColorSpaceLowValue != desc->ddckCKSrcBlt.dwColorSpaceHighValue))
6064 WARN("Range color keys not supported, returning DDERR_NOCOLORKEYHW.\n");
6065 HeapFree(GetProcessHeap(), 0, texture);
6066 return DDERR_NOCOLORKEYHW;
6069 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
6070 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
6072 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
6073 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
6075 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6077 wined3d_desc.usage |= WINED3DUSAGE_LEGACY_CUBEMAP;
6078 layers = 6;
6081 /* Some applications assume surfaces will always be mapped at the same
6082 * address. Some of those also assume that this address is valid even when
6083 * the surface isn't mapped, and that updates done this way will be
6084 * visible on the screen. The game Nox is such an application,
6085 * Commandos: Behind Enemy Lines is another. We set
6086 * WINED3D_TEXTURE_CREATE_PIN_SYSMEM because of this. */
6087 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, levels,
6088 WINED3D_TEXTURE_CREATE_PIN_SYSMEM, NULL, texture, &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
6090 WARN("Failed to create wined3d texture, hr %#x.\n", hr);
6091 HeapFree(GetProcessHeap(), 0, texture);
6092 return hr_ddraw_from_wined3d(hr);
6095 resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
6096 root = wined3d_resource_get_parent(resource);
6097 wined3d_texture_decref(wined3d_texture);
6098 root->is_complex_root = TRUE;
6099 texture->root = root;
6101 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6102 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6103 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6104 if (desc->dwFlags & DDSD_CKDESTBLT)
6105 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6106 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6107 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6108 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6109 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6110 if (desc->dwFlags & DDSD_CKSRCBLT)
6111 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6112 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6114 for (i = 0; i < layers; ++i)
6116 attach = &root->complex_array[layers - 1 - i];
6118 for (j = 0; j < levels; ++j)
6120 resource = wined3d_texture_get_sub_resource(wined3d_texture, i * levels + j);
6121 mip = wined3d_resource_get_parent(resource);
6122 mip_desc = &mip->surface_desc;
6124 if (j)
6126 wined3d_resource_get_desc(resource, &wined3d_mip_desc);
6127 mip_desc->dwWidth = wined3d_mip_desc.width;
6128 mip_desc->dwHeight = wined3d_mip_desc.height;
6130 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6132 else
6134 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6137 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6139 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6141 switch (i)
6143 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6144 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6145 break;
6146 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6147 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6148 break;
6149 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6150 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6151 break;
6152 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6153 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6154 break;
6155 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6156 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6157 break;
6158 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6159 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6160 break;
6165 if (mip == root)
6166 continue;
6168 *attach = mip;
6169 attach = &mip->complex_array[0];
6173 if ((desc->dwFlags & DDSD_LPSURFACE) && FAILED(hr = wined3d_texture_update_desc(wined3d_texture,
6174 wined3d_desc.width, wined3d_desc.height, wined3d_desc.format,
6175 WINED3D_MULTISAMPLE_NONE, 0, desc->lpSurface, pitch)))
6177 ERR("Failed to set surface memory, hr %#x.\n", hr);
6178 goto fail;
6181 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6183 unsigned int count = desc->u5.dwBackBufferCount;
6184 struct ddraw_surface *last = root;
6186 attach = &last->complex_array[0];
6187 for (i = 0; i < count; ++i)
6189 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
6191 hr = E_OUTOFMEMORY;
6192 goto fail;
6195 texture->version = version;
6196 texture->surface_desc = root->surface_desc;
6197 desc = &texture->surface_desc;
6199 /* Only one surface in the flipping chain is a back buffer, one is
6200 * a front buffer, the others are just flippable surfaces. */
6201 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6202 | DDSCAPS_BACKBUFFER);
6203 if (!i)
6204 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6205 desc->u5.dwBackBufferCount = 0;
6207 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, 1,
6208 WINED3D_TEXTURE_CREATE_PIN_SYSMEM, NULL, texture,
6209 &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
6211 HeapFree(GetProcessHeap(), 0, texture);
6212 hr = hr_ddraw_from_wined3d(hr);
6213 goto fail;
6216 resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
6217 last = wined3d_resource_get_parent(resource);
6218 wined3d_texture_decref(wined3d_texture);
6219 texture->root = last;
6221 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6222 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6223 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6224 if (desc->dwFlags & DDSD_CKDESTBLT)
6225 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6226 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6227 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6228 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6229 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6230 if (desc->dwFlags & DDSD_CKSRCBLT)
6231 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6232 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6234 *attach = last;
6235 attach = &last->complex_array[0];
6237 *attach = root;
6240 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6241 ddraw->primary = root;
6242 *surface = root;
6244 return DD_OK;
6246 fail:
6247 if (version == 7)
6248 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6249 else if (version == 4)
6250 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6251 else
6252 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6254 return hr;
6257 void ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw,
6258 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
6259 const struct wined3d_parent_ops **parent_ops)
6261 struct ddraw_texture *texture = wined3d_texture_get_parent(wined3d_texture);
6262 unsigned int texture_level, row_pitch, slice_pitch;
6263 DDSURFACEDESC2 *desc = &surface->surface_desc;
6264 unsigned int version = texture->version;
6266 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6267 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6268 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6269 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6270 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6271 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6272 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6273 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6274 surface->iface_count = 1;
6275 surface->version = version;
6276 surface->ddraw = ddraw;
6278 if (version == 7)
6280 surface->ref7 = 1;
6281 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6283 else if (version == 4)
6285 surface->ref4 = 1;
6286 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6288 else
6290 surface->ref1 = 1;
6291 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6294 *desc = texture->surface_desc;
6295 surface->first_attached = surface;
6297 texture_level = desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP ? sub_resource_idx % desc->u2.dwMipMapCount : 0;
6298 wined3d_texture_get_pitch(wined3d_texture, texture_level, &row_pitch, &slice_pitch);
6299 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6301 if (desc->dwFlags & DDSD_LPSURFACE)
6302 desc->u1.dwLinearSize = ~0u;
6303 else
6304 desc->u1.dwLinearSize = slice_pitch;
6305 desc->dwFlags |= DDSD_LINEARSIZE;
6306 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6308 else
6310 if (!(desc->dwFlags & DDSD_LPSURFACE))
6311 desc->u1.lPitch = row_pitch;
6312 desc->dwFlags |= DDSD_PITCH;
6313 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6315 desc->lpSurface = NULL;
6317 wined3d_texture_incref(surface->wined3d_texture = wined3d_texture);
6318 surface->sub_resource_idx = sub_resource_idx;
6319 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6321 wined3d_private_store_init(&surface->private_store);
6324 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
6326 struct ddraw_surface *surface = parent;
6328 /* If the surface reference count drops to zero, we release our reference
6329 * to the view, but don't clear the pointer yet, in case e.g. a
6330 * GetRenderTarget() call brings the surface back before the view is
6331 * actually destroyed. When the view is destroyed, we need to clear the
6332 * pointer, or a subsequent surface AddRef() would reference it again.
6334 * This is safe because as long as the view still has a reference to the
6335 * texture, the surface is also still alive, and we're called before the
6336 * view releases that reference. */
6337 surface->wined3d_rtv = NULL;
6340 static const struct wined3d_parent_ops ddraw_view_wined3d_parent_ops =
6342 view_wined3d_object_destroyed,
6345 struct wined3d_rendertarget_view *ddraw_surface_get_rendertarget_view(struct ddraw_surface *surface)
6347 HRESULT hr;
6349 if (surface->wined3d_rtv)
6350 return surface->wined3d_rtv;
6352 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
6353 surface->sub_resource_idx, surface, &ddraw_view_wined3d_parent_ops, &surface->wined3d_rtv)))
6355 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
6356 return NULL;
6359 return surface->wined3d_rtv;