include: Correctly spell STATUS_SMI_PRIMITIVE_INSTALLER_FAILED.
[wine/multimedia.git] / dlls / ddraw / surface.c
blobfd2c1a4d7077adf7c09158be69b4ae14b5464290
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;
50 if (!rect)
52 x = 0;
53 y = 0;
54 w = surface->surface_desc.dwWidth;
55 h = surface->surface_desc.dwHeight;
57 else
59 x = rect->left;
60 y = rect->top;
61 w = rect->right - rect->left;
62 h = rect->bottom - rect->top;
65 if (w <= 0 || h <= 0)
66 return DD_OK;
68 if (surface->ddraw->swapchain_window)
70 /* Nothing to do, we control the frontbuffer, or at least the parts we
71 * care about. */
72 if (read)
73 return DD_OK;
75 return wined3d_surface_blt(surface->ddraw->wined3d_frontbuffer, rect,
76 surface->wined3d_surface, rect, 0, NULL, WINED3D_TEXF_POINT);
79 if (FAILED(hr = wined3d_surface_getdc(surface->wined3d_surface, &surface_dc)))
81 ERR("Failed to get surface DC, hr %#x.\n", hr);
82 return hr;
85 if (!(screen_dc = GetDC(NULL)))
87 wined3d_surface_releasedc(surface->wined3d_surface, surface_dc);
88 ERR("Failed to get screen DC.\n");
89 return E_FAIL;
92 if (read)
93 ret = BitBlt(surface_dc, x, y, w, h,
94 screen_dc, x, y, SRCCOPY);
95 else
96 ret = BitBlt(screen_dc, x, y, w, h,
97 surface_dc, x, y, SRCCOPY);
99 ReleaseDC(NULL, screen_dc);
100 wined3d_surface_releasedc(surface->wined3d_surface, surface_dc);
102 if (!ret)
104 ERR("Failed to blit to/from screen.\n");
105 return E_FAIL;
108 return DD_OK;
111 /*****************************************************************************
112 * IUnknown parts follow
113 *****************************************************************************/
115 /*****************************************************************************
116 * IDirectDrawSurface7::QueryInterface
118 * A normal QueryInterface implementation. For QueryInterface rules
119 * see ddraw.c, IDirectDraw7::QueryInterface. This method
120 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
121 * in all versions, the IDirectDrawGammaControl interface and it can
122 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
124 * Params:
125 * riid: The interface id queried for
126 * obj: Address to write the pointer to
128 * Returns:
129 * S_OK on success
130 * E_NOINTERFACE if the requested interface wasn't found
132 *****************************************************************************/
133 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
135 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
137 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
139 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
140 *obj = NULL;
142 if(!riid)
143 return DDERR_INVALIDPARAMS;
145 if (IsEqualGUID(riid, &IID_IDirectDrawSurface7))
147 IDirectDrawSurface7_AddRef(iface);
148 *obj = iface;
149 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
150 return S_OK;
153 if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
155 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
156 *obj = &This->IDirectDrawSurface4_iface;
157 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
158 return S_OK;
161 if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
163 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
164 *obj = &This->IDirectDrawSurface3_iface;
165 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
166 return S_OK;
169 if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
171 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
172 *obj = &This->IDirectDrawSurface2_iface;
173 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
174 return S_OK;
177 if (IsEqualGUID(riid, &IID_IDirectDrawSurface)
178 || IsEqualGUID(riid, &IID_IUnknown))
180 IDirectDrawSurface_AddRef(&This->IDirectDrawSurface_iface);
181 *obj = &This->IDirectDrawSurface_iface;
182 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
183 return S_OK;
186 if (IsEqualGUID(riid, &IID_IDirectDrawGammaControl))
188 IDirectDrawGammaControl_AddRef(&This->IDirectDrawGammaControl_iface);
189 *obj = &This->IDirectDrawGammaControl_iface;
190 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
191 return S_OK;
194 if (IsEqualGUID(riid, &IID_IDirectDrawColorControl))
196 WARN("Color control not implemented.\n");
197 *obj = NULL;
198 return E_NOINTERFACE;
201 if (This->version != 7)
203 if (IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D)
204 || IsEqualGUID(riid, &IID_IDirect3DHALDevice)
205 || IsEqualGUID(riid, &IID_IDirect3DRGBDevice))
207 wined3d_mutex_lock();
208 if (!This->device1)
210 HRESULT hr;
212 if (FAILED(hr = d3d_device_create(This->ddraw, This, (IUnknown *)&This->IDirectDrawSurface_iface,
213 1, &This->device1, (IUnknown *)&This->IDirectDrawSurface_iface)))
215 This->device1 = NULL;
216 wined3d_mutex_unlock();
217 WARN("Failed to create device, hr %#x.\n", hr);
218 return hr;
221 wined3d_mutex_unlock();
223 IDirect3DDevice_AddRef(&This->device1->IDirect3DDevice_iface);
224 *obj = &This->device1->IDirect3DDevice_iface;
225 return S_OK;
228 if (IsEqualGUID(&IID_IDirect3DTexture2, riid))
230 IDirect3DTexture2_AddRef(&This->IDirect3DTexture2_iface);
231 *obj = &This->IDirect3DTexture2_iface;
232 return S_OK;
235 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
237 IDirect3DTexture2_AddRef(&This->IDirect3DTexture_iface);
238 *obj = &This->IDirect3DTexture_iface;
239 return S_OK;
243 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
245 if (This->version != 7)
246 return E_INVALIDARG;
248 return E_NOINTERFACE;
251 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
253 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
255 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
257 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
260 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
262 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
264 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
266 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
269 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
271 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
273 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
275 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
278 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
280 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
282 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
284 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
287 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
288 REFIID riid, void **object)
290 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
292 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
294 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
297 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
299 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
301 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
303 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
306 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
308 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
310 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
312 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
315 static void ddraw_surface_add_iface(struct ddraw_surface *surface)
317 ULONG iface_count = InterlockedIncrement(&surface->iface_count);
318 TRACE("%p increasing iface count to %u.\n", surface, iface_count);
320 if (iface_count == 1)
322 if (surface->ifaceToRelease)
323 IUnknown_AddRef(surface->ifaceToRelease);
324 wined3d_mutex_lock();
325 if (surface->wined3d_surface)
326 wined3d_surface_incref(surface->wined3d_surface);
327 if (surface->wined3d_texture)
328 wined3d_texture_incref(surface->wined3d_texture);
329 wined3d_mutex_unlock();
333 /*****************************************************************************
334 * IDirectDrawSurface7::AddRef
336 * A normal addref implementation
338 * Returns:
339 * The new refcount
341 *****************************************************************************/
342 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
344 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
345 ULONG refcount = InterlockedIncrement(&This->ref7);
347 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
349 if (refcount == 1)
351 ddraw_surface_add_iface(This);
354 return refcount;
357 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
359 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
360 ULONG refcount = InterlockedIncrement(&This->ref4);
362 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
364 if (refcount == 1)
366 ddraw_surface_add_iface(This);
369 return refcount;
372 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
374 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
375 ULONG refcount = InterlockedIncrement(&This->ref3);
377 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
379 if (refcount == 1)
381 ddraw_surface_add_iface(This);
384 return refcount;
387 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
389 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
390 ULONG refcount = InterlockedIncrement(&This->ref2);
392 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
394 if (refcount == 1)
396 ddraw_surface_add_iface(This);
399 return refcount;
402 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
404 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
405 ULONG refcount = InterlockedIncrement(&This->ref1);
407 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
409 if (refcount == 1)
411 ddraw_surface_add_iface(This);
414 return refcount;
417 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
419 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
420 ULONG refcount = InterlockedIncrement(&This->gamma_count);
422 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
424 if (refcount == 1)
426 ddraw_surface_add_iface(This);
429 return refcount;
432 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
434 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
436 TRACE("iface %p.\n", iface);
438 return IUnknown_AddRef(surface->texture_outer);
441 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
443 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
445 TRACE("iface %p.\n", iface);
447 return IUnknown_AddRef(surface->texture_outer);
450 static HRESULT ddraw_surface_set_palette(struct ddraw_surface *surface, IDirectDrawPalette *palette)
452 struct ddraw_palette *palette_impl = unsafe_impl_from_IDirectDrawPalette(palette);
453 struct ddraw_palette *prev;
455 TRACE("iface %p, palette %p.\n", surface, palette);
457 if (palette_impl && palette_impl->flags & DDPCAPS_ALPHA
458 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
460 WARN("Alpha palette set on non-texture surface, returning DDERR_INVALIDSURFACETYPE.\n");
461 return DDERR_INVALIDSURFACETYPE;
464 if (!format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
465 return DDERR_INVALIDPIXELFORMAT;
467 wined3d_mutex_lock();
469 prev = surface->palette;
470 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
472 if (prev)
473 prev->flags &= ~DDPCAPS_PRIMARYSURFACE;
474 if (palette_impl)
475 palette_impl->flags |= DDPCAPS_PRIMARYSURFACE;
476 wined3d_swapchain_set_palette(surface->ddraw->wined3d_swapchain,
477 palette_impl ? palette_impl->wineD3DPalette : NULL);
478 ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
480 if (palette_impl)
481 IDirectDrawPalette_AddRef(&palette_impl->IDirectDrawPalette_iface);
482 if (prev)
483 IDirectDrawPalette_Release(&prev->IDirectDrawPalette_iface);
484 surface->palette = palette_impl;
486 wined3d_mutex_unlock();
488 return DD_OK;
491 static void ddraw_surface_cleanup(struct ddraw_surface *surface)
493 struct ddraw_surface *surf;
494 UINT i;
496 TRACE("surface %p.\n", surface);
498 /* The refcount test shows that the palette is detached when the surface
499 * is destroyed. */
500 ddraw_surface_set_palette(surface, NULL);
502 /* Loop through all complex attached surfaces and destroy them.
504 * Yet again, only the root can have more than one complexly attached
505 * surface, all the others have a total of one. */
506 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
508 if (!surface->complex_array[i])
509 break;
511 surf = surface->complex_array[i];
512 surface->complex_array[i] = NULL;
513 if (!surf->is_complex_root)
514 ddraw_surface_cleanup(surf);
517 if (surface->device1)
518 IUnknown_Release(&surface->device1->IUnknown_inner);
520 if (surface->iface_count > 1)
522 /* This can happen when a complex surface is destroyed, because the
523 * 2nd surface was addref()ed when the app called
524 * GetAttachedSurface(). */
525 WARN("Destroying surface %p with refcounts 7: %u 4: %u 3: %u 2: %u 1: %u.\n",
526 surface, surface->ref7, surface->ref4, surface->ref3, surface->ref2, surface->ref1);
529 if (surface->wined3d_texture)
530 wined3d_texture_decref(surface->wined3d_texture);
531 if (surface->wined3d_surface)
532 wined3d_surface_decref(surface->wined3d_surface);
535 ULONG ddraw_surface_release_iface(struct ddraw_surface *This)
537 ULONG iface_count = InterlockedDecrement(&This->iface_count);
538 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
540 if (iface_count == 0)
542 IUnknown *release_iface = This->ifaceToRelease;
544 /* Complex attached surfaces are destroyed implicitly when the root is released */
545 wined3d_mutex_lock();
546 if(!This->is_complex_root)
548 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
549 wined3d_mutex_unlock();
550 return iface_count;
552 ddraw_surface_cleanup(This);
553 wined3d_mutex_unlock();
555 if (release_iface)
556 IUnknown_Release(release_iface);
559 return iface_count;
562 /*****************************************************************************
563 * IDirectDrawSurface7::Release
565 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
566 * surface is destroyed.
568 * Destroying the surface is a bit tricky. For the connection between
569 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
570 * It has a nice graph explaining the connection.
572 * What happens here is basically this:
573 * When a surface is destroyed, its WineD3DSurface is released,
574 * and the refcount of the DirectDraw interface is reduced by 1. If it has
575 * complex surfaces attached to it, then these surfaces are destroyed too,
576 * regardless of their refcount. If any surface being destroyed has another
577 * surface attached to it (with a "soft" attachment, not complex), then
578 * this surface is detached with DeleteAttachedSurface.
580 * When the surface is a texture, the WineD3DTexture is released.
581 * If the surface is the Direct3D render target, then the D3D
582 * capabilities of the WineD3DDevice are uninitialized, which causes the
583 * swapchain to be released.
585 * When a complex sublevel falls to ref zero, then this is ignored.
587 * Returns:
588 * The new refcount
590 *****************************************************************************/
591 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
593 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
594 ULONG refcount = InterlockedDecrement(&This->ref7);
596 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
598 if (refcount == 0)
600 ddraw_surface_release_iface(This);
603 return refcount;
606 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
608 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
609 ULONG refcount = InterlockedDecrement(&This->ref4);
611 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
613 if (refcount == 0)
615 ddraw_surface_release_iface(This);
618 return refcount;
621 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
623 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
624 ULONG refcount = InterlockedDecrement(&This->ref3);
626 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
628 if (refcount == 0)
630 ddraw_surface_release_iface(This);
633 return refcount;
636 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
638 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
639 ULONG refcount = InterlockedDecrement(&This->ref2);
641 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
643 if (refcount == 0)
645 ddraw_surface_release_iface(This);
648 return refcount;
651 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
653 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
654 ULONG refcount = InterlockedDecrement(&This->ref1);
656 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
658 if (refcount == 0)
660 ddraw_surface_release_iface(This);
663 return refcount;
666 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
668 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
669 ULONG refcount = InterlockedDecrement(&This->gamma_count);
671 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
673 if (refcount == 0)
675 ddraw_surface_release_iface(This);
678 return refcount;
681 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
683 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
685 TRACE("iface %p.\n", iface);
687 return IUnknown_Release(surface->texture_outer);
690 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
692 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
694 TRACE("iface %p.\n", iface);
696 return IUnknown_Release(surface->texture_outer);
699 /*****************************************************************************
700 * IDirectDrawSurface7::GetAttachedSurface
702 * Returns an attached surface with the requested caps. Surface attachment
703 * and complex surfaces are not clearly described by the MSDN or sdk,
704 * so this method is tricky and likely to contain problems.
705 * This implementation searches the complex list first, then the
706 * attachment chain.
708 * The chains are searched from This down to the last surface in the chain,
709 * not from the first element in the chain. The first surface found is
710 * returned. The MSDN says that this method fails if more than one surface
711 * matches the caps, but it is not sure if that is right. The attachment
712 * structure may not even allow two matching surfaces.
714 * The found surface is AddRef-ed before it is returned.
716 * Params:
717 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
718 * Surface: Address to store the found surface
720 * Returns:
721 * DD_OK on success
722 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
723 * DDERR_NOTFOUND if no surface was found
725 *****************************************************************************/
726 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
727 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
729 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
730 struct ddraw_surface *surf;
731 DDSCAPS2 our_caps;
732 int i;
734 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
736 wined3d_mutex_lock();
738 if(This->version < 7)
740 /* Earlier dx apps put garbage into these members, clear them */
741 our_caps.dwCaps = Caps->dwCaps;
742 our_caps.dwCaps2 = 0;
743 our_caps.dwCaps3 = 0;
744 our_caps.dwCaps4 = 0;
746 else
748 our_caps = *Caps;
751 TRACE("(%p): Looking for caps: %x,%x,%x,%x\n", This, our_caps.dwCaps, our_caps.dwCaps2, our_caps.dwCaps3, our_caps.dwCaps4); /* FIXME: Better debugging */
753 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
755 surf = This->complex_array[i];
756 if(!surf) break;
758 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
759 surf->surface_desc.ddsCaps.dwCaps,
760 surf->surface_desc.ddsCaps.dwCaps2,
761 surf->surface_desc.ddsCaps.dwCaps3,
762 surf->surface_desc.ddsCaps.dwCaps4);
764 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
765 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
767 /* MSDN: "This method fails if more than one surface is attached
768 * that matches the capabilities requested."
770 * Not sure how to test this.
773 TRACE("(%p): Returning surface %p\n", This, surf);
774 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
775 *Surface = &surf->IDirectDrawSurface7_iface;
776 ddraw_surface7_AddRef(*Surface);
777 wined3d_mutex_unlock();
779 return DD_OK;
783 /* Next, look at the attachment chain */
784 surf = This;
786 while( (surf = surf->next_attached) )
788 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
789 surf->surface_desc.ddsCaps.dwCaps,
790 surf->surface_desc.ddsCaps.dwCaps2,
791 surf->surface_desc.ddsCaps.dwCaps3,
792 surf->surface_desc.ddsCaps.dwCaps4);
794 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
795 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
797 TRACE("(%p): Returning surface %p\n", This, surf);
798 *Surface = &surf->IDirectDrawSurface7_iface;
799 ddraw_surface7_AddRef(*Surface);
800 wined3d_mutex_unlock();
801 return DD_OK;
805 TRACE("(%p) Didn't find a valid surface\n", This);
807 wined3d_mutex_unlock();
809 *Surface = NULL;
810 return DDERR_NOTFOUND;
813 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
814 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
816 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
817 struct ddraw_surface *attachment_impl;
818 IDirectDrawSurface7 *attachment7;
819 HRESULT hr;
821 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
823 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
824 caps, &attachment7);
825 if (FAILED(hr))
827 *attachment = NULL;
828 return hr;
830 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
831 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
832 ddraw_surface4_AddRef(*attachment);
833 ddraw_surface7_Release(attachment7);
835 return hr;
838 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
839 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
841 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
842 struct ddraw_surface *attachment_impl;
843 IDirectDrawSurface7 *attachment7;
844 DDSCAPS2 caps2;
845 HRESULT hr;
847 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
849 caps2.dwCaps = caps->dwCaps;
850 caps2.dwCaps2 = 0;
851 caps2.dwCaps3 = 0;
852 caps2.dwCaps4 = 0;
854 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
855 &caps2, &attachment7);
856 if (FAILED(hr))
858 *attachment = NULL;
859 return hr;
861 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
862 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
863 ddraw_surface3_AddRef(*attachment);
864 ddraw_surface7_Release(attachment7);
866 return hr;
869 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
870 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
872 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
873 struct ddraw_surface *attachment_impl;
874 IDirectDrawSurface7 *attachment7;
875 DDSCAPS2 caps2;
876 HRESULT hr;
878 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
880 caps2.dwCaps = caps->dwCaps;
881 caps2.dwCaps2 = 0;
882 caps2.dwCaps3 = 0;
883 caps2.dwCaps4 = 0;
885 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
886 &caps2, &attachment7);
887 if (FAILED(hr))
889 *attachment = NULL;
890 return hr;
892 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
893 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
894 ddraw_surface2_AddRef(*attachment);
895 ddraw_surface7_Release(attachment7);
897 return hr;
900 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
901 DDSCAPS *caps, IDirectDrawSurface **attachment)
903 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
904 struct ddraw_surface *attachment_impl;
905 IDirectDrawSurface7 *attachment7;
906 DDSCAPS2 caps2;
907 HRESULT hr;
909 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
911 caps2.dwCaps = caps->dwCaps;
912 caps2.dwCaps2 = 0;
913 caps2.dwCaps3 = 0;
914 caps2.dwCaps4 = 0;
916 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
917 &caps2, &attachment7);
918 if (FAILED(hr))
920 *attachment = NULL;
921 return hr;
923 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
924 *attachment = &attachment_impl->IDirectDrawSurface_iface;
925 ddraw_surface1_AddRef(*attachment);
926 ddraw_surface7_Release(attachment7);
928 return hr;
931 /*****************************************************************************
932 * IDirectDrawSurface7::Lock
934 * Locks the surface and returns a pointer to the surface's memory
936 * Params:
937 * Rect: Rectangle to lock. If NULL, the whole surface is locked
938 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
939 * Flags: Locking flags, e.g Read only or write only
940 * h: An event handle that's not used and must be NULL
942 * Returns:
943 * DD_OK on success
944 * DDERR_INVALIDPARAMS if DDSD is NULL
945 * For more details, see IWineD3DSurface::LockRect
947 *****************************************************************************/
948 static HRESULT surface_lock(struct ddraw_surface *This,
949 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
951 struct wined3d_map_desc map_desc;
952 HRESULT hr = DD_OK;
954 TRACE("This %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
955 This, wine_dbgstr_rect(Rect), DDSD, Flags, h);
957 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
958 wined3d_mutex_lock();
960 /* Should I check for the handle to be NULL?
962 * The DDLOCK flags and the D3DLOCK flags are equal
963 * for the supported values. The others are ignored by WineD3D
966 /* Windows zeroes this if the rect is invalid */
967 DDSD->lpSurface = 0;
969 if (Rect)
971 if ((Rect->left < 0)
972 || (Rect->top < 0)
973 || (Rect->left > Rect->right)
974 || (Rect->top > Rect->bottom)
975 || (Rect->right > This->surface_desc.dwWidth)
976 || (Rect->bottom > This->surface_desc.dwHeight))
978 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
979 wined3d_mutex_unlock();
980 return DDERR_INVALIDPARAMS;
984 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
985 hr = ddraw_surface_update_frontbuffer(This, Rect, TRUE);
986 if (SUCCEEDED(hr))
987 hr = wined3d_surface_map(This->wined3d_surface, &map_desc, Rect, Flags);
988 if (FAILED(hr))
990 wined3d_mutex_unlock();
991 switch(hr)
993 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
994 * specific error. But since IWineD3DSurface::LockRect returns that error in this
995 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
996 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
997 * is much easier to do it in one place in ddraw
999 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
1000 default: return hr;
1004 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1006 if (Flags & DDLOCK_READONLY)
1007 memset(&This->ddraw->primary_lock, 0, sizeof(This->ddraw->primary_lock));
1008 else if (Rect)
1009 This->ddraw->primary_lock = *Rect;
1010 else
1011 SetRect(&This->ddraw->primary_lock, 0, 0, This->surface_desc.dwWidth, This->surface_desc.dwHeight);
1014 /* Override the memory area. The pitch should be set already. Strangely windows
1015 * does not set the LPSURFACE flag on locked surfaces !?!.
1016 * DDSD->dwFlags |= DDSD_LPSURFACE;
1018 This->surface_desc.lpSurface = map_desc.data;
1019 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
1021 TRACE("locked surface returning description :\n");
1022 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1024 wined3d_mutex_unlock();
1026 return DD_OK;
1029 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1030 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1032 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1034 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1035 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1037 if (!surface_desc) return DDERR_INVALIDPARAMS;
1038 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1039 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1041 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1042 return DDERR_INVALIDPARAMS;
1044 return surface_lock(surface, rect, surface_desc, flags, h);
1047 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1048 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1050 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1052 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1053 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1055 if (!surface_desc) return DDERR_INVALIDPARAMS;
1056 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1057 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1059 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1060 return DDERR_INVALIDPARAMS;
1062 return surface_lock(surface, rect, surface_desc, flags, h);
1065 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1066 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1068 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1069 DDSURFACEDESC2 surface_desc2;
1070 HRESULT hr;
1072 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1073 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1075 if (!surface_desc) return DDERR_INVALIDPARAMS;
1076 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1077 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1079 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1080 return DDERR_INVALIDPARAMS;
1083 surface_desc2.dwSize = surface_desc->dwSize;
1084 surface_desc2.dwFlags = 0;
1085 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1086 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1087 surface_desc->dwSize = surface_desc2.dwSize;
1088 return hr;
1091 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1092 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1094 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1095 DDSURFACEDESC2 surface_desc2;
1096 HRESULT hr;
1098 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1099 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1101 if (!surface_desc) return DDERR_INVALIDPARAMS;
1102 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1103 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1105 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1106 return DDERR_INVALIDPARAMS;
1109 surface_desc2.dwSize = surface_desc->dwSize;
1110 surface_desc2.dwFlags = 0;
1111 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1112 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1113 surface_desc->dwSize = surface_desc2.dwSize;
1114 return hr;
1117 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1118 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1120 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1121 DDSURFACEDESC2 surface_desc2;
1122 HRESULT hr;
1123 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1124 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1126 if (!surface_desc) return DDERR_INVALIDPARAMS;
1127 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1128 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1130 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1131 return DDERR_INVALIDPARAMS;
1134 surface_desc2.dwSize = surface_desc->dwSize;
1135 surface_desc2.dwFlags = 0;
1136 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1137 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1138 surface_desc->dwSize = surface_desc2.dwSize;
1139 return hr;
1142 /*****************************************************************************
1143 * IDirectDrawSurface7::Unlock
1145 * Unlocks an locked surface
1147 * Params:
1148 * Rect: Not used by this implementation
1150 * Returns:
1151 * D3D_OK on success
1152 * For more details, see IWineD3DSurface::UnlockRect
1154 *****************************************************************************/
1155 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
1157 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1158 HRESULT hr;
1160 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1162 wined3d_mutex_lock();
1163 hr = wined3d_surface_unmap(surface->wined3d_surface);
1164 if (SUCCEEDED(hr))
1166 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1167 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE);
1168 surface->surface_desc.lpSurface = NULL;
1170 wined3d_mutex_unlock();
1172 return hr;
1175 static HRESULT WINAPI ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1177 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1179 TRACE("iface %p, rect %p.\n", iface, pRect);
1181 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, pRect);
1184 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1186 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1188 TRACE("iface %p, data %p.\n", iface, data);
1190 /* data might not be the LPRECT of later versions, so drop it. */
1191 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1194 static HRESULT WINAPI ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1196 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1198 TRACE("iface %p, data %p.\n", iface, data);
1200 /* data might not be the LPRECT of later versions, so drop it. */
1201 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1204 static HRESULT WINAPI ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1206 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1208 TRACE("iface %p, data %p.\n", iface, data);
1210 /* data might not be the LPRECT of later versions, so drop it. */
1211 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1214 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *src, DWORD flags)
1216 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
1217 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src);
1218 struct ddraw_texture *ddraw_texture, *prev_ddraw_texture;
1219 DDSCAPS2 caps = {DDSCAPS_FLIP, 0, 0, 0};
1220 struct wined3d_surface *tmp, *rt;
1221 struct wined3d_texture *texture;
1222 IDirectDrawSurface7 *current;
1223 HRESULT hr;
1225 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1227 if (src == iface || !(dst_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1228 return DDERR_NOTFLIPPABLE;
1230 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
1231 return DDERR_SURFACELOST;
1233 wined3d_mutex_lock();
1235 tmp = dst_impl->wined3d_surface;
1236 texture = dst_impl->wined3d_texture;
1237 rt = wined3d_device_get_render_target(dst_impl->ddraw->wined3d_device, 0);
1238 ddraw_texture = wined3d_texture_get_parent(dst_impl->wined3d_texture);
1240 if (src_impl)
1242 for (current = iface; current != src;)
1244 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1246 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1247 wined3d_mutex_unlock();
1248 return DDERR_NOTFLIPPABLE;
1250 ddraw_surface7_Release(current);
1251 if (current == iface)
1253 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1254 wined3d_mutex_unlock();
1255 return DDERR_NOTFLIPPABLE;
1259 if (rt == dst_impl->wined3d_surface)
1260 wined3d_device_set_render_target(dst_impl->ddraw->wined3d_device, 0, src_impl->wined3d_surface, FALSE);
1261 wined3d_resource_set_parent(wined3d_surface_get_resource(src_impl->wined3d_surface), dst_impl);
1262 dst_impl->wined3d_surface = src_impl->wined3d_surface;
1263 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1264 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1265 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1266 ddraw_texture = prev_ddraw_texture;
1268 else
1270 for (current = iface;;)
1272 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1274 ERR("Can't find a flip target\n");
1275 wined3d_mutex_unlock();
1276 return DDERR_NOTFLIPPABLE; /* Unchecked */
1278 ddraw_surface7_Release(current);
1279 if (current == iface)
1281 dst_impl = impl_from_IDirectDrawSurface7(iface);
1282 break;
1285 src_impl = impl_from_IDirectDrawSurface7(current);
1286 if (rt == dst_impl->wined3d_surface)
1287 wined3d_device_set_render_target(dst_impl->ddraw->wined3d_device, 0, src_impl->wined3d_surface, FALSE);
1288 wined3d_resource_set_parent(wined3d_surface_get_resource(src_impl->wined3d_surface), dst_impl);
1289 dst_impl->wined3d_surface = src_impl->wined3d_surface;
1290 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1291 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1292 ddraw_texture = prev_ddraw_texture;
1293 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1294 dst_impl = src_impl;
1298 /* We don't have to worry about potential texture bindings, since
1299 * flippable surfaces can never be textures. */
1300 if (rt == src_impl->wined3d_surface)
1301 wined3d_device_set_render_target(dst_impl->ddraw->wined3d_device, 0, tmp, FALSE);
1302 wined3d_resource_set_parent(wined3d_surface_get_resource(tmp), src_impl);
1303 src_impl->wined3d_surface = tmp;
1304 wined3d_resource_set_parent(wined3d_texture_get_resource(texture), ddraw_texture);
1305 src_impl->wined3d_texture = texture;
1307 if (flags)
1309 static UINT once;
1310 if (!once++)
1311 FIXME("Ignoring flags %#x.\n", flags);
1312 else
1313 WARN("Ignoring flags %#x.\n", flags);
1316 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1317 hr = ddraw_surface_update_frontbuffer(dst_impl, NULL, FALSE);
1318 else
1319 hr = DD_OK;
1321 wined3d_mutex_unlock();
1323 return hr;
1326 static HRESULT WINAPI ddraw_surface4_Flip(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *dst, DWORD flags)
1328 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1329 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1331 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1333 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1334 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1337 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
1339 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1340 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1342 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1344 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1345 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1348 static HRESULT WINAPI ddraw_surface2_Flip(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *dst, DWORD flags)
1350 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1351 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1353 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1355 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1356 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1359 static HRESULT WINAPI ddraw_surface1_Flip(IDirectDrawSurface *iface, IDirectDrawSurface *dst, DWORD flags)
1361 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1362 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1364 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1366 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1367 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1370 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1371 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags,
1372 const WINEDDBLTFX *fx, enum wined3d_texture_filter_type filter)
1374 struct wined3d_surface *wined3d_src_surface = src_surface ? src_surface->wined3d_surface : NULL;
1375 RECT src_rect, dst_rect;
1376 float scale_x, scale_y;
1377 const RECT *clip_rect;
1378 UINT clip_list_size;
1379 RGNDATA *clip_list;
1380 HRESULT hr = DD_OK;
1381 UINT i;
1383 if (!dst_surface->clipper)
1385 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1386 hr = ddraw_surface_update_frontbuffer(src_surface, src_rect_in, TRUE);
1387 if (SUCCEEDED(hr))
1388 hr = wined3d_surface_blt(dst_surface->wined3d_surface, dst_rect_in,
1389 wined3d_src_surface, src_rect_in, flags, fx, filter);
1390 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
1391 hr = ddraw_surface_update_frontbuffer(dst_surface, dst_rect_in, FALSE);
1393 return hr;
1396 if (!dst_rect_in)
1398 dst_rect.left = 0;
1399 dst_rect.top = 0;
1400 dst_rect.right = dst_surface->surface_desc.dwWidth;
1401 dst_rect.bottom = dst_surface->surface_desc.dwHeight;
1403 else
1405 dst_rect = *dst_rect_in;
1408 if (IsRectEmpty(&dst_rect))
1409 return DDERR_INVALIDRECT;
1411 if (src_surface)
1413 if (!src_rect_in)
1415 src_rect.left = 0;
1416 src_rect.top = 0;
1417 src_rect.right = src_surface->surface_desc.dwWidth;
1418 src_rect.bottom = src_surface->surface_desc.dwHeight;
1420 else
1422 src_rect = *src_rect_in;
1425 if (IsRectEmpty(&src_rect))
1426 return DDERR_INVALIDRECT;
1428 else
1430 SetRect(&src_rect, 0, 0, 0, 0);
1433 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1434 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1436 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1437 &dst_rect, NULL, &clip_list_size)))
1439 WARN("Failed to get clip list size, hr %#x.\n", hr);
1440 return hr;
1443 if (!(clip_list = HeapAlloc(GetProcessHeap(), 0, clip_list_size)))
1445 WARN("Failed to allocate clip list.\n");
1446 return E_OUTOFMEMORY;
1449 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1450 &dst_rect, clip_list, &clip_list_size)))
1452 WARN("Failed to get clip list, hr %#x.\n", hr);
1453 HeapFree(GetProcessHeap(), 0, clip_list);
1454 return hr;
1457 clip_rect = (RECT *)clip_list->Buffer;
1458 for (i = 0; i < clip_list->rdh.nCount; ++i)
1460 RECT src_rect_clipped = src_rect;
1462 if (src_surface)
1464 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1465 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1466 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1467 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1469 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1471 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE)))
1472 break;
1476 if (FAILED(hr = wined3d_surface_blt(dst_surface->wined3d_surface, &clip_rect[i],
1477 wined3d_src_surface, &src_rect_clipped, flags, fx, filter)))
1478 break;
1480 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1482 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE)))
1483 break;
1487 HeapFree(GetProcessHeap(), 0, clip_list);
1488 return hr;
1491 /*****************************************************************************
1492 * IDirectDrawSurface7::Blt
1494 * Performs a blit on the surface
1496 * Params:
1497 * DestRect: Destination rectangle, can be NULL
1498 * SrcSurface: Source surface, can be NULL
1499 * SrcRect: Source rectangle, can be NULL
1500 * Flags: Blt flags
1501 * DDBltFx: Some extended blt parameters, connected to the flags
1503 * Returns:
1504 * D3D_OK on success
1505 * See IWineD3DSurface::Blt for more details
1507 *****************************************************************************/
1508 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1509 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1511 struct ddraw_surface *dst_surface = impl_from_IDirectDrawSurface7(iface);
1512 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1513 HRESULT hr = DD_OK;
1515 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1516 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1518 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1519 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1521 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1522 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1523 return DDERR_INVALIDPARAMS;
1526 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1527 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1528 return DDERR_INVALIDPARAMS;
1531 wined3d_mutex_lock();
1533 if (Flags & DDBLT_KEYSRC && (!src_surface || !(src_surface->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1535 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1536 wined3d_mutex_unlock();
1537 return DDERR_INVALIDPARAMS;
1540 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1541 * does, copy the struct, and replace the ddraw surfaces with the wined3d
1542 * surfaces. So far no blitting operations using surfaces in the bltfx
1543 * struct are supported anyway. */
1544 hr = ddraw_surface_blt_clipped(dst_surface, DestRect, src_surface, SrcRect,
1545 Flags, (WINEDDBLTFX *)DDBltFx, WINED3D_TEXF_LINEAR);
1547 wined3d_mutex_unlock();
1548 switch(hr)
1550 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1551 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
1552 default: return hr;
1556 static HRESULT WINAPI ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1557 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1559 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1560 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1562 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1563 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1565 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1566 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1569 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1570 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1572 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1573 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1575 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1576 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1578 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1579 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1582 static HRESULT WINAPI ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1583 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1585 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1586 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1588 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1589 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1591 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1592 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1595 static HRESULT WINAPI ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1596 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1598 struct ddraw_surface *dst = impl_from_IDirectDrawSurface(iface);
1599 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1601 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1602 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1604 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1605 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1608 /*****************************************************************************
1609 * IDirectDrawSurface7::AddAttachedSurface
1611 * Attaches a surface to another surface. How the surface attachments work
1612 * is not totally understood yet, and this method is prone to problems.
1613 * The surface that is attached is AddRef-ed.
1615 * Tests with complex surfaces suggest that the surface attachments form a
1616 * tree, but no method to test this has been found yet.
1618 * The attachment list consists of a first surface (first_attached) and
1619 * for each surface a pointer to the next attached surface (next_attached).
1620 * For the first surface, and a surface that has no attachments
1621 * first_attached points to the surface itself. A surface that has
1622 * no successors in the chain has next_attached set to NULL.
1624 * Newly attached surfaces are attached right after the root surface.
1625 * If a surface is attached to a complex surface compound, it's attached to
1626 * the surface that the app requested, not the complex root. See
1627 * GetAttachedSurface for a description how surfaces are found.
1629 * This is how the current implementation works, and it was coded by looking
1630 * at the needs of the applications.
1632 * So far only Z-Buffer attachments are tested, and they are activated in
1633 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1634 * Back buffers should work in 2D mode, but they are not tested(They can be
1635 * attached in older iface versions). Rendering to the front buffer and
1636 * switching between that and double buffering is not yet implemented in
1637 * WineD3D, so for 3D it might have unexpected results.
1639 * ddraw_surface_attach_surface is the real thing,
1640 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1641 * performs additional checks. Version 7 of this interface is much more restrictive
1642 * than its predecessors.
1644 * Params:
1645 * Attach: Surface to attach to iface
1647 * Returns:
1648 * DD_OK on success
1649 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1651 *****************************************************************************/
1652 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1654 TRACE("surface %p, attachment %p.\n", This, Surf);
1656 if(Surf == This)
1657 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1659 wined3d_mutex_lock();
1661 /* Check if the surface is already attached somewhere */
1662 if (Surf->next_attached || Surf->first_attached != Surf)
1664 /* TODO: Test for the structure of the manual attachment. Is it a
1665 * chain or a list? What happens if one surface is attached to 2
1666 * different surfaces? */
1667 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1668 Surf, Surf->next_attached, Surf->first_attached);
1670 wined3d_mutex_unlock();
1671 return DDERR_SURFACEALREADYATTACHED;
1674 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1675 Surf->next_attached = This->next_attached;
1676 Surf->first_attached = This->first_attached;
1677 This->next_attached = Surf;
1679 /* Check if the WineD3D depth stencil needs updating */
1680 if (This->ddraw->d3ddevice)
1681 d3d_device_update_depth_stencil(This->ddraw->d3ddevice);
1683 wined3d_mutex_unlock();
1685 return DD_OK;
1688 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
1690 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
1691 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1692 HRESULT hr;
1694 TRACE("iface %p, attachment %p.\n", iface, attachment);
1696 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1697 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1700 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1701 attachment_impl->surface_desc.ddsCaps.dwCaps);
1702 return DDERR_CANNOTATTACHSURFACE;
1705 hr = ddraw_surface_attach_surface(This, attachment_impl);
1706 if (FAILED(hr))
1708 return hr;
1710 attachment_impl->attached_iface = (IUnknown *)attachment;
1711 IUnknown_AddRef(attachment_impl->attached_iface);
1712 return hr;
1715 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1717 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1718 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1719 HRESULT hr;
1721 TRACE("iface %p, attachment %p.\n", iface, attachment);
1723 /* Tests suggest that
1724 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1725 * -> offscreen plain surfaces can be attached to primaries
1726 * -> primaries can be attached to offscreen plain surfaces
1727 * -> z buffers can be attached to primaries */
1728 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1729 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1731 /* Sizes have to match */
1732 if (attachment_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
1733 || attachment_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
1735 WARN("Surface sizes do not match.\n");
1736 return DDERR_CANNOTATTACHSURFACE;
1739 else if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
1740 || !(attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER)))
1742 WARN("Invalid attachment combination.\n");
1743 return DDERR_CANNOTATTACHSURFACE;
1746 if (FAILED(hr = ddraw_surface_attach_surface(surface, attachment_impl)))
1747 return hr;
1749 attachment_impl->attached_iface = (IUnknown *)attachment;
1750 IUnknown_AddRef(attachment_impl->attached_iface);
1751 return hr;
1754 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1756 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1757 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1758 HRESULT hr;
1760 TRACE("iface %p, attachment %p.\n", iface, attachment);
1762 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1763 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1764 return hr;
1766 attachment_impl->attached_iface = (IUnknown *)attachment;
1767 IUnknown_AddRef(attachment_impl->attached_iface);
1768 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1769 return hr;
1772 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1774 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1775 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1776 HRESULT hr;
1778 TRACE("iface %p, attachment %p.\n", iface, attachment);
1780 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1781 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1782 return hr;
1784 attachment_impl->attached_iface = (IUnknown *)attachment;
1785 IUnknown_AddRef(attachment_impl->attached_iface);
1786 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1787 return hr;
1790 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1792 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1793 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1794 HRESULT hr;
1796 TRACE("iface %p, attachment %p.\n", iface, attachment);
1798 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1799 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1800 return hr;
1802 attachment_impl->attached_iface = (IUnknown *)attachment;
1803 IUnknown_AddRef(attachment_impl->attached_iface);
1804 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1805 return hr;
1808 /*****************************************************************************
1809 * IDirectDrawSurface7::DeleteAttachedSurface
1811 * Removes a surface from the attachment chain. The surface's refcount
1812 * is decreased by one after it has been removed
1814 * Params:
1815 * Flags: Some flags, not used by this implementation
1816 * Attach: Surface to detach
1818 * Returns:
1819 * DD_OK on success
1820 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1822 *****************************************************************************/
1823 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
1824 struct ddraw_surface *attachment, IUnknown *detach_iface)
1826 struct ddraw_surface *prev = surface;
1828 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
1830 wined3d_mutex_lock();
1831 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
1833 wined3d_mutex_unlock();
1834 return DDERR_CANNOTDETACHSURFACE;
1837 if (attachment->attached_iface != detach_iface)
1839 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
1840 wined3d_mutex_unlock();
1841 return DDERR_SURFACENOTATTACHED;
1844 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1845 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1847 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1848 /* FIXME: we should probably also subtract from dwMipMapCount of this
1849 * and all parent surfaces */
1852 /* Find the predecessor of the detached surface */
1853 while (prev)
1855 if (prev->next_attached == attachment)
1856 break;
1857 prev = prev->next_attached;
1860 /* There must be a surface, otherwise there's a bug */
1861 assert(prev);
1863 /* Unchain the surface */
1864 prev->next_attached = attachment->next_attached;
1865 attachment->next_attached = NULL;
1866 attachment->first_attached = attachment;
1868 /* Check if the wined3d depth stencil needs updating. */
1869 if (surface->ddraw->d3ddevice)
1870 d3d_device_update_depth_stencil(surface->ddraw->d3ddevice);
1871 wined3d_mutex_unlock();
1873 /* Set attached_iface to NULL before releasing it, the surface may go
1874 * away. */
1875 attachment->attached_iface = NULL;
1876 IUnknown_Release(detach_iface);
1878 return DD_OK;
1881 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1882 DWORD flags, IDirectDrawSurface7 *attachment)
1884 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1885 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1887 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1889 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1892 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1893 DWORD flags, IDirectDrawSurface4 *attachment)
1895 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1896 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1898 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1900 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1903 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1904 DWORD flags, IDirectDrawSurface3 *attachment)
1906 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1907 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1909 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1911 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1914 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
1915 DWORD flags, IDirectDrawSurface2 *attachment)
1917 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1918 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1920 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1922 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1925 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
1926 DWORD flags, IDirectDrawSurface *attachment)
1928 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1929 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1931 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1933 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1936 /*****************************************************************************
1937 * IDirectDrawSurface7::AddOverlayDirtyRect
1939 * "This method is not currently implemented"
1941 * Params:
1942 * Rect: ?
1944 * Returns:
1945 * DDERR_UNSUPPORTED
1947 *****************************************************************************/
1948 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1950 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1952 return DDERR_UNSUPPORTED; /* unchecked */
1955 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
1957 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1959 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1961 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1964 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1966 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1968 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1970 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1973 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
1975 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1977 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1979 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1982 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
1984 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1986 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1988 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1991 /*****************************************************************************
1992 * IDirectDrawSurface7::GetDC
1994 * Returns a GDI device context for the surface
1996 * Params:
1997 * hdc: Address of a HDC variable to store the dc to
1999 * Returns:
2000 * DD_OK on success
2001 * DDERR_INVALIDPARAMS if hdc is NULL
2002 * For details, see IWineD3DSurface::GetDC
2004 *****************************************************************************/
2005 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
2007 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2008 HRESULT hr = DD_OK;
2010 TRACE("iface %p, dc %p.\n", iface, hdc);
2012 if(!hdc)
2013 return DDERR_INVALIDPARAMS;
2015 wined3d_mutex_lock();
2016 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2017 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE);
2018 if (SUCCEEDED(hr))
2019 hr = wined3d_surface_getdc(surface->wined3d_surface, hdc);
2021 if (SUCCEEDED(hr) && format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
2023 const struct ddraw_palette *palette;
2025 if (surface->palette)
2026 palette = surface->palette;
2027 else if (surface->ddraw->primary)
2028 palette = surface->ddraw->primary->palette;
2029 else
2030 palette = NULL;
2032 if (palette)
2033 wined3d_palette_apply_to_dc(palette->wineD3DPalette, *hdc);
2036 wined3d_mutex_unlock();
2037 switch(hr)
2039 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
2040 * touch *hdc
2042 case WINED3DERR_INVALIDCALL:
2043 if(hdc) *hdc = NULL;
2044 return DDERR_INVALIDPARAMS;
2046 default: return hr;
2050 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
2052 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2054 TRACE("iface %p, dc %p.\n", iface, dc);
2056 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2059 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
2061 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2063 TRACE("iface %p, dc %p.\n", iface, dc);
2065 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2068 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2070 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2072 TRACE("iface %p, dc %p.\n", iface, dc);
2074 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2077 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2079 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2081 TRACE("iface %p, dc %p.\n", iface, dc);
2083 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2086 /*****************************************************************************
2087 * IDirectDrawSurface7::ReleaseDC
2089 * Releases the DC that was constructed with GetDC
2091 * Params:
2092 * hdc: HDC to release
2094 * Returns:
2095 * DD_OK on success
2096 * For more details, see IWineD3DSurface::ReleaseDC
2098 *****************************************************************************/
2099 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2101 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2102 HRESULT hr;
2104 TRACE("iface %p, dc %p.\n", iface, hdc);
2106 wined3d_mutex_lock();
2107 hr = wined3d_surface_releasedc(surface->wined3d_surface, hdc);
2108 if (SUCCEEDED(hr) && (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2109 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
2110 wined3d_mutex_unlock();
2112 return hr;
2115 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2117 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2119 TRACE("iface %p, dc %p.\n", iface, dc);
2121 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2124 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2126 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2128 TRACE("iface %p, dc %p.\n", iface, dc);
2130 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2133 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2135 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2137 TRACE("iface %p, dc %p.\n", iface, dc);
2139 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2142 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2144 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2146 TRACE("iface %p, dc %p.\n", iface, dc);
2148 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2151 /*****************************************************************************
2152 * IDirectDrawSurface7::GetCaps
2154 * Returns the surface's caps
2156 * Params:
2157 * Caps: Address to write the caps to
2159 * Returns:
2160 * DD_OK on success
2161 * DDERR_INVALIDPARAMS if Caps is NULL
2163 *****************************************************************************/
2164 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2166 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2168 TRACE("iface %p, caps %p.\n", iface, Caps);
2170 if(!Caps)
2171 return DDERR_INVALIDPARAMS;
2173 *Caps = surface->surface_desc.ddsCaps;
2175 return DD_OK;
2178 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2180 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2182 TRACE("iface %p, caps %p.\n", iface, caps);
2184 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2187 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2189 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2190 DDSCAPS2 caps2;
2191 HRESULT hr;
2193 TRACE("iface %p, caps %p.\n", iface, caps);
2195 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2196 if (FAILED(hr)) return hr;
2198 caps->dwCaps = caps2.dwCaps;
2199 return hr;
2202 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2204 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2205 DDSCAPS2 caps2;
2206 HRESULT hr;
2208 TRACE("iface %p, caps %p.\n", iface, caps);
2210 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2211 if (FAILED(hr)) return hr;
2213 caps->dwCaps = caps2.dwCaps;
2214 return hr;
2217 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2219 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2220 DDSCAPS2 caps2;
2221 HRESULT hr;
2223 TRACE("iface %p, caps %p.\n", iface, caps);
2225 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2226 if (FAILED(hr)) return hr;
2228 caps->dwCaps = caps2.dwCaps;
2229 return hr;
2232 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD priority)
2234 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2235 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2236 HRESULT hr;
2237 struct wined3d_resource *resource;
2239 TRACE("iface %p, priority %u.\n", iface, priority);
2241 wined3d_mutex_lock();
2242 /* No need to check for offscreen plain surfaces or mipmap sublevels. SetPriority
2243 * calls on such surfaces segfault on Windows. */
2244 if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed))
2246 WARN("Called on non-managed texture returning DDERR_INVALIDPARAMS.\n");
2247 hr = DDERR_INVALIDPARAMS;
2249 else
2251 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2252 wined3d_resource_set_priority(resource, priority);
2253 hr = DD_OK;
2255 wined3d_mutex_unlock();
2257 return hr;
2260 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *priority)
2262 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2263 const struct wined3d_resource *resource;
2264 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2265 HRESULT hr;
2267 TRACE("iface %p, priority %p.\n", iface, priority);
2269 wined3d_mutex_lock();
2270 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN)
2272 WARN("Called on offscreenplain surface, returning DDERR_INVALIDOBJECT.\n");
2273 hr = DDERR_INVALIDOBJECT;
2275 else if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed) || !surface->wined3d_texture)
2277 WARN("Called on non-managed texture or mipmap sublevel, returning DDERR_INVALIDPARAMS.\n");
2278 hr = DDERR_INVALIDPARAMS;
2280 else
2282 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2283 *priority = wined3d_resource_get_priority(resource);
2284 hr = DD_OK;
2286 wined3d_mutex_unlock();
2288 return hr;
2291 /*****************************************************************************
2292 * IDirectDrawSurface7::SetPrivateData
2294 * Stores some data in the surface that is intended for the application's
2295 * use.
2297 * Params:
2298 * tag: GUID that identifies the data
2299 * Data: Pointer to the private data
2300 * Size: Size of the private data
2301 * Flags: Some flags
2303 * Returns:
2304 * D3D_OK on success
2305 * For more details, see IWineD3DSurface::SetPrivateData
2307 *****************************************************************************/
2308 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2309 REFGUID tag, void *data, DWORD size, DWORD flags)
2311 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2312 HRESULT hr;
2314 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2315 iface, debugstr_guid(tag), data, size, flags);
2317 if (!data)
2319 WARN("data is NULL, returning DDERR_INVALIDPARAMS.\n");
2320 return DDERR_INVALIDPARAMS;
2323 wined3d_mutex_lock();
2324 hr = wined3d_private_store_set_private_data(&surface->private_store, tag, data, size, flags);
2325 wined3d_mutex_unlock();
2326 return hr_ddraw_from_wined3d(hr);
2329 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2330 REFGUID tag, void *data, DWORD size, DWORD flags)
2332 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2334 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2335 iface, debugstr_guid(tag), data, size, flags);
2337 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2340 /*****************************************************************************
2341 * IDirectDrawSurface7::GetPrivateData
2343 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2345 * Params:
2346 * tag: GUID of the data to return
2347 * Data: Address where to write the data to
2348 * Size: Size of the buffer at Data
2350 * Returns:
2351 * DD_OK on success
2352 * DDERR_INVALIDPARAMS if Data is NULL
2353 * For more details, see IWineD3DSurface::GetPrivateData
2355 *****************************************************************************/
2356 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *data, DWORD *size)
2358 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2359 const struct wined3d_private_data *stored_data;
2360 HRESULT hr;
2362 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2363 iface, debugstr_guid(tag), data, size);
2365 wined3d_mutex_lock();
2366 stored_data = wined3d_private_store_get_private_data(&surface->private_store, tag);
2367 if (!stored_data)
2369 hr = DDERR_NOTFOUND;
2370 goto done;
2372 if (!size)
2374 hr = DDERR_INVALIDPARAMS;
2375 goto done;
2377 if (*size < stored_data->size)
2379 *size = stored_data->size;
2380 hr = DDERR_MOREDATA;
2381 goto done;
2383 if (!data)
2385 hr = DDERR_INVALIDPARAMS;
2386 goto done;
2389 *size = stored_data->size;
2390 memcpy(data, stored_data->content.data, stored_data->size);
2391 hr = DD_OK;
2393 done:
2394 wined3d_mutex_unlock();
2395 return hr;
2398 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2400 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2402 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2403 iface, debugstr_guid(tag), data, size);
2405 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2408 /*****************************************************************************
2409 * IDirectDrawSurface7::FreePrivateData
2411 * Frees private data stored in the surface
2413 * Params:
2414 * tag: Tag of the data to free
2416 * Returns:
2417 * D3D_OK on success
2418 * For more details, see IWineD3DSurface::FreePrivateData
2420 *****************************************************************************/
2421 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2423 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2424 struct wined3d_private_data *entry;
2426 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2428 wined3d_mutex_lock();
2429 entry = wined3d_private_store_get_private_data(&surface->private_store, tag);
2430 if (!entry)
2432 wined3d_mutex_unlock();
2433 return DDERR_NOTFOUND;
2436 wined3d_private_store_free_private_data(&surface->private_store, entry);
2437 wined3d_mutex_unlock();
2439 return DD_OK;
2442 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2444 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2446 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2448 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2451 /*****************************************************************************
2452 * IDirectDrawSurface7::PageLock
2454 * Prevents a sysmem surface from being paged out
2456 * Params:
2457 * Flags: Not used, must be 0(unchecked)
2459 * Returns:
2460 * DD_OK, because it's a stub
2462 *****************************************************************************/
2463 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2465 TRACE("iface %p, flags %#x.\n", iface, Flags);
2467 /* This is Windows memory management related - we don't need this */
2468 return DD_OK;
2471 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2473 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2475 TRACE("iface %p, flags %#x.\n", iface, flags);
2477 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2480 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2482 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2484 TRACE("iface %p, flags %#x.\n", iface, flags);
2486 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2489 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2491 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2493 TRACE("iface %p, flags %#x.\n", iface, flags);
2495 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2498 /*****************************************************************************
2499 * IDirectDrawSurface7::PageUnlock
2501 * Allows a sysmem surface to be paged out
2503 * Params:
2504 * Flags: Not used, must be 0(unchecked)
2506 * Returns:
2507 * DD_OK, because it's a stub
2509 *****************************************************************************/
2510 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2512 TRACE("iface %p, flags %#x.\n", iface, Flags);
2514 return DD_OK;
2517 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2519 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2521 TRACE("iface %p, flags %#x.\n", iface, flags);
2523 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2526 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2528 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2530 TRACE("iface %p, flags %#x.\n", iface, flags);
2532 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2535 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2537 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2539 TRACE("iface %p, flags %#x.\n", iface, flags);
2541 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2544 /*****************************************************************************
2545 * IDirectDrawSurface7::BltBatch
2547 * An unimplemented function
2549 * Params:
2552 * Returns:
2553 * DDERR_UNSUPPORTED
2555 *****************************************************************************/
2556 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2558 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2560 /* MSDN: "not currently implemented" */
2561 return DDERR_UNSUPPORTED;
2564 static HRESULT WINAPI ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2566 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2568 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2570 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2573 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2575 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2577 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2579 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2582 static HRESULT WINAPI ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2584 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2586 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2588 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2591 static HRESULT WINAPI ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2593 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2595 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2597 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2600 /*****************************************************************************
2601 * IDirectDrawSurface7::EnumAttachedSurfaces
2603 * Enumerates all surfaces attached to this surface
2605 * Params:
2606 * context: Pointer to pass unmodified to the callback
2607 * cb: Callback function to call for each surface
2609 * Returns:
2610 * DD_OK on success
2611 * DDERR_INVALIDPARAMS if cb is NULL
2613 *****************************************************************************/
2614 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2615 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2617 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2618 struct ddraw_surface *surf;
2619 DDSURFACEDESC2 desc;
2620 int i;
2622 /* Attached surfaces aren't handled in WineD3D */
2623 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2625 if(!cb)
2626 return DDERR_INVALIDPARAMS;
2628 wined3d_mutex_lock();
2630 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2632 surf = surface->complex_array[i];
2633 if(!surf) break;
2635 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2636 desc = surf->surface_desc;
2637 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2638 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2640 wined3d_mutex_unlock();
2641 return DD_OK;
2645 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2647 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2648 desc = surf->surface_desc;
2649 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2650 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2652 wined3d_mutex_unlock();
2653 return DD_OK;
2657 TRACE(" end of enumeration.\n");
2659 wined3d_mutex_unlock();
2661 return DD_OK;
2664 struct callback_info2
2666 LPDDENUMSURFACESCALLBACK2 callback;
2667 void *context;
2670 struct callback_info
2672 LPDDENUMSURFACESCALLBACK callback;
2673 void *context;
2676 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2678 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2679 const struct callback_info2 *info = context;
2681 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
2682 ddraw_surface7_Release(surface);
2684 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
2687 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2689 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2690 const struct callback_info *info = context;
2692 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
2693 ddraw_surface7_Release(surface);
2695 /* FIXME: Check surface_test.dwSize */
2696 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2697 (DDSURFACEDESC *)surface_desc, info->context);
2700 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2701 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2703 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2704 struct callback_info2 info;
2706 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2708 info.callback = callback;
2709 info.context = context;
2711 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2712 &info, EnumCallback2);
2715 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2716 void *context, LPDDENUMSURFACESCALLBACK callback)
2718 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2719 struct callback_info info;
2721 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2723 info.callback = callback;
2724 info.context = context;
2726 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2727 &info, EnumCallback);
2730 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2731 void *context, LPDDENUMSURFACESCALLBACK callback)
2733 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2734 struct callback_info info;
2736 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2738 info.callback = callback;
2739 info.context = context;
2741 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2742 &info, EnumCallback);
2745 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2746 void *context, LPDDENUMSURFACESCALLBACK callback)
2748 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2749 struct callback_info info;
2751 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2753 info.callback = callback;
2754 info.context = context;
2756 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2757 &info, EnumCallback);
2760 /*****************************************************************************
2761 * IDirectDrawSurface7::EnumOverlayZOrders
2763 * "Enumerates the overlay surfaces on the specified destination"
2765 * Params:
2766 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2767 * context: context to pass back to the callback
2768 * cb: callback function to call for each enumerated surface
2770 * Returns:
2771 * DD_OK, because it's a stub
2773 *****************************************************************************/
2774 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2775 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2777 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2779 return DD_OK;
2782 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2783 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2785 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2786 struct callback_info2 info;
2788 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2790 info.callback = callback;
2791 info.context = context;
2793 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2794 flags, &info, EnumCallback2);
2797 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2798 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2800 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2801 struct callback_info info;
2803 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2805 info.callback = callback;
2806 info.context = context;
2808 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2809 flags, &info, EnumCallback);
2812 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2813 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2815 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2816 struct callback_info info;
2818 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2820 info.callback = callback;
2821 info.context = context;
2823 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2824 flags, &info, EnumCallback);
2827 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2828 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2830 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2831 struct callback_info info;
2833 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2835 info.callback = callback;
2836 info.context = context;
2838 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2839 flags, &info, EnumCallback);
2842 /*****************************************************************************
2843 * IDirectDrawSurface7::GetBltStatus
2845 * Returns the blitting status
2847 * Params:
2848 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2850 * Returns:
2851 * See IWineD3DSurface::Blt
2853 *****************************************************************************/
2854 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2856 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2857 HRESULT hr;
2859 TRACE("iface %p, flags %#x.\n", iface, Flags);
2861 wined3d_mutex_lock();
2862 hr = wined3d_surface_get_blt_status(surface->wined3d_surface, Flags);
2863 wined3d_mutex_unlock();
2864 switch(hr)
2866 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2867 default: return hr;
2871 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2873 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2875 TRACE("iface %p, flags %#x.\n", iface, flags);
2877 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2880 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2882 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2884 TRACE("iface %p, flags %#x.\n", iface, flags);
2886 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2889 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2891 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2893 TRACE("iface %p, flags %#x.\n", iface, flags);
2895 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2898 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
2900 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2902 TRACE("iface %p, flags %#x.\n", iface, flags);
2904 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2907 /*****************************************************************************
2908 * IDirectDrawSurface7::GetColorKey
2910 * Returns the color key assigned to the surface
2912 * Params:
2913 * Flags: Some flags
2914 * CKey: Address to store the key to
2916 * Returns:
2917 * DD_OK on success
2918 * DDERR_INVALIDPARAMS if CKey is NULL
2920 *****************************************************************************/
2921 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2923 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
2925 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2927 if(!CKey)
2928 return DDERR_INVALIDPARAMS;
2930 wined3d_mutex_lock();
2932 switch (Flags)
2934 case DDCKEY_DESTBLT:
2935 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
2937 wined3d_mutex_unlock();
2938 return DDERR_NOCOLORKEY;
2940 *CKey = This->surface_desc.ddckCKDestBlt;
2941 break;
2943 case DDCKEY_DESTOVERLAY:
2944 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
2946 wined3d_mutex_unlock();
2947 return DDERR_NOCOLORKEY;
2949 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
2950 break;
2952 case DDCKEY_SRCBLT:
2953 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
2955 wined3d_mutex_unlock();
2956 return DDERR_NOCOLORKEY;
2958 *CKey = This->surface_desc.ddckCKSrcBlt;
2959 break;
2961 case DDCKEY_SRCOVERLAY:
2962 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
2964 wined3d_mutex_unlock();
2965 return DDERR_NOCOLORKEY;
2967 *CKey = This->surface_desc.ddckCKSrcOverlay;
2968 break;
2970 default:
2971 wined3d_mutex_unlock();
2972 return DDERR_INVALIDPARAMS;
2975 wined3d_mutex_unlock();
2977 return DD_OK;
2980 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
2982 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2984 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2986 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
2989 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2991 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2993 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2995 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
2998 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
3000 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3002 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3004 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3007 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
3009 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3011 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3013 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3016 /*****************************************************************************
3017 * IDirectDrawSurface7::GetFlipStatus
3019 * Returns the flipping status of the surface
3021 * Params:
3022 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
3024 * Returns:
3025 * See IWineD3DSurface::GetFlipStatus
3027 *****************************************************************************/
3028 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3030 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3031 HRESULT hr;
3033 TRACE("iface %p, flags %#x.\n", iface, Flags);
3035 wined3d_mutex_lock();
3036 hr = wined3d_surface_get_flip_status(surface->wined3d_surface, Flags);
3037 wined3d_mutex_unlock();
3039 switch(hr)
3041 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3042 default: return hr;
3046 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
3048 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3050 TRACE("iface %p, flags %#x.\n", iface, flags);
3052 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3055 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
3057 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3059 TRACE("iface %p, flags %#x.\n", iface, flags);
3061 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3064 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
3066 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3068 TRACE("iface %p, flags %#x.\n", iface, flags);
3070 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3073 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
3075 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3077 TRACE("iface %p, flags %#x.\n", iface, flags);
3079 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3082 /*****************************************************************************
3083 * IDirectDrawSurface7::GetOverlayPosition
3085 * Returns the display coordinates of a visible and active overlay surface
3087 * Params:
3091 * Returns:
3092 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3093 *****************************************************************************/
3094 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
3096 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3097 HRESULT hr;
3099 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
3101 wined3d_mutex_lock();
3102 hr = wined3d_surface_get_overlay_position(surface->wined3d_surface, X, Y);
3103 wined3d_mutex_unlock();
3105 return hr;
3108 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3110 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3112 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3114 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3117 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3119 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3121 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3123 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3126 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3128 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3130 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3132 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3135 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3137 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3139 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3141 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3144 /*****************************************************************************
3145 * IDirectDrawSurface7::GetPixelFormat
3147 * Returns the pixel format of the Surface
3149 * Params:
3150 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3151 * format should be written
3153 * Returns:
3154 * DD_OK on success
3155 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3157 *****************************************************************************/
3158 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3160 /* What is DDERR_INVALIDSURFACETYPE for here? */
3161 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3163 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3165 if(!PixelFormat)
3166 return DDERR_INVALIDPARAMS;
3168 wined3d_mutex_lock();
3169 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3170 wined3d_mutex_unlock();
3172 return DD_OK;
3175 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3177 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3179 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3181 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3184 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3186 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3188 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3190 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3193 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3195 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3197 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3199 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3202 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3204 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3206 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3208 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3211 /*****************************************************************************
3212 * IDirectDrawSurface7::GetSurfaceDesc
3214 * Returns the description of this surface
3216 * Params:
3217 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3218 * surface desc
3220 * Returns:
3221 * DD_OK on success
3222 * DDERR_INVALIDPARAMS if DDSD is NULL
3224 *****************************************************************************/
3225 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3227 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3229 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3231 if(!DDSD)
3232 return DDERR_INVALIDPARAMS;
3234 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3236 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3237 return DDERR_INVALIDPARAMS;
3240 wined3d_mutex_lock();
3241 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3242 TRACE("Returning surface desc:\n");
3243 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3244 wined3d_mutex_unlock();
3246 return DD_OK;
3249 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3251 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3253 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3255 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3258 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3260 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3262 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3264 if (!surface_desc) return DDERR_INVALIDPARAMS;
3266 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3268 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3269 return DDERR_INVALIDPARAMS;
3272 wined3d_mutex_lock();
3273 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3274 TRACE("Returning surface desc:\n");
3275 if (TRACE_ON(ddraw))
3277 /* DDRAW_dump_surface_desc handles the smaller size */
3278 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3280 wined3d_mutex_unlock();
3282 return DD_OK;
3285 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3287 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3289 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3291 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3294 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3296 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3298 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3300 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3303 /*****************************************************************************
3304 * IDirectDrawSurface7::Initialize
3306 * Initializes the surface. This is a no-op in Wine
3308 * Params:
3309 * DD: Pointer to an DirectDraw interface
3310 * DDSD: Surface description for initialization
3312 * Returns:
3313 * DDERR_ALREADYINITIALIZED
3315 *****************************************************************************/
3316 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3317 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3319 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3321 return DDERR_ALREADYINITIALIZED;
3324 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3325 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3327 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3329 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3331 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3332 ddraw, surface_desc);
3335 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3336 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3338 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3339 DDSURFACEDESC2 surface_desc2;
3341 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3343 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3344 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3345 ddraw, surface_desc ? &surface_desc2 : NULL);
3348 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3349 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3351 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3352 DDSURFACEDESC2 surface_desc2;
3354 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3356 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3357 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3358 ddraw, surface_desc ? &surface_desc2 : NULL);
3361 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3362 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3364 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3365 DDSURFACEDESC2 surface_desc2;
3367 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3369 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3370 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3371 ddraw, surface_desc ? &surface_desc2 : NULL);
3374 /*****************************************************************************
3375 * IDirect3DTexture1::Initialize
3377 * The sdk says it's not implemented
3379 * Params:
3382 * Returns
3383 * DDERR_UNSUPPORTED
3385 *****************************************************************************/
3386 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3387 IDirect3DDevice *device, IDirectDrawSurface *surface)
3389 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3391 return DDERR_UNSUPPORTED; /* Unchecked */
3394 /*****************************************************************************
3395 * IDirectDrawSurface7::IsLost
3397 * Checks if the surface is lost
3399 * Returns:
3400 * DD_OK, if the surface is usable
3401 * DDERR_ISLOST if the surface is lost
3402 * See IWineD3DSurface::IsLost for more details
3404 *****************************************************************************/
3405 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3407 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3408 HRESULT hr;
3410 TRACE("iface %p.\n", iface);
3412 wined3d_mutex_lock();
3413 hr = wined3d_surface_is_lost(surface->wined3d_surface);
3414 wined3d_mutex_unlock();
3416 switch(hr)
3418 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
3419 * WineD3D uses the same error for surfaces
3421 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
3422 default: return hr;
3426 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3428 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3430 TRACE("iface %p.\n", iface);
3432 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3435 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3437 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3439 TRACE("iface %p.\n", iface);
3441 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3444 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3446 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3448 TRACE("iface %p.\n", iface);
3450 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3453 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3455 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3457 TRACE("iface %p.\n", iface);
3459 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3462 /*****************************************************************************
3463 * IDirectDrawSurface7::Restore
3465 * Restores a lost surface. This makes the surface usable again, but
3466 * doesn't reload its old contents
3468 * Returns:
3469 * DD_OK on success
3470 * See IWineD3DSurface::Restore for more details
3472 *****************************************************************************/
3473 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3475 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3476 HRESULT hr;
3478 TRACE("iface %p.\n", iface);
3480 wined3d_mutex_lock();
3481 hr = wined3d_surface_restore(surface->wined3d_surface);
3482 wined3d_mutex_unlock();
3484 return hr;
3487 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3489 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3491 TRACE("iface %p.\n", iface);
3493 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3496 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3498 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3500 TRACE("iface %p.\n", iface);
3502 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3505 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3507 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3509 TRACE("iface %p.\n", iface);
3511 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3514 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3516 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3518 TRACE("iface %p.\n", iface);
3520 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3523 /*****************************************************************************
3524 * IDirectDrawSurface7::SetOverlayPosition
3526 * Changes the display coordinates of an overlay surface
3528 * Params:
3529 * X:
3530 * Y:
3532 * Returns:
3533 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3534 *****************************************************************************/
3535 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
3537 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3538 HRESULT hr;
3540 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
3542 wined3d_mutex_lock();
3543 hr = wined3d_surface_set_overlay_position(surface->wined3d_surface, X, Y);
3544 wined3d_mutex_unlock();
3546 return hr;
3549 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3551 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3553 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3555 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3558 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3560 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3562 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3564 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3567 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3569 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3571 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3573 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3576 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3578 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3580 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3582 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3585 /*****************************************************************************
3586 * IDirectDrawSurface7::UpdateOverlay
3588 * Modifies the attributes of an overlay surface.
3590 * Params:
3591 * SrcRect: The section of the source being used for the overlay
3592 * DstSurface: Address of the surface that is overlaid
3593 * DstRect: Place of the overlay
3594 * Flags: some DDOVER_* flags
3596 * Returns:
3597 * DDERR_UNSUPPORTED, because we don't support overlays
3599 *****************************************************************************/
3600 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
3601 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
3603 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3604 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(DstSurface);
3605 HRESULT hr;
3607 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3608 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
3610 wined3d_mutex_lock();
3611 hr = wined3d_surface_update_overlay(src_impl->wined3d_surface, SrcRect,
3612 dst_impl ? dst_impl->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
3613 wined3d_mutex_unlock();
3615 switch(hr) {
3616 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3617 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3618 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3619 default:
3620 return hr;
3624 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3625 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3627 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3628 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3630 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3631 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3633 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3634 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3637 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3638 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3640 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
3641 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3643 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3644 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3646 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3647 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3650 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3651 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3653 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
3654 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3656 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3657 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3659 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3660 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3663 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3664 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3666 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
3667 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3669 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3670 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3672 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3673 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3676 /*****************************************************************************
3677 * IDirectDrawSurface7::UpdateOverlayDisplay
3679 * The DX7 sdk says that it's not implemented
3681 * Params:
3682 * Flags: ?
3684 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3686 *****************************************************************************/
3687 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3689 TRACE("iface %p, flags %#x.\n", iface, Flags);
3691 return DDERR_UNSUPPORTED;
3694 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3696 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3698 TRACE("iface %p, flags %#x.\n", iface, flags);
3700 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3703 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3705 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3707 TRACE("iface %p, flags %#x.\n", iface, flags);
3709 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3712 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3714 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3716 TRACE("iface %p, flags %#x.\n", iface, flags);
3718 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3721 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3723 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3725 TRACE("iface %p, flags %#x.\n", iface, flags);
3727 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3730 /*****************************************************************************
3731 * IDirectDrawSurface7::UpdateOverlayZOrder
3733 * Sets an overlay's Z order
3735 * Params:
3736 * Flags: DDOVERZ_* flags
3737 * DDSRef: Defines the relative position in the overlay chain
3739 * Returns:
3740 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3742 *****************************************************************************/
3743 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3744 DWORD Flags, IDirectDrawSurface7 *DDSRef)
3746 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3747 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface7(DDSRef);
3748 HRESULT hr;
3750 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
3752 wined3d_mutex_lock();
3753 hr = wined3d_surface_update_overlay_z_order(surface->wined3d_surface,
3754 Flags, reference_impl ? reference_impl->wined3d_surface : NULL);
3755 wined3d_mutex_unlock();
3757 return hr;
3760 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3761 DWORD flags, IDirectDrawSurface4 *reference)
3763 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3764 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3766 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3768 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3769 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3772 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3773 DWORD flags, IDirectDrawSurface3 *reference)
3775 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3776 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3778 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3780 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3781 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3784 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3785 DWORD flags, IDirectDrawSurface2 *reference)
3787 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3788 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3790 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3792 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3793 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3796 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3797 DWORD flags, IDirectDrawSurface *reference)
3799 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3800 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3802 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3804 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3805 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3808 /*****************************************************************************
3809 * IDirectDrawSurface7::GetDDInterface
3811 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3812 * surface belongs to
3814 * Params:
3815 * DD: Address to write the interface pointer to
3817 * Returns:
3818 * DD_OK on success
3819 * DDERR_INVALIDPARAMS if DD is NULL
3821 *****************************************************************************/
3822 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3824 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3826 TRACE("iface %p, ddraw %p.\n", iface, DD);
3828 if(!DD)
3829 return DDERR_INVALIDPARAMS;
3831 switch(This->version)
3833 case 7:
3834 *DD = &This->ddraw->IDirectDraw7_iface;
3835 break;
3837 case 4:
3838 *DD = &This->ddraw->IDirectDraw4_iface;
3839 break;
3841 case 2:
3842 *DD = &This->ddraw->IDirectDraw2_iface;
3843 break;
3845 case 1:
3846 *DD = &This->ddraw->IDirectDraw_iface;
3847 break;
3850 IUnknown_AddRef((IUnknown *)*DD);
3852 return DD_OK;
3855 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3857 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3859 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3861 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3864 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3866 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3868 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3870 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3873 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3875 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3877 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3879 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3882 /* This seems also windows implementation specific - I don't think WineD3D needs this */
3883 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3885 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3886 volatile struct ddraw_surface* vThis = This;
3888 TRACE("iface %p.\n", iface);
3890 wined3d_mutex_lock();
3891 /* A uniqueness value of 0 is apparently special.
3892 * This needs to be checked.
3893 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
3895 while (1) {
3896 DWORD old_uniqueness_value = vThis->uniqueness_value;
3897 DWORD new_uniqueness_value = old_uniqueness_value+1;
3899 if (old_uniqueness_value == 0) break;
3900 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
3902 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
3903 old_uniqueness_value,
3904 new_uniqueness_value)
3905 == old_uniqueness_value)
3906 break;
3909 wined3d_mutex_unlock();
3911 return DD_OK;
3914 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
3916 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3918 TRACE("iface %p.\n", iface);
3920 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
3923 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
3925 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3927 TRACE("iface %p, value %p.\n", iface, pValue);
3929 wined3d_mutex_lock();
3930 *pValue = surface->uniqueness_value;
3931 wined3d_mutex_unlock();
3933 return DD_OK;
3936 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
3938 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3940 TRACE("iface %p, value %p.\n", iface, pValue);
3942 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
3945 /*****************************************************************************
3946 * IDirectDrawSurface7::SetLOD
3948 * Sets the level of detail of a texture
3950 * Params:
3951 * MaxLOD: LOD to set
3953 * Returns:
3954 * DD_OK on success
3955 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3957 *****************************************************************************/
3958 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
3960 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3961 HRESULT hr;
3963 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
3965 wined3d_mutex_lock();
3966 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3968 wined3d_mutex_unlock();
3969 return DDERR_INVALIDOBJECT;
3972 if (!surface->wined3d_texture)
3974 ERR("The ddraw surface has no wined3d texture.\n");
3975 wined3d_mutex_unlock();
3976 return DDERR_INVALIDOBJECT;
3979 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
3980 wined3d_mutex_unlock();
3982 return hr;
3985 /*****************************************************************************
3986 * IDirectDrawSurface7::GetLOD
3988 * Returns the level of detail of a Direct3D texture
3990 * Params:
3991 * MaxLOD: Address to write the LOD to
3993 * Returns:
3994 * DD_OK on success
3995 * DDERR_INVALIDPARAMS if MaxLOD is NULL
3996 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3998 *****************************************************************************/
3999 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4001 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4003 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4005 if(!MaxLOD)
4006 return DDERR_INVALIDPARAMS;
4008 wined3d_mutex_lock();
4009 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4011 wined3d_mutex_unlock();
4012 return DDERR_INVALIDOBJECT;
4015 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4016 wined3d_mutex_unlock();
4018 return DD_OK;
4021 /*****************************************************************************
4022 * IDirectDrawSurface7::BltFast
4024 * Performs a fast Blit.
4026 * Params:
4027 * dstx: The x coordinate to blit to on the destination
4028 * dsty: The y coordinate to blit to on the destination
4029 * Source: The source surface
4030 * rsrc: The source rectangle
4031 * trans: Type of transfer. Some DDBLTFAST_* flags
4033 * Returns:
4034 * DD_OK on success
4035 * For more details, see IWineD3DSurface::BltFast
4037 *****************************************************************************/
4038 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
4039 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
4041 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4042 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface7(Source);
4043 DWORD src_w, src_h, dst_w, dst_h;
4044 HRESULT hr = DD_OK;
4045 DWORD flags = 0;
4046 RECT dst_rect;
4048 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4049 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
4051 dst_w = This->surface_desc.dwWidth;
4052 dst_h = This->surface_desc.dwHeight;
4054 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
4055 * in that case
4057 if(rsrc)
4059 src_w = rsrc->right - rsrc->left;
4060 src_h = rsrc->bottom - rsrc->top;
4062 else
4064 src_w = src->surface_desc.dwWidth;
4065 src_h = src->surface_desc.dwHeight;
4068 if (src_w > dst_w || dstx > dst_w - src_w
4069 || src_h > dst_h || dsty > dst_h - src_h)
4071 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4072 return DDERR_INVALIDRECT;
4075 SetRect(&dst_rect, dstx, dsty, dstx + src_w, dsty + src_h);
4076 if (trans & DDBLTFAST_SRCCOLORKEY)
4077 flags |= WINEDDBLT_KEYSRC;
4078 if (trans & DDBLTFAST_DESTCOLORKEY)
4079 flags |= WINEDDBLT_KEYDEST;
4080 if (trans & DDBLTFAST_WAIT)
4081 flags |= WINEDDBLT_WAIT;
4082 if (trans & DDBLTFAST_DONOTWAIT)
4083 flags |= WINEDDBLT_DONOTWAIT;
4085 wined3d_mutex_lock();
4086 if (This->clipper)
4088 wined3d_mutex_unlock();
4089 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4090 return DDERR_BLTFASTCANTCLIP;
4093 if (src->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4094 hr = ddraw_surface_update_frontbuffer(src, rsrc, TRUE);
4095 if (SUCCEEDED(hr))
4096 hr = wined3d_surface_blt(This->wined3d_surface, &dst_rect,
4097 src->wined3d_surface, rsrc, flags, NULL, WINED3D_TEXF_POINT);
4098 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4099 hr = ddraw_surface_update_frontbuffer(This, &dst_rect, FALSE);
4100 wined3d_mutex_unlock();
4102 switch(hr)
4104 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4105 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
4106 default: return hr;
4110 static HRESULT WINAPI ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4111 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4113 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4114 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4116 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4117 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4119 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4120 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4123 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4124 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4126 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4127 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4129 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4130 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4132 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4133 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4136 static HRESULT WINAPI ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4137 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4139 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4140 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4142 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4143 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4145 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4146 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4149 static HRESULT WINAPI ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4150 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4152 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4153 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4155 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4156 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4158 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4159 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4162 /*****************************************************************************
4163 * IDirectDrawSurface7::GetClipper
4165 * Returns the IDirectDrawClipper interface of the clipper assigned to this
4166 * surface
4168 * Params:
4169 * Clipper: Address to store the interface pointer at
4171 * Returns:
4172 * DD_OK on success
4173 * DDERR_INVALIDPARAMS if Clipper is NULL
4174 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
4176 *****************************************************************************/
4177 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
4179 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4181 TRACE("iface %p, clipper %p.\n", iface, Clipper);
4183 if (!Clipper)
4184 return DDERR_INVALIDPARAMS;
4186 wined3d_mutex_lock();
4187 if (!surface->clipper)
4189 wined3d_mutex_unlock();
4190 return DDERR_NOCLIPPERATTACHED;
4193 *Clipper = (IDirectDrawClipper *)surface->clipper;
4194 IDirectDrawClipper_AddRef(*Clipper);
4195 wined3d_mutex_unlock();
4197 return DD_OK;
4200 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4202 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4204 TRACE("iface %p, clipper %p.\n", iface, clipper);
4206 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4209 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4211 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4213 TRACE("iface %p, clipper %p.\n", iface, clipper);
4215 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4218 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4220 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4222 TRACE("iface %p, clipper %p.\n", iface, clipper);
4224 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4227 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4229 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4231 TRACE("iface %p, clipper %p.\n", iface, clipper);
4233 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4236 /*****************************************************************************
4237 * IDirectDrawSurface7::SetClipper
4239 * Sets a clipper for the surface
4241 * Params:
4242 * Clipper: IDirectDrawClipper interface of the clipper to set
4244 * Returns:
4245 * DD_OK on success
4247 *****************************************************************************/
4248 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4249 IDirectDrawClipper *iclipper)
4251 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4252 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4253 struct ddraw_clipper *old_clipper = This->clipper;
4254 HWND clipWindow;
4256 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4258 wined3d_mutex_lock();
4259 if (clipper == This->clipper)
4261 wined3d_mutex_unlock();
4262 return DD_OK;
4265 This->clipper = clipper;
4267 if (clipper != NULL)
4268 IDirectDrawClipper_AddRef(iclipper);
4269 if (old_clipper)
4270 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4272 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4274 clipWindow = NULL;
4275 if(clipper) {
4276 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4279 if (clipWindow)
4281 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4282 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4284 else
4286 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4287 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4291 wined3d_mutex_unlock();
4293 return DD_OK;
4296 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4298 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4300 TRACE("iface %p, clipper %p.\n", iface, clipper);
4302 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4305 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4307 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4309 TRACE("iface %p, clipper %p.\n", iface, clipper);
4311 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4314 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4316 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4318 TRACE("iface %p, clipper %p.\n", iface, clipper);
4320 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4323 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4325 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4327 TRACE("iface %p, clipper %p.\n", iface, clipper);
4329 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4332 /*****************************************************************************
4333 * IDirectDrawSurface7::SetSurfaceDesc
4335 * Sets the surface description. It can override the pixel format, the surface
4336 * memory, ...
4337 * It's not really tested.
4339 * Params:
4340 * DDSD: Pointer to the new surface description to set
4341 * Flags: Some flags
4343 * Returns:
4344 * DD_OK on success
4345 * DDERR_INVALIDPARAMS if DDSD is NULL
4347 *****************************************************************************/
4348 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4350 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4351 HRESULT hr;
4352 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4353 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4354 enum wined3d_format_id format_id;
4355 UINT pitch, width, height;
4357 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4359 if (!DDSD)
4361 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4362 return DDERR_INVALIDPARAMS;
4364 if (Flags)
4366 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4367 return DDERR_INVALIDPARAMS;
4369 if (!(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4370 || This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4371 || This->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4373 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4374 return DDERR_INVALIDSURFACETYPE;
4377 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4378 * for PIXELFORMAT to work */
4379 if (DDSD->dwFlags & ~allowed_flags)
4381 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4382 return DDERR_INVALIDPARAMS;
4384 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4386 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4387 return DDERR_INVALIDPARAMS;
4389 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4391 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4392 return DDERR_INVALIDCAPS;
4394 if (DDSD->dwFlags & DDSD_WIDTH)
4396 if (!(DDSD->dwFlags & DDSD_PITCH))
4398 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4399 return DDERR_INVALIDPARAMS;
4401 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4403 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4404 DDSD->u1.lPitch, DDSD->dwWidth);
4405 return DDERR_INVALIDPARAMS;
4407 if (DDSD->dwWidth != This->surface_desc.dwWidth)
4408 TRACE("Surface width changed from %u to %u.\n", This->surface_desc.dwWidth, DDSD->dwWidth);
4409 if (DDSD->u1.lPitch != This->surface_desc.u1.lPitch)
4410 TRACE("Surface pitch changed from %u to %u.\n", This->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4411 pitch = DDSD->u1.lPitch;
4412 width = DDSD->dwWidth;
4414 else if (DDSD->dwFlags & DDSD_PITCH)
4416 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4417 return DDERR_INVALIDPARAMS;
4419 else
4421 pitch = This->surface_desc.u1.lPitch;
4422 width = This->surface_desc.dwWidth;
4425 if (DDSD->dwFlags & DDSD_HEIGHT)
4427 if (!DDSD->dwHeight)
4429 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4430 return DDERR_INVALIDPARAMS;
4432 if (DDSD->dwHeight != This->surface_desc.dwHeight)
4433 TRACE("Surface height changed from %u to %u.\n", This->surface_desc.dwHeight, DDSD->dwHeight);
4434 height = DDSD->dwHeight;
4436 else
4438 height = This->surface_desc.dwHeight;
4441 wined3d_mutex_lock();
4442 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4444 enum wined3d_format_id current_format_id;
4445 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4447 if (format_id == WINED3DFMT_UNKNOWN)
4449 ERR("Requested to set an unknown pixelformat\n");
4450 wined3d_mutex_unlock();
4451 return DDERR_INVALIDPARAMS;
4453 current_format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4454 if (format_id != current_format_id)
4455 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4457 else
4459 format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4462 if (FAILED(hr = wined3d_surface_update_desc(This->wined3d_surface, width, height,
4463 format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4465 WARN("Failed to update surface desc, hr %#x.\n", hr);
4466 wined3d_mutex_unlock();
4467 return hr_ddraw_from_wined3d(hr);
4470 if (DDSD->dwFlags & DDSD_WIDTH)
4471 This->surface_desc.dwWidth = width;
4472 if (DDSD->dwFlags & DDSD_PITCH)
4473 This->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4474 if (DDSD->dwFlags & DDSD_HEIGHT)
4475 This->surface_desc.dwHeight = height;
4476 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4477 This->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4479 wined3d_mutex_unlock();
4481 return DD_OK;
4484 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4485 DDSURFACEDESC2 *surface_desc, DWORD flags)
4487 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4489 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4491 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4492 surface_desc, flags);
4495 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4496 DDSURFACEDESC *surface_desc, DWORD flags)
4498 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4499 DDSURFACEDESC2 surface_desc2;
4501 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4503 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4504 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4505 surface_desc ? &surface_desc2 : NULL, flags);
4508 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4510 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4511 struct ddraw_palette *palette_impl;
4512 HRESULT hr = DD_OK;
4514 TRACE("iface %p, palette %p.\n", iface, palette);
4516 if (!palette)
4517 return DDERR_INVALIDPARAMS;
4518 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4520 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4521 return DDERR_SURFACELOST;
4524 wined3d_mutex_lock();
4525 if ((palette_impl = surface->palette))
4527 *palette = &palette_impl->IDirectDrawPalette_iface;
4528 IDirectDrawPalette_AddRef(*palette);
4530 else
4532 *palette = NULL;
4533 hr = DDERR_NOPALETTEATTACHED;
4535 wined3d_mutex_unlock();
4537 return hr;
4540 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4542 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4544 TRACE("iface %p, palette %p.\n", iface, palette);
4546 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4549 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4551 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4553 TRACE("iface %p, palette %p.\n", iface, palette);
4555 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4558 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4560 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4562 TRACE("iface %p, palette %p.\n", iface, palette);
4564 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4567 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4569 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4571 TRACE("iface %p, palette %p.\n", iface, palette);
4573 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4576 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4578 DDCOLORKEY fixed_color_key;
4579 HRESULT hr = WINED3D_OK;
4581 wined3d_mutex_lock();
4583 if (color_key)
4585 fixed_color_key = *color_key;
4586 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
4587 if (fixed_color_key.dwColorSpaceHighValue < fixed_color_key.dwColorSpaceLowValue)
4588 fixed_color_key.dwColorSpaceHighValue = fixed_color_key.dwColorSpaceLowValue;
4590 switch (flags & ~DDCKEY_COLORSPACE)
4592 case DDCKEY_DESTBLT:
4593 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4594 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4595 break;
4597 case DDCKEY_DESTOVERLAY:
4598 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4599 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4600 break;
4602 case DDCKEY_SRCOVERLAY:
4603 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4604 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4605 break;
4607 case DDCKEY_SRCBLT:
4608 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4609 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4610 break;
4612 default:
4613 wined3d_mutex_unlock();
4614 return DDERR_INVALIDPARAMS;
4617 else
4619 switch (flags & ~DDCKEY_COLORSPACE)
4621 case DDCKEY_DESTBLT:
4622 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4623 break;
4625 case DDCKEY_DESTOVERLAY:
4626 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4627 break;
4629 case DDCKEY_SRCOVERLAY:
4630 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4631 break;
4633 case DDCKEY_SRCBLT:
4634 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4635 break;
4637 default:
4638 wined3d_mutex_unlock();
4639 return DDERR_INVALIDPARAMS;
4643 if (surface->wined3d_texture)
4644 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags,
4645 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4647 wined3d_mutex_unlock();
4649 return hr_ddraw_from_wined3d(hr);
4652 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4654 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4656 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4658 wined3d_mutex_lock();
4659 if (!surface->wined3d_texture)
4661 wined3d_mutex_unlock();
4662 return DDERR_NOTONMIPMAPSUBLEVEL;
4664 wined3d_mutex_unlock();
4666 return ddraw_surface_set_color_key(surface, flags, color_key);
4669 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4671 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4673 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4675 return ddraw_surface_set_color_key(surface, flags, color_key);
4678 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4680 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4682 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4684 return ddraw_surface_set_color_key(surface, flags, color_key);
4687 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4689 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4691 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4693 return ddraw_surface_set_color_key(surface, flags, color_key);
4696 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4698 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4700 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4702 return ddraw_surface_set_color_key(surface, flags, color_key);
4705 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
4707 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4709 TRACE("iface %p, palette %p.\n", iface, palette);
4711 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4712 return DDERR_NOTONMIPMAPSUBLEVEL;
4713 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4715 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4716 return DDERR_SURFACELOST;
4719 return ddraw_surface_set_palette(surface, palette);
4722 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4724 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4726 TRACE("iface %p, palette %p.\n", iface, palette);
4728 if (IDirectDrawSurface4_IsLost(iface) == DDERR_SURFACELOST)
4730 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4731 return DDERR_SURFACELOST;
4734 return ddraw_surface_set_palette(surface, palette);
4737 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4739 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4741 TRACE("iface %p, palette %p.\n", iface, palette);
4743 if (IDirectDrawSurface3_IsLost(iface) == DDERR_SURFACELOST)
4745 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4746 return DDERR_SURFACELOST;
4749 return ddraw_surface_set_palette(surface, palette);
4752 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4754 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4756 TRACE("iface %p, palette %p.\n", iface, palette);
4758 if (IDirectDrawSurface2_IsLost(iface) == DDERR_SURFACELOST)
4760 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4761 return DDERR_SURFACELOST;
4764 return ddraw_surface_set_palette(surface, palette);
4767 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4769 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4771 TRACE("iface %p, palette %p.\n", iface, palette);
4773 if (IDirectDrawSurface_IsLost(iface) == DDERR_SURFACELOST)
4775 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4776 return DDERR_SURFACELOST;
4779 return ddraw_surface_set_palette(surface, palette);
4782 /**********************************************************
4783 * IDirectDrawGammaControl::GetGammaRamp
4785 * Returns the current gamma ramp for a surface
4787 * Params:
4788 * flags: Ignored
4789 * gamma_ramp: Address to write the ramp to
4791 * Returns:
4792 * DD_OK on success
4793 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4795 **********************************************************/
4796 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4797 DWORD flags, DDGAMMARAMP *gamma_ramp)
4799 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4801 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4803 if (!gamma_ramp)
4805 WARN("Invalid gamma_ramp passed.\n");
4806 return DDERR_INVALIDPARAMS;
4809 wined3d_mutex_lock();
4810 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4812 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4813 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
4815 else
4817 ERR("Not implemented for non-primary surfaces.\n");
4819 wined3d_mutex_unlock();
4821 return DD_OK;
4824 /**********************************************************
4825 * IDirectDrawGammaControl::SetGammaRamp
4827 * Sets the red, green and blue gamma ramps for
4829 * Params:
4830 * flags: Can be DDSGR_CALIBRATE to request calibration
4831 * gamma_ramp: Structure containing the new gamma ramp
4833 * Returns:
4834 * DD_OK on success
4835 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4837 **********************************************************/
4838 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4839 DWORD flags, DDGAMMARAMP *gamma_ramp)
4841 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4843 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4845 if (!gamma_ramp)
4847 WARN("Invalid gamma_ramp passed.\n");
4848 return DDERR_INVALIDPARAMS;
4851 wined3d_mutex_lock();
4852 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4854 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4855 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
4856 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
4858 else
4860 ERR("Not implemented for non-primary surfaces.\n");
4862 wined3d_mutex_unlock();
4864 return DD_OK;
4867 /*****************************************************************************
4868 * IDirect3DTexture2::PaletteChanged
4870 * Informs the texture about a palette change
4872 * Params:
4873 * start: Start index of the change
4874 * count: The number of changed entries
4876 * Returns
4877 * D3D_OK, because it's a stub
4879 *****************************************************************************/
4880 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4882 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4884 return D3D_OK;
4887 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4889 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
4891 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4893 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
4896 /*****************************************************************************
4897 * IDirect3DTexture::Unload
4899 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4902 * Returns:
4903 * DDERR_UNSUPPORTED
4905 *****************************************************************************/
4906 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4908 WARN("iface %p. Not implemented.\n", iface);
4910 return DDERR_UNSUPPORTED;
4913 /*****************************************************************************
4914 * IDirect3DTexture2::GetHandle
4916 * Returns handle for the texture. At the moment, the interface
4917 * to the IWineD3DTexture is used.
4919 * Params:
4920 * device: Device this handle is assigned to
4921 * handle: Address to store the handle at.
4923 * Returns:
4924 * D3D_OK
4926 *****************************************************************************/
4927 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4928 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4930 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
4931 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
4933 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4935 wined3d_mutex_lock();
4937 if (!surface->Handle)
4939 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
4940 if (h == DDRAW_INVALID_HANDLE)
4942 ERR("Failed to allocate a texture handle.\n");
4943 wined3d_mutex_unlock();
4944 return DDERR_OUTOFMEMORY;
4947 surface->Handle = h + 1;
4950 TRACE("Returning handle %08x.\n", surface->Handle);
4951 *handle = surface->Handle;
4953 wined3d_mutex_unlock();
4955 return D3D_OK;
4958 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
4959 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
4961 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
4962 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
4964 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4966 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
4967 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
4970 /*****************************************************************************
4971 * get_sub_mimaplevel
4973 * Helper function that returns the next mipmap level
4975 * tex_ptr: Surface of which to return the next level
4977 *****************************************************************************/
4978 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
4980 /* Now go down the mipmap chain to the next surface */
4981 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
4982 IDirectDrawSurface7 *next_level;
4983 HRESULT hr;
4985 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
4986 if (FAILED(hr)) return NULL;
4988 ddraw_surface7_Release(next_level);
4990 return impl_from_IDirectDrawSurface7(next_level);
4993 /*****************************************************************************
4994 * IDirect3DTexture2::Load
4996 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
4998 * This function isn't relayed to WineD3D because the whole interface is
4999 * implemented in DDraw only. For speed improvements an implementation which
5000 * takes OpenGL more into account could be placed into WineD3D.
5002 * Params:
5003 * src_texture: Address of the texture to load
5005 * Returns:
5006 * D3D_OK on success
5007 * D3DERR_TEXTURE_LOAD_FAILED.
5009 *****************************************************************************/
5010 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5012 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5013 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5014 HRESULT hr;
5016 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5018 if (src_surface == dst_surface)
5020 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5021 return D3D_OK;
5024 wined3d_mutex_lock();
5026 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5027 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5028 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5030 ERR("Trying to load surfaces with different mip-map counts.\n");
5033 for (;;)
5035 struct ddraw_palette *dst_pal, *src_pal;
5036 DDSURFACEDESC *src_desc, *dst_desc;
5038 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
5039 src_surface, dst_surface, src_surface->mipmap_level);
5041 /* Suppress the ALLOCONLOAD flag */
5042 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5044 /* Get the palettes */
5045 dst_pal = dst_surface->palette;
5046 src_pal = src_surface->palette;
5048 if (src_pal)
5050 PALETTEENTRY palent[256];
5052 if (!dst_pal)
5054 wined3d_mutex_unlock();
5055 return DDERR_NOPALETTEATTACHED;
5057 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5058 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5061 /* Copy one surface on the other */
5062 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5063 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5065 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5067 /* Should also check for same pixel format, u1.lPitch, ... */
5068 ERR("Error in surface sizes.\n");
5069 wined3d_mutex_unlock();
5070 return D3DERR_TEXTURE_LOAD_FAILED;
5072 else
5074 struct wined3d_map_desc src_map_desc, dst_map_desc;
5076 /* Copy the src blit color key if the source has one, don't erase
5077 * the destination's ckey if the source has none */
5078 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5080 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5081 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5084 /* Copy the main memory texture into the surface that corresponds
5085 * to the OpenGL texture object. */
5087 hr = wined3d_surface_map(src_surface->wined3d_surface, &src_map_desc, NULL, 0);
5088 if (FAILED(hr))
5090 ERR("Failed to lock source surface, hr %#x.\n", hr);
5091 wined3d_mutex_unlock();
5092 return D3DERR_TEXTURE_LOAD_FAILED;
5095 hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_map_desc, NULL, 0);
5096 if (FAILED(hr))
5098 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5099 wined3d_surface_unmap(src_surface->wined3d_surface);
5100 wined3d_mutex_unlock();
5101 return D3DERR_TEXTURE_LOAD_FAILED;
5104 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5105 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5106 else
5107 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5109 wined3d_surface_unmap(src_surface->wined3d_surface);
5110 wined3d_surface_unmap(dst_surface->wined3d_surface);
5113 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5114 src_surface = get_sub_mimaplevel(src_surface);
5115 else
5116 src_surface = NULL;
5118 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5119 dst_surface = get_sub_mimaplevel(dst_surface);
5120 else
5121 dst_surface = NULL;
5123 if (!src_surface || !dst_surface)
5125 if (src_surface != dst_surface)
5126 ERR("Loading surface with different mipmap structure.\n");
5127 break;
5131 wined3d_mutex_unlock();
5133 return hr;
5136 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5138 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5139 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5141 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5143 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5144 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5147 /*****************************************************************************
5148 * The VTable
5149 *****************************************************************************/
5151 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5153 /* IUnknown */
5154 ddraw_surface7_QueryInterface,
5155 ddraw_surface7_AddRef,
5156 ddraw_surface7_Release,
5157 /* IDirectDrawSurface */
5158 ddraw_surface7_AddAttachedSurface,
5159 ddraw_surface7_AddOverlayDirtyRect,
5160 ddraw_surface7_Blt,
5161 ddraw_surface7_BltBatch,
5162 ddraw_surface7_BltFast,
5163 ddraw_surface7_DeleteAttachedSurface,
5164 ddraw_surface7_EnumAttachedSurfaces,
5165 ddraw_surface7_EnumOverlayZOrders,
5166 ddraw_surface7_Flip,
5167 ddraw_surface7_GetAttachedSurface,
5168 ddraw_surface7_GetBltStatus,
5169 ddraw_surface7_GetCaps,
5170 ddraw_surface7_GetClipper,
5171 ddraw_surface7_GetColorKey,
5172 ddraw_surface7_GetDC,
5173 ddraw_surface7_GetFlipStatus,
5174 ddraw_surface7_GetOverlayPosition,
5175 ddraw_surface7_GetPalette,
5176 ddraw_surface7_GetPixelFormat,
5177 ddraw_surface7_GetSurfaceDesc,
5178 ddraw_surface7_Initialize,
5179 ddraw_surface7_IsLost,
5180 ddraw_surface7_Lock,
5181 ddraw_surface7_ReleaseDC,
5182 ddraw_surface7_Restore,
5183 ddraw_surface7_SetClipper,
5184 ddraw_surface7_SetColorKey,
5185 ddraw_surface7_SetOverlayPosition,
5186 ddraw_surface7_SetPalette,
5187 ddraw_surface7_Unlock,
5188 ddraw_surface7_UpdateOverlay,
5189 ddraw_surface7_UpdateOverlayDisplay,
5190 ddraw_surface7_UpdateOverlayZOrder,
5191 /* IDirectDrawSurface2 */
5192 ddraw_surface7_GetDDInterface,
5193 ddraw_surface7_PageLock,
5194 ddraw_surface7_PageUnlock,
5195 /* IDirectDrawSurface3 */
5196 ddraw_surface7_SetSurfaceDesc,
5197 /* IDirectDrawSurface4 */
5198 ddraw_surface7_SetPrivateData,
5199 ddraw_surface7_GetPrivateData,
5200 ddraw_surface7_FreePrivateData,
5201 ddraw_surface7_GetUniquenessValue,
5202 ddraw_surface7_ChangeUniquenessValue,
5203 /* IDirectDrawSurface7 */
5204 ddraw_surface7_SetPriority,
5205 ddraw_surface7_GetPriority,
5206 ddraw_surface7_SetLOD,
5207 ddraw_surface7_GetLOD,
5210 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5212 /* IUnknown */
5213 ddraw_surface4_QueryInterface,
5214 ddraw_surface4_AddRef,
5215 ddraw_surface4_Release,
5216 /* IDirectDrawSurface */
5217 ddraw_surface4_AddAttachedSurface,
5218 ddraw_surface4_AddOverlayDirtyRect,
5219 ddraw_surface4_Blt,
5220 ddraw_surface4_BltBatch,
5221 ddraw_surface4_BltFast,
5222 ddraw_surface4_DeleteAttachedSurface,
5223 ddraw_surface4_EnumAttachedSurfaces,
5224 ddraw_surface4_EnumOverlayZOrders,
5225 ddraw_surface4_Flip,
5226 ddraw_surface4_GetAttachedSurface,
5227 ddraw_surface4_GetBltStatus,
5228 ddraw_surface4_GetCaps,
5229 ddraw_surface4_GetClipper,
5230 ddraw_surface4_GetColorKey,
5231 ddraw_surface4_GetDC,
5232 ddraw_surface4_GetFlipStatus,
5233 ddraw_surface4_GetOverlayPosition,
5234 ddraw_surface4_GetPalette,
5235 ddraw_surface4_GetPixelFormat,
5236 ddraw_surface4_GetSurfaceDesc,
5237 ddraw_surface4_Initialize,
5238 ddraw_surface4_IsLost,
5239 ddraw_surface4_Lock,
5240 ddraw_surface4_ReleaseDC,
5241 ddraw_surface4_Restore,
5242 ddraw_surface4_SetClipper,
5243 ddraw_surface4_SetColorKey,
5244 ddraw_surface4_SetOverlayPosition,
5245 ddraw_surface4_SetPalette,
5246 ddraw_surface4_Unlock,
5247 ddraw_surface4_UpdateOverlay,
5248 ddraw_surface4_UpdateOverlayDisplay,
5249 ddraw_surface4_UpdateOverlayZOrder,
5250 /* IDirectDrawSurface2 */
5251 ddraw_surface4_GetDDInterface,
5252 ddraw_surface4_PageLock,
5253 ddraw_surface4_PageUnlock,
5254 /* IDirectDrawSurface3 */
5255 ddraw_surface4_SetSurfaceDesc,
5256 /* IDirectDrawSurface4 */
5257 ddraw_surface4_SetPrivateData,
5258 ddraw_surface4_GetPrivateData,
5259 ddraw_surface4_FreePrivateData,
5260 ddraw_surface4_GetUniquenessValue,
5261 ddraw_surface4_ChangeUniquenessValue,
5264 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5266 /* IUnknown */
5267 ddraw_surface3_QueryInterface,
5268 ddraw_surface3_AddRef,
5269 ddraw_surface3_Release,
5270 /* IDirectDrawSurface */
5271 ddraw_surface3_AddAttachedSurface,
5272 ddraw_surface3_AddOverlayDirtyRect,
5273 ddraw_surface3_Blt,
5274 ddraw_surface3_BltBatch,
5275 ddraw_surface3_BltFast,
5276 ddraw_surface3_DeleteAttachedSurface,
5277 ddraw_surface3_EnumAttachedSurfaces,
5278 ddraw_surface3_EnumOverlayZOrders,
5279 ddraw_surface3_Flip,
5280 ddraw_surface3_GetAttachedSurface,
5281 ddraw_surface3_GetBltStatus,
5282 ddraw_surface3_GetCaps,
5283 ddraw_surface3_GetClipper,
5284 ddraw_surface3_GetColorKey,
5285 ddraw_surface3_GetDC,
5286 ddraw_surface3_GetFlipStatus,
5287 ddraw_surface3_GetOverlayPosition,
5288 ddraw_surface3_GetPalette,
5289 ddraw_surface3_GetPixelFormat,
5290 ddraw_surface3_GetSurfaceDesc,
5291 ddraw_surface3_Initialize,
5292 ddraw_surface3_IsLost,
5293 ddraw_surface3_Lock,
5294 ddraw_surface3_ReleaseDC,
5295 ddraw_surface3_Restore,
5296 ddraw_surface3_SetClipper,
5297 ddraw_surface3_SetColorKey,
5298 ddraw_surface3_SetOverlayPosition,
5299 ddraw_surface3_SetPalette,
5300 ddraw_surface3_Unlock,
5301 ddraw_surface3_UpdateOverlay,
5302 ddraw_surface3_UpdateOverlayDisplay,
5303 ddraw_surface3_UpdateOverlayZOrder,
5304 /* IDirectDrawSurface2 */
5305 ddraw_surface3_GetDDInterface,
5306 ddraw_surface3_PageLock,
5307 ddraw_surface3_PageUnlock,
5308 /* IDirectDrawSurface3 */
5309 ddraw_surface3_SetSurfaceDesc,
5312 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5314 /* IUnknown */
5315 ddraw_surface2_QueryInterface,
5316 ddraw_surface2_AddRef,
5317 ddraw_surface2_Release,
5318 /* IDirectDrawSurface */
5319 ddraw_surface2_AddAttachedSurface,
5320 ddraw_surface2_AddOverlayDirtyRect,
5321 ddraw_surface2_Blt,
5322 ddraw_surface2_BltBatch,
5323 ddraw_surface2_BltFast,
5324 ddraw_surface2_DeleteAttachedSurface,
5325 ddraw_surface2_EnumAttachedSurfaces,
5326 ddraw_surface2_EnumOverlayZOrders,
5327 ddraw_surface2_Flip,
5328 ddraw_surface2_GetAttachedSurface,
5329 ddraw_surface2_GetBltStatus,
5330 ddraw_surface2_GetCaps,
5331 ddraw_surface2_GetClipper,
5332 ddraw_surface2_GetColorKey,
5333 ddraw_surface2_GetDC,
5334 ddraw_surface2_GetFlipStatus,
5335 ddraw_surface2_GetOverlayPosition,
5336 ddraw_surface2_GetPalette,
5337 ddraw_surface2_GetPixelFormat,
5338 ddraw_surface2_GetSurfaceDesc,
5339 ddraw_surface2_Initialize,
5340 ddraw_surface2_IsLost,
5341 ddraw_surface2_Lock,
5342 ddraw_surface2_ReleaseDC,
5343 ddraw_surface2_Restore,
5344 ddraw_surface2_SetClipper,
5345 ddraw_surface2_SetColorKey,
5346 ddraw_surface2_SetOverlayPosition,
5347 ddraw_surface2_SetPalette,
5348 ddraw_surface2_Unlock,
5349 ddraw_surface2_UpdateOverlay,
5350 ddraw_surface2_UpdateOverlayDisplay,
5351 ddraw_surface2_UpdateOverlayZOrder,
5352 /* IDirectDrawSurface2 */
5353 ddraw_surface2_GetDDInterface,
5354 ddraw_surface2_PageLock,
5355 ddraw_surface2_PageUnlock,
5358 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5360 /* IUnknown */
5361 ddraw_surface1_QueryInterface,
5362 ddraw_surface1_AddRef,
5363 ddraw_surface1_Release,
5364 /* IDirectDrawSurface */
5365 ddraw_surface1_AddAttachedSurface,
5366 ddraw_surface1_AddOverlayDirtyRect,
5367 ddraw_surface1_Blt,
5368 ddraw_surface1_BltBatch,
5369 ddraw_surface1_BltFast,
5370 ddraw_surface1_DeleteAttachedSurface,
5371 ddraw_surface1_EnumAttachedSurfaces,
5372 ddraw_surface1_EnumOverlayZOrders,
5373 ddraw_surface1_Flip,
5374 ddraw_surface1_GetAttachedSurface,
5375 ddraw_surface1_GetBltStatus,
5376 ddraw_surface1_GetCaps,
5377 ddraw_surface1_GetClipper,
5378 ddraw_surface1_GetColorKey,
5379 ddraw_surface1_GetDC,
5380 ddraw_surface1_GetFlipStatus,
5381 ddraw_surface1_GetOverlayPosition,
5382 ddraw_surface1_GetPalette,
5383 ddraw_surface1_GetPixelFormat,
5384 ddraw_surface1_GetSurfaceDesc,
5385 ddraw_surface1_Initialize,
5386 ddraw_surface1_IsLost,
5387 ddraw_surface1_Lock,
5388 ddraw_surface1_ReleaseDC,
5389 ddraw_surface1_Restore,
5390 ddraw_surface1_SetClipper,
5391 ddraw_surface1_SetColorKey,
5392 ddraw_surface1_SetOverlayPosition,
5393 ddraw_surface1_SetPalette,
5394 ddraw_surface1_Unlock,
5395 ddraw_surface1_UpdateOverlay,
5396 ddraw_surface1_UpdateOverlayDisplay,
5397 ddraw_surface1_UpdateOverlayZOrder,
5400 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5402 ddraw_gamma_control_QueryInterface,
5403 ddraw_gamma_control_AddRef,
5404 ddraw_gamma_control_Release,
5405 ddraw_gamma_control_GetGammaRamp,
5406 ddraw_gamma_control_SetGammaRamp,
5409 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5411 d3d_texture2_QueryInterface,
5412 d3d_texture2_AddRef,
5413 d3d_texture2_Release,
5414 d3d_texture2_GetHandle,
5415 d3d_texture2_PaletteChanged,
5416 d3d_texture2_Load,
5419 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5421 d3d_texture1_QueryInterface,
5422 d3d_texture1_AddRef,
5423 d3d_texture1_Release,
5424 d3d_texture1_Initialize,
5425 d3d_texture1_GetHandle,
5426 d3d_texture1_PaletteChanged,
5427 d3d_texture1_Load,
5428 d3d_texture1_Unload,
5431 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5432 * IDirectDrawSurface interface to ddraw methods. */
5433 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5435 if (!iface) return NULL;
5436 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5438 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5439 if (FAILED(hr))
5441 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5442 return NULL;
5444 IDirectDrawSurface7_Release(iface);
5446 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5449 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5451 if (!iface) return NULL;
5452 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5454 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5455 if (FAILED(hr))
5457 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5458 return NULL;
5460 IDirectDrawSurface4_Release(iface);
5462 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5465 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5467 if (!iface) return NULL;
5468 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5470 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5471 if (FAILED(hr))
5473 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5474 return NULL;
5476 IDirectDrawSurface3_Release(iface);
5478 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5481 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5483 if (!iface) return NULL;
5484 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5486 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5487 if (FAILED(hr))
5489 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5490 return NULL;
5492 IDirectDrawSurface2_Release(iface);
5494 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5497 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5499 if (!iface) return NULL;
5500 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5502 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5503 if (FAILED(hr))
5505 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5506 return NULL;
5508 IDirectDrawSurface_Release(iface);
5510 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5513 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5515 if (!iface) return NULL;
5516 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5517 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5520 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5522 if (!iface) return NULL;
5523 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5524 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5527 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5529 struct ddraw_surface *surface = parent;
5531 TRACE("surface %p.\n", surface);
5533 /* Check for attached surfaces and detach them. */
5534 if (surface->first_attached != surface)
5536 /* Well, this shouldn't happen: The surface being attached is
5537 * referenced in AddAttachedSurface(), so it shouldn't be released
5538 * until DeleteAttachedSurface() is called, because the refcount is
5539 * held. It looks like the application released it often enough to
5540 * force this. */
5541 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
5543 /* The refcount will drop to -1 here */
5544 if (FAILED(ddraw_surface_delete_attached_surface(surface->first_attached, surface, surface->attached_iface)))
5545 ERR("DeleteAttachedSurface failed.\n");
5548 while (surface->next_attached)
5549 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5550 surface->next_attached, surface->next_attached->attached_iface)))
5551 ERR("DeleteAttachedSurface failed.\n");
5553 /* Having a texture handle set implies that the device still exists. */
5554 if (surface->Handle)
5555 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5557 /* Reduce the ddraw surface count. */
5558 list_remove(&surface->surface_list_entry);
5560 if (surface == surface->ddraw->primary)
5561 surface->ddraw->primary = NULL;
5563 wined3d_private_store_cleanup(&surface->private_store);
5565 HeapFree(GetProcessHeap(), 0, surface);
5568 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5570 ddraw_surface_wined3d_object_destroyed,
5573 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5575 TRACE("parent %p.\n", parent);
5577 HeapFree(GetProcessHeap(), 0, parent);
5580 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5582 ddraw_texture_wined3d_object_destroyed,
5585 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5587 return DD_OK;
5590 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
5591 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
5593 struct ddraw_surface *root, *mip, **attach;
5594 struct wined3d_resource_desc wined3d_desc;
5595 struct wined3d_texture *wined3d_texture;
5596 struct wined3d_resource *resource;
5597 struct wined3d_display_mode mode;
5598 DDSURFACEDESC2 *desc, *mip_desc;
5599 struct ddraw_texture *texture;
5600 UINT layers, levels, i, j;
5601 HRESULT hr;
5603 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
5604 ddraw, surface_desc, surface, outer_unknown, version);
5605 if (TRACE_ON(ddraw))
5607 TRACE("Requesting surface desc:\n");
5608 DDRAW_dump_surface_desc(surface_desc);
5611 if (outer_unknown)
5612 return CLASS_E_NOAGGREGATION;
5614 if (!surface)
5615 return E_POINTER;
5617 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
5618 return E_OUTOFMEMORY;
5620 texture->version = version;
5621 texture->surface_desc = *surface_desc;
5622 desc = &texture->surface_desc;
5624 /* Ensure DDSD_CAPS is always set. */
5625 desc->dwFlags |= DDSD_CAPS;
5627 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5629 DWORD flippable = desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_COMPLEX);
5631 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5633 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
5634 HeapFree(GetProcessHeap(), 0, texture);
5635 return DDERR_INVALIDCAPS;
5638 if (flippable)
5640 if (flippable != (DDSCAPS_FLIP | DDSCAPS_COMPLEX))
5642 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
5643 HeapFree(GetProcessHeap(), 0, texture);
5644 return DDERR_INVALIDCAPS;
5647 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->dwBackBufferCount)
5649 WARN("Tried to create a flippable primary surface without any back buffers.\n");
5650 HeapFree(GetProcessHeap(), 0, texture);
5651 return DDERR_INVALIDCAPS;
5654 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
5656 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
5657 HeapFree(GetProcessHeap(), 0, texture);
5658 return DDERR_NOEXCLUSIVEMODE;
5663 /* This is a special case in ddrawex, but not allowed in ddraw. */
5664 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5665 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5667 WARN("Tried to create a surface in both system and video memory.\n");
5668 HeapFree(GetProcessHeap(), 0, texture);
5669 return DDERR_INVALIDCAPS;
5672 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
5673 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
5675 WARN("Cube map faces requested without cube map flag.\n");
5676 HeapFree(GetProcessHeap(), 0, texture);
5677 return DDERR_INVALIDCAPS;
5680 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5681 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
5683 WARN("Cube map without faces requested.\n");
5684 HeapFree(GetProcessHeap(), 0, texture);
5685 return DDERR_INVALIDPARAMS;
5688 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5689 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
5690 FIXME("Partial cube maps not implemented.\n");
5692 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
5694 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5696 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
5697 HeapFree(GetProcessHeap(), 0, texture);
5698 return DDERR_INVALIDCAPS;
5700 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5702 WARN("DDSCAPS2_TEXTUREMANAGE used width DDSCAPS_VIDEOMEMORY "
5703 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
5704 HeapFree(GetProcessHeap(), 0, texture);
5705 return DDERR_INVALIDCAPS;
5709 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
5711 ERR("Failed to get display mode, hr %#x.\n", hr);
5712 HeapFree(GetProcessHeap(), 0, texture);
5713 return hr_ddraw_from_wined3d(hr);
5716 /* No pixelformat given? Use the current screen format. */
5717 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
5719 desc->dwFlags |= DDSD_PIXELFORMAT;
5720 desc->u4.ddpfPixelFormat.dwSize = sizeof(desc->u4.ddpfPixelFormat);
5721 ddrawformat_from_wined3dformat(&desc->u4.ddpfPixelFormat, mode.format_id);
5724 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
5725 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
5727 WARN("Unsupported / unknown pixelformat.\n");
5728 HeapFree(GetProcessHeap(), 0, texture);
5729 return DDERR_INVALIDPIXELFORMAT;
5732 /* No width or no height? Use the screen size. */
5733 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
5735 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
5737 WARN("No width / height specified.\n");
5738 HeapFree(GetProcessHeap(), 0, texture);
5739 return DDERR_INVALIDPARAMS;
5742 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
5743 desc->dwWidth = mode.width;
5744 desc->dwHeight = mode.height;
5747 if (!desc->dwWidth || !desc->dwHeight)
5749 HeapFree(GetProcessHeap(), 0, texture);
5750 return DDERR_INVALIDPARAMS;
5753 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5755 /* The first surface is a front buffer, the back buffers are created
5756 * afterwards. */
5757 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
5758 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
5759 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5760 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
5762 struct wined3d_swapchain_desc swapchain_desc;
5764 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
5765 swapchain_desc.backbuffer_width = mode.width;
5766 swapchain_desc.backbuffer_height = mode.height;
5767 swapchain_desc.backbuffer_format = mode.format_id;
5769 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
5770 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
5772 ERR("Failed to reset device.\n");
5773 HeapFree(GetProcessHeap(), 0, texture);
5774 return hr_ddraw_from_wined3d(hr);
5779 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5780 wined3d_desc.multisample_quality = 0;
5781 wined3d_desc.usage = 0;
5782 wined3d_desc.pool = WINED3D_POOL_DEFAULT;
5783 wined3d_desc.width = desc->dwWidth;
5784 wined3d_desc.height = desc->dwHeight;
5785 wined3d_desc.depth = 1;
5786 wined3d_desc.size = 0;
5788 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
5790 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
5791 /* Do not fail surface creation, only fail 3D device creation. */
5794 /* Mipmap count fixes */
5795 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5797 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
5799 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
5801 /* Mipmap count is given, should not be 0. */
5802 if (!desc->u2.dwMipMapCount)
5804 HeapFree(GetProcessHeap(), 0, texture);
5805 return DDERR_INVALIDPARAMS;
5808 else
5810 /* Undocumented feature: Create sublevels until either the
5811 * width or the height is 1. */
5812 DWORD min = desc->dwWidth < desc->dwHeight ? desc->dwWidth : desc->dwHeight;
5814 desc->u2.dwMipMapCount = 0;
5815 while (min)
5817 ++desc->u2.dwMipMapCount;
5818 min >>= 1;
5822 else
5824 desc->u2.dwMipMapCount = 1;
5827 desc->dwFlags |= DDSD_MIPMAPCOUNT;
5828 levels = desc->u2.dwMipMapCount;
5830 else
5832 levels = 1;
5835 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
5837 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
5839 enum wined3d_resource_type rtype;
5840 DWORD usage = 0;
5842 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5843 rtype = WINED3D_RTYPE_CUBE_TEXTURE;
5844 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5845 rtype = WINED3D_RTYPE_TEXTURE;
5846 else
5847 rtype = WINED3D_RTYPE_SURFACE;
5849 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5850 usage = WINED3DUSAGE_DEPTHSTENCIL;
5851 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5852 usage = WINED3DUSAGE_RENDERTARGET;
5854 if (SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
5855 WINED3D_DEVICE_TYPE_HAL, mode.format_id, usage, rtype, wined3d_desc.format)))
5856 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
5857 else
5858 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5860 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5862 /* Tests show surfaces without memory flags get these flags added
5863 * right after creation. */
5864 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5868 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5869 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5871 WARN("System memory overlays are not allowed.\n");
5872 HeapFree(GetProcessHeap(), 0, texture);
5873 return DDERR_NOOVERLAYHW;
5876 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5878 wined3d_desc.pool = WINED3D_POOL_SYSTEM_MEM;
5880 else
5882 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5883 wined3d_desc.usage |= WINED3DUSAGE_TEXTURE;
5884 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5885 wined3d_desc.usage |= WINED3DUSAGE_DEPTHSTENCIL;
5886 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5887 wined3d_desc.usage |= WINED3DUSAGE_RENDERTARGET;
5889 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
5891 wined3d_desc.pool = WINED3D_POOL_MANAGED;
5892 /* Managed textures have the system memory flag set. */
5893 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5895 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5897 /* Videomemory adds localvidmem. This is mutually exclusive with
5898 * systemmemory and texturemanage. */
5899 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
5900 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
5904 /* If the surface is of the 'ALLOCONLOAD' type, ignore the LPSURFACE
5905 * field. Frank Herbert's Dune specifies a NULL pointer for lpSurface. */
5906 if ((desc->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD) || !desc->lpSurface)
5907 desc->dwFlags &= ~DDSD_LPSURFACE;
5908 if (desc->dwFlags & DDSD_LPSURFACE)
5910 if (wined3d_desc.pool != WINED3D_POOL_SYSTEM_MEM)
5912 WARN("User memory surfaces should be in the system memory pool.\n");
5913 HeapFree(GetProcessHeap(), 0, texture);
5914 return DDERR_INVALIDCAPS;
5917 if (version < 4)
5919 WARN("User memory surfaces not supported before version 4.\n");
5920 HeapFree(GetProcessHeap(), 0, texture);
5921 return DDERR_INVALIDPARAMS;
5924 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
5926 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
5928 WARN("Pitch specified on a compressed user memory surface.\n");
5929 HeapFree(GetProcessHeap(), 0, texture);
5930 return DDERR_INVALIDPARAMS;
5933 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
5935 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
5936 HeapFree(GetProcessHeap(), 0, texture);
5937 return DDERR_INVALIDPARAMS;
5940 else
5942 if (!(desc->dwFlags & DDSD_PITCH))
5944 WARN("User memory surfaces should explicitly specify the pitch.\n");
5945 HeapFree(GetProcessHeap(), 0, texture);
5946 return DDERR_INVALIDPARAMS;
5951 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
5952 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
5954 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
5955 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
5957 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5959 wined3d_desc.resource_type = WINED3D_RTYPE_CUBE_TEXTURE;
5960 layers = 6;
5962 else
5964 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE;
5965 layers = 1;
5968 /* Some applications assume surfaces will always be mapped at the same
5969 * address. Some of those also assume that this address is valid even when
5970 * the surface isn't mapped, and that updates done this way will be
5971 * visible on the screen. The game Nox is such an application,
5972 * Commandos: Behind Enemy Lines is another. We set
5973 * WINED3D_SURFACE_PIN_SYSMEM because of this. */
5974 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, levels,
5975 WINED3D_SURFACE_PIN_SYSMEM, texture, &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
5977 WARN("Failed to create wined3d texture, hr %#x.\n", hr);
5978 HeapFree(GetProcessHeap(), 0, texture);
5979 return hr_ddraw_from_wined3d(hr);
5982 resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
5983 root = wined3d_resource_get_parent(resource);
5984 root->wined3d_texture = wined3d_texture;
5985 root->is_complex_root = TRUE;
5986 texture->root = root;
5988 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
5989 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
5990 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
5991 if (desc->dwFlags & DDSD_CKDESTBLT)
5992 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
5993 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
5994 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
5995 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
5996 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
5997 if (desc->dwFlags & DDSD_CKSRCBLT)
5998 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
5999 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6001 for (i = 0; i < layers; ++i)
6003 attach = &root->complex_array[layers - 1 - i];
6005 for (j = 0; j < levels; ++j)
6007 resource = wined3d_texture_get_sub_resource(wined3d_texture, i * levels + j);
6008 mip = wined3d_resource_get_parent(resource);
6009 mip_desc = &mip->surface_desc;
6011 if (j)
6012 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6013 else
6014 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6016 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6018 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6020 switch (i)
6022 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6023 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6024 break;
6025 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6026 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6027 break;
6028 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6029 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6030 break;
6031 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6032 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6033 break;
6034 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6035 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6036 break;
6037 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6038 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6039 break;
6044 if (mip == root)
6045 continue;
6047 *attach = mip;
6048 attach = &mip->complex_array[0];
6052 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6054 unsigned int count = desc->dwBackBufferCount;
6055 struct ddraw_surface *last = root;
6057 attach = &last->complex_array[0];
6058 for (i = 0; i < count; ++i)
6060 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
6062 hr = E_OUTOFMEMORY;
6063 goto fail;
6066 texture->version = version;
6067 texture->surface_desc = root->surface_desc;
6068 desc = &texture->surface_desc;
6070 /* Only one surface in the flipping chain is a back buffer, one is
6071 * a front buffer, the others are just flippable surfaces. */
6072 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6073 | DDSCAPS_BACKBUFFER);
6074 if (!i)
6075 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6076 desc->dwBackBufferCount = 0;
6078 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, 1,
6079 WINED3D_SURFACE_PIN_SYSMEM, texture, &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
6081 HeapFree(GetProcessHeap(), 0, texture);
6082 hr = hr_ddraw_from_wined3d(hr);
6083 goto fail;
6086 resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
6087 last = wined3d_resource_get_parent(resource);
6088 last->wined3d_texture = wined3d_texture;
6089 texture->root = last;
6091 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6092 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6093 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6094 if (desc->dwFlags & DDSD_CKDESTBLT)
6095 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6096 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6097 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6098 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6099 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6100 if (desc->dwFlags & DDSD_CKSRCBLT)
6101 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6102 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6104 *attach = last;
6105 attach = &last->complex_array[0];
6107 *attach = root;
6110 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6111 ddraw->primary = root;
6112 *surface = root;
6114 return DD_OK;
6116 fail:
6117 if (version == 7)
6118 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6119 else if (version == 4)
6120 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6121 else
6122 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6124 return hr;
6127 HRESULT ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw, struct ddraw_texture *texture,
6128 struct wined3d_surface *wined3d_surface, const struct wined3d_parent_ops **parent_ops)
6130 DDSURFACEDESC2 *desc = &surface->surface_desc;
6131 struct wined3d_resource_desc wined3d_desc;
6132 unsigned int version = texture->version;
6133 HRESULT hr;
6135 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6136 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6137 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6138 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6139 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6140 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6141 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6142 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6143 surface->iface_count = 1;
6144 surface->version = version;
6145 surface->ddraw = ddraw;
6147 if (version == 7)
6149 surface->ref7 = 1;
6150 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6152 else if (version == 4)
6154 surface->ref4 = 1;
6155 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6157 else
6159 surface->ref1 = 1;
6160 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6163 *desc = texture->surface_desc;
6164 wined3d_resource_get_desc(wined3d_surface_get_resource(wined3d_surface), &wined3d_desc);
6165 desc->dwWidth = wined3d_desc.width;
6166 desc->dwHeight = wined3d_desc.height;
6167 surface->first_attached = surface;
6169 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6171 if (desc->dwFlags & DDSD_LPSURFACE)
6173 if ((desc->dwFlags & DDSD_LINEARSIZE)
6174 && desc->u1.dwLinearSize < wined3d_surface_get_pitch(wined3d_surface) * ((desc->dwHeight + 3) / 4))
6176 WARN("Invalid linear size %u specified.\n", desc->u1.dwLinearSize);
6177 return DDERR_INVALIDPARAMS;
6180 if (FAILED(hr = wined3d_surface_update_desc(wined3d_surface, wined3d_desc.width,
6181 wined3d_desc.height, wined3d_desc.format, WINED3D_MULTISAMPLE_NONE, 0,
6182 desc->lpSurface, 0)))
6184 ERR("Failed to set surface memory, hr %#x.\n", hr);
6185 return hr;
6188 desc->dwFlags |= DDSD_LINEARSIZE;
6189 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6190 desc->u1.dwLinearSize = ~0u;
6192 else
6194 desc->dwFlags |= DDSD_LINEARSIZE;
6195 desc->dwFlags &= ~DDSD_PITCH;
6196 desc->u1.dwLinearSize = wined3d_surface_get_pitch(wined3d_surface) * ((desc->dwHeight + 3) / 4);
6199 else
6201 if (desc->dwFlags & DDSD_LPSURFACE)
6203 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
6204 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
6206 WARN("Invalid pitch %u specified.\n", desc->u1.lPitch);
6207 return DDERR_INVALIDPARAMS;
6210 if (FAILED(hr = wined3d_surface_update_desc(wined3d_surface, wined3d_desc.width,
6211 wined3d_desc.height, wined3d_desc.format, WINED3D_MULTISAMPLE_NONE, 0,
6212 desc->lpSurface, desc->u1.lPitch)))
6214 ERR("Failed to set surface memory, hr %#x.\n", hr);
6215 return hr;
6218 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6220 else
6222 desc->dwFlags |= DDSD_PITCH;
6223 desc->dwFlags &= ~DDSD_LINEARSIZE;
6224 desc->u1.lPitch = wined3d_surface_get_pitch(wined3d_surface);
6228 wined3d_surface_incref(wined3d_surface);
6229 surface->wined3d_surface = wined3d_surface;
6230 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6232 wined3d_private_store_init(&surface->private_store);
6234 return DD_OK;