ddraw: Move IDirect3DTexture and IDirect3DTexture2 to IDirectDrawSurface reference...
[wine.git] / dlls / ddraw / surface.c
blobb027940bf778524cb8f0f83ad2d284d054464eec
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 /*****************************************************************************
43 * IUnknown parts follow
44 *****************************************************************************/
46 /*****************************************************************************
47 * IDirectDrawSurface7::QueryInterface
49 * A normal QueryInterface implementation. For QueryInterface rules
50 * see ddraw.c, IDirectDraw7::QueryInterface. This method
51 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
52 * in all versions, the IDirectDrawGammaControl interface and it can
53 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
55 * Params:
56 * riid: The interface id queried for
57 * obj: Address to write the pointer to
59 * Returns:
60 * S_OK on success
61 * E_NOINTERFACE if the requested interface wasn't found
63 *****************************************************************************/
64 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
66 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
68 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
70 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
71 *obj = NULL;
73 if(!riid)
74 return DDERR_INVALIDPARAMS;
76 if (IsEqualGUID(riid, &IID_IUnknown)
77 || IsEqualGUID(riid, &IID_IDirectDrawSurface7) )
79 IDirectDrawSurface7_AddRef(iface);
80 *obj = iface;
81 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
82 return S_OK;
84 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface4))
86 IDirectDrawSurface4_AddRef(&This->IDirectDrawSurface4_iface);
87 *obj = &This->IDirectDrawSurface4_iface;
88 TRACE("(%p) returning IDirectDrawSurface4 interface at %p\n", This, *obj);
89 return S_OK;
91 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface3))
93 IDirectDrawSurface3_AddRef(&This->IDirectDrawSurface3_iface);
94 *obj = &This->IDirectDrawSurface3_iface;
95 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
96 return S_OK;
98 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface2))
100 IDirectDrawSurface2_AddRef(&This->IDirectDrawSurface2_iface);
101 *obj = &This->IDirectDrawSurface2_iface;
102 TRACE("(%p) returning IDirectDrawSurface2 interface at %p\n", This, *obj);
103 return S_OK;
105 else if (IsEqualGUID(riid, &IID_IDirectDrawSurface))
107 IDirectDrawSurface_AddRef(&This->IDirectDrawSurface_iface);
108 *obj = &This->IDirectDrawSurface_iface;
109 TRACE("(%p) returning IDirectDrawSurface interface at %p\n", This, *obj);
110 return S_OK;
112 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
114 IUnknown_AddRef(iface);
115 *obj = &This->IDirectDrawGammaControl_iface;
116 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
117 return S_OK;
119 else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
120 IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
121 IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
123 IDirect3DDevice7 *d3d;
125 /* Call into IDirect3D7 for creation */
126 IDirect3D7_CreateDevice(&This->ddraw->IDirect3D7_iface, riid, &This->IDirectDrawSurface7_iface,
127 &d3d);
129 if (d3d)
131 *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
132 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
133 return S_OK;
136 WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
137 return E_NOINTERFACE;
139 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
140 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
142 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
144 *obj = &This->IDirect3DTexture_vtbl;
145 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
147 else
149 *obj = &This->IDirect3DTexture2_vtbl;
150 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
152 IUnknown_AddRef( (IUnknown *) *obj);
153 return S_OK;
156 ERR("No interface\n");
157 return E_NOINTERFACE;
160 static HRESULT WINAPI ddraw_surface4_QueryInterface(IDirectDrawSurface4 *iface, REFIID riid, void **object)
162 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
163 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
165 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
168 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
170 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
171 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
173 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
176 static HRESULT WINAPI ddraw_surface2_QueryInterface(IDirectDrawSurface2 *iface, REFIID riid, void **object)
178 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
179 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
181 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
184 static HRESULT WINAPI ddraw_surface1_QueryInterface(IDirectDrawSurface *iface, REFIID riid, void **object)
186 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
187 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
189 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
192 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface,
193 REFIID riid, void **object)
195 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
197 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
199 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
202 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
204 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
205 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
207 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
210 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
212 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
213 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
215 return ddraw_surface7_QueryInterface(&This->IDirectDrawSurface7_iface, riid, object);
218 static void ddraw_surface_add_iface(IDirectDrawSurfaceImpl *This)
220 ULONG iface_count = InterlockedIncrement(&This->iface_count);
221 TRACE("%p increasing iface count to %u.\n", This, iface_count);
223 if (iface_count == 1)
225 EnterCriticalSection(&ddraw_cs);
226 if (This->wined3d_surface)
227 wined3d_surface_incref(This->wined3d_surface);
228 if (This->wined3d_texture)
229 wined3d_texture_incref(This->wined3d_texture);
230 LeaveCriticalSection(&ddraw_cs);
234 /*****************************************************************************
235 * IDirectDrawSurface7::AddRef
237 * A normal addref implementation
239 * Returns:
240 * The new refcount
242 *****************************************************************************/
243 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
245 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
246 ULONG refcount = InterlockedIncrement(&This->ref7);
248 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
250 if (refcount == 1)
252 ddraw_surface_add_iface(This);
255 return refcount;
258 static ULONG WINAPI ddraw_surface4_AddRef(IDirectDrawSurface4 *iface)
260 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
261 ULONG refcount = InterlockedIncrement(&This->ref4);
263 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
265 if (refcount == 1)
267 ddraw_surface_add_iface(This);
270 return refcount;
273 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
275 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
276 ULONG refcount = InterlockedIncrement(&This->ref3);
278 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
280 if (refcount == 1)
282 ddraw_surface_add_iface(This);
285 return refcount;
288 static ULONG WINAPI ddraw_surface2_AddRef(IDirectDrawSurface2 *iface)
290 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
291 ULONG refcount = InterlockedIncrement(&This->ref2);
293 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
295 if (refcount == 1)
297 ddraw_surface_add_iface(This);
300 return refcount;
303 static ULONG WINAPI ddraw_surface1_AddRef(IDirectDrawSurface *iface)
305 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
306 ULONG refcount = InterlockedIncrement(&This->ref1);
308 TRACE("iface %p increasing refcount to %u.\n", iface, refcount);
310 if (refcount == 1)
312 ddraw_surface_add_iface(This);
315 return refcount;
318 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
320 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
322 TRACE("iface %p.\n", iface);
324 return ddraw_surface7_AddRef(&This->IDirectDrawSurface7_iface);
327 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
329 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
330 TRACE("iface %p.\n", iface);
332 return ddraw_surface1_AddRef(&This->IDirectDrawSurface_iface);
335 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
337 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
338 TRACE("iface %p.\n", iface);
340 return ddraw_surface1_AddRef(&This->IDirectDrawSurface_iface);
343 /*****************************************************************************
344 * ddraw_surface_destroy
346 * A helper function for IDirectDrawSurface7::Release
348 * Frees the surface, regardless of its refcount.
349 * See IDirectDrawSurface7::Release for more information
351 * Params:
352 * This: Surface to free
354 *****************************************************************************/
355 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
357 TRACE("surface %p.\n", This);
359 /* Check the iface count and give a warning */
360 if(This->iface_count > 1)
362 /* This can happen when a complex surface is destroyed,
363 * because the 2nd surface was addref()ed when the app
364 * called GetAttachedSurface
366 WARN("(%p): Destroying surface with refcounts 7: %d 4: %d 3: %d 2: %d 1: %d\n",
367 This, This->ref7, This->ref4, This->ref3, This->ref2, This->ref1);
370 if (This->wined3d_surface)
371 wined3d_surface_decref(This->wined3d_surface);
374 static void ddraw_surface_cleanup(IDirectDrawSurfaceImpl *surface)
376 IDirectDrawSurfaceImpl *surf;
377 IUnknown *ifaceToRelease;
378 UINT i;
380 TRACE("surface %p.\n", surface);
382 if (surface->wined3d_swapchain)
384 IDirectDrawImpl *ddraw = surface->ddraw;
386 /* If it's the render target, destroy the D3D device. */
387 if (ddraw->d3d_initialized && surface == ddraw->d3d_target)
389 TRACE("Destroying the render target, uninitializing D3D.\n");
391 for (i = 0; i < ddraw->numConvertedDecls; ++i)
393 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
395 HeapFree(GetProcessHeap(), 0, ddraw->decls);
396 ddraw->numConvertedDecls = 0;
398 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
400 ERR("Failed to uninit 3D.\n");
402 else
404 /* Free the d3d window if one was created. */
405 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
407 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
408 DestroyWindow(ddraw->d3d_window);
409 ddraw->d3d_window = 0;
413 ddraw->d3d_initialized = FALSE;
414 ddraw->d3d_target = NULL;
416 else
418 wined3d_device_uninit_gdi(ddraw->wined3d_device);
421 surface->wined3d_swapchain = NULL;
423 /* Reset to the default surface implementation type. This is needed
424 * if applications use non render target surfaces and expect blits to
425 * work after destroying the render target.
427 * TODO: Recreate existing offscreen surfaces. */
428 ddraw->ImplType = DefaultSurfaceType;
430 TRACE("D3D unloaded.\n");
433 /* The refcount test shows that the palette is detached when the surface
434 * is destroyed. */
435 IDirectDrawSurface7_SetPalette(&surface->IDirectDrawSurface7_iface, NULL);
437 /* Loop through all complex attached surfaces and destroy them.
439 * Yet again, only the root can have more than one complexly attached
440 * surface, all the others have a total of one. */
441 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
443 if (!surface->complex_array[i])
444 break;
446 surf = surface->complex_array[i];
447 surface->complex_array[i] = NULL;
448 while (surf)
450 IDirectDrawSurfaceImpl *destroy = surf;
451 surf = surf->complex_array[0]; /* Iterate through the "tree" */
452 ddraw_surface_destroy(destroy); /* Destroy it */
456 ifaceToRelease = surface->ifaceToRelease;
458 /* Destroy the root surface. */
459 ddraw_surface_destroy(surface);
461 /* Reduce the ddraw refcount */
462 if (ifaceToRelease)
463 IUnknown_Release(ifaceToRelease);
466 ULONG ddraw_surface_release_iface(IDirectDrawSurfaceImpl *This)
468 ULONG iface_count = InterlockedDecrement(&This->iface_count);
469 TRACE("%p decreasing iface count to %u.\n", This, iface_count);
471 if (iface_count == 0)
473 /* Complex attached surfaces are destroyed implicitly when the root is released */
474 EnterCriticalSection(&ddraw_cs);
475 if(!This->is_complex_root)
477 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
478 LeaveCriticalSection(&ddraw_cs);
479 return iface_count;
481 if (This->wined3d_texture) /* If it's a texture, destroy the wined3d texture. */
482 wined3d_texture_decref(This->wined3d_texture);
483 else
484 ddraw_surface_cleanup(This);
485 LeaveCriticalSection(&ddraw_cs);
488 return iface_count;
491 /*****************************************************************************
492 * IDirectDrawSurface7::Release
494 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
495 * surface is destroyed.
497 * Destroying the surface is a bit tricky. For the connection between
498 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
499 * It has a nice graph explaining the connection.
501 * What happens here is basically this:
502 * When a surface is destroyed, its WineD3DSurface is released,
503 * and the refcount of the DirectDraw interface is reduced by 1. If it has
504 * complex surfaces attached to it, then these surfaces are destroyed too,
505 * regardless of their refcount. If any surface being destroyed has another
506 * surface attached to it (with a "soft" attachment, not complex), then
507 * this surface is detached with DeleteAttachedSurface.
509 * When the surface is a texture, the WineD3DTexture is released.
510 * If the surface is the Direct3D render target, then the D3D
511 * capabilities of the WineD3DDevice are uninitialized, which causes the
512 * swapchain to be released.
514 * When a complex sublevel falls to ref zero, then this is ignored.
516 * Returns:
517 * The new refcount
519 *****************************************************************************/
520 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
522 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
523 ULONG refcount = InterlockedDecrement(&This->ref7);
525 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
527 if (refcount == 0)
529 ddraw_surface_release_iface(This);
532 return refcount;
535 static ULONG WINAPI ddraw_surface4_Release(IDirectDrawSurface4 *iface)
537 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
538 ULONG refcount = InterlockedDecrement(&This->ref4);
540 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
542 if (refcount == 0)
544 ddraw_surface_release_iface(This);
547 return refcount;
550 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
552 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
553 ULONG refcount = InterlockedDecrement(&This->ref3);
555 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
557 if (refcount == 0)
559 ddraw_surface_release_iface(This);
562 return refcount;
565 static ULONG WINAPI ddraw_surface2_Release(IDirectDrawSurface2 *iface)
567 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
568 ULONG refcount = InterlockedDecrement(&This->ref2);
570 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
572 if (refcount == 0)
574 ddraw_surface_release_iface(This);
577 return refcount;
580 static ULONG WINAPI ddraw_surface1_Release(IDirectDrawSurface *iface)
582 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
583 ULONG refcount = InterlockedDecrement(&This->ref1);
585 TRACE("iface %p decreasing refcount to %u.\n", iface, refcount);
587 if (refcount == 0)
589 ddraw_surface_release_iface(This);
592 return refcount;
595 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
597 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawGammaControl(iface);
599 TRACE("iface %p.\n", iface);
601 return ddraw_surface7_Release(&This->IDirectDrawSurface7_iface);
604 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
606 IDirectDrawSurfaceImpl *This = surface_from_texture2(iface);
607 TRACE("iface %p.\n", iface);
609 return ddraw_surface1_Release(&This->IDirectDrawSurface_iface);
612 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
614 IDirectDrawSurfaceImpl *This = surface_from_texture1(iface);
615 TRACE("iface %p.\n", iface);
617 return ddraw_surface1_Release(&This->IDirectDrawSurface_iface);
620 /*****************************************************************************
621 * IDirectDrawSurface7::GetAttachedSurface
623 * Returns an attached surface with the requested caps. Surface attachment
624 * and complex surfaces are not clearly described by the MSDN or sdk,
625 * so this method is tricky and likely to contain problems.
626 * This implementation searches the complex list first, then the
627 * attachment chain.
629 * The chains are searched from This down to the last surface in the chain,
630 * not from the first element in the chain. The first surface found is
631 * returned. The MSDN says that this method fails if more than one surface
632 * matches the caps, but it is not sure if that is right. The attachment
633 * structure may not even allow two matching surfaces.
635 * The found surface is AddRef-ed before it is returned.
637 * Params:
638 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
639 * Surface: Address to store the found surface
641 * Returns:
642 * DD_OK on success
643 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
644 * DDERR_NOTFOUND if no surface was found
646 *****************************************************************************/
647 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
648 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
650 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
651 IDirectDrawSurfaceImpl *surf;
652 DDSCAPS2 our_caps;
653 int i;
655 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
657 EnterCriticalSection(&ddraw_cs);
659 if(This->version < 7)
661 /* Earlier dx apps put garbage into these members, clear them */
662 our_caps.dwCaps = Caps->dwCaps;
663 our_caps.dwCaps2 = 0;
664 our_caps.dwCaps3 = 0;
665 our_caps.dwCaps4 = 0;
667 else
669 our_caps = *Caps;
672 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 */
674 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
676 surf = This->complex_array[i];
677 if(!surf) break;
679 if (TRACE_ON(ddraw))
681 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
682 surf->surface_desc.ddsCaps.dwCaps,
683 surf->surface_desc.ddsCaps.dwCaps2,
684 surf->surface_desc.ddsCaps.dwCaps3,
685 surf->surface_desc.ddsCaps.dwCaps4);
688 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
689 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
691 /* MSDN: "This method fails if more than one surface is attached
692 * that matches the capabilities requested."
694 * Not sure how to test this.
697 TRACE("(%p): Returning surface %p\n", This, surf);
698 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
699 *Surface = &surf->IDirectDrawSurface7_iface;
700 ddraw_surface7_AddRef(*Surface);
701 LeaveCriticalSection(&ddraw_cs);
702 return DD_OK;
706 /* Next, look at the attachment chain */
707 surf = This;
709 while( (surf = surf->next_attached) )
711 if (TRACE_ON(ddraw))
713 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
714 surf->surface_desc.ddsCaps.dwCaps,
715 surf->surface_desc.ddsCaps.dwCaps2,
716 surf->surface_desc.ddsCaps.dwCaps3,
717 surf->surface_desc.ddsCaps.dwCaps4);
720 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
721 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
723 TRACE("(%p): Returning surface %p\n", This, surf);
724 *Surface = &surf->IDirectDrawSurface7_iface;
725 ddraw_surface7_AddRef(*Surface);
726 LeaveCriticalSection(&ddraw_cs);
727 return DD_OK;
731 TRACE("(%p) Didn't find a valid surface\n", This);
732 LeaveCriticalSection(&ddraw_cs);
734 *Surface = NULL;
735 return DDERR_NOTFOUND;
738 static HRESULT WINAPI ddraw_surface4_GetAttachedSurface(IDirectDrawSurface4 *iface,
739 DDSCAPS2 *caps, IDirectDrawSurface4 **attachment)
741 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
742 IDirectDrawSurface7 *attachment7;
743 IDirectDrawSurfaceImpl *attachment_impl;
744 HRESULT hr;
746 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
748 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
749 caps, &attachment7);
750 if (FAILED(hr))
752 *attachment = NULL;
753 return hr;
755 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
756 *attachment = &attachment_impl->IDirectDrawSurface4_iface;
757 ddraw_surface4_AddRef(*attachment);
758 ddraw_surface7_Release(attachment7);
760 return hr;
763 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
764 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
766 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
767 IDirectDrawSurface7 *attachment7;
768 IDirectDrawSurfaceImpl *attachment_impl;
769 DDSCAPS2 caps2;
770 HRESULT hr;
772 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
774 caps2.dwCaps = caps->dwCaps;
775 caps2.dwCaps2 = 0;
776 caps2.dwCaps3 = 0;
777 caps2.dwCaps4 = 0;
779 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
780 &caps2, &attachment7);
781 if (FAILED(hr))
783 *attachment = NULL;
784 return hr;
786 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
787 *attachment = &attachment_impl->IDirectDrawSurface3_iface;
788 ddraw_surface3_AddRef(*attachment);
789 ddraw_surface7_Release(attachment7);
791 return hr;
794 static HRESULT WINAPI ddraw_surface2_GetAttachedSurface(IDirectDrawSurface2 *iface,
795 DDSCAPS *caps, IDirectDrawSurface2 **attachment)
797 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
798 IDirectDrawSurface7 *attachment7;
799 IDirectDrawSurfaceImpl *attachment_impl;
800 DDSCAPS2 caps2;
801 HRESULT hr;
803 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
805 caps2.dwCaps = caps->dwCaps;
806 caps2.dwCaps2 = 0;
807 caps2.dwCaps3 = 0;
808 caps2.dwCaps4 = 0;
810 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
811 &caps2, &attachment7);
812 if (FAILED(hr))
814 *attachment = NULL;
815 return hr;
817 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
818 *attachment = &attachment_impl->IDirectDrawSurface2_iface;
819 ddraw_surface2_AddRef(*attachment);
820 ddraw_surface7_Release(attachment7);
822 return hr;
825 static HRESULT WINAPI ddraw_surface1_GetAttachedSurface(IDirectDrawSurface *iface,
826 DDSCAPS *caps, IDirectDrawSurface **attachment)
828 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
829 IDirectDrawSurface7 *attachment7;
830 IDirectDrawSurfaceImpl *attachment_impl;
831 DDSCAPS2 caps2;
832 HRESULT hr;
834 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
836 caps2.dwCaps = caps->dwCaps;
837 caps2.dwCaps2 = 0;
838 caps2.dwCaps3 = 0;
839 caps2.dwCaps4 = 0;
841 hr = ddraw_surface7_GetAttachedSurface(&This->IDirectDrawSurface7_iface,
842 &caps2, &attachment7);
843 if (FAILED(hr))
845 *attachment = NULL;
846 return hr;
848 attachment_impl = impl_from_IDirectDrawSurface7(attachment7);
849 *attachment = &attachment_impl->IDirectDrawSurface_iface;
850 ddraw_surface1_AddRef(*attachment);
851 ddraw_surface7_Release(attachment7);
853 return hr;
856 /*****************************************************************************
857 * IDirectDrawSurface7::Lock
859 * Locks the surface and returns a pointer to the surface's memory
861 * Params:
862 * Rect: Rectangle to lock. If NULL, the whole surface is locked
863 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
864 * Flags: Locking flags, e.g Read only or write only
865 * h: An event handle that's not used and must be NULL
867 * Returns:
868 * DD_OK on success
869 * DDERR_INVALIDPARAMS if DDSD is NULL
870 * For more details, see IWineD3DSurface::LockRect
872 *****************************************************************************/
873 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
874 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
876 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
877 WINED3DLOCKED_RECT LockedRect;
878 HRESULT hr;
880 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
881 iface, wine_dbgstr_rect(Rect), DDSD, Flags, h);
883 if(!DDSD)
884 return DDERR_INVALIDPARAMS;
886 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
887 EnterCriticalSection(&ddraw_cs);
889 /* Should I check for the handle to be NULL?
891 * The DDLOCK flags and the D3DLOCK flags are equal
892 * for the supported values. The others are ignored by WineD3D
895 if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
896 DDSD->dwSize != sizeof(DDSURFACEDESC2))
898 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDSD->dwSize);
899 LeaveCriticalSection(&ddraw_cs);
900 return DDERR_INVALIDPARAMS;
903 /* Windows zeroes this if the rect is invalid */
904 DDSD->lpSurface = 0;
906 if (Rect)
908 if ((Rect->left < 0)
909 || (Rect->top < 0)
910 || (Rect->left > Rect->right)
911 || (Rect->top > Rect->bottom)
912 || (Rect->right > This->surface_desc.dwWidth)
913 || (Rect->bottom > This->surface_desc.dwHeight))
915 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
916 LeaveCriticalSection(&ddraw_cs);
917 return DDERR_INVALIDPARAMS;
921 hr = wined3d_surface_map(This->wined3d_surface, &LockedRect, Rect, Flags);
922 if (FAILED(hr))
924 LeaveCriticalSection(&ddraw_cs);
925 switch(hr)
927 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
928 * specific error. But since IWineD3DSurface::LockRect returns that error in this
929 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
930 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
931 * is much easier to do it in one place in ddraw
933 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
934 default: return hr;
938 /* Override the memory area. The pitch should be set already. Strangely windows
939 * does not set the LPSURFACE flag on locked surfaces !?!.
940 * DDSD->dwFlags |= DDSD_LPSURFACE;
942 This->surface_desc.lpSurface = LockedRect.pBits;
943 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
945 TRACE("locked surface returning description :\n");
946 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
948 LeaveCriticalSection(&ddraw_cs);
949 return DD_OK;
952 static HRESULT WINAPI ddraw_surface4_Lock(IDirectDrawSurface4 *iface, RECT *rect,
953 DDSURFACEDESC2 *surface_desc, DWORD flags, HANDLE h)
955 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
956 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
957 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
959 return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
960 rect, surface_desc, flags, h);
963 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
964 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
966 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
967 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
968 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
970 return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
971 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
974 static HRESULT WINAPI ddraw_surface2_Lock(IDirectDrawSurface2 *iface, RECT *rect,
975 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
977 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
978 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
979 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
981 return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
982 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
985 static HRESULT WINAPI ddraw_surface1_Lock(IDirectDrawSurface *iface, RECT *rect,
986 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
988 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
989 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
990 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
992 return ddraw_surface7_Lock(&This->IDirectDrawSurface7_iface,
993 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
996 /*****************************************************************************
997 * IDirectDrawSurface7::Unlock
999 * Unlocks an locked surface
1001 * Params:
1002 * Rect: Not used by this implementation
1004 * Returns:
1005 * D3D_OK on success
1006 * For more details, see IWineD3DSurface::UnlockRect
1008 *****************************************************************************/
1009 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
1011 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1012 HRESULT hr;
1014 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
1016 EnterCriticalSection(&ddraw_cs);
1017 hr = wined3d_surface_unmap(This->wined3d_surface);
1018 if (SUCCEEDED(hr))
1020 This->surface_desc.lpSurface = NULL;
1022 LeaveCriticalSection(&ddraw_cs);
1023 return hr;
1026 static HRESULT WINAPI ddraw_surface4_Unlock(IDirectDrawSurface4 *iface, RECT *pRect)
1028 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1029 TRACE("iface %p, rect %p.\n", iface, pRect);
1031 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, pRect);
1034 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
1036 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1037 TRACE("iface %p, data %p.\n", iface, data);
1039 /* data might not be the LPRECT of later versions, so drop it. */
1040 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1043 static HRESULT WINAPI ddraw_surface2_Unlock(IDirectDrawSurface2 *iface, void *data)
1045 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1046 TRACE("iface %p, data %p.\n", iface, data);
1048 /* data might not be the LPRECT of later versions, so drop it. */
1049 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1052 static HRESULT WINAPI ddraw_surface1_Unlock(IDirectDrawSurface *iface, void *data)
1054 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1055 TRACE("iface %p, data %p.\n", iface, data);
1057 /* data might not be the LPRECT of later versions, so drop it. */
1058 return ddraw_surface7_Unlock(&This->IDirectDrawSurface7_iface, NULL);
1061 /*****************************************************************************
1062 * IDirectDrawSurface7::Flip
1064 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
1065 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
1066 * the flip target is passed to WineD3D, even if the app didn't specify one
1068 * Params:
1069 * DestOverride: Specifies the surface that will become the new front
1070 * buffer. If NULL, the current back buffer is used
1071 * Flags: some DirectDraw flags, see include/ddraw.h
1073 * Returns:
1074 * DD_OK on success
1075 * DDERR_NOTFLIPPABLE if no flip target could be found
1076 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
1077 * For more details, see IWineD3DSurface::Flip
1079 *****************************************************************************/
1080 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
1082 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1083 IDirectDrawSurfaceImpl *Override = unsafe_impl_from_IDirectDrawSurface7(DestOverride);
1084 IDirectDrawSurface7 *Override7;
1085 HRESULT hr;
1087 TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
1089 /* Flip has to be called from a front buffer
1090 * What about overlay surfaces, AFAIK they can flip too?
1092 if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
1093 return DDERR_INVALIDOBJECT; /* Unchecked */
1095 EnterCriticalSection(&ddraw_cs);
1097 /* WineD3D doesn't keep track of attached surface, so find the target */
1098 if(!Override)
1100 DDSCAPS2 Caps;
1102 memset(&Caps, 0, sizeof(Caps));
1103 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
1104 hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
1105 if(hr != DD_OK)
1107 ERR("Can't find a flip target\n");
1108 LeaveCriticalSection(&ddraw_cs);
1109 return DDERR_NOTFLIPPABLE; /* Unchecked */
1111 Override = impl_from_IDirectDrawSurface7(Override7);
1113 /* For the GetAttachedSurface */
1114 ddraw_surface7_Release(Override7);
1117 hr = wined3d_surface_flip(This->wined3d_surface, Override->wined3d_surface, Flags);
1118 LeaveCriticalSection(&ddraw_cs);
1119 return hr;
1122 static HRESULT WINAPI ddraw_surface4_Flip(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *dst, DWORD flags)
1124 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1125 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst);
1126 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1128 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1129 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1132 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
1134 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1135 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
1136 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1138 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1139 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1142 static HRESULT WINAPI ddraw_surface2_Flip(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *dst, DWORD flags)
1144 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1145 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst);
1146 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1148 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1149 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1152 static HRESULT WINAPI ddraw_surface1_Flip(IDirectDrawSurface *iface, IDirectDrawSurface *dst, DWORD flags)
1154 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1155 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst);
1156 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
1158 return ddraw_surface7_Flip(&This->IDirectDrawSurface7_iface,
1159 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, flags);
1162 /*****************************************************************************
1163 * IDirectDrawSurface7::Blt
1165 * Performs a blit on the surface
1167 * Params:
1168 * DestRect: Destination rectangle, can be NULL
1169 * SrcSurface: Source surface, can be NULL
1170 * SrcRect: Source rectangle, can be NULL
1171 * Flags: Blt flags
1172 * DDBltFx: Some extended blt parameters, connected to the flags
1174 * Returns:
1175 * D3D_OK on success
1176 * See IWineD3DSurface::Blt for more details
1178 *****************************************************************************/
1179 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
1180 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
1182 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1183 IDirectDrawSurfaceImpl *Src = unsafe_impl_from_IDirectDrawSurface7(SrcSurface);
1184 HRESULT hr;
1186 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1187 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
1189 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
1190 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
1192 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
1193 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
1194 return DDERR_INVALIDPARAMS;
1197 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
1198 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
1199 return DDERR_INVALIDPARAMS;
1202 /* Sizes can change, therefore hold the lock when testing the rectangles */
1203 EnterCriticalSection(&ddraw_cs);
1204 if(DestRect)
1206 if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
1207 DestRect->right > This->surface_desc.dwWidth ||
1208 DestRect->bottom > This->surface_desc.dwHeight)
1210 WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
1211 LeaveCriticalSection(&ddraw_cs);
1212 return DDERR_INVALIDRECT;
1215 if(Src && SrcRect)
1217 if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
1218 SrcRect->right > Src->surface_desc.dwWidth ||
1219 SrcRect->bottom > Src->surface_desc.dwHeight)
1221 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
1222 LeaveCriticalSection(&ddraw_cs);
1223 return DDERR_INVALIDRECT;
1227 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
1228 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
1229 LeaveCriticalSection(&ddraw_cs);
1230 return DDERR_INVALIDPARAMS;
1233 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
1234 * does, copy the struct, and replace the ddraw surfaces with the wined3d
1235 * surfaces. So far no blitting operations using surfaces in the bltfx
1236 * struct are supported anyway. */
1237 hr = wined3d_surface_blt(This->wined3d_surface, DestRect, Src ? Src->wined3d_surface : NULL,
1238 SrcRect, Flags, (WINEDDBLTFX *)DDBltFx, WINED3DTEXF_LINEAR);
1240 LeaveCriticalSection(&ddraw_cs);
1241 switch(hr)
1243 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
1244 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
1245 default: return hr;
1249 static HRESULT WINAPI ddraw_surface4_Blt(IDirectDrawSurface4 *iface, RECT *dst_rect,
1250 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1252 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1253 IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface4(src_surface);
1254 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1255 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1257 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1258 src ? &src->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1261 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
1262 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1264 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1265 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
1266 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1267 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1269 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1270 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1273 static HRESULT WINAPI ddraw_surface2_Blt(IDirectDrawSurface2 *iface, RECT *dst_rect,
1274 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1276 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1277 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
1278 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1279 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1281 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1282 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1285 static HRESULT WINAPI ddraw_surface1_Blt(IDirectDrawSurface *iface, RECT *dst_rect,
1286 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
1288 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1289 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
1290 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
1291 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
1293 return ddraw_surface7_Blt(&This->IDirectDrawSurface7_iface, dst_rect,
1294 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags, fx);
1297 /*****************************************************************************
1298 * IDirectDrawSurface7::AddAttachedSurface
1300 * Attaches a surface to another surface. How the surface attachments work
1301 * is not totally understood yet, and this method is prone to problems.
1302 * he surface that is attached is AddRef-ed.
1304 * Tests with complex surfaces suggest that the surface attachments form a
1305 * tree, but no method to test this has been found yet.
1307 * The attachment list consists of a first surface (first_attached) and
1308 * for each surface a pointer to the next attached surface (next_attached).
1309 * For the first surface, and a surface that has no attachments
1310 * first_attached points to the surface itself. A surface that has
1311 * no successors in the chain has next_attached set to NULL.
1313 * Newly attached surfaces are attached right after the root surface.
1314 * If a surface is attached to a complex surface compound, it's attached to
1315 * the surface that the app requested, not the complex root. See
1316 * GetAttachedSurface for a description how surfaces are found.
1318 * This is how the current implementation works, and it was coded by looking
1319 * at the needs of the applications.
1321 * So far only Z-Buffer attachments are tested, and they are activated in
1322 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
1323 * Back buffers should work in 2D mode, but they are not tested(They can be
1324 * attached in older iface versions). Rendering to the front buffer and
1325 * switching between that and double buffering is not yet implemented in
1326 * WineD3D, so for 3D it might have unexpected results.
1328 * ddraw_surface_attach_surface is the real thing,
1329 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
1330 * performs additional checks. Version 7 of this interface is much more restrictive
1331 * than its predecessors.
1333 * Params:
1334 * Attach: Surface to attach to iface
1336 * Returns:
1337 * DD_OK on success
1338 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
1340 *****************************************************************************/
1341 static HRESULT ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
1343 TRACE("surface %p, attachment %p.\n", This, Surf);
1345 if(Surf == This)
1346 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
1348 EnterCriticalSection(&ddraw_cs);
1350 /* Check if the surface is already attached somewhere */
1351 if (Surf->next_attached || Surf->first_attached != Surf)
1353 /* TODO: Test for the structure of the manual attachment. Is it a
1354 * chain or a list? What happens if one surface is attached to 2
1355 * different surfaces? */
1356 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
1357 Surf, Surf->next_attached, Surf->first_attached);
1359 LeaveCriticalSection(&ddraw_cs);
1360 return DDERR_SURFACEALREADYATTACHED;
1363 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
1364 Surf->next_attached = This->next_attached;
1365 Surf->first_attached = This->first_attached;
1366 This->next_attached = Surf;
1368 /* Check if the WineD3D depth stencil needs updating */
1369 if(This->ddraw->d3ddevice)
1371 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1374 LeaveCriticalSection(&ddraw_cs);
1375 return DD_OK;
1378 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
1380 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1381 IDirectDrawSurfaceImpl *Surf = unsafe_impl_from_IDirectDrawSurface7(Attach);
1382 HRESULT hr;
1384 TRACE("iface %p, attachment %p.\n", iface, Attach);
1386 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1387 if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1390 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1391 Surf->surface_desc.ddsCaps.dwCaps);
1392 return DDERR_CANNOTATTACHSURFACE;
1395 hr = ddraw_surface_attach_surface(This, Surf);
1396 if (FAILED(hr))
1398 return hr;
1400 ddraw_surface7_AddRef(Attach);
1401 return hr;
1404 static HRESULT WINAPI ddraw_surface4_AddAttachedSurface(IDirectDrawSurface4 *iface, IDirectDrawSurface4 *attachment)
1406 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1407 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1408 HRESULT hr;
1410 TRACE("iface %p, attachment %p.\n", iface, attachment);
1412 hr = ddraw_surface7_AddAttachedSurface(&This->IDirectDrawSurface7_iface,
1413 attachment_impl ? &attachment_impl->IDirectDrawSurface7_iface : NULL);
1414 if (FAILED(hr))
1416 return hr;
1418 ddraw_surface4_AddRef(attachment);
1419 ddraw_surface7_Release(&attachment_impl->IDirectDrawSurface7_iface);
1420 return hr;
1422 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1424 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1425 IDirectDrawSurfaceImpl *attach_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1426 HRESULT hr;
1428 TRACE("iface %p, attachment %p.\n", iface, attachment);
1430 /* Tests suggest that
1431 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1432 * -> offscreen plain surfaces can be attached to primaries
1433 * -> primaries can be attached to offscreen plain surfaces
1434 * -> z buffers can be attached to primaries */
1435 if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1436 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1438 /* Sizes have to match */
1439 if (attach_impl->surface_desc.dwWidth != This->surface_desc.dwWidth
1440 || attach_impl->surface_desc.dwHeight != This->surface_desc.dwHeight)
1442 WARN("Surface sizes do not match.\n");
1443 return DDERR_CANNOTATTACHSURFACE;
1445 /* OK */
1447 else if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1448 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1450 /* OK */
1452 else
1454 WARN("Invalid attachment combination.\n");
1455 return DDERR_CANNOTATTACHSURFACE;
1458 hr = ddraw_surface_attach_surface(This, attach_impl);
1459 if (FAILED(hr))
1461 return hr;
1463 ddraw_surface3_AddRef(attachment);
1464 return hr;
1467 static HRESULT WINAPI ddraw_surface2_AddAttachedSurface(IDirectDrawSurface2 *iface, IDirectDrawSurface2 *attachment)
1469 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1470 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1471 HRESULT hr;
1473 TRACE("iface %p, attachment %p.\n", iface, attachment);
1475 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1476 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1477 if (FAILED(hr))
1479 return hr;
1481 ddraw_surface2_AddRef(attachment);
1482 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1483 return hr;
1486 static HRESULT WINAPI ddraw_surface1_AddAttachedSurface(IDirectDrawSurface *iface, IDirectDrawSurface *attachment)
1488 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1489 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1490 HRESULT hr;
1492 TRACE("iface %p, attachment %p.\n", iface, attachment);
1494 hr = ddraw_surface3_AddAttachedSurface(&This->IDirectDrawSurface3_iface,
1495 attachment_impl ? &attachment_impl->IDirectDrawSurface3_iface : NULL);
1496 if (FAILED(hr))
1498 return hr;
1500 ddraw_surface1_AddRef(attachment);
1501 ddraw_surface3_Release(&attachment_impl->IDirectDrawSurface3_iface);
1502 return hr;
1505 /*****************************************************************************
1506 * IDirectDrawSurface7::DeleteAttachedSurface
1508 * Removes a surface from the attachment chain. The surface's refcount
1509 * is decreased by one after it has been removed
1511 * Params:
1512 * Flags: Some flags, not used by this implementation
1513 * Attach: Surface to detach
1515 * Returns:
1516 * DD_OK on success
1517 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1519 *****************************************************************************/
1520 static HRESULT ddraw_surface_delete_attached_surface(IDirectDrawSurfaceImpl *This,
1521 IDirectDrawSurfaceImpl *Surf)
1523 IDirectDrawSurfaceImpl *Prev = This;
1525 TRACE("surface %p, attachment %p.\n", This, Surf);
1527 EnterCriticalSection(&ddraw_cs);
1528 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1530 LeaveCriticalSection(&ddraw_cs);
1531 return DDERR_CANNOTDETACHSURFACE;
1534 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1535 if (This->surface_desc.ddsCaps.dwCaps &
1536 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1538 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1539 /* FIXME: we should probably also subtract from dwMipMapCount of this
1540 * and all parent surfaces */
1543 /* Find the predecessor of the detached surface */
1544 while(Prev)
1546 if(Prev->next_attached == Surf) break;
1547 Prev = Prev->next_attached;
1550 /* There must be a surface, otherwise there's a bug */
1551 assert(Prev != NULL);
1553 /* Unchain the surface */
1554 Prev->next_attached = Surf->next_attached;
1555 Surf->next_attached = NULL;
1556 Surf->first_attached = Surf;
1558 /* Check if the WineD3D depth stencil needs updating */
1559 if(This->ddraw->d3ddevice)
1561 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1563 LeaveCriticalSection(&ddraw_cs);
1564 return DD_OK;
1567 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1568 DWORD flags, IDirectDrawSurface7 *attachment)
1570 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1571 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface7(attachment);
1572 HRESULT hr;
1574 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1576 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1577 if (FAILED(hr))
1579 return hr;
1581 ddraw_surface7_Release(attachment);
1582 return hr;
1585 static HRESULT WINAPI ddraw_surface4_DeleteAttachedSurface(IDirectDrawSurface4 *iface,
1586 DWORD flags, IDirectDrawSurface4 *attachment)
1588 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1589 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface4(attachment);
1590 HRESULT hr;
1592 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1594 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1595 if (FAILED(hr))
1597 return hr;
1599 ddraw_surface4_Release(attachment);
1600 return hr;
1603 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1604 DWORD flags, IDirectDrawSurface3 *attachment)
1606 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1607 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1608 HRESULT hr;
1609 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1611 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1612 if (FAILED(hr))
1614 return hr;
1616 ddraw_surface3_Release(attachment);
1617 return hr;
1620 static HRESULT WINAPI ddraw_surface2_DeleteAttachedSurface(IDirectDrawSurface2 *iface,
1621 DWORD flags, IDirectDrawSurface2 *attachment)
1623 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1624 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface2(attachment);
1625 HRESULT hr;
1626 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1628 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1629 if (FAILED(hr))
1631 return hr;
1633 ddraw_surface2_Release(attachment);
1634 return hr;
1637 static HRESULT WINAPI ddraw_surface1_DeleteAttachedSurface(IDirectDrawSurface *iface,
1638 DWORD flags, IDirectDrawSurface *attachment)
1640 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1641 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface(attachment);
1642 HRESULT hr;
1643 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1645 hr = ddraw_surface_delete_attached_surface(This, attachment_impl);
1646 if (FAILED(hr))
1648 return hr;
1650 ddraw_surface1_Release(attachment);
1651 return hr;
1654 /*****************************************************************************
1655 * IDirectDrawSurface7::AddOverlayDirtyRect
1657 * "This method is not currently implemented"
1659 * Params:
1660 * Rect: ?
1662 * Returns:
1663 * DDERR_UNSUPPORTED
1665 *****************************************************************************/
1666 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1668 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1670 return DDERR_UNSUPPORTED; /* unchecked */
1673 static HRESULT WINAPI ddraw_surface4_AddOverlayDirtyRect(IDirectDrawSurface4 *iface, RECT *rect)
1675 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1676 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1678 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1681 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1683 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1684 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1686 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1689 static HRESULT WINAPI ddraw_surface2_AddOverlayDirtyRect(IDirectDrawSurface2 *iface, RECT *rect)
1691 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1692 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1694 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1697 static HRESULT WINAPI ddraw_surface1_AddOverlayDirtyRect(IDirectDrawSurface *iface, RECT *rect)
1699 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1700 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1702 return ddraw_surface7_AddOverlayDirtyRect(&This->IDirectDrawSurface7_iface, rect);
1705 /*****************************************************************************
1706 * IDirectDrawSurface7::GetDC
1708 * Returns a GDI device context for the surface
1710 * Params:
1711 * hdc: Address of a HDC variable to store the dc to
1713 * Returns:
1714 * DD_OK on success
1715 * DDERR_INVALIDPARAMS if hdc is NULL
1716 * For details, see IWineD3DSurface::GetDC
1718 *****************************************************************************/
1719 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1721 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1722 HRESULT hr;
1724 TRACE("iface %p, dc %p.\n", iface, hdc);
1726 if(!hdc)
1727 return DDERR_INVALIDPARAMS;
1729 EnterCriticalSection(&ddraw_cs);
1730 hr = wined3d_surface_getdc(This->wined3d_surface, hdc);
1731 LeaveCriticalSection(&ddraw_cs);
1732 switch(hr)
1734 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1735 * touch *hdc
1737 case WINED3DERR_INVALIDCALL:
1738 if(hdc) *hdc = NULL;
1739 return DDERR_INVALIDPARAMS;
1741 default: return hr;
1745 static HRESULT WINAPI ddraw_surface4_GetDC(IDirectDrawSurface4 *iface, HDC *dc)
1747 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1748 TRACE("iface %p, dc %p.\n", iface, dc);
1750 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1753 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1755 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1756 TRACE("iface %p, dc %p.\n", iface, dc);
1758 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1761 static HRESULT WINAPI ddraw_surface2_GetDC(IDirectDrawSurface2 *iface, HDC *dc)
1763 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1764 TRACE("iface %p, dc %p.\n", iface, dc);
1766 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1769 static HRESULT WINAPI ddraw_surface1_GetDC(IDirectDrawSurface *iface, HDC *dc)
1771 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1772 TRACE("iface %p, dc %p.\n", iface, dc);
1774 return ddraw_surface7_GetDC(&This->IDirectDrawSurface7_iface, dc);
1777 /*****************************************************************************
1778 * IDirectDrawSurface7::ReleaseDC
1780 * Releases the DC that was constructed with GetDC
1782 * Params:
1783 * hdc: HDC to release
1785 * Returns:
1786 * DD_OK on success
1787 * For more details, see IWineD3DSurface::ReleaseDC
1789 *****************************************************************************/
1790 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1792 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1793 HRESULT hr;
1795 TRACE("iface %p, dc %p.\n", iface, hdc);
1797 EnterCriticalSection(&ddraw_cs);
1798 hr = wined3d_surface_releasedc(This->wined3d_surface, hdc);
1799 LeaveCriticalSection(&ddraw_cs);
1800 return hr;
1803 static HRESULT WINAPI ddraw_surface4_ReleaseDC(IDirectDrawSurface4 *iface, HDC dc)
1805 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1806 TRACE("iface %p, dc %p.\n", iface, dc);
1808 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1811 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1813 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1814 TRACE("iface %p, dc %p.\n", iface, dc);
1816 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1819 static HRESULT WINAPI ddraw_surface2_ReleaseDC(IDirectDrawSurface2 *iface, HDC dc)
1821 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1822 TRACE("iface %p, dc %p.\n", iface, dc);
1824 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1827 static HRESULT WINAPI ddraw_surface1_ReleaseDC(IDirectDrawSurface *iface, HDC dc)
1829 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1830 TRACE("iface %p, dc %p.\n", iface, dc);
1832 return ddraw_surface7_ReleaseDC(&This->IDirectDrawSurface7_iface, dc);
1835 /*****************************************************************************
1836 * IDirectDrawSurface7::GetCaps
1838 * Returns the surface's caps
1840 * Params:
1841 * Caps: Address to write the caps to
1843 * Returns:
1844 * DD_OK on success
1845 * DDERR_INVALIDPARAMS if Caps is NULL
1847 *****************************************************************************/
1848 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1850 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1852 TRACE("iface %p, caps %p.\n", iface, Caps);
1854 if(!Caps)
1855 return DDERR_INVALIDPARAMS;
1857 *Caps = This->surface_desc.ddsCaps;
1858 return DD_OK;
1861 static HRESULT WINAPI ddraw_surface4_GetCaps(IDirectDrawSurface4 *iface, DDSCAPS2 *caps)
1863 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
1864 TRACE("iface %p, caps %p.\n", iface, caps);
1866 return ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, caps);
1869 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1871 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1872 DDSCAPS2 caps2;
1873 HRESULT hr;
1875 TRACE("iface %p, caps %p.\n", iface, caps);
1877 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1878 if (FAILED(hr)) return hr;
1880 caps->dwCaps = caps2.dwCaps;
1881 return hr;
1884 static HRESULT WINAPI ddraw_surface2_GetCaps(IDirectDrawSurface2 *iface, DDSCAPS *caps)
1886 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
1887 DDSCAPS2 caps2;
1888 HRESULT hr;
1890 TRACE("iface %p, caps %p.\n", iface, caps);
1892 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1893 if (FAILED(hr)) return hr;
1895 caps->dwCaps = caps2.dwCaps;
1896 return hr;
1899 static HRESULT WINAPI ddraw_surface1_GetCaps(IDirectDrawSurface *iface, DDSCAPS *caps)
1901 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
1902 DDSCAPS2 caps2;
1903 HRESULT hr;
1905 TRACE("iface %p, caps %p.\n", iface, caps);
1907 hr = ddraw_surface7_GetCaps(&This->IDirectDrawSurface7_iface, &caps2);
1908 if (FAILED(hr)) return hr;
1910 caps->dwCaps = caps2.dwCaps;
1911 return hr;
1914 /*****************************************************************************
1915 * IDirectDrawSurface7::SetPriority
1917 * Sets a texture priority for managed textures.
1919 * Params:
1920 * Priority: The new priority
1922 * Returns:
1923 * DD_OK on success
1924 * For more details, see IWineD3DSurface::SetPriority
1926 *****************************************************************************/
1927 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1929 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1930 HRESULT hr;
1932 TRACE("iface %p, priority %u.\n", iface, Priority);
1934 EnterCriticalSection(&ddraw_cs);
1935 hr = wined3d_surface_set_priority(This->wined3d_surface, Priority);
1936 LeaveCriticalSection(&ddraw_cs);
1937 return hr;
1940 /*****************************************************************************
1941 * IDirectDrawSurface7::GetPriority
1943 * Returns the surface's priority
1945 * Params:
1946 * Priority: Address of a variable to write the priority to
1948 * Returns:
1949 * D3D_OK on success
1950 * DDERR_INVALIDPARAMS if Priority == NULL
1951 * For more details, see IWineD3DSurface::GetPriority
1953 *****************************************************************************/
1954 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
1956 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1958 TRACE("iface %p, priority %p.\n", iface, Priority);
1960 if(!Priority)
1962 return DDERR_INVALIDPARAMS;
1965 EnterCriticalSection(&ddraw_cs);
1966 *Priority = wined3d_surface_get_priority(This->wined3d_surface);
1967 LeaveCriticalSection(&ddraw_cs);
1968 return DD_OK;
1971 /*****************************************************************************
1972 * IDirectDrawSurface7::SetPrivateData
1974 * Stores some data in the surface that is intended for the application's
1975 * use.
1977 * Params:
1978 * tag: GUID that identifies the data
1979 * Data: Pointer to the private data
1980 * Size: Size of the private data
1981 * Flags: Some flags
1983 * Returns:
1984 * D3D_OK on success
1985 * For more details, see IWineD3DSurface::SetPrivateData
1987 *****************************************************************************/
1988 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
1989 REFGUID tag, void *Data, DWORD Size, DWORD Flags)
1991 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
1992 struct wined3d_resource *resource;
1993 HRESULT hr;
1995 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
1996 iface, debugstr_guid(tag), Data, Size, Flags);
1998 EnterCriticalSection(&ddraw_cs);
1999 resource = wined3d_surface_get_resource(This->wined3d_surface);
2000 hr = wined3d_resource_set_private_data(resource, tag, Data, Size, Flags);
2001 LeaveCriticalSection(&ddraw_cs);
2002 switch(hr)
2004 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2005 default: return hr;
2009 static HRESULT WINAPI ddraw_surface4_SetPrivateData(IDirectDrawSurface4 *iface,
2010 REFGUID tag, void *data, DWORD size, DWORD flags)
2012 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2013 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
2014 iface, debugstr_guid(tag), data, size, flags);
2016 return ddraw_surface7_SetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size, flags);
2019 /*****************************************************************************
2020 * IDirectDrawSurface7::GetPrivateData
2022 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
2024 * Params:
2025 * tag: GUID of the data to return
2026 * Data: Address where to write the data to
2027 * Size: Size of the buffer at Data
2029 * Returns:
2030 * DD_OK on success
2031 * DDERR_INVALIDPARAMS if Data is NULL
2032 * For more details, see IWineD3DSurface::GetPrivateData
2034 *****************************************************************************/
2035 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
2037 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2038 struct wined3d_resource *resource;
2039 HRESULT hr;
2041 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2042 iface, debugstr_guid(tag), Data, Size);
2044 if(!Data)
2045 return DDERR_INVALIDPARAMS;
2047 EnterCriticalSection(&ddraw_cs);
2048 resource = wined3d_surface_get_resource(This->wined3d_surface);
2049 hr = wined3d_resource_get_private_data(resource, tag, Data, Size);
2050 LeaveCriticalSection(&ddraw_cs);
2051 return hr;
2054 static HRESULT WINAPI ddraw_surface4_GetPrivateData(IDirectDrawSurface4 *iface, REFGUID tag, void *data, DWORD *size)
2056 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2057 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
2058 iface, debugstr_guid(tag), data, size);
2060 return ddraw_surface7_GetPrivateData(&This->IDirectDrawSurface7_iface, tag, data, size);
2063 /*****************************************************************************
2064 * IDirectDrawSurface7::FreePrivateData
2066 * Frees private data stored in the surface
2068 * Params:
2069 * tag: Tag of the data to free
2071 * Returns:
2072 * D3D_OK on success
2073 * For more details, see IWineD3DSurface::FreePrivateData
2075 *****************************************************************************/
2076 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
2078 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2079 struct wined3d_resource *resource;
2080 HRESULT hr;
2082 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2084 EnterCriticalSection(&ddraw_cs);
2085 resource = wined3d_surface_get_resource(This->wined3d_surface);
2086 hr = wined3d_resource_free_private_data(resource, tag);
2087 LeaveCriticalSection(&ddraw_cs);
2088 return hr;
2091 static HRESULT WINAPI ddraw_surface4_FreePrivateData(IDirectDrawSurface4 *iface, REFGUID tag)
2093 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2094 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
2096 return ddraw_surface7_FreePrivateData(&This->IDirectDrawSurface7_iface, tag);
2099 /*****************************************************************************
2100 * IDirectDrawSurface7::PageLock
2102 * Prevents a sysmem surface from being paged out
2104 * Params:
2105 * Flags: Not used, must be 0(unchecked)
2107 * Returns:
2108 * DD_OK, because it's a stub
2110 *****************************************************************************/
2111 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
2113 TRACE("iface %p, flags %#x.\n", iface, Flags);
2115 /* This is Windows memory management related - we don't need this */
2116 return DD_OK;
2119 static HRESULT WINAPI ddraw_surface4_PageLock(IDirectDrawSurface4 *iface, DWORD flags)
2121 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2122 TRACE("iface %p, flags %#x.\n", iface, flags);
2124 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2127 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
2129 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2130 TRACE("iface %p, flags %#x.\n", iface, flags);
2132 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2135 static HRESULT WINAPI ddraw_surface2_PageLock(IDirectDrawSurface2 *iface, DWORD flags)
2137 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2138 TRACE("iface %p, flags %#x.\n", iface, flags);
2140 return ddraw_surface7_PageLock(&This->IDirectDrawSurface7_iface, flags);
2143 /*****************************************************************************
2144 * IDirectDrawSurface7::PageUnlock
2146 * Allows a sysmem surface to be paged out
2148 * Params:
2149 * Flags: Not used, must be 0(unchecked)
2151 * Returns:
2152 * DD_OK, because it's a stub
2154 *****************************************************************************/
2155 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
2157 TRACE("iface %p, flags %#x.\n", iface, Flags);
2159 return DD_OK;
2162 static HRESULT WINAPI ddraw_surface4_PageUnlock(IDirectDrawSurface4 *iface, DWORD flags)
2164 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2165 TRACE("iface %p, flags %#x.\n", iface, flags);
2167 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2170 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
2172 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2173 TRACE("iface %p, flags %#x.\n", iface, flags);
2175 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2178 static HRESULT WINAPI ddraw_surface2_PageUnlock(IDirectDrawSurface2 *iface, DWORD flags)
2180 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2181 TRACE("iface %p, flags %#x.\n", iface, flags);
2183 return ddraw_surface7_PageUnlock(&This->IDirectDrawSurface7_iface, flags);
2186 /*****************************************************************************
2187 * IDirectDrawSurface7::BltBatch
2189 * An unimplemented function
2191 * Params:
2194 * Returns:
2195 * DDERR_UNSUPPORTED
2197 *****************************************************************************/
2198 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
2200 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
2202 /* MSDN: "not currently implemented" */
2203 return DDERR_UNSUPPORTED;
2206 static HRESULT WINAPI ddraw_surface4_BltBatch(IDirectDrawSurface4 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2208 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2209 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2211 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2214 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2216 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2217 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2219 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2222 static HRESULT WINAPI ddraw_surface2_BltBatch(IDirectDrawSurface2 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2224 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2225 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2227 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2230 static HRESULT WINAPI ddraw_surface1_BltBatch(IDirectDrawSurface *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
2232 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2233 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
2235 return ddraw_surface7_BltBatch(&This->IDirectDrawSurface7_iface, batch, count, flags);
2238 /*****************************************************************************
2239 * IDirectDrawSurface7::EnumAttachedSurfaces
2241 * Enumerates all surfaces attached to this surface
2243 * Params:
2244 * context: Pointer to pass unmodified to the callback
2245 * cb: Callback function to call for each surface
2247 * Returns:
2248 * DD_OK on success
2249 * DDERR_INVALIDPARAMS if cb is NULL
2251 *****************************************************************************/
2252 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
2253 void *context, LPDDENUMSURFACESCALLBACK7 cb)
2255 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2256 IDirectDrawSurfaceImpl *surf;
2257 DDSURFACEDESC2 desc;
2258 int i;
2260 /* Attached surfaces aren't handled in WineD3D */
2261 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
2263 if(!cb)
2264 return DDERR_INVALIDPARAMS;
2266 EnterCriticalSection(&ddraw_cs);
2267 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
2269 surf = This->complex_array[i];
2270 if(!surf) break;
2272 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2273 desc = surf->surface_desc;
2274 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2275 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2277 LeaveCriticalSection(&ddraw_cs);
2278 return DD_OK;
2282 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
2284 ddraw_surface7_AddRef(&surf->IDirectDrawSurface7_iface);
2285 desc = surf->surface_desc;
2286 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
2287 if (cb(&surf->IDirectDrawSurface7_iface, &desc, context) == DDENUMRET_CANCEL)
2289 LeaveCriticalSection(&ddraw_cs);
2290 return DD_OK;
2294 TRACE(" end of enumeration.\n");
2296 LeaveCriticalSection(&ddraw_cs);
2297 return DD_OK;
2300 struct callback_info2
2302 LPDDENUMSURFACESCALLBACK2 callback;
2303 void *context;
2306 struct callback_info
2308 LPDDENUMSURFACESCALLBACK callback;
2309 void *context;
2312 static HRESULT CALLBACK EnumCallback2(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2314 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
2315 const struct callback_info2 *info = context;
2317 ddraw_surface4_AddRef(&This->IDirectDrawSurface4_iface);
2318 ddraw_surface7_Release(surface);
2320 return info->callback(&This->IDirectDrawSurface4_iface, surface_desc, info->context);
2323 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
2325 IDirectDrawSurfaceImpl *surface_impl = impl_from_IDirectDrawSurface7(surface);
2326 const struct callback_info *info = context;
2328 ddraw_surface1_AddRef(&surface_impl->IDirectDrawSurface_iface);
2329 ddraw_surface7_Release(surface);
2331 return info->callback(&surface_impl->IDirectDrawSurface_iface,
2332 (DDSURFACEDESC *)surface_desc, info->context);
2335 static HRESULT WINAPI ddraw_surface4_EnumAttachedSurfaces(IDirectDrawSurface4 *iface,
2336 void *context, LPDDENUMSURFACESCALLBACK2 callback)
2338 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2339 struct callback_info2 info;
2341 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2343 info.callback = callback;
2344 info.context = context;
2346 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2347 &info, EnumCallback2);
2350 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
2351 void *context, LPDDENUMSURFACESCALLBACK callback)
2353 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2354 struct callback_info info;
2356 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2358 info.callback = callback;
2359 info.context = context;
2361 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2362 &info, EnumCallback);
2365 static HRESULT WINAPI ddraw_surface2_EnumAttachedSurfaces(IDirectDrawSurface2 *iface,
2366 void *context, LPDDENUMSURFACESCALLBACK callback)
2368 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2369 struct callback_info info;
2371 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2373 info.callback = callback;
2374 info.context = context;
2376 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2377 &info, EnumCallback);
2380 static HRESULT WINAPI ddraw_surface1_EnumAttachedSurfaces(IDirectDrawSurface *iface,
2381 void *context, LPDDENUMSURFACESCALLBACK callback)
2383 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2384 struct callback_info info;
2386 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
2388 info.callback = callback;
2389 info.context = context;
2391 return ddraw_surface7_EnumAttachedSurfaces(&This->IDirectDrawSurface7_iface,
2392 &info, EnumCallback);
2395 /*****************************************************************************
2396 * IDirectDrawSurface7::EnumOverlayZOrders
2398 * "Enumerates the overlay surfaces on the specified destination"
2400 * Params:
2401 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
2402 * context: context to pass back to the callback
2403 * cb: callback function to call for each enumerated surface
2405 * Returns:
2406 * DD_OK, because it's a stub
2408 *****************************************************************************/
2409 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
2410 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
2412 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
2414 return DD_OK;
2417 static HRESULT WINAPI ddraw_surface4_EnumOverlayZOrders(IDirectDrawSurface4 *iface,
2418 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK2 callback)
2420 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2421 struct callback_info2 info;
2423 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2425 info.callback = callback;
2426 info.context = context;
2428 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2429 flags, &info, EnumCallback2);
2432 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
2433 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2435 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2436 struct callback_info info;
2438 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2440 info.callback = callback;
2441 info.context = context;
2443 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2444 flags, &info, EnumCallback);
2447 static HRESULT WINAPI ddraw_surface2_EnumOverlayZOrders(IDirectDrawSurface2 *iface,
2448 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2450 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2451 struct callback_info info;
2453 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2455 info.callback = callback;
2456 info.context = context;
2458 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2459 flags, &info, EnumCallback);
2462 static HRESULT WINAPI ddraw_surface1_EnumOverlayZOrders(IDirectDrawSurface *iface,
2463 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
2465 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2466 struct callback_info info;
2468 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
2470 info.callback = callback;
2471 info.context = context;
2473 return ddraw_surface7_EnumOverlayZOrders(&This->IDirectDrawSurface7_iface,
2474 flags, &info, EnumCallback);
2477 /*****************************************************************************
2478 * IDirectDrawSurface7::GetBltStatus
2480 * Returns the blitting status
2482 * Params:
2483 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
2485 * Returns:
2486 * See IWineD3DSurface::Blt
2488 *****************************************************************************/
2489 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2491 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2492 HRESULT hr;
2494 TRACE("iface %p, flags %#x.\n", iface, Flags);
2496 EnterCriticalSection(&ddraw_cs);
2497 hr = wined3d_surface_get_blt_status(This->wined3d_surface, Flags);
2498 LeaveCriticalSection(&ddraw_cs);
2499 switch(hr)
2501 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2502 default: return hr;
2506 static HRESULT WINAPI ddraw_surface4_GetBltStatus(IDirectDrawSurface4 *iface, DWORD flags)
2508 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2509 TRACE("iface %p, flags %#x.\n", iface, flags);
2511 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2514 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
2516 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2517 TRACE("iface %p, flags %#x.\n", iface, flags);
2519 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2522 static HRESULT WINAPI ddraw_surface2_GetBltStatus(IDirectDrawSurface2 *iface, DWORD flags)
2524 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2525 TRACE("iface %p, flags %#x.\n", iface, flags);
2527 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2530 static HRESULT WINAPI ddraw_surface1_GetBltStatus(IDirectDrawSurface *iface, DWORD flags)
2532 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2533 TRACE("iface %p, flags %#x.\n", iface, flags);
2535 return ddraw_surface7_GetBltStatus(&This->IDirectDrawSurface7_iface, flags);
2538 /*****************************************************************************
2539 * IDirectDrawSurface7::GetColorKey
2541 * Returns the color key assigned to the surface
2543 * Params:
2544 * Flags: Some flags
2545 * CKey: Address to store the key to
2547 * Returns:
2548 * DD_OK on success
2549 * DDERR_INVALIDPARAMS if CKey is NULL
2551 *****************************************************************************/
2552 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2554 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2556 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2558 if(!CKey)
2559 return DDERR_INVALIDPARAMS;
2561 EnterCriticalSection(&ddraw_cs);
2563 switch (Flags)
2565 case DDCKEY_DESTBLT:
2566 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
2568 LeaveCriticalSection(&ddraw_cs);
2569 return DDERR_NOCOLORKEY;
2571 *CKey = This->surface_desc.ddckCKDestBlt;
2572 break;
2574 case DDCKEY_DESTOVERLAY:
2575 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
2577 LeaveCriticalSection(&ddraw_cs);
2578 return DDERR_NOCOLORKEY;
2580 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
2581 break;
2583 case DDCKEY_SRCBLT:
2584 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
2586 LeaveCriticalSection(&ddraw_cs);
2587 return DDERR_NOCOLORKEY;
2589 *CKey = This->surface_desc.ddckCKSrcBlt;
2590 break;
2592 case DDCKEY_SRCOVERLAY:
2593 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
2595 LeaveCriticalSection(&ddraw_cs);
2596 return DDERR_NOCOLORKEY;
2598 *CKey = This->surface_desc.ddckCKSrcOverlay;
2599 break;
2601 default:
2602 LeaveCriticalSection(&ddraw_cs);
2603 return DDERR_INVALIDPARAMS;
2606 LeaveCriticalSection(&ddraw_cs);
2607 return DD_OK;
2610 static HRESULT WINAPI ddraw_surface4_GetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
2612 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2613 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2615 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2618 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2620 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2621 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2623 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2626 static HRESULT WINAPI ddraw_surface2_GetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
2628 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2629 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2631 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2634 static HRESULT WINAPI ddraw_surface1_GetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
2636 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2637 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2639 return ddraw_surface7_GetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
2642 /*****************************************************************************
2643 * IDirectDrawSurface7::GetFlipStatus
2645 * Returns the flipping status of the surface
2647 * Params:
2648 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
2650 * Returns:
2651 * See IWineD3DSurface::GetFlipStatus
2653 *****************************************************************************/
2654 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
2656 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2657 HRESULT hr;
2659 TRACE("iface %p, flags %#x.\n", iface, Flags);
2661 EnterCriticalSection(&ddraw_cs);
2662 hr = wined3d_surface_get_flip_status(This->wined3d_surface, Flags);
2663 LeaveCriticalSection(&ddraw_cs);
2664 switch(hr)
2666 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2667 default: return hr;
2671 static HRESULT WINAPI ddraw_surface4_GetFlipStatus(IDirectDrawSurface4 *iface, DWORD flags)
2673 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2674 TRACE("iface %p, flags %#x.\n", iface, flags);
2676 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2679 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
2681 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2682 TRACE("iface %p, flags %#x.\n", iface, flags);
2684 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2687 static HRESULT WINAPI ddraw_surface2_GetFlipStatus(IDirectDrawSurface2 *iface, DWORD flags)
2689 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2690 TRACE("iface %p, flags %#x.\n", iface, flags);
2692 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2695 static HRESULT WINAPI ddraw_surface1_GetFlipStatus(IDirectDrawSurface *iface, DWORD flags)
2697 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2698 TRACE("iface %p, flags %#x.\n", iface, flags);
2700 return ddraw_surface7_GetFlipStatus(&This->IDirectDrawSurface7_iface, flags);
2703 /*****************************************************************************
2704 * IDirectDrawSurface7::GetOverlayPosition
2706 * Returns the display coordinates of a visible and active overlay surface
2708 * Params:
2712 * Returns:
2713 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
2714 *****************************************************************************/
2715 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
2717 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2718 HRESULT hr;
2720 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
2722 EnterCriticalSection(&ddraw_cs);
2723 hr = wined3d_surface_get_overlay_position(This->wined3d_surface, X, Y);
2724 LeaveCriticalSection(&ddraw_cs);
2725 return hr;
2728 static HRESULT WINAPI ddraw_surface4_GetOverlayPosition(IDirectDrawSurface4 *iface, LONG *x, LONG *y)
2730 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2731 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2733 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2736 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
2738 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2739 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2741 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2744 static HRESULT WINAPI ddraw_surface2_GetOverlayPosition(IDirectDrawSurface2 *iface, LONG *x, LONG *y)
2746 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2747 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2749 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2752 static HRESULT WINAPI ddraw_surface1_GetOverlayPosition(IDirectDrawSurface *iface, LONG *x, LONG *y)
2754 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2755 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
2757 return ddraw_surface7_GetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
2760 /*****************************************************************************
2761 * IDirectDrawSurface7::GetPixelFormat
2763 * Returns the pixel format of the Surface
2765 * Params:
2766 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
2767 * format should be written
2769 * Returns:
2770 * DD_OK on success
2771 * DDERR_INVALIDPARAMS if PixelFormat is NULL
2773 *****************************************************************************/
2774 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
2776 /* What is DDERR_INVALIDSURFACETYPE for here? */
2777 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2779 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
2781 if(!PixelFormat)
2782 return DDERR_INVALIDPARAMS;
2784 EnterCriticalSection(&ddraw_cs);
2785 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
2786 LeaveCriticalSection(&ddraw_cs);
2788 return DD_OK;
2791 static HRESULT WINAPI ddraw_surface4_GetPixelFormat(IDirectDrawSurface4 *iface, DDPIXELFORMAT *pixel_format)
2793 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2794 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2796 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2799 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
2801 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2802 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2804 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2807 static HRESULT WINAPI ddraw_surface2_GetPixelFormat(IDirectDrawSurface2 *iface, DDPIXELFORMAT *pixel_format)
2809 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2810 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2812 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2815 static HRESULT WINAPI ddraw_surface1_GetPixelFormat(IDirectDrawSurface *iface, DDPIXELFORMAT *pixel_format)
2817 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2818 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
2820 return ddraw_surface7_GetPixelFormat(&This->IDirectDrawSurface7_iface, pixel_format);
2823 /*****************************************************************************
2824 * IDirectDrawSurface7::GetSurfaceDesc
2826 * Returns the description of this surface
2828 * Params:
2829 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
2830 * surface desc
2832 * Returns:
2833 * DD_OK on success
2834 * DDERR_INVALIDPARAMS if DDSD is NULL
2836 *****************************************************************************/
2837 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
2839 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
2841 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2843 if(!DDSD)
2844 return DDERR_INVALIDPARAMS;
2846 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
2848 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
2849 return DDERR_INVALIDPARAMS;
2852 EnterCriticalSection(&ddraw_cs);
2853 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
2854 TRACE("Returning surface desc:\n");
2855 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
2857 LeaveCriticalSection(&ddraw_cs);
2858 return DD_OK;
2861 static HRESULT WINAPI ddraw_surface4_GetSurfaceDesc(IDirectDrawSurface4 *iface, DDSURFACEDESC2 *DDSD)
2863 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2864 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2866 return ddraw_surface7_GetSurfaceDesc(&This->IDirectDrawSurface7_iface, DDSD);
2869 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
2871 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2873 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
2875 if (!surface_desc) return DDERR_INVALIDPARAMS;
2877 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
2879 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
2880 return DDERR_INVALIDPARAMS;
2883 EnterCriticalSection(&ddraw_cs);
2884 DD_STRUCT_COPY_BYSIZE(surface_desc, (DDSURFACEDESC *)&This->surface_desc);
2885 TRACE("Returning surface desc:\n");
2886 if (TRACE_ON(ddraw))
2888 /* DDRAW_dump_surface_desc handles the smaller size */
2889 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
2892 LeaveCriticalSection(&ddraw_cs);
2893 return DD_OK;
2896 static HRESULT WINAPI ddraw_surface2_GetSurfaceDesc(IDirectDrawSurface2 *iface, DDSURFACEDESC *DDSD)
2898 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2899 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2901 return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
2904 static HRESULT WINAPI ddraw_surface1_GetSurfaceDesc(IDirectDrawSurface *iface, DDSURFACEDESC *DDSD)
2906 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2907 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
2909 return ddraw_surface3_GetSurfaceDesc(&This->IDirectDrawSurface3_iface, DDSD);
2912 /*****************************************************************************
2913 * IDirectDrawSurface7::Initialize
2915 * Initializes the surface. This is a no-op in Wine
2917 * Params:
2918 * DD: Pointer to an DirectDraw interface
2919 * DDSD: Surface description for initialization
2921 * Returns:
2922 * DDERR_ALREADYINITIALIZED
2924 *****************************************************************************/
2925 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
2926 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
2928 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2930 return DDERR_ALREADYINITIALIZED;
2933 static HRESULT WINAPI ddraw_surface4_Initialize(IDirectDrawSurface4 *iface,
2934 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
2936 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
2937 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2939 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2940 ddraw, surface_desc);
2943 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
2944 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2946 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2947 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2949 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2950 ddraw, (DDSURFACEDESC2 *)surface_desc);
2953 static HRESULT WINAPI ddraw_surface2_Initialize(IDirectDrawSurface2 *iface,
2954 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2956 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
2957 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2959 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2960 ddraw, (DDSURFACEDESC2 *)surface_desc);
2963 static HRESULT WINAPI ddraw_surface1_Initialize(IDirectDrawSurface *iface,
2964 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
2966 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
2967 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
2969 return ddraw_surface7_Initialize(&This->IDirectDrawSurface7_iface,
2970 ddraw, (DDSURFACEDESC2 *)surface_desc);
2973 /*****************************************************************************
2974 * IDirect3DTexture1::Initialize
2976 * The sdk says it's not implemented
2978 * Params:
2981 * Returns
2982 * DDERR_UNSUPPORTED
2984 *****************************************************************************/
2985 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
2986 IDirect3DDevice *device, IDirectDrawSurface *surface)
2988 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
2990 return DDERR_UNSUPPORTED; /* Unchecked */
2993 /*****************************************************************************
2994 * IDirectDrawSurface7::IsLost
2996 * Checks if the surface is lost
2998 * Returns:
2999 * DD_OK, if the surface is usable
3000 * DDERR_ISLOST if the surface is lost
3001 * See IWineD3DSurface::IsLost for more details
3003 *****************************************************************************/
3004 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
3006 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3007 HRESULT hr;
3009 TRACE("iface %p.\n", iface);
3011 EnterCriticalSection(&ddraw_cs);
3012 /* We lose the surface if the implementation was changed */
3013 if(This->ImplType != This->ddraw->ImplType)
3015 /* But this shouldn't happen. When we change the implementation,
3016 * all surfaces are re-created automatically, and their content
3017 * is copied
3019 ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
3020 LeaveCriticalSection(&ddraw_cs);
3021 return DDERR_SURFACELOST;
3024 hr = wined3d_surface_is_lost(This->wined3d_surface);
3025 LeaveCriticalSection(&ddraw_cs);
3026 switch(hr)
3028 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
3029 * WineD3D uses the same error for surfaces
3031 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
3032 default: return hr;
3036 static HRESULT WINAPI ddraw_surface4_IsLost(IDirectDrawSurface4 *iface)
3038 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3039 TRACE("iface %p.\n", iface);
3041 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3044 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
3046 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3047 TRACE("iface %p.\n", iface);
3049 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3052 static HRESULT WINAPI ddraw_surface2_IsLost(IDirectDrawSurface2 *iface)
3054 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3055 TRACE("iface %p.\n", iface);
3057 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3060 static HRESULT WINAPI ddraw_surface1_IsLost(IDirectDrawSurface *iface)
3062 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3063 TRACE("iface %p.\n", iface);
3065 return ddraw_surface7_IsLost(&This->IDirectDrawSurface7_iface);
3068 /*****************************************************************************
3069 * IDirectDrawSurface7::Restore
3071 * Restores a lost surface. This makes the surface usable again, but
3072 * doesn't reload its old contents
3074 * Returns:
3075 * DD_OK on success
3076 * See IWineD3DSurface::Restore for more details
3078 *****************************************************************************/
3079 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
3081 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3082 HRESULT hr;
3084 TRACE("iface %p.\n", iface);
3086 EnterCriticalSection(&ddraw_cs);
3087 if(This->ImplType != This->ddraw->ImplType)
3089 /* Call the recreation callback. Make sure to AddRef first */
3090 IDirectDrawSurface_AddRef(iface);
3091 ddraw_recreate_surfaces_cb(iface, &This->surface_desc, NULL /* Not needed */);
3093 hr = wined3d_surface_restore(This->wined3d_surface);
3094 LeaveCriticalSection(&ddraw_cs);
3095 return hr;
3098 static HRESULT WINAPI ddraw_surface4_Restore(IDirectDrawSurface4 *iface)
3100 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3101 TRACE("iface %p.\n", iface);
3103 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3106 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
3108 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3109 TRACE("iface %p.\n", iface);
3111 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3114 static HRESULT WINAPI ddraw_surface2_Restore(IDirectDrawSurface2 *iface)
3116 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3117 TRACE("iface %p.\n", iface);
3119 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3122 static HRESULT WINAPI ddraw_surface1_Restore(IDirectDrawSurface *iface)
3124 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3125 TRACE("iface %p.\n", iface);
3127 return ddraw_surface7_Restore(&This->IDirectDrawSurface7_iface);
3130 /*****************************************************************************
3131 * IDirectDrawSurface7::SetOverlayPosition
3133 * Changes the display coordinates of an overlay surface
3135 * Params:
3136 * X:
3137 * Y:
3139 * Returns:
3140 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
3141 *****************************************************************************/
3142 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
3144 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3145 HRESULT hr;
3147 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
3149 EnterCriticalSection(&ddraw_cs);
3150 hr = wined3d_surface_set_overlay_position(This->wined3d_surface, X, Y);
3151 LeaveCriticalSection(&ddraw_cs);
3152 return hr;
3155 static HRESULT WINAPI ddraw_surface4_SetOverlayPosition(IDirectDrawSurface4 *iface, LONG x, LONG y)
3157 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3158 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3160 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3163 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
3165 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3166 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3168 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3171 static HRESULT WINAPI ddraw_surface2_SetOverlayPosition(IDirectDrawSurface2 *iface, LONG x, LONG y)
3173 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3174 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3176 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3179 static HRESULT WINAPI ddraw_surface1_SetOverlayPosition(IDirectDrawSurface *iface, LONG x, LONG y)
3181 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3182 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
3184 return ddraw_surface7_SetOverlayPosition(&This->IDirectDrawSurface7_iface, x, y);
3187 /*****************************************************************************
3188 * IDirectDrawSurface7::UpdateOverlay
3190 * Modifies the attributes of an overlay surface.
3192 * Params:
3193 * SrcRect: The section of the source being used for the overlay
3194 * DstSurface: Address of the surface that is overlaid
3195 * DstRect: Place of the overlay
3196 * Flags: some DDOVER_* flags
3198 * Returns:
3199 * DDERR_UNSUPPORTED, because we don't support overlays
3201 *****************************************************************************/
3202 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
3203 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
3205 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3206 IDirectDrawSurfaceImpl *Dst = unsafe_impl_from_IDirectDrawSurface7(DstSurface);
3207 HRESULT hr;
3209 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3210 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
3212 EnterCriticalSection(&ddraw_cs);
3213 hr = wined3d_surface_update_overlay(This->wined3d_surface, SrcRect,
3214 Dst ? Dst->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
3215 LeaveCriticalSection(&ddraw_cs);
3216 switch(hr) {
3217 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
3218 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
3219 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
3220 default:
3221 return hr;
3225 static HRESULT WINAPI ddraw_surface4_UpdateOverlay(IDirectDrawSurface4 *iface, RECT *src_rect,
3226 IDirectDrawSurface4 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3228 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3229 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface4(dst_surface);
3230 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3231 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3233 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3234 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3237 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
3238 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3240 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3241 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
3242 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3243 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3245 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3246 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3249 static HRESULT WINAPI ddraw_surface2_UpdateOverlay(IDirectDrawSurface2 *iface, RECT *src_rect,
3250 IDirectDrawSurface2 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3252 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3253 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface2(dst_surface);
3254 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3255 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3257 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3258 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3261 static HRESULT WINAPI ddraw_surface1_UpdateOverlay(IDirectDrawSurface *iface, RECT *src_rect,
3262 IDirectDrawSurface *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
3264 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3265 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface(dst_surface);
3266 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
3267 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
3269 return ddraw_surface7_UpdateOverlay(&This->IDirectDrawSurface7_iface, src_rect,
3270 dst_impl ? &dst_impl->IDirectDrawSurface7_iface : NULL, dst_rect, flags, fx);
3273 /*****************************************************************************
3274 * IDirectDrawSurface7::UpdateOverlayDisplay
3276 * The DX7 sdk says that it's not implemented
3278 * Params:
3279 * Flags: ?
3281 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
3283 *****************************************************************************/
3284 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
3286 TRACE("iface %p, flags %#x.\n", iface, Flags);
3288 return DDERR_UNSUPPORTED;
3291 static HRESULT WINAPI ddraw_surface4_UpdateOverlayDisplay(IDirectDrawSurface4 *iface, DWORD flags)
3293 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3294 TRACE("iface %p, flags %#x.\n", iface, flags);
3296 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3299 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
3301 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3302 TRACE("iface %p, flags %#x.\n", iface, flags);
3304 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3307 static HRESULT WINAPI ddraw_surface2_UpdateOverlayDisplay(IDirectDrawSurface2 *iface, DWORD flags)
3309 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3310 TRACE("iface %p, flags %#x.\n", iface, flags);
3312 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3315 static HRESULT WINAPI ddraw_surface1_UpdateOverlayDisplay(IDirectDrawSurface *iface, DWORD flags)
3317 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3318 TRACE("iface %p, flags %#x.\n", iface, flags);
3320 return ddraw_surface7_UpdateOverlayDisplay(&This->IDirectDrawSurface7_iface, flags);
3323 /*****************************************************************************
3324 * IDirectDrawSurface7::UpdateOverlayZOrder
3326 * Sets an overlay's Z order
3328 * Params:
3329 * Flags: DDOVERZ_* flags
3330 * DDSRef: Defines the relative position in the overlay chain
3332 * Returns:
3333 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
3335 *****************************************************************************/
3336 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
3337 DWORD Flags, IDirectDrawSurface7 *DDSRef)
3339 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3340 IDirectDrawSurfaceImpl *Ref = unsafe_impl_from_IDirectDrawSurface7(DDSRef);
3341 HRESULT hr;
3343 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
3345 EnterCriticalSection(&ddraw_cs);
3346 hr = wined3d_surface_update_overlay_z_order(This->wined3d_surface,
3347 Flags, Ref ? Ref->wined3d_surface : NULL);
3348 LeaveCriticalSection(&ddraw_cs);
3349 return hr;
3352 static HRESULT WINAPI ddraw_surface4_UpdateOverlayZOrder(IDirectDrawSurface4 *iface,
3353 DWORD flags, IDirectDrawSurface4 *reference)
3355 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3356 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface4(reference);
3357 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3359 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3360 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3363 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
3364 DWORD flags, IDirectDrawSurface3 *reference)
3366 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3367 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
3368 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3370 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3371 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3374 static HRESULT WINAPI ddraw_surface2_UpdateOverlayZOrder(IDirectDrawSurface2 *iface,
3375 DWORD flags, IDirectDrawSurface2 *reference)
3377 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3378 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface2(reference);
3379 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3381 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3382 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3385 static HRESULT WINAPI ddraw_surface1_UpdateOverlayZOrder(IDirectDrawSurface *iface,
3386 DWORD flags, IDirectDrawSurface *reference)
3388 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3389 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface(reference);
3390 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
3392 return ddraw_surface7_UpdateOverlayZOrder(&This->IDirectDrawSurface7_iface, flags,
3393 reference_impl ? &reference_impl->IDirectDrawSurface7_iface : NULL);
3396 /*****************************************************************************
3397 * IDirectDrawSurface7::GetDDInterface
3399 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
3400 * surface belongs to
3402 * Params:
3403 * DD: Address to write the interface pointer to
3405 * Returns:
3406 * DD_OK on success
3407 * DDERR_INVALIDPARAMS if DD is NULL
3409 *****************************************************************************/
3410 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
3412 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3414 TRACE("iface %p, ddraw %p.\n", iface, DD);
3416 if(!DD)
3417 return DDERR_INVALIDPARAMS;
3419 switch(This->version)
3421 case 7:
3422 *DD = &This->ddraw->IDirectDraw7_iface;
3423 break;
3425 case 4:
3426 *DD = &This->ddraw->IDirectDraw4_iface;
3427 break;
3429 case 2:
3430 *DD = &This->ddraw->IDirectDraw2_iface;
3431 break;
3433 case 1:
3434 *DD = &This->ddraw->IDirectDraw_iface;
3435 break;
3438 IUnknown_AddRef((IUnknown *)*DD);
3440 return DD_OK;
3443 static HRESULT WINAPI ddraw_surface4_GetDDInterface(IDirectDrawSurface4 *iface, void **ddraw)
3445 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3446 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3448 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3451 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
3453 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3454 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3456 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3459 static HRESULT WINAPI ddraw_surface2_GetDDInterface(IDirectDrawSurface2 *iface, void **ddraw)
3461 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3462 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
3464 return ddraw_surface7_GetDDInterface(&This->IDirectDrawSurface7_iface, ddraw);
3467 /* This seems also windows implementation specific - I don't think WineD3D needs this */
3468 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
3470 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3471 volatile IDirectDrawSurfaceImpl* vThis = This;
3473 TRACE("iface %p.\n", iface);
3475 EnterCriticalSection(&ddraw_cs);
3476 /* A uniqueness value of 0 is apparently special.
3477 * This needs to be checked.
3478 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
3480 while (1) {
3481 DWORD old_uniqueness_value = vThis->uniqueness_value;
3482 DWORD new_uniqueness_value = old_uniqueness_value+1;
3484 if (old_uniqueness_value == 0) break;
3485 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
3487 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
3488 old_uniqueness_value,
3489 new_uniqueness_value)
3490 == old_uniqueness_value)
3491 break;
3494 LeaveCriticalSection(&ddraw_cs);
3495 return DD_OK;
3498 static HRESULT WINAPI ddraw_surface4_ChangeUniquenessValue(IDirectDrawSurface4 *iface)
3500 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3501 TRACE("iface %p.\n", iface);
3503 return ddraw_surface7_ChangeUniquenessValue(&This->IDirectDrawSurface7_iface);
3506 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
3508 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3510 TRACE("iface %p, value %p.\n", iface, pValue);
3512 EnterCriticalSection(&ddraw_cs);
3513 *pValue = This->uniqueness_value;
3514 LeaveCriticalSection(&ddraw_cs);
3515 return DD_OK;
3518 static HRESULT WINAPI ddraw_surface4_GetUniquenessValue(IDirectDrawSurface4 *iface, DWORD *pValue)
3520 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3521 TRACE("iface %p, value %p.\n", iface, pValue);
3523 return ddraw_surface7_GetUniquenessValue(&This->IDirectDrawSurface7_iface, pValue);
3526 /*****************************************************************************
3527 * IDirectDrawSurface7::SetLOD
3529 * Sets the level of detail of a texture
3531 * Params:
3532 * MaxLOD: LOD to set
3534 * Returns:
3535 * DD_OK on success
3536 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3538 *****************************************************************************/
3539 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
3541 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3542 HRESULT hr;
3544 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
3546 EnterCriticalSection(&ddraw_cs);
3547 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3549 LeaveCriticalSection(&ddraw_cs);
3550 return DDERR_INVALIDOBJECT;
3553 if (!This->wined3d_texture)
3555 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
3556 LeaveCriticalSection(&ddraw_cs);
3557 return DDERR_INVALIDOBJECT;
3560 hr = wined3d_texture_set_lod(This->wined3d_texture, MaxLOD);
3561 LeaveCriticalSection(&ddraw_cs);
3562 return hr;
3565 /*****************************************************************************
3566 * IDirectDrawSurface7::GetLOD
3568 * Returns the level of detail of a Direct3D texture
3570 * Params:
3571 * MaxLOD: Address to write the LOD to
3573 * Returns:
3574 * DD_OK on success
3575 * DDERR_INVALIDPARAMS if MaxLOD is NULL
3576 * DDERR_INVALIDOBJECT if the surface is invalid for this method
3578 *****************************************************************************/
3579 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
3581 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3583 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
3585 if(!MaxLOD)
3586 return DDERR_INVALIDPARAMS;
3588 EnterCriticalSection(&ddraw_cs);
3589 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
3591 LeaveCriticalSection(&ddraw_cs);
3592 return DDERR_INVALIDOBJECT;
3595 *MaxLOD = wined3d_texture_get_lod(This->wined3d_texture);
3596 LeaveCriticalSection(&ddraw_cs);
3597 return DD_OK;
3600 /*****************************************************************************
3601 * IDirectDrawSurface7::BltFast
3603 * Performs a fast Blit.
3605 * Params:
3606 * dstx: The x coordinate to blit to on the destination
3607 * dsty: The y coordinate to blit to on the destination
3608 * Source: The source surface
3609 * rsrc: The source rectangle
3610 * trans: Type of transfer. Some DDBLTFAST_* flags
3612 * Returns:
3613 * DD_OK on success
3614 * For more details, see IWineD3DSurface::BltFast
3616 *****************************************************************************/
3617 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
3618 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
3620 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3621 IDirectDrawSurfaceImpl *src = unsafe_impl_from_IDirectDrawSurface7(Source);
3622 DWORD src_w, src_h, dst_w, dst_h;
3623 HRESULT hr;
3625 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3626 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
3628 dst_w = This->surface_desc.dwWidth;
3629 dst_h = This->surface_desc.dwHeight;
3631 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
3632 * in that case
3634 if(rsrc)
3636 if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
3637 rsrc->right > src->surface_desc.dwWidth ||
3638 rsrc->bottom > src->surface_desc.dwHeight)
3640 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
3641 return DDERR_INVALIDRECT;
3644 src_w = rsrc->right - rsrc->left;
3645 src_h = rsrc->bottom - rsrc->top;
3647 else
3649 src_w = src->surface_desc.dwWidth;
3650 src_h = src->surface_desc.dwHeight;
3653 if (src_w > dst_w || dstx > dst_w - src_w
3654 || src_h > dst_h || dsty > dst_h - src_h)
3656 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
3657 return DDERR_INVALIDRECT;
3660 EnterCriticalSection(&ddraw_cs);
3661 hr = wined3d_surface_bltfast(This->wined3d_surface, dstx, dsty,
3662 src->wined3d_surface, rsrc, trans);
3663 LeaveCriticalSection(&ddraw_cs);
3664 switch(hr)
3666 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
3667 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
3668 default: return hr;
3672 static HRESULT WINAPI ddraw_surface4_BltFast(IDirectDrawSurface4 *iface, DWORD dst_x, DWORD dst_y,
3673 IDirectDrawSurface4 *src_surface, RECT *src_rect, DWORD flags)
3675 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3676 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface4(src_surface);
3677 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3678 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3680 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3681 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3684 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
3685 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
3687 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3688 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
3689 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3690 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3692 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3693 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3696 static HRESULT WINAPI ddraw_surface2_BltFast(IDirectDrawSurface2 *iface, DWORD dst_x, DWORD dst_y,
3697 IDirectDrawSurface2 *src_surface, RECT *src_rect, DWORD flags)
3699 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3700 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface2(src_surface);
3701 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3702 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3704 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3705 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3708 static HRESULT WINAPI ddraw_surface1_BltFast(IDirectDrawSurface *iface, DWORD dst_x, DWORD dst_y,
3709 IDirectDrawSurface *src_surface, RECT *src_rect, DWORD flags)
3711 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3712 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface(src_surface);
3713 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
3714 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
3716 return ddraw_surface7_BltFast(&This->IDirectDrawSurface7_iface, dst_x, dst_y,
3717 src_impl ? &src_impl->IDirectDrawSurface7_iface : NULL, src_rect, flags);
3720 /*****************************************************************************
3721 * IDirectDrawSurface7::GetClipper
3723 * Returns the IDirectDrawClipper interface of the clipper assigned to this
3724 * surface
3726 * Params:
3727 * Clipper: Address to store the interface pointer at
3729 * Returns:
3730 * DD_OK on success
3731 * DDERR_INVALIDPARAMS if Clipper is NULL
3732 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
3734 *****************************************************************************/
3735 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
3737 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3739 TRACE("iface %p, clipper %p.\n", iface, Clipper);
3741 if(!Clipper)
3743 LeaveCriticalSection(&ddraw_cs);
3744 return DDERR_INVALIDPARAMS;
3747 EnterCriticalSection(&ddraw_cs);
3748 if(This->clipper == NULL)
3750 LeaveCriticalSection(&ddraw_cs);
3751 return DDERR_NOCLIPPERATTACHED;
3754 *Clipper = (IDirectDrawClipper *)This->clipper;
3755 IDirectDrawClipper_AddRef(*Clipper);
3756 LeaveCriticalSection(&ddraw_cs);
3757 return DD_OK;
3760 static HRESULT WINAPI ddraw_surface4_GetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper **clipper)
3762 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3763 TRACE("iface %p, clipper %p.\n", iface, clipper);
3765 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3768 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
3770 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3771 TRACE("iface %p, clipper %p.\n", iface, clipper);
3773 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3776 static HRESULT WINAPI ddraw_surface2_GetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper **clipper)
3778 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3779 TRACE("iface %p, clipper %p.\n", iface, clipper);
3781 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3784 static HRESULT WINAPI ddraw_surface1_GetClipper(IDirectDrawSurface *iface, IDirectDrawClipper **clipper)
3786 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3787 TRACE("iface %p, clipper %p.\n", iface, clipper);
3789 return ddraw_surface7_GetClipper(&This->IDirectDrawSurface7_iface, clipper);
3792 /*****************************************************************************
3793 * IDirectDrawSurface7::SetClipper
3795 * Sets a clipper for the surface
3797 * Params:
3798 * Clipper: IDirectDrawClipper interface of the clipper to set
3800 * Returns:
3801 * DD_OK on success
3803 *****************************************************************************/
3804 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface,
3805 IDirectDrawClipper *iclipper)
3807 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3808 IDirectDrawClipperImpl *clipper = unsafe_impl_from_IDirectDrawClipper(iclipper);
3809 IDirectDrawClipperImpl *oldClipper = This->clipper;
3810 HWND clipWindow;
3811 HRESULT hr;
3813 TRACE("iface %p, clipper %p.\n", iface, iclipper);
3815 EnterCriticalSection(&ddraw_cs);
3816 if (clipper == This->clipper)
3818 LeaveCriticalSection(&ddraw_cs);
3819 return DD_OK;
3822 This->clipper = clipper;
3824 if (clipper != NULL)
3825 IDirectDrawClipper_AddRef(iclipper);
3826 if(oldClipper)
3827 IDirectDrawClipper_Release(&oldClipper->IDirectDrawClipper_iface);
3829 hr = wined3d_surface_set_clipper(This->wined3d_surface,
3830 This->clipper ? This->clipper->wineD3DClipper : NULL);
3832 if (This->wined3d_swapchain)
3834 clipWindow = NULL;
3835 if(clipper) {
3836 IDirectDrawClipper_GetHWnd(iclipper, &clipWindow);
3839 if (clipWindow)
3840 wined3d_swapchain_set_window(This->wined3d_swapchain, clipWindow);
3841 else
3842 wined3d_swapchain_set_window(This->wined3d_swapchain, This->ddraw->d3d_window);
3845 LeaveCriticalSection(&ddraw_cs);
3846 return hr;
3849 static HRESULT WINAPI ddraw_surface4_SetClipper(IDirectDrawSurface4 *iface, IDirectDrawClipper *clipper)
3851 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3852 TRACE("iface %p, clipper %p.\n", iface, clipper);
3854 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3857 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
3859 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3860 TRACE("iface %p, clipper %p.\n", iface, clipper);
3862 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3865 static HRESULT WINAPI ddraw_surface2_SetClipper(IDirectDrawSurface2 *iface, IDirectDrawClipper *clipper)
3867 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
3868 TRACE("iface %p, clipper %p.\n", iface, clipper);
3870 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3873 static HRESULT WINAPI ddraw_surface1_SetClipper(IDirectDrawSurface *iface, IDirectDrawClipper *clipper)
3875 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
3876 TRACE("iface %p, clipper %p.\n", iface, clipper);
3878 return ddraw_surface7_SetClipper(&This->IDirectDrawSurface7_iface, clipper);
3881 /*****************************************************************************
3882 * IDirectDrawSurface7::SetSurfaceDesc
3884 * Sets the surface description. It can override the pixel format, the surface
3885 * memory, ...
3886 * It's not really tested.
3888 * Params:
3889 * DDSD: Pointer to the new surface description to set
3890 * Flags: Some flags
3892 * Returns:
3893 * DD_OK on success
3894 * DDERR_INVALIDPARAMS if DDSD is NULL
3896 *****************************************************************************/
3897 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
3899 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
3900 enum wined3d_format_id newFormat = WINED3DFMT_UNKNOWN;
3901 HRESULT hr;
3903 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
3905 if(!DDSD)
3906 return DDERR_INVALIDPARAMS;
3908 EnterCriticalSection(&ddraw_cs);
3909 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
3911 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
3913 if(newFormat == WINED3DFMT_UNKNOWN)
3915 ERR("Requested to set an unknown pixelformat\n");
3916 LeaveCriticalSection(&ddraw_cs);
3917 return DDERR_INVALIDPARAMS;
3919 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
3921 hr = wined3d_surface_set_format(This->wined3d_surface, newFormat);
3922 if (FAILED(hr))
3924 LeaveCriticalSection(&ddraw_cs);
3925 return hr;
3929 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
3931 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTOVERLAY,
3932 (WINEDDCOLORKEY *)&DDSD->u3.ddckCKDestOverlay);
3934 if (DDSD->dwFlags & DDSD_CKDESTBLT)
3936 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTBLT,
3937 (WINEDDCOLORKEY *)&DDSD->ddckCKDestBlt);
3939 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
3941 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCOVERLAY,
3942 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcOverlay);
3944 if (DDSD->dwFlags & DDSD_CKSRCBLT)
3946 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCBLT,
3947 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcBlt);
3949 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
3951 hr = wined3d_surface_set_mem(This->wined3d_surface, DDSD->lpSurface);
3952 if (FAILED(hr))
3954 /* No need for a trace here, wined3d does that for us */
3955 switch(hr)
3957 case WINED3DERR_INVALIDCALL:
3958 LeaveCriticalSection(&ddraw_cs);
3959 return DDERR_INVALIDPARAMS;
3960 default:
3961 break; /* Go on */
3966 This->surface_desc = *DDSD;
3968 LeaveCriticalSection(&ddraw_cs);
3969 return DD_OK;
3972 static HRESULT WINAPI ddraw_surface4_SetSurfaceDesc(IDirectDrawSurface4 *iface,
3973 DDSURFACEDESC2 *surface_desc, DWORD flags)
3975 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
3976 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
3978 return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
3979 surface_desc, flags);
3982 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
3983 DDSURFACEDESC *surface_desc, DWORD flags)
3985 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
3986 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
3988 return ddraw_surface7_SetSurfaceDesc(&This->IDirectDrawSurface7_iface,
3989 (DDSURFACEDESC2 *)surface_desc, flags);
3992 /*****************************************************************************
3993 * IDirectDrawSurface7::GetPalette
3995 * Returns the IDirectDrawPalette interface of the palette currently assigned
3996 * to the surface
3998 * Params:
3999 * Pal: Address to write the interface pointer to
4001 * Returns:
4002 * DD_OK on success
4003 * DDERR_INVALIDPARAMS if Pal is NULL
4005 *****************************************************************************/
4006 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
4008 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4009 struct wined3d_palette *wined3d_palette;
4010 HRESULT hr = DD_OK;
4012 TRACE("iface %p, palette %p.\n", iface, Pal);
4014 if(!Pal)
4015 return DDERR_INVALIDPARAMS;
4017 EnterCriticalSection(&ddraw_cs);
4018 wined3d_palette = wined3d_surface_get_palette(This->wined3d_surface);
4019 if (wined3d_palette)
4021 *Pal = wined3d_palette_get_parent(wined3d_palette);
4022 IDirectDrawPalette_AddRef(*Pal);
4024 else
4026 *Pal = NULL;
4027 hr = DDERR_NOPALETTEATTACHED;
4030 LeaveCriticalSection(&ddraw_cs);
4031 return hr;
4034 static HRESULT WINAPI ddraw_surface4_GetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette **palette)
4036 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4037 TRACE("iface %p, palette %p.\n", iface, palette);
4039 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4042 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
4044 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4045 TRACE("iface %p, palette %p.\n", iface, palette);
4047 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4050 static HRESULT WINAPI ddraw_surface2_GetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette **palette)
4052 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4053 TRACE("iface %p, palette %p.\n", iface, palette);
4055 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4058 static HRESULT WINAPI ddraw_surface1_GetPalette(IDirectDrawSurface *iface, IDirectDrawPalette **palette)
4060 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4061 TRACE("iface %p, palette %p.\n", iface, palette);
4063 return ddraw_surface7_GetPalette(&This->IDirectDrawSurface7_iface, palette);
4066 /*****************************************************************************
4067 * SetColorKeyEnum
4069 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
4070 * recursively in the surface tree
4072 *****************************************************************************/
4073 struct SCKContext
4075 HRESULT ret;
4076 WINEDDCOLORKEY *CKey;
4077 DWORD Flags;
4080 static HRESULT WINAPI
4081 SetColorKeyEnum(IDirectDrawSurface7 *surface,
4082 DDSURFACEDESC2 *desc,
4083 void *context)
4085 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(surface);
4086 struct SCKContext *ctx = context;
4087 HRESULT hr;
4089 hr = wined3d_surface_set_color_key(This->wined3d_surface, ctx->Flags, ctx->CKey);
4090 if (FAILED(hr))
4092 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
4093 ctx->ret = hr;
4096 ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
4097 ddraw_surface7_Release(surface);
4099 return DDENUMRET_OK;
4102 /*****************************************************************************
4103 * IDirectDrawSurface7::SetColorKey
4105 * Sets the color keying options for the surface. Observations showed that
4106 * in case of complex surfaces the color key has to be assigned to all
4107 * sublevels.
4109 * Params:
4110 * Flags: DDCKEY_*
4111 * CKey: The new color key
4113 * Returns:
4114 * DD_OK on success
4115 * See IWineD3DSurface::SetColorKey for details
4117 *****************************************************************************/
4118 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
4120 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4121 DDCOLORKEY FixedCKey;
4122 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
4124 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
4126 EnterCriticalSection(&ddraw_cs);
4127 if (CKey)
4129 FixedCKey = *CKey;
4130 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
4131 if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
4132 FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
4134 switch (Flags & ~DDCKEY_COLORSPACE)
4136 case DDCKEY_DESTBLT:
4137 This->surface_desc.ddckCKDestBlt = FixedCKey;
4138 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
4139 break;
4141 case DDCKEY_DESTOVERLAY:
4142 This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
4143 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
4144 break;
4146 case DDCKEY_SRCOVERLAY:
4147 This->surface_desc.ddckCKSrcOverlay = FixedCKey;
4148 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
4149 break;
4151 case DDCKEY_SRCBLT:
4152 This->surface_desc.ddckCKSrcBlt = FixedCKey;
4153 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
4154 break;
4156 default:
4157 LeaveCriticalSection(&ddraw_cs);
4158 return DDERR_INVALIDPARAMS;
4161 else
4163 switch (Flags & ~DDCKEY_COLORSPACE)
4165 case DDCKEY_DESTBLT:
4166 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
4167 break;
4169 case DDCKEY_DESTOVERLAY:
4170 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
4171 break;
4173 case DDCKEY_SRCOVERLAY:
4174 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
4175 break;
4177 case DDCKEY_SRCBLT:
4178 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
4179 break;
4181 default:
4182 LeaveCriticalSection(&ddraw_cs);
4183 return DDERR_INVALIDPARAMS;
4186 ctx.ret = wined3d_surface_set_color_key(This->wined3d_surface, Flags, ctx.CKey);
4187 ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
4188 LeaveCriticalSection(&ddraw_cs);
4189 switch(ctx.ret)
4191 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
4192 default: return ctx.ret;
4196 static HRESULT WINAPI ddraw_surface4_SetColorKey(IDirectDrawSurface4 *iface, DWORD flags, DDCOLORKEY *color_key)
4198 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4199 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4201 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4204 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
4206 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4207 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4209 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4212 static HRESULT WINAPI ddraw_surface2_SetColorKey(IDirectDrawSurface2 *iface, DWORD flags, DDCOLORKEY *color_key)
4214 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4215 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4217 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4220 static HRESULT WINAPI ddraw_surface1_SetColorKey(IDirectDrawSurface *iface, DWORD flags, DDCOLORKEY *color_key)
4222 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4223 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
4225 return ddraw_surface7_SetColorKey(&This->IDirectDrawSurface7_iface, flags, color_key);
4228 /*****************************************************************************
4229 * IDirectDrawSurface7::SetPalette
4231 * Assigns a DirectDrawPalette object to the surface
4233 * Params:
4234 * Pal: Interface to the palette to set
4236 * Returns:
4237 * DD_OK on success
4239 *****************************************************************************/
4240 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
4242 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface7(iface);
4243 IDirectDrawPalette *oldPal;
4244 IDirectDrawSurfaceImpl *surf;
4245 IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
4246 HRESULT hr;
4248 TRACE("iface %p, palette %p.\n", iface, Pal);
4250 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
4251 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
4252 return DDERR_INVALIDPIXELFORMAT;
4255 /* Find the old palette */
4256 EnterCriticalSection(&ddraw_cs);
4257 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
4258 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
4260 LeaveCriticalSection(&ddraw_cs);
4261 return hr;
4263 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
4265 /* Set the new Palette */
4266 wined3d_surface_set_palette(This->wined3d_surface, PalImpl ? PalImpl->wineD3DPalette : NULL);
4267 /* AddRef the Palette */
4268 if(Pal) IDirectDrawPalette_AddRef(Pal);
4270 /* Release the old palette */
4271 if(oldPal) IDirectDrawPalette_Release(oldPal);
4273 /* If this is a front buffer, also update the back buffers
4274 * TODO: How do things work for palettized cube textures?
4276 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
4278 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
4279 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
4281 surf = This;
4282 while(1)
4284 IDirectDrawSurface7 *attach;
4285 HRESULT hr;
4286 hr = ddraw_surface7_GetAttachedSurface(&surf->IDirectDrawSurface7_iface, &caps2, &attach);
4287 if(hr != DD_OK)
4289 break;
4292 TRACE("Setting palette on %p\n", attach);
4293 ddraw_surface7_SetPalette(attach, Pal);
4294 surf = impl_from_IDirectDrawSurface7(attach);
4295 ddraw_surface7_Release(attach);
4299 LeaveCriticalSection(&ddraw_cs);
4300 return DD_OK;
4303 static HRESULT WINAPI ddraw_surface4_SetPalette(IDirectDrawSurface4 *iface, IDirectDrawPalette *palette)
4305 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface4(iface);
4306 TRACE("iface %p, palette %p.\n", iface, palette);
4308 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4311 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
4313 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
4314 TRACE("iface %p, palette %p.\n", iface, palette);
4316 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4319 static HRESULT WINAPI ddraw_surface2_SetPalette(IDirectDrawSurface2 *iface, IDirectDrawPalette *palette)
4321 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface2(iface);
4322 TRACE("iface %p, palette %p.\n", iface, palette);
4324 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4327 static HRESULT WINAPI ddraw_surface1_SetPalette(IDirectDrawSurface *iface, IDirectDrawPalette *palette)
4329 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface(iface);
4330 TRACE("iface %p, palette %p.\n", iface, palette);
4332 return ddraw_surface7_SetPalette(&This->IDirectDrawSurface7_iface, palette);
4335 /**********************************************************
4336 * IDirectDrawGammaControl::GetGammaRamp
4338 * Returns the current gamma ramp for a surface
4340 * Params:
4341 * flags: Ignored
4342 * gamma_ramp: Address to write the ramp to
4344 * Returns:
4345 * DD_OK on success
4346 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4348 **********************************************************/
4349 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
4350 DWORD flags, DDGAMMARAMP *gamma_ramp)
4352 IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4354 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4356 if (!gamma_ramp)
4358 WARN("Invalid gamma_ramp passed.\n");
4359 return DDERR_INVALIDPARAMS;
4362 EnterCriticalSection(&ddraw_cs);
4363 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4365 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP. */
4366 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (WINED3DGAMMARAMP *)gamma_ramp);
4368 else
4370 ERR("Not implemented for non-primary surfaces.\n");
4372 LeaveCriticalSection(&ddraw_cs);
4374 return DD_OK;
4377 /**********************************************************
4378 * IDirectDrawGammaControl::SetGammaRamp
4380 * Sets the red, green and blue gamma ramps for
4382 * Params:
4383 * flags: Can be DDSGR_CALIBRATE to request calibration
4384 * gamma_ramp: Structure containing the new gamma ramp
4386 * Returns:
4387 * DD_OK on success
4388 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
4390 **********************************************************/
4391 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
4392 DWORD flags, DDGAMMARAMP *gamma_ramp)
4394 IDirectDrawSurfaceImpl *surface = impl_from_IDirectDrawGammaControl(iface);
4396 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
4398 if (!gamma_ramp)
4400 WARN("Invalid gamma_ramp passed.\n");
4401 return DDERR_INVALIDPARAMS;
4404 EnterCriticalSection(&ddraw_cs);
4405 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
4407 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP */
4408 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device, 0, flags, (WINED3DGAMMARAMP *)gamma_ramp);
4410 else
4412 ERR("Not implemented for non-primary surfaces.\n");
4414 LeaveCriticalSection(&ddraw_cs);
4416 return DD_OK;
4419 /*****************************************************************************
4420 * IDirect3DTexture2::PaletteChanged
4422 * Informs the texture about a palette change
4424 * Params:
4425 * start: Start index of the change
4426 * count: The number of changed entries
4428 * Returns
4429 * D3D_OK, because it's a stub
4431 *****************************************************************************/
4432 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
4434 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
4436 return D3D_OK;
4439 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
4441 IDirectDrawSurfaceImpl *surface = surface_from_texture1(iface);
4443 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
4445 return d3d_texture2_PaletteChanged((IDirect3DTexture2 *)&surface->IDirect3DTexture2_vtbl, start, count);
4448 /*****************************************************************************
4449 * IDirect3DTexture::Unload
4451 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
4454 * Returns:
4455 * DDERR_UNSUPPORTED
4457 *****************************************************************************/
4458 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
4460 WARN("iface %p. Not implemented.\n", iface);
4462 return DDERR_UNSUPPORTED;
4465 /*****************************************************************************
4466 * IDirect3DTexture2::GetHandle
4468 * Returns handle for the texture. At the moment, the interface
4469 * to the IWineD3DTexture is used.
4471 * Params:
4472 * device: Device this handle is assigned to
4473 * handle: Address to store the handle at.
4475 * Returns:
4476 * D3D_OK
4478 *****************************************************************************/
4479 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
4480 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
4482 IDirectDrawSurfaceImpl *surface = surface_from_texture2(iface);
4484 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4486 EnterCriticalSection(&ddraw_cs);
4488 if (!surface->Handle)
4490 DWORD h = ddraw_allocate_handle(&device_from_device2(device)->handle_table, surface, DDRAW_HANDLE_SURFACE);
4491 if (h == DDRAW_INVALID_HANDLE)
4493 ERR("Failed to allocate a texture handle.\n");
4494 LeaveCriticalSection(&ddraw_cs);
4495 return DDERR_OUTOFMEMORY;
4498 surface->Handle = h + 1;
4501 TRACE("Returning handle %08x.\n", surface->Handle);
4502 *handle = surface->Handle;
4504 LeaveCriticalSection(&ddraw_cs);
4506 return D3D_OK;
4509 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
4510 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
4512 IDirect3DTexture2 *texture2 = (IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl;
4513 IDirect3DDevice2 *device2 = (IDirect3DDevice2 *)&device_from_device1(device)->IDirect3DDevice2_vtbl;
4515 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
4517 return d3d_texture2_GetHandle(texture2, device2, handle);
4520 /*****************************************************************************
4521 * get_sub_mimaplevel
4523 * Helper function that returns the next mipmap level
4525 * tex_ptr: Surface of which to return the next level
4527 *****************************************************************************/
4528 static IDirectDrawSurfaceImpl *get_sub_mimaplevel(IDirectDrawSurfaceImpl *surface)
4530 /* Now go down the mipmap chain to the next surface */
4531 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
4532 IDirectDrawSurface7 *next_level;
4533 HRESULT hr;
4535 hr = ddraw_surface7_GetAttachedSurface(&surface->IDirectDrawSurface7_iface, &mipmap_caps, &next_level);
4536 if (FAILED(hr)) return NULL;
4538 ddraw_surface7_Release(next_level);
4540 return impl_from_IDirectDrawSurface7(next_level);
4543 /*****************************************************************************
4544 * IDirect3DTexture2::Load
4546 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
4548 * This function isn't relayed to WineD3D because the whole interface is
4549 * implemented in DDraw only. For speed improvements a implementation which
4550 * takes OpenGL more into account could be placed into WineD3D.
4552 * Params:
4553 * src_texture: Address of the texture to load
4555 * Returns:
4556 * D3D_OK on success
4557 * D3DERR_TEXTURE_LOAD_FAILED.
4559 *****************************************************************************/
4560 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
4562 IDirectDrawSurfaceImpl *dst_surface = surface_from_texture2(iface);
4563 IDirectDrawSurfaceImpl *src_surface = surface_from_texture2(src_texture);
4564 HRESULT hr;
4566 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4568 if (src_surface == dst_surface)
4570 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
4571 return D3D_OK;
4574 EnterCriticalSection(&ddraw_cs);
4576 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4577 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
4578 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
4580 ERR("Trying to load surfaces with different mip-map counts.\n");
4583 for (;;)
4585 struct wined3d_palette *wined3d_dst_pal, *wined3d_src_pal;
4586 IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
4587 DDSURFACEDESC *src_desc, *dst_desc;
4589 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
4590 src_surface, dst_surface, src_surface->mipmap_level);
4592 /* Suppress the ALLOCONLOAD flag */
4593 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
4595 /* Get the palettes */
4596 wined3d_dst_pal = wined3d_surface_get_palette(dst_surface->wined3d_surface);
4597 if (wined3d_dst_pal)
4598 dst_pal = wined3d_palette_get_parent(wined3d_dst_pal);
4600 wined3d_src_pal = wined3d_surface_get_palette(src_surface->wined3d_surface);
4601 if (wined3d_src_pal)
4602 src_pal = wined3d_palette_get_parent(wined3d_src_pal);
4604 if (src_pal)
4606 PALETTEENTRY palent[256];
4608 if (!dst_pal)
4610 LeaveCriticalSection(&ddraw_cs);
4611 return DDERR_NOPALETTEATTACHED;
4613 IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
4614 IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
4617 /* Copy one surface on the other */
4618 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
4619 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
4621 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
4623 /* Should also check for same pixel format, u1.lPitch, ... */
4624 ERR("Error in surface sizes.\n");
4625 LeaveCriticalSection(&ddraw_cs);
4626 return D3DERR_TEXTURE_LOAD_FAILED;
4628 else
4630 WINED3DLOCKED_RECT src_rect, dst_rect;
4632 /* Copy also the ColorKeying stuff */
4633 if (src_desc->dwFlags & DDSD_CKSRCBLT)
4635 dst_desc->dwFlags |= DDSD_CKSRCBLT;
4636 dst_desc->ddckCKSrcBlt.dwColorSpaceLowValue = src_desc->ddckCKSrcBlt.dwColorSpaceLowValue;
4637 dst_desc->ddckCKSrcBlt.dwColorSpaceHighValue = src_desc->ddckCKSrcBlt.dwColorSpaceHighValue;
4640 /* Copy the main memory texture into the surface that corresponds
4641 * to the OpenGL texture object. */
4643 hr = wined3d_surface_map(src_surface->wined3d_surface, &src_rect, NULL, 0);
4644 if (FAILED(hr))
4646 ERR("Failed to lock source surface, hr %#x.\n", hr);
4647 LeaveCriticalSection(&ddraw_cs);
4648 return D3DERR_TEXTURE_LOAD_FAILED;
4651 hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_rect, NULL, 0);
4652 if (FAILED(hr))
4654 ERR("Failed to lock destination surface, hr %#x.\n", hr);
4655 wined3d_surface_unmap(src_surface->wined3d_surface);
4656 LeaveCriticalSection(&ddraw_cs);
4657 return D3DERR_TEXTURE_LOAD_FAILED;
4660 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
4661 memcpy(dst_rect.pBits, src_rect.pBits, src_surface->surface_desc.u1.dwLinearSize);
4662 else
4663 memcpy(dst_rect.pBits, src_rect.pBits, src_rect.Pitch * src_desc->dwHeight);
4665 wined3d_surface_unmap(src_surface->wined3d_surface);
4666 wined3d_surface_unmap(dst_surface->wined3d_surface);
4669 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4670 src_surface = get_sub_mimaplevel(src_surface);
4671 else
4672 src_surface = NULL;
4674 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
4675 dst_surface = get_sub_mimaplevel(dst_surface);
4676 else
4677 dst_surface = NULL;
4679 if (!src_surface || !dst_surface)
4681 if (src_surface != dst_surface)
4682 ERR("Loading surface with different mipmap structure.\n");
4683 break;
4687 LeaveCriticalSection(&ddraw_cs);
4689 return hr;
4692 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
4694 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
4696 return d3d_texture2_Load((IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl,
4697 src_texture ? (IDirect3DTexture2 *)&surface_from_texture1(src_texture)->IDirect3DTexture2_vtbl : NULL);
4700 /*****************************************************************************
4701 * The VTable
4702 *****************************************************************************/
4704 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
4706 /* IUnknown */
4707 ddraw_surface7_QueryInterface,
4708 ddraw_surface7_AddRef,
4709 ddraw_surface7_Release,
4710 /* IDirectDrawSurface */
4711 ddraw_surface7_AddAttachedSurface,
4712 ddraw_surface7_AddOverlayDirtyRect,
4713 ddraw_surface7_Blt,
4714 ddraw_surface7_BltBatch,
4715 ddraw_surface7_BltFast,
4716 ddraw_surface7_DeleteAttachedSurface,
4717 ddraw_surface7_EnumAttachedSurfaces,
4718 ddraw_surface7_EnumOverlayZOrders,
4719 ddraw_surface7_Flip,
4720 ddraw_surface7_GetAttachedSurface,
4721 ddraw_surface7_GetBltStatus,
4722 ddraw_surface7_GetCaps,
4723 ddraw_surface7_GetClipper,
4724 ddraw_surface7_GetColorKey,
4725 ddraw_surface7_GetDC,
4726 ddraw_surface7_GetFlipStatus,
4727 ddraw_surface7_GetOverlayPosition,
4728 ddraw_surface7_GetPalette,
4729 ddraw_surface7_GetPixelFormat,
4730 ddraw_surface7_GetSurfaceDesc,
4731 ddraw_surface7_Initialize,
4732 ddraw_surface7_IsLost,
4733 ddraw_surface7_Lock,
4734 ddraw_surface7_ReleaseDC,
4735 ddraw_surface7_Restore,
4736 ddraw_surface7_SetClipper,
4737 ddraw_surface7_SetColorKey,
4738 ddraw_surface7_SetOverlayPosition,
4739 ddraw_surface7_SetPalette,
4740 ddraw_surface7_Unlock,
4741 ddraw_surface7_UpdateOverlay,
4742 ddraw_surface7_UpdateOverlayDisplay,
4743 ddraw_surface7_UpdateOverlayZOrder,
4744 /* IDirectDrawSurface2 */
4745 ddraw_surface7_GetDDInterface,
4746 ddraw_surface7_PageLock,
4747 ddraw_surface7_PageUnlock,
4748 /* IDirectDrawSurface3 */
4749 ddraw_surface7_SetSurfaceDesc,
4750 /* IDirectDrawSurface4 */
4751 ddraw_surface7_SetPrivateData,
4752 ddraw_surface7_GetPrivateData,
4753 ddraw_surface7_FreePrivateData,
4754 ddraw_surface7_GetUniquenessValue,
4755 ddraw_surface7_ChangeUniquenessValue,
4756 /* IDirectDrawSurface7 */
4757 ddraw_surface7_SetPriority,
4758 ddraw_surface7_GetPriority,
4759 ddraw_surface7_SetLOD,
4760 ddraw_surface7_GetLOD,
4763 static const struct IDirectDrawSurface4Vtbl ddraw_surface4_vtbl =
4765 /* IUnknown */
4766 ddraw_surface4_QueryInterface,
4767 ddraw_surface4_AddRef,
4768 ddraw_surface4_Release,
4769 /* IDirectDrawSurface */
4770 ddraw_surface4_AddAttachedSurface,
4771 ddraw_surface4_AddOverlayDirtyRect,
4772 ddraw_surface4_Blt,
4773 ddraw_surface4_BltBatch,
4774 ddraw_surface4_BltFast,
4775 ddraw_surface4_DeleteAttachedSurface,
4776 ddraw_surface4_EnumAttachedSurfaces,
4777 ddraw_surface4_EnumOverlayZOrders,
4778 ddraw_surface4_Flip,
4779 ddraw_surface4_GetAttachedSurface,
4780 ddraw_surface4_GetBltStatus,
4781 ddraw_surface4_GetCaps,
4782 ddraw_surface4_GetClipper,
4783 ddraw_surface4_GetColorKey,
4784 ddraw_surface4_GetDC,
4785 ddraw_surface4_GetFlipStatus,
4786 ddraw_surface4_GetOverlayPosition,
4787 ddraw_surface4_GetPalette,
4788 ddraw_surface4_GetPixelFormat,
4789 ddraw_surface4_GetSurfaceDesc,
4790 ddraw_surface4_Initialize,
4791 ddraw_surface4_IsLost,
4792 ddraw_surface4_Lock,
4793 ddraw_surface4_ReleaseDC,
4794 ddraw_surface4_Restore,
4795 ddraw_surface4_SetClipper,
4796 ddraw_surface4_SetColorKey,
4797 ddraw_surface4_SetOverlayPosition,
4798 ddraw_surface4_SetPalette,
4799 ddraw_surface4_Unlock,
4800 ddraw_surface4_UpdateOverlay,
4801 ddraw_surface4_UpdateOverlayDisplay,
4802 ddraw_surface4_UpdateOverlayZOrder,
4803 /* IDirectDrawSurface2 */
4804 ddraw_surface4_GetDDInterface,
4805 ddraw_surface4_PageLock,
4806 ddraw_surface4_PageUnlock,
4807 /* IDirectDrawSurface3 */
4808 ddraw_surface4_SetSurfaceDesc,
4809 /* IDirectDrawSurface4 */
4810 ddraw_surface4_SetPrivateData,
4811 ddraw_surface4_GetPrivateData,
4812 ddraw_surface4_FreePrivateData,
4813 ddraw_surface4_GetUniquenessValue,
4814 ddraw_surface4_ChangeUniquenessValue,
4817 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
4819 /* IUnknown */
4820 ddraw_surface3_QueryInterface,
4821 ddraw_surface3_AddRef,
4822 ddraw_surface3_Release,
4823 /* IDirectDrawSurface */
4824 ddraw_surface3_AddAttachedSurface,
4825 ddraw_surface3_AddOverlayDirtyRect,
4826 ddraw_surface3_Blt,
4827 ddraw_surface3_BltBatch,
4828 ddraw_surface3_BltFast,
4829 ddraw_surface3_DeleteAttachedSurface,
4830 ddraw_surface3_EnumAttachedSurfaces,
4831 ddraw_surface3_EnumOverlayZOrders,
4832 ddraw_surface3_Flip,
4833 ddraw_surface3_GetAttachedSurface,
4834 ddraw_surface3_GetBltStatus,
4835 ddraw_surface3_GetCaps,
4836 ddraw_surface3_GetClipper,
4837 ddraw_surface3_GetColorKey,
4838 ddraw_surface3_GetDC,
4839 ddraw_surface3_GetFlipStatus,
4840 ddraw_surface3_GetOverlayPosition,
4841 ddraw_surface3_GetPalette,
4842 ddraw_surface3_GetPixelFormat,
4843 ddraw_surface3_GetSurfaceDesc,
4844 ddraw_surface3_Initialize,
4845 ddraw_surface3_IsLost,
4846 ddraw_surface3_Lock,
4847 ddraw_surface3_ReleaseDC,
4848 ddraw_surface3_Restore,
4849 ddraw_surface3_SetClipper,
4850 ddraw_surface3_SetColorKey,
4851 ddraw_surface3_SetOverlayPosition,
4852 ddraw_surface3_SetPalette,
4853 ddraw_surface3_Unlock,
4854 ddraw_surface3_UpdateOverlay,
4855 ddraw_surface3_UpdateOverlayDisplay,
4856 ddraw_surface3_UpdateOverlayZOrder,
4857 /* IDirectDrawSurface2 */
4858 ddraw_surface3_GetDDInterface,
4859 ddraw_surface3_PageLock,
4860 ddraw_surface3_PageUnlock,
4861 /* IDirectDrawSurface3 */
4862 ddraw_surface3_SetSurfaceDesc,
4865 static const struct IDirectDrawSurface2Vtbl ddraw_surface2_vtbl =
4867 /* IUnknown */
4868 ddraw_surface2_QueryInterface,
4869 ddraw_surface2_AddRef,
4870 ddraw_surface2_Release,
4871 /* IDirectDrawSurface */
4872 ddraw_surface2_AddAttachedSurface,
4873 ddraw_surface2_AddOverlayDirtyRect,
4874 ddraw_surface2_Blt,
4875 ddraw_surface2_BltBatch,
4876 ddraw_surface2_BltFast,
4877 ddraw_surface2_DeleteAttachedSurface,
4878 ddraw_surface2_EnumAttachedSurfaces,
4879 ddraw_surface2_EnumOverlayZOrders,
4880 ddraw_surface2_Flip,
4881 ddraw_surface2_GetAttachedSurface,
4882 ddraw_surface2_GetBltStatus,
4883 ddraw_surface2_GetCaps,
4884 ddraw_surface2_GetClipper,
4885 ddraw_surface2_GetColorKey,
4886 ddraw_surface2_GetDC,
4887 ddraw_surface2_GetFlipStatus,
4888 ddraw_surface2_GetOverlayPosition,
4889 ddraw_surface2_GetPalette,
4890 ddraw_surface2_GetPixelFormat,
4891 ddraw_surface2_GetSurfaceDesc,
4892 ddraw_surface2_Initialize,
4893 ddraw_surface2_IsLost,
4894 ddraw_surface2_Lock,
4895 ddraw_surface2_ReleaseDC,
4896 ddraw_surface2_Restore,
4897 ddraw_surface2_SetClipper,
4898 ddraw_surface2_SetColorKey,
4899 ddraw_surface2_SetOverlayPosition,
4900 ddraw_surface2_SetPalette,
4901 ddraw_surface2_Unlock,
4902 ddraw_surface2_UpdateOverlay,
4903 ddraw_surface2_UpdateOverlayDisplay,
4904 ddraw_surface2_UpdateOverlayZOrder,
4905 /* IDirectDrawSurface2 */
4906 ddraw_surface2_GetDDInterface,
4907 ddraw_surface2_PageLock,
4908 ddraw_surface2_PageUnlock,
4911 static const struct IDirectDrawSurfaceVtbl ddraw_surface1_vtbl =
4913 /* IUnknown */
4914 ddraw_surface1_QueryInterface,
4915 ddraw_surface1_AddRef,
4916 ddraw_surface1_Release,
4917 /* IDirectDrawSurface */
4918 ddraw_surface1_AddAttachedSurface,
4919 ddraw_surface1_AddOverlayDirtyRect,
4920 ddraw_surface1_Blt,
4921 ddraw_surface1_BltBatch,
4922 ddraw_surface1_BltFast,
4923 ddraw_surface1_DeleteAttachedSurface,
4924 ddraw_surface1_EnumAttachedSurfaces,
4925 ddraw_surface1_EnumOverlayZOrders,
4926 ddraw_surface1_Flip,
4927 ddraw_surface1_GetAttachedSurface,
4928 ddraw_surface1_GetBltStatus,
4929 ddraw_surface1_GetCaps,
4930 ddraw_surface1_GetClipper,
4931 ddraw_surface1_GetColorKey,
4932 ddraw_surface1_GetDC,
4933 ddraw_surface1_GetFlipStatus,
4934 ddraw_surface1_GetOverlayPosition,
4935 ddraw_surface1_GetPalette,
4936 ddraw_surface1_GetPixelFormat,
4937 ddraw_surface1_GetSurfaceDesc,
4938 ddraw_surface1_Initialize,
4939 ddraw_surface1_IsLost,
4940 ddraw_surface1_Lock,
4941 ddraw_surface1_ReleaseDC,
4942 ddraw_surface1_Restore,
4943 ddraw_surface1_SetClipper,
4944 ddraw_surface1_SetColorKey,
4945 ddraw_surface1_SetOverlayPosition,
4946 ddraw_surface1_SetPalette,
4947 ddraw_surface1_Unlock,
4948 ddraw_surface1_UpdateOverlay,
4949 ddraw_surface1_UpdateOverlayDisplay,
4950 ddraw_surface1_UpdateOverlayZOrder,
4953 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
4955 ddraw_gamma_control_QueryInterface,
4956 ddraw_gamma_control_AddRef,
4957 ddraw_gamma_control_Release,
4958 ddraw_gamma_control_GetGammaRamp,
4959 ddraw_gamma_control_SetGammaRamp,
4962 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
4964 d3d_texture2_QueryInterface,
4965 d3d_texture2_AddRef,
4966 d3d_texture2_Release,
4967 d3d_texture2_GetHandle,
4968 d3d_texture2_PaletteChanged,
4969 d3d_texture2_Load,
4972 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
4974 d3d_texture1_QueryInterface,
4975 d3d_texture1_AddRef,
4976 d3d_texture1_Release,
4977 d3d_texture1_Initialize,
4978 d3d_texture1_GetHandle,
4979 d3d_texture1_PaletteChanged,
4980 d3d_texture1_Load,
4981 d3d_texture1_Unload,
4984 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface7(IDirectDrawSurface7 *iface)
4986 if (!iface) return NULL;
4987 assert(iface->lpVtbl == &ddraw_surface7_vtbl);
4988 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface7_iface);
4991 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface4(IDirectDrawSurface4 *iface)
4993 if (!iface) return NULL;
4994 assert(iface->lpVtbl == &ddraw_surface4_vtbl);
4995 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface4_iface);
4998 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
5000 if (!iface) return NULL;
5001 assert(iface->lpVtbl == &ddraw_surface3_vtbl);
5002 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface3_iface);
5005 static IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface2(IDirectDrawSurface2 *iface)
5007 if (!iface) return NULL;
5008 assert(iface->lpVtbl == &ddraw_surface2_vtbl);
5009 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface2_iface);
5012 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface(IDirectDrawSurface *iface)
5014 if (!iface) return NULL;
5015 assert(iface->lpVtbl == &ddraw_surface1_vtbl);
5016 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface_iface);
5019 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
5021 IDirectDrawSurfaceImpl *surface = parent;
5023 TRACE("surface %p.\n", surface);
5025 /* Check for attached surfaces and detach them. */
5026 if (surface->first_attached != surface)
5028 IDirectDrawSurface7 *root = &surface->first_attached->IDirectDrawSurface7_iface;
5029 IDirectDrawSurface7 *detach = &surface->IDirectDrawSurface7_iface;
5031 /* Well, this shouldn't happen: The surface being attached is
5032 * referenced in AddAttachedSurface(), so it shouldn't be released
5033 * until DeleteAttachedSurface() is called, because the refcount is
5034 * held. It looks like the application released it often enough to
5035 * force this. */
5036 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
5038 /* The refcount will drop to -1 here */
5039 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
5040 ERR("DeleteAttachedSurface failed.\n");
5043 while (surface->next_attached)
5045 IDirectDrawSurface7 *root = &surface->IDirectDrawSurface7_iface;
5046 IDirectDrawSurface7 *detach = &surface->next_attached->IDirectDrawSurface7_iface;
5048 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
5049 ERR("DeleteAttachedSurface failed.\n");
5052 /* Having a texture handle set implies that the device still exists. */
5053 if (surface->Handle)
5054 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
5056 /* Reduce the ddraw surface count. */
5057 InterlockedDecrement(&surface->ddraw->surfaces);
5058 list_remove(&surface->surface_list_entry);
5060 HeapFree(GetProcessHeap(), 0, surface);
5063 const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
5065 ddraw_surface_wined3d_object_destroyed,
5068 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
5070 IDirectDrawSurfaceImpl *surface = parent;
5072 TRACE("surface %p.\n", surface);
5074 ddraw_surface_cleanup(surface);
5077 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
5079 ddraw_texture_wined3d_object_destroyed,
5082 HRESULT ddraw_surface_create_texture(IDirectDrawSurfaceImpl *surface)
5084 const DDSURFACEDESC2 *desc = &surface->surface_desc;
5085 enum wined3d_format_id format;
5086 WINED3DPOOL pool;
5087 UINT levels;
5089 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
5090 levels = desc->u2.dwMipMapCount;
5091 else
5092 levels = 1;
5094 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM.
5095 * Should I forward the MANAGED cap to the managed pool? */
5096 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5097 pool = WINED3DPOOL_SYSTEMMEM;
5098 else
5099 pool = WINED3DPOOL_DEFAULT;
5101 format = PixelFormat_DD2WineD3D(&surface->surface_desc.u4.ddpfPixelFormat);
5102 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
5103 return wined3d_texture_create_cube(surface->ddraw->wined3d_device, desc->dwWidth,
5104 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5105 else
5106 return wined3d_texture_create_2d(surface->ddraw->wined3d_device, desc->dwWidth, desc->dwHeight,
5107 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
5110 HRESULT ddraw_surface_init(IDirectDrawSurfaceImpl *surface, IDirectDrawImpl *ddraw,
5111 DDSURFACEDESC2 *desc, UINT mip_level, WINED3DSURFTYPE surface_type, UINT version)
5113 struct wined3d_resource_desc wined3d_desc;
5114 struct wined3d_resource *wined3d_resource;
5115 WINED3DPOOL pool = WINED3DPOOL_DEFAULT;
5116 enum wined3d_format_id format;
5117 DWORD usage = 0;
5118 HRESULT hr;
5120 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
5121 && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
5122 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
5124 /* Tests show surfaces without memory flags get these flags added
5125 * right after creation. */
5126 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
5129 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
5131 usage |= WINED3DUSAGE_RENDERTARGET;
5132 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
5135 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && !(desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
5137 usage |= WINED3DUSAGE_RENDERTARGET;
5140 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
5142 usage |= WINED3DUSAGE_OVERLAY;
5145 if (ddraw->depthstencil || (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
5147 /* The depth stencil creation callback sets this flag. Set the
5148 * wined3d usage to let it know it's a depth/stencil surface. */
5149 usage |= WINED3DUSAGE_DEPTHSTENCIL;
5152 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
5154 pool = WINED3DPOOL_SYSTEMMEM;
5156 else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
5158 pool = WINED3DPOOL_MANAGED;
5159 /* Managed textures have the system memory flag set. */
5160 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
5162 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5164 /* Videomemory adds localvidmem. This is mutually exclusive with
5165 * systemmemory and texturemanage. */
5166 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
5169 format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
5170 if (format == WINED3DFMT_UNKNOWN)
5172 WARN("Unsupported / unknown pixelformat.\n");
5173 return DDERR_INVALIDPIXELFORMAT;
5176 surface->IDirectDrawSurface7_iface.lpVtbl = &ddraw_surface7_vtbl;
5177 surface->IDirectDrawSurface4_iface.lpVtbl = &ddraw_surface4_vtbl;
5178 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
5179 surface->IDirectDrawSurface2_iface.lpVtbl = &ddraw_surface2_vtbl;
5180 surface->IDirectDrawSurface_iface.lpVtbl = &ddraw_surface1_vtbl;
5181 surface->IDirectDrawGammaControl_iface.lpVtbl = &ddraw_gamma_control_vtbl;
5182 surface->IDirect3DTexture2_vtbl = &d3d_texture2_vtbl;
5183 surface->IDirect3DTexture_vtbl = &d3d_texture1_vtbl;
5184 surface->iface_count = 1;
5185 surface->version = version;
5186 surface->ddraw = ddraw;
5188 if (version == 7)
5190 surface->ref7 = 1;
5192 else if (version == 4)
5194 surface->ref4 = 1;
5196 else
5198 surface->ref1 = 1;
5201 copy_to_surfacedesc2(&surface->surface_desc, desc);
5203 surface->first_attached = surface;
5204 surface->ImplType = surface_type;
5206 hr = wined3d_surface_create(ddraw->wined3d_device, desc->dwWidth, desc->dwHeight, format,
5207 TRUE /* Lockable */, FALSE /* Discard */, mip_level, usage, pool,
5208 WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, surface_type, surface,
5209 &ddraw_surface_wined3d_parent_ops, &surface->wined3d_surface);
5210 if (FAILED(hr))
5212 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
5213 return hr;
5216 surface->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
5217 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
5218 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
5220 format = wined3d_desc.format;
5221 if (format == WINED3DFMT_UNKNOWN)
5223 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN.\n");
5225 PixelFormat_WineD3DtoDD(&surface->surface_desc.u4.ddpfPixelFormat, format);
5227 /* Anno 1602 stores the pitch right after surface creation, so make sure
5228 * it's there. TODO: Test other fourcc formats. */
5229 if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
5230 || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
5232 surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
5233 if (format == WINED3DFMT_DXT1)
5235 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height) / 2;
5237 else
5239 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height);
5242 else
5244 surface->surface_desc.dwFlags |= DDSD_PITCH;
5245 surface->surface_desc.u1.lPitch = wined3d_surface_get_pitch(surface->wined3d_surface);
5248 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
5250 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTOVERLAY,
5251 (WINEDDCOLORKEY *)&desc->u3.ddckCKDestOverlay);
5253 if (desc->dwFlags & DDSD_CKDESTBLT)
5255 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTBLT,
5256 (WINEDDCOLORKEY *)&desc->ddckCKDestBlt);
5258 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
5260 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCOVERLAY,
5261 (WINEDDCOLORKEY *)&desc->ddckCKSrcOverlay);
5263 if (desc->dwFlags & DDSD_CKSRCBLT)
5265 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCBLT,
5266 (WINEDDCOLORKEY *)&desc->ddckCKSrcBlt);
5268 if (desc->dwFlags & DDSD_LPSURFACE)
5270 hr = wined3d_surface_set_mem(surface->wined3d_surface, desc->lpSurface);
5271 if (FAILED(hr))
5273 ERR("Failed to set surface memory, hr %#x.\n", hr);
5274 wined3d_surface_decref(surface->wined3d_surface);
5275 return hr;
5279 return DD_OK;