gdi32: Reselect the pattern brush on every use if it's mapped with DIB_PAL_COLORS.
[wine/multimedia.git] / dlls / ddraw / surface.c
blob567d086ca02665f4971fa81a98f6fa0d21d2360d
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 file contains the (internal) driver registration functions,
10 * driver enumeration APIs and DirectDraw creation functions.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include "ddraw_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
34 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface);
35 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface);
37 static inline IDirectDrawSurfaceImpl *impl_from_IDirectDrawGammaControl(IDirectDrawGammaControl *iface)
39 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawGammaControl_iface);
42 /* This is slow, of course. Also, in case of locks, we can't prevent other
43 * applications from drawing to the screen while we've locked the frontbuffer.
44 * We'd like to do this in wined3d instead, but for that to work wined3d needs
45 * to support windowless rendering first. */
46 static HRESULT ddraw_surface_update_frontbuffer(IDirectDrawSurfaceImpl *surface, const RECT *rect, BOOL read)
48 HDC surface_dc, screen_dc;
49 int x, y, w, h;
50 HRESULT hr;
51 BOOL ret;
53 if (!rect)
55 x = 0;
56 y = 0;
57 w = surface->surface_desc.dwWidth;
58 h = surface->surface_desc.dwHeight;
60 else
62 x = rect->left;
63 y = rect->top;
64 w = rect->right - rect->left;
65 h = rect->bottom - rect->top;
68 if (w <= 0 || h <= 0)
69 return DD_OK;
71 if (surface->ddraw->swapchain_window)
73 /* Nothing to do, we control the frontbuffer, or at least the parts we
74 * care about. */
75 if (read)
76 return DD_OK;
78 return wined3d_surface_blt(surface->ddraw->wined3d_frontbuffer, rect,
79 surface->wined3d_surface, rect, 0, NULL, WINED3DTEXF_POINT);
82 if (FAILED(hr = wined3d_surface_getdc(surface->wined3d_surface, &surface_dc)))
84 ERR("Failed to get surface DC, hr %#x.\n", hr);
85 return hr;
88 if (!(screen_dc = GetDC(NULL)))
90 wined3d_surface_releasedc(surface->wined3d_surface, surface_dc);
91 ERR("Failed to get screen DC.\n");
92 return E_FAIL;
95 if (read)
96 ret = BitBlt(surface_dc, x, y, w, h,
97 screen_dc, x, y, SRCCOPY);
98 else
99 ret = BitBlt(screen_dc, x, y, w, h,
100 surface_dc, x, y, SRCCOPY);
102 ReleaseDC(NULL, screen_dc);
103 wined3d_surface_releasedc(surface->wined3d_surface, surface_dc);
105 if (!ret)
107 ERR("Failed to blit to/from screen.\n");
108 return E_FAIL;
111 return DD_OK;
114 /*****************************************************************************
115 * IUnknown parts follow
116 *****************************************************************************/
118 /*****************************************************************************
119 * IDirectDrawSurface7::QueryInterface
121 * A normal QueryInterface implementation. For QueryInterface rules
122 * see ddraw.c, IDirectDraw7::QueryInterface. This method
123 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
124 * in all versions, the IDirectDrawGammaControl interface and it can
125 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
127 * Params:
128 * riid: The interface id queried for
129 * obj: Address to write the pointer to
131 * Returns:
132 * S_OK on success
133 * E_NOINTERFACE if the requested interface wasn't found
135 *****************************************************************************/
136 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
138 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
140 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
142 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
143 *obj = NULL;
145 if(!riid)
146 return DDERR_INVALIDPARAMS;
148 if (IsEqualGUID(riid, &IID_IUnknown)
149 || IsEqualGUID(riid, &IID_IDirectDrawSurface7) )
151 IDirectDrawSurface7_AddRef(iface);
152 *obj = iface;
153 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
154 return S_OK;
156 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
158 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
159 *obj = &This->IDirectDrawSurface4_iface;
160 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
161 return S_OK;
163 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
165 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
166 *obj = &This->IDirectDrawSurface3_iface;
167 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
168 return S_OK;
170 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
172 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
173 *obj = &This->IDirectDrawSurface2_iface;
174 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
175 return S_OK;
177 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface))
179 IDirectDrawSurface_AddRef(&This->IDirectDrawSurface_iface);
180 *obj = &This->IDirectDrawSurface_iface;
181 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
182 return S_OK;
184 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
186 IDirectDrawGammaControl_AddRef(&This->IDirectDrawGammaControl_iface);
187 *obj = &This->IDirectDrawGammaControl_iface;
188 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
189 return S_OK;
191 else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
192 IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
193 IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
195 IDirect3DDevice7 *d3d;
196 IDirect3DDeviceImpl *device_impl;
198 /* Call into IDirect3D7 for creation */
199 IDirect3D7_CreateDevice(&This->ddraw->IDirect3D7_iface, riid, &This->IDirectDrawSurface7_iface,
200 &d3d);
202 if (d3d)
204 device_impl = impl_from_IDirect3DDevice7(d3d);
205 *obj = &device_impl->IDirect3DDevice_iface;
206 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
207 return S_OK;
210 WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
211 return E_NOINTERFACE;
213 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
214 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
216 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
218 *obj = &This->IDirect3DTexture_iface;
219 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
221 else
223 *obj = &This->IDirect3DTexture2_iface;
224 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
226 IUnknown_AddRef( (IUnknown *) *obj);
227 return S_OK;
230 ERR("No interface\n");
231 return E_NOINTERFACE;
234 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
236 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
237 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
239 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
242 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
244 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
245 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
247 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
250 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
252 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
253 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
255 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
258 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
260 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
261 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
263 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
266 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
267 REFIID riid, void **object)
269 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
271 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
273 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
276 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
278 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture2(iface);
279 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
281 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
284 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
286 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture(iface);
287 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
289 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
292 static void ddraw_surface_add_iface(IDirectDrawSurfaceImpl *This)
294 ULONG iface_count = InterlockedIncrement(&This->iface_count);
295 TRACE("%p increasing iface count to %u.\n", This, iface_count);
297 if (iface_count == 1)
299 EnterCriticalSection(&ddraw_cs);
300 if (This->wined3d_surface)
301 wined3d_surface_incref(This->wined3d_surface);
302 if (This->wined3d_texture)
303 wined3d_texture_incref(This->wined3d_texture);
304 LeaveCriticalSection(&ddraw_cs);
308 /*****************************************************************************
309 * IDirectDrawSurface7::AddRef
311 * A normal addref implementation
313 * Returns:
314 * The new refcount
316 *****************************************************************************/
317 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
319 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
320 ULONG refcount = InterlockedIncrement(&This->ref7);
322 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
324 if (refcount == 1)
326 ddraw_surface_add_iface(This);
329 return refcount;
332 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
334 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
335 ULONG refcount = InterlockedIncrement(&This->ref4);
337 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
339 if (refcount == 1)
341 ddraw_surface_add_iface(This);
344 return refcount;
347 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
349 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
350 ULONG refcount = InterlockedIncrement(&This->ref3);
352 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
354 if (refcount == 1)
356 ddraw_surface_add_iface(This);
359 return refcount;
362 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
364 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
365 ULONG refcount = InterlockedIncrement(&This->ref2);
367 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
369 if (refcount == 1)
371 ddraw_surface_add_iface(This);
374 return refcount;
377 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
379 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
380 ULONG refcount = InterlockedIncrement(&This->ref1);
382 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
384 if (refcount == 1)
386 ddraw_surface_add_iface(This);
389 return refcount;
392 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
394 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
395 ULONG refcount = InterlockedIncrement(&This->gamma_count);
397 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
399 if (refcount == 1)
401 ddraw_surface_add_iface(This);
404 return refcount;
407 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
409 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture2(iface);
410 TRACE("iface %p.\n", iface);
412 return ddraw_surface1_AddRef(&This->IDirectDrawSurface_iface);
415 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
417 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture(iface);
418 TRACE("iface %p.\n", iface);
420 return ddraw_surface1_AddRef(&This->IDirectDrawSurface_iface);
423 /*****************************************************************************
424 * ddraw_surface_destroy
426 * A helper function for IDirectDrawSurface7::Release
428 * Frees the surface, regardless of its refcount.
429 * See IDirectDrawSurface7::Release for more information
431 * Params:
432 * This: Surface to free
434 *****************************************************************************/
435 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
437 TRACE("surface %p.\n", This);
439 /* Check the iface count and give a warning */
440 if(This->iface_count > 1)
442 /* This can happen when a complex surface is destroyed,
443 * because the 2nd surface was addref()ed when the app
444 * called GetAttachedSurface
446 WARN("(%p): Destroying surface with refcounts 7: %d 4: %d 3: %d 2: %d 1: %d\n",
447 This, This->ref7, This->ref4, This->ref3, This->ref2, This->ref1);
450 if (This->wined3d_surface)
451 wined3d_surface_decref(This->wined3d_surface);
454 static void ddraw_surface_cleanup(IDirectDrawSurfaceImpl *surface)
456 IDirectDrawImpl *ddraw = surface->ddraw;
457 BOOL destroy_swapchain = FALSE;
458 IDirectDrawSurfaceImpl *surf;
459 IUnknown *ifaceToRelease;
460 UINT i;
462 TRACE("surface %p.\n", surface);
464 if ((ddraw->d3d_initialized && surface == ddraw->d3d_target
465 && DefaultSurfaceType == SURFACE_OPENGL)
466 || ((surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
467 && DefaultSurfaceType != SURFACE_OPENGL))
468 destroy_swapchain = TRUE;
470 /* The refcount test shows that the palette is detached when the surface
471 * is destroyed. */
472 IDirectDrawSurface7_SetPalette(&surface->IDirectDrawSurface7_iface, NULL);
474 /* Loop through all complex attached surfaces and destroy them.
476 * Yet again, only the root can have more than one complexly attached
477 * surface, all the others have a total of one. */
478 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
480 if (!surface->complex_array[i])
481 break;
483 surf = surface->complex_array[i];
484 surface->complex_array[i] = NULL;
485 while (surf)
487 IDirectDrawSurfaceImpl *destroy = surf;
488 surf = surf->complex_array[0]; /* Iterate through the "tree" */
489 ddraw_surface_destroy(destroy); /* Destroy it */
493 ifaceToRelease = surface->ifaceToRelease;
495 /* Destroy the root surface. */
496 ddraw_surface_destroy(surface);
498 if (ddraw->wined3d_swapchain && destroy_swapchain)
499 ddraw_destroy_swapchain(ddraw);
501 /* Reduce the ddraw refcount */
502 if (ifaceToRelease)
503 IUnknown_Release(ifaceToRelease);
506 ULONG ddraw_surface_release_iface(IDirectDrawSurfaceImpl *This)
508 ULONG iface_count = InterlockedDecrement(&This->iface_count);
509 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
511 if (iface_count == 0)
513 /* Complex attached surfaces are destroyed implicitly when the root is released */
514 EnterCriticalSection(&ddraw_cs);
515 if(!This->is_complex_root)
517 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
518 LeaveCriticalSection(&ddraw_cs);
519 return iface_count;
521 if (This->wined3d_texture) /* If it's a texture, destroy the wined3d texture. */
522 wined3d_texture_decref(This->wined3d_texture);
523 else
524 ddraw_surface_cleanup(This);
525 LeaveCriticalSection(&ddraw_cs);
528 return iface_count;
531 /*****************************************************************************
532 * IDirectDrawSurface7::Release
534 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
535 * surface is destroyed.
537 * Destroying the surface is a bit tricky. For the connection between
538 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
539 * It has a nice graph explaining the connection.
541 * What happens here is basically this:
542 * When a surface is destroyed, its WineD3DSurface is released,
543 * and the refcount of the DirectDraw interface is reduced by 1. If it has
544 * complex surfaces attached to it, then these surfaces are destroyed too,
545 * regardless of their refcount. If any surface being destroyed has another
546 * surface attached to it (with a "soft" attachment, not complex), then
547 * this surface is detached with DeleteAttachedSurface.
549 * When the surface is a texture, the WineD3DTexture is released.
550 * If the surface is the Direct3D render target, then the D3D
551 * capabilities of the WineD3DDevice are uninitialized, which causes the
552 * swapchain to be released.
554 * When a complex sublevel falls to ref zero, then this is ignored.
556 * Returns:
557 * The new refcount
559 *****************************************************************************/
560 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
562 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
563 ULONG refcount = InterlockedDecrement(&This->ref7);
565 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
567 if (refcount == 0)
569 ddraw_surface_release_iface(This);
572 return refcount;
575 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
577 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
578 ULONG refcount = InterlockedDecrement(&This->ref4);
580 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
582 if (refcount == 0)
584 ddraw_surface_release_iface(This);
587 return refcount;
590 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
592 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
593 ULONG refcount = InterlockedDecrement(&This->ref3);
595 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
597 if (refcount == 0)
599 ddraw_surface_release_iface(This);
602 return refcount;
605 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
607 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
608 ULONG refcount = InterlockedDecrement(&This->ref2);
610 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
612 if (refcount == 0)
614 ddraw_surface_release_iface(This);
617 return refcount;
620 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
622 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
623 ULONG refcount = InterlockedDecrement(&This->ref1);
625 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
627 if (refcount == 0)
629 ddraw_surface_release_iface(This);
632 return refcount;
635 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
637 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
638 ULONG refcount = InterlockedDecrement(&This->gamma_count);
640 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
642 if (refcount == 0)
644 ddraw_surface_release_iface(This);
647 return refcount;
650 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
652 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture2(iface);
653 TRACE("iface %p.\n", iface);
655 return ddraw_surface1_Release(&This->IDirectDrawSurface_iface);
658 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
660 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture(iface);
661 TRACE("iface %p.\n", iface);
663 return ddraw_surface1_Release(&This->IDirectDrawSurface_iface);
666 /*****************************************************************************
667 * IDirectDrawSurface7::GetAttachedSurface
669 * Returns an attached surface with the requested caps. Surface attachment
670 * and complex surfaces are not clearly described by the MSDN or sdk,
671 * so this method is tricky and likely to contain problems.
672 * This implementation searches the complex list first, then the
673 * attachment chain.
675 * The chains are searched from This down to the last surface in the chain,
676 * not from the first element in the chain. The first surface found is
677 * returned. The MSDN says that this method fails if more than one surface
678 * matches the caps, but it is not sure if that is right. The attachment
679 * structure may not even allow two matching surfaces.
681 * The found surface is AddRef-ed before it is returned.
683 * Params:
684 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
685 * Surface: Address to store the found surface
687 * Returns:
688 * DD_OK on success
689 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
690 * DDERR_NOTFOUND if no surface was found
692 *****************************************************************************/
693 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
694 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
696 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
697 IDirectDrawSurfaceImpl *surf;
698 DDSCAPS2 our_caps;
699 int i;
701 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
703 EnterCriticalSection(&ddraw_cs);
705 if(This->version < 7)
707 /* Earlier dx apps put garbage into these members, clear them */
708 our_caps.dwCaps = Caps->dwCaps;
709 our_caps.dwCaps2 = 0;
710 our_caps.dwCaps3 = 0;
711 our_caps.dwCaps4 = 0;
713 else
715 our_caps = *Caps;
718 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 */
720 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
722 surf = This->complex_array[i];
723 if(!surf) break;
725 if (TRACE_ON(ddraw))
727 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
728 surf->surface_desc.ddsCaps.dwCaps,
729 surf->surface_desc.ddsCaps.dwCaps2,
730 surf->surface_desc.ddsCaps.dwCaps3,
731 surf->surface_desc.ddsCaps.dwCaps4);
734 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
735 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
737 /* MSDN: "This method fails if more than one surface is attached
738 * that matches the capabilities requested."
740 * Not sure how to test this.
743 TRACE("(%p): Returning surface %p\n", This, surf);
744 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
745 *Surface = &surf->IDirectDrawSurface7_iface;
746 ddraw_surface7_AddRef(*Surface);
747 LeaveCriticalSection(&ddraw_cs);
748 return DD_OK;
752 /* Next, look at the attachment chain */
753 surf = This;
755 while( (surf = surf->next_attached) )
757 if (TRACE_ON(ddraw))
759 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
760 surf->surface_desc.ddsCaps.dwCaps,
761 surf->surface_desc.ddsCaps.dwCaps2,
762 surf->surface_desc.ddsCaps.dwCaps3,
763 surf->surface_desc.ddsCaps.dwCaps4);
766 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
767 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
769 TRACE("(%p): Returning surface %p\n", This, surf);
770 *Surface = &surf->IDirectDrawSurface7_iface;
771 ddraw_surface7_AddRef(*Surface);
772 LeaveCriticalSection(&ddraw_cs);
773 return DD_OK;
777 TRACE("(%p) Didn't find a valid surface\n", This);
778 LeaveCriticalSection(&ddraw_cs);
780 *Surface = NULL;
781 return DDERR_NOTFOUND;
784 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
785 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
787 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
788 IDirectDrawSurface7 *attachment7;
789 IDirectDrawSurfaceImpl *attachment_impl;
790 HRESULT hr;
792 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
794 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
795 caps, &attachment7);
796 if (FAILED(hr))
798 *attachment = NULL;
799 return hr;
801 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
802 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
803 ddraw_surface4_AddRef(*attachment);
804 ddraw_surface7_Release(attachment7);
806 return hr;
809 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
810 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
812 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
813 IDirectDrawSurface7 *attachment7;
814 IDirectDrawSurfaceImpl *attachment_impl;
815 DDSCAPS2 caps2;
816 HRESULT hr;
818 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
820 caps2.dwCaps = caps->dwCaps;
821 caps2.dwCaps2 = 0;
822 caps2.dwCaps3 = 0;
823 caps2.dwCaps4 = 0;
825 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
826 &caps2, &attachment7);
827 if (FAILED(hr))
829 *attachment = NULL;
830 return hr;
832 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
833 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
834 ddraw_surface3_AddRef(*attachment);
835 ddraw_surface7_Release(attachment7);
837 return hr;
840 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
841 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
843 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
844 IDirectDrawSurface7 *attachment7;
845 IDirectDrawSurfaceImpl *attachment_impl;
846 DDSCAPS2 caps2;
847 HRESULT hr;
849 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
851 caps2.dwCaps = caps->dwCaps;
852 caps2.dwCaps2 = 0;
853 caps2.dwCaps3 = 0;
854 caps2.dwCaps4 = 0;
856 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
857 &caps2, &attachment7);
858 if (FAILED(hr))
860 *attachment = NULL;
861 return hr;
863 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
864 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
865 ddraw_surface2_AddRef(*attachment);
866 ddraw_surface7_Release(attachment7);
868 return hr;
871 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
872 DDSCAPS *caps, IDirectDrawSurface **attachment)
874 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
875 IDirectDrawSurface7 *attachment7;
876 IDirectDrawSurfaceImpl *attachment_impl;
877 DDSCAPS2 caps2;
878 HRESULT hr;
880 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
882 caps2.dwCaps = caps->dwCaps;
883 caps2.dwCaps2 = 0;
884 caps2.dwCaps3 = 0;
885 caps2.dwCaps4 = 0;
887 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
888 &caps2, &attachment7);
889 if (FAILED(hr))
891 *attachment = NULL;
892 return hr;
894 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
895 *attachment = &attachment_impl->IDirectDrawSurface_iface;
896 ddraw_surface1_AddRef(*attachment);
897 ddraw_surface7_Release(attachment7);
899 return hr;
902 /*****************************************************************************
903 * IDirectDrawSurface7::Lock
905 * Locks the surface and returns a pointer to the surface's memory
907 * Params:
908 * Rect: Rectangle to lock. If NULL, the whole surface is locked
909 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
910 * Flags: Locking flags, e.g Read only or write only
911 * h: An event handle that's not used and must be NULL
913 * Returns:
914 * DD_OK on success
915 * DDERR_INVALIDPARAMS if DDSD is NULL
916 * For more details, see IWineD3DSurface::LockRect
918 *****************************************************************************/
919 static HRESULT surface_lock(IDirectDrawSurfaceImpl *This,
920 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
922 WINED3DLOCKED_RECT LockedRect;
923 HRESULT hr = DD_OK;
925 TRACE("This %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
926 This, wine_dbgstr_rect(Rect), DDSD, Flags, h);
928 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
929 EnterCriticalSection(&ddraw_cs);
931 /* Should I check for the handle to be NULL?
933 * The DDLOCK flags and the D3DLOCK flags are equal
934 * for the supported values. The others are ignored by WineD3D
937 /* Windows zeroes this if the rect is invalid */
938 DDSD->lpSurface = 0;
940 if (Rect)
942 if ((Rect->left < 0)
943 || (Rect->top < 0)
944 || (Rect->left > Rect->right)
945 || (Rect->top > Rect->bottom)
946 || (Rect->right > This->surface_desc.dwWidth)
947 || (Rect->bottom > This->surface_desc.dwHeight))
949 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
950 LeaveCriticalSection(&ddraw_cs);
951 return DDERR_INVALIDPARAMS;
955 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
956 hr = ddraw_surface_update_frontbuffer(This, Rect, TRUE);
957 if (SUCCEEDED(hr))
958 hr = wined3d_surface_map(This->wined3d_surface, &LockedRect, Rect, Flags);
959 if (FAILED(hr))
961 LeaveCriticalSection(&ddraw_cs);
962 switch(hr)
964 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
965 * specific error. But since IWineD3DSurface::LockRect returns that error in this
966 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
967 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
968 * is much easier to do it in one place in ddraw
970 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
971 default: return hr;
975 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
977 if (Flags & DDLOCK_READONLY)
978 memset(&This->ddraw->primary_lock, 0, sizeof(This->ddraw->primary_lock));
979 else if (Rect)
980 This->ddraw->primary_lock = *Rect;
981 else
982 SetRect(&This->ddraw->primary_lock, 0, 0, This->surface_desc.dwWidth, This->surface_desc.dwHeight);
985 /* Override the memory area. The pitch should be set already. Strangely windows
986 * does not set the LPSURFACE flag on locked surfaces !?!.
987 * DDSD->dwFlags |= DDSD_LPSURFACE;
989 This->surface_desc.lpSurface = LockedRect.pBits;
990 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
992 TRACE("locked surface returning description :\n");
993 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
995 LeaveCriticalSection(&ddraw_cs);
996 return DD_OK;
999 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
1000 RECT *rect, DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1002 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1003 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1004 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1006 if (!surface_desc) return DDERR_INVALIDPARAMS;
1007 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1008 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1010 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1011 return DDERR_INVALIDPARAMS;
1013 return surface_lock(This, rect, surface_desc, flags, h);
1016 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
1017 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
1019 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1020 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1021 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1023 if (!surface_desc) return DDERR_INVALIDPARAMS;
1024 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1025 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1027 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1028 return DDERR_INVALIDPARAMS;
1030 return surface_lock(This, rect, surface_desc, flags, h);
1033 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
1034 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1036 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1037 DDSURFACEDESC2 surface_desc2;
1038 HRESULT hr;
1039 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1040 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1042 if (!surface_desc) return DDERR_INVALIDPARAMS;
1043 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1044 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1046 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1047 return DDERR_INVALIDPARAMS;
1050 surface_desc2.dwSize = surface_desc->dwSize;
1051 surface_desc2.dwFlags = 0;
1052 hr = surface_lock(This, rect, &surface_desc2, flags, h);
1053 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1054 surface_desc->dwSize = surface_desc2.dwSize;
1055 return hr;
1058 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
1059 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1061 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1062 DDSURFACEDESC2 surface_desc2;
1063 HRESULT hr;
1064 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1065 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1067 if (!surface_desc) return DDERR_INVALIDPARAMS;
1068 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1069 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1071 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1072 return DDERR_INVALIDPARAMS;
1075 surface_desc2.dwSize = surface_desc->dwSize;
1076 surface_desc2.dwFlags = 0;
1077 hr = surface_lock(This, rect, &surface_desc2, flags, h);
1078 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1079 surface_desc->dwSize = surface_desc2.dwSize;
1080 return hr;
1083 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
1084 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
1086 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1087 DDSURFACEDESC2 surface_desc2;
1088 HRESULT hr;
1089 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
1090 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
1092 if (!surface_desc) return DDERR_INVALIDPARAMS;
1093 if (surface_desc->dwSize != sizeof(DDSURFACEDESC) &&
1094 surface_desc->dwSize != sizeof(DDSURFACEDESC2))
1096 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", surface_desc->dwSize);
1097 return DDERR_INVALIDPARAMS;
1100 surface_desc2.dwSize = surface_desc->dwSize;
1101 surface_desc2.dwFlags = 0;
1102 hr = surface_lock(This, rect, &surface_desc2, flags, h);
1103 DDSD2_to_DDSD(&surface_desc2, surface_desc);
1104 surface_desc->dwSize = surface_desc2.dwSize;
1105 return hr;
1108 /*****************************************************************************
1109 * IDirectDrawSurface7::Unlock
1111 * Unlocks an locked surface
1113 * Params:
1114 * Rect: Not used by this implementation
1116 * Returns:
1117 * D3D_OK on success
1118 * For more details, see IWineD3DSurface::UnlockRect
1120 *****************************************************************************/
1121 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
1123 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1124 HRESULT hr;
1126 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1128 EnterCriticalSection(&ddraw_cs);
1129 hr = wined3d_surface_unmap(This->wined3d_surface);
1130 if (SUCCEEDED(hr))
1132 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1133 hr = ddraw_surface_update_frontbuffer(This, &This->ddraw->primary_lock, FALSE);
1134 This->surface_desc.lpSurface = NULL;
1136 LeaveCriticalSection(&ddraw_cs);
1137 return hr;
1140 static HRESULT WINAPI ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1142 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1143 TRACE("iface %p, rect %p.\n", iface, pRect);
1145 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, pRect);
1148 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1150 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1151 TRACE("iface %p, data %p.\n", iface, data);
1153 /* data might not be the LPRECT of later versions, so drop it. */
1154 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1157 static HRESULT WINAPI ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1159 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1160 TRACE("iface %p, data %p.\n", iface, data);
1162 /* data might not be the LPRECT of later versions, so drop it. */
1163 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1166 static HRESULT WINAPI ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1168 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1169 TRACE("iface %p, data %p.\n", iface, data);
1171 /* data might not be the LPRECT of later versions, so drop it. */
1172 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1175 /*****************************************************************************
1176 * IDirectDrawSurface7::Flip
1178 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
1179 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
1180 * the flip target is passed to WineD3D, even if the app didn't specify one
1182 * Params:
1183 * DestOverride: Specifies the surface that will become the new front
1184 * buffer. If NULL, the current back buffer is used
1185 * Flags: some DirectDraw flags, see include/ddraw.h
1187 * Returns:
1188 * DD_OK on success
1189 * DDERR_NOTFLIPPABLE if no flip target could be found
1190 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
1191 * For more details, see IWineD3DSurface::Flip
1193 *****************************************************************************/
1194 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
1196 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1197 IDirectDrawSurfaceImpl *Override = unsafe_impl_from_IDirectDrawSurface7(DestOverride);
1198 IDirectDrawSurface7 *Override7;
1199 HRESULT hr;
1201 TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
1203 /* Flip has to be called from a front buffer
1204 * What about overlay surfaces, AFAIK they can flip too?
1206 if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
1207 return DDERR_INVALIDOBJECT; /* Unchecked */
1209 EnterCriticalSection(&ddraw_cs);
1211 /* WineD3D doesn't keep track of attached surface, so find the target */
1212 if(!Override)
1214 DDSCAPS2 Caps;
1216 memset(&Caps, 0, sizeof(Caps));
1217 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
1218 hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
1219 if(hr != DD_OK)
1221 ERR("Can't find a flip target\n");
1222 LeaveCriticalSection(&ddraw_cs);
1223 return DDERR_NOTFLIPPABLE; /* Unchecked */
1225 Override = impl_from_IDirectDrawSurface7(Override7);
1227 /* For the GetAttachedSurface */
1228 ddraw_surface7_Release(Override7);
1231 hr = wined3d_surface_flip(This->wined3d_surface, Override->wined3d_surface, Flags);
1232 if (SUCCEEDED(hr) && This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1233 hr = ddraw_surface_update_frontbuffer(This, NULL, FALSE);
1235 LeaveCriticalSection(&ddraw_cs);
1236 return hr;
1239 static HRESULT WINAPI ddraw_surface4_Flip(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *dst, DWORD flags)
1241 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1242 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1243 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1245 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1246 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1249 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
1251 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1252 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1253 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1255 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1256 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1259 static HRESULT WINAPI ddraw_surface2_Flip(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *dst, DWORD flags)
1261 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1262 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1263 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1265 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1266 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1269 static HRESULT WINAPI ddraw_surface1_Flip(IDirectDrawSurface *iface, IDirectDrawSurface *dst, DWORD flags)
1271 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1272 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1273 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1275 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1276 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1279 /*****************************************************************************
1280 * IDirectDrawSurface7::Blt
1282 * Performs a blit on the surface
1284 * Params:
1285 * DestRect: Destination rectangle, can be NULL
1286 * SrcSurface: Source surface, can be NULL
1287 * SrcRect: Source rectangle, can be NULL
1288 * Flags: Blt flags
1289 * DDBltFx: Some extended blt parameters, connected to the flags
1291 * Returns:
1292 * D3D_OK on success
1293 * See IWineD3DSurface::Blt for more details
1295 *****************************************************************************/
1296 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1297 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1299 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1300 IDirectDrawSurfaceImpl *Src = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1301 HRESULT hr = DD_OK;
1303 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1304 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1306 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1307 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1309 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1310 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1311 return DDERR_INVALIDPARAMS;
1314 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1315 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1316 return DDERR_INVALIDPARAMS;
1319 EnterCriticalSection(&ddraw_cs);
1321 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
1322 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1323 LeaveCriticalSection(&ddraw_cs);
1324 return DDERR_INVALIDPARAMS;
1327 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1328 * does, copy the struct, and replace the ddraw surfaces with the wined3d
1329 * surfaces. So far no blitting operations using surfaces in the bltfx
1330 * struct are supported anyway. */
1331 if (Src && Src->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1332 hr = ddraw_surface_update_frontbuffer(Src, SrcRect, TRUE);
1333 if (SUCCEEDED(hr))
1334 hr = wined3d_surface_blt(This->wined3d_surface, DestRect, Src ? Src->wined3d_surface : NULL,
1335 SrcRect, Flags, (WINEDDBLTFX *)DDBltFx, WINED3DTEXF_LINEAR);
1336 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER))
1337 hr = ddraw_surface_update_frontbuffer(This, DestRect, FALSE);
1339 LeaveCriticalSection(&ddraw_cs);
1340 switch(hr)
1342 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1343 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
1344 default: return hr;
1348 static HRESULT WINAPI ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1349 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1351 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1352 IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1353 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1354 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1356 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1357 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1360 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1361 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1363 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1364 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1365 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1366 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1368 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1369 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1372 static HRESULT WINAPI ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1373 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1375 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1376 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1377 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1378 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1380 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1381 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1384 static HRESULT WINAPI ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1385 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1387 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1388 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1389 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1390 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1392 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1393 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1396 /*****************************************************************************
1397 * IDirectDrawSurface7::AddAttachedSurface
1399 * Attaches a surface to another surface. How the surface attachments work
1400 * is not totally understood yet, and this method is prone to problems.
1401 * The surface that is attached is AddRef-ed.
1403 * Tests with complex surfaces suggest that the surface attachments form a
1404 * tree, but no method to test this has been found yet.
1406 * The attachment list consists of a first surface (first_attached) and
1407 * for each surface a pointer to the next attached surface (next_attached).
1408 * For the first surface, and a surface that has no attachments
1409 * first_attached points to the surface itself. A surface that has
1410 * no successors in the chain has next_attached set to NULL.
1412 * Newly attached surfaces are attached right after the root surface.
1413 * If a surface is attached to a complex surface compound, it's attached to
1414 * the surface that the app requested, not the complex root. See
1415 * GetAttachedSurface for a description how surfaces are found.
1417 * This is how the current implementation works, and it was coded by looking
1418 * at the needs of the applications.
1420 * So far only Z-Buffer attachments are tested, and they are activated in
1421 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1422 * Back buffers should work in 2D mode, but they are not tested(They can be
1423 * attached in older iface versions). Rendering to the front buffer and
1424 * switching between that and double buffering is not yet implemented in
1425 * WineD3D, so for 3D it might have unexpected results.
1427 * ddraw_surface_attach_surface is the real thing,
1428 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1429 * performs additional checks. Version 7 of this interface is much more restrictive
1430 * than its predecessors.
1432 * Params:
1433 * Attach: Surface to attach to iface
1435 * Returns:
1436 * DD_OK on success
1437 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1439 *****************************************************************************/
1440 static HRESULT ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
1442 TRACE("surface %p, attachment %p.\n", This, Surf);
1444 if(Surf == This)
1445 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1447 EnterCriticalSection(&ddraw_cs);
1449 /* Check if the surface is already attached somewhere */
1450 if (Surf->next_attached || Surf->first_attached != Surf)
1452 /* TODO: Test for the structure of the manual attachment. Is it a
1453 * chain or a list? What happens if one surface is attached to 2
1454 * different surfaces? */
1455 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1456 Surf, Surf->next_attached, Surf->first_attached);
1458 LeaveCriticalSection(&ddraw_cs);
1459 return DDERR_SURFACEALREADYATTACHED;
1462 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1463 Surf->next_attached = This->next_attached;
1464 Surf->first_attached = This->first_attached;
1465 This->next_attached = Surf;
1467 /* Check if the WineD3D depth stencil needs updating */
1468 if(This->ddraw->d3ddevice)
1470 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1473 LeaveCriticalSection(&ddraw_cs);
1474 return DD_OK;
1477 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *attachment)
1479 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1480 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1481 HRESULT hr;
1483 TRACE("iface %p, attachment %p.\n", iface, attachment);
1485 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1486 if(!(attachment_impl->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1489 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1490 attachment_impl->surface_desc.ddsCaps.dwCaps);
1491 return DDERR_CANNOTATTACHSURFACE;
1494 hr = ddraw_surface_attach_surface(This, attachment_impl);
1495 if (FAILED(hr))
1497 return hr;
1499 ddraw_surface7_AddRef(attachment);
1500 attachment_impl->attached_iface = (IUnknown *)attachment;
1501 return hr;
1504 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1506 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1507 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1508 HRESULT hr;
1510 TRACE("iface %p, attachment %p.\n", iface, attachment);
1512 hr = ddraw_surface7_AddAttachedSurface(&This->IDirectDrawSurface7_iface,
1513 attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1514 if (FAILED(hr))
1516 return hr;
1518 ddraw_surface4_AddRef(attachment);
1519 ddraw_surface7_Release(&attachment_impl->IDirectDrawSurface7_iface);
1520 attachment_impl->attached_iface = (IUnknown *)attachment;
1521 return hr;
1523 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1525 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1526 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1527 HRESULT hr;
1529 TRACE("iface %p, attachment %p.\n", iface, attachment);
1531 /* Tests suggest that
1532 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1533 * -> offscreen plain surfaces can be attached to primaries
1534 * -> primaries can be attached to offscreen plain surfaces
1535 * -> z buffers can be attached to primaries */
1536 if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1537 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1539 /* Sizes have to match */
1540 if (attachment_impl->surface_desc.dwWidth != This->surface_desc.dwWidth
1541 || attachment_impl->surface_desc.dwHeight != This->surface_desc.dwHeight)
1543 WARN("Surface sizes do not match.\n");
1544 return DDERR_CANNOTATTACHSURFACE;
1546 /* OK */
1548 else if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1549 && attachment_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1551 /* OK */
1553 else
1555 WARN("Invalid attachment combination.\n");
1556 return DDERR_CANNOTATTACHSURFACE;
1559 hr = ddraw_surface_attach_surface(This, attachment_impl);
1560 if (FAILED(hr))
1562 return hr;
1564 ddraw_surface3_AddRef(attachment);
1565 attachment_impl->attached_iface = (IUnknown *)attachment;
1566 return hr;
1569 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1571 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1572 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1573 HRESULT hr;
1575 TRACE("iface %p, attachment %p.\n", iface, attachment);
1577 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1578 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1579 if (FAILED(hr))
1581 return hr;
1583 ddraw_surface2_AddRef(attachment);
1584 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1585 attachment_impl->attached_iface = (IUnknown *)attachment;
1586 return hr;
1589 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1591 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1592 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1593 HRESULT hr;
1595 TRACE("iface %p, attachment %p.\n", iface, attachment);
1597 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1598 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1599 if (FAILED(hr))
1601 return hr;
1603 ddraw_surface1_AddRef(attachment);
1604 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1605 attachment_impl->attached_iface = (IUnknown *)attachment;
1606 return hr;
1609 /*****************************************************************************
1610 * IDirectDrawSurface7::DeleteAttachedSurface
1612 * Removes a surface from the attachment chain. The surface's refcount
1613 * is decreased by one after it has been removed
1615 * Params:
1616 * Flags: Some flags, not used by this implementation
1617 * Attach: Surface to detach
1619 * Returns:
1620 * DD_OK on success
1621 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1623 *****************************************************************************/
1624 static HRESULT ddraw_surface_delete_attached_surface(IDirectDrawSurfaceImpl *This,
1625 IDirectDrawSurfaceImpl *Surf, IUnknown *detach_iface)
1627 IDirectDrawSurfaceImpl *Prev = This;
1629 TRACE("surface %p, attachment %p, detach_iface %p.\n", This, Surf, detach_iface);
1631 EnterCriticalSection(&ddraw_cs);
1632 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1634 LeaveCriticalSection(&ddraw_cs);
1635 return DDERR_CANNOTDETACHSURFACE;
1638 if (Surf->attached_iface != detach_iface)
1640 WARN("Surf->attach_iface %p != detach_iface %p.\n", Surf->attached_iface, detach_iface);
1641 LeaveCriticalSection(&ddraw_cs);
1642 return DDERR_SURFACENOTATTACHED;
1645 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1646 if (This->surface_desc.ddsCaps.dwCaps &
1647 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1649 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1650 /* FIXME: we should probably also subtract from dwMipMapCount of this
1651 * and all parent surfaces */
1654 /* Find the predecessor of the detached surface */
1655 while(Prev)
1657 if(Prev->next_attached == Surf) break;
1658 Prev = Prev->next_attached;
1661 /* There must be a surface, otherwise there's a bug */
1662 assert(Prev != NULL);
1664 /* Unchain the surface */
1665 Prev->next_attached = Surf->next_attached;
1666 Surf->next_attached = NULL;
1667 Surf->first_attached = Surf;
1669 /* Check if the WineD3D depth stencil needs updating */
1670 if(This->ddraw->d3ddevice)
1672 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1674 LeaveCriticalSection(&ddraw_cs);
1676 /* Set attached_iface to NULL before releasing it, the surface may go
1677 * away. */
1678 Surf->attached_iface = NULL;
1679 IUnknown_Release(detach_iface);
1681 return DD_OK;
1684 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1685 DWORD flags, IDirectDrawSurface7 *attachment)
1687 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1688 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1690 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1692 return ddraw_surface_delete_attached_surface(This, attachment_impl, (IUnknown *)attachment);
1695 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1696 DWORD flags, IDirectDrawSurface4 *attachment)
1698 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1699 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1701 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1703 return ddraw_surface_delete_attached_surface(This, attachment_impl, (IUnknown *)attachment);
1706 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1707 DWORD flags, IDirectDrawSurface3 *attachment)
1709 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1710 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1712 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1714 return ddraw_surface_delete_attached_surface(This, attachment_impl, (IUnknown *)attachment);
1717 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
1718 DWORD flags, IDirectDrawSurface2 *attachment)
1720 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1721 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1723 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1725 return ddraw_surface_delete_attached_surface(This, attachment_impl, (IUnknown *)attachment);
1728 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
1729 DWORD flags, IDirectDrawSurface *attachment)
1731 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1732 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1734 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1736 return ddraw_surface_delete_attached_surface(This, attachment_impl, (IUnknown *)attachment);
1739 /*****************************************************************************
1740 * IDirectDrawSurface7::AddOverlayDirtyRect
1742 * "This method is not currently implemented"
1744 * Params:
1745 * Rect: ?
1747 * Returns:
1748 * DDERR_UNSUPPORTED
1750 *****************************************************************************/
1751 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1753 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1755 return DDERR_UNSUPPORTED; /* unchecked */
1758 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
1760 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1761 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1763 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1766 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1768 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1769 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1771 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1774 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
1776 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1777 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1779 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1782 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
1784 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1785 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1787 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1790 /*****************************************************************************
1791 * IDirectDrawSurface7::GetDC
1793 * Returns a GDI device context for the surface
1795 * Params:
1796 * hdc: Address of a HDC variable to store the dc to
1798 * Returns:
1799 * DD_OK on success
1800 * DDERR_INVALIDPARAMS if hdc is NULL
1801 * For details, see IWineD3DSurface::GetDC
1803 *****************************************************************************/
1804 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1806 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1807 HRESULT hr = DD_OK;
1809 TRACE("iface %p, dc %p.\n", iface, hdc);
1811 if(!hdc)
1812 return DDERR_INVALIDPARAMS;
1814 EnterCriticalSection(&ddraw_cs);
1815 if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
1816 hr = ddraw_surface_update_frontbuffer(This, NULL, TRUE);
1817 if (SUCCEEDED(hr))
1818 hr = wined3d_surface_getdc(This->wined3d_surface, hdc);
1819 LeaveCriticalSection(&ddraw_cs);
1820 switch(hr)
1822 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1823 * touch *hdc
1825 case WINED3DERR_INVALIDCALL:
1826 if(hdc) *hdc = NULL;
1827 return DDERR_INVALIDPARAMS;
1829 default: return hr;
1833 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
1835 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1836 TRACE("iface %p, dc %p.\n", iface, dc);
1838 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1841 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1843 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1844 TRACE("iface %p, dc %p.\n", iface, dc);
1846 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1849 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
1851 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1852 TRACE("iface %p, dc %p.\n", iface, dc);
1854 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1857 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
1859 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1860 TRACE("iface %p, dc %p.\n", iface, dc);
1862 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1865 /*****************************************************************************
1866 * IDirectDrawSurface7::ReleaseDC
1868 * Releases the DC that was constructed with GetDC
1870 * Params:
1871 * hdc: HDC to release
1873 * Returns:
1874 * DD_OK on success
1875 * For more details, see IWineD3DSurface::ReleaseDC
1877 *****************************************************************************/
1878 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1880 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1881 HRESULT hr;
1883 TRACE("iface %p, dc %p.\n", iface, hdc);
1885 EnterCriticalSection(&ddraw_cs);
1886 hr = wined3d_surface_releasedc(This->wined3d_surface, hdc);
1887 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER))
1888 hr = ddraw_surface_update_frontbuffer(This, NULL, FALSE);
1889 LeaveCriticalSection(&ddraw_cs);
1890 return hr;
1893 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
1895 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1896 TRACE("iface %p, dc %p.\n", iface, dc);
1898 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1901 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1903 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1904 TRACE("iface %p, dc %p.\n", iface, dc);
1906 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1909 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
1911 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1912 TRACE("iface %p, dc %p.\n", iface, dc);
1914 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1917 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
1919 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1920 TRACE("iface %p, dc %p.\n", iface, dc);
1922 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1925 /*****************************************************************************
1926 * IDirectDrawSurface7::GetCaps
1928 * Returns the surface's caps
1930 * Params:
1931 * Caps: Address to write the caps to
1933 * Returns:
1934 * DD_OK on success
1935 * DDERR_INVALIDPARAMS if Caps is NULL
1937 *****************************************************************************/
1938 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1940 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1942 TRACE("iface %p, caps %p.\n", iface, Caps);
1944 if(!Caps)
1945 return DDERR_INVALIDPARAMS;
1947 *Caps = This->surface_desc.ddsCaps;
1948 return DD_OK;
1951 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
1953 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1954 TRACE("iface %p, caps %p.\n", iface, caps);
1956 return ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, caps);
1959 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1961 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1962 DDSCAPS2 caps2;
1963 HRESULT hr;
1965 TRACE("iface %p, caps %p.\n", iface, caps);
1967 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1968 if (FAILED(hr)) return hr;
1970 caps->dwCaps = caps2.dwCaps;
1971 return hr;
1974 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
1976 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1977 DDSCAPS2 caps2;
1978 HRESULT hr;
1980 TRACE("iface %p, caps %p.\n", iface, caps);
1982 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1983 if (FAILED(hr)) return hr;
1985 caps->dwCaps = caps2.dwCaps;
1986 return hr;
1989 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
1991 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1992 DDSCAPS2 caps2;
1993 HRESULT hr;
1995 TRACE("iface %p, caps %p.\n", iface, caps);
1997 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1998 if (FAILED(hr)) return hr;
2000 caps->dwCaps = caps2.dwCaps;
2001 return hr;
2004 /*****************************************************************************
2005 * IDirectDrawSurface7::SetPriority
2007 * Sets a texture priority for managed textures.
2009 * Params:
2010 * Priority: The new priority
2012 * Returns:
2013 * DD_OK on success
2014 * For more details, see IWineD3DSurface::SetPriority
2016 *****************************************************************************/
2017 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
2019 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2020 HRESULT hr;
2022 TRACE("iface %p, priority %u.\n", iface, Priority);
2024 EnterCriticalSection(&ddraw_cs);
2025 hr = wined3d_surface_set_priority(This->wined3d_surface, Priority);
2026 LeaveCriticalSection(&ddraw_cs);
2027 return hr;
2030 /*****************************************************************************
2031 * IDirectDrawSurface7::GetPriority
2033 * Returns the surface's priority
2035 * Params:
2036 * Priority: Address of a variable to write the priority to
2038 * Returns:
2039 * D3D_OK on success
2040 * DDERR_INVALIDPARAMS if Priority == NULL
2041 * For more details, see IWineD3DSurface::GetPriority
2043 *****************************************************************************/
2044 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
2046 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2048 TRACE("iface %p, priority %p.\n", iface, Priority);
2050 if(!Priority)
2052 return DDERR_INVALIDPARAMS;
2055 EnterCriticalSection(&ddraw_cs);
2056 *Priority = wined3d_surface_get_priority(This->wined3d_surface);
2057 LeaveCriticalSection(&ddraw_cs);
2058 return DD_OK;
2061 /*****************************************************************************
2062 * IDirectDrawSurface7::SetPrivateData
2064 * Stores some data in the surface that is intended for the application's
2065 * use.
2067 * Params:
2068 * tag: GUID that identifies the data
2069 * Data: Pointer to the private data
2070 * Size: Size of the private data
2071 * Flags: Some flags
2073 * Returns:
2074 * D3D_OK on success
2075 * For more details, see IWineD3DSurface::SetPrivateData
2077 *****************************************************************************/
2078 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
2079 REFGUID tag, void *Data, DWORD Size, DWORD Flags)
2081 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2082 struct wined3d_resource *resource;
2083 HRESULT hr;
2085 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2086 iface, debugstr_guid(tag), Data, Size, Flags);
2088 EnterCriticalSection(&ddraw_cs);
2089 resource = wined3d_surface_get_resource(This->wined3d_surface);
2090 hr = wined3d_resource_set_private_data(resource, tag, Data, Size, Flags);
2091 LeaveCriticalSection(&ddraw_cs);
2092 switch(hr)
2094 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2095 default: return hr;
2099 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2100 REFGUID tag, void *data, DWORD size, DWORD flags)
2102 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2103 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2104 iface, debugstr_guid(tag), data, size, flags);
2106 return ddraw_surface7_SetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size, flags);
2109 /*****************************************************************************
2110 * IDirectDrawSurface7::GetPrivateData
2112 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2114 * Params:
2115 * tag: GUID of the data to return
2116 * Data: Address where to write the data to
2117 * Size: Size of the buffer at Data
2119 * Returns:
2120 * DD_OK on success
2121 * DDERR_INVALIDPARAMS if Data is NULL
2122 * For more details, see IWineD3DSurface::GetPrivateData
2124 *****************************************************************************/
2125 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
2127 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2128 struct wined3d_resource *resource;
2129 HRESULT hr;
2131 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2132 iface, debugstr_guid(tag), Data, Size);
2134 if(!Data)
2135 return DDERR_INVALIDPARAMS;
2137 EnterCriticalSection(&ddraw_cs);
2138 resource = wined3d_surface_get_resource(This->wined3d_surface);
2139 hr = wined3d_resource_get_private_data(resource, tag, Data, Size);
2140 LeaveCriticalSection(&ddraw_cs);
2141 return hr;
2144 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2146 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2147 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2148 iface, debugstr_guid(tag), data, size);
2150 return ddraw_surface7_GetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size);
2153 /*****************************************************************************
2154 * IDirectDrawSurface7::FreePrivateData
2156 * Frees private data stored in the surface
2158 * Params:
2159 * tag: Tag of the data to free
2161 * Returns:
2162 * D3D_OK on success
2163 * For more details, see IWineD3DSurface::FreePrivateData
2165 *****************************************************************************/
2166 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2168 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2169 struct wined3d_resource *resource;
2170 HRESULT hr;
2172 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2174 EnterCriticalSection(&ddraw_cs);
2175 resource = wined3d_surface_get_resource(This->wined3d_surface);
2176 hr = wined3d_resource_free_private_data(resource, tag);
2177 LeaveCriticalSection(&ddraw_cs);
2178 return hr;
2181 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2183 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2184 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2186 return ddraw_surface7_FreePrivateData(&This->IDirectDrawSurface7_iface, tag);
2189 /*****************************************************************************
2190 * IDirectDrawSurface7::PageLock
2192 * Prevents a sysmem surface from being paged out
2194 * Params:
2195 * Flags: Not used, must be 0(unchecked)
2197 * Returns:
2198 * DD_OK, because it's a stub
2200 *****************************************************************************/
2201 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2203 TRACE("iface %p, flags %#x.\n", iface, Flags);
2205 /* This is Windows memory management related - we don't need this */
2206 return DD_OK;
2209 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2211 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2212 TRACE("iface %p, flags %#x.\n", iface, flags);
2214 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2217 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2219 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2220 TRACE("iface %p, flags %#x.\n", iface, flags);
2222 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2225 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2227 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2228 TRACE("iface %p, flags %#x.\n", iface, flags);
2230 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2233 /*****************************************************************************
2234 * IDirectDrawSurface7::PageUnlock
2236 * Allows a sysmem surface to be paged out
2238 * Params:
2239 * Flags: Not used, must be 0(unchecked)
2241 * Returns:
2242 * DD_OK, because it's a stub
2244 *****************************************************************************/
2245 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2247 TRACE("iface %p, flags %#x.\n", iface, Flags);
2249 return DD_OK;
2252 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2254 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2255 TRACE("iface %p, flags %#x.\n", iface, flags);
2257 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2260 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2262 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2263 TRACE("iface %p, flags %#x.\n", iface, flags);
2265 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2268 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2270 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2271 TRACE("iface %p, flags %#x.\n", iface, flags);
2273 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2276 /*****************************************************************************
2277 * IDirectDrawSurface7::BltBatch
2279 * An unimplemented function
2281 * Params:
2284 * Returns:
2285 * DDERR_UNSUPPORTED
2287 *****************************************************************************/
2288 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2290 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2292 /* MSDN: "not currently implemented" */
2293 return DDERR_UNSUPPORTED;
2296 static HRESULT WINAPI ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2298 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2299 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2301 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2304 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2306 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2307 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2309 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2312 static HRESULT WINAPI ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2314 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2315 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2317 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2320 static HRESULT WINAPI ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2322 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2323 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2325 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2328 /*****************************************************************************
2329 * IDirectDrawSurface7::EnumAttachedSurfaces
2331 * Enumerates all surfaces attached to this surface
2333 * Params:
2334 * context: Pointer to pass unmodified to the callback
2335 * cb: Callback function to call for each surface
2337 * Returns:
2338 * DD_OK on success
2339 * DDERR_INVALIDPARAMS if cb is NULL
2341 *****************************************************************************/
2342 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2343 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2345 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2346 IDirectDrawSurfaceImpl *surf;
2347 DDSURFACEDESC2 desc;
2348 int i;
2350 /* Attached surfaces aren't handled in WineD3D */
2351 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2353 if(!cb)
2354 return DDERR_INVALIDPARAMS;
2356 EnterCriticalSection(&ddraw_cs);
2357 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2359 surf = This->complex_array[i];
2360 if(!surf) break;
2362 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2363 desc = surf->surface_desc;
2364 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2365 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2367 LeaveCriticalSection(&ddraw_cs);
2368 return DD_OK;
2372 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
2374 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2375 desc = surf->surface_desc;
2376 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2377 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2379 LeaveCriticalSection(&ddraw_cs);
2380 return DD_OK;
2384 TRACE(" end of enumeration.\n");
2386 LeaveCriticalSection(&ddraw_cs);
2387 return DD_OK;
2390 struct callback_info2
2392 LPDDENUMSURFACESCALLBACK2 callback;
2393 void *context;
2396 struct callback_info
2398 LPDDENUMSURFACESCALLBACK callback;
2399 void *context;
2402 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2404 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
2405 const struct callback_info2 *info = context;
2407 ddraw_surface4_AddRef(&This->IDirectDrawSurface4_iface);
2408 ddraw_surface7_Release(surface);
2410 return info->callback(&This->IDirectDrawSurface4_iface, surface_desc, info->context);
2413 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2415 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
2416 const struct callback_info *info = context;
2418 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
2419 ddraw_surface7_Release(surface);
2421 /* FIXME: Check surface_test.dwSize */
2422 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2423 (DDSURFACEDESC *)surface_desc, info->context);
2426 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2427 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2429 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2430 struct callback_info2 info;
2432 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2434 info.callback = callback;
2435 info.context = context;
2437 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2438 &info, EnumCallback2);
2441 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2442 void *context, LPDDENUMSURFACESCALLBACK callback)
2444 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2445 struct callback_info info;
2447 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2449 info.callback = callback;
2450 info.context = context;
2452 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2453 &info, EnumCallback);
2456 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2457 void *context, LPDDENUMSURFACESCALLBACK callback)
2459 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2460 struct callback_info info;
2462 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2464 info.callback = callback;
2465 info.context = context;
2467 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2468 &info, EnumCallback);
2471 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2472 void *context, LPDDENUMSURFACESCALLBACK callback)
2474 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2475 struct callback_info info;
2477 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2479 info.callback = callback;
2480 info.context = context;
2482 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2483 &info, EnumCallback);
2486 /*****************************************************************************
2487 * IDirectDrawSurface7::EnumOverlayZOrders
2489 * "Enumerates the overlay surfaces on the specified destination"
2491 * Params:
2492 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2493 * context: context to pass back to the callback
2494 * cb: callback function to call for each enumerated surface
2496 * Returns:
2497 * DD_OK, because it's a stub
2499 *****************************************************************************/
2500 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2501 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2503 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2505 return DD_OK;
2508 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2509 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2511 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2512 struct callback_info2 info;
2514 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2516 info.callback = callback;
2517 info.context = context;
2519 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2520 flags, &info, EnumCallback2);
2523 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2524 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2526 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2527 struct callback_info info;
2529 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2531 info.callback = callback;
2532 info.context = context;
2534 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2535 flags, &info, EnumCallback);
2538 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2539 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2541 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2542 struct callback_info info;
2544 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2546 info.callback = callback;
2547 info.context = context;
2549 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2550 flags, &info, EnumCallback);
2553 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2554 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2556 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2557 struct callback_info info;
2559 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2561 info.callback = callback;
2562 info.context = context;
2564 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2565 flags, &info, EnumCallback);
2568 /*****************************************************************************
2569 * IDirectDrawSurface7::GetBltStatus
2571 * Returns the blitting status
2573 * Params:
2574 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2576 * Returns:
2577 * See IWineD3DSurface::Blt
2579 *****************************************************************************/
2580 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2582 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2583 HRESULT hr;
2585 TRACE("iface %p, flags %#x.\n", iface, Flags);
2587 EnterCriticalSection(&ddraw_cs);
2588 hr = wined3d_surface_get_blt_status(This->wined3d_surface, Flags);
2589 LeaveCriticalSection(&ddraw_cs);
2590 switch(hr)
2592 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2593 default: return hr;
2597 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2599 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2600 TRACE("iface %p, flags %#x.\n", iface, flags);
2602 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2605 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2607 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2608 TRACE("iface %p, flags %#x.\n", iface, flags);
2610 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2613 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2615 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2616 TRACE("iface %p, flags %#x.\n", iface, flags);
2618 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2621 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
2623 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2624 TRACE("iface %p, flags %#x.\n", iface, flags);
2626 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2629 /*****************************************************************************
2630 * IDirectDrawSurface7::GetColorKey
2632 * Returns the color key assigned to the surface
2634 * Params:
2635 * Flags: Some flags
2636 * CKey: Address to store the key to
2638 * Returns:
2639 * DD_OK on success
2640 * DDERR_INVALIDPARAMS if CKey is NULL
2642 *****************************************************************************/
2643 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2645 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2647 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2649 if(!CKey)
2650 return DDERR_INVALIDPARAMS;
2652 EnterCriticalSection(&ddraw_cs);
2654 switch (Flags)
2656 case DDCKEY_DESTBLT:
2657 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
2659 LeaveCriticalSection(&ddraw_cs);
2660 return DDERR_NOCOLORKEY;
2662 *CKey = This->surface_desc.ddckCKDestBlt;
2663 break;
2665 case DDCKEY_DESTOVERLAY:
2666 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
2668 LeaveCriticalSection(&ddraw_cs);
2669 return DDERR_NOCOLORKEY;
2671 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
2672 break;
2674 case DDCKEY_SRCBLT:
2675 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
2677 LeaveCriticalSection(&ddraw_cs);
2678 return DDERR_NOCOLORKEY;
2680 *CKey = This->surface_desc.ddckCKSrcBlt;
2681 break;
2683 case DDCKEY_SRCOVERLAY:
2684 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
2686 LeaveCriticalSection(&ddraw_cs);
2687 return DDERR_NOCOLORKEY;
2689 *CKey = This->surface_desc.ddckCKSrcOverlay;
2690 break;
2692 default:
2693 LeaveCriticalSection(&ddraw_cs);
2694 return DDERR_INVALIDPARAMS;
2697 LeaveCriticalSection(&ddraw_cs);
2698 return DD_OK;
2701 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
2703 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2704 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2706 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2709 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2711 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2712 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2714 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2717 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
2719 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2720 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2722 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2725 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
2727 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2728 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2730 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2733 /*****************************************************************************
2734 * IDirectDrawSurface7::GetFlipStatus
2736 * Returns the flipping status of the surface
2738 * Params:
2739 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
2741 * Returns:
2742 * See IWineD3DSurface::GetFlipStatus
2744 *****************************************************************************/
2745 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2747 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2748 HRESULT hr;
2750 TRACE("iface %p, flags %#x.\n", iface, Flags);
2752 EnterCriticalSection(&ddraw_cs);
2753 hr = wined3d_surface_get_flip_status(This->wined3d_surface, Flags);
2754 LeaveCriticalSection(&ddraw_cs);
2755 switch(hr)
2757 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2758 default: return hr;
2762 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
2764 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2765 TRACE("iface %p, flags %#x.\n", iface, flags);
2767 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2770 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
2772 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2773 TRACE("iface %p, flags %#x.\n", iface, flags);
2775 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2778 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
2780 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2781 TRACE("iface %p, flags %#x.\n", iface, flags);
2783 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2786 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
2788 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2789 TRACE("iface %p, flags %#x.\n", iface, flags);
2791 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2794 /*****************************************************************************
2795 * IDirectDrawSurface7::GetOverlayPosition
2797 * Returns the display coordinates of a visible and active overlay surface
2799 * Params:
2803 * Returns:
2804 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
2805 *****************************************************************************/
2806 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
2808 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2809 HRESULT hr;
2811 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
2813 EnterCriticalSection(&ddraw_cs);
2814 hr = wined3d_surface_get_overlay_position(This->wined3d_surface, X, Y);
2815 LeaveCriticalSection(&ddraw_cs);
2816 return hr;
2819 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
2821 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2822 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2824 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2827 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
2829 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2830 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2832 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2835 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
2837 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2838 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2840 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2843 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
2845 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2846 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2848 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2851 /*****************************************************************************
2852 * IDirectDrawSurface7::GetPixelFormat
2854 * Returns the pixel format of the Surface
2856 * Params:
2857 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
2858 * format should be written
2860 * Returns:
2861 * DD_OK on success
2862 * DDERR_INVALIDPARAMS if PixelFormat is NULL
2864 *****************************************************************************/
2865 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
2867 /* What is DDERR_INVALIDSURFACETYPE for here? */
2868 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2870 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
2872 if(!PixelFormat)
2873 return DDERR_INVALIDPARAMS;
2875 EnterCriticalSection(&ddraw_cs);
2876 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
2877 LeaveCriticalSection(&ddraw_cs);
2879 return DD_OK;
2882 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
2884 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2885 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2887 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2890 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
2892 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2893 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2895 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2898 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
2900 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2901 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2903 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2906 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
2908 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2909 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2911 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2914 /*****************************************************************************
2915 * IDirectDrawSurface7::GetSurfaceDesc
2917 * Returns the description of this surface
2919 * Params:
2920 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
2921 * surface desc
2923 * Returns:
2924 * DD_OK on success
2925 * DDERR_INVALIDPARAMS if DDSD is NULL
2927 *****************************************************************************/
2928 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
2930 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2932 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2934 if(!DDSD)
2935 return DDERR_INVALIDPARAMS;
2937 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
2939 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
2940 return DDERR_INVALIDPARAMS;
2943 EnterCriticalSection(&ddraw_cs);
2944 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
2945 TRACE("Returning surface desc:\n");
2946 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
2948 LeaveCriticalSection(&ddraw_cs);
2949 return DD_OK;
2952 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
2954 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2955 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2957 return ddraw_surface7_GetSurfaceDesc(&This->IDirectDrawSurface7_iface, DDSD);
2960 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
2962 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2964 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
2966 if (!surface_desc) return DDERR_INVALIDPARAMS;
2968 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
2970 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
2971 return DDERR_INVALIDPARAMS;
2974 EnterCriticalSection(&ddraw_cs);
2975 DDSD2_to_DDSD(&This->surface_desc, surface_desc);
2976 TRACE("Returning surface desc:\n");
2977 if (TRACE_ON(ddraw))
2979 /* DDRAW_dump_surface_desc handles the smaller size */
2980 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2983 LeaveCriticalSection(&ddraw_cs);
2984 return DD_OK;
2987 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
2989 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2990 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2992 return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
2995 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
2997 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2998 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
3000 return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
3003 /*****************************************************************************
3004 * IDirectDrawSurface7::Initialize
3006 * Initializes the surface. This is a no-op in Wine
3008 * Params:
3009 * DD: Pointer to an DirectDraw interface
3010 * DDSD: Surface description for initialization
3012 * Returns:
3013 * DDERR_ALREADYINITIALIZED
3015 *****************************************************************************/
3016 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
3017 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3019 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3021 return DDERR_ALREADYINITIALIZED;
3024 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
3025 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
3027 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3028 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3030 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
3031 ddraw, surface_desc);
3034 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
3035 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3037 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3038 DDSURFACEDESC2 surface_desc2;
3039 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3041 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3042 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
3043 ddraw, surface_desc ? &surface_desc2 : NULL);
3046 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
3047 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3049 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3050 DDSURFACEDESC2 surface_desc2;
3051 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3053 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3054 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
3055 ddraw, surface_desc ? &surface_desc2 : NULL);
3058 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
3059 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
3061 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3062 DDSURFACEDESC2 surface_desc2;
3063 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
3065 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
3066 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
3067 ddraw, surface_desc ? &surface_desc2 : NULL);
3070 /*****************************************************************************
3071 * IDirect3DTexture1::Initialize
3073 * The sdk says it's not implemented
3075 * Params:
3078 * Returns
3079 * DDERR_UNSUPPORTED
3081 *****************************************************************************/
3082 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
3083 IDirect3DDevice *device, IDirectDrawSurface *surface)
3085 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
3087 return DDERR_UNSUPPORTED; /* Unchecked */
3090 /*****************************************************************************
3091 * IDirectDrawSurface7::IsLost
3093 * Checks if the surface is lost
3095 * Returns:
3096 * DD_OK, if the surface is usable
3097 * DDERR_ISLOST if the surface is lost
3098 * See IWineD3DSurface::IsLost for more details
3100 *****************************************************************************/
3101 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3103 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3104 HRESULT hr;
3106 TRACE("iface %p.\n", iface);
3108 EnterCriticalSection(&ddraw_cs);
3109 hr = wined3d_surface_is_lost(This->wined3d_surface);
3110 LeaveCriticalSection(&ddraw_cs);
3111 switch(hr)
3113 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
3114 * WineD3D uses the same error for surfaces
3116 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
3117 default: return hr;
3121 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3123 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3124 TRACE("iface %p.\n", iface);
3126 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3129 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3131 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3132 TRACE("iface %p.\n", iface);
3134 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3137 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3139 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3140 TRACE("iface %p.\n", iface);
3142 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3145 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3147 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3148 TRACE("iface %p.\n", iface);
3150 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3153 /*****************************************************************************
3154 * IDirectDrawSurface7::Restore
3156 * Restores a lost surface. This makes the surface usable again, but
3157 * doesn't reload its old contents
3159 * Returns:
3160 * DD_OK on success
3161 * See IWineD3DSurface::Restore for more details
3163 *****************************************************************************/
3164 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3166 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3167 HRESULT hr;
3169 TRACE("iface %p.\n", iface);
3171 EnterCriticalSection(&ddraw_cs);
3172 hr = wined3d_surface_restore(This->wined3d_surface);
3173 LeaveCriticalSection(&ddraw_cs);
3174 return hr;
3177 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3179 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3180 TRACE("iface %p.\n", iface);
3182 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3185 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3187 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3188 TRACE("iface %p.\n", iface);
3190 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3193 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3195 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3196 TRACE("iface %p.\n", iface);
3198 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3201 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3203 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3204 TRACE("iface %p.\n", iface);
3206 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3209 /*****************************************************************************
3210 * IDirectDrawSurface7::SetOverlayPosition
3212 * Changes the display coordinates of an overlay surface
3214 * Params:
3215 * X:
3216 * Y:
3218 * Returns:
3219 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3220 *****************************************************************************/
3221 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
3223 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3224 HRESULT hr;
3226 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
3228 EnterCriticalSection(&ddraw_cs);
3229 hr = wined3d_surface_set_overlay_position(This->wined3d_surface, X, Y);
3230 LeaveCriticalSection(&ddraw_cs);
3231 return hr;
3234 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3236 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3237 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3239 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3242 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3244 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3245 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3247 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3250 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3252 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3253 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3255 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3258 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3260 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3261 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3263 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3266 /*****************************************************************************
3267 * IDirectDrawSurface7::UpdateOverlay
3269 * Modifies the attributes of an overlay surface.
3271 * Params:
3272 * SrcRect: The section of the source being used for the overlay
3273 * DstSurface: Address of the surface that is overlaid
3274 * DstRect: Place of the overlay
3275 * Flags: some DDOVER_* flags
3277 * Returns:
3278 * DDERR_UNSUPPORTED, because we don't support overlays
3280 *****************************************************************************/
3281 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
3282 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
3284 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3285 IDirectDrawSurfaceImpl *Dst = unsafe_impl_from_IDirectDrawSurface7(DstSurface);
3286 HRESULT hr;
3288 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3289 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
3291 EnterCriticalSection(&ddraw_cs);
3292 hr = wined3d_surface_update_overlay(This->wined3d_surface, SrcRect,
3293 Dst ? Dst->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
3294 LeaveCriticalSection(&ddraw_cs);
3295 switch(hr) {
3296 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3297 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3298 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3299 default:
3300 return hr;
3304 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3305 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3307 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3308 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3309 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3310 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3312 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3313 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3316 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3317 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3319 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3320 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3321 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3322 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3324 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3325 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3328 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3329 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3331 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3332 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3333 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3334 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3336 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3337 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3340 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3341 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3343 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3344 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3345 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3346 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3348 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3349 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3352 /*****************************************************************************
3353 * IDirectDrawSurface7::UpdateOverlayDisplay
3355 * The DX7 sdk says that it's not implemented
3357 * Params:
3358 * Flags: ?
3360 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3362 *****************************************************************************/
3363 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3365 TRACE("iface %p, flags %#x.\n", iface, Flags);
3367 return DDERR_UNSUPPORTED;
3370 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3372 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3373 TRACE("iface %p, flags %#x.\n", iface, flags);
3375 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3378 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3380 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3381 TRACE("iface %p, flags %#x.\n", iface, flags);
3383 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3386 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3388 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3389 TRACE("iface %p, flags %#x.\n", iface, flags);
3391 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3394 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3396 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3397 TRACE("iface %p, flags %#x.\n", iface, flags);
3399 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3402 /*****************************************************************************
3403 * IDirectDrawSurface7::UpdateOverlayZOrder
3405 * Sets an overlay's Z order
3407 * Params:
3408 * Flags: DDOVERZ_* flags
3409 * DDSRef: Defines the relative position in the overlay chain
3411 * Returns:
3412 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3414 *****************************************************************************/
3415 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3416 DWORD Flags, IDirectDrawSurface7 *DDSRef)
3418 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3419 IDirectDrawSurfaceImpl *Ref = unsafe_impl_from_IDirectDrawSurface7(DDSRef);
3420 HRESULT hr;
3422 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
3424 EnterCriticalSection(&ddraw_cs);
3425 hr = wined3d_surface_update_overlay_z_order(This->wined3d_surface,
3426 Flags, Ref ? Ref->wined3d_surface : NULL);
3427 LeaveCriticalSection(&ddraw_cs);
3428 return hr;
3431 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3432 DWORD flags, IDirectDrawSurface4 *reference)
3434 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3435 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3436 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3438 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3439 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3442 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3443 DWORD flags, IDirectDrawSurface3 *reference)
3445 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3446 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3447 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3449 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3450 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3453 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3454 DWORD flags, IDirectDrawSurface2 *reference)
3456 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3457 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3458 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3460 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3461 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3464 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3465 DWORD flags, IDirectDrawSurface *reference)
3467 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3468 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3469 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3471 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3472 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3475 /*****************************************************************************
3476 * IDirectDrawSurface7::GetDDInterface
3478 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3479 * surface belongs to
3481 * Params:
3482 * DD: Address to write the interface pointer to
3484 * Returns:
3485 * DD_OK on success
3486 * DDERR_INVALIDPARAMS if DD is NULL
3488 *****************************************************************************/
3489 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3491 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3493 TRACE("iface %p, ddraw %p.\n", iface, DD);
3495 if(!DD)
3496 return DDERR_INVALIDPARAMS;
3498 switch(This->version)
3500 case 7:
3501 *DD = &This->ddraw->IDirectDraw7_iface;
3502 break;
3504 case 4:
3505 *DD = &This->ddraw->IDirectDraw4_iface;
3506 break;
3508 case 2:
3509 *DD = &This->ddraw->IDirectDraw2_iface;
3510 break;
3512 case 1:
3513 *DD = &This->ddraw->IDirectDraw_iface;
3514 break;
3517 IUnknown_AddRef((IUnknown *)*DD);
3519 return DD_OK;
3522 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3524 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3525 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3527 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3530 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3532 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3533 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3535 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3538 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3540 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3541 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3543 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3546 /* This seems also windows implementation specific - I don't think WineD3D needs this */
3547 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3549 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3550 volatile IDirectDrawSurfaceImpl* vThis = This;
3552 TRACE("iface %p.\n", iface);
3554 EnterCriticalSection(&ddraw_cs);
3555 /* A uniqueness value of 0 is apparently special.
3556 * This needs to be checked.
3557 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
3559 while (1) {
3560 DWORD old_uniqueness_value = vThis->uniqueness_value;
3561 DWORD new_uniqueness_value = old_uniqueness_value+1;
3563 if (old_uniqueness_value == 0) break;
3564 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
3566 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
3567 old_uniqueness_value,
3568 new_uniqueness_value)
3569 == old_uniqueness_value)
3570 break;
3573 LeaveCriticalSection(&ddraw_cs);
3574 return DD_OK;
3577 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
3579 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3580 TRACE("iface %p.\n", iface);
3582 return ddraw_surface7_ChangeUniquenessValue(&This->IDirectDrawSurface7_iface);
3585 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
3587 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3589 TRACE("iface %p, value %p.\n", iface, pValue);
3591 EnterCriticalSection(&ddraw_cs);
3592 *pValue = This->uniqueness_value;
3593 LeaveCriticalSection(&ddraw_cs);
3594 return DD_OK;
3597 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
3599 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3600 TRACE("iface %p, value %p.\n", iface, pValue);
3602 return ddraw_surface7_GetUniquenessValue(&This->IDirectDrawSurface7_iface, pValue);
3605 /*****************************************************************************
3606 * IDirectDrawSurface7::SetLOD
3608 * Sets the level of detail of a texture
3610 * Params:
3611 * MaxLOD: LOD to set
3613 * Returns:
3614 * DD_OK on success
3615 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3617 *****************************************************************************/
3618 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
3620 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3621 HRESULT hr;
3623 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
3625 EnterCriticalSection(&ddraw_cs);
3626 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3628 LeaveCriticalSection(&ddraw_cs);
3629 return DDERR_INVALIDOBJECT;
3632 if (!This->wined3d_texture)
3634 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
3635 LeaveCriticalSection(&ddraw_cs);
3636 return DDERR_INVALIDOBJECT;
3639 hr = wined3d_texture_set_lod(This->wined3d_texture, MaxLOD);
3640 LeaveCriticalSection(&ddraw_cs);
3641 return hr;
3644 /*****************************************************************************
3645 * IDirectDrawSurface7::GetLOD
3647 * Returns the level of detail of a Direct3D texture
3649 * Params:
3650 * MaxLOD: Address to write the LOD to
3652 * Returns:
3653 * DD_OK on success
3654 * DDERR_INVALIDPARAMS if MaxLOD is NULL
3655 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3657 *****************************************************************************/
3658 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
3660 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3662 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
3664 if(!MaxLOD)
3665 return DDERR_INVALIDPARAMS;
3667 EnterCriticalSection(&ddraw_cs);
3668 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3670 LeaveCriticalSection(&ddraw_cs);
3671 return DDERR_INVALIDOBJECT;
3674 *MaxLOD = wined3d_texture_get_lod(This->wined3d_texture);
3675 LeaveCriticalSection(&ddraw_cs);
3676 return DD_OK;
3679 /*****************************************************************************
3680 * IDirectDrawSurface7::BltFast
3682 * Performs a fast Blit.
3684 * Params:
3685 * dstx: The x coordinate to blit to on the destination
3686 * dsty: The y coordinate to blit to on the destination
3687 * Source: The source surface
3688 * rsrc: The source rectangle
3689 * trans: Type of transfer. Some DDBLTFAST_* flags
3691 * Returns:
3692 * DD_OK on success
3693 * For more details, see IWineD3DSurface::BltFast
3695 *****************************************************************************/
3696 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
3697 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
3699 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3700 IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface7(Source);
3701 DWORD src_w, src_h, dst_w, dst_h;
3702 HRESULT hr = DD_OK;
3703 DWORD flags = 0;
3704 RECT dst_rect;
3706 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3707 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
3709 dst_w = This->surface_desc.dwWidth;
3710 dst_h = This->surface_desc.dwHeight;
3712 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
3713 * in that case
3715 if(rsrc)
3717 src_w = rsrc->right - rsrc->left;
3718 src_h = rsrc->bottom - rsrc->top;
3720 else
3722 src_w = src->surface_desc.dwWidth;
3723 src_h = src->surface_desc.dwHeight;
3726 if (src_w > dst_w || dstx > dst_w - src_w
3727 || src_h > dst_h || dsty > dst_h - src_h)
3729 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
3730 return DDERR_INVALIDRECT;
3733 SetRect(&dst_rect, dstx, dsty, dstx + src_w, dsty + src_h);
3734 if (trans & DDBLTFAST_SRCCOLORKEY)
3735 flags |= WINEDDBLT_KEYSRC;
3736 if (trans & DDBLTFAST_DESTCOLORKEY)
3737 flags |= WINEDDBLT_KEYDEST;
3738 if (trans & DDBLTFAST_WAIT)
3739 flags |= WINEDDBLT_WAIT;
3740 if (trans & DDBLTFAST_DONOTWAIT)
3741 flags |= WINEDDBLT_DONOTWAIT;
3743 EnterCriticalSection(&ddraw_cs);
3744 if (src->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
3745 hr = ddraw_surface_update_frontbuffer(src, rsrc, TRUE);
3746 if (SUCCEEDED(hr))
3747 hr = wined3d_surface_blt(This->wined3d_surface, &dst_rect,
3748 src->wined3d_surface, rsrc, flags, NULL, WINED3DTEXF_POINT);
3749 if (SUCCEEDED(hr) && (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER))
3750 hr = ddraw_surface_update_frontbuffer(This, &dst_rect, FALSE);
3751 LeaveCriticalSection(&ddraw_cs);
3752 switch(hr)
3754 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
3755 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
3756 default: return hr;
3760 static HRESULT WINAPI ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
3761 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
3763 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3764 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
3765 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3766 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3768 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3769 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3772 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
3773 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
3775 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3776 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
3777 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3778 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3780 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3781 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3784 static HRESULT WINAPI ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
3785 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
3787 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3788 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
3789 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3790 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3792 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3793 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3796 static HRESULT WINAPI ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
3797 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
3799 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3800 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
3801 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3802 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3804 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3805 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3808 /*****************************************************************************
3809 * IDirectDrawSurface7::GetClipper
3811 * Returns the IDirectDrawClipper interface of the clipper assigned to this
3812 * surface
3814 * Params:
3815 * Clipper: Address to store the interface pointer at
3817 * Returns:
3818 * DD_OK on success
3819 * DDERR_INVALIDPARAMS if Clipper is NULL
3820 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
3822 *****************************************************************************/
3823 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
3825 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3827 TRACE("iface %p, clipper %p.\n", iface, Clipper);
3829 if(!Clipper)
3831 LeaveCriticalSection(&ddraw_cs);
3832 return DDERR_INVALIDPARAMS;
3835 EnterCriticalSection(&ddraw_cs);
3836 if(This->clipper == NULL)
3838 LeaveCriticalSection(&ddraw_cs);
3839 return DDERR_NOCLIPPERATTACHED;
3842 *Clipper = (IDirectDrawClipper *)This->clipper;
3843 IDirectDrawClipper_AddRef(*Clipper);
3844 LeaveCriticalSection(&ddraw_cs);
3845 return DD_OK;
3848 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
3850 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3851 TRACE("iface %p, clipper %p.\n", iface, clipper);
3853 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3856 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
3858 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3859 TRACE("iface %p, clipper %p.\n", iface, clipper);
3861 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3864 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
3866 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3867 TRACE("iface %p, clipper %p.\n", iface, clipper);
3869 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3872 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
3874 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3875 TRACE("iface %p, clipper %p.\n", iface, clipper);
3877 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3880 /*****************************************************************************
3881 * IDirectDrawSurface7::SetClipper
3883 * Sets a clipper for the surface
3885 * Params:
3886 * Clipper: IDirectDrawClipper interface of the clipper to set
3888 * Returns:
3889 * DD_OK on success
3891 *****************************************************************************/
3892 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
3893 IDirectDrawClipper *iclipper)
3895 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3896 IDirectDrawClipperImpl *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
3897 IDirectDrawClipperImpl *oldClipper = This->clipper;
3898 HWND clipWindow;
3899 HRESULT hr;
3901 TRACE("iface %p, clipper %p.\n", iface, iclipper);
3903 EnterCriticalSection(&ddraw_cs);
3904 if (clipper == This->clipper)
3906 LeaveCriticalSection(&ddraw_cs);
3907 return DD_OK;
3910 This->clipper = clipper;
3912 if (clipper != NULL)
3913 IDirectDrawClipper_AddRef(iclipper);
3914 if(oldClipper)
3915 IDirectDrawClipper_Release(&oldClipper->IDirectDrawClipper_iface);
3917 hr = wined3d_surface_set_clipper(This->wined3d_surface,
3918 This->clipper ? This->clipper->wineD3DClipper : NULL);
3920 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && This->ddraw->wined3d_swapchain)
3922 clipWindow = NULL;
3923 if(clipper) {
3924 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
3927 if (clipWindow)
3929 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, clipWindow);
3930 This->ddraw->swapchain_window = clipWindow;
3932 else
3934 wined3d_swapchain_set_window(This->ddraw->wined3d_swapchain, This->ddraw->d3d_window);
3935 This->ddraw->swapchain_window = This->ddraw->dest_window;
3939 LeaveCriticalSection(&ddraw_cs);
3940 return hr;
3943 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
3945 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3946 TRACE("iface %p, clipper %p.\n", iface, clipper);
3948 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3951 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
3953 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3954 TRACE("iface %p, clipper %p.\n", iface, clipper);
3956 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3959 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
3961 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3962 TRACE("iface %p, clipper %p.\n", iface, clipper);
3964 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3967 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
3969 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3970 TRACE("iface %p, clipper %p.\n", iface, clipper);
3972 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3975 /*****************************************************************************
3976 * IDirectDrawSurface7::SetSurfaceDesc
3978 * Sets the surface description. It can override the pixel format, the surface
3979 * memory, ...
3980 * It's not really tested.
3982 * Params:
3983 * DDSD: Pointer to the new surface description to set
3984 * Flags: Some flags
3986 * Returns:
3987 * DD_OK on success
3988 * DDERR_INVALIDPARAMS if DDSD is NULL
3990 *****************************************************************************/
3991 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
3993 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3994 enum wined3d_format_id newFormat = WINED3DFMT_UNKNOWN;
3995 HRESULT hr;
3997 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
3999 if (!DDSD)
4001 WARN("DDSD is NULL, returning DDERR_INVALIDPARAMS\n");
4002 return DDERR_INVALIDPARAMS;
4004 if (Flags)
4006 WARN("Flags is %x, returning DDERR_INVALIDPARAMS\n", Flags);
4007 return DDERR_INVALIDPARAMS;
4010 EnterCriticalSection(&ddraw_cs);
4011 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
4013 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
4015 if(newFormat == WINED3DFMT_UNKNOWN)
4017 ERR("Requested to set an unknown pixelformat\n");
4018 LeaveCriticalSection(&ddraw_cs);
4019 return DDERR_INVALIDPARAMS;
4021 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
4023 hr = wined3d_surface_set_format(This->wined3d_surface, newFormat);
4024 if (FAILED(hr))
4026 LeaveCriticalSection(&ddraw_cs);
4027 return hr;
4031 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
4033 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTOVERLAY,
4034 (WINEDDCOLORKEY *)&DDSD->u3.ddckCKDestOverlay);
4036 if (DDSD->dwFlags & DDSD_CKDESTBLT)
4038 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTBLT,
4039 (WINEDDCOLORKEY *)&DDSD->ddckCKDestBlt);
4041 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
4043 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCOVERLAY,
4044 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcOverlay);
4046 if (DDSD->dwFlags & DDSD_CKSRCBLT)
4048 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCBLT,
4049 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcBlt);
4051 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
4053 hr = wined3d_surface_set_mem(This->wined3d_surface, DDSD->lpSurface);
4054 if (FAILED(hr))
4056 /* No need for a trace here, wined3d does that for us */
4057 switch(hr)
4059 case WINED3DERR_INVALIDCALL:
4060 LeaveCriticalSection(&ddraw_cs);
4061 return DDERR_INVALIDPARAMS;
4062 default:
4063 break; /* Go on */
4068 This->surface_desc = *DDSD;
4070 LeaveCriticalSection(&ddraw_cs);
4071 return DD_OK;
4074 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
4075 DDSURFACEDESC2 *surface_desc, DWORD flags)
4077 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4078 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4080 return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
4081 surface_desc, flags);
4084 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
4085 DDSURFACEDESC *surface_desc, DWORD flags)
4087 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4088 DDSURFACEDESC2 surface_desc2;
4089 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
4091 if (surface_desc) DDSD_to_DDSD2(surface_desc, &surface_desc2);
4092 return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
4093 surface_desc ? &surface_desc2 : NULL, flags);
4096 /*****************************************************************************
4097 * IDirectDrawSurface7::GetPalette
4099 * Returns the IDirectDrawPalette interface of the palette currently assigned
4100 * to the surface
4102 * Params:
4103 * Pal: Address to write the interface pointer to
4105 * Returns:
4106 * DD_OK on success
4107 * DDERR_INVALIDPARAMS if Pal is NULL
4109 *****************************************************************************/
4110 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
4112 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4113 struct wined3d_palette *wined3d_palette;
4114 HRESULT hr = DD_OK;
4116 TRACE("iface %p, palette %p.\n", iface, Pal);
4118 if(!Pal)
4119 return DDERR_INVALIDPARAMS;
4121 EnterCriticalSection(&ddraw_cs);
4122 wined3d_palette = wined3d_surface_get_palette(This->wined3d_surface);
4123 if (wined3d_palette)
4125 *Pal = wined3d_palette_get_parent(wined3d_palette);
4126 IDirectDrawPalette_AddRef(*Pal);
4128 else
4130 *Pal = NULL;
4131 hr = DDERR_NOPALETTEATTACHED;
4134 LeaveCriticalSection(&ddraw_cs);
4135 return hr;
4138 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4140 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4141 TRACE("iface %p, palette %p.\n", iface, palette);
4143 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4146 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4148 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4149 TRACE("iface %p, palette %p.\n", iface, palette);
4151 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4154 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4156 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4157 TRACE("iface %p, palette %p.\n", iface, palette);
4159 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4162 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4164 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4165 TRACE("iface %p, palette %p.\n", iface, palette);
4167 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4170 /*****************************************************************************
4171 * SetColorKeyEnum
4173 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
4174 * recursively in the surface tree
4176 *****************************************************************************/
4177 struct SCKContext
4179 HRESULT ret;
4180 WINEDDCOLORKEY *CKey;
4181 DWORD Flags;
4184 static HRESULT WINAPI
4185 SetColorKeyEnum(IDirectDrawSurface7 *surface,
4186 DDSURFACEDESC2 *desc,
4187 void *context)
4189 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
4190 struct SCKContext *ctx = context;
4191 HRESULT hr;
4193 hr = wined3d_surface_set_color_key(This->wined3d_surface, ctx->Flags, ctx->CKey);
4194 if (FAILED(hr))
4196 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
4197 ctx->ret = hr;
4200 ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
4201 ddraw_surface7_Release(surface);
4203 return DDENUMRET_OK;
4206 /*****************************************************************************
4207 * IDirectDrawSurface7::SetColorKey
4209 * Sets the color keying options for the surface. Observations showed that
4210 * in case of complex surfaces the color key has to be assigned to all
4211 * sublevels.
4213 * Params:
4214 * Flags: DDCKEY_*
4215 * CKey: The new color key
4217 * Returns:
4218 * DD_OK on success
4219 * See IWineD3DSurface::SetColorKey for details
4221 *****************************************************************************/
4222 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
4224 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4225 DDCOLORKEY FixedCKey;
4226 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
4228 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
4230 EnterCriticalSection(&ddraw_cs);
4231 if (CKey)
4233 FixedCKey = *CKey;
4234 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
4235 if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
4236 FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
4238 switch (Flags & ~DDCKEY_COLORSPACE)
4240 case DDCKEY_DESTBLT:
4241 This->surface_desc.ddckCKDestBlt = FixedCKey;
4242 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4243 break;
4245 case DDCKEY_DESTOVERLAY:
4246 This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
4247 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4248 break;
4250 case DDCKEY_SRCOVERLAY:
4251 This->surface_desc.ddckCKSrcOverlay = FixedCKey;
4252 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4253 break;
4255 case DDCKEY_SRCBLT:
4256 This->surface_desc.ddckCKSrcBlt = FixedCKey;
4257 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4258 break;
4260 default:
4261 LeaveCriticalSection(&ddraw_cs);
4262 return DDERR_INVALIDPARAMS;
4265 else
4267 switch (Flags & ~DDCKEY_COLORSPACE)
4269 case DDCKEY_DESTBLT:
4270 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4271 break;
4273 case DDCKEY_DESTOVERLAY:
4274 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4275 break;
4277 case DDCKEY_SRCOVERLAY:
4278 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4279 break;
4281 case DDCKEY_SRCBLT:
4282 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4283 break;
4285 default:
4286 LeaveCriticalSection(&ddraw_cs);
4287 return DDERR_INVALIDPARAMS;
4290 ctx.ret = wined3d_surface_set_color_key(This->wined3d_surface, Flags, ctx.CKey);
4291 ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
4292 LeaveCriticalSection(&ddraw_cs);
4293 switch(ctx.ret)
4295 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
4296 default: return ctx.ret;
4300 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4302 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4303 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4305 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4308 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4310 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4311 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4313 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4316 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4318 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4319 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4321 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4324 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4326 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4327 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4329 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4332 /*****************************************************************************
4333 * IDirectDrawSurface7::SetPalette
4335 * Assigns a DirectDrawPalette object to the surface
4337 * Params:
4338 * Pal: Interface to the palette to set
4340 * Returns:
4341 * DD_OK on success
4343 *****************************************************************************/
4344 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
4346 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4347 IDirectDrawPalette *oldPal;
4348 IDirectDrawSurfaceImpl *surf;
4349 IDirectDrawPaletteImpl *PalImpl = unsafe_impl_from_IDirectDrawPalette(Pal);
4350 HRESULT hr;
4352 TRACE("iface %p, palette %p.\n", iface, Pal);
4354 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
4355 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
4356 return DDERR_INVALIDPIXELFORMAT;
4359 if (This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_MIPMAPSUBLEVEL)
4361 return DDERR_NOTONMIPMAPSUBLEVEL;
4364 /* Find the old palette */
4365 EnterCriticalSection(&ddraw_cs);
4366 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
4367 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
4369 LeaveCriticalSection(&ddraw_cs);
4370 return hr;
4372 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
4374 /* Set the new Palette */
4375 wined3d_surface_set_palette(This->wined3d_surface, PalImpl ? PalImpl->wineD3DPalette : NULL);
4376 /* AddRef the Palette */
4377 if(Pal) IDirectDrawPalette_AddRef(Pal);
4379 /* Release the old palette */
4380 if(oldPal) IDirectDrawPalette_Release(oldPal);
4382 /* Update the wined3d frontbuffer if this is the frontbuffer. */
4383 if ((This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER) && This->ddraw->wined3d_frontbuffer)
4385 hr = wined3d_surface_set_palette(This->ddraw->wined3d_frontbuffer, PalImpl ? PalImpl->wineD3DPalette : NULL);
4386 if (FAILED(hr))
4387 ERR("Failed to set frontbuffer palette, hr %#x.\n", hr);
4390 /* If this is a front buffer, also update the back buffers
4391 * TODO: How do things work for palettized cube textures?
4393 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
4395 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
4396 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
4398 surf = This;
4399 while(1)
4401 IDirectDrawSurface7 *attach;
4402 HRESULT hr;
4403 hr = ddraw_surface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &caps2, &attach);
4404 if(hr != DD_OK)
4406 break;
4409 TRACE("Setting palette on %p\n", attach);
4410 ddraw_surface7_SetPalette(attach, Pal);
4411 surf = impl_from_IDirectDrawSurface7(attach);
4412 ddraw_surface7_Release(attach);
4416 LeaveCriticalSection(&ddraw_cs);
4417 return DD_OK;
4420 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4422 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4423 TRACE("iface %p, palette %p.\n", iface, palette);
4425 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4428 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4430 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4431 TRACE("iface %p, palette %p.\n", iface, palette);
4433 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4436 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4438 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4439 TRACE("iface %p, palette %p.\n", iface, palette);
4441 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4444 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4446 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4447 TRACE("iface %p, palette %p.\n", iface, palette);
4449 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4452 /**********************************************************
4453 * IDirectDrawGammaControl::GetGammaRamp
4455 * Returns the current gamma ramp for a surface
4457 * Params:
4458 * flags: Ignored
4459 * gamma_ramp: Address to write the ramp to
4461 * Returns:
4462 * DD_OK on success
4463 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4465 **********************************************************/
4466 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4467 DWORD flags, DDGAMMARAMP *gamma_ramp)
4469 IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4471 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4473 if (!gamma_ramp)
4475 WARN("Invalid gamma_ramp passed.\n");
4476 return DDERR_INVALIDPARAMS;
4479 EnterCriticalSection(&ddraw_cs);
4480 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4482 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP. */
4483 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (WINED3DGAMMARAMP *)gamma_ramp);
4485 else
4487 ERR("Not implemented for non-primary surfaces.\n");
4489 LeaveCriticalSection(&ddraw_cs);
4491 return DD_OK;
4494 /**********************************************************
4495 * IDirectDrawGammaControl::SetGammaRamp
4497 * Sets the red, green and blue gamma ramps for
4499 * Params:
4500 * flags: Can be DDSGR_CALIBRATE to request calibration
4501 * gamma_ramp: Structure containing the new gamma ramp
4503 * Returns:
4504 * DD_OK on success
4505 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4507 **********************************************************/
4508 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4509 DWORD flags, DDGAMMARAMP *gamma_ramp)
4511 IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4513 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4515 if (!gamma_ramp)
4517 WARN("Invalid gamma_ramp passed.\n");
4518 return DDERR_INVALIDPARAMS;
4521 EnterCriticalSection(&ddraw_cs);
4522 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4524 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP */
4525 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device, 0, flags, (WINED3DGAMMARAMP *)gamma_ramp);
4527 else
4529 ERR("Not implemented for non-primary surfaces.\n");
4531 LeaveCriticalSection(&ddraw_cs);
4533 return DD_OK;
4536 /*****************************************************************************
4537 * IDirect3DTexture2::PaletteChanged
4539 * Informs the texture about a palette change
4541 * Params:
4542 * start: Start index of the change
4543 * count: The number of changed entries
4545 * Returns
4546 * D3D_OK, because it's a stub
4548 *****************************************************************************/
4549 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4551 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4553 return D3D_OK;
4556 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4558 IDirectDrawSurfaceImpl *surface = impl_from_IDirect3DTexture(iface);
4560 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4562 return d3d_texture2_PaletteChanged(&surface->IDirect3DTexture2_iface, start, count);
4565 /*****************************************************************************
4566 * IDirect3DTexture::Unload
4568 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4571 * Returns:
4572 * DDERR_UNSUPPORTED
4574 *****************************************************************************/
4575 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4577 WARN("iface %p. Not implemented.\n", iface);
4579 return DDERR_UNSUPPORTED;
4582 /*****************************************************************************
4583 * IDirect3DTexture2::GetHandle
4585 * Returns handle for the texture. At the moment, the interface
4586 * to the IWineD3DTexture is used.
4588 * Params:
4589 * device: Device this handle is assigned to
4590 * handle: Address to store the handle at.
4592 * Returns:
4593 * D3D_OK
4595 *****************************************************************************/
4596 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4597 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4599 IDirectDrawSurfaceImpl *surface = impl_from_IDirect3DTexture2(iface);
4600 IDirect3DDeviceImpl *device_impl = unsafe_impl_from_IDirect3DDevice2(device);
4602 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4604 EnterCriticalSection(&ddraw_cs);
4606 if (!surface->Handle)
4608 DWORD h = ddraw_allocate_handle(&device_impl->handle_table, surface, DDRAW_HANDLE_SURFACE);
4609 if (h == DDRAW_INVALID_HANDLE)
4611 ERR("Failed to allocate a texture handle.\n");
4612 LeaveCriticalSection(&ddraw_cs);
4613 return DDERR_OUTOFMEMORY;
4616 surface->Handle = h + 1;
4619 TRACE("Returning handle %08x.\n", surface->Handle);
4620 *handle = surface->Handle;
4622 LeaveCriticalSection(&ddraw_cs);
4624 return D3D_OK;
4627 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
4628 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
4630 IDirectDrawSurfaceImpl *This = impl_from_IDirect3DTexture(iface);
4631 IDirect3DDeviceImpl *device_impl = unsafe_impl_from_IDirect3DDevice(device);
4633 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4635 return d3d_texture2_GetHandle(&This->IDirect3DTexture2_iface,
4636 device_impl ? &device_impl->IDirect3DDevice2_iface : NULL, handle);
4639 /*****************************************************************************
4640 * get_sub_mimaplevel
4642 * Helper function that returns the next mipmap level
4644 * tex_ptr: Surface of which to return the next level
4646 *****************************************************************************/
4647 static IDirectDrawSurfaceImpl *get_sub_mimaplevel(IDirectDrawSurfaceImpl *surface)
4649 /* Now go down the mipmap chain to the next surface */
4650 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
4651 IDirectDrawSurface7 *next_level;
4652 HRESULT hr;
4654 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
4655 if (FAILED(hr)) return NULL;
4657 ddraw_surface7_Release(next_level);
4659 return impl_from_IDirectDrawSurface7(next_level);
4662 /*****************************************************************************
4663 * IDirect3DTexture2::Load
4665 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
4667 * This function isn't relayed to WineD3D because the whole interface is
4668 * implemented in DDraw only. For speed improvements a implementation which
4669 * takes OpenGL more into account could be placed into WineD3D.
4671 * Params:
4672 * src_texture: Address of the texture to load
4674 * Returns:
4675 * D3D_OK on success
4676 * D3DERR_TEXTURE_LOAD_FAILED.
4678 *****************************************************************************/
4679 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
4681 IDirectDrawSurfaceImpl *dst_surface = impl_from_IDirect3DTexture2(iface);
4682 IDirectDrawSurfaceImpl *src_surface = unsafe_impl_from_IDirect3DTexture2(src_texture);
4683 HRESULT hr;
4685 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4687 if (src_surface == dst_surface)
4689 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
4690 return D3D_OK;
4693 EnterCriticalSection(&ddraw_cs);
4695 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4696 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
4697 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
4699 ERR("Trying to load surfaces with different mip-map counts.\n");
4702 for (;;)
4704 struct wined3d_palette *wined3d_dst_pal, *wined3d_src_pal;
4705 IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
4706 DDSURFACEDESC *src_desc, *dst_desc;
4708 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
4709 src_surface, dst_surface, src_surface->mipmap_level);
4711 /* Suppress the ALLOCONLOAD flag */
4712 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
4714 /* Get the palettes */
4715 wined3d_dst_pal = wined3d_surface_get_palette(dst_surface->wined3d_surface);
4716 if (wined3d_dst_pal)
4717 dst_pal = wined3d_palette_get_parent(wined3d_dst_pal);
4719 wined3d_src_pal = wined3d_surface_get_palette(src_surface->wined3d_surface);
4720 if (wined3d_src_pal)
4721 src_pal = wined3d_palette_get_parent(wined3d_src_pal);
4723 if (src_pal)
4725 PALETTEENTRY palent[256];
4727 if (!dst_pal)
4729 LeaveCriticalSection(&ddraw_cs);
4730 return DDERR_NOPALETTEATTACHED;
4732 IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
4733 IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
4736 /* Copy one surface on the other */
4737 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
4738 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
4740 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
4742 /* Should also check for same pixel format, u1.lPitch, ... */
4743 ERR("Error in surface sizes.\n");
4744 LeaveCriticalSection(&ddraw_cs);
4745 return D3DERR_TEXTURE_LOAD_FAILED;
4747 else
4749 WINED3DLOCKED_RECT src_rect, dst_rect;
4751 /* Copy also the ColorKeying stuff */
4752 if (src_desc->dwFlags & DDSD_CKSRCBLT)
4754 dst_desc->dwFlags |= DDSD_CKSRCBLT;
4755 dst_desc->ddckCKSrcBlt.dwColorSpaceLowValue = src_desc->ddckCKSrcBlt.dwColorSpaceLowValue;
4756 dst_desc->ddckCKSrcBlt.dwColorSpaceHighValue = src_desc->ddckCKSrcBlt.dwColorSpaceHighValue;
4759 /* Copy the main memory texture into the surface that corresponds
4760 * to the OpenGL texture object. */
4762 hr = wined3d_surface_map(src_surface->wined3d_surface, &src_rect, NULL, 0);
4763 if (FAILED(hr))
4765 ERR("Failed to lock source surface, hr %#x.\n", hr);
4766 LeaveCriticalSection(&ddraw_cs);
4767 return D3DERR_TEXTURE_LOAD_FAILED;
4770 hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_rect, NULL, 0);
4771 if (FAILED(hr))
4773 ERR("Failed to lock destination surface, hr %#x.\n", hr);
4774 wined3d_surface_unmap(src_surface->wined3d_surface);
4775 LeaveCriticalSection(&ddraw_cs);
4776 return D3DERR_TEXTURE_LOAD_FAILED;
4779 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
4780 memcpy(dst_rect.pBits, src_rect.pBits, src_surface->surface_desc.u1.dwLinearSize);
4781 else
4782 memcpy(dst_rect.pBits, src_rect.pBits, src_rect.Pitch * src_desc->dwHeight);
4784 wined3d_surface_unmap(src_surface->wined3d_surface);
4785 wined3d_surface_unmap(dst_surface->wined3d_surface);
4788 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4789 src_surface = get_sub_mimaplevel(src_surface);
4790 else
4791 src_surface = NULL;
4793 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4794 dst_surface = get_sub_mimaplevel(dst_surface);
4795 else
4796 dst_surface = NULL;
4798 if (!src_surface || !dst_surface)
4800 if (src_surface != dst_surface)
4801 ERR("Loading surface with different mipmap structure.\n");
4802 break;
4806 LeaveCriticalSection(&ddraw_cs);
4808 return hr;
4811 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
4813 IDirectDrawSurfaceImpl* This = impl_from_IDirect3DTexture(iface);
4814 IDirectDrawSurfaceImpl* src_surface = unsafe_impl_from_IDirect3DTexture(src_texture);
4815 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4817 return d3d_texture2_Load(&This->IDirect3DTexture2_iface,
4818 src_surface ? &src_surface->IDirect3DTexture2_iface : NULL);
4821 /*****************************************************************************
4822 * The VTable
4823 *****************************************************************************/
4825 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
4827 /* IUnknown */
4828 ddraw_surface7_QueryInterface,
4829 ddraw_surface7_AddRef,
4830 ddraw_surface7_Release,
4831 /* IDirectDrawSurface */
4832 ddraw_surface7_AddAttachedSurface,
4833 ddraw_surface7_AddOverlayDirtyRect,
4834 ddraw_surface7_Blt,
4835 ddraw_surface7_BltBatch,
4836 ddraw_surface7_BltFast,
4837 ddraw_surface7_DeleteAttachedSurface,
4838 ddraw_surface7_EnumAttachedSurfaces,
4839 ddraw_surface7_EnumOverlayZOrders,
4840 ddraw_surface7_Flip,
4841 ddraw_surface7_GetAttachedSurface,
4842 ddraw_surface7_GetBltStatus,
4843 ddraw_surface7_GetCaps,
4844 ddraw_surface7_GetClipper,
4845 ddraw_surface7_GetColorKey,
4846 ddraw_surface7_GetDC,
4847 ddraw_surface7_GetFlipStatus,
4848 ddraw_surface7_GetOverlayPosition,
4849 ddraw_surface7_GetPalette,
4850 ddraw_surface7_GetPixelFormat,
4851 ddraw_surface7_GetSurfaceDesc,
4852 ddraw_surface7_Initialize,
4853 ddraw_surface7_IsLost,
4854 ddraw_surface7_Lock,
4855 ddraw_surface7_ReleaseDC,
4856 ddraw_surface7_Restore,
4857 ddraw_surface7_SetClipper,
4858 ddraw_surface7_SetColorKey,
4859 ddraw_surface7_SetOverlayPosition,
4860 ddraw_surface7_SetPalette,
4861 ddraw_surface7_Unlock,
4862 ddraw_surface7_UpdateOverlay,
4863 ddraw_surface7_UpdateOverlayDisplay,
4864 ddraw_surface7_UpdateOverlayZOrder,
4865 /* IDirectDrawSurface2 */
4866 ddraw_surface7_GetDDInterface,
4867 ddraw_surface7_PageLock,
4868 ddraw_surface7_PageUnlock,
4869 /* IDirectDrawSurface3 */
4870 ddraw_surface7_SetSurfaceDesc,
4871 /* IDirectDrawSurface4 */
4872 ddraw_surface7_SetPrivateData,
4873 ddraw_surface7_GetPrivateData,
4874 ddraw_surface7_FreePrivateData,
4875 ddraw_surface7_GetUniquenessValue,
4876 ddraw_surface7_ChangeUniquenessValue,
4877 /* IDirectDrawSurface7 */
4878 ddraw_surface7_SetPriority,
4879 ddraw_surface7_GetPriority,
4880 ddraw_surface7_SetLOD,
4881 ddraw_surface7_GetLOD,
4884 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
4886 /* IUnknown */
4887 ddraw_surface4_QueryInterface,
4888 ddraw_surface4_AddRef,
4889 ddraw_surface4_Release,
4890 /* IDirectDrawSurface */
4891 ddraw_surface4_AddAttachedSurface,
4892 ddraw_surface4_AddOverlayDirtyRect,
4893 ddraw_surface4_Blt,
4894 ddraw_surface4_BltBatch,
4895 ddraw_surface4_BltFast,
4896 ddraw_surface4_DeleteAttachedSurface,
4897 ddraw_surface4_EnumAttachedSurfaces,
4898 ddraw_surface4_EnumOverlayZOrders,
4899 ddraw_surface4_Flip,
4900 ddraw_surface4_GetAttachedSurface,
4901 ddraw_surface4_GetBltStatus,
4902 ddraw_surface4_GetCaps,
4903 ddraw_surface4_GetClipper,
4904 ddraw_surface4_GetColorKey,
4905 ddraw_surface4_GetDC,
4906 ddraw_surface4_GetFlipStatus,
4907 ddraw_surface4_GetOverlayPosition,
4908 ddraw_surface4_GetPalette,
4909 ddraw_surface4_GetPixelFormat,
4910 ddraw_surface4_GetSurfaceDesc,
4911 ddraw_surface4_Initialize,
4912 ddraw_surface4_IsLost,
4913 ddraw_surface4_Lock,
4914 ddraw_surface4_ReleaseDC,
4915 ddraw_surface4_Restore,
4916 ddraw_surface4_SetClipper,
4917 ddraw_surface4_SetColorKey,
4918 ddraw_surface4_SetOverlayPosition,
4919 ddraw_surface4_SetPalette,
4920 ddraw_surface4_Unlock,
4921 ddraw_surface4_UpdateOverlay,
4922 ddraw_surface4_UpdateOverlayDisplay,
4923 ddraw_surface4_UpdateOverlayZOrder,
4924 /* IDirectDrawSurface2 */
4925 ddraw_surface4_GetDDInterface,
4926 ddraw_surface4_PageLock,
4927 ddraw_surface4_PageUnlock,
4928 /* IDirectDrawSurface3 */
4929 ddraw_surface4_SetSurfaceDesc,
4930 /* IDirectDrawSurface4 */
4931 ddraw_surface4_SetPrivateData,
4932 ddraw_surface4_GetPrivateData,
4933 ddraw_surface4_FreePrivateData,
4934 ddraw_surface4_GetUniquenessValue,
4935 ddraw_surface4_ChangeUniquenessValue,
4938 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
4940 /* IUnknown */
4941 ddraw_surface3_QueryInterface,
4942 ddraw_surface3_AddRef,
4943 ddraw_surface3_Release,
4944 /* IDirectDrawSurface */
4945 ddraw_surface3_AddAttachedSurface,
4946 ddraw_surface3_AddOverlayDirtyRect,
4947 ddraw_surface3_Blt,
4948 ddraw_surface3_BltBatch,
4949 ddraw_surface3_BltFast,
4950 ddraw_surface3_DeleteAttachedSurface,
4951 ddraw_surface3_EnumAttachedSurfaces,
4952 ddraw_surface3_EnumOverlayZOrders,
4953 ddraw_surface3_Flip,
4954 ddraw_surface3_GetAttachedSurface,
4955 ddraw_surface3_GetBltStatus,
4956 ddraw_surface3_GetCaps,
4957 ddraw_surface3_GetClipper,
4958 ddraw_surface3_GetColorKey,
4959 ddraw_surface3_GetDC,
4960 ddraw_surface3_GetFlipStatus,
4961 ddraw_surface3_GetOverlayPosition,
4962 ddraw_surface3_GetPalette,
4963 ddraw_surface3_GetPixelFormat,
4964 ddraw_surface3_GetSurfaceDesc,
4965 ddraw_surface3_Initialize,
4966 ddraw_surface3_IsLost,
4967 ddraw_surface3_Lock,
4968 ddraw_surface3_ReleaseDC,
4969 ddraw_surface3_Restore,
4970 ddraw_surface3_SetClipper,
4971 ddraw_surface3_SetColorKey,
4972 ddraw_surface3_SetOverlayPosition,
4973 ddraw_surface3_SetPalette,
4974 ddraw_surface3_Unlock,
4975 ddraw_surface3_UpdateOverlay,
4976 ddraw_surface3_UpdateOverlayDisplay,
4977 ddraw_surface3_UpdateOverlayZOrder,
4978 /* IDirectDrawSurface2 */
4979 ddraw_surface3_GetDDInterface,
4980 ddraw_surface3_PageLock,
4981 ddraw_surface3_PageUnlock,
4982 /* IDirectDrawSurface3 */
4983 ddraw_surface3_SetSurfaceDesc,
4986 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
4988 /* IUnknown */
4989 ddraw_surface2_QueryInterface,
4990 ddraw_surface2_AddRef,
4991 ddraw_surface2_Release,
4992 /* IDirectDrawSurface */
4993 ddraw_surface2_AddAttachedSurface,
4994 ddraw_surface2_AddOverlayDirtyRect,
4995 ddraw_surface2_Blt,
4996 ddraw_surface2_BltBatch,
4997 ddraw_surface2_BltFast,
4998 ddraw_surface2_DeleteAttachedSurface,
4999 ddraw_surface2_EnumAttachedSurfaces,
5000 ddraw_surface2_EnumOverlayZOrders,
5001 ddraw_surface2_Flip,
5002 ddraw_surface2_GetAttachedSurface,
5003 ddraw_surface2_GetBltStatus,
5004 ddraw_surface2_GetCaps,
5005 ddraw_surface2_GetClipper,
5006 ddraw_surface2_GetColorKey,
5007 ddraw_surface2_GetDC,
5008 ddraw_surface2_GetFlipStatus,
5009 ddraw_surface2_GetOverlayPosition,
5010 ddraw_surface2_GetPalette,
5011 ddraw_surface2_GetPixelFormat,
5012 ddraw_surface2_GetSurfaceDesc,
5013 ddraw_surface2_Initialize,
5014 ddraw_surface2_IsLost,
5015 ddraw_surface2_Lock,
5016 ddraw_surface2_ReleaseDC,
5017 ddraw_surface2_Restore,
5018 ddraw_surface2_SetClipper,
5019 ddraw_surface2_SetColorKey,
5020 ddraw_surface2_SetOverlayPosition,
5021 ddraw_surface2_SetPalette,
5022 ddraw_surface2_Unlock,
5023 ddraw_surface2_UpdateOverlay,
5024 ddraw_surface2_UpdateOverlayDisplay,
5025 ddraw_surface2_UpdateOverlayZOrder,
5026 /* IDirectDrawSurface2 */
5027 ddraw_surface2_GetDDInterface,
5028 ddraw_surface2_PageLock,
5029 ddraw_surface2_PageUnlock,
5032 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
5034 /* IUnknown */
5035 ddraw_surface1_QueryInterface,
5036 ddraw_surface1_AddRef,
5037 ddraw_surface1_Release,
5038 /* IDirectDrawSurface */
5039 ddraw_surface1_AddAttachedSurface,
5040 ddraw_surface1_AddOverlayDirtyRect,
5041 ddraw_surface1_Blt,
5042 ddraw_surface1_BltBatch,
5043 ddraw_surface1_BltFast,
5044 ddraw_surface1_DeleteAttachedSurface,
5045 ddraw_surface1_EnumAttachedSurfaces,
5046 ddraw_surface1_EnumOverlayZOrders,
5047 ddraw_surface1_Flip,
5048 ddraw_surface1_GetAttachedSurface,
5049 ddraw_surface1_GetBltStatus,
5050 ddraw_surface1_GetCaps,
5051 ddraw_surface1_GetClipper,
5052 ddraw_surface1_GetColorKey,
5053 ddraw_surface1_GetDC,
5054 ddraw_surface1_GetFlipStatus,
5055 ddraw_surface1_GetOverlayPosition,
5056 ddraw_surface1_GetPalette,
5057 ddraw_surface1_GetPixelFormat,
5058 ddraw_surface1_GetSurfaceDesc,
5059 ddraw_surface1_Initialize,
5060 ddraw_surface1_IsLost,
5061 ddraw_surface1_Lock,
5062 ddraw_surface1_ReleaseDC,
5063 ddraw_surface1_Restore,
5064 ddraw_surface1_SetClipper,
5065 ddraw_surface1_SetColorKey,
5066 ddraw_surface1_SetOverlayPosition,
5067 ddraw_surface1_SetPalette,
5068 ddraw_surface1_Unlock,
5069 ddraw_surface1_UpdateOverlay,
5070 ddraw_surface1_UpdateOverlayDisplay,
5071 ddraw_surface1_UpdateOverlayZOrder,
5074 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
5076 ddraw_gamma_control_QueryInterface,
5077 ddraw_gamma_control_AddRef,
5078 ddraw_gamma_control_Release,
5079 ddraw_gamma_control_GetGammaRamp,
5080 ddraw_gamma_control_SetGammaRamp,
5083 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
5085 d3d_texture2_QueryInterface,
5086 d3d_texture2_AddRef,
5087 d3d_texture2_Release,
5088 d3d_texture2_GetHandle,
5089 d3d_texture2_PaletteChanged,
5090 d3d_texture2_Load,
5093 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
5095 d3d_texture1_QueryInterface,
5096 d3d_texture1_AddRef,
5097 d3d_texture1_Release,
5098 d3d_texture1_Initialize,
5099 d3d_texture1_GetHandle,
5100 d3d_texture1_PaletteChanged,
5101 d3d_texture1_Load,
5102 d3d_texture1_Unload,
5105 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
5107 if (!iface) return NULL;
5108 assert(iface->lpVtbl == &ddraw_surface7_vtbl);
5109 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface7_iface);
5112 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
5114 if (!iface) return NULL;
5115 assert(iface->lpVtbl == &ddraw_surface4_vtbl);
5116 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface4_iface);
5119 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5121 if (!iface) return NULL;
5122 assert(iface->lpVtbl == &ddraw_surface3_vtbl);
5123 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface3_iface);
5126 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5128 if (!iface) return NULL;
5129 assert(iface->lpVtbl == &ddraw_surface2_vtbl);
5130 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface2_iface);
5133 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5135 if (!iface) return NULL;
5136 assert(iface->lpVtbl == &ddraw_surface1_vtbl);
5137 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface_iface);
5140 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirect3DTexture2(IDirect3DTexture2 *iface)
5142 if (!iface) return NULL;
5143 assert(iface->lpVtbl == &d3d_texture2_vtbl);
5144 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirect3DTexture2_iface);
5147 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirect3DTexture(IDirect3DTexture *iface)
5149 if (!iface) return NULL;
5150 assert(iface->lpVtbl == &d3d_texture1_vtbl);
5151 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirect3DTexture_iface);
5154 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5156 IDirectDrawSurfaceImpl *surface = parent;
5158 TRACE("surface %p.\n", surface);
5160 /* Check for attached surfaces and detach them. */
5161 if (surface->first_attached != surface)
5163 /* Well, this shouldn't happen: The surface being attached is
5164 * referenced in AddAttachedSurface(), so it shouldn't be released
5165 * until DeleteAttachedSurface() is called, because the refcount is
5166 * held. It looks like the application released it often enough to
5167 * force this. */
5168 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
5170 /* The refcount will drop to -1 here */
5171 if (FAILED(ddraw_surface_delete_attached_surface(surface->first_attached, surface, surface->attached_iface)))
5172 ERR("DeleteAttachedSurface failed.\n");
5175 while (surface->next_attached)
5176 if (FAILED(ddraw_surface_delete_attached_surface(surface,
5177 surface->next_attached, surface->next_attached->attached_iface)))
5178 ERR("DeleteAttachedSurface failed.\n");
5180 /* Having a texture handle set implies that the device still exists. */
5181 if (surface->Handle)
5182 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5184 /* Reduce the ddraw surface count. */
5185 list_remove(&surface->surface_list_entry);
5187 if (surface == surface->ddraw->primary)
5188 surface->ddraw->primary = NULL;
5190 HeapFree(GetProcessHeap(), 0, surface);
5193 const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5195 ddraw_surface_wined3d_object_destroyed,
5198 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5200 IDirectDrawSurfaceImpl *surface = parent;
5202 TRACE("surface %p.\n", surface);
5204 ddraw_surface_cleanup(surface);
5207 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5209 ddraw_texture_wined3d_object_destroyed,
5212 HRESULT ddraw_surface_create_texture(IDirectDrawSurfaceImpl *surface)
5214 const DDSURFACEDESC2 *desc = &surface->surface_desc;
5215 enum wined3d_format_id format;
5216 WINED3DPOOL pool;
5217 UINT levels;
5219 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5220 levels = desc->u2.dwMipMapCount;
5221 else
5222 levels = 1;
5224 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM.
5225 * Should I forward the MANAGED cap to the managed pool? */
5226 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5227 pool = WINED3DPOOL_SYSTEMMEM;
5228 else
5229 pool = WINED3DPOOL_DEFAULT;
5231 format = PixelFormat_DD2WineD3D(&surface->surface_desc.u4.ddpfPixelFormat);
5232 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5233 return wined3d_texture_create_cube(surface->ddraw->wined3d_device, desc->dwWidth,
5234 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5235 else
5236 return wined3d_texture_create_2d(surface->ddraw->wined3d_device, desc->dwWidth, desc->dwHeight,
5237 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5240 HRESULT ddraw_surface_init(IDirectDrawSurfaceImpl *surface, IDirectDrawImpl *ddraw,
5241 DDSURFACEDESC2 *desc, UINT mip_level, UINT version)
5243 WINED3DPOOL pool = WINED3DPOOL_DEFAULT;
5244 enum wined3d_format_id format;
5245 DWORD usage = 0;
5246 HRESULT hr;
5248 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5249 && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5250 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
5252 /* Tests show surfaces without memory flags get these flags added
5253 * right after creation. */
5254 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5257 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5259 usage |= WINED3DUSAGE_RENDERTARGET;
5260 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5263 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && !(desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
5265 usage |= WINED3DUSAGE_RENDERTARGET;
5268 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
5270 usage |= WINED3DUSAGE_OVERLAY;
5273 if (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
5274 usage |= WINED3DUSAGE_DEPTHSTENCIL;
5276 if (desc->ddsCaps.dwCaps & DDSCAPS_OWNDC)
5277 usage |= WINED3DUSAGE_OWNDC;
5279 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5281 pool = WINED3DPOOL_SYSTEMMEM;
5283 else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
5285 pool = WINED3DPOOL_MANAGED;
5286 /* Managed textures have the system memory flag set. */
5287 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5289 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5291 /* Videomemory adds localvidmem. This is mutually exclusive with
5292 * systemmemory and texturemanage. */
5293 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
5296 format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
5297 if (format == WINED3DFMT_UNKNOWN)
5299 WARN("Unsupported / unknown pixelformat.\n");
5300 return DDERR_INVALIDPIXELFORMAT;
5303 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
5304 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
5305 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
5306 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
5307 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
5308 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
5309 surface->IDirect3DTexture2_iface.lpVtbl = &d3d_texture2_vtbl;
5310 surface->IDirect3DTexture_iface.lpVtbl = &d3d_texture1_vtbl;
5311 surface->iface_count = 1;
5312 surface->version = version;
5313 surface->ddraw = ddraw;
5315 if (version == 7)
5317 surface->ref7 = 1;
5319 else if (version == 4)
5321 surface->ref4 = 1;
5323 else
5325 surface->ref1 = 1;
5328 copy_to_surfacedesc2(&surface->surface_desc, desc);
5330 surface->first_attached = surface;
5332 hr = wined3d_surface_create(ddraw->wined3d_device, desc->dwWidth, desc->dwHeight, format,
5333 TRUE /* Lockable */, FALSE /* Discard */, mip_level, usage, pool,
5334 WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, DefaultSurfaceType, surface,
5335 &ddraw_surface_wined3d_parent_ops, &surface->wined3d_surface);
5336 if (FAILED(hr))
5338 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
5339 return hr;
5342 /* Anno 1602 stores the pitch right after surface creation, so make sure
5343 * it's there. TODO: Test other fourcc formats. */
5344 if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
5345 || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
5347 surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
5348 if (format == WINED3DFMT_DXT1)
5350 surface->surface_desc.u1.dwLinearSize = max(4, desc->dwWidth) * max(4, desc->dwHeight) / 2;
5352 else
5354 surface->surface_desc.u1.dwLinearSize = max(4, desc->dwWidth) * max(4, desc->dwHeight);
5357 else
5359 surface->surface_desc.dwFlags |= DDSD_PITCH;
5360 surface->surface_desc.u1.lPitch = wined3d_surface_get_pitch(surface->wined3d_surface);
5363 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
5365 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTOVERLAY,
5366 (WINEDDCOLORKEY *)&desc->u3.ddckCKDestOverlay);
5368 if (desc->dwFlags & DDSD_CKDESTBLT)
5370 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTBLT,
5371 (WINEDDCOLORKEY *)&desc->ddckCKDestBlt);
5373 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
5375 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCOVERLAY,
5376 (WINEDDCOLORKEY *)&desc->ddckCKSrcOverlay);
5378 if (desc->dwFlags & DDSD_CKSRCBLT)
5380 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCBLT,
5381 (WINEDDCOLORKEY *)&desc->ddckCKSrcBlt);
5383 if (desc->dwFlags & DDSD_LPSURFACE)
5385 hr = wined3d_surface_set_mem(surface->wined3d_surface, desc->lpSurface);
5386 if (FAILED(hr))
5388 ERR("Failed to set surface memory, hr %#x.\n", hr);
5389 wined3d_surface_decref(surface->wined3d_surface);
5390 return hr;
5394 return DD_OK;