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
8 * This file contains the (internal) driver registration functions,
9 * driver enumeration APIs and DirectDraw creation functions.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/port.h"
29 #include "ddraw_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
33 static inline IDirectDrawSurfaceImpl *surface_from_gamma_control(IDirectDrawGammaControl *iface)
35 return (IDirectDrawSurfaceImpl *)((char*)iface
36 - FIELD_OFFSET(IDirectDrawSurfaceImpl, IDirectDrawGammaControl_vtbl));
39 /*****************************************************************************
40 * IUnknown parts follow
41 *****************************************************************************/
43 /*****************************************************************************
44 * IDirectDrawSurface7::QueryInterface
46 * A normal QueryInterface implementation. For QueryInterface rules
47 * see ddraw.c, IDirectDraw7::QueryInterface. This method
48 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
49 * in all versions, the IDirectDrawGammaControl interface and it can
50 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
53 * riid: The interface id queried for
54 * obj: Address to write the pointer to
58 * E_NOINTERFACE if the requested interface wasn't found
60 *****************************************************************************/
61 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
63 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
65 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
67 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
71 return DDERR_INVALIDPARAMS;
73 if (IsEqualGUID(riid, &IID_IUnknown)
74 || IsEqualGUID(riid, &IID_IDirectDrawSurface7)
75 || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
77 IUnknown_AddRef(iface);
79 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
82 else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
83 || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
84 || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
86 IUnknown_AddRef(iface);
87 *obj = &This->IDirectDrawSurface3_vtbl;
88 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
91 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
93 IUnknown_AddRef(iface);
94 *obj = &This->IDirectDrawGammaControl_vtbl;
95 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
98 else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
99 IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
100 IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
102 IDirect3DDevice7 *d3d;
104 /* Call into IDirect3D7 for creation */
105 IDirect3D7_CreateDevice(&This->ddraw->IDirect3D7_iface, riid, (IDirectDrawSurface7 *)This,
110 *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
111 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
115 WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
116 return E_NOINTERFACE;
118 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
119 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
121 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
123 *obj = &This->IDirect3DTexture_vtbl;
124 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
128 *obj = &This->IDirect3DTexture2_vtbl;
129 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
131 IUnknown_AddRef( (IUnknown *) *obj);
135 ERR("No interface\n");
136 return E_NOINTERFACE;
139 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
141 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
143 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), riid, object);
146 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface, REFIID riid, void **object)
148 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
150 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_gamma_control(iface), riid, object);
153 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
155 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
157 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_texture2(iface), riid, object);
160 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
162 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
164 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_texture1(iface), riid, object);
167 /*****************************************************************************
168 * IDirectDrawSurface7::AddRef
170 * A normal addref implementation
175 *****************************************************************************/
176 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
178 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
179 ULONG refCount = InterlockedIncrement(&This->ref);
181 TRACE("%p increasing refcount to %u.\n", This, refCount);
185 EnterCriticalSection(&ddraw_cs);
186 if (This->WineD3DSurface)
187 IWineD3DSurface_AddRef(This->WineD3DSurface);
188 if (This->wined3d_texture)
189 wined3d_texture_incref(This->wined3d_texture);
190 LeaveCriticalSection(&ddraw_cs);
196 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
198 TRACE("iface %p.\n", iface);
200 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_surface3(iface));
203 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
205 TRACE("iface %p.\n", iface);
207 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_gamma_control(iface));
210 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
212 TRACE("iface %p.\n", iface);
214 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_texture2(iface));
217 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
219 TRACE("iface %p.\n", iface);
221 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_texture1(iface));
224 /*****************************************************************************
225 * ddraw_surface_destroy
227 * A helper function for IDirectDrawSurface7::Release
229 * Frees the surface, regardless of its refcount.
230 * See IDirectDrawSurface7::Release for more information
233 * This: Surface to free
235 *****************************************************************************/
236 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
238 TRACE("surface %p.\n", This);
240 /* Check the refcount and give a warning */
243 /* This can happen when a complex surface is destroyed,
244 * because the 2nd surface was addref()ed when the app
245 * called GetAttachedSurface
247 WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
250 if (This->WineD3DSurface)
251 IWineD3DSurface_Release(This->WineD3DSurface);
254 static void ddraw_surface_cleanup(IDirectDrawSurfaceImpl *surface)
256 IDirectDrawSurfaceImpl *surf;
257 IUnknown *ifaceToRelease;
260 TRACE("surface %p.\n", surface);
262 if (surface->wined3d_swapchain)
264 IDirectDrawImpl *ddraw = surface->ddraw;
266 /* If it's the render target, destroy the D3D device. */
267 if (ddraw->d3d_initialized && surface == ddraw->d3d_target)
269 TRACE("Destroying the render target, uninitializing D3D.\n");
271 for (i = 0; i < ddraw->numConvertedDecls; ++i)
273 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
275 HeapFree(GetProcessHeap(), 0, ddraw->decls);
276 ddraw->numConvertedDecls = 0;
278 if (FAILED(IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice)))
280 ERR("Failed to uninit 3D.\n");
284 /* Free the d3d window if one was created. */
285 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
287 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
288 DestroyWindow(ddraw->d3d_window);
289 ddraw->d3d_window = 0;
293 ddraw->d3d_initialized = FALSE;
294 ddraw->d3d_target = NULL;
298 IWineD3DDevice_UninitGDI(ddraw->wineD3DDevice);
301 surface->wined3d_swapchain = NULL;
303 /* Reset to the default surface implementation type. This is needed
304 * if applications use non render target surfaces and expect blits to
305 * work after destroying the render target.
307 * TODO: Recreate existing offscreen surfaces. */
308 ddraw->ImplType = DefaultSurfaceType;
310 TRACE("D3D unloaded.\n");
313 /* The refcount test shows that the palette is detached when the surface
315 IDirectDrawSurface7_SetPalette((IDirectDrawSurface7 *)surface, NULL);
317 /* Loop through all complex attached surfaces and destroy them.
319 * Yet again, only the root can have more than one complexly attached
320 * surface, all the others have a total of one. */
321 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
323 if (!surface->complex_array[i])
326 surf = surface->complex_array[i];
327 surface->complex_array[i] = NULL;
330 IDirectDrawSurfaceImpl *destroy = surf;
331 surf = surf->complex_array[0]; /* Iterate through the "tree" */
332 ddraw_surface_destroy(destroy); /* Destroy it */
336 ifaceToRelease = surface->ifaceToRelease;
338 /* Destroy the root surface. */
339 ddraw_surface_destroy(surface);
341 /* Reduce the ddraw refcount */
343 IUnknown_Release(ifaceToRelease);
346 /*****************************************************************************
347 * IDirectDrawSurface7::Release
349 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
350 * surface is destroyed.
352 * Destroying the surface is a bit tricky. For the connection between
353 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
354 * It has a nice graph explaining the connection.
356 * What happens here is basically this:
357 * When a surface is destroyed, its WineD3DSurface is released,
358 * and the refcount of the DirectDraw interface is reduced by 1. If it has
359 * complex surfaces attached to it, then these surfaces are destroyed too,
360 * regardless of their refcount. If any surface being destroyed has another
361 * surface attached to it (with a "soft" attachment, not complex), then
362 * this surface is detached with DeleteAttachedSurface.
364 * When the surface is a texture, the WineD3DTexture is released.
365 * If the surface is the Direct3D render target, then the D3D
366 * capabilities of the WineD3DDevice are uninitialized, which causes the
367 * swapchain to be released.
369 * When a complex sublevel falls to ref zero, then this is ignored.
374 *****************************************************************************/
375 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
377 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
378 ULONG ref = InterlockedDecrement(&This->ref);
380 TRACE("%p decreasing refcount to %u.\n", This, ref);
384 /* Complex attached surfaces are destroyed implicitly when the root is released */
385 EnterCriticalSection(&ddraw_cs);
386 if(!This->is_complex_root)
388 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
389 LeaveCriticalSection(&ddraw_cs);
392 if (This->wined3d_texture) /* If it's a texture, destroy the wined3d texture. */
393 wined3d_texture_decref(This->wined3d_texture);
395 ddraw_surface_cleanup(This);
396 LeaveCriticalSection(&ddraw_cs);
402 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
404 TRACE("iface %p.\n", iface);
406 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_surface3(iface));
409 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
411 TRACE("iface %p.\n", iface);
413 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_gamma_control(iface));
416 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
418 TRACE("iface %p.\n", iface);
420 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_texture2(iface));
423 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
425 TRACE("iface %p.\n", iface);
427 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_texture1(iface));
430 /*****************************************************************************
431 * IDirectDrawSurface7::GetAttachedSurface
433 * Returns an attached surface with the requested caps. Surface attachment
434 * and complex surfaces are not clearly described by the MSDN or sdk,
435 * so this method is tricky and likely to contain problems.
436 * This implementation searches the complex list first, then the
439 * The chains are searched from This down to the last surface in the chain,
440 * not from the first element in the chain. The first surface found is
441 * returned. The MSDN says that this method fails if more than one surface
442 * matches the caps, but it is not sure if that is right. The attachment
443 * structure may not even allow two matching surfaces.
445 * The found surface is AddRef-ed before it is returned.
448 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
449 * Surface: Address to store the found surface
453 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
454 * DDERR_NOTFOUND if no surface was found
456 *****************************************************************************/
457 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
458 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
460 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
461 IDirectDrawSurfaceImpl *surf;
465 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
467 EnterCriticalSection(&ddraw_cs);
469 if(This->version < 7)
471 /* Earlier dx apps put garbage into these members, clear them */
472 our_caps.dwCaps = Caps->dwCaps;
473 our_caps.dwCaps2 = 0;
474 our_caps.dwCaps3 = 0;
475 our_caps.dwCaps4 = 0;
482 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 */
484 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
486 surf = This->complex_array[i];
491 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
492 surf->surface_desc.ddsCaps.dwCaps,
493 surf->surface_desc.ddsCaps.dwCaps2,
494 surf->surface_desc.ddsCaps.dwCaps3,
495 surf->surface_desc.ddsCaps.dwCaps4);
498 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
499 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
501 /* MSDN: "This method fails if more than one surface is attached
502 * that matches the capabilities requested."
504 * Not sure how to test this.
507 TRACE("(%p): Returning surface %p\n", This, surf);
508 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
509 *Surface = (IDirectDrawSurface7 *)surf;
510 ddraw_surface7_AddRef(*Surface);
511 LeaveCriticalSection(&ddraw_cs);
516 /* Next, look at the attachment chain */
519 while( (surf = surf->next_attached) )
523 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
524 surf->surface_desc.ddsCaps.dwCaps,
525 surf->surface_desc.ddsCaps.dwCaps2,
526 surf->surface_desc.ddsCaps.dwCaps3,
527 surf->surface_desc.ddsCaps.dwCaps4);
530 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
531 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
533 TRACE("(%p): Returning surface %p\n", This, surf);
534 *Surface = (IDirectDrawSurface7 *)surf;
535 ddraw_surface7_AddRef(*Surface);
536 LeaveCriticalSection(&ddraw_cs);
541 TRACE("(%p) Didn't find a valid surface\n", This);
542 LeaveCriticalSection(&ddraw_cs);
545 return DDERR_NOTFOUND;
548 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
549 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
551 IDirectDrawSurface7 *attachment7;
555 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
557 caps2.dwCaps = caps->dwCaps;
562 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface),
563 &caps2, &attachment7);
564 if (FAILED(hr)) *attachment = NULL;
565 else *attachment = attachment7 ?
566 (IDirectDrawSurface3 *)&((IDirectDrawSurfaceImpl *)attachment7)->IDirectDrawSurface3_vtbl : NULL;
571 /*****************************************************************************
572 * IDirectDrawSurface7::Lock
574 * Locks the surface and returns a pointer to the surface's memory
577 * Rect: Rectangle to lock. If NULL, the whole surface is locked
578 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
579 * Flags: Locking flags, e.g Read only or write only
580 * h: An event handle that's not used and must be NULL
584 * DDERR_INVALIDPARAMS if DDSD is NULL
585 * For more details, see IWineD3DSurface::LockRect
587 *****************************************************************************/
588 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
589 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
591 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
592 WINED3DLOCKED_RECT LockedRect;
595 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
596 iface, wine_dbgstr_rect(Rect), DDSD, Flags, h);
599 return DDERR_INVALIDPARAMS;
601 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
602 EnterCriticalSection(&ddraw_cs);
604 /* Should I check for the handle to be NULL?
606 * The DDLOCK flags and the D3DLOCK flags are equal
607 * for the supported values. The others are ignored by WineD3D
610 if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
611 DDSD->dwSize != sizeof(DDSURFACEDESC2))
613 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
614 LeaveCriticalSection(&ddraw_cs);
615 return DDERR_INVALIDPARAMS;
618 /* Windows zeroes this if the rect is invalid */
625 || (Rect->left > Rect->right)
626 || (Rect->top > Rect->bottom)
627 || (Rect->right > This->surface_desc.dwWidth)
628 || (Rect->bottom > This->surface_desc.dwHeight))
630 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
631 LeaveCriticalSection(&ddraw_cs);
632 return DDERR_INVALIDPARAMS;
636 hr = IWineD3DSurface_Map(This->WineD3DSurface, &LockedRect, Rect, Flags);
639 LeaveCriticalSection(&ddraw_cs);
642 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
643 * specific error. But since IWineD3DSurface::LockRect returns that error in this
644 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
645 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
646 * is much easier to do it in one place in ddraw
648 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
653 /* Override the memory area. The pitch should be set already. Strangely windows
654 * does not set the LPSURFACE flag on locked surfaces !?!.
655 * DDSD->dwFlags |= DDSD_LPSURFACE;
657 This->surface_desc.lpSurface = LockedRect.pBits;
658 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
660 TRACE("locked surface returning description :\n");
661 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
663 LeaveCriticalSection(&ddraw_cs);
667 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
668 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
670 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
671 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
673 return ddraw_surface7_Lock((IDirectDrawSurface7 *)surface_from_surface3(iface),
674 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
677 /*****************************************************************************
678 * IDirectDrawSurface7::Unlock
680 * Unlocks an locked surface
683 * Rect: Not used by this implementation
687 * For more details, see IWineD3DSurface::UnlockRect
689 *****************************************************************************/
690 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
692 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
695 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
697 EnterCriticalSection(&ddraw_cs);
698 hr = IWineD3DSurface_Unmap(This->WineD3DSurface);
701 This->surface_desc.lpSurface = NULL;
703 LeaveCriticalSection(&ddraw_cs);
707 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
709 TRACE("iface %p, data %p.\n", iface, data);
711 /* data might not be the LPRECT of later versions, so drop it. */
712 return ddraw_surface7_Unlock((IDirectDrawSurface7 *)surface_from_surface3(iface), NULL);
715 /*****************************************************************************
716 * IDirectDrawSurface7::Flip
718 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
719 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
720 * the flip target is passed to WineD3D, even if the app didn't specify one
723 * DestOverride: Specifies the surface that will become the new front
724 * buffer. If NULL, the current back buffer is used
725 * Flags: some DirectDraw flags, see include/ddraw.h
729 * DDERR_NOTFLIPPABLE if no flip target could be found
730 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
731 * For more details, see IWineD3DSurface::Flip
733 *****************************************************************************/
734 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
736 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
737 IDirectDrawSurfaceImpl *Override = (IDirectDrawSurfaceImpl *)DestOverride;
738 IDirectDrawSurface7 *Override7;
741 TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
743 /* Flip has to be called from a front buffer
744 * What about overlay surfaces, AFAIK they can flip too?
746 if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
747 return DDERR_INVALIDOBJECT; /* Unchecked */
749 EnterCriticalSection(&ddraw_cs);
751 /* WineD3D doesn't keep track of attached surface, so find the target */
756 memset(&Caps, 0, sizeof(Caps));
757 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
758 hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
761 ERR("Can't find a flip target\n");
762 LeaveCriticalSection(&ddraw_cs);
763 return DDERR_NOTFLIPPABLE; /* Unchecked */
765 Override = (IDirectDrawSurfaceImpl *)Override7;
767 /* For the GetAttachedSurface */
768 ddraw_surface7_Release(Override7);
771 hr = IWineD3DSurface_Flip(This->WineD3DSurface,
772 Override->WineD3DSurface,
774 LeaveCriticalSection(&ddraw_cs);
778 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
780 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
782 return ddraw_surface7_Flip((IDirectDrawSurface7 *)surface_from_surface3(iface),
783 dst ? (IDirectDrawSurface7 *)surface_from_surface3(dst) : NULL, flags);
786 /*****************************************************************************
787 * IDirectDrawSurface7::Blt
789 * Performs a blit on the surface
792 * DestRect: Destination rectangle, can be NULL
793 * SrcSurface: Source surface, can be NULL
794 * SrcRect: Source rectangle, can be NULL
796 * DDBltFx: Some extended blt parameters, connected to the flags
800 * See IWineD3DSurface::Blt for more details
802 *****************************************************************************/
803 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
804 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
806 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
807 IDirectDrawSurfaceImpl *Src = (IDirectDrawSurfaceImpl *)SrcSurface;
810 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
811 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
813 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
814 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
816 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
817 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
818 return DDERR_INVALIDPARAMS;
821 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
822 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
823 return DDERR_INVALIDPARAMS;
826 /* Sizes can change, therefore hold the lock when testing the rectangles */
827 EnterCriticalSection(&ddraw_cs);
830 if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
831 DestRect->right > This->surface_desc.dwWidth ||
832 DestRect->bottom > This->surface_desc.dwHeight)
834 WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
835 LeaveCriticalSection(&ddraw_cs);
836 return DDERR_INVALIDRECT;
841 if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
842 SrcRect->right > Src->surface_desc.dwWidth ||
843 SrcRect->bottom > Src->surface_desc.dwHeight)
845 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
846 LeaveCriticalSection(&ddraw_cs);
847 return DDERR_INVALIDRECT;
851 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
852 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
853 LeaveCriticalSection(&ddraw_cs);
854 return DDERR_INVALIDPARAMS;
857 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it does, copy the struct,
858 * and replace the ddraw surfaces with the wined3d surfaces
859 * So far no blitting operations using surfaces in the bltfx struct are supported anyway.
861 hr = IWineD3DSurface_Blt(This->WineD3DSurface, DestRect, Src ? Src->WineD3DSurface : NULL,
862 SrcRect, Flags, (WINEDDBLTFX *)DDBltFx, WINED3DTEXF_LINEAR);
864 LeaveCriticalSection(&ddraw_cs);
867 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
868 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
873 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
874 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
876 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
877 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
879 return ddraw_surface7_Blt((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_rect,
880 src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags, fx);
883 /*****************************************************************************
884 * IDirectDrawSurface7::AddAttachedSurface
886 * Attaches a surface to another surface. How the surface attachments work
887 * is not totally understood yet, and this method is prone to problems.
888 * he surface that is attached is AddRef-ed.
890 * Tests with complex surfaces suggest that the surface attachments form a
891 * tree, but no method to test this has been found yet.
893 * The attachment list consists of a first surface (first_attached) and
894 * for each surface a pointer to the next attached surface (next_attached).
895 * For the first surface, and a surface that has no attachments
896 * first_attached points to the surface itself. A surface that has
897 * no successors in the chain has next_attached set to NULL.
899 * Newly attached surfaces are attached right after the root surface.
900 * If a surface is attached to a complex surface compound, it's attached to
901 * the surface that the app requested, not the complex root. See
902 * GetAttachedSurface for a description how surfaces are found.
904 * This is how the current implementation works, and it was coded by looking
905 * at the needs of the applications.
907 * So far only Z-Buffer attachments are tested, and they are activated in
908 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
909 * Back buffers should work in 2D mode, but they are not tested(They can be
910 * attached in older iface versions). Rendering to the front buffer and
911 * switching between that and double buffering is not yet implemented in
912 * WineD3D, so for 3D it might have unexpected results.
914 * ddraw_surface_attach_surface is the real thing,
915 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
916 * performs additional checks. Version 7 of this interface is much more restrictive
917 * than its predecessors.
920 * Attach: Surface to attach to iface
924 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
926 *****************************************************************************/
927 static HRESULT ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
929 TRACE("surface %p, attachment %p.\n", This, Surf);
932 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
934 EnterCriticalSection(&ddraw_cs);
936 /* Check if the surface is already attached somewhere */
937 if (Surf->next_attached || Surf->first_attached != Surf)
939 /* TODO: Test for the structure of the manual attachment. Is it a
940 * chain or a list? What happens if one surface is attached to 2
941 * different surfaces? */
942 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
943 Surf, Surf->next_attached, Surf->first_attached);
945 LeaveCriticalSection(&ddraw_cs);
946 return DDERR_SURFACEALREADYATTACHED;
949 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
950 Surf->next_attached = This->next_attached;
951 Surf->first_attached = This->first_attached;
952 This->next_attached = Surf;
954 /* Check if the WineD3D depth stencil needs updating */
955 if(This->ddraw->d3ddevice)
957 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
960 ddraw_surface7_AddRef((IDirectDrawSurface7 *)Surf);
961 LeaveCriticalSection(&ddraw_cs);
965 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
967 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
968 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
970 TRACE("iface %p, attachment %p.\n", iface, Attach);
972 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
973 if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
976 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
977 Surf->surface_desc.ddsCaps.dwCaps);
978 return DDERR_CANNOTATTACHSURFACE;
981 return ddraw_surface_attach_surface(This, Surf);
984 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
986 IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
987 IDirectDrawSurfaceImpl *attach_impl = surface_from_surface3(attachment);
989 TRACE("iface %p, attachment %p.\n", iface, attachment);
991 /* Tests suggest that
992 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
993 * -> offscreen plain surfaces can be attached to primaries
994 * -> primaries can be attached to offscreen plain surfaces
995 * -> z buffers can be attached to primaries */
996 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
997 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
999 /* Sizes have to match */
1000 if (attach_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
1001 || attach_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
1003 WARN("Surface sizes do not match.\n");
1004 return DDERR_CANNOTATTACHSURFACE;
1008 else if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1009 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1015 WARN("Invalid attachment combination.\n");
1016 return DDERR_CANNOTATTACHSURFACE;
1019 return ddraw_surface_attach_surface(surface, attach_impl);
1022 /*****************************************************************************
1023 * IDirectDrawSurface7::DeleteAttachedSurface
1025 * Removes a surface from the attachment chain. The surface's refcount
1026 * is decreased by one after it has been removed
1029 * Flags: Some flags, not used by this implementation
1030 * Attach: Surface to detach
1034 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1036 *****************************************************************************/
1037 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1038 DWORD Flags, IDirectDrawSurface7 *Attach)
1040 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1041 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
1042 IDirectDrawSurfaceImpl *Prev = This;
1044 TRACE("iface %p, flags %#x, attachment %p.\n", iface, Flags, Attach);
1046 EnterCriticalSection(&ddraw_cs);
1047 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1049 LeaveCriticalSection(&ddraw_cs);
1050 return DDERR_CANNOTDETACHSURFACE;
1053 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1054 if (This->surface_desc.ddsCaps.dwCaps &
1055 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1057 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1058 /* FIXME: we should probably also subtract from dwMipMapCount of this
1059 * and all parent surfaces */
1062 /* Find the predecessor of the detached surface */
1065 if(Prev->next_attached == Surf) break;
1066 Prev = Prev->next_attached;
1069 /* There must be a surface, otherwise there's a bug */
1070 assert(Prev != NULL);
1072 /* Unchain the surface */
1073 Prev->next_attached = Surf->next_attached;
1074 Surf->next_attached = NULL;
1075 Surf->first_attached = Surf;
1077 /* Check if the WineD3D depth stencil needs updating */
1078 if(This->ddraw->d3ddevice)
1080 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1083 ddraw_surface7_Release(Attach);
1084 LeaveCriticalSection(&ddraw_cs);
1088 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1089 DWORD flags, IDirectDrawSurface3 *attachment)
1091 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1093 return ddraw_surface7_DeleteAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
1094 attachment ? (IDirectDrawSurface7 *)surface_from_surface3(attachment) : NULL);
1097 /*****************************************************************************
1098 * IDirectDrawSurface7::AddOverlayDirtyRect
1100 * "This method is not currently implemented"
1108 *****************************************************************************/
1109 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1111 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1113 return DDERR_UNSUPPORTED; /* unchecked */
1116 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1118 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1120 return ddraw_surface7_AddOverlayDirtyRect((IDirectDrawSurface7 *)surface_from_surface3(iface), rect);
1123 /*****************************************************************************
1124 * IDirectDrawSurface7::GetDC
1126 * Returns a GDI device context for the surface
1129 * hdc: Address of a HDC variable to store the dc to
1133 * DDERR_INVALIDPARAMS if hdc is NULL
1134 * For details, see IWineD3DSurface::GetDC
1136 *****************************************************************************/
1137 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1139 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1142 TRACE("iface %p, dc %p.\n", iface, hdc);
1145 return DDERR_INVALIDPARAMS;
1147 EnterCriticalSection(&ddraw_cs);
1148 hr = IWineD3DSurface_GetDC(This->WineD3DSurface,
1150 LeaveCriticalSection(&ddraw_cs);
1153 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1156 case WINED3DERR_INVALIDCALL:
1157 if(hdc) *hdc = NULL;
1158 return DDERR_INVALIDPARAMS;
1164 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1166 TRACE("iface %p, dc %p.\n", iface, dc);
1168 return ddraw_surface7_GetDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1171 /*****************************************************************************
1172 * IDirectDrawSurface7::ReleaseDC
1174 * Releases the DC that was constructed with GetDC
1177 * hdc: HDC to release
1181 * For more details, see IWineD3DSurface::ReleaseDC
1183 *****************************************************************************/
1184 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1186 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1189 TRACE("iface %p, dc %p.\n", iface, hdc);
1191 EnterCriticalSection(&ddraw_cs);
1192 hr = IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
1193 LeaveCriticalSection(&ddraw_cs);
1197 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1199 TRACE("iface %p, dc %p.\n", iface, dc);
1201 return ddraw_surface7_ReleaseDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1204 /*****************************************************************************
1205 * IDirectDrawSurface7::GetCaps
1207 * Returns the surface's caps
1210 * Caps: Address to write the caps to
1214 * DDERR_INVALIDPARAMS if Caps is NULL
1216 *****************************************************************************/
1217 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1219 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1221 TRACE("iface %p, caps %p.\n", iface, Caps);
1224 return DDERR_INVALIDPARAMS;
1226 *Caps = This->surface_desc.ddsCaps;
1230 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1235 TRACE("iface %p, caps %p.\n", iface, caps);
1237 hr = ddraw_surface7_GetCaps((IDirectDrawSurface7 *)surface_from_surface3(iface), &caps2);
1238 if (FAILED(hr)) return hr;
1240 caps->dwCaps = caps2.dwCaps;
1244 /*****************************************************************************
1245 * IDirectDrawSurface7::SetPriority
1247 * Sets a texture priority for managed textures.
1250 * Priority: The new priority
1254 * For more details, see IWineD3DSurface::SetPriority
1256 *****************************************************************************/
1257 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1259 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1262 TRACE("iface %p, priority %u.\n", iface, Priority);
1264 EnterCriticalSection(&ddraw_cs);
1265 hr = IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
1266 LeaveCriticalSection(&ddraw_cs);
1270 /*****************************************************************************
1271 * IDirectDrawSurface7::GetPriority
1273 * Returns the surface's priority
1276 * Priority: Address of a variable to write the priority to
1280 * DDERR_INVALIDPARAMS if Priority == NULL
1281 * For more details, see IWineD3DSurface::GetPriority
1283 *****************************************************************************/
1284 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
1286 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1288 TRACE("iface %p, priority %p.\n", iface, Priority);
1292 return DDERR_INVALIDPARAMS;
1295 EnterCriticalSection(&ddraw_cs);
1296 *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1297 LeaveCriticalSection(&ddraw_cs);
1301 /*****************************************************************************
1302 * IDirectDrawSurface7::SetPrivateData
1304 * Stores some data in the surface that is intended for the application's
1308 * tag: GUID that identifies the data
1309 * Data: Pointer to the private data
1310 * Size: Size of the private data
1315 * For more details, see IWineD3DSurface::SetPrivateData
1317 *****************************************************************************/
1318 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
1319 REFGUID tag, void *Data, DWORD Size, DWORD Flags)
1321 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1324 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
1325 iface, debugstr_guid(tag), Data, Size, Flags);
1327 EnterCriticalSection(&ddraw_cs);
1328 hr = IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1333 LeaveCriticalSection(&ddraw_cs);
1336 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1341 /*****************************************************************************
1342 * IDirectDrawSurface7::GetPrivateData
1344 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1347 * tag: GUID of the data to return
1348 * Data: Address where to write the data to
1349 * Size: Size of the buffer at Data
1353 * DDERR_INVALIDPARAMS if Data is NULL
1354 * For more details, see IWineD3DSurface::GetPrivateData
1356 *****************************************************************************/
1357 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
1359 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1362 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
1363 iface, debugstr_guid(tag), Data, Size);
1366 return DDERR_INVALIDPARAMS;
1368 EnterCriticalSection(&ddraw_cs);
1369 hr = IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1373 LeaveCriticalSection(&ddraw_cs);
1377 /*****************************************************************************
1378 * IDirectDrawSurface7::FreePrivateData
1380 * Frees private data stored in the surface
1383 * tag: Tag of the data to free
1387 * For more details, see IWineD3DSurface::FreePrivateData
1389 *****************************************************************************/
1390 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
1392 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1395 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
1397 EnterCriticalSection(&ddraw_cs);
1398 hr = IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1399 LeaveCriticalSection(&ddraw_cs);
1403 /*****************************************************************************
1404 * IDirectDrawSurface7::PageLock
1406 * Prevents a sysmem surface from being paged out
1409 * Flags: Not used, must be 0(unchecked)
1412 * DD_OK, because it's a stub
1414 *****************************************************************************/
1415 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
1417 TRACE("iface %p, flags %#x.\n", iface, Flags);
1419 /* This is Windows memory management related - we don't need this */
1423 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
1425 TRACE("iface %p, flags %#x.\n", iface, flags);
1427 return ddraw_surface7_PageLock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1430 /*****************************************************************************
1431 * IDirectDrawSurface7::PageUnlock
1433 * Allows a sysmem surface to be paged out
1436 * Flags: Not used, must be 0(unchecked)
1439 * DD_OK, because it's a stub
1441 *****************************************************************************/
1442 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
1444 TRACE("iface %p, flags %#x.\n", iface, Flags);
1449 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
1451 TRACE("iface %p, flags %#x.\n", iface, flags);
1453 return ddraw_surface7_PageUnlock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1456 /*****************************************************************************
1457 * IDirectDrawSurface7::BltBatch
1459 * An unimplemented function
1467 *****************************************************************************/
1468 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1470 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
1472 /* MSDN: "not currently implemented" */
1473 return DDERR_UNSUPPORTED;
1476 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
1478 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
1480 return ddraw_surface7_BltBatch((IDirectDrawSurface7 *)surface_from_surface3(iface), batch, count, flags);
1483 /*****************************************************************************
1484 * IDirectDrawSurface7::EnumAttachedSurfaces
1486 * Enumerates all surfaces attached to this surface
1489 * context: Pointer to pass unmodified to the callback
1490 * cb: Callback function to call for each surface
1494 * DDERR_INVALIDPARAMS if cb is NULL
1496 *****************************************************************************/
1497 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1498 void *context, LPDDENUMSURFACESCALLBACK7 cb)
1500 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1501 IDirectDrawSurfaceImpl *surf;
1502 DDSURFACEDESC2 desc;
1505 /* Attached surfaces aren't handled in WineD3D */
1506 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
1509 return DDERR_INVALIDPARAMS;
1511 EnterCriticalSection(&ddraw_cs);
1512 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1514 surf = This->complex_array[i];
1517 ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1518 desc = surf->surface_desc;
1519 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1520 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1522 LeaveCriticalSection(&ddraw_cs);
1527 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1529 ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1530 desc = surf->surface_desc;
1531 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1532 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1534 LeaveCriticalSection(&ddraw_cs);
1539 TRACE(" end of enumeration.\n");
1541 LeaveCriticalSection(&ddraw_cs);
1545 struct callback_info
1547 LPDDENUMSURFACESCALLBACK callback;
1551 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
1553 const struct callback_info *info = context;
1555 return info->callback((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_vtbl,
1556 (DDSURFACEDESC *)surface_desc, info->context);
1559 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
1560 void *context, LPDDENUMSURFACESCALLBACK callback)
1562 struct callback_info info;
1564 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
1566 info.callback = callback;
1567 info.context = context;
1569 return ddraw_surface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)surface_from_surface3(iface),
1570 &info, EnumCallback);
1573 /*****************************************************************************
1574 * IDirectDrawSurface7::EnumOverlayZOrders
1576 * "Enumerates the overlay surfaces on the specified destination"
1579 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
1580 * context: context to pass back to the callback
1581 * cb: callback function to call for each enumerated surface
1584 * DD_OK, because it's a stub
1586 *****************************************************************************/
1587 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1588 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
1590 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
1595 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
1596 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
1598 struct callback_info info;
1600 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
1602 info.callback = callback;
1603 info.context = context;
1605 return ddraw_surface7_EnumOverlayZOrders((IDirectDrawSurface7 *)surface_from_surface3(iface),
1606 flags, &info, EnumCallback);
1609 /*****************************************************************************
1610 * IDirectDrawSurface7::GetBltStatus
1612 * Returns the blitting status
1615 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1618 * See IWineD3DSurface::Blt
1620 *****************************************************************************/
1621 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1623 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1626 TRACE("iface %p, flags %#x.\n", iface, Flags);
1628 EnterCriticalSection(&ddraw_cs);
1629 hr = IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1630 LeaveCriticalSection(&ddraw_cs);
1633 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1638 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
1640 TRACE("iface %p, flags %#x.\n", iface, flags);
1642 return ddraw_surface7_GetBltStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1645 /*****************************************************************************
1646 * IDirectDrawSurface7::GetColorKey
1648 * Returns the color key assigned to the surface
1652 * CKey: Address to store the key to
1656 * DDERR_INVALIDPARAMS if CKey is NULL
1658 *****************************************************************************/
1659 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
1661 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1663 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
1666 return DDERR_INVALIDPARAMS;
1668 EnterCriticalSection(&ddraw_cs);
1672 case DDCKEY_DESTBLT:
1673 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
1675 LeaveCriticalSection(&ddraw_cs);
1676 return DDERR_NOCOLORKEY;
1678 *CKey = This->surface_desc.ddckCKDestBlt;
1681 case DDCKEY_DESTOVERLAY:
1682 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
1684 LeaveCriticalSection(&ddraw_cs);
1685 return DDERR_NOCOLORKEY;
1687 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1691 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
1693 LeaveCriticalSection(&ddraw_cs);
1694 return DDERR_NOCOLORKEY;
1696 *CKey = This->surface_desc.ddckCKSrcBlt;
1699 case DDCKEY_SRCOVERLAY:
1700 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
1702 LeaveCriticalSection(&ddraw_cs);
1703 return DDERR_NOCOLORKEY;
1705 *CKey = This->surface_desc.ddckCKSrcOverlay;
1709 LeaveCriticalSection(&ddraw_cs);
1710 return DDERR_INVALIDPARAMS;
1713 LeaveCriticalSection(&ddraw_cs);
1717 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
1719 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
1721 return ddraw_surface7_GetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
1724 /*****************************************************************************
1725 * IDirectDrawSurface7::GetFlipStatus
1727 * Returns the flipping status of the surface
1730 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1733 * See IWineD3DSurface::GetFlipStatus
1735 *****************************************************************************/
1736 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1738 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1741 TRACE("iface %p, flags %#x.\n", iface, Flags);
1743 EnterCriticalSection(&ddraw_cs);
1744 hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1745 LeaveCriticalSection(&ddraw_cs);
1748 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1753 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
1755 TRACE("iface %p, flags %#x.\n", iface, flags);
1757 return ddraw_surface7_GetFlipStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1760 /*****************************************************************************
1761 * IDirectDrawSurface7::GetOverlayPosition
1763 * Returns the display coordinates of a visible and active overlay surface
1770 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
1771 *****************************************************************************/
1772 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
1774 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1777 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
1779 EnterCriticalSection(&ddraw_cs);
1780 hr = IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1783 LeaveCriticalSection(&ddraw_cs);
1787 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
1789 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
1791 return ddraw_surface7_GetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
1794 /*****************************************************************************
1795 * IDirectDrawSurface7::GetPixelFormat
1797 * Returns the pixel format of the Surface
1800 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1801 * format should be written
1805 * DDERR_INVALIDPARAMS if PixelFormat is NULL
1807 *****************************************************************************/
1808 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
1810 /* What is DDERR_INVALIDSURFACETYPE for here? */
1811 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1813 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
1816 return DDERR_INVALIDPARAMS;
1818 EnterCriticalSection(&ddraw_cs);
1819 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1820 LeaveCriticalSection(&ddraw_cs);
1825 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
1827 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
1829 return ddraw_surface7_GetPixelFormat((IDirectDrawSurface7 *)surface_from_surface3(iface), pixel_format);
1832 /*****************************************************************************
1833 * IDirectDrawSurface7::GetSurfaceDesc
1835 * Returns the description of this surface
1838 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1843 * DDERR_INVALIDPARAMS if DDSD is NULL
1845 *****************************************************************************/
1846 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
1848 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1850 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1853 return DDERR_INVALIDPARAMS;
1855 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1857 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1858 return DDERR_INVALIDPARAMS;
1861 EnterCriticalSection(&ddraw_cs);
1862 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1863 TRACE("Returning surface desc:\n");
1864 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1866 LeaveCriticalSection(&ddraw_cs);
1870 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
1872 IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
1874 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1876 if (!surface_desc) return DDERR_INVALIDPARAMS;
1878 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
1880 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
1881 return DDERR_INVALIDPARAMS;
1884 EnterCriticalSection(&ddraw_cs);
1885 DD_STRUCT_COPY_BYSIZE(surface_desc, (DDSURFACEDESC *)&surface->surface_desc);
1886 TRACE("Returning surface desc:\n");
1887 if (TRACE_ON(ddraw))
1889 /* DDRAW_dump_surface_desc handles the smaller size */
1890 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
1893 LeaveCriticalSection(&ddraw_cs);
1897 /*****************************************************************************
1898 * IDirectDrawSurface7::Initialize
1900 * Initializes the surface. This is a no-op in Wine
1903 * DD: Pointer to an DirectDraw interface
1904 * DDSD: Surface description for initialization
1907 * DDERR_ALREADYINITIALIZED
1909 *****************************************************************************/
1910 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
1911 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
1913 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1915 return DDERR_ALREADYINITIALIZED;
1918 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
1919 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
1921 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1923 return ddraw_surface7_Initialize((IDirectDrawSurface7 *)surface_from_surface3(iface),
1924 ddraw, (DDSURFACEDESC2 *)surface_desc);
1927 /*****************************************************************************
1928 * IDirect3DTexture1::Initialize
1930 * The sdk says it's not implemented
1938 *****************************************************************************/
1939 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
1940 IDirect3DDevice *device, IDirectDrawSurface *surface)
1942 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
1944 return DDERR_UNSUPPORTED; /* Unchecked */
1947 /*****************************************************************************
1948 * IDirectDrawSurface7::IsLost
1950 * Checks if the surface is lost
1953 * DD_OK, if the surface is usable
1954 * DDERR_ISLOST if the surface is lost
1955 * See IWineD3DSurface::IsLost for more details
1957 *****************************************************************************/
1958 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
1960 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1963 TRACE("iface %p.\n", iface);
1965 EnterCriticalSection(&ddraw_cs);
1966 /* We lose the surface if the implementation was changed */
1967 if(This->ImplType != This->ddraw->ImplType)
1969 /* But this shouldn't happen. When we change the implementation,
1970 * all surfaces are re-created automatically, and their content
1973 ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1974 LeaveCriticalSection(&ddraw_cs);
1975 return DDERR_SURFACELOST;
1978 hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
1979 LeaveCriticalSection(&ddraw_cs);
1982 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1983 * WineD3D uses the same error for surfaces
1985 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
1990 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
1992 TRACE("iface %p.\n", iface);
1994 return ddraw_surface7_IsLost((IDirectDrawSurface7 *)surface_from_surface3(iface));
1997 /*****************************************************************************
1998 * IDirectDrawSurface7::Restore
2000 * Restores a lost surface. This makes the surface usable again, but
2001 * doesn't reload its old contents
2005 * See IWineD3DSurface::Restore for more details
2007 *****************************************************************************/
2008 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
2010 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2013 TRACE("iface %p.\n", iface);
2015 EnterCriticalSection(&ddraw_cs);
2016 if(This->ImplType != This->ddraw->ImplType)
2018 /* Call the recreation callback. Make sure to AddRef first */
2019 IDirectDrawSurface_AddRef(iface);
2020 ddraw_recreate_surfaces_cb(iface, &This->surface_desc, NULL /* Not needed */);
2022 hr = IWineD3DSurface_Restore(This->WineD3DSurface);
2023 LeaveCriticalSection(&ddraw_cs);
2027 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
2029 TRACE("iface %p.\n", iface);
2031 return ddraw_surface7_Restore((IDirectDrawSurface7 *)surface_from_surface3(iface));
2034 /*****************************************************************************
2035 * IDirectDrawSurface7::SetOverlayPosition
2037 * Changes the display coordinates of an overlay surface
2044 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
2045 *****************************************************************************/
2046 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
2048 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2051 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
2053 EnterCriticalSection(&ddraw_cs);
2054 hr = IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
2057 LeaveCriticalSection(&ddraw_cs);
2061 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
2063 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
2065 return ddraw_surface7_SetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
2068 /*****************************************************************************
2069 * IDirectDrawSurface7::UpdateOverlay
2071 * Modifies the attributes of an overlay surface.
2074 * SrcRect: The section of the source being used for the overlay
2075 * DstSurface: Address of the surface that is overlaid
2076 * DstRect: Place of the overlay
2077 * Flags: some DDOVER_* flags
2080 * DDERR_UNSUPPORTED, because we don't support overlays
2082 *****************************************************************************/
2083 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
2084 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
2086 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2087 IDirectDrawSurfaceImpl *Dst = (IDirectDrawSurfaceImpl *)DstSurface;
2090 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2091 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
2093 EnterCriticalSection(&ddraw_cs);
2094 hr = IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
2096 Dst ? Dst->WineD3DSurface : NULL,
2099 (WINEDDOVERLAYFX *) FX);
2100 LeaveCriticalSection(&ddraw_cs);
2102 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2103 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
2104 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
2110 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
2111 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
2113 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2114 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
2116 return ddraw_surface7_UpdateOverlay((IDirectDrawSurface7 *)surface_from_surface3(iface), src_rect,
2117 dst_surface ? (IDirectDrawSurface7 *)surface_from_surface3(dst_surface) : NULL, dst_rect, flags, fx);
2120 /*****************************************************************************
2121 * IDirectDrawSurface7::UpdateOverlayDisplay
2123 * The DX7 sdk says that it's not implemented
2128 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
2130 *****************************************************************************/
2131 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
2133 TRACE("iface %p, flags %#x.\n", iface, Flags);
2135 return DDERR_UNSUPPORTED;
2138 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
2140 TRACE("iface %p, flags %#x.\n", iface, flags);
2142 return ddraw_surface7_UpdateOverlayDisplay((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
2145 /*****************************************************************************
2146 * IDirectDrawSurface7::UpdateOverlayZOrder
2148 * Sets an overlay's Z order
2151 * Flags: DDOVERZ_* flags
2152 * DDSRef: Defines the relative position in the overlay chain
2155 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
2157 *****************************************************************************/
2158 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
2159 DWORD Flags, IDirectDrawSurface7 *DDSRef)
2161 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2162 IDirectDrawSurfaceImpl *Ref = (IDirectDrawSurfaceImpl *)DDSRef;
2165 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
2167 EnterCriticalSection(&ddraw_cs);
2168 hr = IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
2170 Ref ? Ref->WineD3DSurface : NULL);
2171 LeaveCriticalSection(&ddraw_cs);
2175 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
2176 DWORD flags, IDirectDrawSurface3 *reference)
2178 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
2180 return ddraw_surface7_UpdateOverlayZOrder((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
2181 reference ? (IDirectDrawSurface7 *)surface_from_surface3(reference) : NULL);
2184 /*****************************************************************************
2185 * IDirectDrawSurface7::GetDDInterface
2187 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
2188 * surface belongs to
2191 * DD: Address to write the interface pointer to
2195 * DDERR_INVALIDPARAMS if DD is NULL
2197 *****************************************************************************/
2198 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
2200 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2202 TRACE("iface %p, ddraw %p.\n", iface, DD);
2205 return DDERR_INVALIDPARAMS;
2207 switch(This->version)
2210 *DD = &This->ddraw->IDirectDraw7_iface;
2214 *DD = &This->ddraw->IDirectDraw4_iface;
2218 *DD = &This->ddraw->IDirectDraw2_iface;
2222 *DD = &This->ddraw->IDirectDraw_iface;
2226 IUnknown_AddRef((IUnknown *)*DD);
2231 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
2233 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
2235 return ddraw_surface7_GetDDInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), ddraw);
2238 /* This seems also windows implementation specific - I don't think WineD3D needs this */
2239 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
2241 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2242 volatile IDirectDrawSurfaceImpl* vThis = This;
2244 TRACE("iface %p.\n", iface);
2246 EnterCriticalSection(&ddraw_cs);
2247 /* A uniqueness value of 0 is apparently special.
2248 * This needs to be checked.
2249 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
2252 DWORD old_uniqueness_value = vThis->uniqueness_value;
2253 DWORD new_uniqueness_value = old_uniqueness_value+1;
2255 if (old_uniqueness_value == 0) break;
2256 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
2258 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
2259 old_uniqueness_value,
2260 new_uniqueness_value)
2261 == old_uniqueness_value)
2265 LeaveCriticalSection(&ddraw_cs);
2269 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
2271 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2273 TRACE("iface %p, value %p.\n", iface, pValue);
2275 EnterCriticalSection(&ddraw_cs);
2276 *pValue = This->uniqueness_value;
2277 LeaveCriticalSection(&ddraw_cs);
2281 /*****************************************************************************
2282 * IDirectDrawSurface7::SetLOD
2284 * Sets the level of detail of a texture
2287 * MaxLOD: LOD to set
2291 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2293 *****************************************************************************/
2294 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
2296 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2299 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
2301 EnterCriticalSection(&ddraw_cs);
2302 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2304 LeaveCriticalSection(&ddraw_cs);
2305 return DDERR_INVALIDOBJECT;
2308 if (!This->wined3d_texture)
2310 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2311 LeaveCriticalSection(&ddraw_cs);
2312 return DDERR_INVALIDOBJECT;
2315 hr = wined3d_texture_set_lod(This->wined3d_texture, MaxLOD);
2316 LeaveCriticalSection(&ddraw_cs);
2320 /*****************************************************************************
2321 * IDirectDrawSurface7::GetLOD
2323 * Returns the level of detail of a Direct3D texture
2326 * MaxLOD: Address to write the LOD to
2330 * DDERR_INVALIDPARAMS if MaxLOD is NULL
2331 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2333 *****************************************************************************/
2334 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
2336 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2338 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
2341 return DDERR_INVALIDPARAMS;
2343 EnterCriticalSection(&ddraw_cs);
2344 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2346 LeaveCriticalSection(&ddraw_cs);
2347 return DDERR_INVALIDOBJECT;
2350 *MaxLOD = wined3d_texture_get_lod(This->wined3d_texture);
2351 LeaveCriticalSection(&ddraw_cs);
2355 /*****************************************************************************
2356 * IDirectDrawSurface7::BltFast
2358 * Performs a fast Blit.
2361 * dstx: The x coordinate to blit to on the destination
2362 * dsty: The y coordinate to blit to on the destination
2363 * Source: The source surface
2364 * rsrc: The source rectangle
2365 * trans: Type of transfer. Some DDBLTFAST_* flags
2369 * For more details, see IWineD3DSurface::BltFast
2371 *****************************************************************************/
2372 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
2373 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
2375 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2376 IDirectDrawSurfaceImpl *src = (IDirectDrawSurfaceImpl *)Source;
2377 DWORD src_w, src_h, dst_w, dst_h;
2380 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2381 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
2383 dst_w = This->surface_desc.dwWidth;
2384 dst_h = This->surface_desc.dwHeight;
2386 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2391 if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2392 rsrc->right > src->surface_desc.dwWidth ||
2393 rsrc->bottom > src->surface_desc.dwHeight)
2395 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2396 return DDERR_INVALIDRECT;
2399 src_w = rsrc->right - rsrc->left;
2400 src_h = rsrc->bottom - rsrc->top;
2404 src_w = src->surface_desc.dwWidth;
2405 src_h = src->surface_desc.dwHeight;
2408 if (src_w > dst_w || dstx > dst_w - src_w
2409 || src_h > dst_h || dsty > dst_h - src_h)
2411 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
2412 return DDERR_INVALIDRECT;
2415 EnterCriticalSection(&ddraw_cs);
2416 hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
2418 src ? src->WineD3DSurface : NULL,
2421 LeaveCriticalSection(&ddraw_cs);
2424 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
2425 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
2430 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
2431 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
2433 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2434 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
2436 return ddraw_surface7_BltFast((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_x, dst_y,
2437 src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags);
2440 /*****************************************************************************
2441 * IDirectDrawSurface7::GetClipper
2443 * Returns the IDirectDrawClipper interface of the clipper assigned to this
2447 * Clipper: Address to store the interface pointer at
2451 * DDERR_INVALIDPARAMS if Clipper is NULL
2452 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
2454 *****************************************************************************/
2455 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
2457 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2459 TRACE("iface %p, clipper %p.\n", iface, Clipper);
2463 LeaveCriticalSection(&ddraw_cs);
2464 return DDERR_INVALIDPARAMS;
2467 EnterCriticalSection(&ddraw_cs);
2468 if(This->clipper == NULL)
2470 LeaveCriticalSection(&ddraw_cs);
2471 return DDERR_NOCLIPPERATTACHED;
2474 *Clipper = (IDirectDrawClipper *)This->clipper;
2475 IDirectDrawClipper_AddRef(*Clipper);
2476 LeaveCriticalSection(&ddraw_cs);
2480 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
2482 TRACE("iface %p, clipper %p.\n", iface, clipper);
2484 return ddraw_surface7_GetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2487 /*****************************************************************************
2488 * IDirectDrawSurface7::SetClipper
2490 * Sets a clipper for the surface
2493 * Clipper: IDirectDrawClipper interface of the clipper to set
2498 *****************************************************************************/
2499 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper *Clipper)
2501 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2502 IDirectDrawClipperImpl *oldClipper = This->clipper;
2506 TRACE("iface %p, clipper %p.\n", iface, Clipper);
2508 EnterCriticalSection(&ddraw_cs);
2509 if ((IDirectDrawClipperImpl *)Clipper == This->clipper)
2511 LeaveCriticalSection(&ddraw_cs);
2515 This->clipper = (IDirectDrawClipperImpl *)Clipper;
2517 if (Clipper != NULL)
2518 IDirectDrawClipper_AddRef(Clipper);
2520 IDirectDrawClipper_Release((IDirectDrawClipper *)oldClipper);
2522 hr = IWineD3DSurface_SetClipper(This->WineD3DSurface, This->clipper ? This->clipper->wineD3DClipper : NULL);
2524 if (This->wined3d_swapchain)
2528 IDirectDrawClipper_GetHWnd(Clipper, &clipWindow);
2532 wined3d_swapchain_set_window(This->wined3d_swapchain, clipWindow);
2534 wined3d_swapchain_set_window(This->wined3d_swapchain, This->ddraw->d3d_window);
2537 LeaveCriticalSection(&ddraw_cs);
2541 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
2543 TRACE("iface %p, clipper %p.\n", iface, clipper);
2545 return ddraw_surface7_SetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2548 /*****************************************************************************
2549 * IDirectDrawSurface7::SetSurfaceDesc
2551 * Sets the surface description. It can override the pixel format, the surface
2553 * It's not really tested.
2556 * DDSD: Pointer to the new surface description to set
2561 * DDERR_INVALIDPARAMS if DDSD is NULL
2563 *****************************************************************************/
2564 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
2566 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2567 enum wined3d_format_id newFormat = WINED3DFMT_UNKNOWN;
2570 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
2573 return DDERR_INVALIDPARAMS;
2575 EnterCriticalSection(&ddraw_cs);
2576 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2578 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2580 if(newFormat == WINED3DFMT_UNKNOWN)
2582 ERR("Requested to set an unknown pixelformat\n");
2583 LeaveCriticalSection(&ddraw_cs);
2584 return DDERR_INVALIDPARAMS;
2586 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2588 hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2592 LeaveCriticalSection(&ddraw_cs);
2597 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2599 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2601 (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2603 if (DDSD->dwFlags & DDSD_CKDESTBLT)
2605 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2607 (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2609 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2611 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2613 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2615 if (DDSD->dwFlags & DDSD_CKSRCBLT)
2617 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2619 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2621 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2623 hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2624 if(hr != WINED3D_OK)
2626 /* No need for a trace here, wined3d does that for us */
2629 case WINED3DERR_INVALIDCALL:
2630 LeaveCriticalSection(&ddraw_cs);
2631 return DDERR_INVALIDPARAMS;
2638 This->surface_desc = *DDSD;
2640 LeaveCriticalSection(&ddraw_cs);
2644 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
2645 DDSURFACEDESC *surface_desc, DWORD flags)
2647 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
2649 return ddraw_surface7_SetSurfaceDesc((IDirectDrawSurface7 *)surface_from_surface3(iface),
2650 (DDSURFACEDESC2 *)surface_desc, flags);
2653 /*****************************************************************************
2654 * IDirectDrawSurface7::GetPalette
2656 * Returns the IDirectDrawPalette interface of the palette currently assigned
2660 * Pal: Address to write the interface pointer to
2664 * DDERR_INVALIDPARAMS if Pal is NULL
2666 *****************************************************************************/
2667 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
2669 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2670 struct wined3d_palette *wined3d_palette;
2673 TRACE("iface %p, palette %p.\n", iface, Pal);
2676 return DDERR_INVALIDPARAMS;
2678 EnterCriticalSection(&ddraw_cs);
2679 hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wined3d_palette);
2682 LeaveCriticalSection(&ddraw_cs);
2686 if (wined3d_palette)
2688 *Pal = wined3d_palette_get_parent(wined3d_palette);
2689 IDirectDrawPalette_AddRef(*Pal);
2694 hr = DDERR_NOPALETTEATTACHED;
2697 LeaveCriticalSection(&ddraw_cs);
2701 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
2703 TRACE("iface %p, palette %p.\n", iface, palette);
2705 return ddraw_surface7_GetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2708 /*****************************************************************************
2711 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2712 * recursively in the surface tree
2714 *****************************************************************************/
2718 WINEDDCOLORKEY *CKey;
2722 static HRESULT WINAPI
2723 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2724 DDSURFACEDESC2 *desc,
2727 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)surface;
2728 struct SCKContext *ctx = context;
2731 hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2736 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2740 ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
2741 ddraw_surface7_Release(surface);
2743 return DDENUMRET_OK;
2746 /*****************************************************************************
2747 * IDirectDrawSurface7::SetColorKey
2749 * Sets the color keying options for the surface. Observations showed that
2750 * in case of complex surfaces the color key has to be assigned to all
2755 * CKey: The new color key
2759 * See IWineD3DSurface::SetColorKey for details
2761 *****************************************************************************/
2762 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2764 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2765 DDCOLORKEY FixedCKey;
2766 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
2768 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2770 EnterCriticalSection(&ddraw_cs);
2774 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
2775 if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
2776 FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
2778 switch (Flags & ~DDCKEY_COLORSPACE)
2780 case DDCKEY_DESTBLT:
2781 This->surface_desc.ddckCKDestBlt = FixedCKey;
2782 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2785 case DDCKEY_DESTOVERLAY:
2786 This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
2787 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2790 case DDCKEY_SRCOVERLAY:
2791 This->surface_desc.ddckCKSrcOverlay = FixedCKey;
2792 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2796 This->surface_desc.ddckCKSrcBlt = FixedCKey;
2797 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2801 LeaveCriticalSection(&ddraw_cs);
2802 return DDERR_INVALIDPARAMS;
2807 switch (Flags & ~DDCKEY_COLORSPACE)
2809 case DDCKEY_DESTBLT:
2810 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2813 case DDCKEY_DESTOVERLAY:
2814 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2817 case DDCKEY_SRCOVERLAY:
2818 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2822 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2826 LeaveCriticalSection(&ddraw_cs);
2827 return DDERR_INVALIDPARAMS;
2830 ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface, Flags, ctx.CKey);
2831 ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
2832 LeaveCriticalSection(&ddraw_cs);
2835 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2836 default: return ctx.ret;
2840 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2842 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2844 return ddraw_surface7_SetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
2847 /*****************************************************************************
2848 * IDirectDrawSurface7::SetPalette
2850 * Assigns a DirectDrawPalette object to the surface
2853 * Pal: Interface to the palette to set
2858 *****************************************************************************/
2859 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
2861 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2862 IDirectDrawPalette *oldPal;
2863 IDirectDrawSurfaceImpl *surf;
2864 IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
2867 TRACE("iface %p, palette %p.\n", iface, Pal);
2869 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
2870 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
2871 return DDERR_INVALIDPIXELFORMAT;
2874 /* Find the old palette */
2875 EnterCriticalSection(&ddraw_cs);
2876 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2877 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2879 LeaveCriticalSection(&ddraw_cs);
2882 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
2884 /* Set the new Palette */
2885 IWineD3DSurface_SetPalette(This->WineD3DSurface,
2886 PalImpl ? PalImpl->wineD3DPalette : NULL);
2887 /* AddRef the Palette */
2888 if(Pal) IDirectDrawPalette_AddRef(Pal);
2890 /* Release the old palette */
2891 if(oldPal) IDirectDrawPalette_Release(oldPal);
2893 /* If this is a front buffer, also update the back buffers
2894 * TODO: How do things work for palettized cube textures?
2896 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2898 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2899 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2904 IDirectDrawSurface7 *attach;
2906 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &caps2, &attach);
2912 TRACE("Setting palette on %p\n", attach);
2913 ddraw_surface7_SetPalette(attach, Pal);
2914 surf = (IDirectDrawSurfaceImpl *)attach;
2915 ddraw_surface7_Release(attach);
2919 LeaveCriticalSection(&ddraw_cs);
2923 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
2925 TRACE("iface %p, palette %p.\n", iface, palette);
2927 return ddraw_surface7_SetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2930 /**********************************************************
2931 * IDirectDrawGammaControl::GetGammaRamp
2933 * Returns the current gamma ramp for a surface
2937 * gamma_ramp: Address to write the ramp to
2941 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
2943 **********************************************************/
2944 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
2945 DWORD flags, DDGAMMARAMP *gamma_ramp)
2947 IDirectDrawSurfaceImpl *surface = surface_from_gamma_control(iface);
2949 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
2953 WARN("Invalid gamma_ramp passed.\n");
2954 return DDERR_INVALIDPARAMS;
2957 EnterCriticalSection(&ddraw_cs);
2958 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2960 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP. */
2961 IWineD3DDevice_GetGammaRamp(surface->ddraw->wineD3DDevice, 0, (WINED3DGAMMARAMP *)gamma_ramp);
2965 ERR("Not implemented for non-primary surfaces.\n");
2967 LeaveCriticalSection(&ddraw_cs);
2972 /**********************************************************
2973 * IDirectDrawGammaControl::SetGammaRamp
2975 * Sets the red, green and blue gamma ramps for
2978 * flags: Can be DDSGR_CALIBRATE to request calibration
2979 * gamma_ramp: Structure containing the new gamma ramp
2983 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
2985 **********************************************************/
2986 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
2987 DWORD flags, DDGAMMARAMP *gamma_ramp)
2989 IDirectDrawSurfaceImpl *surface = surface_from_gamma_control(iface);
2991 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
2995 WARN("Invalid gamma_ramp passed.\n");
2996 return DDERR_INVALIDPARAMS;
2999 EnterCriticalSection(&ddraw_cs);
3000 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3002 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP */
3003 IWineD3DDevice_SetGammaRamp(surface->ddraw->wineD3DDevice, 0, flags, (WINED3DGAMMARAMP *)gamma_ramp);
3007 ERR("Not implemented for non-primary surfaces.\n");
3009 LeaveCriticalSection(&ddraw_cs);
3014 /*****************************************************************************
3015 * IDirect3DTexture2::PaletteChanged
3017 * Informs the texture about a palette change
3020 * start: Start index of the change
3021 * count: The number of changed entries
3024 * D3D_OK, because it's a stub
3026 *****************************************************************************/
3027 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
3029 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
3034 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
3036 IDirectDrawSurfaceImpl *surface = surface_from_texture1(iface);
3038 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
3040 return d3d_texture2_PaletteChanged((IDirect3DTexture2 *)&surface->IDirect3DTexture2_vtbl, start, count);
3043 /*****************************************************************************
3044 * IDirect3DTexture::Unload
3046 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
3052 *****************************************************************************/
3053 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
3055 WARN("iface %p. Not implemented.\n", iface);
3057 return DDERR_UNSUPPORTED;
3060 /*****************************************************************************
3061 * IDirect3DTexture2::GetHandle
3063 * Returns handle for the texture. At the moment, the interface
3064 * to the IWineD3DTexture is used.
3067 * device: Device this handle is assigned to
3068 * handle: Address to store the handle at.
3073 *****************************************************************************/
3074 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
3075 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
3077 IDirectDrawSurfaceImpl *surface = surface_from_texture2(iface);
3079 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
3081 EnterCriticalSection(&ddraw_cs);
3083 if (!surface->Handle)
3085 DWORD h = ddraw_allocate_handle(&device_from_device2(device)->handle_table, surface, DDRAW_HANDLE_SURFACE);
3086 if (h == DDRAW_INVALID_HANDLE)
3088 ERR("Failed to allocate a texture handle.\n");
3089 LeaveCriticalSection(&ddraw_cs);
3090 return DDERR_OUTOFMEMORY;
3093 surface->Handle = h + 1;
3096 TRACE("Returning handle %08x.\n", surface->Handle);
3097 *handle = surface->Handle;
3099 LeaveCriticalSection(&ddraw_cs);
3104 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
3105 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
3107 IDirect3DTexture2 *texture2 = (IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl;
3108 IDirect3DDevice2 *device2 = (IDirect3DDevice2 *)&device_from_device1(device)->IDirect3DDevice2_vtbl;
3110 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
3112 return d3d_texture2_GetHandle(texture2, device2, handle);
3115 /*****************************************************************************
3116 * get_sub_mimaplevel
3118 * Helper function that returns the next mipmap level
3120 * tex_ptr: Surface of which to return the next level
3122 *****************************************************************************/
3123 static IDirectDrawSurfaceImpl *get_sub_mimaplevel(IDirectDrawSurfaceImpl *surface)
3125 /* Now go down the mipmap chain to the next surface */
3126 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
3127 IDirectDrawSurface7 *next_level;
3130 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surface, &mipmap_caps, &next_level);
3131 if (FAILED(hr)) return NULL;
3133 ddraw_surface7_Release(next_level);
3135 return (IDirectDrawSurfaceImpl *)next_level;
3138 /*****************************************************************************
3139 * IDirect3DTexture2::Load
3141 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
3143 * This function isn't relayed to WineD3D because the whole interface is
3144 * implemented in DDraw only. For speed improvements a implementation which
3145 * takes OpenGL more into account could be placed into WineD3D.
3148 * src_texture: Address of the texture to load
3152 * D3DERR_TEXTURE_LOAD_FAILED.
3154 *****************************************************************************/
3155 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
3157 IDirectDrawSurfaceImpl *dst_surface = surface_from_texture2(iface);
3158 IDirectDrawSurfaceImpl *src_surface = surface_from_texture2(src_texture);
3161 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
3163 if (src_surface == dst_surface)
3165 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
3169 EnterCriticalSection(&ddraw_cs);
3171 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3172 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
3173 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
3175 ERR("Trying to load surfaces with different mip-map counts.\n");
3180 struct wined3d_palette *wined3d_dst_pal, *wined3d_src_pal;
3181 IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
3182 DDSURFACEDESC *src_desc, *dst_desc;
3184 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
3185 src_surface, dst_surface, src_surface->mipmap_level);
3187 /* Suppress the ALLOCONLOAD flag */
3188 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
3190 /* Get the palettes */
3191 hr = IWineD3DSurface_GetPalette(dst_surface->WineD3DSurface, &wined3d_dst_pal);
3194 ERR("Failed to get destination palette, hr %#x.\n", hr);
3195 LeaveCriticalSection(&ddraw_cs);
3196 return D3DERR_TEXTURE_LOAD_FAILED;
3198 if (wined3d_dst_pal)
3199 dst_pal = wined3d_palette_get_parent(wined3d_dst_pal);
3201 hr = IWineD3DSurface_GetPalette(src_surface->WineD3DSurface, &wined3d_src_pal);
3204 ERR("Failed to get source palette, hr %#x.\n", hr);
3205 LeaveCriticalSection(&ddraw_cs);
3206 return D3DERR_TEXTURE_LOAD_FAILED;
3208 if (wined3d_src_pal)
3209 src_pal = wined3d_palette_get_parent(wined3d_src_pal);
3213 PALETTEENTRY palent[256];
3217 LeaveCriticalSection(&ddraw_cs);
3218 return DDERR_NOPALETTEATTACHED;
3220 IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
3221 IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
3224 /* Copy one surface on the other */
3225 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
3226 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
3228 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
3230 /* Should also check for same pixel format, u1.lPitch, ... */
3231 ERR("Error in surface sizes.\n");
3232 LeaveCriticalSection(&ddraw_cs);
3233 return D3DERR_TEXTURE_LOAD_FAILED;
3237 WINED3DLOCKED_RECT src_rect, dst_rect;
3239 /* Copy also the ColorKeying stuff */
3240 if (src_desc->dwFlags & DDSD_CKSRCBLT)
3242 dst_desc->dwFlags |= DDSD_CKSRCBLT;
3243 dst_desc->ddckCKSrcBlt.dwColorSpaceLowValue = src_desc->ddckCKSrcBlt.dwColorSpaceLowValue;
3244 dst_desc->ddckCKSrcBlt.dwColorSpaceHighValue = src_desc->ddckCKSrcBlt.dwColorSpaceHighValue;
3247 /* Copy the main memory texture into the surface that corresponds
3248 * to the OpenGL texture object. */
3250 hr = IWineD3DSurface_Map(src_surface->WineD3DSurface, &src_rect, NULL, 0);
3253 ERR("Failed to lock source surface, hr %#x.\n", hr);
3254 LeaveCriticalSection(&ddraw_cs);
3255 return D3DERR_TEXTURE_LOAD_FAILED;
3258 hr = IWineD3DSurface_Map(dst_surface->WineD3DSurface, &dst_rect, NULL, 0);
3261 ERR("Failed to lock destination surface, hr %#x.\n", hr);
3262 IWineD3DSurface_Unmap(src_surface->WineD3DSurface);
3263 LeaveCriticalSection(&ddraw_cs);
3264 return D3DERR_TEXTURE_LOAD_FAILED;
3267 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
3268 memcpy(dst_rect.pBits, src_rect.pBits, src_surface->surface_desc.u1.dwLinearSize);
3270 memcpy(dst_rect.pBits, src_rect.pBits, src_rect.Pitch * src_desc->dwHeight);
3272 IWineD3DSurface_Unmap(src_surface->WineD3DSurface);
3273 IWineD3DSurface_Unmap(dst_surface->WineD3DSurface);
3276 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3277 src_surface = get_sub_mimaplevel(src_surface);
3281 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3282 dst_surface = get_sub_mimaplevel(dst_surface);
3286 if (!src_surface || !dst_surface)
3288 if (src_surface != dst_surface)
3289 ERR("Loading surface with different mipmap structure.\n");
3294 LeaveCriticalSection(&ddraw_cs);
3299 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
3301 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
3303 return d3d_texture2_Load((IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl,
3304 src_texture ? (IDirect3DTexture2 *)&surface_from_texture1(src_texture)->IDirect3DTexture2_vtbl : NULL);
3307 /*****************************************************************************
3309 *****************************************************************************/
3311 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
3314 ddraw_surface7_QueryInterface,
3315 ddraw_surface7_AddRef,
3316 ddraw_surface7_Release,
3317 /* IDirectDrawSurface */
3318 ddraw_surface7_AddAttachedSurface,
3319 ddraw_surface7_AddOverlayDirtyRect,
3321 ddraw_surface7_BltBatch,
3322 ddraw_surface7_BltFast,
3323 ddraw_surface7_DeleteAttachedSurface,
3324 ddraw_surface7_EnumAttachedSurfaces,
3325 ddraw_surface7_EnumOverlayZOrders,
3326 ddraw_surface7_Flip,
3327 ddraw_surface7_GetAttachedSurface,
3328 ddraw_surface7_GetBltStatus,
3329 ddraw_surface7_GetCaps,
3330 ddraw_surface7_GetClipper,
3331 ddraw_surface7_GetColorKey,
3332 ddraw_surface7_GetDC,
3333 ddraw_surface7_GetFlipStatus,
3334 ddraw_surface7_GetOverlayPosition,
3335 ddraw_surface7_GetPalette,
3336 ddraw_surface7_GetPixelFormat,
3337 ddraw_surface7_GetSurfaceDesc,
3338 ddraw_surface7_Initialize,
3339 ddraw_surface7_IsLost,
3340 ddraw_surface7_Lock,
3341 ddraw_surface7_ReleaseDC,
3342 ddraw_surface7_Restore,
3343 ddraw_surface7_SetClipper,
3344 ddraw_surface7_SetColorKey,
3345 ddraw_surface7_SetOverlayPosition,
3346 ddraw_surface7_SetPalette,
3347 ddraw_surface7_Unlock,
3348 ddraw_surface7_UpdateOverlay,
3349 ddraw_surface7_UpdateOverlayDisplay,
3350 ddraw_surface7_UpdateOverlayZOrder,
3351 /* IDirectDrawSurface2 */
3352 ddraw_surface7_GetDDInterface,
3353 ddraw_surface7_PageLock,
3354 ddraw_surface7_PageUnlock,
3355 /* IDirectDrawSurface3 */
3356 ddraw_surface7_SetSurfaceDesc,
3357 /* IDirectDrawSurface4 */
3358 ddraw_surface7_SetPrivateData,
3359 ddraw_surface7_GetPrivateData,
3360 ddraw_surface7_FreePrivateData,
3361 ddraw_surface7_GetUniquenessValue,
3362 ddraw_surface7_ChangeUniquenessValue,
3363 /* IDirectDrawSurface7 */
3364 ddraw_surface7_SetPriority,
3365 ddraw_surface7_GetPriority,
3366 ddraw_surface7_SetLOD,
3367 ddraw_surface7_GetLOD,
3370 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
3373 ddraw_surface3_QueryInterface,
3374 ddraw_surface3_AddRef,
3375 ddraw_surface3_Release,
3376 /* IDirectDrawSurface */
3377 ddraw_surface3_AddAttachedSurface,
3378 ddraw_surface3_AddOverlayDirtyRect,
3380 ddraw_surface3_BltBatch,
3381 ddraw_surface3_BltFast,
3382 ddraw_surface3_DeleteAttachedSurface,
3383 ddraw_surface3_EnumAttachedSurfaces,
3384 ddraw_surface3_EnumOverlayZOrders,
3385 ddraw_surface3_Flip,
3386 ddraw_surface3_GetAttachedSurface,
3387 ddraw_surface3_GetBltStatus,
3388 ddraw_surface3_GetCaps,
3389 ddraw_surface3_GetClipper,
3390 ddraw_surface3_GetColorKey,
3391 ddraw_surface3_GetDC,
3392 ddraw_surface3_GetFlipStatus,
3393 ddraw_surface3_GetOverlayPosition,
3394 ddraw_surface3_GetPalette,
3395 ddraw_surface3_GetPixelFormat,
3396 ddraw_surface3_GetSurfaceDesc,
3397 ddraw_surface3_Initialize,
3398 ddraw_surface3_IsLost,
3399 ddraw_surface3_Lock,
3400 ddraw_surface3_ReleaseDC,
3401 ddraw_surface3_Restore,
3402 ddraw_surface3_SetClipper,
3403 ddraw_surface3_SetColorKey,
3404 ddraw_surface3_SetOverlayPosition,
3405 ddraw_surface3_SetPalette,
3406 ddraw_surface3_Unlock,
3407 ddraw_surface3_UpdateOverlay,
3408 ddraw_surface3_UpdateOverlayDisplay,
3409 ddraw_surface3_UpdateOverlayZOrder,
3410 /* IDirectDrawSurface2 */
3411 ddraw_surface3_GetDDInterface,
3412 ddraw_surface3_PageLock,
3413 ddraw_surface3_PageUnlock,
3414 /* IDirectDrawSurface3 */
3415 ddraw_surface3_SetSurfaceDesc,
3418 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
3420 ddraw_gamma_control_QueryInterface,
3421 ddraw_gamma_control_AddRef,
3422 ddraw_gamma_control_Release,
3423 ddraw_gamma_control_GetGammaRamp,
3424 ddraw_gamma_control_SetGammaRamp,
3427 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
3429 d3d_texture2_QueryInterface,
3430 d3d_texture2_AddRef,
3431 d3d_texture2_Release,
3432 d3d_texture2_GetHandle,
3433 d3d_texture2_PaletteChanged,
3437 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
3439 d3d_texture1_QueryInterface,
3440 d3d_texture1_AddRef,
3441 d3d_texture1_Release,
3442 d3d_texture1_Initialize,
3443 d3d_texture1_GetHandle,
3444 d3d_texture1_PaletteChanged,
3446 d3d_texture1_Unload,
3449 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
3451 IDirectDrawSurfaceImpl *surface = parent;
3453 TRACE("surface %p.\n", surface);
3455 /* Check for attached surfaces and detach them. */
3456 if (surface->first_attached != surface)
3458 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)surface->first_attached;
3459 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)surface;
3461 /* Well, this shouldn't happen: The surface being attached is
3462 * referenced in AddAttachedSurface(), so it shouldn't be released
3463 * until DeleteAttachedSurface() is called, because the refcount is
3464 * held. It looks like the application released it often enough to
3466 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
3468 /* The refcount will drop to -1 here */
3469 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
3470 ERR("DeleteAttachedSurface failed.\n");
3473 while (surface->next_attached)
3475 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)surface;
3476 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)surface->next_attached;
3478 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
3479 ERR("DeleteAttachedSurface failed.\n");
3482 /* Having a texture handle set implies that the device still exists. */
3483 if (surface->Handle)
3484 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
3486 /* Reduce the ddraw surface count. */
3487 InterlockedDecrement(&surface->ddraw->surfaces);
3488 list_remove(&surface->surface_list_entry);
3490 HeapFree(GetProcessHeap(), 0, surface);
3493 const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
3495 ddraw_surface_wined3d_object_destroyed,
3498 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
3500 IDirectDrawSurfaceImpl *surface = parent;
3502 TRACE("surface %p.\n", surface);
3504 ddraw_surface_cleanup(surface);
3507 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
3509 ddraw_texture_wined3d_object_destroyed,
3512 HRESULT ddraw_surface_create_texture(IDirectDrawSurfaceImpl *surface)
3514 const DDSURFACEDESC2 *desc = &surface->surface_desc;
3515 enum wined3d_format_id format;
3519 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3520 levels = desc->u2.dwMipMapCount;
3524 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM.
3525 * Should I forward the MANAGED cap to the managed pool? */
3526 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
3527 pool = WINED3DPOOL_SYSTEMMEM;
3529 pool = WINED3DPOOL_DEFAULT;
3531 format = PixelFormat_DD2WineD3D(&surface->surface_desc.u4.ddpfPixelFormat);
3532 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3533 return IWineD3DDevice_CreateCubeTexture(surface->ddraw->wineD3DDevice, desc->dwWidth,
3534 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
3536 return IWineD3DDevice_CreateTexture(surface->ddraw->wineD3DDevice, desc->dwWidth, desc->dwHeight,
3537 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
3540 HRESULT ddraw_surface_init(IDirectDrawSurfaceImpl *surface, IDirectDrawImpl *ddraw,
3541 DDSURFACEDESC2 *desc, UINT mip_level, WINED3DSURFTYPE surface_type)
3543 struct wined3d_resource_desc wined3d_desc;
3544 struct wined3d_resource *wined3d_resource;
3545 WINED3DPOOL pool = WINED3DPOOL_DEFAULT;
3546 enum wined3d_format_id format;
3550 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
3551 && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3552 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
3554 /* Tests show surfaces without memory flags get these flags added
3555 * right after creation. */
3556 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
3559 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3561 usage |= WINED3DUSAGE_RENDERTARGET;
3562 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
3565 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && !(desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
3567 usage |= WINED3DUSAGE_RENDERTARGET;
3570 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
3572 usage |= WINED3DUSAGE_OVERLAY;
3575 if (ddraw->depthstencil || (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
3577 /* The depth stencil creation callback sets this flag. Set the
3578 * wined3d usage to let it know it's a depth/stencil surface. */
3579 usage |= WINED3DUSAGE_DEPTHSTENCIL;
3582 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
3584 pool = WINED3DPOOL_SYSTEMMEM;
3586 else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
3588 pool = WINED3DPOOL_MANAGED;
3589 /* Managed textures have the system memory flag set. */
3590 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
3592 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
3594 /* Videomemory adds localvidmem. This is mutually exclusive with
3595 * systemmemory and texturemanage. */
3596 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
3599 format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
3600 if (format == WINED3DFMT_UNKNOWN)
3602 WARN("Unsupported / unknown pixelformat.\n");
3603 return DDERR_INVALIDPIXELFORMAT;
3606 surface->lpVtbl = &ddraw_surface7_vtbl;
3607 surface->IDirectDrawSurface3_vtbl = &ddraw_surface3_vtbl;
3608 surface->IDirectDrawGammaControl_vtbl = &ddraw_gamma_control_vtbl;
3609 surface->IDirect3DTexture2_vtbl = &d3d_texture2_vtbl;
3610 surface->IDirect3DTexture_vtbl = &d3d_texture1_vtbl;
3612 surface->version = 7;
3613 surface->ddraw = ddraw;
3615 surface->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
3616 surface->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3617 DD_STRUCT_COPY_BYSIZE(&surface->surface_desc, desc);
3619 surface->first_attached = surface;
3620 surface->ImplType = surface_type;
3622 hr = IWineD3DDevice_CreateSurface(ddraw->wineD3DDevice, desc->dwWidth, desc->dwHeight, format,
3623 TRUE /* Lockable */, FALSE /* Discard */, mip_level, usage, pool,
3624 WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, surface_type, surface,
3625 &ddraw_surface_wined3d_parent_ops, &surface->WineD3DSurface);
3628 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
3632 surface->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
3633 wined3d_resource = IWineD3DSurface_GetResource(surface->WineD3DSurface);
3634 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
3636 format = wined3d_desc.format;
3637 if (format == WINED3DFMT_UNKNOWN)
3639 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN.\n");
3641 PixelFormat_WineD3DtoDD(&surface->surface_desc.u4.ddpfPixelFormat, format);
3643 /* Anno 1602 stores the pitch right after surface creation, so make sure
3644 * it's there. TODO: Test other fourcc formats. */
3645 if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
3646 || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
3648 surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
3649 if (format == WINED3DFMT_DXT1)
3651 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height) / 2;
3655 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height);
3660 surface->surface_desc.dwFlags |= DDSD_PITCH;
3661 surface->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch(surface->WineD3DSurface);
3664 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
3666 IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3667 DDCKEY_DESTOVERLAY, (WINEDDCOLORKEY *)&desc->u3.ddckCKDestOverlay);
3669 if (desc->dwFlags & DDSD_CKDESTBLT)
3671 IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3672 DDCKEY_DESTBLT, (WINEDDCOLORKEY *)&desc->ddckCKDestBlt);
3674 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
3676 IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3677 DDCKEY_SRCOVERLAY, (WINEDDCOLORKEY *)&desc->ddckCKSrcOverlay);
3679 if (desc->dwFlags & DDSD_CKSRCBLT)
3681 IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3682 DDCKEY_SRCBLT, (WINEDDCOLORKEY *)&desc->ddckCKSrcBlt);
3684 if (desc->dwFlags & DDSD_LPSURFACE)
3686 hr = IWineD3DSurface_SetMem(surface->WineD3DSurface, desc->lpSurface);
3689 ERR("Failed to set surface memory, hr %#x.\n", hr);
3690 IWineD3DSurface_Release(surface->WineD3DSurface);