ddraw: Set user memory in ddraw_surface_create() instead of ddraw_surface_init().
[wine.git] / dlls / ddraw / surface.c
blob8c48140cf4283f905a5706fb36e57ee342752d23
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_rtv)
326 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
327 if (surface->wined3d_surface)
328 wined3d_surface_incref(surface->wined3d_surface);
329 if (surface->wined3d_texture)
330 wined3d_texture_incref(surface->wined3d_texture);
331 wined3d_mutex_unlock();
335 /*****************************************************************************
336 * IDirectDrawSurface7::AddRef
338 * A normal addref implementation
340 * Returns:
341 * The new refcount
343 *****************************************************************************/
344 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
346 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
347 ULONG refcount = InterlockedIncrement(&This->ref7);
349 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
351 if (refcount == 1)
353 ddraw_surface_add_iface(This);
356 return refcount;
359 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
361 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
362 ULONG refcount = InterlockedIncrement(&This->ref4);
364 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
366 if (refcount == 1)
368 ddraw_surface_add_iface(This);
371 return refcount;
374 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
376 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
377 ULONG refcount = InterlockedIncrement(&This->ref3);
379 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
381 if (refcount == 1)
383 ddraw_surface_add_iface(This);
386 return refcount;
389 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
391 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
392 ULONG refcount = InterlockedIncrement(&This->ref2);
394 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
396 if (refcount == 1)
398 ddraw_surface_add_iface(This);
401 return refcount;
404 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
406 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
407 ULONG refcount = InterlockedIncrement(&This->ref1);
409 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
411 if (refcount == 1)
413 ddraw_surface_add_iface(This);
416 return refcount;
419 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
421 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
422 ULONG refcount = InterlockedIncrement(&This->gamma_count);
424 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
426 if (refcount == 1)
428 ddraw_surface_add_iface(This);
431 return refcount;
434 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
436 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
438 TRACE("iface %p.\n", iface);
440 return IUnknown_AddRef(surface->texture_outer);
443 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
445 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
447 TRACE("iface %p.\n", iface);
449 return IUnknown_AddRef(surface->texture_outer);
452 static HRESULT ddraw_surface_set_palette(struct ddraw_surface *surface, IDirectDrawPalette *palette)
454 struct ddraw_palette *palette_impl = unsafe_impl_from_IDirectDrawPalette(palette);
455 struct ddraw_palette *prev;
457 TRACE("iface %p, palette %p.\n", surface, palette);
459 if (palette_impl && palette_impl->flags & DDPCAPS_ALPHA
460 && !(surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE))
462 WARN("Alpha palette set on non-texture surface, returning DDERR_INVALIDSURFACETYPE.\n");
463 return DDERR_INVALIDSURFACETYPE;
466 if (!format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
467 return DDERR_INVALIDPIXELFORMAT;
469 wined3d_mutex_lock();
471 prev = surface->palette;
472 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
474 if (prev)
475 prev->flags &= ~DDPCAPS_PRIMARYSURFACE;
476 if (palette_impl)
477 palette_impl->flags |= DDPCAPS_PRIMARYSURFACE;
478 wined3d_swapchain_set_palette(surface->ddraw->wined3d_swapchain,
479 palette_impl ? palette_impl->wineD3DPalette : NULL);
480 ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
482 if (palette_impl)
483 IDirectDrawPalette_AddRef(&palette_impl->IDirectDrawPalette_iface);
484 if (prev)
485 IDirectDrawPalette_Release(&prev->IDirectDrawPalette_iface);
486 surface->palette = palette_impl;
488 wined3d_mutex_unlock();
490 return DD_OK;
493 static void ddraw_surface_cleanup(struct ddraw_surface *surface)
495 struct ddraw_surface *surf;
496 UINT i;
498 TRACE("surface %p.\n", surface);
500 /* The refcount test shows that the palette is detached when the surface
501 * is destroyed. */
502 ddraw_surface_set_palette(surface, NULL);
504 /* Loop through all complex attached surfaces and destroy them.
506 * Yet again, only the root can have more than one complexly attached
507 * surface, all the others have a total of one. */
508 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
510 if (!surface->complex_array[i])
511 break;
513 surf = surface->complex_array[i];
514 surface->complex_array[i] = NULL;
515 if (!surf->is_complex_root)
516 ddraw_surface_cleanup(surf);
519 if (surface->device1)
520 IUnknown_Release(&surface->device1->IUnknown_inner);
522 if (surface->iface_count > 1)
524 /* This can happen when a complex surface is destroyed, because the
525 * 2nd surface was addref()ed when the app called
526 * GetAttachedSurface(). */
527 WARN("Destroying surface %p with refcounts 7: %u 4: %u 3: %u 2: %u 1: %u.\n",
528 surface, surface->ref7, surface->ref4, surface->ref3, surface->ref2, surface->ref1);
531 if (surface->wined3d_rtv)
532 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
533 if (surface->wined3d_texture)
534 wined3d_texture_decref(surface->wined3d_texture);
535 if (surface->wined3d_surface)
536 wined3d_surface_decref(surface->wined3d_surface);
539 ULONG ddraw_surface_release_iface(struct ddraw_surface *This)
541 ULONG iface_count = InterlockedDecrement(&This->iface_count);
542 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
544 if (iface_count == 0)
546 IUnknown *release_iface = This->ifaceToRelease;
548 /* Complex attached surfaces are destroyed implicitly when the root is released */
549 wined3d_mutex_lock();
550 if(!This->is_complex_root)
552 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
553 wined3d_mutex_unlock();
554 return iface_count;
556 ddraw_surface_cleanup(This);
557 wined3d_mutex_unlock();
559 if (release_iface)
560 IUnknown_Release(release_iface);
563 return iface_count;
566 /*****************************************************************************
567 * IDirectDrawSurface7::Release
569 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
570 * surface is destroyed.
572 * Destroying the surface is a bit tricky. For the connection between
573 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
574 * It has a nice graph explaining the connection.
576 * What happens here is basically this:
577 * When a surface is destroyed, its WineD3DSurface is released,
578 * and the refcount of the DirectDraw interface is reduced by 1. If it has
579 * complex surfaces attached to it, then these surfaces are destroyed too,
580 * regardless of their refcount. If any surface being destroyed has another
581 * surface attached to it (with a "soft" attachment, not complex), then
582 * this surface is detached with DeleteAttachedSurface.
584 * When the surface is a texture, the WineD3DTexture is released.
585 * If the surface is the Direct3D render target, then the D3D
586 * capabilities of the WineD3DDevice are uninitialized, which causes the
587 * swapchain to be released.
589 * When a complex sublevel falls to ref zero, then this is ignored.
591 * Returns:
592 * The new refcount
594 *****************************************************************************/
595 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
597 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
598 ULONG refcount = InterlockedDecrement(&This->ref7);
600 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
602 if (refcount == 0)
604 ddraw_surface_release_iface(This);
607 return refcount;
610 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
612 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
613 ULONG refcount = InterlockedDecrement(&This->ref4);
615 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
617 if (refcount == 0)
619 ddraw_surface_release_iface(This);
622 return refcount;
625 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
627 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
628 ULONG refcount = InterlockedDecrement(&This->ref3);
630 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
632 if (refcount == 0)
634 ddraw_surface_release_iface(This);
637 return refcount;
640 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
642 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
643 ULONG refcount = InterlockedDecrement(&This->ref2);
645 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
647 if (refcount == 0)
649 ddraw_surface_release_iface(This);
652 return refcount;
655 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
657 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
658 ULONG refcount = InterlockedDecrement(&This->ref1);
660 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
662 if (refcount == 0)
664 ddraw_surface_release_iface(This);
667 return refcount;
670 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
672 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
673 ULONG refcount = InterlockedDecrement(&This->gamma_count);
675 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
677 if (refcount == 0)
679 ddraw_surface_release_iface(This);
682 return refcount;
685 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
687 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
689 TRACE("iface %p.\n", iface);
691 return IUnknown_Release(surface->texture_outer);
694 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
696 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
698 TRACE("iface %p.\n", iface);
700 return IUnknown_Release(surface->texture_outer);
703 /*****************************************************************************
704 * IDirectDrawSurface7::GetAttachedSurface
706 * Returns an attached surface with the requested caps. Surface attachment
707 * and complex surfaces are not clearly described by the MSDN or sdk,
708 * so this method is tricky and likely to contain problems.
709 * This implementation searches the complex list first, then the
710 * attachment chain.
712 * The chains are searched from This down to the last surface in the chain,
713 * not from the first element in the chain. The first surface found is
714 * returned. The MSDN says that this method fails if more than one surface
715 * matches the caps, but it is not sure if that is right. The attachment
716 * structure may not even allow two matching surfaces.
718 * The found surface is AddRef-ed before it is returned.
720 * Params:
721 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
722 * Surface: Address to store the found surface
724 * Returns:
725 * DD_OK on success
726 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
727 * DDERR_NOTFOUND if no surface was found
729 *****************************************************************************/
730 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
731 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
733 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
734 struct ddraw_surface *surf;
735 DDSCAPS2 our_caps;
736 int i;
738 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
740 wined3d_mutex_lock();
742 if(This->version < 7)
744 /* Earlier dx apps put garbage into these members, clear them */
745 our_caps.dwCaps = Caps->dwCaps;
746 our_caps.dwCaps2 = 0;
747 our_caps.dwCaps3 = 0;
748 our_caps.dwCaps4 = 0;
750 else
752 our_caps = *Caps;
755 TRACE("(%p): Looking for caps: %x,%x,%x,%x\n", This, our_caps.dwCaps, our_caps.dwCaps2, our_caps.dwCaps3, our_caps.dwCaps4); /* FIXME: Better debugging */
757 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
759 surf = This->complex_array[i];
760 if(!surf) break;
762 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
763 surf->surface_desc.ddsCaps.dwCaps,
764 surf->surface_desc.ddsCaps.dwCaps2,
765 surf->surface_desc.ddsCaps.dwCaps3,
766 surf->surface_desc.ddsCaps.dwCaps4);
768 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
769 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
771 /* MSDN: "This method fails if more than one surface is attached
772 * that matches the capabilities requested."
774 * Not sure how to test this.
777 TRACE("(%p): Returning surface %p\n", This, surf);
778 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
779 *Surface = &surf->IDirectDrawSurface7_iface;
780 ddraw_surface7_AddRef(*Surface);
781 wined3d_mutex_unlock();
783 return DD_OK;
787 /* Next, look at the attachment chain */
788 surf = This;
790 while( (surf = surf->next_attached) )
792 TRACE("Surface: (%p) caps: %#x, %#x, %#x, %#x.\n", surf,
793 surf->surface_desc.ddsCaps.dwCaps,
794 surf->surface_desc.ddsCaps.dwCaps2,
795 surf->surface_desc.ddsCaps.dwCaps3,
796 surf->surface_desc.ddsCaps.dwCaps4);
798 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
799 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
801 TRACE("(%p): Returning surface %p\n", This, surf);
802 *Surface = &surf->IDirectDrawSurface7_iface;
803 ddraw_surface7_AddRef(*Surface);
804 wined3d_mutex_unlock();
805 return DD_OK;
809 TRACE("(%p) Didn't find a valid surface\n", This);
811 wined3d_mutex_unlock();
813 *Surface = NULL;
814 return DDERR_NOTFOUND;
817 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
818 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
820 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
821 struct ddraw_surface *attachment_impl;
822 IDirectDrawSurface7 *attachment7;
823 HRESULT hr;
825 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
827 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
828 caps, &attachment7);
829 if (FAILED(hr))
831 *attachment = NULL;
832 return hr;
834 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
835 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
836 ddraw_surface4_AddRef(*attachment);
837 ddraw_surface7_Release(attachment7);
839 return hr;
842 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
843 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
845 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
846 struct ddraw_surface *attachment_impl;
847 IDirectDrawSurface7 *attachment7;
848 DDSCAPS2 caps2;
849 HRESULT hr;
851 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
853 caps2.dwCaps = caps->dwCaps;
854 caps2.dwCaps2 = 0;
855 caps2.dwCaps3 = 0;
856 caps2.dwCaps4 = 0;
858 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
859 &caps2, &attachment7);
860 if (FAILED(hr))
862 *attachment = NULL;
863 return hr;
865 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
866 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
867 ddraw_surface3_AddRef(*attachment);
868 ddraw_surface7_Release(attachment7);
870 return hr;
873 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
874 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
876 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
877 struct ddraw_surface *attachment_impl;
878 IDirectDrawSurface7 *attachment7;
879 DDSCAPS2 caps2;
880 HRESULT hr;
882 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
884 caps2.dwCaps = caps->dwCaps;
885 caps2.dwCaps2 = 0;
886 caps2.dwCaps3 = 0;
887 caps2.dwCaps4 = 0;
889 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
890 &caps2, &attachment7);
891 if (FAILED(hr))
893 *attachment = NULL;
894 return hr;
896 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
897 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
898 ddraw_surface2_AddRef(*attachment);
899 ddraw_surface7_Release(attachment7);
901 return hr;
904 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
905 DDSCAPS *caps, IDirectDrawSurface **attachment)
907 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
908 struct ddraw_surface *attachment_impl;
909 IDirectDrawSurface7 *attachment7;
910 DDSCAPS2 caps2;
911 HRESULT hr;
913 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
915 caps2.dwCaps = caps->dwCaps;
916 caps2.dwCaps2 = 0;
917 caps2.dwCaps3 = 0;
918 caps2.dwCaps4 = 0;
920 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
921 &caps2, &attachment7);
922 if (FAILED(hr))
924 *attachment = NULL;
925 return hr;
927 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
928 *attachment = &attachment_impl->IDirectDrawSurface_iface;
929 ddraw_surface1_AddRef(*attachment);
930 ddraw_surface7_Release(attachment7);
932 return hr;
935 /*****************************************************************************
936 * IDirectDrawSurface7::Lock
938 * Locks the surface and returns a pointer to the surface's memory
940 * Params:
941 * Rect: Rectangle to lock. If NULL, the whole surface is locked
942 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
943 * Flags: Locking flags, e.g Read only or write only
944 * h: An event handle that's not used and must be NULL
946 * Returns:
947 * DD_OK on success
948 * DDERR_INVALIDPARAMS if DDSD is NULL
949 * For more details, see IWineD3DSurface::LockRect
951 *****************************************************************************/
952 static HRESULT surface_lock(struct ddraw_surface *This,
953 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
955 struct wined3d_map_desc map_desc;
956 HRESULT hr = DD_OK;
958 TRACE("This %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
959 This, wine_dbgstr_rect(Rect), DDSD, Flags, h);
961 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
962 wined3d_mutex_lock();
964 /* Should I check for the handle to be NULL?
966 * The DDLOCK flags and the D3DLOCK flags are equal
967 * for the supported values. The others are ignored by WineD3D
970 /* Windows zeroes this if the rect is invalid */
971 DDSD->lpSurface = 0;
973 if (Rect)
975 if ((Rect->left < 0)
976 || (Rect->top < 0)
977 || (Rect->left > Rect->right)
978 || (Rect->top > Rect->bottom)
979 || (Rect->right > This->surface_desc.dwWidth)
980 || (Rect->bottom > This->surface_desc.dwHeight))
982 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
983 wined3d_mutex_unlock();
984 return DDERR_INVALIDPARAMS;
988 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
989 hr = ddraw_surface_update_frontbuffer(This, Rect, TRUE);
990 if (SUCCEEDED(hr))
991 hr = wined3d_surface_map(This->wined3d_surface, &map_desc, Rect, Flags);
992 if (FAILED(hr))
994 wined3d_mutex_unlock();
995 switch(hr)
997 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
998 * specific error. But since IWineD3DSurface::LockRect returns that error in this
999 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
1000 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
1001 * is much easier to do it in one place in ddraw
1003 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
1004 default: return hr;
1008 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1010 if (Flags & DDLOCK_READONLY)
1011 memset(&This->ddraw->primary_lock, 0, sizeof(This->ddraw->primary_lock));
1012 else if (Rect)
1013 This->ddraw->primary_lock = *Rect;
1014 else
1015 SetRect(&This->ddraw->primary_lock, 0, 0, This->surface_desc.dwWidth, This->surface_desc.dwHeight);
1018 /* Windows does not set DDSD_LPSURFACE on locked surfaces. */
1019 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
1020 DDSD->lpSurface = map_desc.data;
1022 TRACE("locked surface returning description :\n");
1023 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1025 wined3d_mutex_unlock();
1027 return DD_OK;
1030 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1031 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1033 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1035 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1036 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1038 if (!surface_desc) return DDERR_INVALIDPARAMS;
1039 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1040 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1042 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1043 return DDERR_INVALIDPARAMS;
1045 return surface_lock(surface, rect, surface_desc, flags, h);
1048 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1049 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1051 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1053 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1054 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1056 if (!surface_desc) return DDERR_INVALIDPARAMS;
1057 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1058 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1060 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1061 return DDERR_INVALIDPARAMS;
1063 return surface_lock(surface, rect, surface_desc, flags, h);
1066 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1067 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1069 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1070 DDSURFACEDESC2 surface_desc2;
1071 HRESULT hr;
1073 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1074 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1076 if (!surface_desc) return DDERR_INVALIDPARAMS;
1077 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1078 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1080 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1081 return DDERR_INVALIDPARAMS;
1084 surface_desc2.dwSize = surface_desc->dwSize;
1085 surface_desc2.dwFlags = 0;
1086 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1087 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1088 surface_desc->dwSize = surface_desc2.dwSize;
1089 return hr;
1092 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1093 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1095 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1096 DDSURFACEDESC2 surface_desc2;
1097 HRESULT hr;
1099 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1100 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1102 if (!surface_desc) return DDERR_INVALIDPARAMS;
1103 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1104 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1106 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1107 return DDERR_INVALIDPARAMS;
1110 surface_desc2.dwSize = surface_desc->dwSize;
1111 surface_desc2.dwFlags = 0;
1112 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1113 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1114 surface_desc->dwSize = surface_desc2.dwSize;
1115 return hr;
1118 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1119 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1121 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1122 DDSURFACEDESC2 surface_desc2;
1123 HRESULT hr;
1124 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1125 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1127 if (!surface_desc) return DDERR_INVALIDPARAMS;
1128 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1129 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1131 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1132 return DDERR_INVALIDPARAMS;
1135 surface_desc2.dwSize = surface_desc->dwSize;
1136 surface_desc2.dwFlags = 0;
1137 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1138 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1139 surface_desc->dwSize = surface_desc2.dwSize;
1140 return hr;
1143 /*****************************************************************************
1144 * IDirectDrawSurface7::Unlock
1146 * Unlocks an locked surface
1148 * Params:
1149 * Rect: Not used by this implementation
1151 * Returns:
1152 * D3D_OK on success
1153 * For more details, see IWineD3DSurface::UnlockRect
1155 *****************************************************************************/
1156 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
1158 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1159 HRESULT hr;
1161 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1163 wined3d_mutex_lock();
1164 hr = wined3d_surface_unmap(surface->wined3d_surface);
1165 if (SUCCEEDED(hr) && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1166 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE);
1167 wined3d_mutex_unlock();
1169 return hr;
1172 static HRESULT WINAPI ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1174 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1176 TRACE("iface %p, rect %p.\n", iface, pRect);
1178 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, pRect);
1181 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1183 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1185 TRACE("iface %p, data %p.\n", iface, data);
1187 /* data might not be the LPRECT of later versions, so drop it. */
1188 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1191 static HRESULT WINAPI ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1193 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1195 TRACE("iface %p, data %p.\n", iface, data);
1197 /* data might not be the LPRECT of later versions, so drop it. */
1198 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1201 static HRESULT WINAPI ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1203 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1205 TRACE("iface %p, data %p.\n", iface, data);
1207 /* data might not be the LPRECT of later versions, so drop it. */
1208 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1211 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *src, DWORD flags)
1213 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface7(iface);
1214 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface7(src);
1215 struct wined3d_rendertarget_view *tmp_rtv, *src_rtv, *rtv;
1216 struct ddraw_texture *ddraw_texture, *prev_ddraw_texture;
1217 DDSCAPS2 caps = {DDSCAPS_FLIP, 0, 0, 0};
1218 struct wined3d_texture *texture;
1219 IDirectDrawSurface7 *current;
1220 struct wined3d_surface *tmp;
1221 HRESULT hr;
1223 TRACE("iface %p, src %p, flags %#x.\n", iface, src, flags);
1225 if (src == iface || !(dst_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1226 return DDERR_NOTFLIPPABLE;
1228 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
1229 return DDERR_SURFACELOST;
1231 wined3d_mutex_lock();
1233 tmp_rtv = ddraw_surface_get_rendertarget_view(dst_impl);
1234 tmp = dst_impl->wined3d_surface;
1235 texture = dst_impl->wined3d_texture;
1236 rtv = wined3d_device_get_rendertarget_view(dst_impl->ddraw->wined3d_device, 0);
1237 ddraw_texture = wined3d_texture_get_parent(dst_impl->wined3d_texture);
1239 if (src_impl)
1241 for (current = iface; current != src;)
1243 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1245 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1246 wined3d_mutex_unlock();
1247 return DDERR_NOTFLIPPABLE;
1249 ddraw_surface7_Release(current);
1250 if (current == iface)
1252 WARN("Surface %p is not on the same flip chain as surface %p.\n", src, iface);
1253 wined3d_mutex_unlock();
1254 return DDERR_NOTFLIPPABLE;
1258 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1259 if (rtv == dst_impl->wined3d_rtv)
1260 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1261 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1262 dst_impl->wined3d_rtv = src_rtv;
1263 wined3d_resource_set_parent(wined3d_surface_get_resource(src_impl->wined3d_surface), dst_impl);
1264 dst_impl->wined3d_surface = src_impl->wined3d_surface;
1265 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1266 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1267 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1268 ddraw_texture = prev_ddraw_texture;
1270 else
1272 for (current = iface;;)
1274 if (FAILED(hr = ddraw_surface7_GetAttachedSurface(current, &caps, &current)))
1276 ERR("Can't find a flip target\n");
1277 wined3d_mutex_unlock();
1278 return DDERR_NOTFLIPPABLE; /* Unchecked */
1280 ddraw_surface7_Release(current);
1281 if (current == iface)
1283 dst_impl = impl_from_IDirectDrawSurface7(iface);
1284 break;
1287 src_impl = impl_from_IDirectDrawSurface7(current);
1288 src_rtv = ddraw_surface_get_rendertarget_view(src_impl);
1289 if (rtv == dst_impl->wined3d_rtv)
1290 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, src_rtv, FALSE);
1291 wined3d_rendertarget_view_set_parent(src_rtv, dst_impl);
1292 dst_impl->wined3d_rtv = src_rtv;
1293 wined3d_resource_set_parent(wined3d_surface_get_resource(src_impl->wined3d_surface), dst_impl);
1294 dst_impl->wined3d_surface = src_impl->wined3d_surface;
1295 prev_ddraw_texture = wined3d_texture_get_parent(src_impl->wined3d_texture);
1296 wined3d_resource_set_parent(wined3d_texture_get_resource(src_impl->wined3d_texture), ddraw_texture);
1297 ddraw_texture = prev_ddraw_texture;
1298 dst_impl->wined3d_texture = src_impl->wined3d_texture;
1299 dst_impl = src_impl;
1303 /* We don't have to worry about potential texture bindings, since
1304 * flippable surfaces can never be textures. */
1305 if (rtv == src_impl->wined3d_rtv)
1306 wined3d_device_set_rendertarget_view(dst_impl->ddraw->wined3d_device, 0, tmp_rtv, FALSE);
1307 wined3d_rendertarget_view_set_parent(tmp_rtv, src_impl);
1308 src_impl->wined3d_rtv = tmp_rtv;
1309 wined3d_resource_set_parent(wined3d_surface_get_resource(tmp), src_impl);
1310 src_impl->wined3d_surface = tmp;
1311 wined3d_resource_set_parent(wined3d_texture_get_resource(texture), ddraw_texture);
1312 src_impl->wined3d_texture = texture;
1314 if (flags)
1316 static UINT once;
1317 if (!once++)
1318 FIXME("Ignoring flags %#x.\n", flags);
1319 else
1320 WARN("Ignoring flags %#x.\n", flags);
1323 if (dst_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1324 hr = ddraw_surface_update_frontbuffer(dst_impl, NULL, FALSE);
1325 else
1326 hr = DD_OK;
1328 wined3d_mutex_unlock();
1330 return hr;
1333 static HRESULT WINAPI ddraw_surface4_Flip(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *dst, DWORD flags)
1335 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1336 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1338 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1340 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1341 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1344 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
1346 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1347 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1349 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1351 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1352 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1355 static HRESULT WINAPI ddraw_surface2_Flip(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *dst, DWORD flags)
1357 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1358 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1360 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1362 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1363 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1366 static HRESULT WINAPI ddraw_surface1_Flip(IDirectDrawSurface *iface, IDirectDrawSurface *dst, DWORD flags)
1368 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1369 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1371 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1373 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1374 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1377 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1378 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags,
1379 const WINEDDBLTFX *fx, enum wined3d_texture_filter_type filter)
1381 struct wined3d_surface *wined3d_src_surface = src_surface ? src_surface->wined3d_surface : NULL;
1382 RECT src_rect, dst_rect;
1383 float scale_x, scale_y;
1384 const RECT *clip_rect;
1385 UINT clip_list_size;
1386 RGNDATA *clip_list;
1387 HRESULT hr = DD_OK;
1388 UINT i;
1390 if (!dst_surface->clipper)
1392 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1393 hr = ddraw_surface_update_frontbuffer(src_surface, src_rect_in, TRUE);
1394 if (SUCCEEDED(hr))
1395 hr = wined3d_surface_blt(dst_surface->wined3d_surface, dst_rect_in,
1396 wined3d_src_surface, src_rect_in, flags, fx, filter);
1397 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
1398 hr = ddraw_surface_update_frontbuffer(dst_surface, dst_rect_in, FALSE);
1400 return hr;
1403 if (!dst_rect_in)
1405 dst_rect.left = 0;
1406 dst_rect.top = 0;
1407 dst_rect.right = dst_surface->surface_desc.dwWidth;
1408 dst_rect.bottom = dst_surface->surface_desc.dwHeight;
1410 else
1412 dst_rect = *dst_rect_in;
1415 if (IsRectEmpty(&dst_rect))
1416 return DDERR_INVALIDRECT;
1418 if (src_surface)
1420 if (!src_rect_in)
1422 src_rect.left = 0;
1423 src_rect.top = 0;
1424 src_rect.right = src_surface->surface_desc.dwWidth;
1425 src_rect.bottom = src_surface->surface_desc.dwHeight;
1427 else
1429 src_rect = *src_rect_in;
1432 if (IsRectEmpty(&src_rect))
1433 return DDERR_INVALIDRECT;
1435 else
1437 SetRect(&src_rect, 0, 0, 0, 0);
1440 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1441 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1443 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1444 &dst_rect, NULL, &clip_list_size)))
1446 WARN("Failed to get clip list size, hr %#x.\n", hr);
1447 return hr;
1450 if (!(clip_list = HeapAlloc(GetProcessHeap(), 0, clip_list_size)))
1452 WARN("Failed to allocate clip list.\n");
1453 return E_OUTOFMEMORY;
1456 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1457 &dst_rect, clip_list, &clip_list_size)))
1459 WARN("Failed to get clip list, hr %#x.\n", hr);
1460 HeapFree(GetProcessHeap(), 0, clip_list);
1461 return hr;
1464 clip_rect = (RECT *)clip_list->Buffer;
1465 for (i = 0; i < clip_list->rdh.nCount; ++i)
1467 RECT src_rect_clipped = src_rect;
1469 if (src_surface)
1471 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1472 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1473 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1474 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1476 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1478 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE)))
1479 break;
1483 if (FAILED(hr = wined3d_surface_blt(dst_surface->wined3d_surface, &clip_rect[i],
1484 wined3d_src_surface, &src_rect_clipped, flags, fx, filter)))
1485 break;
1487 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
1489 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE)))
1490 break;
1494 HeapFree(GetProcessHeap(), 0, clip_list);
1495 return hr;
1498 /*****************************************************************************
1499 * IDirectDrawSurface7::Blt
1501 * Performs a blit on the surface
1503 * Params:
1504 * DestRect: Destination rectangle, can be NULL
1505 * SrcSurface: Source surface, can be NULL
1506 * SrcRect: Source rectangle, can be NULL
1507 * Flags: Blt flags
1508 * DDBltFx: Some extended blt parameters, connected to the flags
1510 * Returns:
1511 * D3D_OK on success
1512 * See IWineD3DSurface::Blt for more details
1514 *****************************************************************************/
1515 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1516 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1518 struct ddraw_surface *dst_surface = impl_from_IDirectDrawSurface7(iface);
1519 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1520 HRESULT hr = DD_OK;
1522 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1523 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1525 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1526 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1528 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1529 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1530 return DDERR_INVALIDPARAMS;
1533 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1534 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1535 return DDERR_INVALIDPARAMS;
1538 wined3d_mutex_lock();
1540 if (Flags & DDBLT_KEYSRC && (!src_surface || !(src_surface->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1542 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1543 wined3d_mutex_unlock();
1544 return DDERR_INVALIDPARAMS;
1547 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1548 * does, copy the struct, and replace the ddraw surfaces with the wined3d
1549 * surfaces. So far no blitting operations using surfaces in the bltfx
1550 * struct are supported anyway. */
1551 hr = ddraw_surface_blt_clipped(dst_surface, DestRect, src_surface, SrcRect,
1552 Flags, (WINEDDBLTFX *)DDBltFx, WINED3D_TEXF_LINEAR);
1554 wined3d_mutex_unlock();
1555 switch(hr)
1557 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1558 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
1559 default: return hr;
1563 static HRESULT WINAPI ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1564 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1566 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1567 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1569 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1570 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1572 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1573 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1576 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1577 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1579 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1580 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1582 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1583 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1585 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1586 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1589 static HRESULT WINAPI ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1590 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1592 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1593 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1595 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1596 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1598 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1599 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1602 static HRESULT WINAPI ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1603 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1605 struct ddraw_surface *dst = impl_from_IDirectDrawSurface(iface);
1606 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1608 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1609 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1611 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1612 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1615 /*****************************************************************************
1616 * IDirectDrawSurface7::AddAttachedSurface
1618 * Attaches a surface to another surface. How the surface attachments work
1619 * is not totally understood yet, and this method is prone to problems.
1620 * The surface that is attached is AddRef-ed.
1622 * Tests with complex surfaces suggest that the surface attachments form a
1623 * tree, but no method to test this has been found yet.
1625 * The attachment list consists of a first surface (first_attached) and
1626 * for each surface a pointer to the next attached surface (next_attached).
1627 * For the first surface, and a surface that has no attachments
1628 * first_attached points to the surface itself. A surface that has
1629 * no successors in the chain has next_attached set to NULL.
1631 * Newly attached surfaces are attached right after the root surface.
1632 * If a surface is attached to a complex surface compound, it's attached to
1633 * the surface that the app requested, not the complex root. See
1634 * GetAttachedSurface for a description how surfaces are found.
1636 * This is how the current implementation works, and it was coded by looking
1637 * at the needs of the applications.
1639 * So far only Z-Buffer attachments are tested, and they are activated in
1640 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1641 * Back buffers should work in 2D mode, but they are not tested(They can be
1642 * attached in older iface versions). Rendering to the front buffer and
1643 * switching between that and double buffering is not yet implemented in
1644 * WineD3D, so for 3D it might have unexpected results.
1646 * ddraw_surface_attach_surface is the real thing,
1647 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1648 * performs additional checks. Version 7 of this interface is much more restrictive
1649 * than its predecessors.
1651 * Params:
1652 * Attach: Surface to attach to iface
1654 * Returns:
1655 * DD_OK on success
1656 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1658 *****************************************************************************/
1659 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1661 TRACE("surface %p, attachment %p.\n", This, Surf);
1663 if(Surf == This)
1664 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1666 wined3d_mutex_lock();
1668 /* Check if the surface is already attached somewhere */
1669 if (Surf->next_attached || Surf->first_attached != Surf)
1671 /* TODO: Test for the structure of the manual attachment. Is it a
1672 * chain or a list? What happens if one surface is attached to 2
1673 * different surfaces? */
1674 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1675 Surf, Surf->next_attached, Surf->first_attached);
1677 wined3d_mutex_unlock();
1678 return DDERR_SURFACEALREADYATTACHED;
1681 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1682 Surf->next_attached = This->next_attached;
1683 Surf->first_attached = This->first_attached;
1684 This->next_attached = Surf;
1686 /* Check if the WineD3D depth stencil needs updating */
1687 if (This->ddraw->d3ddevice)
1688 d3d_device_update_depth_stencil(This->ddraw->d3ddevice);
1690 wined3d_mutex_unlock();
1692 return DD_OK;
1695 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
1697 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
1698 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1699 HRESULT hr;
1701 TRACE("iface %p, attachment %p.\n", iface, attachment);
1703 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1704 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1707 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1708 attachment_impl->surface_desc.ddsCaps.dwCaps);
1709 return DDERR_CANNOTATTACHSURFACE;
1712 hr = ddraw_surface_attach_surface(This, attachment_impl);
1713 if (FAILED(hr))
1715 return hr;
1717 attachment_impl->attached_iface = (IUnknown *)attachment;
1718 IUnknown_AddRef(attachment_impl->attached_iface);
1719 return hr;
1722 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1724 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1725 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1726 HRESULT hr;
1728 TRACE("iface %p, attachment %p.\n", iface, attachment);
1730 /* Tests suggest that
1731 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1732 * -> offscreen plain surfaces can be attached to primaries
1733 * -> primaries can be attached to offscreen plain surfaces
1734 * -> z buffers can be attached to primaries */
1735 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1736 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1738 /* Sizes have to match */
1739 if (attachment_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
1740 || attachment_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
1742 WARN("Surface sizes do not match.\n");
1743 return DDERR_CANNOTATTACHSURFACE;
1746 else if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
1747 || !(attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER)))
1749 WARN("Invalid attachment combination.\n");
1750 return DDERR_CANNOTATTACHSURFACE;
1753 if (FAILED(hr = ddraw_surface_attach_surface(surface, attachment_impl)))
1754 return hr;
1756 attachment_impl->attached_iface = (IUnknown *)attachment;
1757 IUnknown_AddRef(attachment_impl->attached_iface);
1758 return hr;
1761 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1763 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1764 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1765 HRESULT hr;
1767 TRACE("iface %p, attachment %p.\n", iface, attachment);
1769 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1770 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1771 return hr;
1773 attachment_impl->attached_iface = (IUnknown *)attachment;
1774 IUnknown_AddRef(attachment_impl->attached_iface);
1775 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1776 return hr;
1779 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1781 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1782 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1783 HRESULT hr;
1785 TRACE("iface %p, attachment %p.\n", iface, attachment);
1787 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1788 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1789 return hr;
1791 attachment_impl->attached_iface = (IUnknown *)attachment;
1792 IUnknown_AddRef(attachment_impl->attached_iface);
1793 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1794 return hr;
1797 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1799 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1800 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1801 HRESULT hr;
1803 TRACE("iface %p, attachment %p.\n", iface, attachment);
1805 if (FAILED(hr = ddraw_surface4_AddAttachedSurface(&surface->IDirectDrawSurface4_iface,
1806 attachment_impl ? &attachment_impl->IDirectDrawSurface4_iface : NULL)))
1807 return hr;
1809 attachment_impl->attached_iface = (IUnknown *)attachment;
1810 IUnknown_AddRef(attachment_impl->attached_iface);
1811 ddraw_surface4_Release(&attachment_impl->IDirectDrawSurface4_iface);
1812 return hr;
1815 /*****************************************************************************
1816 * IDirectDrawSurface7::DeleteAttachedSurface
1818 * Removes a surface from the attachment chain. The surface's refcount
1819 * is decreased by one after it has been removed
1821 * Params:
1822 * Flags: Some flags, not used by this implementation
1823 * Attach: Surface to detach
1825 * Returns:
1826 * DD_OK on success
1827 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1829 *****************************************************************************/
1830 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
1831 struct ddraw_surface *attachment, IUnknown *detach_iface)
1833 struct ddraw_surface *prev = surface;
1835 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
1837 wined3d_mutex_lock();
1838 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
1840 wined3d_mutex_unlock();
1841 return DDERR_CANNOTDETACHSURFACE;
1844 if (attachment->attached_iface != detach_iface)
1846 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
1847 wined3d_mutex_unlock();
1848 return DDERR_SURFACENOTATTACHED;
1851 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1852 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1854 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1855 /* FIXME: we should probably also subtract from dwMipMapCount of this
1856 * and all parent surfaces */
1859 /* Find the predecessor of the detached surface */
1860 while (prev)
1862 if (prev->next_attached == attachment)
1863 break;
1864 prev = prev->next_attached;
1867 /* There must be a surface, otherwise there's a bug */
1868 assert(prev);
1870 /* Unchain the surface */
1871 prev->next_attached = attachment->next_attached;
1872 attachment->next_attached = NULL;
1873 attachment->first_attached = attachment;
1875 /* Check if the wined3d depth stencil needs updating. */
1876 if (surface->ddraw->d3ddevice)
1877 d3d_device_update_depth_stencil(surface->ddraw->d3ddevice);
1878 wined3d_mutex_unlock();
1880 /* Set attached_iface to NULL before releasing it, the surface may go
1881 * away. */
1882 attachment->attached_iface = NULL;
1883 IUnknown_Release(detach_iface);
1885 return DD_OK;
1888 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1889 DWORD flags, IDirectDrawSurface7 *attachment)
1891 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1892 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1894 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1896 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1899 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1900 DWORD flags, IDirectDrawSurface4 *attachment)
1902 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1903 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1905 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1907 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1910 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1911 DWORD flags, IDirectDrawSurface3 *attachment)
1913 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1914 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1916 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1918 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1921 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
1922 DWORD flags, IDirectDrawSurface2 *attachment)
1924 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1925 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1927 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1929 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1932 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
1933 DWORD flags, IDirectDrawSurface *attachment)
1935 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1936 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1938 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1940 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1943 /*****************************************************************************
1944 * IDirectDrawSurface7::AddOverlayDirtyRect
1946 * "This method is not currently implemented"
1948 * Params:
1949 * Rect: ?
1951 * Returns:
1952 * DDERR_UNSUPPORTED
1954 *****************************************************************************/
1955 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1957 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1959 return DDERR_UNSUPPORTED; /* unchecked */
1962 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
1964 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1966 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1968 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1971 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1973 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1975 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1977 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1980 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
1982 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1984 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1986 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1989 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
1991 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1993 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1995 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1998 /*****************************************************************************
1999 * IDirectDrawSurface7::GetDC
2001 * Returns a GDI device context for the surface
2003 * Params:
2004 * hdc: Address of a HDC variable to store the dc to
2006 * Returns:
2007 * DD_OK on success
2008 * DDERR_INVALIDPARAMS if hdc is NULL
2009 * For details, see IWineD3DSurface::GetDC
2011 *****************************************************************************/
2012 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
2014 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2015 HRESULT hr = DD_OK;
2017 TRACE("iface %p, dc %p.\n", iface, hdc);
2019 if(!hdc)
2020 return DDERR_INVALIDPARAMS;
2022 wined3d_mutex_lock();
2023 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2024 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE);
2025 if (SUCCEEDED(hr))
2026 hr = wined3d_surface_getdc(surface->wined3d_surface, hdc);
2028 if (SUCCEEDED(hr) && format_is_paletteindexed(&surface->surface_desc.u4.ddpfPixelFormat))
2030 const struct ddraw_palette *palette;
2032 if (surface->palette)
2033 palette = surface->palette;
2034 else if (surface->ddraw->primary)
2035 palette = surface->ddraw->primary->palette;
2036 else
2037 palette = NULL;
2039 if (palette)
2040 wined3d_palette_apply_to_dc(palette->wineD3DPalette, *hdc);
2043 wined3d_mutex_unlock();
2044 switch(hr)
2046 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
2047 * touch *hdc
2049 case WINED3DERR_INVALIDCALL:
2050 if(hdc) *hdc = NULL;
2051 return DDERR_INVALIDPARAMS;
2053 default: return hr;
2057 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
2059 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2061 TRACE("iface %p, dc %p.\n", iface, dc);
2063 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2066 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
2068 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2070 TRACE("iface %p, dc %p.\n", iface, dc);
2072 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2075 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2077 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2079 TRACE("iface %p, dc %p.\n", iface, dc);
2081 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2084 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2086 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2088 TRACE("iface %p, dc %p.\n", iface, dc);
2090 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2093 /*****************************************************************************
2094 * IDirectDrawSurface7::ReleaseDC
2096 * Releases the DC that was constructed with GetDC
2098 * Params:
2099 * hdc: HDC to release
2101 * Returns:
2102 * DD_OK on success
2103 * For more details, see IWineD3DSurface::ReleaseDC
2105 *****************************************************************************/
2106 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2108 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2109 HRESULT hr;
2111 TRACE("iface %p, dc %p.\n", iface, hdc);
2113 wined3d_mutex_lock();
2114 hr = wined3d_surface_releasedc(surface->wined3d_surface, hdc);
2115 if (SUCCEEDED(hr) && (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2116 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
2117 wined3d_mutex_unlock();
2119 return hr;
2122 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2124 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2126 TRACE("iface %p, dc %p.\n", iface, dc);
2128 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2131 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2133 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2135 TRACE("iface %p, dc %p.\n", iface, dc);
2137 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2140 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2142 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2144 TRACE("iface %p, dc %p.\n", iface, dc);
2146 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2149 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2151 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2153 TRACE("iface %p, dc %p.\n", iface, dc);
2155 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2158 /*****************************************************************************
2159 * IDirectDrawSurface7::GetCaps
2161 * Returns the surface's caps
2163 * Params:
2164 * Caps: Address to write the caps to
2166 * Returns:
2167 * DD_OK on success
2168 * DDERR_INVALIDPARAMS if Caps is NULL
2170 *****************************************************************************/
2171 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2173 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2175 TRACE("iface %p, caps %p.\n", iface, Caps);
2177 if(!Caps)
2178 return DDERR_INVALIDPARAMS;
2180 *Caps = surface->surface_desc.ddsCaps;
2182 return DD_OK;
2185 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2187 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2189 TRACE("iface %p, caps %p.\n", iface, caps);
2191 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2194 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2196 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2197 DDSCAPS2 caps2;
2198 HRESULT hr;
2200 TRACE("iface %p, caps %p.\n", iface, caps);
2202 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2203 if (FAILED(hr)) return hr;
2205 caps->dwCaps = caps2.dwCaps;
2206 return hr;
2209 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2211 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2212 DDSCAPS2 caps2;
2213 HRESULT hr;
2215 TRACE("iface %p, caps %p.\n", iface, caps);
2217 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2218 if (FAILED(hr)) return hr;
2220 caps->dwCaps = caps2.dwCaps;
2221 return hr;
2224 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2226 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2227 DDSCAPS2 caps2;
2228 HRESULT hr;
2230 TRACE("iface %p, caps %p.\n", iface, caps);
2232 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2233 if (FAILED(hr)) return hr;
2235 caps->dwCaps = caps2.dwCaps;
2236 return hr;
2239 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD priority)
2241 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2242 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2243 HRESULT hr;
2244 struct wined3d_resource *resource;
2246 TRACE("iface %p, priority %u.\n", iface, priority);
2248 wined3d_mutex_lock();
2249 /* No need to check for offscreen plain surfaces or mipmap sublevels. SetPriority
2250 * calls on such surfaces segfault on Windows. */
2251 if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed))
2253 WARN("Called on non-managed texture returning DDERR_INVALIDPARAMS.\n");
2254 hr = DDERR_INVALIDPARAMS;
2256 else
2258 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2259 wined3d_resource_set_priority(resource, priority);
2260 hr = DD_OK;
2262 wined3d_mutex_unlock();
2264 return hr;
2267 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *priority)
2269 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2270 const struct wined3d_resource *resource;
2271 DWORD managed = DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE;
2272 HRESULT hr;
2274 TRACE("iface %p, priority %p.\n", iface, priority);
2276 wined3d_mutex_lock();
2277 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN)
2279 WARN("Called on offscreenplain surface, returning DDERR_INVALIDOBJECT.\n");
2280 hr = DDERR_INVALIDOBJECT;
2282 else if (!(surface->surface_desc.ddsCaps.dwCaps2 & managed) || !surface->wined3d_texture)
2284 WARN("Called on non-managed texture or mipmap sublevel, returning DDERR_INVALIDPARAMS.\n");
2285 hr = DDERR_INVALIDPARAMS;
2287 else
2289 resource = wined3d_texture_get_resource(surface->wined3d_texture);
2290 *priority = wined3d_resource_get_priority(resource);
2291 hr = DD_OK;
2293 wined3d_mutex_unlock();
2295 return hr;
2298 /*****************************************************************************
2299 * IDirectDrawSurface7::SetPrivateData
2301 * Stores some data in the surface that is intended for the application's
2302 * use.
2304 * Params:
2305 * tag: GUID that identifies the data
2306 * Data: Pointer to the private data
2307 * Size: Size of the private data
2308 * Flags: Some flags
2310 * Returns:
2311 * D3D_OK on success
2312 * For more details, see IWineD3DSurface::SetPrivateData
2314 *****************************************************************************/
2315 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2316 REFGUID tag, void *data, DWORD size, DWORD flags)
2318 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2319 HRESULT hr;
2321 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2322 iface, debugstr_guid(tag), data, size, flags);
2324 if (!data)
2326 WARN("data is NULL, returning DDERR_INVALIDPARAMS.\n");
2327 return DDERR_INVALIDPARAMS;
2330 wined3d_mutex_lock();
2331 hr = wined3d_private_store_set_private_data(&surface->private_store, tag, data, size, flags);
2332 wined3d_mutex_unlock();
2333 return hr_ddraw_from_wined3d(hr);
2336 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2337 REFGUID tag, void *data, DWORD size, DWORD flags)
2339 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2341 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2342 iface, debugstr_guid(tag), data, size, flags);
2344 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2347 /*****************************************************************************
2348 * IDirectDrawSurface7::GetPrivateData
2350 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2352 * Params:
2353 * tag: GUID of the data to return
2354 * Data: Address where to write the data to
2355 * Size: Size of the buffer at Data
2357 * Returns:
2358 * DD_OK on success
2359 * DDERR_INVALIDPARAMS if Data is NULL
2360 * For more details, see IWineD3DSurface::GetPrivateData
2362 *****************************************************************************/
2363 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *data, DWORD *size)
2365 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2366 const struct wined3d_private_data *stored_data;
2367 HRESULT hr;
2369 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2370 iface, debugstr_guid(tag), data, size);
2372 wined3d_mutex_lock();
2373 stored_data = wined3d_private_store_get_private_data(&surface->private_store, tag);
2374 if (!stored_data)
2376 hr = DDERR_NOTFOUND;
2377 goto done;
2379 if (!size)
2381 hr = DDERR_INVALIDPARAMS;
2382 goto done;
2384 if (*size < stored_data->size)
2386 *size = stored_data->size;
2387 hr = DDERR_MOREDATA;
2388 goto done;
2390 if (!data)
2392 hr = DDERR_INVALIDPARAMS;
2393 goto done;
2396 *size = stored_data->size;
2397 memcpy(data, stored_data->content.data, stored_data->size);
2398 hr = DD_OK;
2400 done:
2401 wined3d_mutex_unlock();
2402 return hr;
2405 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2407 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2409 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2410 iface, debugstr_guid(tag), data, size);
2412 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2415 /*****************************************************************************
2416 * IDirectDrawSurface7::FreePrivateData
2418 * Frees private data stored in the surface
2420 * Params:
2421 * tag: Tag of the data to free
2423 * Returns:
2424 * D3D_OK on success
2425 * For more details, see IWineD3DSurface::FreePrivateData
2427 *****************************************************************************/
2428 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2430 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2431 struct wined3d_private_data *entry;
2433 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2435 wined3d_mutex_lock();
2436 entry = wined3d_private_store_get_private_data(&surface->private_store, tag);
2437 if (!entry)
2439 wined3d_mutex_unlock();
2440 return DDERR_NOTFOUND;
2443 wined3d_private_store_free_private_data(&surface->private_store, entry);
2444 wined3d_mutex_unlock();
2446 return DD_OK;
2449 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2451 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2453 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2455 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2458 /*****************************************************************************
2459 * IDirectDrawSurface7::PageLock
2461 * Prevents a sysmem surface from being paged out
2463 * Params:
2464 * Flags: Not used, must be 0(unchecked)
2466 * Returns:
2467 * DD_OK, because it's a stub
2469 *****************************************************************************/
2470 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2472 TRACE("iface %p, flags %#x.\n", iface, Flags);
2474 /* This is Windows memory management related - we don't need this */
2475 return DD_OK;
2478 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2480 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2482 TRACE("iface %p, flags %#x.\n", iface, flags);
2484 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2487 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2489 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2491 TRACE("iface %p, flags %#x.\n", iface, flags);
2493 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2496 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2498 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2500 TRACE("iface %p, flags %#x.\n", iface, flags);
2502 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2505 /*****************************************************************************
2506 * IDirectDrawSurface7::PageUnlock
2508 * Allows a sysmem surface to be paged out
2510 * Params:
2511 * Flags: Not used, must be 0(unchecked)
2513 * Returns:
2514 * DD_OK, because it's a stub
2516 *****************************************************************************/
2517 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2519 TRACE("iface %p, flags %#x.\n", iface, Flags);
2521 return DD_OK;
2524 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2526 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2528 TRACE("iface %p, flags %#x.\n", iface, flags);
2530 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2533 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2535 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2537 TRACE("iface %p, flags %#x.\n", iface, flags);
2539 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2542 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2544 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2546 TRACE("iface %p, flags %#x.\n", iface, flags);
2548 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2551 /*****************************************************************************
2552 * IDirectDrawSurface7::BltBatch
2554 * An unimplemented function
2556 * Params:
2559 * Returns:
2560 * DDERR_UNSUPPORTED
2562 *****************************************************************************/
2563 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2565 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2567 /* MSDN: "not currently implemented" */
2568 return DDERR_UNSUPPORTED;
2571 static HRESULT WINAPI ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2573 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2575 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2577 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2580 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2582 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2584 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2586 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2589 static HRESULT WINAPI ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2591 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2593 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2595 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2598 static HRESULT WINAPI ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2600 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2602 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2604 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2607 /*****************************************************************************
2608 * IDirectDrawSurface7::EnumAttachedSurfaces
2610 * Enumerates all surfaces attached to this surface
2612 * Params:
2613 * context: Pointer to pass unmodified to the callback
2614 * cb: Callback function to call for each surface
2616 * Returns:
2617 * DD_OK on success
2618 * DDERR_INVALIDPARAMS if cb is NULL
2620 *****************************************************************************/
2621 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2622 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2624 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2625 struct ddraw_surface *surf;
2626 DDSURFACEDESC2 desc;
2627 int i;
2629 /* Attached surfaces aren't handled in WineD3D */
2630 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2632 if(!cb)
2633 return DDERR_INVALIDPARAMS;
2635 wined3d_mutex_lock();
2637 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2639 surf = surface->complex_array[i];
2640 if(!surf) break;
2642 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2643 desc = surf->surface_desc;
2644 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2645 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2647 wined3d_mutex_unlock();
2648 return DD_OK;
2652 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2654 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2655 desc = surf->surface_desc;
2656 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2657 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2659 wined3d_mutex_unlock();
2660 return DD_OK;
2664 TRACE(" end of enumeration.\n");
2666 wined3d_mutex_unlock();
2668 return DD_OK;
2671 struct callback_info2
2673 LPDDENUMSURFACESCALLBACK2 callback;
2674 void *context;
2677 struct callback_info
2679 LPDDENUMSURFACESCALLBACK callback;
2680 void *context;
2683 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2685 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2686 const struct callback_info2 *info = context;
2688 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
2689 ddraw_surface7_Release(surface);
2691 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
2694 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2696 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2697 const struct callback_info *info = context;
2699 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
2700 ddraw_surface7_Release(surface);
2702 /* FIXME: Check surface_test.dwSize */
2703 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2704 (DDSURFACEDESC *)surface_desc, info->context);
2707 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2708 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2710 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2711 struct callback_info2 info;
2713 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2715 info.callback = callback;
2716 info.context = context;
2718 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2719 &info, EnumCallback2);
2722 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2723 void *context, LPDDENUMSURFACESCALLBACK callback)
2725 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2726 struct callback_info info;
2728 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2730 info.callback = callback;
2731 info.context = context;
2733 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2734 &info, EnumCallback);
2737 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2738 void *context, LPDDENUMSURFACESCALLBACK callback)
2740 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2741 struct callback_info info;
2743 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2745 info.callback = callback;
2746 info.context = context;
2748 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2749 &info, EnumCallback);
2752 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2753 void *context, LPDDENUMSURFACESCALLBACK callback)
2755 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2756 struct callback_info info;
2758 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2760 info.callback = callback;
2761 info.context = context;
2763 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2764 &info, EnumCallback);
2767 /*****************************************************************************
2768 * IDirectDrawSurface7::EnumOverlayZOrders
2770 * "Enumerates the overlay surfaces on the specified destination"
2772 * Params:
2773 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2774 * context: context to pass back to the callback
2775 * cb: callback function to call for each enumerated surface
2777 * Returns:
2778 * DD_OK, because it's a stub
2780 *****************************************************************************/
2781 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2782 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2784 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2786 return DD_OK;
2789 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2790 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2792 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2793 struct callback_info2 info;
2795 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2797 info.callback = callback;
2798 info.context = context;
2800 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2801 flags, &info, EnumCallback2);
2804 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2805 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2807 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2808 struct callback_info info;
2810 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2812 info.callback = callback;
2813 info.context = context;
2815 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2816 flags, &info, EnumCallback);
2819 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2820 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2822 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2823 struct callback_info info;
2825 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2827 info.callback = callback;
2828 info.context = context;
2830 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2831 flags, &info, EnumCallback);
2834 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2835 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2837 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2838 struct callback_info info;
2840 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2842 info.callback = callback;
2843 info.context = context;
2845 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2846 flags, &info, EnumCallback);
2849 /*****************************************************************************
2850 * IDirectDrawSurface7::GetBltStatus
2852 * Returns the blitting status
2854 * Params:
2855 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2857 * Returns:
2858 * See IWineD3DSurface::Blt
2860 *****************************************************************************/
2861 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2863 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2864 HRESULT hr;
2866 TRACE("iface %p, flags %#x.\n", iface, Flags);
2868 wined3d_mutex_lock();
2869 hr = wined3d_surface_get_blt_status(surface->wined3d_surface, Flags);
2870 wined3d_mutex_unlock();
2871 switch(hr)
2873 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2874 default: return hr;
2878 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2880 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2882 TRACE("iface %p, flags %#x.\n", iface, flags);
2884 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2887 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2889 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2891 TRACE("iface %p, flags %#x.\n", iface, flags);
2893 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2896 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2898 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2900 TRACE("iface %p, flags %#x.\n", iface, flags);
2902 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2905 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
2907 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2909 TRACE("iface %p, flags %#x.\n", iface, flags);
2911 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2914 /*****************************************************************************
2915 * IDirectDrawSurface7::GetColorKey
2917 * Returns the color key assigned to the surface
2919 * Params:
2920 * Flags: Some flags
2921 * CKey: Address to store the key to
2923 * Returns:
2924 * DD_OK on success
2925 * DDERR_INVALIDPARAMS if CKey is NULL
2927 *****************************************************************************/
2928 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2930 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
2932 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2934 if(!CKey)
2935 return DDERR_INVALIDPARAMS;
2937 wined3d_mutex_lock();
2939 switch (Flags)
2941 case DDCKEY_DESTBLT:
2942 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
2944 wined3d_mutex_unlock();
2945 return DDERR_NOCOLORKEY;
2947 *CKey = This->surface_desc.ddckCKDestBlt;
2948 break;
2950 case DDCKEY_DESTOVERLAY:
2951 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
2953 wined3d_mutex_unlock();
2954 return DDERR_NOCOLORKEY;
2956 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
2957 break;
2959 case DDCKEY_SRCBLT:
2960 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
2962 wined3d_mutex_unlock();
2963 return DDERR_NOCOLORKEY;
2965 *CKey = This->surface_desc.ddckCKSrcBlt;
2966 break;
2968 case DDCKEY_SRCOVERLAY:
2969 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
2971 wined3d_mutex_unlock();
2972 return DDERR_NOCOLORKEY;
2974 *CKey = This->surface_desc.ddckCKSrcOverlay;
2975 break;
2977 default:
2978 wined3d_mutex_unlock();
2979 return DDERR_INVALIDPARAMS;
2982 wined3d_mutex_unlock();
2984 return DD_OK;
2987 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
2989 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2991 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2993 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
2996 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2998 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3000 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3002 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3005 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
3007 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3009 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3011 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3014 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
3016 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3018 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
3020 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
3023 /*****************************************************************************
3024 * IDirectDrawSurface7::GetFlipStatus
3026 * Returns the flipping status of the surface
3028 * Params:
3029 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
3031 * Returns:
3032 * See IWineD3DSurface::GetFlipStatus
3034 *****************************************************************************/
3035 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
3037 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3038 HRESULT hr;
3040 TRACE("iface %p, flags %#x.\n", iface, Flags);
3042 wined3d_mutex_lock();
3043 hr = wined3d_surface_get_flip_status(surface->wined3d_surface, Flags);
3044 wined3d_mutex_unlock();
3046 switch(hr)
3048 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3049 default: return hr;
3053 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
3055 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3057 TRACE("iface %p, flags %#x.\n", iface, flags);
3059 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3062 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
3064 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3066 TRACE("iface %p, flags %#x.\n", iface, flags);
3068 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3071 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
3073 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3075 TRACE("iface %p, flags %#x.\n", iface, flags);
3077 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3080 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
3082 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3084 TRACE("iface %p, flags %#x.\n", iface, flags);
3086 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
3089 /*****************************************************************************
3090 * IDirectDrawSurface7::GetOverlayPosition
3092 * Returns the display coordinates of a visible and active overlay surface
3094 * Params:
3098 * Returns:
3099 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3100 *****************************************************************************/
3101 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
3103 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3104 HRESULT hr;
3106 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
3108 wined3d_mutex_lock();
3109 hr = wined3d_surface_get_overlay_position(surface->wined3d_surface, X, Y);
3110 wined3d_mutex_unlock();
3112 return hr;
3115 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3117 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3119 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3121 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3124 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3126 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3128 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3130 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3133 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3135 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3137 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3139 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3142 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3144 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3146 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3148 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3151 /*****************************************************************************
3152 * IDirectDrawSurface7::GetPixelFormat
3154 * Returns the pixel format of the Surface
3156 * Params:
3157 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3158 * format should be written
3160 * Returns:
3161 * DD_OK on success
3162 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3164 *****************************************************************************/
3165 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3167 /* What is DDERR_INVALIDSURFACETYPE for here? */
3168 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3170 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3172 if(!PixelFormat)
3173 return DDERR_INVALIDPARAMS;
3175 wined3d_mutex_lock();
3176 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3177 wined3d_mutex_unlock();
3179 return DD_OK;
3182 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3184 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3186 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3188 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3191 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3193 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3195 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3197 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3200 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3202 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3204 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3206 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3209 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3211 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3213 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3215 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3218 /*****************************************************************************
3219 * IDirectDrawSurface7::GetSurfaceDesc
3221 * Returns the description of this surface
3223 * Params:
3224 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3225 * surface desc
3227 * Returns:
3228 * DD_OK on success
3229 * DDERR_INVALIDPARAMS if DDSD is NULL
3231 *****************************************************************************/
3232 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3234 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3236 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3238 if(!DDSD)
3239 return DDERR_INVALIDPARAMS;
3241 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3243 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3244 return DDERR_INVALIDPARAMS;
3247 wined3d_mutex_lock();
3248 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3249 TRACE("Returning surface desc:\n");
3250 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3251 wined3d_mutex_unlock();
3253 return DD_OK;
3256 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3258 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3260 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3262 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3265 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3267 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3269 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3271 if (!surface_desc) return DDERR_INVALIDPARAMS;
3273 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3275 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3276 return DDERR_INVALIDPARAMS;
3279 wined3d_mutex_lock();
3280 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3281 TRACE("Returning surface desc:\n");
3282 if (TRACE_ON(ddraw))
3284 /* DDRAW_dump_surface_desc handles the smaller size */
3285 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3287 wined3d_mutex_unlock();
3289 return DD_OK;
3292 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3294 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3296 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3298 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3301 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3303 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3305 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3307 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3310 /*****************************************************************************
3311 * IDirectDrawSurface7::Initialize
3313 * Initializes the surface. This is a no-op in Wine
3315 * Params:
3316 * DD: Pointer to an DirectDraw interface
3317 * DDSD: Surface description for initialization
3319 * Returns:
3320 * DDERR_ALREADYINITIALIZED
3322 *****************************************************************************/
3323 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3324 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3326 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3328 return DDERR_ALREADYINITIALIZED;
3331 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3332 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3334 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3336 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3338 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3339 ddraw, surface_desc);
3342 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3343 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3345 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3346 DDSURFACEDESC2 surface_desc2;
3348 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3350 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3351 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3352 ddraw, surface_desc ? &surface_desc2 : NULL);
3355 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3356 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3358 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3359 DDSURFACEDESC2 surface_desc2;
3361 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3363 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3364 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3365 ddraw, surface_desc ? &surface_desc2 : NULL);
3368 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3369 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3371 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3372 DDSURFACEDESC2 surface_desc2;
3374 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3376 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3377 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3378 ddraw, surface_desc ? &surface_desc2 : NULL);
3381 /*****************************************************************************
3382 * IDirect3DTexture1::Initialize
3384 * The sdk says it's not implemented
3386 * Params:
3389 * Returns
3390 * DDERR_UNSUPPORTED
3392 *****************************************************************************/
3393 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3394 IDirect3DDevice *device, IDirectDrawSurface *surface)
3396 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3398 return DDERR_UNSUPPORTED; /* Unchecked */
3401 /*****************************************************************************
3402 * IDirectDrawSurface7::IsLost
3404 * Checks if the surface is lost
3406 * Returns:
3407 * DD_OK, if the surface is usable
3408 * DDERR_ISLOST if the surface is lost
3409 * See IWineD3DSurface::IsLost for more details
3411 *****************************************************************************/
3412 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3414 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3415 HRESULT hr;
3417 TRACE("iface %p.\n", iface);
3419 wined3d_mutex_lock();
3420 hr = wined3d_surface_is_lost(surface->wined3d_surface);
3421 wined3d_mutex_unlock();
3423 switch(hr)
3425 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
3426 * WineD3D uses the same error for surfaces
3428 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
3429 default: return hr;
3433 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3435 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3437 TRACE("iface %p.\n", iface);
3439 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3442 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3444 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3446 TRACE("iface %p.\n", iface);
3448 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3451 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3453 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3455 TRACE("iface %p.\n", iface);
3457 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3460 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3462 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3464 TRACE("iface %p.\n", iface);
3466 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3469 /*****************************************************************************
3470 * IDirectDrawSurface7::Restore
3472 * Restores a lost surface. This makes the surface usable again, but
3473 * doesn't reload its old contents
3475 * Returns:
3476 * DD_OK on success
3477 * See IWineD3DSurface::Restore for more details
3479 *****************************************************************************/
3480 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3482 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3483 HRESULT hr;
3485 TRACE("iface %p.\n", iface);
3487 wined3d_mutex_lock();
3488 hr = wined3d_surface_restore(surface->wined3d_surface);
3489 wined3d_mutex_unlock();
3491 return hr;
3494 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3496 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3498 TRACE("iface %p.\n", iface);
3500 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3503 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3505 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3507 TRACE("iface %p.\n", iface);
3509 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3512 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3514 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3516 TRACE("iface %p.\n", iface);
3518 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3521 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3523 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3525 TRACE("iface %p.\n", iface);
3527 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3530 /*****************************************************************************
3531 * IDirectDrawSurface7::SetOverlayPosition
3533 * Changes the display coordinates of an overlay surface
3535 * Params:
3536 * X:
3537 * Y:
3539 * Returns:
3540 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3541 *****************************************************************************/
3542 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
3544 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3545 HRESULT hr;
3547 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
3549 wined3d_mutex_lock();
3550 hr = wined3d_surface_set_overlay_position(surface->wined3d_surface, X, Y);
3551 wined3d_mutex_unlock();
3553 return hr;
3556 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3558 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3560 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3562 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3565 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3567 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3569 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3571 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3574 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3576 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3578 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3580 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3583 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3585 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3587 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3589 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3592 /*****************************************************************************
3593 * IDirectDrawSurface7::UpdateOverlay
3595 * Modifies the attributes of an overlay surface.
3597 * Params:
3598 * SrcRect: The section of the source being used for the overlay
3599 * DstSurface: Address of the surface that is overlaid
3600 * DstRect: Place of the overlay
3601 * Flags: some DDOVER_* flags
3603 * Returns:
3604 * DDERR_UNSUPPORTED, because we don't support overlays
3606 *****************************************************************************/
3607 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
3608 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
3610 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3611 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(DstSurface);
3612 HRESULT hr;
3614 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3615 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
3617 wined3d_mutex_lock();
3618 hr = wined3d_surface_update_overlay(src_impl->wined3d_surface, SrcRect,
3619 dst_impl ? dst_impl->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
3620 wined3d_mutex_unlock();
3622 switch(hr) {
3623 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3624 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3625 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3626 default:
3627 return hr;
3631 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3632 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3634 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3635 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3637 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3638 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3640 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3641 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3644 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3645 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3647 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
3648 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3650 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3651 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3653 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3654 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3657 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3658 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3660 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
3661 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3663 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3664 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3666 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3667 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3670 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3671 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3673 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
3674 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3676 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3677 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3679 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3680 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3683 /*****************************************************************************
3684 * IDirectDrawSurface7::UpdateOverlayDisplay
3686 * The DX7 sdk says that it's not implemented
3688 * Params:
3689 * Flags: ?
3691 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3693 *****************************************************************************/
3694 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3696 TRACE("iface %p, flags %#x.\n", iface, Flags);
3698 return DDERR_UNSUPPORTED;
3701 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3703 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3705 TRACE("iface %p, flags %#x.\n", iface, flags);
3707 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3710 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3712 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3714 TRACE("iface %p, flags %#x.\n", iface, flags);
3716 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3719 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3721 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3723 TRACE("iface %p, flags %#x.\n", iface, flags);
3725 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3728 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3730 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3732 TRACE("iface %p, flags %#x.\n", iface, flags);
3734 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3737 /*****************************************************************************
3738 * IDirectDrawSurface7::UpdateOverlayZOrder
3740 * Sets an overlay's Z order
3742 * Params:
3743 * Flags: DDOVERZ_* flags
3744 * DDSRef: Defines the relative position in the overlay chain
3746 * Returns:
3747 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3749 *****************************************************************************/
3750 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3751 DWORD Flags, IDirectDrawSurface7 *DDSRef)
3753 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3754 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface7(DDSRef);
3755 HRESULT hr;
3757 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
3759 wined3d_mutex_lock();
3760 hr = wined3d_surface_update_overlay_z_order(surface->wined3d_surface,
3761 Flags, reference_impl ? reference_impl->wined3d_surface : NULL);
3762 wined3d_mutex_unlock();
3764 return hr;
3767 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3768 DWORD flags, IDirectDrawSurface4 *reference)
3770 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3771 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3773 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3775 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3776 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3779 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3780 DWORD flags, IDirectDrawSurface3 *reference)
3782 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3783 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3785 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3787 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3788 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3791 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3792 DWORD flags, IDirectDrawSurface2 *reference)
3794 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3795 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3797 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3799 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3800 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3803 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3804 DWORD flags, IDirectDrawSurface *reference)
3806 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3807 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3809 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3811 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3812 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3815 /*****************************************************************************
3816 * IDirectDrawSurface7::GetDDInterface
3818 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3819 * surface belongs to
3821 * Params:
3822 * DD: Address to write the interface pointer to
3824 * Returns:
3825 * DD_OK on success
3826 * DDERR_INVALIDPARAMS if DD is NULL
3828 *****************************************************************************/
3829 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3831 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3833 TRACE("iface %p, ddraw %p.\n", iface, DD);
3835 if(!DD)
3836 return DDERR_INVALIDPARAMS;
3838 switch(This->version)
3840 case 7:
3841 *DD = &This->ddraw->IDirectDraw7_iface;
3842 break;
3844 case 4:
3845 *DD = &This->ddraw->IDirectDraw4_iface;
3846 break;
3848 case 2:
3849 *DD = &This->ddraw->IDirectDraw2_iface;
3850 break;
3852 case 1:
3853 *DD = &This->ddraw->IDirectDraw_iface;
3854 break;
3857 IUnknown_AddRef((IUnknown *)*DD);
3859 return DD_OK;
3862 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3864 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3866 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3868 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3871 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3873 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3875 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3877 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3880 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3882 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3884 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3886 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3889 /* This seems also windows implementation specific - I don't think WineD3D needs this */
3890 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3892 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3893 volatile struct ddraw_surface* vThis = This;
3895 TRACE("iface %p.\n", iface);
3897 wined3d_mutex_lock();
3898 /* A uniqueness value of 0 is apparently special.
3899 * This needs to be checked.
3900 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
3902 while (1) {
3903 DWORD old_uniqueness_value = vThis->uniqueness_value;
3904 DWORD new_uniqueness_value = old_uniqueness_value+1;
3906 if (old_uniqueness_value == 0) break;
3907 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
3909 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
3910 old_uniqueness_value,
3911 new_uniqueness_value)
3912 == old_uniqueness_value)
3913 break;
3916 wined3d_mutex_unlock();
3918 return DD_OK;
3921 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
3923 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3925 TRACE("iface %p.\n", iface);
3927 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
3930 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
3932 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3934 TRACE("iface %p, value %p.\n", iface, pValue);
3936 wined3d_mutex_lock();
3937 *pValue = surface->uniqueness_value;
3938 wined3d_mutex_unlock();
3940 return DD_OK;
3943 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
3945 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3947 TRACE("iface %p, value %p.\n", iface, pValue);
3949 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
3952 /*****************************************************************************
3953 * IDirectDrawSurface7::SetLOD
3955 * Sets the level of detail of a texture
3957 * Params:
3958 * MaxLOD: LOD to set
3960 * Returns:
3961 * DD_OK on success
3962 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3964 *****************************************************************************/
3965 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
3967 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3968 HRESULT hr;
3970 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
3972 wined3d_mutex_lock();
3973 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3975 wined3d_mutex_unlock();
3976 return DDERR_INVALIDOBJECT;
3979 if (!surface->wined3d_texture)
3981 ERR("The ddraw surface has no wined3d texture.\n");
3982 wined3d_mutex_unlock();
3983 return DDERR_INVALIDOBJECT;
3986 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
3987 wined3d_mutex_unlock();
3989 return hr;
3992 /*****************************************************************************
3993 * IDirectDrawSurface7::GetLOD
3995 * Returns the level of detail of a Direct3D texture
3997 * Params:
3998 * MaxLOD: Address to write the LOD to
4000 * Returns:
4001 * DD_OK on success
4002 * DDERR_INVALIDPARAMS if MaxLOD is NULL
4003 * DDERR_INVALIDOBJECT if the surface is invalid for this method
4005 *****************************************************************************/
4006 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
4008 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4010 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
4012 if(!MaxLOD)
4013 return DDERR_INVALIDPARAMS;
4015 wined3d_mutex_lock();
4016 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
4018 wined3d_mutex_unlock();
4019 return DDERR_INVALIDOBJECT;
4022 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
4023 wined3d_mutex_unlock();
4025 return DD_OK;
4028 /*****************************************************************************
4029 * IDirectDrawSurface7::BltFast
4031 * Performs a fast Blit.
4033 * Params:
4034 * dstx: The x coordinate to blit to on the destination
4035 * dsty: The y coordinate to blit to on the destination
4036 * Source: The source surface
4037 * rsrc: The source rectangle
4038 * trans: Type of transfer. Some DDBLTFAST_* flags
4040 * Returns:
4041 * DD_OK on success
4042 * For more details, see IWineD3DSurface::BltFast
4044 *****************************************************************************/
4045 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
4046 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
4048 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4049 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface7(Source);
4050 DWORD src_w, src_h, dst_w, dst_h;
4051 HRESULT hr = DD_OK;
4052 DWORD flags = 0;
4053 RECT dst_rect;
4055 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4056 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
4058 dst_w = This->surface_desc.dwWidth;
4059 dst_h = This->surface_desc.dwHeight;
4061 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
4062 * in that case
4064 if(rsrc)
4066 src_w = rsrc->right - rsrc->left;
4067 src_h = rsrc->bottom - rsrc->top;
4069 else
4071 src_w = src->surface_desc.dwWidth;
4072 src_h = src->surface_desc.dwHeight;
4075 if (src_w > dst_w || dstx > dst_w - src_w
4076 || src_h > dst_h || dsty > dst_h - src_h)
4078 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
4079 return DDERR_INVALIDRECT;
4082 SetRect(&dst_rect, dstx, dsty, dstx + src_w, dsty + src_h);
4083 if (trans & DDBLTFAST_SRCCOLORKEY)
4084 flags |= WINEDDBLT_KEYSRC;
4085 if (trans & DDBLTFAST_DESTCOLORKEY)
4086 flags |= WINEDDBLT_KEYDEST;
4087 if (trans & DDBLTFAST_WAIT)
4088 flags |= WINEDDBLT_WAIT;
4089 if (trans & DDBLTFAST_DONOTWAIT)
4090 flags |= WINEDDBLT_DONOTWAIT;
4092 wined3d_mutex_lock();
4093 if (This->clipper)
4095 wined3d_mutex_unlock();
4096 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
4097 return DDERR_BLTFASTCANTCLIP;
4100 if (src->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4101 hr = ddraw_surface_update_frontbuffer(src, rsrc, TRUE);
4102 if (SUCCEEDED(hr))
4103 hr = wined3d_surface_blt(This->wined3d_surface, &dst_rect,
4104 src->wined3d_surface, rsrc, flags, NULL, WINED3D_TEXF_POINT);
4105 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
4106 hr = ddraw_surface_update_frontbuffer(This, &dst_rect, FALSE);
4107 wined3d_mutex_unlock();
4109 switch(hr)
4111 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4112 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
4113 default: return hr;
4117 static HRESULT WINAPI ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4118 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4120 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4121 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4123 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4124 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4126 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4127 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4130 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4131 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4133 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4134 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4136 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4137 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4139 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4140 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4143 static HRESULT WINAPI ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4144 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4146 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4147 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4149 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4150 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4152 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4153 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4156 static HRESULT WINAPI ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4157 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4159 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4160 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4162 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4163 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4165 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4166 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4169 /*****************************************************************************
4170 * IDirectDrawSurface7::GetClipper
4172 * Returns the IDirectDrawClipper interface of the clipper assigned to this
4173 * surface
4175 * Params:
4176 * Clipper: Address to store the interface pointer at
4178 * Returns:
4179 * DD_OK on success
4180 * DDERR_INVALIDPARAMS if Clipper is NULL
4181 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
4183 *****************************************************************************/
4184 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
4186 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4188 TRACE("iface %p, clipper %p.\n", iface, Clipper);
4190 if (!Clipper)
4191 return DDERR_INVALIDPARAMS;
4193 wined3d_mutex_lock();
4194 if (!surface->clipper)
4196 wined3d_mutex_unlock();
4197 return DDERR_NOCLIPPERATTACHED;
4200 *Clipper = (IDirectDrawClipper *)surface->clipper;
4201 IDirectDrawClipper_AddRef(*Clipper);
4202 wined3d_mutex_unlock();
4204 return DD_OK;
4207 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4209 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4211 TRACE("iface %p, clipper %p.\n", iface, clipper);
4213 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4216 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4218 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4220 TRACE("iface %p, clipper %p.\n", iface, clipper);
4222 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4225 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4227 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4229 TRACE("iface %p, clipper %p.\n", iface, clipper);
4231 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4234 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4236 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4238 TRACE("iface %p, clipper %p.\n", iface, clipper);
4240 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4243 /*****************************************************************************
4244 * IDirectDrawSurface7::SetClipper
4246 * Sets a clipper for the surface
4248 * Params:
4249 * Clipper: IDirectDrawClipper interface of the clipper to set
4251 * Returns:
4252 * DD_OK on success
4254 *****************************************************************************/
4255 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4256 IDirectDrawClipper *iclipper)
4258 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4259 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4260 struct ddraw_clipper *old_clipper = This->clipper;
4261 HWND clipWindow;
4263 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4265 wined3d_mutex_lock();
4266 if (clipper == This->clipper)
4268 wined3d_mutex_unlock();
4269 return DD_OK;
4272 This->clipper = clipper;
4274 if (clipper != NULL)
4275 IDirectDrawClipper_AddRef(iclipper);
4276 if (old_clipper)
4277 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4279 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4281 clipWindow = NULL;
4282 if(clipper) {
4283 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4286 if (clipWindow)
4288 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4289 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4291 else
4293 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4294 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4298 wined3d_mutex_unlock();
4300 return DD_OK;
4303 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4305 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4307 TRACE("iface %p, clipper %p.\n", iface, clipper);
4309 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4312 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4314 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4316 TRACE("iface %p, clipper %p.\n", iface, clipper);
4318 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4321 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4323 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4325 TRACE("iface %p, clipper %p.\n", iface, clipper);
4327 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4330 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4332 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4334 TRACE("iface %p, clipper %p.\n", iface, clipper);
4336 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4339 /*****************************************************************************
4340 * IDirectDrawSurface7::SetSurfaceDesc
4342 * Sets the surface description. It can override the pixel format, the surface
4343 * memory, ...
4344 * It's not really tested.
4346 * Params:
4347 * DDSD: Pointer to the new surface description to set
4348 * Flags: Some flags
4350 * Returns:
4351 * DD_OK on success
4352 * DDERR_INVALIDPARAMS if DDSD is NULL
4354 *****************************************************************************/
4355 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4357 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4358 HRESULT hr;
4359 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4360 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4361 enum wined3d_format_id format_id;
4362 UINT pitch, width, height;
4364 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4366 if (!DDSD)
4368 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4369 return DDERR_INVALIDPARAMS;
4371 if (Flags)
4373 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4374 return DDERR_INVALIDPARAMS;
4376 if (!(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
4377 || This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE
4378 || This->surface_desc.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
4380 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4381 return DDERR_INVALIDSURFACETYPE;
4384 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4385 * for PIXELFORMAT to work */
4386 if (DDSD->dwFlags & ~allowed_flags)
4388 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4389 return DDERR_INVALIDPARAMS;
4391 if (!(DDSD->dwFlags & DDSD_LPSURFACE) || !DDSD->lpSurface)
4393 WARN("DDSD_LPSURFACE is not set or lpSurface is NULL, returning DDERR_INVALIDPARAMS\n");
4394 return DDERR_INVALIDPARAMS;
4396 if ((DDSD->dwFlags & DDSD_CAPS) && DDSD->ddsCaps.dwCaps)
4398 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4399 return DDERR_INVALIDCAPS;
4401 if (DDSD->dwFlags & DDSD_WIDTH)
4403 if (!(DDSD->dwFlags & DDSD_PITCH))
4405 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4406 return DDERR_INVALIDPARAMS;
4408 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4410 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4411 DDSD->u1.lPitch, DDSD->dwWidth);
4412 return DDERR_INVALIDPARAMS;
4414 if (DDSD->dwWidth != This->surface_desc.dwWidth)
4415 TRACE("Surface width changed from %u to %u.\n", This->surface_desc.dwWidth, DDSD->dwWidth);
4416 if (DDSD->u1.lPitch != This->surface_desc.u1.lPitch)
4417 TRACE("Surface pitch changed from %u to %u.\n", This->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4418 pitch = DDSD->u1.lPitch;
4419 width = DDSD->dwWidth;
4421 else if (DDSD->dwFlags & DDSD_PITCH)
4423 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4424 return DDERR_INVALIDPARAMS;
4426 else
4428 pitch = This->surface_desc.u1.lPitch;
4429 width = This->surface_desc.dwWidth;
4432 if (DDSD->dwFlags & DDSD_HEIGHT)
4434 if (!DDSD->dwHeight)
4436 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4437 return DDERR_INVALIDPARAMS;
4439 if (DDSD->dwHeight != This->surface_desc.dwHeight)
4440 TRACE("Surface height changed from %u to %u.\n", This->surface_desc.dwHeight, DDSD->dwHeight);
4441 height = DDSD->dwHeight;
4443 else
4445 height = This->surface_desc.dwHeight;
4448 wined3d_mutex_lock();
4449 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4451 enum wined3d_format_id current_format_id;
4452 format_id = wined3dformat_from_ddrawformat(&DDSD->u4.ddpfPixelFormat);
4454 if (format_id == WINED3DFMT_UNKNOWN)
4456 ERR("Requested to set an unknown pixelformat\n");
4457 wined3d_mutex_unlock();
4458 return DDERR_INVALIDPARAMS;
4460 current_format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4461 if (format_id != current_format_id)
4462 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4464 else
4466 format_id = wined3dformat_from_ddrawformat(&This->surface_desc.u4.ddpfPixelFormat);
4469 if (FAILED(hr = wined3d_surface_update_desc(This->wined3d_surface, width, height,
4470 format_id, WINED3D_MULTISAMPLE_NONE, 0, DDSD->lpSurface, pitch)))
4472 WARN("Failed to update surface desc, hr %#x.\n", hr);
4473 wined3d_mutex_unlock();
4474 return hr_ddraw_from_wined3d(hr);
4477 if (DDSD->dwFlags & DDSD_WIDTH)
4478 This->surface_desc.dwWidth = width;
4479 if (DDSD->dwFlags & DDSD_PITCH)
4480 This->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4481 if (DDSD->dwFlags & DDSD_HEIGHT)
4482 This->surface_desc.dwHeight = height;
4483 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4484 This->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4486 wined3d_mutex_unlock();
4488 return DD_OK;
4491 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4492 DDSURFACEDESC2 *surface_desc, DWORD flags)
4494 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4496 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4498 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4499 surface_desc, flags);
4502 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4503 DDSURFACEDESC *surface_desc, DWORD flags)
4505 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4506 DDSURFACEDESC2 surface_desc2;
4508 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4510 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4511 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4512 surface_desc ? &surface_desc2 : NULL, flags);
4515 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **palette)
4517 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4518 struct ddraw_palette *palette_impl;
4519 HRESULT hr = DD_OK;
4521 TRACE("iface %p, palette %p.\n", iface, palette);
4523 if (!palette)
4524 return DDERR_INVALIDPARAMS;
4525 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4527 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4528 return DDERR_SURFACELOST;
4531 wined3d_mutex_lock();
4532 if ((palette_impl = surface->palette))
4534 *palette = &palette_impl->IDirectDrawPalette_iface;
4535 IDirectDrawPalette_AddRef(*palette);
4537 else
4539 *palette = NULL;
4540 hr = DDERR_NOPALETTEATTACHED;
4542 wined3d_mutex_unlock();
4544 return hr;
4547 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4549 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4551 TRACE("iface %p, palette %p.\n", iface, palette);
4553 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4556 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4558 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4560 TRACE("iface %p, palette %p.\n", iface, palette);
4562 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4565 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4567 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4569 TRACE("iface %p, palette %p.\n", iface, palette);
4571 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4574 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4576 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4578 TRACE("iface %p, palette %p.\n", iface, palette);
4580 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4583 static HRESULT ddraw_surface_set_color_key(struct ddraw_surface *surface, DWORD flags, DDCOLORKEY *color_key)
4585 DDCOLORKEY fixed_color_key;
4586 HRESULT hr = WINED3D_OK;
4588 wined3d_mutex_lock();
4590 if (color_key)
4592 fixed_color_key = *color_key;
4593 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
4594 if (fixed_color_key.dwColorSpaceHighValue < fixed_color_key.dwColorSpaceLowValue)
4595 fixed_color_key.dwColorSpaceHighValue = fixed_color_key.dwColorSpaceLowValue;
4597 switch (flags & ~DDCKEY_COLORSPACE)
4599 case DDCKEY_DESTBLT:
4600 surface->surface_desc.ddckCKDestBlt = fixed_color_key;
4601 surface->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4602 break;
4604 case DDCKEY_DESTOVERLAY:
4605 surface->surface_desc.u3.ddckCKDestOverlay = fixed_color_key;
4606 surface->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4607 break;
4609 case DDCKEY_SRCOVERLAY:
4610 surface->surface_desc.ddckCKSrcOverlay = fixed_color_key;
4611 surface->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4612 break;
4614 case DDCKEY_SRCBLT:
4615 surface->surface_desc.ddckCKSrcBlt = fixed_color_key;
4616 surface->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4617 break;
4619 default:
4620 wined3d_mutex_unlock();
4621 return DDERR_INVALIDPARAMS;
4624 else
4626 switch (flags & ~DDCKEY_COLORSPACE)
4628 case DDCKEY_DESTBLT:
4629 surface->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4630 break;
4632 case DDCKEY_DESTOVERLAY:
4633 surface->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4634 break;
4636 case DDCKEY_SRCOVERLAY:
4637 surface->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4638 break;
4640 case DDCKEY_SRCBLT:
4641 surface->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4642 break;
4644 default:
4645 wined3d_mutex_unlock();
4646 return DDERR_INVALIDPARAMS;
4650 if (surface->wined3d_texture)
4651 hr = wined3d_texture_set_color_key(surface->wined3d_texture, flags,
4652 color_key ? (struct wined3d_color_key *)&fixed_color_key : NULL);
4654 wined3d_mutex_unlock();
4656 return hr_ddraw_from_wined3d(hr);
4659 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD flags, DDCOLORKEY *color_key)
4661 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4663 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4665 wined3d_mutex_lock();
4666 if (!surface->wined3d_texture)
4668 wined3d_mutex_unlock();
4669 return DDERR_NOTONMIPMAPSUBLEVEL;
4671 wined3d_mutex_unlock();
4673 return ddraw_surface_set_color_key(surface, flags, color_key);
4676 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4678 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4680 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4682 return ddraw_surface_set_color_key(surface, flags, color_key);
4685 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4687 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4689 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4691 return ddraw_surface_set_color_key(surface, flags, color_key);
4694 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4696 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4698 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4700 return ddraw_surface_set_color_key(surface, flags, color_key);
4703 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4705 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4707 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4709 return ddraw_surface_set_color_key(surface, flags, color_key);
4712 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *palette)
4714 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4716 TRACE("iface %p, palette %p.\n", iface, palette);
4718 if (surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4719 return DDERR_NOTONMIPMAPSUBLEVEL;
4720 if (IDirectDrawSurface7_IsLost(iface) == DDERR_SURFACELOST)
4722 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4723 return DDERR_SURFACELOST;
4726 return ddraw_surface_set_palette(surface, palette);
4729 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4731 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4733 TRACE("iface %p, palette %p.\n", iface, palette);
4735 if (IDirectDrawSurface4_IsLost(iface) == DDERR_SURFACELOST)
4737 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4738 return DDERR_SURFACELOST;
4741 return ddraw_surface_set_palette(surface, palette);
4744 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4746 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4748 TRACE("iface %p, palette %p.\n", iface, palette);
4750 if (IDirectDrawSurface3_IsLost(iface) == DDERR_SURFACELOST)
4752 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4753 return DDERR_SURFACELOST;
4756 return ddraw_surface_set_palette(surface, palette);
4759 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4761 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4763 TRACE("iface %p, palette %p.\n", iface, palette);
4765 if (IDirectDrawSurface2_IsLost(iface) == DDERR_SURFACELOST)
4767 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4768 return DDERR_SURFACELOST;
4771 return ddraw_surface_set_palette(surface, palette);
4774 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4776 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4778 TRACE("iface %p, palette %p.\n", iface, palette);
4780 if (IDirectDrawSurface_IsLost(iface) == DDERR_SURFACELOST)
4782 WARN("Surface lost, returning DDERR_SURFACELOST.\n");
4783 return DDERR_SURFACELOST;
4786 return ddraw_surface_set_palette(surface, palette);
4789 /**********************************************************
4790 * IDirectDrawGammaControl::GetGammaRamp
4792 * Returns the current gamma ramp for a surface
4794 * Params:
4795 * flags: Ignored
4796 * gamma_ramp: Address to write the ramp to
4798 * Returns:
4799 * DD_OK on success
4800 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4802 **********************************************************/
4803 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4804 DWORD flags, DDGAMMARAMP *gamma_ramp)
4806 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4808 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4810 if (!gamma_ramp)
4812 WARN("Invalid gamma_ramp passed.\n");
4813 return DDERR_INVALIDPARAMS;
4816 wined3d_mutex_lock();
4817 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4819 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4820 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
4822 else
4824 ERR("Not implemented for non-primary surfaces.\n");
4826 wined3d_mutex_unlock();
4828 return DD_OK;
4831 /**********************************************************
4832 * IDirectDrawGammaControl::SetGammaRamp
4834 * Sets the red, green and blue gamma ramps for
4836 * Params:
4837 * flags: Can be DDSGR_CALIBRATE to request calibration
4838 * gamma_ramp: Structure containing the new gamma ramp
4840 * Returns:
4841 * DD_OK on success
4842 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4844 **********************************************************/
4845 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4846 DWORD flags, DDGAMMARAMP *gamma_ramp)
4848 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4850 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4852 if (!gamma_ramp)
4854 WARN("Invalid gamma_ramp passed.\n");
4855 return DDERR_INVALIDPARAMS;
4858 wined3d_mutex_lock();
4859 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4861 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4862 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
4863 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
4865 else
4867 ERR("Not implemented for non-primary surfaces.\n");
4869 wined3d_mutex_unlock();
4871 return DD_OK;
4874 /*****************************************************************************
4875 * IDirect3DTexture2::PaletteChanged
4877 * Informs the texture about a palette change
4879 * Params:
4880 * start: Start index of the change
4881 * count: The number of changed entries
4883 * Returns
4884 * D3D_OK, because it's a stub
4886 *****************************************************************************/
4887 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4889 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4891 return D3D_OK;
4894 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4896 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
4898 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4900 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
4903 /*****************************************************************************
4904 * IDirect3DTexture::Unload
4906 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4909 * Returns:
4910 * DDERR_UNSUPPORTED
4912 *****************************************************************************/
4913 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4915 WARN("iface %p. Not implemented.\n", iface);
4917 return DDERR_UNSUPPORTED;
4920 /*****************************************************************************
4921 * IDirect3DTexture2::GetHandle
4923 * Returns handle for the texture. At the moment, the interface
4924 * to the IWineD3DTexture is used.
4926 * Params:
4927 * device: Device this handle is assigned to
4928 * handle: Address to store the handle at.
4930 * Returns:
4931 * D3D_OK
4933 *****************************************************************************/
4934 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4935 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4937 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
4938 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
4940 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4942 wined3d_mutex_lock();
4944 if (!surface->Handle)
4946 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
4947 if (h == DDRAW_INVALID_HANDLE)
4949 ERR("Failed to allocate a texture handle.\n");
4950 wined3d_mutex_unlock();
4951 return DDERR_OUTOFMEMORY;
4954 surface->Handle = h + 1;
4957 TRACE("Returning handle %08x.\n", surface->Handle);
4958 *handle = surface->Handle;
4960 wined3d_mutex_unlock();
4962 return D3D_OK;
4965 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
4966 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
4968 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
4969 struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice(device);
4971 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4973 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
4974 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
4977 /*****************************************************************************
4978 * get_sub_mimaplevel
4980 * Helper function that returns the next mipmap level
4982 * tex_ptr: Surface of which to return the next level
4984 *****************************************************************************/
4985 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
4987 /* Now go down the mipmap chain to the next surface */
4988 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
4989 IDirectDrawSurface7 *next_level;
4990 HRESULT hr;
4992 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
4993 if (FAILED(hr)) return NULL;
4995 ddraw_surface7_Release(next_level);
4997 return impl_from_IDirectDrawSurface7(next_level);
5000 /*****************************************************************************
5001 * IDirect3DTexture2::Load
5003 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5005 * This function isn't relayed to WineD3D because the whole interface is
5006 * implemented in DDraw only. For speed improvements an implementation which
5007 * takes OpenGL more into account could be placed into WineD3D.
5009 * Params:
5010 * src_texture: Address of the texture to load
5012 * Returns:
5013 * D3D_OK on success
5014 * D3DERR_TEXTURE_LOAD_FAILED.
5016 *****************************************************************************/
5017 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5019 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5020 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5021 HRESULT hr;
5023 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5025 if (src_surface == dst_surface)
5027 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5028 return D3D_OK;
5031 wined3d_mutex_lock();
5033 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5034 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5035 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5037 ERR("Trying to load surfaces with different mip-map counts.\n");
5040 for (;;)
5042 struct ddraw_palette *dst_pal, *src_pal;
5043 DDSURFACEDESC *src_desc, *dst_desc;
5045 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
5046 src_surface, dst_surface, src_surface->mipmap_level);
5048 /* Suppress the ALLOCONLOAD flag */
5049 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5051 /* Get the palettes */
5052 dst_pal = dst_surface->palette;
5053 src_pal = src_surface->palette;
5055 if (src_pal)
5057 PALETTEENTRY palent[256];
5059 if (!dst_pal)
5061 wined3d_mutex_unlock();
5062 return DDERR_NOPALETTEATTACHED;
5064 IDirectDrawPalette_GetEntries(&src_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5065 IDirectDrawPalette_SetEntries(&dst_pal->IDirectDrawPalette_iface, 0, 0, 256, palent);
5068 /* Copy one surface on the other */
5069 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5070 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5072 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5074 /* Should also check for same pixel format, u1.lPitch, ... */
5075 ERR("Error in surface sizes.\n");
5076 wined3d_mutex_unlock();
5077 return D3DERR_TEXTURE_LOAD_FAILED;
5079 else
5081 struct wined3d_map_desc src_map_desc, dst_map_desc;
5083 /* Copy the src blit color key if the source has one, don't erase
5084 * the destination's ckey if the source has none */
5085 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5087 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5088 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5091 /* Copy the main memory texture into the surface that corresponds
5092 * to the OpenGL texture object. */
5094 hr = wined3d_surface_map(src_surface->wined3d_surface, &src_map_desc, NULL, 0);
5095 if (FAILED(hr))
5097 ERR("Failed to lock source surface, hr %#x.\n", hr);
5098 wined3d_mutex_unlock();
5099 return D3DERR_TEXTURE_LOAD_FAILED;
5102 hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_map_desc, NULL, 0);
5103 if (FAILED(hr))
5105 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5106 wined3d_surface_unmap(src_surface->wined3d_surface);
5107 wined3d_mutex_unlock();
5108 return D3DERR_TEXTURE_LOAD_FAILED;
5111 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5112 memcpy(dst_map_desc.data, src_map_desc.data, src_surface->surface_desc.u1.dwLinearSize);
5113 else
5114 memcpy(dst_map_desc.data, src_map_desc.data, src_map_desc.row_pitch * src_desc->dwHeight);
5116 wined3d_surface_unmap(src_surface->wined3d_surface);
5117 wined3d_surface_unmap(dst_surface->wined3d_surface);
5120 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5121 src_surface = get_sub_mimaplevel(src_surface);
5122 else
5123 src_surface = NULL;
5125 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5126 dst_surface = get_sub_mimaplevel(dst_surface);
5127 else
5128 dst_surface = NULL;
5130 if (!src_surface || !dst_surface)
5132 if (src_surface != dst_surface)
5133 ERR("Loading surface with different mipmap structure.\n");
5134 break;
5138 wined3d_mutex_unlock();
5140 return hr;
5143 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5145 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5146 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5148 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5150 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5151 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5154 /*****************************************************************************
5155 * The VTable
5156 *****************************************************************************/
5158 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5160 /* IUnknown */
5161 ddraw_surface7_QueryInterface,
5162 ddraw_surface7_AddRef,
5163 ddraw_surface7_Release,
5164 /* IDirectDrawSurface */
5165 ddraw_surface7_AddAttachedSurface,
5166 ddraw_surface7_AddOverlayDirtyRect,
5167 ddraw_surface7_Blt,
5168 ddraw_surface7_BltBatch,
5169 ddraw_surface7_BltFast,
5170 ddraw_surface7_DeleteAttachedSurface,
5171 ddraw_surface7_EnumAttachedSurfaces,
5172 ddraw_surface7_EnumOverlayZOrders,
5173 ddraw_surface7_Flip,
5174 ddraw_surface7_GetAttachedSurface,
5175 ddraw_surface7_GetBltStatus,
5176 ddraw_surface7_GetCaps,
5177 ddraw_surface7_GetClipper,
5178 ddraw_surface7_GetColorKey,
5179 ddraw_surface7_GetDC,
5180 ddraw_surface7_GetFlipStatus,
5181 ddraw_surface7_GetOverlayPosition,
5182 ddraw_surface7_GetPalette,
5183 ddraw_surface7_GetPixelFormat,
5184 ddraw_surface7_GetSurfaceDesc,
5185 ddraw_surface7_Initialize,
5186 ddraw_surface7_IsLost,
5187 ddraw_surface7_Lock,
5188 ddraw_surface7_ReleaseDC,
5189 ddraw_surface7_Restore,
5190 ddraw_surface7_SetClipper,
5191 ddraw_surface7_SetColorKey,
5192 ddraw_surface7_SetOverlayPosition,
5193 ddraw_surface7_SetPalette,
5194 ddraw_surface7_Unlock,
5195 ddraw_surface7_UpdateOverlay,
5196 ddraw_surface7_UpdateOverlayDisplay,
5197 ddraw_surface7_UpdateOverlayZOrder,
5198 /* IDirectDrawSurface2 */
5199 ddraw_surface7_GetDDInterface,
5200 ddraw_surface7_PageLock,
5201 ddraw_surface7_PageUnlock,
5202 /* IDirectDrawSurface3 */
5203 ddraw_surface7_SetSurfaceDesc,
5204 /* IDirectDrawSurface4 */
5205 ddraw_surface7_SetPrivateData,
5206 ddraw_surface7_GetPrivateData,
5207 ddraw_surface7_FreePrivateData,
5208 ddraw_surface7_GetUniquenessValue,
5209 ddraw_surface7_ChangeUniquenessValue,
5210 /* IDirectDrawSurface7 */
5211 ddraw_surface7_SetPriority,
5212 ddraw_surface7_GetPriority,
5213 ddraw_surface7_SetLOD,
5214 ddraw_surface7_GetLOD,
5217 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5219 /* IUnknown */
5220 ddraw_surface4_QueryInterface,
5221 ddraw_surface4_AddRef,
5222 ddraw_surface4_Release,
5223 /* IDirectDrawSurface */
5224 ddraw_surface4_AddAttachedSurface,
5225 ddraw_surface4_AddOverlayDirtyRect,
5226 ddraw_surface4_Blt,
5227 ddraw_surface4_BltBatch,
5228 ddraw_surface4_BltFast,
5229 ddraw_surface4_DeleteAttachedSurface,
5230 ddraw_surface4_EnumAttachedSurfaces,
5231 ddraw_surface4_EnumOverlayZOrders,
5232 ddraw_surface4_Flip,
5233 ddraw_surface4_GetAttachedSurface,
5234 ddraw_surface4_GetBltStatus,
5235 ddraw_surface4_GetCaps,
5236 ddraw_surface4_GetClipper,
5237 ddraw_surface4_GetColorKey,
5238 ddraw_surface4_GetDC,
5239 ddraw_surface4_GetFlipStatus,
5240 ddraw_surface4_GetOverlayPosition,
5241 ddraw_surface4_GetPalette,
5242 ddraw_surface4_GetPixelFormat,
5243 ddraw_surface4_GetSurfaceDesc,
5244 ddraw_surface4_Initialize,
5245 ddraw_surface4_IsLost,
5246 ddraw_surface4_Lock,
5247 ddraw_surface4_ReleaseDC,
5248 ddraw_surface4_Restore,
5249 ddraw_surface4_SetClipper,
5250 ddraw_surface4_SetColorKey,
5251 ddraw_surface4_SetOverlayPosition,
5252 ddraw_surface4_SetPalette,
5253 ddraw_surface4_Unlock,
5254 ddraw_surface4_UpdateOverlay,
5255 ddraw_surface4_UpdateOverlayDisplay,
5256 ddraw_surface4_UpdateOverlayZOrder,
5257 /* IDirectDrawSurface2 */
5258 ddraw_surface4_GetDDInterface,
5259 ddraw_surface4_PageLock,
5260 ddraw_surface4_PageUnlock,
5261 /* IDirectDrawSurface3 */
5262 ddraw_surface4_SetSurfaceDesc,
5263 /* IDirectDrawSurface4 */
5264 ddraw_surface4_SetPrivateData,
5265 ddraw_surface4_GetPrivateData,
5266 ddraw_surface4_FreePrivateData,
5267 ddraw_surface4_GetUniquenessValue,
5268 ddraw_surface4_ChangeUniquenessValue,
5271 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5273 /* IUnknown */
5274 ddraw_surface3_QueryInterface,
5275 ddraw_surface3_AddRef,
5276 ddraw_surface3_Release,
5277 /* IDirectDrawSurface */
5278 ddraw_surface3_AddAttachedSurface,
5279 ddraw_surface3_AddOverlayDirtyRect,
5280 ddraw_surface3_Blt,
5281 ddraw_surface3_BltBatch,
5282 ddraw_surface3_BltFast,
5283 ddraw_surface3_DeleteAttachedSurface,
5284 ddraw_surface3_EnumAttachedSurfaces,
5285 ddraw_surface3_EnumOverlayZOrders,
5286 ddraw_surface3_Flip,
5287 ddraw_surface3_GetAttachedSurface,
5288 ddraw_surface3_GetBltStatus,
5289 ddraw_surface3_GetCaps,
5290 ddraw_surface3_GetClipper,
5291 ddraw_surface3_GetColorKey,
5292 ddraw_surface3_GetDC,
5293 ddraw_surface3_GetFlipStatus,
5294 ddraw_surface3_GetOverlayPosition,
5295 ddraw_surface3_GetPalette,
5296 ddraw_surface3_GetPixelFormat,
5297 ddraw_surface3_GetSurfaceDesc,
5298 ddraw_surface3_Initialize,
5299 ddraw_surface3_IsLost,
5300 ddraw_surface3_Lock,
5301 ddraw_surface3_ReleaseDC,
5302 ddraw_surface3_Restore,
5303 ddraw_surface3_SetClipper,
5304 ddraw_surface3_SetColorKey,
5305 ddraw_surface3_SetOverlayPosition,
5306 ddraw_surface3_SetPalette,
5307 ddraw_surface3_Unlock,
5308 ddraw_surface3_UpdateOverlay,
5309 ddraw_surface3_UpdateOverlayDisplay,
5310 ddraw_surface3_UpdateOverlayZOrder,
5311 /* IDirectDrawSurface2 */
5312 ddraw_surface3_GetDDInterface,
5313 ddraw_surface3_PageLock,
5314 ddraw_surface3_PageUnlock,
5315 /* IDirectDrawSurface3 */
5316 ddraw_surface3_SetSurfaceDesc,
5319 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5321 /* IUnknown */
5322 ddraw_surface2_QueryInterface,
5323 ddraw_surface2_AddRef,
5324 ddraw_surface2_Release,
5325 /* IDirectDrawSurface */
5326 ddraw_surface2_AddAttachedSurface,
5327 ddraw_surface2_AddOverlayDirtyRect,
5328 ddraw_surface2_Blt,
5329 ddraw_surface2_BltBatch,
5330 ddraw_surface2_BltFast,
5331 ddraw_surface2_DeleteAttachedSurface,
5332 ddraw_surface2_EnumAttachedSurfaces,
5333 ddraw_surface2_EnumOverlayZOrders,
5334 ddraw_surface2_Flip,
5335 ddraw_surface2_GetAttachedSurface,
5336 ddraw_surface2_GetBltStatus,
5337 ddraw_surface2_GetCaps,
5338 ddraw_surface2_GetClipper,
5339 ddraw_surface2_GetColorKey,
5340 ddraw_surface2_GetDC,
5341 ddraw_surface2_GetFlipStatus,
5342 ddraw_surface2_GetOverlayPosition,
5343 ddraw_surface2_GetPalette,
5344 ddraw_surface2_GetPixelFormat,
5345 ddraw_surface2_GetSurfaceDesc,
5346 ddraw_surface2_Initialize,
5347 ddraw_surface2_IsLost,
5348 ddraw_surface2_Lock,
5349 ddraw_surface2_ReleaseDC,
5350 ddraw_surface2_Restore,
5351 ddraw_surface2_SetClipper,
5352 ddraw_surface2_SetColorKey,
5353 ddraw_surface2_SetOverlayPosition,
5354 ddraw_surface2_SetPalette,
5355 ddraw_surface2_Unlock,
5356 ddraw_surface2_UpdateOverlay,
5357 ddraw_surface2_UpdateOverlayDisplay,
5358 ddraw_surface2_UpdateOverlayZOrder,
5359 /* IDirectDrawSurface2 */
5360 ddraw_surface2_GetDDInterface,
5361 ddraw_surface2_PageLock,
5362 ddraw_surface2_PageUnlock,
5365 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5367 /* IUnknown */
5368 ddraw_surface1_QueryInterface,
5369 ddraw_surface1_AddRef,
5370 ddraw_surface1_Release,
5371 /* IDirectDrawSurface */
5372 ddraw_surface1_AddAttachedSurface,
5373 ddraw_surface1_AddOverlayDirtyRect,
5374 ddraw_surface1_Blt,
5375 ddraw_surface1_BltBatch,
5376 ddraw_surface1_BltFast,
5377 ddraw_surface1_DeleteAttachedSurface,
5378 ddraw_surface1_EnumAttachedSurfaces,
5379 ddraw_surface1_EnumOverlayZOrders,
5380 ddraw_surface1_Flip,
5381 ddraw_surface1_GetAttachedSurface,
5382 ddraw_surface1_GetBltStatus,
5383 ddraw_surface1_GetCaps,
5384 ddraw_surface1_GetClipper,
5385 ddraw_surface1_GetColorKey,
5386 ddraw_surface1_GetDC,
5387 ddraw_surface1_GetFlipStatus,
5388 ddraw_surface1_GetOverlayPosition,
5389 ddraw_surface1_GetPalette,
5390 ddraw_surface1_GetPixelFormat,
5391 ddraw_surface1_GetSurfaceDesc,
5392 ddraw_surface1_Initialize,
5393 ddraw_surface1_IsLost,
5394 ddraw_surface1_Lock,
5395 ddraw_surface1_ReleaseDC,
5396 ddraw_surface1_Restore,
5397 ddraw_surface1_SetClipper,
5398 ddraw_surface1_SetColorKey,
5399 ddraw_surface1_SetOverlayPosition,
5400 ddraw_surface1_SetPalette,
5401 ddraw_surface1_Unlock,
5402 ddraw_surface1_UpdateOverlay,
5403 ddraw_surface1_UpdateOverlayDisplay,
5404 ddraw_surface1_UpdateOverlayZOrder,
5407 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5409 ddraw_gamma_control_QueryInterface,
5410 ddraw_gamma_control_AddRef,
5411 ddraw_gamma_control_Release,
5412 ddraw_gamma_control_GetGammaRamp,
5413 ddraw_gamma_control_SetGammaRamp,
5416 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5418 d3d_texture2_QueryInterface,
5419 d3d_texture2_AddRef,
5420 d3d_texture2_Release,
5421 d3d_texture2_GetHandle,
5422 d3d_texture2_PaletteChanged,
5423 d3d_texture2_Load,
5426 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5428 d3d_texture1_QueryInterface,
5429 d3d_texture1_AddRef,
5430 d3d_texture1_Release,
5431 d3d_texture1_Initialize,
5432 d3d_texture1_GetHandle,
5433 d3d_texture1_PaletteChanged,
5434 d3d_texture1_Load,
5435 d3d_texture1_Unload,
5438 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5439 * IDirectDrawSurface interface to ddraw methods. */
5440 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5442 if (!iface) return NULL;
5443 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5445 HRESULT hr = IDirectDrawSurface7_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5446 if (FAILED(hr))
5448 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5449 return NULL;
5451 IDirectDrawSurface7_Release(iface);
5453 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5456 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5458 if (!iface) return NULL;
5459 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5461 HRESULT hr = IDirectDrawSurface4_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5462 if (FAILED(hr))
5464 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5465 return NULL;
5467 IDirectDrawSurface4_Release(iface);
5469 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5472 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5474 if (!iface) return NULL;
5475 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5477 HRESULT hr = IDirectDrawSurface3_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5478 if (FAILED(hr))
5480 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5481 return NULL;
5483 IDirectDrawSurface3_Release(iface);
5485 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5488 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5490 if (!iface) return NULL;
5491 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5493 HRESULT hr = IDirectDrawSurface2_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5494 if (FAILED(hr))
5496 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5497 return NULL;
5499 IDirectDrawSurface2_Release(iface);
5501 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5504 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5506 if (!iface) return NULL;
5507 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5509 HRESULT hr = IDirectDrawSurface_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5510 if (FAILED(hr))
5512 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5513 return NULL;
5515 IDirectDrawSurface_Release(iface);
5517 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5520 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5522 if (!iface) return NULL;
5523 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5524 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5527 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5529 if (!iface) return NULL;
5530 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5531 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5534 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5536 struct ddraw_surface *surface = parent;
5538 TRACE("surface %p.\n", surface);
5540 /* Check for attached surfaces and detach them. */
5541 if (surface->first_attached != surface)
5543 /* Well, this shouldn't happen: The surface being attached is
5544 * referenced in AddAttachedSurface(), so it shouldn't be released
5545 * until DeleteAttachedSurface() is called, because the refcount is
5546 * held. It looks like the application released it often enough to
5547 * force this. */
5548 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
5550 /* The refcount will drop to -1 here */
5551 if (FAILED(ddraw_surface_delete_attached_surface(surface->first_attached, surface, surface->attached_iface)))
5552 ERR("DeleteAttachedSurface failed.\n");
5555 while (surface->next_attached)
5556 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5557 surface->next_attached, surface->next_attached->attached_iface)))
5558 ERR("DeleteAttachedSurface failed.\n");
5560 /* Having a texture handle set implies that the device still exists. */
5561 if (surface->Handle)
5562 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5564 /* Reduce the ddraw surface count. */
5565 list_remove(&surface->surface_list_entry);
5567 if (surface == surface->ddraw->primary)
5568 surface->ddraw->primary = NULL;
5570 wined3d_private_store_cleanup(&surface->private_store);
5572 HeapFree(GetProcessHeap(), 0, surface);
5575 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5577 ddraw_surface_wined3d_object_destroyed,
5580 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5582 TRACE("parent %p.\n", parent);
5584 HeapFree(GetProcessHeap(), 0, parent);
5587 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5589 ddraw_texture_wined3d_object_destroyed,
5592 static HRESULT CDECL ddraw_reset_enum_callback(struct wined3d_resource *resource)
5594 return DD_OK;
5597 HRESULT ddraw_surface_create(struct ddraw *ddraw, const DDSURFACEDESC2 *surface_desc,
5598 struct ddraw_surface **surface, IUnknown *outer_unknown, unsigned int version)
5600 struct ddraw_surface *root, *mip, **attach;
5601 struct wined3d_resource_desc wined3d_desc;
5602 struct wined3d_texture *wined3d_texture;
5603 struct wined3d_resource *resource;
5604 struct wined3d_display_mode mode;
5605 DDSURFACEDESC2 *desc, *mip_desc;
5606 struct ddraw_texture *texture;
5607 UINT layers, levels, i, j;
5608 unsigned int pitch = 0;
5609 HRESULT hr;
5611 TRACE("ddraw %p, surface_desc %p, surface %p, outer_unknown %p, version %u.\n",
5612 ddraw, surface_desc, surface, outer_unknown, version);
5613 if (TRACE_ON(ddraw))
5615 TRACE("Requesting surface desc:\n");
5616 DDRAW_dump_surface_desc(surface_desc);
5619 if (outer_unknown)
5620 return CLASS_E_NOAGGREGATION;
5622 if (!surface)
5623 return E_POINTER;
5625 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
5626 return E_OUTOFMEMORY;
5628 texture->version = version;
5629 texture->surface_desc = *surface_desc;
5630 desc = &texture->surface_desc;
5632 /* Ensure DDSD_CAPS is always set. */
5633 desc->dwFlags |= DDSD_CAPS;
5635 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5637 DWORD flippable = desc->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_COMPLEX);
5639 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5641 WARN("Tried to create a primary surface with DDSCAPS_TEXTURE.\n");
5642 HeapFree(GetProcessHeap(), 0, texture);
5643 return DDERR_INVALIDCAPS;
5646 if (flippable)
5648 if (flippable != (DDSCAPS_FLIP | DDSCAPS_COMPLEX))
5650 WARN("Tried to create a flippable primary surface without both DDSCAPS_FLIP and DDSCAPS_COMPLEX.\n");
5651 HeapFree(GetProcessHeap(), 0, texture);
5652 return DDERR_INVALIDCAPS;
5655 if (!(desc->dwFlags & DDSD_BACKBUFFERCOUNT) || !desc->dwBackBufferCount)
5657 WARN("Tried to create a flippable primary surface without any back buffers.\n");
5658 HeapFree(GetProcessHeap(), 0, texture);
5659 return DDERR_INVALIDCAPS;
5662 if (!(ddraw->cooperative_level & DDSCL_EXCLUSIVE))
5664 WARN("Tried to create a flippable primary surface without DDSCL_EXCLUSIVE.\n");
5665 HeapFree(GetProcessHeap(), 0, texture);
5666 return DDERR_NOEXCLUSIVEMODE;
5671 /* This is a special case in ddrawex, but not allowed in ddraw. */
5672 if ((desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5673 == (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5675 WARN("Tried to create a surface in both system and video memory.\n");
5676 HeapFree(GetProcessHeap(), 0, texture);
5677 return DDERR_INVALIDCAPS;
5680 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES)
5681 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
5683 WARN("Cube map faces requested without cube map flag.\n");
5684 HeapFree(GetProcessHeap(), 0, texture);
5685 return DDERR_INVALIDCAPS;
5688 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5689 && !(desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES))
5691 WARN("Cube map without faces requested.\n");
5692 HeapFree(GetProcessHeap(), 0, texture);
5693 return DDERR_INVALIDPARAMS;
5696 if ((desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5697 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
5698 FIXME("Partial cube maps not implemented.\n");
5700 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
5702 if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5704 WARN("DDSCAPS2_TEXTUREMANAGE used without DDSCAPS_TEXTURE, returning DDERR_INVALIDCAPS.\n");
5705 HeapFree(GetProcessHeap(), 0, texture);
5706 return DDERR_INVALIDCAPS;
5708 if (desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5710 WARN("DDSCAPS2_TEXTUREMANAGE used width DDSCAPS_VIDEOMEMORY "
5711 "or DDSCAPS_SYSTEMMEMORY, returning DDERR_INVALIDCAPS.\n");
5712 HeapFree(GetProcessHeap(), 0, texture);
5713 return DDERR_INVALIDCAPS;
5717 if (FAILED(hr = wined3d_get_adapter_display_mode(ddraw->wined3d, WINED3DADAPTER_DEFAULT, &mode, NULL)))
5719 ERR("Failed to get display mode, hr %#x.\n", hr);
5720 HeapFree(GetProcessHeap(), 0, texture);
5721 return hr_ddraw_from_wined3d(hr);
5724 /* No pixelformat given? Use the current screen format. */
5725 if (!(desc->dwFlags & DDSD_PIXELFORMAT))
5727 desc->dwFlags |= DDSD_PIXELFORMAT;
5728 desc->u4.ddpfPixelFormat.dwSize = sizeof(desc->u4.ddpfPixelFormat);
5729 ddrawformat_from_wined3dformat(&desc->u4.ddpfPixelFormat, mode.format_id);
5732 wined3d_desc.format = wined3dformat_from_ddrawformat(&desc->u4.ddpfPixelFormat);
5733 if (wined3d_desc.format == WINED3DFMT_UNKNOWN)
5735 WARN("Unsupported / unknown pixelformat.\n");
5736 HeapFree(GetProcessHeap(), 0, texture);
5737 return DDERR_INVALIDPIXELFORMAT;
5740 /* No width or no height? Use the screen size. */
5741 if (!(desc->dwFlags & DDSD_WIDTH) || !(desc->dwFlags & DDSD_HEIGHT))
5743 if (!(desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
5745 WARN("No width / height specified.\n");
5746 HeapFree(GetProcessHeap(), 0, texture);
5747 return DDERR_INVALIDPARAMS;
5750 desc->dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
5751 desc->dwWidth = mode.width;
5752 desc->dwHeight = mode.height;
5755 if (!desc->dwWidth || !desc->dwHeight)
5757 HeapFree(GetProcessHeap(), 0, texture);
5758 return DDERR_INVALIDPARAMS;
5761 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5763 /* The first surface is a front buffer, the back buffers are created
5764 * afterwards. */
5765 if (desc->ddsCaps.dwCaps & DDSCAPS_FLIP)
5766 desc->ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
5767 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5768 if (ddraw->cooperative_level & DDSCL_EXCLUSIVE)
5770 struct wined3d_swapchain_desc swapchain_desc;
5772 wined3d_swapchain_get_desc(ddraw->wined3d_swapchain, &swapchain_desc);
5773 swapchain_desc.backbuffer_width = mode.width;
5774 swapchain_desc.backbuffer_height = mode.height;
5775 swapchain_desc.backbuffer_format = mode.format_id;
5777 if (FAILED(hr = wined3d_device_reset(ddraw->wined3d_device,
5778 &swapchain_desc, NULL, ddraw_reset_enum_callback, TRUE)))
5780 ERR("Failed to reset device.\n");
5781 HeapFree(GetProcessHeap(), 0, texture);
5782 return hr_ddraw_from_wined3d(hr);
5787 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
5788 wined3d_desc.multisample_quality = 0;
5789 wined3d_desc.usage = 0;
5790 wined3d_desc.pool = WINED3D_POOL_DEFAULT;
5791 wined3d_desc.width = desc->dwWidth;
5792 wined3d_desc.height = desc->dwHeight;
5793 wined3d_desc.depth = 1;
5794 wined3d_desc.size = 0;
5796 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && (ddraw->flags & DDRAW_NO3D))
5798 WARN("The application requests a 3D capable surface, but the ddraw object was created without 3D support.\n");
5799 /* Do not fail surface creation, only fail 3D device creation. */
5802 /* Mipmap count fixes */
5803 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5805 if (desc->ddsCaps.dwCaps & DDSCAPS_COMPLEX)
5807 if (desc->dwFlags & DDSD_MIPMAPCOUNT)
5809 /* Mipmap count is given, should not be 0. */
5810 if (!desc->u2.dwMipMapCount)
5812 HeapFree(GetProcessHeap(), 0, texture);
5813 return DDERR_INVALIDPARAMS;
5816 else
5818 /* Undocumented feature: Create sublevels until either the
5819 * width or the height is 1. */
5820 DWORD min = desc->dwWidth < desc->dwHeight ? desc->dwWidth : desc->dwHeight;
5822 desc->u2.dwMipMapCount = 0;
5823 while (min)
5825 ++desc->u2.dwMipMapCount;
5826 min >>= 1;
5830 else
5832 desc->u2.dwMipMapCount = 1;
5835 desc->dwFlags |= DDSD_MIPMAPCOUNT;
5836 levels = desc->u2.dwMipMapCount;
5838 else
5840 levels = 1;
5843 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)))
5845 if (!(desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)))
5847 enum wined3d_resource_type rtype;
5848 DWORD usage = 0;
5850 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5851 rtype = WINED3D_RTYPE_CUBE_TEXTURE;
5852 else if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5853 rtype = WINED3D_RTYPE_TEXTURE;
5854 else
5855 rtype = WINED3D_RTYPE_SURFACE;
5857 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5858 usage = WINED3DUSAGE_DEPTHSTENCIL;
5859 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5860 usage = WINED3DUSAGE_RENDERTARGET;
5862 if (SUCCEEDED(hr = wined3d_check_device_format(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
5863 WINED3D_DEVICE_TYPE_HAL, mode.format_id, usage, rtype, wined3d_desc.format)))
5864 desc->ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
5865 else
5866 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5868 else if (!(desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE))
5870 /* Tests show surfaces without memory flags get these flags added
5871 * right after creation. */
5872 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5876 if ((desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5877 == (DDSCAPS_OVERLAY | DDSCAPS_SYSTEMMEMORY))
5879 WARN("System memory overlays are not allowed.\n");
5880 HeapFree(GetProcessHeap(), 0, texture);
5881 return DDERR_NOOVERLAYHW;
5884 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5886 wined3d_desc.pool = WINED3D_POOL_SYSTEM_MEM;
5888 else
5890 if (desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5891 wined3d_desc.usage |= WINED3DUSAGE_TEXTURE;
5892 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5893 wined3d_desc.usage |= WINED3DUSAGE_DEPTHSTENCIL;
5894 else if (desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
5895 wined3d_desc.usage |= WINED3DUSAGE_RENDERTARGET;
5897 if (desc->ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE))
5899 wined3d_desc.pool = WINED3D_POOL_MANAGED;
5900 /* Managed textures have the system memory flag set. */
5901 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5903 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5905 /* Videomemory adds localvidmem. This is mutually exclusive with
5906 * systemmemory and texturemanage. */
5907 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
5908 wined3d_desc.usage |= WINED3DUSAGE_DYNAMIC;
5912 /* If the surface is of the 'ALLOCONLOAD' type, ignore the LPSURFACE
5913 * field. Frank Herbert's Dune specifies a NULL pointer for lpSurface. */
5914 if ((desc->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD) || !desc->lpSurface)
5915 desc->dwFlags &= ~DDSD_LPSURFACE;
5916 if (desc->dwFlags & DDSD_LPSURFACE)
5918 if (wined3d_desc.pool != WINED3D_POOL_SYSTEM_MEM)
5920 WARN("User memory surfaces should be in the system memory pool.\n");
5921 HeapFree(GetProcessHeap(), 0, texture);
5922 return DDERR_INVALIDCAPS;
5925 if (version < 4)
5927 WARN("User memory surfaces not supported before version 4.\n");
5928 HeapFree(GetProcessHeap(), 0, texture);
5929 return DDERR_INVALIDPARAMS;
5932 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
5934 if (version != 4 && (desc->dwFlags & DDSD_PITCH))
5936 WARN("Pitch specified on a compressed user memory surface.\n");
5937 HeapFree(GetProcessHeap(), 0, texture);
5938 return DDERR_INVALIDPARAMS;
5941 if (!(desc->dwFlags & (DDSD_LINEARSIZE | DDSD_PITCH)))
5943 WARN("Compressed user memory surfaces should explicitly specify the linear size.\n");
5944 HeapFree(GetProcessHeap(), 0, texture);
5945 return DDERR_INVALIDPARAMS;
5948 if ((desc->dwFlags & DDSD_LINEARSIZE)
5949 && desc->u1.dwLinearSize < wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
5950 wined3d_desc.format, wined3d_desc.width) * ((desc->dwHeight + 3) / 4))
5952 WARN("Invalid linear size %u specified.\n", desc->u1.dwLinearSize);
5953 HeapFree(GetProcessHeap(), 0, texture);
5954 return DDERR_INVALIDPARAMS;
5957 else
5959 if (!(desc->dwFlags & DDSD_PITCH))
5961 WARN("User memory surfaces should explicitly specify the pitch.\n");
5962 HeapFree(GetProcessHeap(), 0, texture);
5963 return DDERR_INVALIDPARAMS;
5966 if (desc->u1.lPitch < wined3d_calculate_format_pitch(ddraw->wined3d, WINED3DADAPTER_DEFAULT,
5967 wined3d_desc.format, wined3d_desc.width) || desc->u1.lPitch & 3)
5969 WARN("Invalid pitch %u specified.\n", desc->u1.lPitch);
5970 HeapFree(GetProcessHeap(), 0, texture);
5971 return DDERR_INVALIDPARAMS;
5974 pitch = desc->u1.lPitch;
5978 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
5979 wined3d_desc.usage |= WINED3DUSAGE_OVERLAY;
5981 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
5982 wined3d_desc.usage |= WINED3DUSAGE_OWNDC;
5984 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5986 wined3d_desc.resource_type = WINED3D_RTYPE_CUBE_TEXTURE;
5987 layers = 6;
5989 else
5991 wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE;
5992 layers = 1;
5995 /* Some applications assume surfaces will always be mapped at the same
5996 * address. Some of those also assume that this address is valid even when
5997 * the surface isn't mapped, and that updates done this way will be
5998 * visible on the screen. The game Nox is such an application,
5999 * Commandos: Behind Enemy Lines is another. We set
6000 * WINED3D_SURFACE_PIN_SYSMEM because of this. */
6001 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, levels,
6002 WINED3D_SURFACE_PIN_SYSMEM, texture, &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
6004 WARN("Failed to create wined3d texture, hr %#x.\n", hr);
6005 HeapFree(GetProcessHeap(), 0, texture);
6006 return hr_ddraw_from_wined3d(hr);
6009 resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
6010 root = wined3d_resource_get_parent(resource);
6011 root->wined3d_texture = wined3d_texture;
6012 root->is_complex_root = TRUE;
6013 texture->root = root;
6015 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6016 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6017 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6018 if (desc->dwFlags & DDSD_CKDESTBLT)
6019 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6020 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6021 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6022 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6023 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6024 if (desc->dwFlags & DDSD_CKSRCBLT)
6025 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6026 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6028 for (i = 0; i < layers; ++i)
6030 attach = &root->complex_array[layers - 1 - i];
6032 for (j = 0; j < levels; ++j)
6034 resource = wined3d_texture_get_sub_resource(wined3d_texture, i * levels + j);
6035 mip = wined3d_resource_get_parent(resource);
6036 mip_desc = &mip->surface_desc;
6038 if (j)
6039 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
6040 else
6041 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
6043 if (mip_desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
6045 mip_desc->ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
6047 switch (i)
6049 case WINED3D_CUBEMAP_FACE_POSITIVE_X:
6050 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
6051 break;
6052 case WINED3D_CUBEMAP_FACE_NEGATIVE_X:
6053 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
6054 break;
6055 case WINED3D_CUBEMAP_FACE_POSITIVE_Y:
6056 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
6057 break;
6058 case WINED3D_CUBEMAP_FACE_NEGATIVE_Y:
6059 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
6060 break;
6061 case WINED3D_CUBEMAP_FACE_POSITIVE_Z:
6062 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
6063 break;
6064 case WINED3D_CUBEMAP_FACE_NEGATIVE_Z:
6065 mip_desc->ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
6066 break;
6071 if (mip == root)
6072 continue;
6074 *attach = mip;
6075 attach = &mip->complex_array[0];
6079 if ((desc->dwFlags & DDSD_LPSURFACE) && FAILED(hr = wined3d_surface_update_desc(root->wined3d_surface,
6080 wined3d_desc.width, wined3d_desc.height, wined3d_desc.format,
6081 WINED3D_MULTISAMPLE_NONE, 0, desc->lpSurface, pitch)))
6083 ERR("Failed to set surface memory, hr %#x.\n", hr);
6084 goto fail;
6087 if (desc->dwFlags & DDSD_BACKBUFFERCOUNT)
6089 unsigned int count = desc->dwBackBufferCount;
6090 struct ddraw_surface *last = root;
6092 attach = &last->complex_array[0];
6093 for (i = 0; i < count; ++i)
6095 if (!(texture = HeapAlloc(GetProcessHeap(), 0, sizeof(*texture))))
6097 hr = E_OUTOFMEMORY;
6098 goto fail;
6101 texture->version = version;
6102 texture->surface_desc = root->surface_desc;
6103 desc = &texture->surface_desc;
6105 /* Only one surface in the flipping chain is a back buffer, one is
6106 * a front buffer, the others are just flippable surfaces. */
6107 desc->ddsCaps.dwCaps &= ~(DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER
6108 | DDSCAPS_BACKBUFFER);
6109 if (!i)
6110 desc->ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
6111 desc->dwBackBufferCount = 0;
6113 if (FAILED(hr = wined3d_texture_create(ddraw->wined3d_device, &wined3d_desc, 1,
6114 WINED3D_SURFACE_PIN_SYSMEM, texture, &ddraw_texture_wined3d_parent_ops, &wined3d_texture)))
6116 HeapFree(GetProcessHeap(), 0, texture);
6117 hr = hr_ddraw_from_wined3d(hr);
6118 goto fail;
6121 resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
6122 last = wined3d_resource_get_parent(resource);
6123 last->wined3d_texture = wined3d_texture;
6124 texture->root = last;
6126 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
6127 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTOVERLAY,
6128 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
6129 if (desc->dwFlags & DDSD_CKDESTBLT)
6130 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_DESTBLT,
6131 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
6132 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
6133 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCOVERLAY,
6134 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
6135 if (desc->dwFlags & DDSD_CKSRCBLT)
6136 wined3d_texture_set_color_key(wined3d_texture, DDCKEY_SRCBLT,
6137 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
6139 *attach = last;
6140 attach = &last->complex_array[0];
6142 *attach = root;
6145 if (surface_desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
6146 ddraw->primary = root;
6147 *surface = root;
6149 return DD_OK;
6151 fail:
6152 if (version == 7)
6153 IDirectDrawSurface7_Release(&root->IDirectDrawSurface7_iface);
6154 else if (version == 4)
6155 IDirectDrawSurface4_Release(&root->IDirectDrawSurface4_iface);
6156 else
6157 IDirectDrawSurface_Release(&root->IDirectDrawSurface_iface);
6159 return hr;
6162 void ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw, struct ddraw_texture *texture,
6163 struct wined3d_surface *wined3d_surface, const struct wined3d_parent_ops **parent_ops)
6165 DDSURFACEDESC2 *desc = &surface->surface_desc;
6166 struct wined3d_resource_desc wined3d_desc;
6167 unsigned int version = texture->version;
6169 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
6170 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
6171 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
6172 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
6173 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
6174 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
6175 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
6176 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
6177 surface->iface_count = 1;
6178 surface->version = version;
6179 surface->ddraw = ddraw;
6181 if (version == 7)
6183 surface->ref7 = 1;
6184 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface7_iface;
6186 else if (version == 4)
6188 surface->ref4 = 1;
6189 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface4_iface;
6191 else
6193 surface->ref1 = 1;
6194 surface->texture_outer = (IUnknown *)&surface->IDirectDrawSurface_iface;
6197 *desc = texture->surface_desc;
6198 wined3d_resource_get_desc(wined3d_surface_get_resource(wined3d_surface), &wined3d_desc);
6199 desc->dwWidth = wined3d_desc.width;
6200 desc->dwHeight = wined3d_desc.height;
6201 surface->first_attached = surface;
6203 if (format_is_compressed(&desc->u4.ddpfPixelFormat))
6205 if (desc->dwFlags & DDSD_LPSURFACE)
6206 desc->u1.dwLinearSize = ~0u;
6207 else
6208 desc->u1.dwLinearSize = wined3d_surface_get_pitch(wined3d_surface) * ((desc->dwHeight + 3) / 4);
6209 desc->dwFlags |= DDSD_LINEARSIZE;
6210 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH);
6212 else
6214 if (!(desc->dwFlags & DDSD_LPSURFACE))
6215 desc->u1.lPitch = wined3d_surface_get_pitch(wined3d_surface);
6216 desc->dwFlags |= DDSD_PITCH;
6217 desc->dwFlags &= ~(DDSD_LPSURFACE | DDSD_LINEARSIZE);
6219 desc->lpSurface = NULL;
6221 wined3d_surface_incref(wined3d_surface);
6222 surface->wined3d_surface = wined3d_surface;
6223 *parent_ops = &ddraw_surface_wined3d_parent_ops;
6225 wined3d_private_store_init(&surface->private_store);
6228 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
6230 struct ddraw_surface *surface = parent;
6232 /* If the surface reference count drops to zero, we release our reference
6233 * to the view, but don't clear the pointer yet, in case e.g. a
6234 * GetRenderTarget() call brings the surface back before the view is
6235 * actually destroyed. When the view is destroyed, we need to clear the
6236 * pointer, or a subsequent surface AddRef() would reference it again.
6238 * This is safe because as long as the view still has a reference to the
6239 * texture, the surface is also still alive, and we're called before the
6240 * view releases that reference. */
6241 surface->wined3d_rtv = NULL;
6244 static const struct wined3d_parent_ops ddraw_view_wined3d_parent_ops =
6246 view_wined3d_object_destroyed,
6249 struct wined3d_rendertarget_view *ddraw_surface_get_rendertarget_view(struct ddraw_surface *surface)
6251 HRESULT hr;
6253 if (surface->wined3d_rtv)
6254 return surface->wined3d_rtv;
6256 if (FAILED(hr = wined3d_rendertarget_view_create_from_surface(surface->wined3d_surface,
6257 surface, &ddraw_view_wined3d_parent_ops, &surface->wined3d_rtv)))
6259 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
6260 return NULL;
6263 return surface->wined3d_rtv;