ddraw: The texture interfaces can't be queried from version 7 surfaces.
[wine/multimedia.git] / dlls / ddraw / surface.c
blobd8c43a1781adcba56615cbc5c53ff9104dd9f1f7
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 static 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 (IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D)
202 || IsEqualGUID(riid, &IID_IDirect3DHALDevice)
203 || IsEqualGUID(riid, &IID_IDirect3DRGBDevice))
205 IDirect3DDevice7 *d3d;
206 IDirect3DDeviceImpl *device_impl;
208 /* Call into IDirect3D7 for creation */
209 IDirect3D7_CreateDevice(&This->ddraw->IDirect3D7_iface, riid, &This->IDirectDrawSurface7_iface,
210 &d3d);
212 if (d3d)
214 device_impl = impl_from_IDirect3DDevice7(d3d);
215 device_impl->from_surface = TRUE;
216 *obj = &device_impl->IDirect3DDevice_iface;
217 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
218 return S_OK;
221 WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
222 return E_NOINTERFACE;
225 if (This->version != 7)
227 if (IsEqualGUID(&IID_IDirect3DTexture2, riid))
229 IDirect3DTexture2_AddRef(&This->IDirect3DTexture2_iface);
230 *obj = &This->IDirect3DTexture2_iface;
231 return S_OK;
234 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
236 IDirect3DTexture2_AddRef(&This->IDirect3DTexture_iface);
237 *obj = &This->IDirect3DTexture_iface;
238 return S_OK;
242 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
244 return E_NOINTERFACE;
247 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
249 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
251 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
253 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
256 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
258 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
260 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
262 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
265 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
267 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
269 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
271 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
274 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
276 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
278 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
280 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
283 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
284 REFIID riid, void **object)
286 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
288 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
290 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
293 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
295 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
297 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
299 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
302 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
304 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
306 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
308 return ddraw_surface7_QueryInterface(&surface->IDirectDrawSurface7_iface, riid, object);
311 static void ddraw_surface_add_iface(struct ddraw_surface *This)
313 ULONG iface_count = InterlockedIncrement(&This->iface_count);
314 TRACE("%p increasing iface count to %u.\n", This, iface_count);
316 if (iface_count == 1)
318 wined3d_mutex_lock();
319 if (This->wined3d_surface)
320 wined3d_surface_incref(This->wined3d_surface);
321 if (This->wined3d_texture)
322 wined3d_texture_incref(This->wined3d_texture);
323 wined3d_mutex_unlock();
327 /*****************************************************************************
328 * IDirectDrawSurface7::AddRef
330 * A normal addref implementation
332 * Returns:
333 * The new refcount
335 *****************************************************************************/
336 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
338 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
339 ULONG refcount = InterlockedIncrement(&This->ref7);
341 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
343 if (refcount == 1)
345 ddraw_surface_add_iface(This);
348 return refcount;
351 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
353 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
354 ULONG refcount = InterlockedIncrement(&This->ref4);
356 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
358 if (refcount == 1)
360 ddraw_surface_add_iface(This);
363 return refcount;
366 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
368 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
369 ULONG refcount = InterlockedIncrement(&This->ref3);
371 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
373 if (refcount == 1)
375 ddraw_surface_add_iface(This);
378 return refcount;
381 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
383 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
384 ULONG refcount = InterlockedIncrement(&This->ref2);
386 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
388 if (refcount == 1)
390 ddraw_surface_add_iface(This);
393 return refcount;
396 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
398 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
399 ULONG refcount = InterlockedIncrement(&This->ref1);
401 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
403 if (refcount == 1)
405 ddraw_surface_add_iface(This);
408 return refcount;
411 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
413 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
414 ULONG refcount = InterlockedIncrement(&This->gamma_count);
416 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
418 if (refcount == 1)
420 ddraw_surface_add_iface(This);
423 return refcount;
426 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
428 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
430 TRACE("iface %p.\n", iface);
432 return ddraw_surface1_AddRef(&surface->IDirectDrawSurface_iface);
435 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
437 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
439 TRACE("iface %p.\n", iface);
441 return ddraw_surface1_AddRef(&surface->IDirectDrawSurface_iface);
444 /*****************************************************************************
445 * ddraw_surface_destroy
447 * A helper function for IDirectDrawSurface7::Release
449 * Frees the surface, regardless of its refcount.
450 * See IDirectDrawSurface7::Release for more information
452 * Params:
453 * This: Surface to free
455 *****************************************************************************/
456 static void ddraw_surface_destroy(struct ddraw_surface *This)
458 TRACE("surface %p.\n", This);
460 /* Check the iface count and give a warning */
461 if(This->iface_count > 1)
463 /* This can happen when a complex surface is destroyed,
464 * because the 2nd surface was addref()ed when the app
465 * called GetAttachedSurface
467 WARN("(%p): Destroying surface with refcounts 7: %d 4: %d 3: %d 2: %d 1: %d\n",
468 This, This->ref7, This->ref4, This->ref3, This->ref2, This->ref1);
471 if (This->wined3d_surface)
472 wined3d_surface_decref(This->wined3d_surface);
475 static void ddraw_surface_cleanup(struct ddraw_surface *surface)
477 struct ddraw_surface *surf;
478 IUnknown *ifaceToRelease;
479 UINT i;
481 TRACE("surface %p.\n", surface);
483 /* The refcount test shows that the palette is detached when the surface
484 * is destroyed. */
485 IDirectDrawSurface7_SetPalette(&surface->IDirectDrawSurface7_iface, NULL);
487 /* Loop through all complex attached surfaces and destroy them.
489 * Yet again, only the root can have more than one complexly attached
490 * surface, all the others have a total of one. */
491 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
493 if (!surface->complex_array[i])
494 break;
496 surf = surface->complex_array[i];
497 surface->complex_array[i] = NULL;
498 while (surf)
500 struct ddraw_surface *destroy = surf;
501 surf = surf->complex_array[0]; /* Iterate through the "tree" */
502 ddraw_surface_destroy(destroy); /* Destroy it */
506 ifaceToRelease = surface->ifaceToRelease;
508 /* Destroy the root surface. */
509 ddraw_surface_destroy(surface);
511 /* Reduce the ddraw refcount */
512 if (ifaceToRelease)
513 IUnknown_Release(ifaceToRelease);
516 ULONG ddraw_surface_release_iface(struct ddraw_surface *This)
518 ULONG iface_count = InterlockedDecrement(&This->iface_count);
519 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
521 if (iface_count == 0)
523 /* Complex attached surfaces are destroyed implicitly when the root is released */
524 wined3d_mutex_lock();
525 if(!This->is_complex_root)
527 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
528 wined3d_mutex_unlock();
529 return iface_count;
531 if (This->wined3d_texture) /* If it's a texture, destroy the wined3d texture. */
532 wined3d_texture_decref(This->wined3d_texture);
533 else
534 ddraw_surface_cleanup(This);
535 wined3d_mutex_unlock();
538 return iface_count;
541 /*****************************************************************************
542 * IDirectDrawSurface7::Release
544 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
545 * surface is destroyed.
547 * Destroying the surface is a bit tricky. For the connection between
548 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
549 * It has a nice graph explaining the connection.
551 * What happens here is basically this:
552 * When a surface is destroyed, its WineD3DSurface is released,
553 * and the refcount of the DirectDraw interface is reduced by 1. If it has
554 * complex surfaces attached to it, then these surfaces are destroyed too,
555 * regardless of their refcount. If any surface being destroyed has another
556 * surface attached to it (with a "soft" attachment, not complex), then
557 * this surface is detached with DeleteAttachedSurface.
559 * When the surface is a texture, the WineD3DTexture is released.
560 * If the surface is the Direct3D render target, then the D3D
561 * capabilities of the WineD3DDevice are uninitialized, which causes the
562 * swapchain to be released.
564 * When a complex sublevel falls to ref zero, then this is ignored.
566 * Returns:
567 * The new refcount
569 *****************************************************************************/
570 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
572 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
573 ULONG refcount = InterlockedDecrement(&This->ref7);
575 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
577 if (refcount == 0)
579 ddraw_surface_release_iface(This);
582 return refcount;
585 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
587 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
588 ULONG refcount = InterlockedDecrement(&This->ref4);
590 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
592 if (refcount == 0)
594 ddraw_surface_release_iface(This);
597 return refcount;
600 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
602 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
603 ULONG refcount = InterlockedDecrement(&This->ref3);
605 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
607 if (refcount == 0)
609 ddraw_surface_release_iface(This);
612 return refcount;
615 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
617 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
618 ULONG refcount = InterlockedDecrement(&This->ref2);
620 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
622 if (refcount == 0)
624 ddraw_surface_release_iface(This);
627 return refcount;
630 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
632 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
633 ULONG refcount = InterlockedDecrement(&This->ref1);
635 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
637 if (refcount == 0)
639 ddraw_surface_release_iface(This);
642 return refcount;
645 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
647 struct ddraw_surface *This = impl_from_IDirectDrawGammaControl(iface);
648 ULONG refcount = InterlockedDecrement(&This->gamma_count);
650 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
652 if (refcount == 0)
654 ddraw_surface_release_iface(This);
657 return refcount;
660 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
662 struct ddraw_surface *This = impl_from_IDirect3DTexture2(iface);
663 TRACE("iface %p.\n", iface);
665 return ddraw_surface1_Release(&This->IDirectDrawSurface_iface);
668 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
670 struct ddraw_surface *This = impl_from_IDirect3DTexture(iface);
671 TRACE("iface %p.\n", iface);
673 return ddraw_surface1_Release(&This->IDirectDrawSurface_iface);
676 /*****************************************************************************
677 * IDirectDrawSurface7::GetAttachedSurface
679 * Returns an attached surface with the requested caps. Surface attachment
680 * and complex surfaces are not clearly described by the MSDN or sdk,
681 * so this method is tricky and likely to contain problems.
682 * This implementation searches the complex list first, then the
683 * attachment chain.
685 * The chains are searched from This down to the last surface in the chain,
686 * not from the first element in the chain. The first surface found is
687 * returned. The MSDN says that this method fails if more than one surface
688 * matches the caps, but it is not sure if that is right. The attachment
689 * structure may not even allow two matching surfaces.
691 * The found surface is AddRef-ed before it is returned.
693 * Params:
694 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
695 * Surface: Address to store the found surface
697 * Returns:
698 * DD_OK on success
699 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
700 * DDERR_NOTFOUND if no surface was found
702 *****************************************************************************/
703 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
704 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
706 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
707 struct ddraw_surface *surf;
708 DDSCAPS2 our_caps;
709 int i;
711 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
713 wined3d_mutex_lock();
715 if(This->version < 7)
717 /* Earlier dx apps put garbage into these members, clear them */
718 our_caps.dwCaps = Caps->dwCaps;
719 our_caps.dwCaps2 = 0;
720 our_caps.dwCaps3 = 0;
721 our_caps.dwCaps4 = 0;
723 else
725 our_caps = *Caps;
728 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 */
730 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
732 surf = This->complex_array[i];
733 if(!surf) break;
735 if (TRACE_ON(ddraw))
737 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
738 surf->surface_desc.ddsCaps.dwCaps,
739 surf->surface_desc.ddsCaps.dwCaps2,
740 surf->surface_desc.ddsCaps.dwCaps3,
741 surf->surface_desc.ddsCaps.dwCaps4);
744 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
745 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
747 /* MSDN: "This method fails if more than one surface is attached
748 * that matches the capabilities requested."
750 * Not sure how to test this.
753 TRACE("(%p): Returning surface %p\n", This, surf);
754 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
755 *Surface = &surf->IDirectDrawSurface7_iface;
756 ddraw_surface7_AddRef(*Surface);
757 wined3d_mutex_unlock();
759 return DD_OK;
763 /* Next, look at the attachment chain */
764 surf = This;
766 while( (surf = surf->next_attached) )
768 if (TRACE_ON(ddraw))
770 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
771 surf->surface_desc.ddsCaps.dwCaps,
772 surf->surface_desc.ddsCaps.dwCaps2,
773 surf->surface_desc.ddsCaps.dwCaps3,
774 surf->surface_desc.ddsCaps.dwCaps4);
777 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
778 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
780 TRACE("(%p): Returning surface %p\n", This, surf);
781 *Surface = &surf->IDirectDrawSurface7_iface;
782 ddraw_surface7_AddRef(*Surface);
783 wined3d_mutex_unlock();
784 return DD_OK;
788 TRACE("(%p) Didn't find a valid surface\n", This);
790 wined3d_mutex_unlock();
792 *Surface = NULL;
793 return DDERR_NOTFOUND;
796 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
797 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
799 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
800 struct ddraw_surface *attachment_impl;
801 IDirectDrawSurface7 *attachment7;
802 HRESULT hr;
804 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
806 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
807 caps, &attachment7);
808 if (FAILED(hr))
810 *attachment = NULL;
811 return hr;
813 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
814 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
815 ddraw_surface4_AddRef(*attachment);
816 ddraw_surface7_Release(attachment7);
818 return hr;
821 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
822 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
824 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
825 struct ddraw_surface *attachment_impl;
826 IDirectDrawSurface7 *attachment7;
827 DDSCAPS2 caps2;
828 HRESULT hr;
830 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
832 caps2.dwCaps = caps->dwCaps;
833 caps2.dwCaps2 = 0;
834 caps2.dwCaps3 = 0;
835 caps2.dwCaps4 = 0;
837 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
838 &caps2, &attachment7);
839 if (FAILED(hr))
841 *attachment = NULL;
842 return hr;
844 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
845 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
846 ddraw_surface3_AddRef(*attachment);
847 ddraw_surface7_Release(attachment7);
849 return hr;
852 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
853 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
855 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
856 struct ddraw_surface *attachment_impl;
857 IDirectDrawSurface7 *attachment7;
858 DDSCAPS2 caps2;
859 HRESULT hr;
861 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
863 caps2.dwCaps = caps->dwCaps;
864 caps2.dwCaps2 = 0;
865 caps2.dwCaps3 = 0;
866 caps2.dwCaps4 = 0;
868 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
869 &caps2, &attachment7);
870 if (FAILED(hr))
872 *attachment = NULL;
873 return hr;
875 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
876 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
877 ddraw_surface2_AddRef(*attachment);
878 ddraw_surface7_Release(attachment7);
880 return hr;
883 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
884 DDSCAPS *caps, IDirectDrawSurface **attachment)
886 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
887 struct ddraw_surface *attachment_impl;
888 IDirectDrawSurface7 *attachment7;
889 DDSCAPS2 caps2;
890 HRESULT hr;
892 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
894 caps2.dwCaps = caps->dwCaps;
895 caps2.dwCaps2 = 0;
896 caps2.dwCaps3 = 0;
897 caps2.dwCaps4 = 0;
899 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface,
900 &caps2, &attachment7);
901 if (FAILED(hr))
903 *attachment = NULL;
904 return hr;
906 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
907 *attachment = &attachment_impl->IDirectDrawSurface_iface;
908 ddraw_surface1_AddRef(*attachment);
909 ddraw_surface7_Release(attachment7);
911 return hr;
914 /*****************************************************************************
915 * IDirectDrawSurface7::Lock
917 * Locks the surface and returns a pointer to the surface's memory
919 * Params:
920 * Rect: Rectangle to lock. If NULL, the whole surface is locked
921 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
922 * Flags: Locking flags, e.g Read only or write only
923 * h: An event handle that's not used and must be NULL
925 * Returns:
926 * DD_OK on success
927 * DDERR_INVALIDPARAMS if DDSD is NULL
928 * For more details, see IWineD3DSurface::LockRect
930 *****************************************************************************/
931 static HRESULT surface_lock(struct ddraw_surface *This,
932 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
934 struct wined3d_mapped_rect mapped_rect;
935 HRESULT hr = DD_OK;
937 TRACE("This %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
938 This, wine_dbgstr_rect(Rect), DDSD, Flags, h);
940 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
941 wined3d_mutex_lock();
943 /* Should I check for the handle to be NULL?
945 * The DDLOCK flags and the D3DLOCK flags are equal
946 * for the supported values. The others are ignored by WineD3D
949 /* Windows zeroes this if the rect is invalid */
950 DDSD->lpSurface = 0;
952 if (Rect)
954 if ((Rect->left < 0)
955 || (Rect->top < 0)
956 || (Rect->left > Rect->right)
957 || (Rect->top > Rect->bottom)
958 || (Rect->right > This->surface_desc.dwWidth)
959 || (Rect->bottom > This->surface_desc.dwHeight))
961 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
962 wined3d_mutex_unlock();
963 return DDERR_INVALIDPARAMS;
967 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
968 hr = ddraw_surface_update_frontbuffer(This, Rect, TRUE);
969 if (SUCCEEDED(hr))
970 hr = wined3d_surface_map(This->wined3d_surface, &mapped_rect, Rect, Flags);
971 if (FAILED(hr))
973 wined3d_mutex_unlock();
974 switch(hr)
976 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
977 * specific error. But since IWineD3DSurface::LockRect returns that error in this
978 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
979 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
980 * is much easier to do it in one place in ddraw
982 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
983 default: return hr;
987 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
989 if (Flags & DDLOCK_READONLY)
990 memset(&This->ddraw->primary_lock, 0, sizeof(This->ddraw->primary_lock));
991 else if (Rect)
992 This->ddraw->primary_lock = *Rect;
993 else
994 SetRect(&This->ddraw->primary_lock, 0, 0, This->surface_desc.dwWidth, This->surface_desc.dwHeight);
997 /* Override the memory area. The pitch should be set already. Strangely windows
998 * does not set the LPSURFACE flag on locked surfaces !?!.
999 * DDSD->dwFlags |= DDSD_LPSURFACE;
1001 This->surface_desc.lpSurface = mapped_rect.data;
1002 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
1004 TRACE("locked surface returning description :\n");
1005 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1007 wined3d_mutex_unlock();
1009 return DD_OK;
1012 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1013 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1015 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1017 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1018 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1020 if (!surface_desc) return DDERR_INVALIDPARAMS;
1021 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1022 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1024 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1025 return DDERR_INVALIDPARAMS;
1027 return surface_lock(surface, rect, surface_desc, flags, h);
1030 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1031 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1033 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(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_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1049 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1051 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1052 DDSURFACEDESC2 surface_desc2;
1053 HRESULT hr;
1055 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1056 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1058 if (!surface_desc) return DDERR_INVALIDPARAMS;
1059 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1060 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1062 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1063 return DDERR_INVALIDPARAMS;
1066 surface_desc2.dwSize = surface_desc->dwSize;
1067 surface_desc2.dwFlags = 0;
1068 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1069 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1070 surface_desc->dwSize = surface_desc2.dwSize;
1071 return hr;
1074 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1075 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1077 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1078 DDSURFACEDESC2 surface_desc2;
1079 HRESULT hr;
1081 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1082 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1084 if (!surface_desc) return DDERR_INVALIDPARAMS;
1085 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1086 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1088 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1089 return DDERR_INVALIDPARAMS;
1092 surface_desc2.dwSize = surface_desc->dwSize;
1093 surface_desc2.dwFlags = 0;
1094 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1095 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1096 surface_desc->dwSize = surface_desc2.dwSize;
1097 return hr;
1100 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1101 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1103 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1104 DDSURFACEDESC2 surface_desc2;
1105 HRESULT hr;
1106 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1107 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1109 if (!surface_desc) return DDERR_INVALIDPARAMS;
1110 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1111 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1113 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1114 return DDERR_INVALIDPARAMS;
1117 surface_desc2.dwSize = surface_desc->dwSize;
1118 surface_desc2.dwFlags = 0;
1119 hr = surface_lock(surface, rect, &surface_desc2, flags, h);
1120 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1121 surface_desc->dwSize = surface_desc2.dwSize;
1122 return hr;
1125 /*****************************************************************************
1126 * IDirectDrawSurface7::Unlock
1128 * Unlocks an locked surface
1130 * Params:
1131 * Rect: Not used by this implementation
1133 * Returns:
1134 * D3D_OK on success
1135 * For more details, see IWineD3DSurface::UnlockRect
1137 *****************************************************************************/
1138 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
1140 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1141 HRESULT hr;
1143 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1145 wined3d_mutex_lock();
1146 hr = wined3d_surface_unmap(surface->wined3d_surface);
1147 if (SUCCEEDED(hr))
1149 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1150 hr = ddraw_surface_update_frontbuffer(surface, &surface->ddraw->primary_lock, FALSE);
1151 surface->surface_desc.lpSurface = NULL;
1153 wined3d_mutex_unlock();
1155 return hr;
1158 static HRESULT WINAPI ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1160 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1162 TRACE("iface %p, rect %p.\n", iface, pRect);
1164 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, pRect);
1167 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1169 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1171 TRACE("iface %p, data %p.\n", iface, data);
1173 /* data might not be the LPRECT of later versions, so drop it. */
1174 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1177 static HRESULT WINAPI ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1179 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1181 TRACE("iface %p, data %p.\n", iface, data);
1183 /* data might not be the LPRECT of later versions, so drop it. */
1184 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1187 static HRESULT WINAPI ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1189 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1191 TRACE("iface %p, data %p.\n", iface, data);
1193 /* data might not be the LPRECT of later versions, so drop it. */
1194 return ddraw_surface7_Unlock(&surface->IDirectDrawSurface7_iface, NULL);
1197 /*****************************************************************************
1198 * IDirectDrawSurface7::Flip
1200 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
1201 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
1202 * the flip target is passed to WineD3D, even if the app didn't specify one
1204 * Params:
1205 * DestOverride: Specifies the surface that will become the new front
1206 * buffer. If NULL, the current back buffer is used
1207 * Flags: some DirectDraw flags, see include/ddraw.h
1209 * Returns:
1210 * DD_OK on success
1211 * DDERR_NOTFLIPPABLE if no flip target could be found
1212 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
1213 * For more details, see IWineD3DSurface::Flip
1215 *****************************************************************************/
1216 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
1218 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1219 struct ddraw_surface *Override = unsafe_impl_from_IDirectDrawSurface7(DestOverride);
1220 IDirectDrawSurface7 *Override7;
1221 HRESULT hr;
1223 TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
1225 /* Flip has to be called from a front buffer
1226 * What about overlay surfaces, AFAIK they can flip too? */
1227 if (!(surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)))
1228 return DDERR_INVALIDOBJECT; /* Unchecked */
1230 wined3d_mutex_lock();
1232 /* WineD3D doesn't keep track of attached surface, so find the target */
1233 if(!Override)
1235 DDSCAPS2 Caps;
1237 memset(&Caps, 0, sizeof(Caps));
1238 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
1239 hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
1240 if(hr != DD_OK)
1242 ERR("Can't find a flip target\n");
1243 wined3d_mutex_unlock();
1244 return DDERR_NOTFLIPPABLE; /* Unchecked */
1246 Override = impl_from_IDirectDrawSurface7(Override7);
1248 /* For the GetAttachedSurface */
1249 ddraw_surface7_Release(Override7);
1252 hr = wined3d_surface_flip(surface->wined3d_surface, Override->wined3d_surface, Flags);
1253 if (SUCCEEDED(hr) && surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1254 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
1256 wined3d_mutex_unlock();
1258 return hr;
1261 static HRESULT WINAPI ddraw_surface4_Flip(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *dst, DWORD flags)
1263 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1264 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1266 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1268 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1269 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1272 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
1274 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1275 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1277 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1279 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1280 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1283 static HRESULT WINAPI ddraw_surface2_Flip(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *dst, DWORD flags)
1285 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1286 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1288 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1290 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1291 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1294 static HRESULT WINAPI ddraw_surface1_Flip(IDirectDrawSurface *iface, IDirectDrawSurface *dst, DWORD flags)
1296 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1297 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1299 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1301 return ddraw_surface7_Flip(&surface->IDirectDrawSurface7_iface,
1302 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1305 static HRESULT ddraw_surface_blt_clipped(struct ddraw_surface *dst_surface, const RECT *dst_rect_in,
1306 struct ddraw_surface *src_surface, const RECT *src_rect_in, DWORD flags,
1307 const WINEDDBLTFX *fx, enum wined3d_texture_filter_type filter)
1309 struct wined3d_surface *wined3d_src_surface = src_surface ? src_surface->wined3d_surface : NULL;
1310 RECT src_rect, dst_rect;
1311 float scale_x, scale_y;
1312 const RECT *clip_rect;
1313 UINT clip_list_size;
1314 RGNDATA *clip_list;
1315 HRESULT hr = DD_OK;
1316 UINT i;
1318 if (!dst_surface->clipper)
1320 if (src_surface && src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1321 hr = ddraw_surface_update_frontbuffer(src_surface, src_rect_in, TRUE);
1322 if (SUCCEEDED(hr))
1323 hr = wined3d_surface_blt(dst_surface->wined3d_surface, dst_rect_in,
1324 wined3d_src_surface, src_rect_in, flags, fx, filter);
1325 if (SUCCEEDED(hr) && (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER))
1326 hr = ddraw_surface_update_frontbuffer(dst_surface, dst_rect_in, FALSE);
1328 return hr;
1331 if (!dst_rect_in)
1333 dst_rect.left = 0;
1334 dst_rect.top = 0;
1335 dst_rect.right = dst_surface->surface_desc.dwWidth;
1336 dst_rect.bottom = dst_surface->surface_desc.dwHeight;
1338 else
1340 dst_rect = *dst_rect_in;
1343 if (IsRectEmpty(&dst_rect))
1344 return DDERR_INVALIDRECT;
1346 if (src_surface)
1348 if (!src_rect_in)
1350 src_rect.left = 0;
1351 src_rect.top = 0;
1352 src_rect.right = src_surface->surface_desc.dwWidth;
1353 src_rect.bottom = src_surface->surface_desc.dwHeight;
1355 else
1357 src_rect = *src_rect_in;
1360 if (IsRectEmpty(&src_rect))
1361 return DDERR_INVALIDRECT;
1363 else
1365 SetRect(&src_rect, 0, 0, 0, 0);
1368 scale_x = (float)(src_rect.right - src_rect.left) / (float)(dst_rect.right - dst_rect.left);
1369 scale_y = (float)(src_rect.bottom - src_rect.top) / (float)(dst_rect.bottom - dst_rect.top);
1371 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1372 &dst_rect, NULL, &clip_list_size)))
1374 WARN("Failed to get clip list size, hr %#x.\n", hr);
1375 return hr;
1378 if (!(clip_list = HeapAlloc(GetProcessHeap(), 0, clip_list_size)))
1380 WARN("Failed to allocate clip list.\n");
1381 return E_OUTOFMEMORY;
1384 if (FAILED(hr = IDirectDrawClipper_GetClipList(&dst_surface->clipper->IDirectDrawClipper_iface,
1385 &dst_rect, clip_list, &clip_list_size)))
1387 WARN("Failed to get clip list, hr %#x.\n", hr);
1388 HeapFree(GetProcessHeap(), 0, clip_list);
1389 return hr;
1392 clip_rect = (RECT *)clip_list->Buffer;
1393 for (i = 0; i < clip_list->rdh.nCount; ++i)
1395 RECT src_rect_clipped = src_rect;
1397 if (src_surface)
1399 src_rect_clipped.left += (LONG)((clip_rect[i].left - dst_rect.left) * scale_x);
1400 src_rect_clipped.top += (LONG)((clip_rect[i].top - dst_rect.top) * scale_y);
1401 src_rect_clipped.right -= (LONG)((dst_rect.right - clip_rect[i].right) * scale_x);
1402 src_rect_clipped.bottom -= (LONG)((dst_rect.bottom - clip_rect[i].bottom) * scale_y);
1404 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1406 if (FAILED(hr = ddraw_surface_update_frontbuffer(src_surface, &src_rect_clipped, TRUE)))
1407 break;
1411 if (FAILED(hr = wined3d_surface_blt(dst_surface->wined3d_surface, &clip_rect[i],
1412 wined3d_src_surface, &src_rect_clipped, flags, fx, filter)))
1413 break;
1415 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1417 if (FAILED(hr = ddraw_surface_update_frontbuffer(dst_surface, &clip_rect[i], FALSE)))
1418 break;
1422 HeapFree(GetProcessHeap(), 0, clip_list);
1423 return hr;
1426 /*****************************************************************************
1427 * IDirectDrawSurface7::Blt
1429 * Performs a blit on the surface
1431 * Params:
1432 * DestRect: Destination rectangle, can be NULL
1433 * SrcSurface: Source surface, can be NULL
1434 * SrcRect: Source rectangle, can be NULL
1435 * Flags: Blt flags
1436 * DDBltFx: Some extended blt parameters, connected to the flags
1438 * Returns:
1439 * D3D_OK on success
1440 * See IWineD3DSurface::Blt for more details
1442 *****************************************************************************/
1443 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1444 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1446 struct ddraw_surface *dst_surface = impl_from_IDirectDrawSurface7(iface);
1447 struct ddraw_surface *src_surface = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1448 HRESULT hr = DD_OK;
1450 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1451 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1453 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1454 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1456 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1457 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1458 return DDERR_INVALIDPARAMS;
1461 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1462 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1463 return DDERR_INVALIDPARAMS;
1466 wined3d_mutex_lock();
1468 if (Flags & DDBLT_KEYSRC && (!src_surface || !(src_surface->surface_desc.dwFlags & DDSD_CKSRCBLT)))
1470 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1471 wined3d_mutex_unlock();
1472 return DDERR_INVALIDPARAMS;
1475 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1476 * does, copy the struct, and replace the ddraw surfaces with the wined3d
1477 * surfaces. So far no blitting operations using surfaces in the bltfx
1478 * struct are supported anyway. */
1479 hr = ddraw_surface_blt_clipped(dst_surface, DestRect, src_surface, SrcRect,
1480 Flags, (WINEDDBLTFX *)DDBltFx, WINED3D_TEXF_LINEAR);
1482 wined3d_mutex_unlock();
1483 switch(hr)
1485 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1486 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
1487 default: return hr;
1491 static HRESULT WINAPI ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1492 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1494 struct ddraw_surface *dst = impl_from_IDirectDrawSurface4(iface);
1495 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1497 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1498 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1500 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1501 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1504 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1505 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1507 struct ddraw_surface *dst = impl_from_IDirectDrawSurface3(iface);
1508 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1510 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1511 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1513 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1514 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1517 static HRESULT WINAPI ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1518 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1520 struct ddraw_surface *dst = impl_from_IDirectDrawSurface2(iface);
1521 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1523 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1524 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1526 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1527 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1530 static HRESULT WINAPI ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1531 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1533 struct ddraw_surface *dst = impl_from_IDirectDrawSurface(iface);
1534 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1536 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1537 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1539 return ddraw_surface7_Blt(&dst->IDirectDrawSurface7_iface, dst_rect,
1540 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1543 /*****************************************************************************
1544 * IDirectDrawSurface7::AddAttachedSurface
1546 * Attaches a surface to another surface. How the surface attachments work
1547 * is not totally understood yet, and this method is prone to problems.
1548 * The surface that is attached is AddRef-ed.
1550 * Tests with complex surfaces suggest that the surface attachments form a
1551 * tree, but no method to test this has been found yet.
1553 * The attachment list consists of a first surface (first_attached) and
1554 * for each surface a pointer to the next attached surface (next_attached).
1555 * For the first surface, and a surface that has no attachments
1556 * first_attached points to the surface itself. A surface that has
1557 * no successors in the chain has next_attached set to NULL.
1559 * Newly attached surfaces are attached right after the root surface.
1560 * If a surface is attached to a complex surface compound, it's attached to
1561 * the surface that the app requested, not the complex root. See
1562 * GetAttachedSurface for a description how surfaces are found.
1564 * This is how the current implementation works, and it was coded by looking
1565 * at the needs of the applications.
1567 * So far only Z-Buffer attachments are tested, and they are activated in
1568 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1569 * Back buffers should work in 2D mode, but they are not tested(They can be
1570 * attached in older iface versions). Rendering to the front buffer and
1571 * switching between that and double buffering is not yet implemented in
1572 * WineD3D, so for 3D it might have unexpected results.
1574 * ddraw_surface_attach_surface is the real thing,
1575 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1576 * performs additional checks. Version 7 of this interface is much more restrictive
1577 * than its predecessors.
1579 * Params:
1580 * Attach: Surface to attach to iface
1582 * Returns:
1583 * DD_OK on success
1584 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1586 *****************************************************************************/
1587 static HRESULT ddraw_surface_attach_surface(struct ddraw_surface *This, struct ddraw_surface *Surf)
1589 TRACE("surface %p, attachment %p.\n", This, Surf);
1591 if(Surf == This)
1592 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1594 wined3d_mutex_lock();
1596 /* Check if the surface is already attached somewhere */
1597 if (Surf->next_attached || Surf->first_attached != Surf)
1599 /* TODO: Test for the structure of the manual attachment. Is it a
1600 * chain or a list? What happens if one surface is attached to 2
1601 * different surfaces? */
1602 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1603 Surf, Surf->next_attached, Surf->first_attached);
1605 wined3d_mutex_unlock();
1606 return DDERR_SURFACEALREADYATTACHED;
1609 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1610 Surf->next_attached = This->next_attached;
1611 Surf->first_attached = This->first_attached;
1612 This->next_attached = Surf;
1614 /* Check if the WineD3D depth stencil needs updating */
1615 if(This->ddraw->d3ddevice)
1617 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1620 wined3d_mutex_unlock();
1622 return DD_OK;
1625 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
1627 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
1628 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1629 HRESULT hr;
1631 TRACE("iface %p, attachment %p.\n", iface, attachment);
1633 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1634 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1637 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1638 attachment_impl->surface_desc.ddsCaps.dwCaps);
1639 return DDERR_CANNOTATTACHSURFACE;
1642 hr = ddraw_surface_attach_surface(This, attachment_impl);
1643 if (FAILED(hr))
1645 return hr;
1647 attachment_impl->attached_iface = (IUnknown *)attachment;
1648 IUnknown_AddRef(attachment_impl->attached_iface);
1649 return hr;
1652 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1654 struct ddraw_surface *This = impl_from_IDirectDrawSurface4(iface);
1655 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1656 HRESULT hr;
1658 TRACE("iface %p, attachment %p.\n", iface, attachment);
1660 hr = ddraw_surface7_AddAttachedSurface(&This->IDirectDrawSurface7_iface,
1661 attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1662 if (FAILED(hr))
1664 return hr;
1666 attachment_impl->attached_iface = (IUnknown *)attachment;
1667 IUnknown_AddRef(attachment_impl->attached_iface);
1668 ddraw_surface7_Release(&attachment_impl->IDirectDrawSurface7_iface);
1669 return hr;
1671 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1673 struct ddraw_surface *This = impl_from_IDirectDrawSurface3(iface);
1674 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1675 HRESULT hr;
1677 TRACE("iface %p, attachment %p.\n", iface, attachment);
1679 /* Tests suggest that
1680 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1681 * -> offscreen plain surfaces can be attached to primaries
1682 * -> primaries can be attached to offscreen plain surfaces
1683 * -> z buffers can be attached to primaries */
1684 if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1685 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1687 /* Sizes have to match */
1688 if (attachment_impl->surface_desc.dwWidth != This->surface_desc.dwWidth
1689 || attachment_impl->surface_desc.dwHeight != This->surface_desc.dwHeight)
1691 WARN("Surface sizes do not match.\n");
1692 return DDERR_CANNOTATTACHSURFACE;
1694 /* OK */
1696 else if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1697 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1699 /* OK */
1701 else
1703 WARN("Invalid attachment combination.\n");
1704 return DDERR_CANNOTATTACHSURFACE;
1707 hr = ddraw_surface_attach_surface(This, attachment_impl);
1708 if (FAILED(hr))
1710 return hr;
1712 attachment_impl->attached_iface = (IUnknown *)attachment;
1713 IUnknown_AddRef(attachment_impl->attached_iface);
1714 return hr;
1717 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1719 struct ddraw_surface *This = impl_from_IDirectDrawSurface2(iface);
1720 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1721 HRESULT hr;
1723 TRACE("iface %p, attachment %p.\n", iface, attachment);
1725 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1726 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1727 if (FAILED(hr))
1729 return hr;
1731 attachment_impl->attached_iface = (IUnknown *)attachment;
1732 IUnknown_AddRef(attachment_impl->attached_iface);
1733 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1734 return hr;
1737 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1739 struct ddraw_surface *This = impl_from_IDirectDrawSurface(iface);
1740 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1741 HRESULT hr;
1743 TRACE("iface %p, attachment %p.\n", iface, attachment);
1745 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1746 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1747 if (FAILED(hr))
1749 return hr;
1751 attachment_impl->attached_iface = (IUnknown *)attachment;
1752 IUnknown_AddRef(attachment_impl->attached_iface);
1753 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1754 return hr;
1757 /*****************************************************************************
1758 * IDirectDrawSurface7::DeleteAttachedSurface
1760 * Removes a surface from the attachment chain. The surface's refcount
1761 * is decreased by one after it has been removed
1763 * Params:
1764 * Flags: Some flags, not used by this implementation
1765 * Attach: Surface to detach
1767 * Returns:
1768 * DD_OK on success
1769 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1771 *****************************************************************************/
1772 static HRESULT ddraw_surface_delete_attached_surface(struct ddraw_surface *surface,
1773 struct ddraw_surface *attachment, IUnknown *detach_iface)
1775 struct ddraw_surface *prev = surface;
1777 TRACE("surface %p, attachment %p, detach_iface %p.\n", surface, attachment, detach_iface);
1779 wined3d_mutex_lock();
1780 if (!attachment || (attachment->first_attached != surface) || (attachment == surface) )
1782 wined3d_mutex_unlock();
1783 return DDERR_CANNOTDETACHSURFACE;
1786 if (attachment->attached_iface != detach_iface)
1788 WARN("attachment->attach_iface %p != detach_iface %p.\n", attachment->attached_iface, detach_iface);
1789 wined3d_mutex_unlock();
1790 return DDERR_SURFACENOTATTACHED;
1793 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1794 if (surface->surface_desc.ddsCaps.dwCaps & attachment->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1796 attachment->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1797 /* FIXME: we should probably also subtract from dwMipMapCount of this
1798 * and all parent surfaces */
1801 /* Find the predecessor of the detached surface */
1802 while (prev)
1804 if (prev->next_attached == attachment)
1805 break;
1806 prev = prev->next_attached;
1809 /* There must be a surface, otherwise there's a bug */
1810 assert(prev);
1812 /* Unchain the surface */
1813 prev->next_attached = attachment->next_attached;
1814 attachment->next_attached = NULL;
1815 attachment->first_attached = attachment;
1817 /* Check if the wined3d depth stencil needs updating. */
1818 if (surface->ddraw->d3ddevice)
1819 IDirect3DDeviceImpl_UpdateDepthStencil(surface->ddraw->d3ddevice);
1820 wined3d_mutex_unlock();
1822 /* Set attached_iface to NULL before releasing it, the surface may go
1823 * away. */
1824 attachment->attached_iface = NULL;
1825 IUnknown_Release(detach_iface);
1827 return DD_OK;
1830 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1831 DWORD flags, IDirectDrawSurface7 *attachment)
1833 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1834 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1836 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1838 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1841 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1842 DWORD flags, IDirectDrawSurface4 *attachment)
1844 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1845 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1847 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1849 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1852 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1853 DWORD flags, IDirectDrawSurface3 *attachment)
1855 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1856 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1858 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1860 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1863 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
1864 DWORD flags, IDirectDrawSurface2 *attachment)
1866 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1867 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1869 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1871 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1874 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
1875 DWORD flags, IDirectDrawSurface *attachment)
1877 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1878 struct ddraw_surface *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1880 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1882 return ddraw_surface_delete_attached_surface(surface, attachment_impl, (IUnknown *)attachment);
1885 /*****************************************************************************
1886 * IDirectDrawSurface7::AddOverlayDirtyRect
1888 * "This method is not currently implemented"
1890 * Params:
1891 * Rect: ?
1893 * Returns:
1894 * DDERR_UNSUPPORTED
1896 *****************************************************************************/
1897 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1899 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1901 return DDERR_UNSUPPORTED; /* unchecked */
1904 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
1906 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1908 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1910 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1913 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1915 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1917 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1919 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1922 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
1924 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
1926 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1928 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1931 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
1933 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
1935 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1937 return ddraw_surface7_AddOverlayDirtyRect(&surface->IDirectDrawSurface7_iface, rect);
1940 /*****************************************************************************
1941 * IDirectDrawSurface7::GetDC
1943 * Returns a GDI device context for the surface
1945 * Params:
1946 * hdc: Address of a HDC variable to store the dc to
1948 * Returns:
1949 * DD_OK on success
1950 * DDERR_INVALIDPARAMS if hdc is NULL
1951 * For details, see IWineD3DSurface::GetDC
1953 *****************************************************************************/
1954 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1956 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
1957 HRESULT hr = DD_OK;
1959 TRACE("iface %p, dc %p.\n", iface, hdc);
1961 if(!hdc)
1962 return DDERR_INVALIDPARAMS;
1964 wined3d_mutex_lock();
1965 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1966 hr = ddraw_surface_update_frontbuffer(surface, NULL, TRUE);
1967 if (SUCCEEDED(hr))
1968 hr = wined3d_surface_getdc(surface->wined3d_surface, hdc);
1969 wined3d_mutex_unlock();
1970 switch(hr)
1972 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1973 * touch *hdc
1975 case WINED3DERR_INVALIDCALL:
1976 if(hdc) *hdc = NULL;
1977 return DDERR_INVALIDPARAMS;
1979 default: return hr;
1983 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
1985 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
1987 TRACE("iface %p, dc %p.\n", iface, dc);
1989 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
1992 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1994 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
1996 TRACE("iface %p, dc %p.\n", iface, dc);
1998 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2001 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
2003 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2005 TRACE("iface %p, dc %p.\n", iface, dc);
2007 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2010 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
2012 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2014 TRACE("iface %p, dc %p.\n", iface, dc);
2016 return ddraw_surface7_GetDC(&surface->IDirectDrawSurface7_iface, dc);
2019 /*****************************************************************************
2020 * IDirectDrawSurface7::ReleaseDC
2022 * Releases the DC that was constructed with GetDC
2024 * Params:
2025 * hdc: HDC to release
2027 * Returns:
2028 * DD_OK on success
2029 * For more details, see IWineD3DSurface::ReleaseDC
2031 *****************************************************************************/
2032 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
2034 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2035 HRESULT hr;
2037 TRACE("iface %p, dc %p.\n", iface, hdc);
2039 wined3d_mutex_lock();
2040 hr = wined3d_surface_releasedc(surface->wined3d_surface, hdc);
2041 if (SUCCEEDED(hr) && (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER))
2042 hr = ddraw_surface_update_frontbuffer(surface, NULL, FALSE);
2043 wined3d_mutex_unlock();
2045 return hr;
2048 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
2050 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2052 TRACE("iface %p, dc %p.\n", iface, dc);
2054 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2057 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
2059 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2061 TRACE("iface %p, dc %p.\n", iface, dc);
2063 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2066 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
2068 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2070 TRACE("iface %p, dc %p.\n", iface, dc);
2072 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2075 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
2077 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2079 TRACE("iface %p, dc %p.\n", iface, dc);
2081 return ddraw_surface7_ReleaseDC(&surface->IDirectDrawSurface7_iface, dc);
2084 /*****************************************************************************
2085 * IDirectDrawSurface7::GetCaps
2087 * Returns the surface's caps
2089 * Params:
2090 * Caps: Address to write the caps to
2092 * Returns:
2093 * DD_OK on success
2094 * DDERR_INVALIDPARAMS if Caps is NULL
2096 *****************************************************************************/
2097 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
2099 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2101 TRACE("iface %p, caps %p.\n", iface, Caps);
2103 if(!Caps)
2104 return DDERR_INVALIDPARAMS;
2106 *Caps = surface->surface_desc.ddsCaps;
2108 return DD_OK;
2111 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
2113 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2115 TRACE("iface %p, caps %p.\n", iface, caps);
2117 return ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, caps);
2120 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
2122 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2123 DDSCAPS2 caps2;
2124 HRESULT hr;
2126 TRACE("iface %p, caps %p.\n", iface, caps);
2128 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2129 if (FAILED(hr)) return hr;
2131 caps->dwCaps = caps2.dwCaps;
2132 return hr;
2135 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
2137 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2138 DDSCAPS2 caps2;
2139 HRESULT hr;
2141 TRACE("iface %p, caps %p.\n", iface, caps);
2143 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2144 if (FAILED(hr)) return hr;
2146 caps->dwCaps = caps2.dwCaps;
2147 return hr;
2150 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
2152 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2153 DDSCAPS2 caps2;
2154 HRESULT hr;
2156 TRACE("iface %p, caps %p.\n", iface, caps);
2158 hr = ddraw_surface7_GetCaps(&surface->IDirectDrawSurface7_iface, &caps2);
2159 if (FAILED(hr)) return hr;
2161 caps->dwCaps = caps2.dwCaps;
2162 return hr;
2165 /*****************************************************************************
2166 * IDirectDrawSurface7::SetPriority
2168 * Sets a texture priority for managed textures.
2170 * Params:
2171 * Priority: The new priority
2173 * Returns:
2174 * DD_OK on success
2175 * For more details, see IWineD3DSurface::SetPriority
2177 *****************************************************************************/
2178 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
2180 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2181 HRESULT hr;
2183 TRACE("iface %p, priority %u.\n", iface, Priority);
2185 wined3d_mutex_lock();
2186 hr = wined3d_surface_set_priority(surface->wined3d_surface, Priority);
2187 wined3d_mutex_unlock();
2189 return hr;
2192 /*****************************************************************************
2193 * IDirectDrawSurface7::GetPriority
2195 * Returns the surface's priority
2197 * Params:
2198 * Priority: Address of a variable to write the priority to
2200 * Returns:
2201 * D3D_OK on success
2202 * DDERR_INVALIDPARAMS if Priority == NULL
2203 * For more details, see IWineD3DSurface::GetPriority
2205 *****************************************************************************/
2206 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
2208 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2210 TRACE("iface %p, priority %p.\n", iface, Priority);
2212 if(!Priority)
2214 return DDERR_INVALIDPARAMS;
2217 wined3d_mutex_lock();
2218 *Priority = wined3d_surface_get_priority(surface->wined3d_surface);
2219 wined3d_mutex_unlock();
2221 return DD_OK;
2224 /*****************************************************************************
2225 * IDirectDrawSurface7::SetPrivateData
2227 * Stores some data in the surface that is intended for the application's
2228 * use.
2230 * Params:
2231 * tag: GUID that identifies the data
2232 * Data: Pointer to the private data
2233 * Size: Size of the private data
2234 * Flags: Some flags
2236 * Returns:
2237 * D3D_OK on success
2238 * For more details, see IWineD3DSurface::SetPrivateData
2240 *****************************************************************************/
2241 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2242 REFGUID tag, void *Data, DWORD Size, DWORD Flags)
2244 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2245 struct wined3d_resource *resource;
2246 HRESULT hr;
2248 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2249 iface, debugstr_guid(tag), Data, Size, Flags);
2251 wined3d_mutex_lock();
2252 resource = wined3d_surface_get_resource(surface->wined3d_surface);
2253 hr = wined3d_resource_set_private_data(resource, tag, Data, Size, Flags);
2254 wined3d_mutex_unlock();
2256 switch(hr)
2258 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2259 default: return hr;
2263 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2264 REFGUID tag, void *data, DWORD size, DWORD flags)
2266 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2268 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2269 iface, debugstr_guid(tag), data, size, flags);
2271 return ddraw_surface7_SetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size, flags);
2274 /*****************************************************************************
2275 * IDirectDrawSurface7::GetPrivateData
2277 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2279 * Params:
2280 * tag: GUID of the data to return
2281 * Data: Address where to write the data to
2282 * Size: Size of the buffer at Data
2284 * Returns:
2285 * DD_OK on success
2286 * DDERR_INVALIDPARAMS if Data is NULL
2287 * For more details, see IWineD3DSurface::GetPrivateData
2289 *****************************************************************************/
2290 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
2292 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2293 struct wined3d_resource *resource;
2294 HRESULT hr;
2296 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2297 iface, debugstr_guid(tag), Data, Size);
2299 if(!Data)
2300 return DDERR_INVALIDPARAMS;
2302 wined3d_mutex_lock();
2303 resource = wined3d_surface_get_resource(surface->wined3d_surface);
2304 hr = wined3d_resource_get_private_data(resource, tag, Data, Size);
2305 wined3d_mutex_unlock();
2307 return hr;
2310 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2312 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2314 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2315 iface, debugstr_guid(tag), data, size);
2317 return ddraw_surface7_GetPrivateData(&surface->IDirectDrawSurface7_iface, tag, data, size);
2320 /*****************************************************************************
2321 * IDirectDrawSurface7::FreePrivateData
2323 * Frees private data stored in the surface
2325 * Params:
2326 * tag: Tag of the data to free
2328 * Returns:
2329 * D3D_OK on success
2330 * For more details, see IWineD3DSurface::FreePrivateData
2332 *****************************************************************************/
2333 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2335 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2336 struct wined3d_resource *resource;
2337 HRESULT hr;
2339 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2341 wined3d_mutex_lock();
2342 resource = wined3d_surface_get_resource(surface->wined3d_surface);
2343 hr = wined3d_resource_free_private_data(resource, tag);
2344 wined3d_mutex_unlock();
2346 return hr;
2349 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2351 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2353 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2355 return ddraw_surface7_FreePrivateData(&surface->IDirectDrawSurface7_iface, tag);
2358 /*****************************************************************************
2359 * IDirectDrawSurface7::PageLock
2361 * Prevents a sysmem surface from being paged out
2363 * Params:
2364 * Flags: Not used, must be 0(unchecked)
2366 * Returns:
2367 * DD_OK, because it's a stub
2369 *****************************************************************************/
2370 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2372 TRACE("iface %p, flags %#x.\n", iface, Flags);
2374 /* This is Windows memory management related - we don't need this */
2375 return DD_OK;
2378 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2380 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2382 TRACE("iface %p, flags %#x.\n", iface, flags);
2384 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2387 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2389 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2391 TRACE("iface %p, flags %#x.\n", iface, flags);
2393 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2396 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2398 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2400 TRACE("iface %p, flags %#x.\n", iface, flags);
2402 return ddraw_surface7_PageLock(&surface->IDirectDrawSurface7_iface, flags);
2405 /*****************************************************************************
2406 * IDirectDrawSurface7::PageUnlock
2408 * Allows a sysmem surface to be paged out
2410 * Params:
2411 * Flags: Not used, must be 0(unchecked)
2413 * Returns:
2414 * DD_OK, because it's a stub
2416 *****************************************************************************/
2417 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2419 TRACE("iface %p, flags %#x.\n", iface, Flags);
2421 return DD_OK;
2424 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2426 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2428 TRACE("iface %p, flags %#x.\n", iface, flags);
2430 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2433 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2435 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2437 TRACE("iface %p, flags %#x.\n", iface, flags);
2439 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2442 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2444 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2446 TRACE("iface %p, flags %#x.\n", iface, flags);
2448 return ddraw_surface7_PageUnlock(&surface->IDirectDrawSurface7_iface, flags);
2451 /*****************************************************************************
2452 * IDirectDrawSurface7::BltBatch
2454 * An unimplemented function
2456 * Params:
2459 * Returns:
2460 * DDERR_UNSUPPORTED
2462 *****************************************************************************/
2463 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2465 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2467 /* MSDN: "not currently implemented" */
2468 return DDERR_UNSUPPORTED;
2471 static HRESULT WINAPI ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2473 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2475 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2477 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2480 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2482 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2484 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2486 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2489 static HRESULT WINAPI ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2491 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2493 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2495 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2498 static HRESULT WINAPI ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2500 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2502 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2504 return ddraw_surface7_BltBatch(&surface->IDirectDrawSurface7_iface, batch, count, flags);
2507 /*****************************************************************************
2508 * IDirectDrawSurface7::EnumAttachedSurfaces
2510 * Enumerates all surfaces attached to this surface
2512 * Params:
2513 * context: Pointer to pass unmodified to the callback
2514 * cb: Callback function to call for each surface
2516 * Returns:
2517 * DD_OK on success
2518 * DDERR_INVALIDPARAMS if cb is NULL
2520 *****************************************************************************/
2521 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2522 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2524 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2525 struct ddraw_surface *surf;
2526 DDSURFACEDESC2 desc;
2527 int i;
2529 /* Attached surfaces aren't handled in WineD3D */
2530 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2532 if(!cb)
2533 return DDERR_INVALIDPARAMS;
2535 wined3d_mutex_lock();
2537 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2539 surf = surface->complex_array[i];
2540 if(!surf) break;
2542 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2543 desc = surf->surface_desc;
2544 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2545 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2547 wined3d_mutex_unlock();
2548 return DD_OK;
2552 for (surf = surface->next_attached; surf != NULL; surf = surf->next_attached)
2554 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2555 desc = surf->surface_desc;
2556 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2557 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2559 wined3d_mutex_unlock();
2560 return DD_OK;
2564 TRACE(" end of enumeration.\n");
2566 wined3d_mutex_unlock();
2568 return DD_OK;
2571 struct callback_info2
2573 LPDDENUMSURFACESCALLBACK2 callback;
2574 void *context;
2577 struct callback_info
2579 LPDDENUMSURFACESCALLBACK callback;
2580 void *context;
2583 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2585 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2586 const struct callback_info2 *info = context;
2588 ddraw_surface4_AddRef(&surface_impl->IDirectDrawSurface4_iface);
2589 ddraw_surface7_Release(surface);
2591 return info->callback(&surface_impl->IDirectDrawSurface4_iface, surface_desc, info->context);
2594 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2596 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
2597 const struct callback_info *info = context;
2599 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
2600 ddraw_surface7_Release(surface);
2602 /* FIXME: Check surface_test.dwSize */
2603 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2604 (DDSURFACEDESC *)surface_desc, info->context);
2607 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2608 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2610 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2611 struct callback_info2 info;
2613 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2615 info.callback = callback;
2616 info.context = context;
2618 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2619 &info, EnumCallback2);
2622 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2623 void *context, LPDDENUMSURFACESCALLBACK callback)
2625 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2626 struct callback_info info;
2628 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2630 info.callback = callback;
2631 info.context = context;
2633 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2634 &info, EnumCallback);
2637 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2638 void *context, LPDDENUMSURFACESCALLBACK callback)
2640 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2641 struct callback_info info;
2643 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2645 info.callback = callback;
2646 info.context = context;
2648 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2649 &info, EnumCallback);
2652 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2653 void *context, LPDDENUMSURFACESCALLBACK callback)
2655 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2656 struct callback_info info;
2658 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2660 info.callback = callback;
2661 info.context = context;
2663 return ddraw_surface7_EnumAttachedSurfaces(&surface->IDirectDrawSurface7_iface,
2664 &info, EnumCallback);
2667 /*****************************************************************************
2668 * IDirectDrawSurface7::EnumOverlayZOrders
2670 * "Enumerates the overlay surfaces on the specified destination"
2672 * Params:
2673 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2674 * context: context to pass back to the callback
2675 * cb: callback function to call for each enumerated surface
2677 * Returns:
2678 * DD_OK, because it's a stub
2680 *****************************************************************************/
2681 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2682 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2684 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2686 return DD_OK;
2689 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2690 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2692 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2693 struct callback_info2 info;
2695 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2697 info.callback = callback;
2698 info.context = context;
2700 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2701 flags, &info, EnumCallback2);
2704 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2705 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2707 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2708 struct callback_info info;
2710 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2712 info.callback = callback;
2713 info.context = context;
2715 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2716 flags, &info, EnumCallback);
2719 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2720 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2722 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2723 struct callback_info info;
2725 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2727 info.callback = callback;
2728 info.context = context;
2730 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2731 flags, &info, EnumCallback);
2734 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2735 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2737 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2738 struct callback_info info;
2740 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2742 info.callback = callback;
2743 info.context = context;
2745 return ddraw_surface7_EnumOverlayZOrders(&surface->IDirectDrawSurface7_iface,
2746 flags, &info, EnumCallback);
2749 /*****************************************************************************
2750 * IDirectDrawSurface7::GetBltStatus
2752 * Returns the blitting status
2754 * Params:
2755 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2757 * Returns:
2758 * See IWineD3DSurface::Blt
2760 *****************************************************************************/
2761 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2763 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2764 HRESULT hr;
2766 TRACE("iface %p, flags %#x.\n", iface, Flags);
2768 wined3d_mutex_lock();
2769 hr = wined3d_surface_get_blt_status(surface->wined3d_surface, Flags);
2770 wined3d_mutex_unlock();
2771 switch(hr)
2773 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2774 default: return hr;
2778 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2780 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2782 TRACE("iface %p, flags %#x.\n", iface, flags);
2784 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2787 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2789 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2791 TRACE("iface %p, flags %#x.\n", iface, flags);
2793 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2796 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2798 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2800 TRACE("iface %p, flags %#x.\n", iface, flags);
2802 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2805 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
2807 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2809 TRACE("iface %p, flags %#x.\n", iface, flags);
2811 return ddraw_surface7_GetBltStatus(&surface->IDirectDrawSurface7_iface, flags);
2814 /*****************************************************************************
2815 * IDirectDrawSurface7::GetColorKey
2817 * Returns the color key assigned to the surface
2819 * Params:
2820 * Flags: Some flags
2821 * CKey: Address to store the key to
2823 * Returns:
2824 * DD_OK on success
2825 * DDERR_INVALIDPARAMS if CKey is NULL
2827 *****************************************************************************/
2828 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2830 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
2832 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2834 if(!CKey)
2835 return DDERR_INVALIDPARAMS;
2837 wined3d_mutex_lock();
2839 switch (Flags)
2841 case DDCKEY_DESTBLT:
2842 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
2844 wined3d_mutex_unlock();
2845 return DDERR_NOCOLORKEY;
2847 *CKey = This->surface_desc.ddckCKDestBlt;
2848 break;
2850 case DDCKEY_DESTOVERLAY:
2851 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
2853 wined3d_mutex_unlock();
2854 return DDERR_NOCOLORKEY;
2856 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
2857 break;
2859 case DDCKEY_SRCBLT:
2860 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
2862 wined3d_mutex_unlock();
2863 return DDERR_NOCOLORKEY;
2865 *CKey = This->surface_desc.ddckCKSrcBlt;
2866 break;
2868 case DDCKEY_SRCOVERLAY:
2869 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
2871 wined3d_mutex_unlock();
2872 return DDERR_NOCOLORKEY;
2874 *CKey = This->surface_desc.ddckCKSrcOverlay;
2875 break;
2877 default:
2878 wined3d_mutex_unlock();
2879 return DDERR_INVALIDPARAMS;
2882 wined3d_mutex_unlock();
2884 return DD_OK;
2887 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
2889 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2891 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2893 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
2896 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2898 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2900 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2902 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
2905 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
2907 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2909 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2911 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
2914 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
2916 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2918 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2920 return ddraw_surface7_GetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
2923 /*****************************************************************************
2924 * IDirectDrawSurface7::GetFlipStatus
2926 * Returns the flipping status of the surface
2928 * Params:
2929 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
2931 * Returns:
2932 * See IWineD3DSurface::GetFlipStatus
2934 *****************************************************************************/
2935 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2937 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
2938 HRESULT hr;
2940 TRACE("iface %p, flags %#x.\n", iface, Flags);
2942 wined3d_mutex_lock();
2943 hr = wined3d_surface_get_flip_status(surface->wined3d_surface, Flags);
2944 wined3d_mutex_unlock();
2946 switch(hr)
2948 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2949 default: return hr;
2953 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
2955 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
2957 TRACE("iface %p, flags %#x.\n", iface, flags);
2959 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
2962 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
2964 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
2966 TRACE("iface %p, flags %#x.\n", iface, flags);
2968 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
2971 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
2973 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
2975 TRACE("iface %p, flags %#x.\n", iface, flags);
2977 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
2980 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
2982 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
2984 TRACE("iface %p, flags %#x.\n", iface, flags);
2986 return ddraw_surface7_GetFlipStatus(&surface->IDirectDrawSurface7_iface, flags);
2989 /*****************************************************************************
2990 * IDirectDrawSurface7::GetOverlayPosition
2992 * Returns the display coordinates of a visible and active overlay surface
2994 * Params:
2998 * Returns:
2999 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
3000 *****************************************************************************/
3001 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
3003 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3004 HRESULT hr;
3006 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
3008 wined3d_mutex_lock();
3009 hr = wined3d_surface_get_overlay_position(surface->wined3d_surface, X, Y);
3010 wined3d_mutex_unlock();
3012 return hr;
3015 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
3017 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3019 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3021 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3024 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
3026 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3028 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3030 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3033 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
3035 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3037 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3039 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3042 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
3044 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3046 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
3048 return ddraw_surface7_GetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3051 /*****************************************************************************
3052 * IDirectDrawSurface7::GetPixelFormat
3054 * Returns the pixel format of the Surface
3056 * Params:
3057 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
3058 * format should be written
3060 * Returns:
3061 * DD_OK on success
3062 * DDERR_INVALIDPARAMS if PixelFormat is NULL
3064 *****************************************************************************/
3065 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
3067 /* What is DDERR_INVALIDSURFACETYPE for here? */
3068 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3070 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
3072 if(!PixelFormat)
3073 return DDERR_INVALIDPARAMS;
3075 wined3d_mutex_lock();
3076 DD_STRUCT_COPY_BYSIZE(PixelFormat, &surface->surface_desc.u4.ddpfPixelFormat);
3077 wined3d_mutex_unlock();
3079 return DD_OK;
3082 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
3084 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3086 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3088 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3091 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
3093 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3095 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3097 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3100 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
3102 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3104 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3106 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3109 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
3111 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3113 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
3115 return ddraw_surface7_GetPixelFormat(&surface->IDirectDrawSurface7_iface, pixel_format);
3118 /*****************************************************************************
3119 * IDirectDrawSurface7::GetSurfaceDesc
3121 * Returns the description of this surface
3123 * Params:
3124 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
3125 * surface desc
3127 * Returns:
3128 * DD_OK on success
3129 * DDERR_INVALIDPARAMS if DDSD is NULL
3131 *****************************************************************************/
3132 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
3134 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3136 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3138 if(!DDSD)
3139 return DDERR_INVALIDPARAMS;
3141 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
3143 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
3144 return DDERR_INVALIDPARAMS;
3147 wined3d_mutex_lock();
3148 DD_STRUCT_COPY_BYSIZE(DDSD, &surface->surface_desc);
3149 TRACE("Returning surface desc:\n");
3150 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
3151 wined3d_mutex_unlock();
3153 return DD_OK;
3156 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
3158 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3160 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3162 return ddraw_surface7_GetSurfaceDesc(&surface->IDirectDrawSurface7_iface, DDSD);
3165 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
3167 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3169 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
3171 if (!surface_desc) return DDERR_INVALIDPARAMS;
3173 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
3175 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
3176 return DDERR_INVALIDPARAMS;
3179 wined3d_mutex_lock();
3180 DDSD2_to_DDSD(&surface->surface_desc, surface_desc);
3181 TRACE("Returning surface desc:\n");
3182 if (TRACE_ON(ddraw))
3184 /* DDRAW_dump_surface_desc handles the smaller size */
3185 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
3187 wined3d_mutex_unlock();
3189 return DD_OK;
3192 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
3194 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3196 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3198 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3201 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
3203 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3205 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3207 return ddraw_surface3_GetSurfaceDesc(&surface->IDirectDrawSurface3_iface, DDSD);
3210 /*****************************************************************************
3211 * IDirectDrawSurface7::Initialize
3213 * Initializes the surface. This is a no-op in Wine
3215 * Params:
3216 * DD: Pointer to an DirectDraw interface
3217 * DDSD: Surface description for initialization
3219 * Returns:
3220 * DDERR_ALREADYINITIALIZED
3222 *****************************************************************************/
3223 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3224 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3226 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3228 return DDERR_ALREADYINITIALIZED;
3231 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3232 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3234 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3236 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3238 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3239 ddraw, surface_desc);
3242 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3243 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3245 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3246 DDSURFACEDESC2 surface_desc2;
3248 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3250 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3251 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3252 ddraw, surface_desc ? &surface_desc2 : NULL);
3255 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3256 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3258 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3259 DDSURFACEDESC2 surface_desc2;
3261 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3263 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3264 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3265 ddraw, surface_desc ? &surface_desc2 : NULL);
3268 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3269 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3271 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3272 DDSURFACEDESC2 surface_desc2;
3274 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3276 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3277 return ddraw_surface7_Initialize(&surface->IDirectDrawSurface7_iface,
3278 ddraw, surface_desc ? &surface_desc2 : NULL);
3281 /*****************************************************************************
3282 * IDirect3DTexture1::Initialize
3284 * The sdk says it's not implemented
3286 * Params:
3289 * Returns
3290 * DDERR_UNSUPPORTED
3292 *****************************************************************************/
3293 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3294 IDirect3DDevice *device, IDirectDrawSurface *surface)
3296 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3298 return DDERR_UNSUPPORTED; /* Unchecked */
3301 /*****************************************************************************
3302 * IDirectDrawSurface7::IsLost
3304 * Checks if the surface is lost
3306 * Returns:
3307 * DD_OK, if the surface is usable
3308 * DDERR_ISLOST if the surface is lost
3309 * See IWineD3DSurface::IsLost for more details
3311 *****************************************************************************/
3312 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3314 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3315 HRESULT hr;
3317 TRACE("iface %p.\n", iface);
3319 wined3d_mutex_lock();
3320 hr = wined3d_surface_is_lost(surface->wined3d_surface);
3321 wined3d_mutex_unlock();
3323 switch(hr)
3325 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
3326 * WineD3D uses the same error for surfaces
3328 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
3329 default: return hr;
3333 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3335 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3337 TRACE("iface %p.\n", iface);
3339 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3342 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3344 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3346 TRACE("iface %p.\n", iface);
3348 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3351 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3353 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3355 TRACE("iface %p.\n", iface);
3357 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3360 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3362 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3364 TRACE("iface %p.\n", iface);
3366 return ddraw_surface7_IsLost(&surface->IDirectDrawSurface7_iface);
3369 /*****************************************************************************
3370 * IDirectDrawSurface7::Restore
3372 * Restores a lost surface. This makes the surface usable again, but
3373 * doesn't reload its old contents
3375 * Returns:
3376 * DD_OK on success
3377 * See IWineD3DSurface::Restore for more details
3379 *****************************************************************************/
3380 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3382 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3383 HRESULT hr;
3385 TRACE("iface %p.\n", iface);
3387 wined3d_mutex_lock();
3388 hr = wined3d_surface_restore(surface->wined3d_surface);
3389 wined3d_mutex_unlock();
3391 return hr;
3394 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3396 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3398 TRACE("iface %p.\n", iface);
3400 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3403 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3405 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3407 TRACE("iface %p.\n", iface);
3409 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3412 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3414 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3416 TRACE("iface %p.\n", iface);
3418 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3421 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3423 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3425 TRACE("iface %p.\n", iface);
3427 return ddraw_surface7_Restore(&surface->IDirectDrawSurface7_iface);
3430 /*****************************************************************************
3431 * IDirectDrawSurface7::SetOverlayPosition
3433 * Changes the display coordinates of an overlay surface
3435 * Params:
3436 * X:
3437 * Y:
3439 * Returns:
3440 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3441 *****************************************************************************/
3442 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
3444 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3445 HRESULT hr;
3447 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
3449 wined3d_mutex_lock();
3450 hr = wined3d_surface_set_overlay_position(surface->wined3d_surface, X, Y);
3451 wined3d_mutex_unlock();
3453 return hr;
3456 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3458 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3460 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3462 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3465 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3467 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3469 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3471 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3474 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3476 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3478 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3480 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3483 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3485 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3487 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3489 return ddraw_surface7_SetOverlayPosition(&surface->IDirectDrawSurface7_iface, x, y);
3492 /*****************************************************************************
3493 * IDirectDrawSurface7::UpdateOverlay
3495 * Modifies the attributes of an overlay surface.
3497 * Params:
3498 * SrcRect: The section of the source being used for the overlay
3499 * DstSurface: Address of the surface that is overlaid
3500 * DstRect: Place of the overlay
3501 * Flags: some DDOVER_* flags
3503 * Returns:
3504 * DDERR_UNSUPPORTED, because we don't support overlays
3506 *****************************************************************************/
3507 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
3508 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
3510 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface7(iface);
3511 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface7(DstSurface);
3512 HRESULT hr;
3514 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3515 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
3517 wined3d_mutex_lock();
3518 hr = wined3d_surface_update_overlay(src_impl->wined3d_surface, SrcRect,
3519 dst_impl ? dst_impl->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
3520 wined3d_mutex_unlock();
3522 switch(hr) {
3523 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3524 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3525 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3526 default:
3527 return hr;
3531 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3532 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3534 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface4(iface);
3535 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3537 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3538 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3540 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3541 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3544 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3545 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3547 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface3(iface);
3548 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3550 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3551 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3553 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3554 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3557 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3558 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3560 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface2(iface);
3561 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3563 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3564 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3566 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3567 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3570 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3571 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3573 struct ddraw_surface *src_impl = impl_from_IDirectDrawSurface(iface);
3574 struct ddraw_surface *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3576 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3577 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3579 return ddraw_surface7_UpdateOverlay(&src_impl->IDirectDrawSurface7_iface, src_rect,
3580 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3583 /*****************************************************************************
3584 * IDirectDrawSurface7::UpdateOverlayDisplay
3586 * The DX7 sdk says that it's not implemented
3588 * Params:
3589 * Flags: ?
3591 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3593 *****************************************************************************/
3594 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3596 TRACE("iface %p, flags %#x.\n", iface, Flags);
3598 return DDERR_UNSUPPORTED;
3601 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3603 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3605 TRACE("iface %p, flags %#x.\n", iface, flags);
3607 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3610 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3612 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3614 TRACE("iface %p, flags %#x.\n", iface, flags);
3616 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3619 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3621 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3623 TRACE("iface %p, flags %#x.\n", iface, flags);
3625 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3628 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3630 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3632 TRACE("iface %p, flags %#x.\n", iface, flags);
3634 return ddraw_surface7_UpdateOverlayDisplay(&surface->IDirectDrawSurface7_iface, flags);
3637 /*****************************************************************************
3638 * IDirectDrawSurface7::UpdateOverlayZOrder
3640 * Sets an overlay's Z order
3642 * Params:
3643 * Flags: DDOVERZ_* flags
3644 * DDSRef: Defines the relative position in the overlay chain
3646 * Returns:
3647 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3649 *****************************************************************************/
3650 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3651 DWORD Flags, IDirectDrawSurface7 *DDSRef)
3653 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3654 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface7(DDSRef);
3655 HRESULT hr;
3657 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
3659 wined3d_mutex_lock();
3660 hr = wined3d_surface_update_overlay_z_order(surface->wined3d_surface,
3661 Flags, reference_impl ? reference_impl->wined3d_surface : NULL);
3662 wined3d_mutex_unlock();
3664 return hr;
3667 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3668 DWORD flags, IDirectDrawSurface4 *reference)
3670 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3671 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3673 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3675 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3676 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3679 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3680 DWORD flags, IDirectDrawSurface3 *reference)
3682 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3683 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3685 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3687 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3688 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3691 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3692 DWORD flags, IDirectDrawSurface2 *reference)
3694 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3695 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3697 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3699 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3700 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3703 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3704 DWORD flags, IDirectDrawSurface *reference)
3706 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
3707 struct ddraw_surface *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3709 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3711 return ddraw_surface7_UpdateOverlayZOrder(&surface->IDirectDrawSurface7_iface, flags,
3712 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3715 /*****************************************************************************
3716 * IDirectDrawSurface7::GetDDInterface
3718 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3719 * surface belongs to
3721 * Params:
3722 * DD: Address to write the interface pointer to
3724 * Returns:
3725 * DD_OK on success
3726 * DDERR_INVALIDPARAMS if DD is NULL
3728 *****************************************************************************/
3729 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3731 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3733 TRACE("iface %p, ddraw %p.\n", iface, DD);
3735 if(!DD)
3736 return DDERR_INVALIDPARAMS;
3738 switch(This->version)
3740 case 7:
3741 *DD = &This->ddraw->IDirectDraw7_iface;
3742 break;
3744 case 4:
3745 *DD = &This->ddraw->IDirectDraw4_iface;
3746 break;
3748 case 2:
3749 *DD = &This->ddraw->IDirectDraw2_iface;
3750 break;
3752 case 1:
3753 *DD = &This->ddraw->IDirectDraw_iface;
3754 break;
3757 IUnknown_AddRef((IUnknown *)*DD);
3759 return DD_OK;
3762 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3764 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3766 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3768 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3771 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3773 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
3775 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3777 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3780 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3782 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
3784 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3786 return ddraw_surface7_GetDDInterface(&surface->IDirectDrawSurface7_iface, ddraw);
3789 /* This seems also windows implementation specific - I don't think WineD3D needs this */
3790 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3792 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3793 volatile struct ddraw_surface* vThis = This;
3795 TRACE("iface %p.\n", iface);
3797 wined3d_mutex_lock();
3798 /* A uniqueness value of 0 is apparently special.
3799 * This needs to be checked.
3800 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
3802 while (1) {
3803 DWORD old_uniqueness_value = vThis->uniqueness_value;
3804 DWORD new_uniqueness_value = old_uniqueness_value+1;
3806 if (old_uniqueness_value == 0) break;
3807 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
3809 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
3810 old_uniqueness_value,
3811 new_uniqueness_value)
3812 == old_uniqueness_value)
3813 break;
3816 wined3d_mutex_unlock();
3818 return DD_OK;
3821 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
3823 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3825 TRACE("iface %p.\n", iface);
3827 return ddraw_surface7_ChangeUniquenessValue(&surface->IDirectDrawSurface7_iface);
3830 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
3832 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3834 TRACE("iface %p, value %p.\n", iface, pValue);
3836 wined3d_mutex_lock();
3837 *pValue = surface->uniqueness_value;
3838 wined3d_mutex_unlock();
3840 return DD_OK;
3843 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
3845 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
3847 TRACE("iface %p, value %p.\n", iface, pValue);
3849 return ddraw_surface7_GetUniquenessValue(&surface->IDirectDrawSurface7_iface, pValue);
3852 /*****************************************************************************
3853 * IDirectDrawSurface7::SetLOD
3855 * Sets the level of detail of a texture
3857 * Params:
3858 * MaxLOD: LOD to set
3860 * Returns:
3861 * DD_OK on success
3862 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3864 *****************************************************************************/
3865 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
3867 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3868 HRESULT hr;
3870 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
3872 wined3d_mutex_lock();
3873 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3875 wined3d_mutex_unlock();
3876 return DDERR_INVALIDOBJECT;
3879 if (!surface->wined3d_texture)
3881 ERR("The ddraw surface has no wined3d texture.\n");
3882 wined3d_mutex_unlock();
3883 return DDERR_INVALIDOBJECT;
3886 hr = wined3d_texture_set_lod(surface->wined3d_texture, MaxLOD);
3887 wined3d_mutex_unlock();
3889 return hr;
3892 /*****************************************************************************
3893 * IDirectDrawSurface7::GetLOD
3895 * Returns the level of detail of a Direct3D texture
3897 * Params:
3898 * MaxLOD: Address to write the LOD to
3900 * Returns:
3901 * DD_OK on success
3902 * DDERR_INVALIDPARAMS if MaxLOD is NULL
3903 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3905 *****************************************************************************/
3906 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
3908 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
3910 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
3912 if(!MaxLOD)
3913 return DDERR_INVALIDPARAMS;
3915 wined3d_mutex_lock();
3916 if (!(surface->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3918 wined3d_mutex_unlock();
3919 return DDERR_INVALIDOBJECT;
3922 *MaxLOD = wined3d_texture_get_lod(surface->wined3d_texture);
3923 wined3d_mutex_unlock();
3925 return DD_OK;
3928 /*****************************************************************************
3929 * IDirectDrawSurface7::BltFast
3931 * Performs a fast Blit.
3933 * Params:
3934 * dstx: The x coordinate to blit to on the destination
3935 * dsty: The y coordinate to blit to on the destination
3936 * Source: The source surface
3937 * rsrc: The source rectangle
3938 * trans: Type of transfer. Some DDBLTFAST_* flags
3940 * Returns:
3941 * DD_OK on success
3942 * For more details, see IWineD3DSurface::BltFast
3944 *****************************************************************************/
3945 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
3946 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
3948 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
3949 struct ddraw_surface *src = unsafe_impl_from_IDirectDrawSurface7(Source);
3950 DWORD src_w, src_h, dst_w, dst_h;
3951 HRESULT hr = DD_OK;
3952 DWORD flags = 0;
3953 RECT dst_rect;
3955 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3956 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
3958 dst_w = This->surface_desc.dwWidth;
3959 dst_h = This->surface_desc.dwHeight;
3961 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
3962 * in that case
3964 if(rsrc)
3966 src_w = rsrc->right - rsrc->left;
3967 src_h = rsrc->bottom - rsrc->top;
3969 else
3971 src_w = src->surface_desc.dwWidth;
3972 src_h = src->surface_desc.dwHeight;
3975 if (src_w > dst_w || dstx > dst_w - src_w
3976 || src_h > dst_h || dsty > dst_h - src_h)
3978 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
3979 return DDERR_INVALIDRECT;
3982 SetRect(&dst_rect, dstx, dsty, dstx + src_w, dsty + src_h);
3983 if (trans & DDBLTFAST_SRCCOLORKEY)
3984 flags |= WINEDDBLT_KEYSRC;
3985 if (trans & DDBLTFAST_DESTCOLORKEY)
3986 flags |= WINEDDBLT_KEYDEST;
3987 if (trans & DDBLTFAST_WAIT)
3988 flags |= WINEDDBLT_WAIT;
3989 if (trans & DDBLTFAST_DONOTWAIT)
3990 flags |= WINEDDBLT_DONOTWAIT;
3992 wined3d_mutex_lock();
3993 if (This->clipper)
3995 wined3d_mutex_unlock();
3996 WARN("Destination surface has a clipper set, returning DDERR_BLTFASTCANTCLIP.\n");
3997 return DDERR_BLTFASTCANTCLIP;
4000 if (src->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
4001 hr = ddraw_surface_update_frontbuffer(src, rsrc, TRUE);
4002 if (SUCCEEDED(hr))
4003 hr = wined3d_surface_blt(This->wined3d_surface, &dst_rect,
4004 src->wined3d_surface, rsrc, flags, NULL, WINED3D_TEXF_POINT);
4005 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER))
4006 hr = ddraw_surface_update_frontbuffer(This, &dst_rect, FALSE);
4007 wined3d_mutex_unlock();
4009 switch(hr)
4011 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
4012 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
4013 default: return hr;
4017 static HRESULT WINAPI ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
4018 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
4020 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface4(iface);
4021 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
4023 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4024 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4026 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4027 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4030 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
4031 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
4033 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface3(iface);
4034 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
4036 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4037 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4039 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4040 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4043 static HRESULT WINAPI ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
4044 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
4046 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface2(iface);
4047 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
4049 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4050 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4052 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4053 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4056 static HRESULT WINAPI ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
4057 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
4059 struct ddraw_surface *dst_impl = impl_from_IDirectDrawSurface(iface);
4060 struct ddraw_surface *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
4062 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4063 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
4065 return ddraw_surface7_BltFast(&dst_impl->IDirectDrawSurface7_iface, dst_x, dst_y,
4066 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
4069 /*****************************************************************************
4070 * IDirectDrawSurface7::GetClipper
4072 * Returns the IDirectDrawClipper interface of the clipper assigned to this
4073 * surface
4075 * Params:
4076 * Clipper: Address to store the interface pointer at
4078 * Returns:
4079 * DD_OK on success
4080 * DDERR_INVALIDPARAMS if Clipper is NULL
4081 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
4083 *****************************************************************************/
4084 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
4086 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4088 TRACE("iface %p, clipper %p.\n", iface, Clipper);
4090 if (!Clipper)
4091 return DDERR_INVALIDPARAMS;
4093 wined3d_mutex_lock();
4094 if (!surface->clipper)
4096 wined3d_mutex_unlock();
4097 return DDERR_NOCLIPPERATTACHED;
4100 *Clipper = (IDirectDrawClipper *)surface->clipper;
4101 IDirectDrawClipper_AddRef(*Clipper);
4102 wined3d_mutex_unlock();
4104 return DD_OK;
4107 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
4109 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4111 TRACE("iface %p, clipper %p.\n", iface, clipper);
4113 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4116 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
4118 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4120 TRACE("iface %p, clipper %p.\n", iface, clipper);
4122 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4125 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
4127 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4129 TRACE("iface %p, clipper %p.\n", iface, clipper);
4131 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4134 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
4136 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4138 TRACE("iface %p, clipper %p.\n", iface, clipper);
4140 return ddraw_surface7_GetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4143 /*****************************************************************************
4144 * IDirectDrawSurface7::SetClipper
4146 * Sets a clipper for the surface
4148 * Params:
4149 * Clipper: IDirectDrawClipper interface of the clipper to set
4151 * Returns:
4152 * DD_OK on success
4154 *****************************************************************************/
4155 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
4156 IDirectDrawClipper *iclipper)
4158 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4159 struct ddraw_clipper *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
4160 struct ddraw_clipper *old_clipper = This->clipper;
4161 HWND clipWindow;
4163 TRACE("iface %p, clipper %p.\n", iface, iclipper);
4165 wined3d_mutex_lock();
4166 if (clipper == This->clipper)
4168 wined3d_mutex_unlock();
4169 return DD_OK;
4172 This->clipper = clipper;
4174 if (clipper != NULL)
4175 IDirectDrawClipper_AddRef(iclipper);
4176 if (old_clipper)
4177 IDirectDrawClipper_Release(&old_clipper->IDirectDrawClipper_iface);
4179 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
4181 clipWindow = NULL;
4182 if(clipper) {
4183 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
4186 if (clipWindow)
4188 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
4189 ddraw_set_swapchain_window(This->ddraw, clipWindow);
4191 else
4193 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
4194 ddraw_set_swapchain_window(This->ddraw, This->ddraw->dest_window);
4198 wined3d_mutex_unlock();
4200 return DD_OK;
4203 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
4205 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4207 TRACE("iface %p, clipper %p.\n", iface, clipper);
4209 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4212 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
4214 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4216 TRACE("iface %p, clipper %p.\n", iface, clipper);
4218 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4221 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
4223 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4225 TRACE("iface %p, clipper %p.\n", iface, clipper);
4227 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4230 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
4232 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4234 TRACE("iface %p, clipper %p.\n", iface, clipper);
4236 return ddraw_surface7_SetClipper(&surface->IDirectDrawSurface7_iface, clipper);
4239 /*****************************************************************************
4240 * IDirectDrawSurface7::SetSurfaceDesc
4242 * Sets the surface description. It can override the pixel format, the surface
4243 * memory, ...
4244 * It's not really tested.
4246 * Params:
4247 * DDSD: Pointer to the new surface description to set
4248 * Flags: Some flags
4250 * Returns:
4251 * DD_OK on success
4252 * DDERR_INVALIDPARAMS if DDSD is NULL
4254 *****************************************************************************/
4255 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
4257 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4258 HRESULT hr;
4259 const DWORD allowed_flags = DDSD_LPSURFACE | DDSD_PIXELFORMAT | DDSD_WIDTH
4260 | DDSD_HEIGHT | DDSD_PITCH | DDSD_CAPS;
4261 enum wined3d_format_id format_id;
4262 BOOL update_wined3d = FALSE;
4263 UINT width, height;
4265 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
4267 if (!DDSD)
4269 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4270 return DDERR_INVALIDPARAMS;
4272 if (Flags)
4274 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4275 return DDERR_INVALIDPARAMS;
4277 if (!(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY))
4279 WARN("Surface is not in system memory, returning DDERR_INVALIDSURFACETYPE.\n");
4280 return DDERR_INVALIDSURFACETYPE;
4283 /* Tests show that only LPSURFACE and PIXELFORMAT can be set, and LPSURFACE is required
4284 * for PIXELFORMAT to work */
4285 if (DDSD->dwFlags & ~allowed_flags)
4287 WARN("Invalid flags (0x%08x) set, returning DDERR_INVALIDPARAMS\n", DDSD->dwFlags);
4288 return DDERR_INVALIDPARAMS;
4290 if (!(DDSD->dwFlags & DDSD_LPSURFACE))
4292 WARN("DDSD_LPSURFACE is not set, returning DDERR_INVALIDPARAMS\n");
4293 return DDERR_INVALIDPARAMS;
4295 if (DDSD->dwFlags & DDSD_CAPS)
4297 WARN("DDSD_CAPS is set, returning DDERR_INVALIDCAPS.\n");
4298 return DDERR_INVALIDCAPS;
4300 if (DDSD->dwFlags & DDSD_WIDTH)
4302 if (!(DDSD->dwFlags & DDSD_PITCH))
4304 WARN("DDSD_WIDTH is set, but DDSD_PITCH is not, returning DDERR_INVALIDPARAMS.\n");
4305 return DDERR_INVALIDPARAMS;
4307 if (!DDSD->dwWidth || DDSD->u1.lPitch <= 0 || DDSD->u1.lPitch & 0x3)
4309 WARN("Pitch is %d, width is %u, returning DDERR_INVALIDPARAMS.\n",
4310 DDSD->u1.lPitch, DDSD->dwWidth);
4311 return DDERR_INVALIDPARAMS;
4313 if (DDSD->dwWidth != This->surface_desc.dwWidth)
4315 TRACE("Surface width changed from %u to %u.\n", This->surface_desc.dwWidth, DDSD->dwWidth);
4316 update_wined3d = TRUE;
4318 if (DDSD->u1.lPitch != This->surface_desc.u1.lPitch)
4320 TRACE("Surface pitch changed from %u to %u.\n", This->surface_desc.u1.lPitch, DDSD->u1.lPitch);
4321 update_wined3d = TRUE;
4323 width = DDSD->dwWidth;
4325 else if (DDSD->dwFlags & DDSD_PITCH)
4327 WARN("DDSD_PITCH is set, but DDSD_WIDTH is not, returning DDERR_INVALIDPARAMS.\n");
4328 return DDERR_INVALIDPARAMS;
4330 else
4332 width = This->surface_desc.dwWidth;
4335 if (DDSD->dwFlags & DDSD_HEIGHT)
4337 if (!DDSD->dwHeight)
4339 WARN("Height is 0, returning DDERR_INVALIDPARAMS.\n");
4340 return DDERR_INVALIDPARAMS;
4342 if (DDSD->dwHeight != This->surface_desc.dwHeight)
4344 TRACE("Surface height changed from %u to %u.\n", This->surface_desc.dwHeight, DDSD->dwHeight);
4345 update_wined3d = TRUE;
4347 height = DDSD->dwHeight;
4349 else
4351 height = This->surface_desc.dwHeight;
4354 wined3d_mutex_lock();
4355 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4357 enum wined3d_format_id current_format_id;
4358 format_id = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
4360 if (format_id == WINED3DFMT_UNKNOWN)
4362 ERR("Requested to set an unknown pixelformat\n");
4363 wined3d_mutex_unlock();
4364 return DDERR_INVALIDPARAMS;
4366 current_format_id = PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat);
4367 if (format_id != current_format_id)
4369 TRACE("Surface format changed from %#x to %#x.\n", current_format_id, format_id);
4370 update_wined3d = TRUE;
4373 else
4375 format_id = PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat);
4378 if (update_wined3d)
4380 if (FAILED(hr = wined3d_surface_update_desc(This->wined3d_surface, width, height,
4381 format_id, WINED3D_MULTISAMPLE_NONE, 0)))
4383 WARN("Failed to update surface desc, hr %#x.\n", hr);
4384 wined3d_mutex_unlock();
4385 return hr;
4388 if (DDSD->dwFlags & DDSD_WIDTH)
4389 This->surface_desc.dwWidth = width;
4390 if (DDSD->dwFlags & DDSD_PITCH)
4391 This->surface_desc.u1.lPitch = DDSD->u1.lPitch;
4392 if (DDSD->dwFlags & DDSD_HEIGHT)
4393 This->surface_desc.dwHeight = height;
4394 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4395 This->surface_desc.u4.ddpfPixelFormat = DDSD->u4.ddpfPixelFormat;
4398 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
4400 hr = wined3d_surface_set_mem(This->wined3d_surface, DDSD->lpSurface);
4401 if (FAILED(hr))
4403 /* No need for a trace here, wined3d does that for us */
4404 switch(hr)
4406 case WINED3DERR_INVALIDCALL:
4407 wined3d_mutex_unlock();
4408 return DDERR_INVALIDPARAMS;
4409 default:
4410 break; /* Go on */
4413 /* DDSD->lpSurface is set by Lock() */
4416 wined3d_mutex_unlock();
4418 return DD_OK;
4421 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4422 DDSURFACEDESC2 *surface_desc, DWORD flags)
4424 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4426 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4428 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4429 surface_desc, flags);
4432 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4433 DDSURFACEDESC *surface_desc, DWORD flags)
4435 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4436 DDSURFACEDESC2 surface_desc2;
4438 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4440 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4441 return ddraw_surface7_SetSurfaceDesc(&surface->IDirectDrawSurface7_iface,
4442 surface_desc ? &surface_desc2 : NULL, flags);
4445 /*****************************************************************************
4446 * IDirectDrawSurface7::GetPalette
4448 * Returns the IDirectDrawPalette interface of the palette currently assigned
4449 * to the surface
4451 * Params:
4452 * Pal: Address to write the interface pointer to
4454 * Returns:
4455 * DD_OK on success
4456 * DDERR_INVALIDPARAMS if Pal is NULL
4458 *****************************************************************************/
4459 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
4461 struct ddraw_surface *surface = impl_from_IDirectDrawSurface7(iface);
4462 struct wined3d_palette *wined3d_palette;
4463 HRESULT hr = DD_OK;
4465 TRACE("iface %p, palette %p.\n", iface, Pal);
4467 if(!Pal)
4468 return DDERR_INVALIDPARAMS;
4470 wined3d_mutex_lock();
4471 wined3d_palette = wined3d_surface_get_palette(surface->wined3d_surface);
4472 if (wined3d_palette)
4474 *Pal = wined3d_palette_get_parent(wined3d_palette);
4475 IDirectDrawPalette_AddRef(*Pal);
4477 else
4479 *Pal = NULL;
4480 hr = DDERR_NOPALETTEATTACHED;
4483 wined3d_mutex_unlock();
4485 return hr;
4488 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4490 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4492 TRACE("iface %p, palette %p.\n", iface, palette);
4494 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4497 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4499 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4501 TRACE("iface %p, palette %p.\n", iface, palette);
4503 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4506 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4508 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4510 TRACE("iface %p, palette %p.\n", iface, palette);
4512 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4515 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4517 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4519 TRACE("iface %p, palette %p.\n", iface, palette);
4521 return ddraw_surface7_GetPalette(&surface->IDirectDrawSurface7_iface, palette);
4524 /*****************************************************************************
4525 * SetColorKeyEnum
4527 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
4528 * recursively in the surface tree
4530 *****************************************************************************/
4531 struct SCKContext
4533 HRESULT ret;
4534 struct wined3d_color_key *color_key;
4535 DWORD Flags;
4538 static HRESULT WINAPI SetColorKeyEnum(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
4540 struct ddraw_surface *surface_impl = impl_from_IDirectDrawSurface7(surface);
4541 struct SCKContext *ctx = context;
4542 HRESULT hr;
4544 hr = wined3d_surface_set_color_key(surface_impl->wined3d_surface, ctx->Flags, ctx->color_key);
4545 if (FAILED(hr))
4547 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
4548 ctx->ret = hr;
4551 ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
4552 ddraw_surface7_Release(surface);
4554 return DDENUMRET_OK;
4557 /*****************************************************************************
4558 * IDirectDrawSurface7::SetColorKey
4560 * Sets the color keying options for the surface. Observations showed that
4561 * in case of complex surfaces the color key has to be assigned to all
4562 * sublevels.
4564 * Params:
4565 * Flags: DDCKEY_*
4566 * CKey: The new color key
4568 * Returns:
4569 * DD_OK on success
4570 * See IWineD3DSurface::SetColorKey for details
4572 *****************************************************************************/
4573 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
4575 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4576 DDCOLORKEY FixedCKey;
4577 struct SCKContext ctx = { DD_OK, (struct wined3d_color_key *)(CKey ? &FixedCKey : NULL), Flags };
4579 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
4581 wined3d_mutex_lock();
4582 if (CKey)
4584 FixedCKey = *CKey;
4585 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
4586 if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
4587 FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
4589 switch (Flags & ~DDCKEY_COLORSPACE)
4591 case DDCKEY_DESTBLT:
4592 This->surface_desc.ddckCKDestBlt = FixedCKey;
4593 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4594 break;
4596 case DDCKEY_DESTOVERLAY:
4597 This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
4598 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4599 break;
4601 case DDCKEY_SRCOVERLAY:
4602 This->surface_desc.ddckCKSrcOverlay = FixedCKey;
4603 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4604 break;
4606 case DDCKEY_SRCBLT:
4607 This->surface_desc.ddckCKSrcBlt = FixedCKey;
4608 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4609 break;
4611 default:
4612 wined3d_mutex_unlock();
4613 return DDERR_INVALIDPARAMS;
4616 else
4618 switch (Flags & ~DDCKEY_COLORSPACE)
4620 case DDCKEY_DESTBLT:
4621 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4622 break;
4624 case DDCKEY_DESTOVERLAY:
4625 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4626 break;
4628 case DDCKEY_SRCOVERLAY:
4629 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4630 break;
4632 case DDCKEY_SRCBLT:
4633 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4634 break;
4636 default:
4637 wined3d_mutex_unlock();
4638 return DDERR_INVALIDPARAMS;
4641 ctx.ret = wined3d_surface_set_color_key(This->wined3d_surface, Flags, ctx.color_key);
4642 ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
4643 wined3d_mutex_unlock();
4645 switch(ctx.ret)
4647 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
4648 default: return ctx.ret;
4652 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4654 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4656 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4658 return ddraw_surface7_SetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
4661 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4663 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4665 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4667 return ddraw_surface7_SetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
4670 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4672 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4674 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4676 return ddraw_surface7_SetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
4679 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4681 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4683 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4685 return ddraw_surface7_SetColorKey(&surface->IDirectDrawSurface7_iface, flags, color_key);
4688 /*****************************************************************************
4689 * IDirectDrawSurface7::SetPalette
4691 * Assigns a DirectDrawPalette object to the surface
4693 * Params:
4694 * Pal: Interface to the palette to set
4696 * Returns:
4697 * DD_OK on success
4699 *****************************************************************************/
4700 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
4702 struct ddraw_surface *This = impl_from_IDirectDrawSurface7(iface);
4703 struct ddraw_palette *palette_impl = unsafe_impl_from_IDirectDrawPalette(Pal);
4704 IDirectDrawPalette *oldPal;
4705 struct ddraw_surface *surf;
4706 HRESULT hr;
4708 TRACE("iface %p, palette %p.\n", iface, Pal);
4710 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
4711 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
4712 return DDERR_INVALIDPIXELFORMAT;
4715 if (This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4717 return DDERR_NOTONMIPMAPSUBLEVEL;
4720 /* Find the old palette */
4721 wined3d_mutex_lock();
4722 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
4723 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
4725 wined3d_mutex_unlock();
4726 return hr;
4728 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
4730 /* Set the new Palette */
4731 wined3d_surface_set_palette(This->wined3d_surface, palette_impl ? palette_impl->wineD3DPalette : NULL);
4732 /* AddRef the Palette */
4733 if(Pal) IDirectDrawPalette_AddRef(Pal);
4735 /* Release the old palette */
4736 if(oldPal) IDirectDrawPalette_Release(oldPal);
4738 /* Update the wined3d frontbuffer if this is the frontbuffer. */
4739 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER) && This->ddraw->wined3d_frontbuffer)
4741 hr = wined3d_surface_set_palette(This->ddraw->wined3d_frontbuffer, palette_impl ? palette_impl->wineD3DPalette : NULL);
4742 if (FAILED(hr))
4743 ERR("Failed to set frontbuffer palette, hr %#x.\n", hr);
4746 /* If this is a front buffer, also update the back buffers
4747 * TODO: How do things work for palettized cube textures?
4749 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
4751 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
4752 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
4754 surf = This;
4755 while(1)
4757 IDirectDrawSurface7 *attach;
4758 HRESULT hr;
4759 hr = ddraw_surface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &caps2, &attach);
4760 if(hr != DD_OK)
4762 break;
4765 TRACE("Setting palette on %p\n", attach);
4766 ddraw_surface7_SetPalette(attach, Pal);
4767 surf = impl_from_IDirectDrawSurface7(attach);
4768 ddraw_surface7_Release(attach);
4772 wined3d_mutex_unlock();
4774 return DD_OK;
4777 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4779 struct ddraw_surface *surface = impl_from_IDirectDrawSurface4(iface);
4781 TRACE("iface %p, palette %p.\n", iface, palette);
4783 return ddraw_surface7_SetPalette(&surface->IDirectDrawSurface7_iface, palette);
4786 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4788 struct ddraw_surface *surface = impl_from_IDirectDrawSurface3(iface);
4790 TRACE("iface %p, palette %p.\n", iface, palette);
4792 return ddraw_surface7_SetPalette(&surface->IDirectDrawSurface7_iface, palette);
4795 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4797 struct ddraw_surface *surface = impl_from_IDirectDrawSurface2(iface);
4799 TRACE("iface %p, palette %p.\n", iface, palette);
4801 return ddraw_surface7_SetPalette(&surface->IDirectDrawSurface7_iface, palette);
4804 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4806 struct ddraw_surface *surface = impl_from_IDirectDrawSurface(iface);
4808 TRACE("iface %p, palette %p.\n", iface, palette);
4810 return ddraw_surface7_SetPalette(&surface->IDirectDrawSurface7_iface, palette);
4813 /**********************************************************
4814 * IDirectDrawGammaControl::GetGammaRamp
4816 * Returns the current gamma ramp for a surface
4818 * Params:
4819 * flags: Ignored
4820 * gamma_ramp: Address to write the ramp to
4822 * Returns:
4823 * DD_OK on success
4824 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4826 **********************************************************/
4827 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4828 DWORD flags, DDGAMMARAMP *gamma_ramp)
4830 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4832 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4834 if (!gamma_ramp)
4836 WARN("Invalid gamma_ramp passed.\n");
4837 return DDERR_INVALIDPARAMS;
4840 wined3d_mutex_lock();
4841 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4843 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4844 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (struct wined3d_gamma_ramp *)gamma_ramp);
4846 else
4848 ERR("Not implemented for non-primary surfaces.\n");
4850 wined3d_mutex_unlock();
4852 return DD_OK;
4855 /**********************************************************
4856 * IDirectDrawGammaControl::SetGammaRamp
4858 * Sets the red, green and blue gamma ramps for
4860 * Params:
4861 * flags: Can be DDSGR_CALIBRATE to request calibration
4862 * gamma_ramp: Structure containing the new gamma ramp
4864 * Returns:
4865 * DD_OK on success
4866 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4868 **********************************************************/
4869 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4870 DWORD flags, DDGAMMARAMP *gamma_ramp)
4872 struct ddraw_surface *surface = impl_from_IDirectDrawGammaControl(iface);
4874 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4876 if (!gamma_ramp)
4878 WARN("Invalid gamma_ramp passed.\n");
4879 return DDERR_INVALIDPARAMS;
4882 wined3d_mutex_lock();
4883 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4885 /* Note: DDGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
4886 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device,
4887 0, flags, (struct wined3d_gamma_ramp *)gamma_ramp);
4889 else
4891 ERR("Not implemented for non-primary surfaces.\n");
4893 wined3d_mutex_unlock();
4895 return DD_OK;
4898 /*****************************************************************************
4899 * IDirect3DTexture2::PaletteChanged
4901 * Informs the texture about a palette change
4903 * Params:
4904 * start: Start index of the change
4905 * count: The number of changed entries
4907 * Returns
4908 * D3D_OK, because it's a stub
4910 *****************************************************************************/
4911 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4913 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4915 return D3D_OK;
4918 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4920 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
4922 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4924 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
4927 /*****************************************************************************
4928 * IDirect3DTexture::Unload
4930 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4933 * Returns:
4934 * DDERR_UNSUPPORTED
4936 *****************************************************************************/
4937 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4939 WARN("iface %p. Not implemented.\n", iface);
4941 return DDERR_UNSUPPORTED;
4944 /*****************************************************************************
4945 * IDirect3DTexture2::GetHandle
4947 * Returns handle for the texture. At the moment, the interface
4948 * to the IWineD3DTexture is used.
4950 * Params:
4951 * device: Device this handle is assigned to
4952 * handle: Address to store the handle at.
4954 * Returns:
4955 * D3D_OK
4957 *****************************************************************************/
4958 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4959 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4961 struct ddraw_surface *surface = impl_from_IDirect3DTexture2(iface);
4962 IDirect3DDeviceImpl *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
4964 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4966 wined3d_mutex_lock();
4968 if (!surface->Handle)
4970 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
4971 if (h == DDRAW_INVALID_HANDLE)
4973 ERR("Failed to allocate a texture handle.\n");
4974 wined3d_mutex_unlock();
4975 return DDERR_OUTOFMEMORY;
4978 surface->Handle = h + 1;
4981 TRACE("Returning handle %08x.\n", surface->Handle);
4982 *handle = surface->Handle;
4984 wined3d_mutex_unlock();
4986 return D3D_OK;
4989 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
4990 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
4992 struct ddraw_surface *surface = impl_from_IDirect3DTexture(iface);
4993 IDirect3DDeviceImpl *device_impl = unsafe_impl_from_IDirect3DDevice(device);
4995 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4997 return d3d_texture2_GetHandle(&surface->IDirect3DTexture2_iface,
4998 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
5001 /*****************************************************************************
5002 * get_sub_mimaplevel
5004 * Helper function that returns the next mipmap level
5006 * tex_ptr: Surface of which to return the next level
5008 *****************************************************************************/
5009 static struct ddraw_surface *get_sub_mimaplevel(struct ddraw_surface *surface)
5011 /* Now go down the mipmap chain to the next surface */
5012 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
5013 IDirectDrawSurface7 *next_level;
5014 HRESULT hr;
5016 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
5017 if (FAILED(hr)) return NULL;
5019 ddraw_surface7_Release(next_level);
5021 return impl_from_IDirectDrawSurface7(next_level);
5024 /*****************************************************************************
5025 * IDirect3DTexture2::Load
5027 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
5029 * This function isn't relayed to WineD3D because the whole interface is
5030 * implemented in DDraw only. For speed improvements a implementation which
5031 * takes OpenGL more into account could be placed into WineD3D.
5033 * Params:
5034 * src_texture: Address of the texture to load
5036 * Returns:
5037 * D3D_OK on success
5038 * D3DERR_TEXTURE_LOAD_FAILED.
5040 *****************************************************************************/
5041 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
5043 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture2(iface);
5044 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
5045 HRESULT hr;
5047 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5049 if (src_surface == dst_surface)
5051 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
5052 return D3D_OK;
5055 wined3d_mutex_lock();
5057 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5058 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
5059 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
5061 ERR("Trying to load surfaces with different mip-map counts.\n");
5064 for (;;)
5066 struct wined3d_palette *wined3d_dst_pal, *wined3d_src_pal;
5067 IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
5068 DDSURFACEDESC *src_desc, *dst_desc;
5070 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
5071 src_surface, dst_surface, src_surface->mipmap_level);
5073 /* Suppress the ALLOCONLOAD flag */
5074 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
5076 /* Get the palettes */
5077 wined3d_dst_pal = wined3d_surface_get_palette(dst_surface->wined3d_surface);
5078 if (wined3d_dst_pal)
5079 dst_pal = wined3d_palette_get_parent(wined3d_dst_pal);
5081 wined3d_src_pal = wined3d_surface_get_palette(src_surface->wined3d_surface);
5082 if (wined3d_src_pal)
5083 src_pal = wined3d_palette_get_parent(wined3d_src_pal);
5085 if (src_pal)
5087 PALETTEENTRY palent[256];
5089 if (!dst_pal)
5091 wined3d_mutex_unlock();
5092 return DDERR_NOPALETTEATTACHED;
5094 IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
5095 IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
5098 /* Copy one surface on the other */
5099 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
5100 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
5102 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
5104 /* Should also check for same pixel format, u1.lPitch, ... */
5105 ERR("Error in surface sizes.\n");
5106 wined3d_mutex_unlock();
5107 return D3DERR_TEXTURE_LOAD_FAILED;
5109 else
5111 struct wined3d_mapped_rect src_rect, dst_rect;
5113 /* Copy the src blit color key if the source has one, don't erase
5114 * the destination's ckey if the source has none */
5115 if (src_desc->dwFlags & DDSD_CKSRCBLT)
5117 IDirectDrawSurface7_SetColorKey(&dst_surface->IDirectDrawSurface7_iface,
5118 DDCKEY_SRCBLT, &src_desc->ddckCKSrcBlt);
5121 /* Copy the main memory texture into the surface that corresponds
5122 * to the OpenGL texture object. */
5124 hr = wined3d_surface_map(src_surface->wined3d_surface, &src_rect, NULL, 0);
5125 if (FAILED(hr))
5127 ERR("Failed to lock source surface, hr %#x.\n", hr);
5128 wined3d_mutex_unlock();
5129 return D3DERR_TEXTURE_LOAD_FAILED;
5132 hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_rect, NULL, 0);
5133 if (FAILED(hr))
5135 ERR("Failed to lock destination surface, hr %#x.\n", hr);
5136 wined3d_surface_unmap(src_surface->wined3d_surface);
5137 wined3d_mutex_unlock();
5138 return D3DERR_TEXTURE_LOAD_FAILED;
5141 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
5142 memcpy(dst_rect.data, src_rect.data, src_surface->surface_desc.u1.dwLinearSize);
5143 else
5144 memcpy(dst_rect.data, src_rect.data, src_rect.row_pitch * src_desc->dwHeight);
5146 wined3d_surface_unmap(src_surface->wined3d_surface);
5147 wined3d_surface_unmap(dst_surface->wined3d_surface);
5150 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5151 src_surface = get_sub_mimaplevel(src_surface);
5152 else
5153 src_surface = NULL;
5155 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5156 dst_surface = get_sub_mimaplevel(dst_surface);
5157 else
5158 dst_surface = NULL;
5160 if (!src_surface || !dst_surface)
5162 if (src_surface != dst_surface)
5163 ERR("Loading surface with different mipmap structure.\n");
5164 break;
5168 wined3d_mutex_unlock();
5170 return hr;
5173 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
5175 struct ddraw_surface *dst_surface = impl_from_IDirect3DTexture(iface);
5176 struct ddraw_surface *src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
5178 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
5180 return d3d_texture2_Load(&dst_surface->IDirect3DTexture2_iface,
5181 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
5184 /*****************************************************************************
5185 * The VTable
5186 *****************************************************************************/
5188 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
5190 /* IUnknown */
5191 ddraw_surface7_QueryInterface,
5192 ddraw_surface7_AddRef,
5193 ddraw_surface7_Release,
5194 /* IDirectDrawSurface */
5195 ddraw_surface7_AddAttachedSurface,
5196 ddraw_surface7_AddOverlayDirtyRect,
5197 ddraw_surface7_Blt,
5198 ddraw_surface7_BltBatch,
5199 ddraw_surface7_BltFast,
5200 ddraw_surface7_DeleteAttachedSurface,
5201 ddraw_surface7_EnumAttachedSurfaces,
5202 ddraw_surface7_EnumOverlayZOrders,
5203 ddraw_surface7_Flip,
5204 ddraw_surface7_GetAttachedSurface,
5205 ddraw_surface7_GetBltStatus,
5206 ddraw_surface7_GetCaps,
5207 ddraw_surface7_GetClipper,
5208 ddraw_surface7_GetColorKey,
5209 ddraw_surface7_GetDC,
5210 ddraw_surface7_GetFlipStatus,
5211 ddraw_surface7_GetOverlayPosition,
5212 ddraw_surface7_GetPalette,
5213 ddraw_surface7_GetPixelFormat,
5214 ddraw_surface7_GetSurfaceDesc,
5215 ddraw_surface7_Initialize,
5216 ddraw_surface7_IsLost,
5217 ddraw_surface7_Lock,
5218 ddraw_surface7_ReleaseDC,
5219 ddraw_surface7_Restore,
5220 ddraw_surface7_SetClipper,
5221 ddraw_surface7_SetColorKey,
5222 ddraw_surface7_SetOverlayPosition,
5223 ddraw_surface7_SetPalette,
5224 ddraw_surface7_Unlock,
5225 ddraw_surface7_UpdateOverlay,
5226 ddraw_surface7_UpdateOverlayDisplay,
5227 ddraw_surface7_UpdateOverlayZOrder,
5228 /* IDirectDrawSurface2 */
5229 ddraw_surface7_GetDDInterface,
5230 ddraw_surface7_PageLock,
5231 ddraw_surface7_PageUnlock,
5232 /* IDirectDrawSurface3 */
5233 ddraw_surface7_SetSurfaceDesc,
5234 /* IDirectDrawSurface4 */
5235 ddraw_surface7_SetPrivateData,
5236 ddraw_surface7_GetPrivateData,
5237 ddraw_surface7_FreePrivateData,
5238 ddraw_surface7_GetUniquenessValue,
5239 ddraw_surface7_ChangeUniquenessValue,
5240 /* IDirectDrawSurface7 */
5241 ddraw_surface7_SetPriority,
5242 ddraw_surface7_GetPriority,
5243 ddraw_surface7_SetLOD,
5244 ddraw_surface7_GetLOD,
5247 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
5249 /* IUnknown */
5250 ddraw_surface4_QueryInterface,
5251 ddraw_surface4_AddRef,
5252 ddraw_surface4_Release,
5253 /* IDirectDrawSurface */
5254 ddraw_surface4_AddAttachedSurface,
5255 ddraw_surface4_AddOverlayDirtyRect,
5256 ddraw_surface4_Blt,
5257 ddraw_surface4_BltBatch,
5258 ddraw_surface4_BltFast,
5259 ddraw_surface4_DeleteAttachedSurface,
5260 ddraw_surface4_EnumAttachedSurfaces,
5261 ddraw_surface4_EnumOverlayZOrders,
5262 ddraw_surface4_Flip,
5263 ddraw_surface4_GetAttachedSurface,
5264 ddraw_surface4_GetBltStatus,
5265 ddraw_surface4_GetCaps,
5266 ddraw_surface4_GetClipper,
5267 ddraw_surface4_GetColorKey,
5268 ddraw_surface4_GetDC,
5269 ddraw_surface4_GetFlipStatus,
5270 ddraw_surface4_GetOverlayPosition,
5271 ddraw_surface4_GetPalette,
5272 ddraw_surface4_GetPixelFormat,
5273 ddraw_surface4_GetSurfaceDesc,
5274 ddraw_surface4_Initialize,
5275 ddraw_surface4_IsLost,
5276 ddraw_surface4_Lock,
5277 ddraw_surface4_ReleaseDC,
5278 ddraw_surface4_Restore,
5279 ddraw_surface4_SetClipper,
5280 ddraw_surface4_SetColorKey,
5281 ddraw_surface4_SetOverlayPosition,
5282 ddraw_surface4_SetPalette,
5283 ddraw_surface4_Unlock,
5284 ddraw_surface4_UpdateOverlay,
5285 ddraw_surface4_UpdateOverlayDisplay,
5286 ddraw_surface4_UpdateOverlayZOrder,
5287 /* IDirectDrawSurface2 */
5288 ddraw_surface4_GetDDInterface,
5289 ddraw_surface4_PageLock,
5290 ddraw_surface4_PageUnlock,
5291 /* IDirectDrawSurface3 */
5292 ddraw_surface4_SetSurfaceDesc,
5293 /* IDirectDrawSurface4 */
5294 ddraw_surface4_SetPrivateData,
5295 ddraw_surface4_GetPrivateData,
5296 ddraw_surface4_FreePrivateData,
5297 ddraw_surface4_GetUniquenessValue,
5298 ddraw_surface4_ChangeUniquenessValue,
5301 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
5303 /* IUnknown */
5304 ddraw_surface3_QueryInterface,
5305 ddraw_surface3_AddRef,
5306 ddraw_surface3_Release,
5307 /* IDirectDrawSurface */
5308 ddraw_surface3_AddAttachedSurface,
5309 ddraw_surface3_AddOverlayDirtyRect,
5310 ddraw_surface3_Blt,
5311 ddraw_surface3_BltBatch,
5312 ddraw_surface3_BltFast,
5313 ddraw_surface3_DeleteAttachedSurface,
5314 ddraw_surface3_EnumAttachedSurfaces,
5315 ddraw_surface3_EnumOverlayZOrders,
5316 ddraw_surface3_Flip,
5317 ddraw_surface3_GetAttachedSurface,
5318 ddraw_surface3_GetBltStatus,
5319 ddraw_surface3_GetCaps,
5320 ddraw_surface3_GetClipper,
5321 ddraw_surface3_GetColorKey,
5322 ddraw_surface3_GetDC,
5323 ddraw_surface3_GetFlipStatus,
5324 ddraw_surface3_GetOverlayPosition,
5325 ddraw_surface3_GetPalette,
5326 ddraw_surface3_GetPixelFormat,
5327 ddraw_surface3_GetSurfaceDesc,
5328 ddraw_surface3_Initialize,
5329 ddraw_surface3_IsLost,
5330 ddraw_surface3_Lock,
5331 ddraw_surface3_ReleaseDC,
5332 ddraw_surface3_Restore,
5333 ddraw_surface3_SetClipper,
5334 ddraw_surface3_SetColorKey,
5335 ddraw_surface3_SetOverlayPosition,
5336 ddraw_surface3_SetPalette,
5337 ddraw_surface3_Unlock,
5338 ddraw_surface3_UpdateOverlay,
5339 ddraw_surface3_UpdateOverlayDisplay,
5340 ddraw_surface3_UpdateOverlayZOrder,
5341 /* IDirectDrawSurface2 */
5342 ddraw_surface3_GetDDInterface,
5343 ddraw_surface3_PageLock,
5344 ddraw_surface3_PageUnlock,
5345 /* IDirectDrawSurface3 */
5346 ddraw_surface3_SetSurfaceDesc,
5349 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
5351 /* IUnknown */
5352 ddraw_surface2_QueryInterface,
5353 ddraw_surface2_AddRef,
5354 ddraw_surface2_Release,
5355 /* IDirectDrawSurface */
5356 ddraw_surface2_AddAttachedSurface,
5357 ddraw_surface2_AddOverlayDirtyRect,
5358 ddraw_surface2_Blt,
5359 ddraw_surface2_BltBatch,
5360 ddraw_surface2_BltFast,
5361 ddraw_surface2_DeleteAttachedSurface,
5362 ddraw_surface2_EnumAttachedSurfaces,
5363 ddraw_surface2_EnumOverlayZOrders,
5364 ddraw_surface2_Flip,
5365 ddraw_surface2_GetAttachedSurface,
5366 ddraw_surface2_GetBltStatus,
5367 ddraw_surface2_GetCaps,
5368 ddraw_surface2_GetClipper,
5369 ddraw_surface2_GetColorKey,
5370 ddraw_surface2_GetDC,
5371 ddraw_surface2_GetFlipStatus,
5372 ddraw_surface2_GetOverlayPosition,
5373 ddraw_surface2_GetPalette,
5374 ddraw_surface2_GetPixelFormat,
5375 ddraw_surface2_GetSurfaceDesc,
5376 ddraw_surface2_Initialize,
5377 ddraw_surface2_IsLost,
5378 ddraw_surface2_Lock,
5379 ddraw_surface2_ReleaseDC,
5380 ddraw_surface2_Restore,
5381 ddraw_surface2_SetClipper,
5382 ddraw_surface2_SetColorKey,
5383 ddraw_surface2_SetOverlayPosition,
5384 ddraw_surface2_SetPalette,
5385 ddraw_surface2_Unlock,
5386 ddraw_surface2_UpdateOverlay,
5387 ddraw_surface2_UpdateOverlayDisplay,
5388 ddraw_surface2_UpdateOverlayZOrder,
5389 /* IDirectDrawSurface2 */
5390 ddraw_surface2_GetDDInterface,
5391 ddraw_surface2_PageLock,
5392 ddraw_surface2_PageUnlock,
5395 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5397 /* IUnknown */
5398 ddraw_surface1_QueryInterface,
5399 ddraw_surface1_AddRef,
5400 ddraw_surface1_Release,
5401 /* IDirectDrawSurface */
5402 ddraw_surface1_AddAttachedSurface,
5403 ddraw_surface1_AddOverlayDirtyRect,
5404 ddraw_surface1_Blt,
5405 ddraw_surface1_BltBatch,
5406 ddraw_surface1_BltFast,
5407 ddraw_surface1_DeleteAttachedSurface,
5408 ddraw_surface1_EnumAttachedSurfaces,
5409 ddraw_surface1_EnumOverlayZOrders,
5410 ddraw_surface1_Flip,
5411 ddraw_surface1_GetAttachedSurface,
5412 ddraw_surface1_GetBltStatus,
5413 ddraw_surface1_GetCaps,
5414 ddraw_surface1_GetClipper,
5415 ddraw_surface1_GetColorKey,
5416 ddraw_surface1_GetDC,
5417 ddraw_surface1_GetFlipStatus,
5418 ddraw_surface1_GetOverlayPosition,
5419 ddraw_surface1_GetPalette,
5420 ddraw_surface1_GetPixelFormat,
5421 ddraw_surface1_GetSurfaceDesc,
5422 ddraw_surface1_Initialize,
5423 ddraw_surface1_IsLost,
5424 ddraw_surface1_Lock,
5425 ddraw_surface1_ReleaseDC,
5426 ddraw_surface1_Restore,
5427 ddraw_surface1_SetClipper,
5428 ddraw_surface1_SetColorKey,
5429 ddraw_surface1_SetOverlayPosition,
5430 ddraw_surface1_SetPalette,
5431 ddraw_surface1_Unlock,
5432 ddraw_surface1_UpdateOverlay,
5433 ddraw_surface1_UpdateOverlayDisplay,
5434 ddraw_surface1_UpdateOverlayZOrder,
5437 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5439 ddraw_gamma_control_QueryInterface,
5440 ddraw_gamma_control_AddRef,
5441 ddraw_gamma_control_Release,
5442 ddraw_gamma_control_GetGammaRamp,
5443 ddraw_gamma_control_SetGammaRamp,
5446 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5448 d3d_texture2_QueryInterface,
5449 d3d_texture2_AddRef,
5450 d3d_texture2_Release,
5451 d3d_texture2_GetHandle,
5452 d3d_texture2_PaletteChanged,
5453 d3d_texture2_Load,
5456 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5458 d3d_texture1_QueryInterface,
5459 d3d_texture1_AddRef,
5460 d3d_texture1_Release,
5461 d3d_texture1_Initialize,
5462 d3d_texture1_GetHandle,
5463 d3d_texture1_PaletteChanged,
5464 d3d_texture1_Load,
5465 d3d_texture1_Unload,
5468 /* Some games (e.g. Tomb Raider 3) pass the wrong version of the
5469 * IDirectDrawSurface interface to ddraw methods. */
5470 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5472 if (!iface) return NULL;
5473 if (iface->lpVtbl != &ddraw_surface7_vtbl)
5475 HRESULT hr = IUnknown_QueryInterface(iface, &IID_IDirectDrawSurface7, (void **)&iface);
5476 if (FAILED(hr))
5478 WARN("Object %p doesn't expose interface IDirectDrawSurface7.\n", iface);
5479 return NULL;
5481 IUnknown_Release(iface);
5483 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface7_iface);
5486 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5488 if (!iface) return NULL;
5489 if (iface->lpVtbl != &ddraw_surface4_vtbl)
5491 HRESULT hr = IUnknown_QueryInterface(iface, &IID_IDirectDrawSurface4, (void **)&iface);
5492 if (FAILED(hr))
5494 WARN("Object %p doesn't expose interface IDirectDrawSurface4.\n", iface);
5495 return NULL;
5497 IUnknown_Release(iface);
5499 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface4_iface);
5502 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5504 if (!iface) return NULL;
5505 if (iface->lpVtbl != &ddraw_surface3_vtbl)
5507 HRESULT hr = IUnknown_QueryInterface(iface, &IID_IDirectDrawSurface3, (void **)&iface);
5508 if (FAILED(hr))
5510 WARN("Object %p doesn't expose interface IDirectDrawSurface3.\n", iface);
5511 return NULL;
5513 IUnknown_Release(iface);
5515 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface3_iface);
5518 static struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5520 if (!iface) return NULL;
5521 if (iface->lpVtbl != &ddraw_surface2_vtbl)
5523 HRESULT hr = IUnknown_QueryInterface(iface, &IID_IDirectDrawSurface2, (void **)&iface);
5524 if (FAILED(hr))
5526 WARN("Object %p doesn't expose interface IDirectDrawSurface2.\n", iface);
5527 return NULL;
5529 IUnknown_Release(iface);
5531 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface2_iface);
5534 struct ddraw_surface *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5536 if (!iface) return NULL;
5537 if (iface->lpVtbl != &ddraw_surface1_vtbl)
5539 HRESULT hr = IUnknown_QueryInterface(iface, &IID_IDirectDrawSurface, (void **)&iface);
5540 if (FAILED(hr))
5542 WARN("Object %p doesn't expose interface IDirectDrawSurface.\n", iface);
5543 return NULL;
5545 IUnknown_Release(iface);
5547 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirectDrawSurface_iface);
5550 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5552 if (!iface) return NULL;
5553 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5554 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture2_iface);
5557 struct ddraw_surface *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5559 if (!iface) return NULL;
5560 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5561 return CONTAINING_RECORD(iface, struct ddraw_surface, IDirect3DTexture_iface);
5564 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5566 struct ddraw_surface *surface = parent;
5568 TRACE("surface %p.\n", surface);
5570 /* Check for attached surfaces and detach them. */
5571 if (surface->first_attached != surface)
5573 /* Well, this shouldn't happen: The surface being attached is
5574 * referenced in AddAttachedSurface(), so it shouldn't be released
5575 * until DeleteAttachedSurface() is called, because the refcount is
5576 * held. It looks like the application released it often enough to
5577 * force this. */
5578 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
5580 /* The refcount will drop to -1 here */
5581 if (FAILED(ddraw_surface_delete_attached_surface(surface->first_attached, surface, surface->attached_iface)))
5582 ERR("DeleteAttachedSurface failed.\n");
5585 while (surface->next_attached)
5586 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5587 surface->next_attached, surface->next_attached->attached_iface)))
5588 ERR("DeleteAttachedSurface failed.\n");
5590 /* Having a texture handle set implies that the device still exists. */
5591 if (surface->Handle)
5592 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5594 /* Reduce the ddraw surface count. */
5595 list_remove(&surface->surface_list_entry);
5597 if (surface == surface->ddraw->primary)
5598 surface->ddraw->primary = NULL;
5600 HeapFree(GetProcessHeap(), 0, surface);
5603 static const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5605 ddraw_surface_wined3d_object_destroyed,
5608 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5610 struct ddraw_surface *surface = parent;
5612 TRACE("surface %p.\n", surface);
5614 ddraw_surface_cleanup(surface);
5617 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5619 ddraw_texture_wined3d_object_destroyed,
5622 HRESULT ddraw_surface_create_texture(struct ddraw_surface *surface)
5624 const DDSURFACEDESC2 *desc = &surface->surface_desc;
5625 enum wined3d_format_id format;
5626 enum wined3d_pool pool;
5627 UINT levels;
5629 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5630 levels = desc->u2.dwMipMapCount;
5631 else
5632 levels = 1;
5634 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3D_POOL_SYSTEM_MEM.
5635 * Should I forward the MANAGED cap to the managed pool? */
5636 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5637 pool = WINED3D_POOL_SYSTEM_MEM;
5638 else
5639 pool = WINED3D_POOL_DEFAULT;
5641 format = PixelFormat_DD2WineD3D(&surface->surface_desc.u4.ddpfPixelFormat);
5642 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5643 return wined3d_texture_create_cube(surface->ddraw->wined3d_device, desc->dwWidth,
5644 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5645 else
5646 return wined3d_texture_create_2d(surface->ddraw->wined3d_device, desc->dwWidth, desc->dwHeight,
5647 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5650 HRESULT ddraw_surface_init(struct ddraw_surface *surface, struct ddraw *ddraw,
5651 DDSURFACEDESC2 *desc, UINT mip_level, UINT version)
5653 enum wined3d_pool pool = WINED3D_POOL_DEFAULT;
5654 DWORD flags = WINED3D_SURFACE_MAPPABLE;
5655 enum wined3d_format_id format;
5656 DWORD usage = 0;
5657 HRESULT hr;
5659 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5660 && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5661 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
5663 /* Tests show surfaces without memory flags get these flags added
5664 * right after creation. */
5665 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5668 /* Some applications assume surfaces will always be mapped at the same
5669 * address. Some of those also assume that this address is valid even when
5670 * the surface isn't mapped, and that updates done this way will be
5671 * visible on the screen. The game Nox is such an application,
5672 * Commandos: Behind Enemy Lines is another. */
5673 flags |= WINED3D_SURFACE_PIN_SYSMEM;
5675 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5677 usage |= WINED3DUSAGE_RENDERTARGET;
5678 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5681 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && !(desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
5683 usage |= WINED3DUSAGE_RENDERTARGET;
5686 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
5688 usage |= WINED3DUSAGE_OVERLAY;
5691 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5692 usage |= WINED3DUSAGE_DEPTHSTENCIL;
5694 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
5695 usage |= WINED3DUSAGE_OWNDC;
5697 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5699 pool = WINED3D_POOL_SYSTEM_MEM;
5701 else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
5703 pool = WINED3D_POOL_MANAGED;
5704 /* Managed textures have the system memory flag set. */
5705 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5707 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5709 /* Videomemory adds localvidmem. This is mutually exclusive with
5710 * systemmemory and texturemanage. */
5711 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
5714 format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
5715 if (format == WINED3DFMT_UNKNOWN)
5717 WARN("Unsupported / unknown pixelformat.\n");
5718 return DDERR_INVALIDPIXELFORMAT;
5721 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
5722 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
5723 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
5724 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
5725 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
5726 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
5727 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
5728 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
5729 surface->iface_count = 1;
5730 surface->version = version;
5731 surface->ddraw = ddraw;
5733 if (version == 7)
5735 surface->ref7 = 1;
5737 else if (version == 4)
5739 surface->ref4 = 1;
5741 else
5743 surface->ref1 = 1;
5746 copy_to_surfacedesc2(&surface->surface_desc, desc);
5748 surface->first_attached = surface;
5750 hr = wined3d_surface_create(ddraw->wined3d_device, desc->dwWidth, desc->dwHeight, format, mip_level,
5751 usage, pool, WINED3D_MULTISAMPLE_NONE, 0, DefaultSurfaceType, flags,
5752 surface, &ddraw_surface_wined3d_parent_ops, &surface->wined3d_surface);
5753 if (FAILED(hr))
5755 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
5756 return hr;
5759 /* Anno 1602 stores the pitch right after surface creation, so make sure
5760 * it's there. TODO: Test other fourcc formats. */
5761 if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
5762 || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
5764 surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
5765 if (format == WINED3DFMT_DXT1)
5767 surface->surface_desc.u1.dwLinearSize = max(4, desc->dwWidth) * max(4, desc->dwHeight) / 2;
5769 else
5771 surface->surface_desc.u1.dwLinearSize = max(4, desc->dwWidth) * max(4, desc->dwHeight);
5774 else
5776 surface->surface_desc.dwFlags |= DDSD_PITCH;
5777 surface->surface_desc.u1.lPitch = wined3d_surface_get_pitch(surface->wined3d_surface);
5780 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
5782 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTOVERLAY,
5783 (struct wined3d_color_key *)&desc->u3.ddckCKDestOverlay);
5785 if (desc->dwFlags & DDSD_CKDESTBLT)
5787 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTBLT,
5788 (struct wined3d_color_key *)&desc->ddckCKDestBlt);
5790 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
5792 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCOVERLAY,
5793 (struct wined3d_color_key *)&desc->ddckCKSrcOverlay);
5795 if (desc->dwFlags & DDSD_CKSRCBLT)
5797 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCBLT,
5798 (struct wined3d_color_key *)&desc->ddckCKSrcBlt);
5800 if (desc->dwFlags & DDSD_LPSURFACE)
5802 hr = wined3d_surface_set_mem(surface->wined3d_surface, desc->lpSurface);
5803 if (FAILED(hr))
5805 ERR("Failed to set surface memory, hr %#x.\n", hr);
5806 wined3d_surface_decref(surface->wined3d_surface);
5807 return hr;
5811 return DD_OK;