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"
35 #define NONAMELESSUNION
41 #include "wine/exception.h"
46 #include "ddraw_private.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
51 /*****************************************************************************
52 * IUnknown parts follow
53 *****************************************************************************/
55 /*****************************************************************************
56 * IDirectDrawSurface7::QueryInterface
58 * A normal QueryInterface implementation. For QueryInterface rules
59 * see ddraw.c, IDirectDraw7::QueryInterface. This method
60 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
61 * in all versions, the IDirectDrawGammaControl interface and it can
62 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
65 * riid: The interface id queried for
66 * obj: Address to write the pointer to
70 * E_NOINTERFACE if the requested interface wasn't found
72 *****************************************************************************/
74 IDirectDrawSurfaceImpl_QueryInterface(IDirectDrawSurface7 *iface,
78 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
80 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
84 return DDERR_INVALIDPARAMS;
86 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),obj);
87 if (IsEqualGUID(riid, &IID_IUnknown)
88 || IsEqualGUID(riid, &IID_IDirectDrawSurface7)
89 || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
91 IUnknown_AddRef(iface);
92 *obj = ICOM_INTERFACE(This, IDirectDrawSurface7);
93 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
96 else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
97 || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
98 || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
100 IUnknown_AddRef(iface);
101 *obj = ICOM_INTERFACE(This, IDirectDrawSurface3);
102 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
105 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
107 IUnknown_AddRef(iface);
108 *obj = ICOM_INTERFACE(This, IDirectDrawGammaControl);
109 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
112 else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
113 IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
114 IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
116 IDirect3DDevice7 *d3d;
118 /* Call into IDirect3D7 for creation */
119 IDirect3D7_CreateDevice(ICOM_INTERFACE(This->ddraw, IDirect3D7),
121 ICOM_INTERFACE(This, IDirectDrawSurface7),
124 *obj = COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice7, IDirect3DDevice, d3d);
125 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
129 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
130 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
132 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
134 *obj = ICOM_INTERFACE(This, IDirect3DTexture);
135 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
139 *obj = ICOM_INTERFACE(This, IDirect3DTexture2);
140 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
142 IUnknown_AddRef( (IUnknown *) *obj);
146 ERR("No interface\n");
147 return E_NOINTERFACE;
150 /*****************************************************************************
151 * IDirectDrawSurface7::AddRef
153 * A normal addref implementation
158 *****************************************************************************/
160 IDirectDrawSurfaceImpl_AddRef(IDirectDrawSurface7 *iface)
162 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
163 ULONG refCount = InterlockedIncrement(&This->ref);
165 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
169 /*****************************************************************************
170 * IDirectDrawSurfaceImpl_Destroy
172 * A helper function for IDirectDrawSurface7::Release
174 * Frees the surface, regardless of its refcount.
175 * See IDirectDrawSurface7::Release for more information
178 * This: Surface to free
180 *****************************************************************************/
181 void IDirectDrawSurfaceImpl_Destroy(IDirectDrawSurfaceImpl *This)
183 TRACE("(%p)\n", This);
185 /* Check the refcount and give a warning */
188 /* This can happen when a complex surface is destroyed,
189 * because the 2nd surface was addref()ed when the app
190 * called GetAttachedSurface
192 WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
195 /* Check for attached surfaces and detach them */
196 if(This->first_attached != This)
198 /* Well, this shouldn't happen: The surface being attached is addref()ed
199 * in AddAttachedSurface, so it shouldn't be released until DeleteAttachedSurface
200 * is called, because the refcount is held. It looks like the app released()
201 * it often enough to force this
203 IDirectDrawSurface7 *root = ICOM_INTERFACE(This->first_attached, IDirectDrawSurface7);
204 IDirectDrawSurface7 *detach = ICOM_INTERFACE(This, IDirectDrawSurface7);
206 FIXME("(%p) Freeing a surface that is attached to surface %p\n", This, This->first_attached);
208 /* The refcount will drop to -1 here */
209 if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
211 ERR("(%p) DeleteAttachedSurface failed!\n", This);
215 while(This->next_attached != NULL)
217 IDirectDrawSurface7 *root = ICOM_INTERFACE(This, IDirectDrawSurface7);
218 IDirectDrawSurface7 *detach = ICOM_INTERFACE(This->next_attached, IDirectDrawSurface7);
220 if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
222 ERR("(%p) DeleteAttachedSurface failed!\n", This);
227 /* Now destroy the surface. Wait: It could have been released if we are a texture */
228 if(This->WineD3DSurface)
229 IWineD3DSurface_Release(This->WineD3DSurface);
231 /* Having a texture handle set implies that the device still exists */
234 This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
235 This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
238 /* Reduce the ddraw surface count */
239 InterlockedDecrement(&This->ddraw->surfaces);
240 list_remove(&This->surface_list_entry);
242 HeapFree(GetProcessHeap(), 0, This);
245 /*****************************************************************************
246 * IDirectDrawSurface7::Release
248 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
249 * surface is destroyed.
251 * Destroying the surface is a bit tricky. For the connection between
252 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
253 * It has a nice graph explaining the connection.
255 * What happens here is basically this:
256 * When a surface is destroyed, its WineD3DSurface is released,
257 * and the refcount of the DirectDraw interface is reduced by 1. If it has
258 * complex surfaces attached to it, then these surfaces are destroyed too,
259 * regardless of their refcount. If any surface being destroyed has another
260 * surface attached to it (with a "soft" attachment, not complex), then
261 * this surface is detached with DeleteAttachedSurface.
263 * When the surface is a texture, the WineD3DTexture is released.
264 * If the surface is the Direct3D render target, then the D3D
265 * capabilities of the WineD3DDevice are uninitialized, which causes the
266 * swapchain to be released.
268 * When a complex sublevel falls to ref zero, then this is ignored.
273 *****************************************************************************/
275 IDirectDrawSurfaceImpl_Release(IDirectDrawSurface7 *iface)
277 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
279 TRACE("(%p) : Releasing from %d\n", This, This->ref);
280 ref = InterlockedDecrement(&This->ref);
285 IDirectDrawSurfaceImpl *surf;
286 IDirectDrawImpl *ddraw;
287 IUnknown *ifaceToRelease = This->ifaceToRelease;
290 /* Complex attached surfaces are destroyed implicitely when the root is released */
291 EnterCriticalSection(&ddraw_cs);
292 if(!This->is_complex_root)
294 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
295 LeaveCriticalSection(&ddraw_cs);
300 /* If it's a texture, destroy the WineD3DTexture.
301 * WineD3D will destroy the IParent interfaces
302 * of the sublevels, which destroys the WineD3DSurfaces.
303 * Set the surfaces to NULL to avoid destroying them again later
305 if(This->wineD3DTexture)
307 IWineD3DBaseTexture_Release(This->wineD3DTexture);
309 /* If it's the RenderTarget, destroy the d3ddevice */
310 else if( (ddraw->d3d_initialized) && (This == ddraw->d3d_target))
312 TRACE("(%p) Destroying the render target, uninitializing D3D\n", This);
314 /* Unset any index buffer, just to be sure */
315 IWineD3DDevice_SetIndices(ddraw->wineD3DDevice, NULL);
316 IWineD3DDevice_SetDepthStencilSurface(ddraw->wineD3DDevice, NULL);
317 IWineD3DDevice_SetVertexDeclaration(ddraw->wineD3DDevice, NULL);
318 for(i = 0; i < ddraw->numConvertedDecls; i++)
320 IWineD3DVertexDeclaration_Release(ddraw->decls[i].decl);
322 HeapFree(GetProcessHeap(), 0, ddraw->decls);
323 ddraw->numConvertedDecls = 0;
325 if(IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain) != D3D_OK)
328 ERR("(%p) Failed to uninit 3D\n", This);
332 /* Free the d3d window if one was created */
333 if(ddraw->d3d_window != 0)
335 TRACE(" (%p) Destroying the hidden render window %p\n", This, ddraw->d3d_window);
336 DestroyWindow(ddraw->d3d_window);
337 ddraw->d3d_window = 0;
339 /* Unset the pointers */
342 ddraw->d3d_initialized = FALSE;
343 ddraw->d3d_target = NULL;
345 /* Reset to the default surface implementation type. This is needed if apps use
346 * non render target surfaces and expect blits to work after destroying the render
349 * TODO: Recreate existing offscreen surfaces
351 ddraw->ImplType = DefaultSurfaceType;
353 /* Write a trace because D3D unloading was the reason for many
354 * crashes during development.
356 TRACE("(%p) D3D unloaded\n", This);
358 else if(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
362 /* It's a render target, but no swapchain was created.
363 * The IParent interfaces have to be released manually.
364 * The same applies for textures without an
365 * IWineD3DTexture object attached
369 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
371 if(This->complex_array[i])
373 /* Only the topmost level can have more than 1 surfaces in the complex
374 * attachment array(Cube texture roots), for all others there is only
377 surf = This->complex_array[i];
380 IWineD3DSurface_GetParent(surf->WineD3DSurface,
381 (IUnknown **) &Parent);
382 IParent_Release(Parent); /* For the getParent */
383 IParent_Release(Parent); /* To release it */
384 surf = surf->complex_array[0];
389 /* Now the top-level surface */
390 IWineD3DSurface_GetParent(This->WineD3DSurface,
391 (IUnknown **) &Parent);
392 IParent_Release(Parent); /* For the getParent */
393 IParent_Release(Parent); /* To release it */
396 /* The refcount test shows that the palette is detached when the surface is destroyed */
397 IDirectDrawSurface7_SetPalette(ICOM_INTERFACE(This, IDirectDrawSurface7),
400 /* Loop through all complex attached surfaces,
403 * Yet again, only the root can have more than one complexly attached surface, all the others
404 * have a total of one;
406 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
408 if(!This->complex_array[i]) break;
410 surf = This->complex_array[i];
411 This->complex_array[i] = NULL;
414 IDirectDrawSurfaceImpl *destroy = surf;
415 surf = surf->complex_array[0]; /* Iterate through the "tree" */
416 IDirectDrawSurfaceImpl_Destroy(destroy); /* Destroy it */
420 /* Destroy the root surface.
422 IDirectDrawSurfaceImpl_Destroy(This);
424 /* Reduce the ddraw refcount */
425 if(ifaceToRelease) IUnknown_Release(ifaceToRelease);
426 LeaveCriticalSection(&ddraw_cs);
432 /*****************************************************************************
433 * IDirectDrawSurface7::GetAttachedSurface
435 * Returns an attached surface with the requested caps. Surface attachment
436 * and complex surfaces are not clearly described by the MSDN or sdk,
437 * so this method is tricky and likely to contain problems.
438 * This implementation searches the complex list first, then the
441 * The chains are searched from This down to the last surface in the chain,
442 * not from the first element in the chain. The first surface found is
443 * returned. The MSDN says that this method fails if more than one surface
444 * matches the caps, but it is not sure if that is right. The attachment
445 * structure may not even allow two matching surfaces.
447 * The found surface is AddRef-ed before it is returned.
450 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
451 * Surface: Address to store the found surface
455 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
456 * DDERR_NOTFOUND if no surface was found
458 *****************************************************************************/
459 static HRESULT WINAPI
460 IDirectDrawSurfaceImpl_GetAttachedSurface(IDirectDrawSurface7 *iface,
462 IDirectDrawSurface7 **Surface)
464 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
465 IDirectDrawSurfaceImpl *surf;
469 TRACE("(%p)->(%p,%p)\n", This, Caps, Surface);
470 EnterCriticalSection(&ddraw_cs);
474 if(This->version < 7)
476 /* Earlier dx apps put garbage into these members, clear them */
477 our_caps.dwCaps2 = 0;
478 our_caps.dwCaps3 = 0;
479 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 = ICOM_INTERFACE(surf, IDirectDrawSurface7);
510 IDirectDrawSurface7_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 = ICOM_INTERFACE(surf, IDirectDrawSurface7);
535 IDirectDrawSurface7_AddRef(*Surface);
536 LeaveCriticalSection(&ddraw_cs);
541 TRACE("(%p) Didn't find a valid surface\n", This);
542 LeaveCriticalSection(&ddraw_cs);
543 return DDERR_NOTFOUND;
546 /*****************************************************************************
547 * IDirectDrawSurface7::Lock
549 * Locks the surface and returns a pointer to the surface's memory
552 * Rect: Rectangle to lock. If NULL, the whole surface is locked
553 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
554 * Flags: Locking flags, e.g Read only or write only
555 * h: An event handle that's not used and must be NULL
559 * DDERR_INVALIDPARAMS if DDSD is NULL
560 * For more details, see IWineD3DSurface::LockRect
562 *****************************************************************************/
563 static HRESULT WINAPI
564 IDirectDrawSurfaceImpl_Lock(IDirectDrawSurface7 *iface,
566 DDSURFACEDESC2 *DDSD,
570 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
571 WINED3DLOCKED_RECT LockedRect;
573 TRACE("(%p)->(%p,%p,%x,%p)\n", This, Rect, DDSD, Flags, h);
576 return DDERR_INVALIDPARAMS;
578 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
579 EnterCriticalSection(&ddraw_cs);
581 /* Should I check for the handle to be NULL?
583 * The DDLOCK flags and the D3DLOCK flags are equal
584 * for the supported values. The others are ignored by WineD3D
587 if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
588 DDSD->dwSize != sizeof(DDSURFACEDESC2))
590 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
591 LeaveCriticalSection(&ddraw_cs);
592 return DDERR_INVALIDPARAMS;
595 /* Windows zeroes this if the rect is invalid */
602 || (Rect->left > Rect->right)
603 || (Rect->top > Rect->bottom)
604 || (Rect->right > This->surface_desc.dwWidth)
605 || (Rect->bottom > This->surface_desc.dwHeight))
607 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
608 LeaveCriticalSection(&ddraw_cs);
609 return DDERR_INVALIDPARAMS;
613 hr = IWineD3DSurface_LockRect(This->WineD3DSurface,
619 LeaveCriticalSection(&ddraw_cs);
622 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
623 * specific error. But since IWineD3DSurface::LockRect returns that error in this
624 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
625 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
626 * is much easier to do it in one place in ddraw
628 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
633 /* Override the memory area. The pitch should be set already. Strangely windows
634 * does not set the LPSURFACE flag on locked surfaces !?!.
635 * DDSD->dwFlags |= DDSD_LPSURFACE;
637 This->surface_desc.lpSurface = LockedRect.pBits;
638 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
640 TRACE("locked surface returning description :\n");
641 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
643 LeaveCriticalSection(&ddraw_cs);
647 /*****************************************************************************
648 * IDirectDrawSurface7::Unlock
650 * Unlocks an locked surface
653 * Rect: Not used by this implementation
657 * For more details, see IWineD3DSurface::UnlockRect
659 *****************************************************************************/
660 static HRESULT WINAPI
661 IDirectDrawSurfaceImpl_Unlock(IDirectDrawSurface7 *iface,
664 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
666 TRACE("(%p)->(%p)\n", This, pRect);
668 EnterCriticalSection(&ddraw_cs);
669 hr = IWineD3DSurface_UnlockRect(This->WineD3DSurface);
672 This->surface_desc.lpSurface = NULL;
674 LeaveCriticalSection(&ddraw_cs);
678 /*****************************************************************************
679 * IDirectDrawSurface7::Flip
681 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
682 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
683 * the flip target is passed to WineD3D, even if the app didn't specify one
686 * DestOverride: Specifies the surface that will become the new front
687 * buffer. If NULL, the current back buffer is used
688 * Flags: some DirectDraw flags, see include/ddraw.h
692 * DDERR_NOTFLIPPABLE if no flip target could be found
693 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
694 * For more details, see IWineD3DSurface::Flip
696 *****************************************************************************/
697 static HRESULT WINAPI
698 IDirectDrawSurfaceImpl_Flip(IDirectDrawSurface7 *iface,
699 IDirectDrawSurface7 *DestOverride,
702 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
703 IDirectDrawSurfaceImpl *Override = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DestOverride);
704 IDirectDrawSurface7 *Override7;
706 TRACE("(%p)->(%p,%x)\n", This, DestOverride, Flags);
708 /* Flip has to be called from a front buffer
709 * What about overlay surfaces, AFAIK they can flip too?
711 if( !(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER) )
712 return DDERR_INVALIDOBJECT; /* Unckecked */
714 EnterCriticalSection(&ddraw_cs);
716 /* WineD3D doesn't keep track of attached surface, so find the target */
721 memset(&Caps, 0, sizeof(Caps));
722 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
723 hr = IDirectDrawSurface7_GetAttachedSurface(iface, &Caps, &Override7);
726 ERR("Can't find a flip target\n");
727 LeaveCriticalSection(&ddraw_cs);
728 return DDERR_NOTFLIPPABLE; /* Unchecked */
730 Override = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Override7);
732 /* For the GetAttachedSurface */
733 IDirectDrawSurface7_Release(Override7);
736 hr = IWineD3DSurface_Flip(This->WineD3DSurface,
737 Override->WineD3DSurface,
739 LeaveCriticalSection(&ddraw_cs);
743 /*****************************************************************************
744 * IDirectDrawSurface7::Blt
746 * Performs a blit on the surface
749 * DestRect: Destination rectangle, can be NULL
750 * SrcSurface: Source surface, can be NULL
751 * SrcRect: Source rectangle, can be NULL
753 * DDBltFx: Some extended blt parameters, connected to the flags
757 * See IWineD3DSurface::Blt for more details
759 *****************************************************************************/
760 static HRESULT WINAPI
761 IDirectDrawSurfaceImpl_Blt(IDirectDrawSurface7 *iface,
763 IDirectDrawSurface7 *SrcSurface,
768 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
770 IDirectDrawSurfaceImpl *Src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, SrcSurface);
771 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
773 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
774 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
776 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
777 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
778 return DDERR_INVALIDPARAMS;
781 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
782 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
783 return DDERR_INVALIDPARAMS;
786 /* Sizes can change, therefore hold the lock when testing the rectangles */
787 EnterCriticalSection(&ddraw_cs);
790 if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
791 DestRect->right > This->surface_desc.dwWidth ||
792 DestRect->bottom > This->surface_desc.dwHeight)
794 WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
795 LeaveCriticalSection(&ddraw_cs);
796 return DDERR_INVALIDRECT;
801 if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
802 SrcRect->right > Src->surface_desc.dwWidth ||
803 SrcRect->bottom > Src->surface_desc.dwHeight)
805 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
806 LeaveCriticalSection(&ddraw_cs);
807 return DDERR_INVALIDRECT;
811 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
812 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
813 LeaveCriticalSection(&ddraw_cs);
814 return DDERR_INVALIDPARAMS;
817 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it does, copy the struct,
818 * and replace the ddraw surfaces with the wined3d surfaces
819 * So far no blitting operations using surfaces in the bltfx struct are supported anyway.
821 hr = IWineD3DSurface_Blt(This->WineD3DSurface,
823 Src ? Src->WineD3DSurface : NULL,
826 (WINEDDBLTFX *) DDBltFx,
829 LeaveCriticalSection(&ddraw_cs);
832 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
833 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
838 /*****************************************************************************
839 * IDirectDrawSurface7::AddAttachedSurface
841 * Attaches a surface to another surface. How the surface attachments work
842 * is not totally understood yet, and this method is prone to problems.
843 * he surface that is attached is AddRef-ed.
845 * Tests with complex surfaces suggest that the surface attachments form a
846 * tree, but no method to test this has been found yet.
848 * The attachment list consists of a first surface (first_attached) and
849 * for each surface a pointer to the next attached surface (next_attached).
850 * For the first surface, and a surface that has no attachments
851 * first_attached points to the surface itself. A surface that has
852 * no successors in the chain has next_attached set to NULL.
854 * Newly attached surfaces are attached right after the root surface.
855 * If a surface is attached to a complex surface compound, it's attached to
856 * the surface that the app requested, not the complex root. See
857 * GetAttachedSurface for a description how surfaces are found.
859 * This is how the current implementation works, and it was coded by looking
860 * at the needs of the applications.
862 * So far only Z-Buffer attachments are tested, and they are activated in
863 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
864 * Back buffers should work in 2D mode, but they are not tested(They can be
865 * attached in older iface versions). Rendering to the front buffer and
866 * switching between that and double buffering is not yet implemented in
867 * WineD3D, so for 3D it might have unexpected results.
869 * IDirectDrawSurfaceImpl_AddAttachedSurface is the real thing,
870 * IDirectDrawSurface7Impl_AddAttachedSurface is a wrapper around it that
871 * performs additional checks. Version 7 of this interface is much more restrictive
872 * than its predecessors.
875 * Attach: Surface to attach to iface
879 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
881 *****************************************************************************/
883 IDirectDrawSurfaceImpl_AddAttachedSurface(IDirectDrawSurfaceImpl *This,
884 IDirectDrawSurfaceImpl *Surf)
886 TRACE("(%p)->(%p)\n", This, Surf);
889 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
891 EnterCriticalSection(&ddraw_cs);
893 /* Check if the surface is already attached somewhere */
894 if( (Surf->next_attached != NULL) ||
895 (Surf->first_attached != Surf) )
897 /* TODO: Test for the structure of the manual attachment. Is it a chain or a list?
898 * What happens if one surface is attached to 2 different surfaces?
900 FIXME("(%p) The Surface %p is already attached somewhere else: next_attached = %p, first_attached = %p, can't handle by now\n", This, Surf, Surf->next_attached, Surf->first_attached);
901 LeaveCriticalSection(&ddraw_cs);
902 return DDERR_SURFACEALREADYATTACHED;
905 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
906 Surf->next_attached = This->next_attached;
907 Surf->first_attached = This->first_attached;
908 This->next_attached = Surf;
910 /* Check if the WineD3D depth stencil needs updating */
911 if(This->ddraw->d3ddevice)
913 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
917 * "This method increments the reference count of the surface being attached."
919 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(Surf, IDirectDrawSurface7));
920 LeaveCriticalSection(&ddraw_cs);
924 static HRESULT WINAPI
925 IDirectDrawSurface7Impl_AddAttachedSurface(IDirectDrawSurface7 *iface,
926 IDirectDrawSurface7 *Attach)
928 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
929 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Attach);
931 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
932 if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
935 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
936 Surf->surface_desc.ddsCaps.dwCaps);
937 return DDERR_CANNOTATTACHSURFACE;
940 return IDirectDrawSurfaceImpl_AddAttachedSurface(This,
943 /*****************************************************************************
944 * IDirectDrawSurface7::DeleteAttachedSurface
946 * Removes a surface from the attachment chain. The surface's refcount
947 * is decreased by one after it has been removed
950 * Flags: Some flags, not used by this implementation
951 * Attach: Surface to detach
955 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
957 *****************************************************************************/
958 static HRESULT WINAPI
959 IDirectDrawSurfaceImpl_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
961 IDirectDrawSurface7 *Attach)
963 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
964 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Attach);
965 IDirectDrawSurfaceImpl *Prev = This;
966 TRACE("(%p)->(%08x,%p)\n", This, Flags, Surf);
968 EnterCriticalSection(&ddraw_cs);
969 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
971 LeaveCriticalSection(&ddraw_cs);
972 return DDERR_CANNOTDETACHSURFACE;
975 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
976 if (This->surface_desc.ddsCaps.dwCaps &
977 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
979 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
980 /* FIXME: we should probably also subtract from dwMipMapCount of this
981 * and all parent surfaces */
984 /* Find the predecessor of the detached surface */
987 if(Prev->next_attached == Surf) break;
988 Prev = Prev->next_attached;
991 /* There must be a surface, otherwise there's a bug */
992 assert(Prev != NULL);
994 /* Unchain the surface */
995 Prev->next_attached = Surf->next_attached;
996 Surf->next_attached = NULL;
997 Surf->first_attached = Surf;
999 /* Check if the WineD3D depth stencil needs updating */
1000 if(This->ddraw->d3ddevice)
1002 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1005 IDirectDrawSurface7_Release(Attach);
1006 LeaveCriticalSection(&ddraw_cs);
1010 /*****************************************************************************
1011 * IDirectDrawSurface7::AddOverlayDirtyRect
1013 * "This method is not currently implemented"
1021 *****************************************************************************/
1022 static HRESULT WINAPI
1023 IDirectDrawSurfaceImpl_AddOverlayDirtyRect(IDirectDrawSurface7 *iface,
1026 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1027 TRACE("(%p)->(%p)\n",This,Rect);
1029 /* MSDN says it's not implemented. I could forward it to WineD3D,
1030 * then we'd implement it, but I don't think that's a good idea
1031 * (Stefan Dösinger)
1034 return IWineD3DSurface_AddOverlayDirtyRect(This->WineD3DSurface, pRect);
1036 return DDERR_UNSUPPORTED; /* unchecked */
1039 /*****************************************************************************
1040 * IDirectDrawSurface7::GetDC
1042 * Returns a GDI device context for the surface
1045 * hdc: Address of a HDC variable to store the dc to
1049 * DDERR_INVALIDPARAMS if hdc is NULL
1050 * For details, see IWineD3DSurface::GetDC
1052 *****************************************************************************/
1053 static HRESULT WINAPI
1054 IDirectDrawSurfaceImpl_GetDC(IDirectDrawSurface7 *iface,
1057 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1059 TRACE("(%p)->(%p): Relay\n", This, hdc);
1062 return DDERR_INVALIDPARAMS;
1064 EnterCriticalSection(&ddraw_cs);
1065 hr = IWineD3DSurface_GetDC(This->WineD3DSurface,
1067 LeaveCriticalSection(&ddraw_cs);
1071 /*****************************************************************************
1072 * IDirectDrawSurface7::ReleaseDC
1074 * Releases the DC that was constructed with GetDC
1077 * hdc: HDC to release
1081 * For more details, see IWineD3DSurface::ReleaseDC
1083 *****************************************************************************/
1084 static HRESULT WINAPI
1085 IDirectDrawSurfaceImpl_ReleaseDC(IDirectDrawSurface7 *iface,
1088 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1090 TRACE("(%p)->(%p): Relay\n", This, hdc);
1092 EnterCriticalSection(&ddraw_cs);
1093 hr = IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
1094 LeaveCriticalSection(&ddraw_cs);
1098 /*****************************************************************************
1099 * IDirectDrawSurface7::GetCaps
1101 * Returns the surface's caps
1104 * Caps: Address to write the caps to
1108 * DDERR_INVALIDPARAMS if Caps is NULL
1110 *****************************************************************************/
1111 static HRESULT WINAPI
1112 IDirectDrawSurfaceImpl_GetCaps(IDirectDrawSurface7 *iface,
1115 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1116 TRACE("(%p)->(%p)\n",This,Caps);
1119 return DDERR_INVALIDPARAMS;
1121 *Caps = This->surface_desc.ddsCaps;
1125 /*****************************************************************************
1126 * IDirectDrawSurface7::SetPriority
1128 * Sets a texture priority for managed textures.
1131 * Priority: The new priority
1135 * For more details, see IWineD3DSurface::SetPriority
1137 *****************************************************************************/
1138 static HRESULT WINAPI
1139 IDirectDrawSurfaceImpl_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1141 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1143 TRACE("(%p)->(%d): Relay!\n",This,Priority);
1145 EnterCriticalSection(&ddraw_cs);
1146 hr = IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
1147 LeaveCriticalSection(&ddraw_cs);
1151 /*****************************************************************************
1152 * IDirectDrawSurface7::GetPriority
1154 * Returns the surface's priority
1157 * Priority: Address of a variable to write the priority to
1161 * DDERR_INVALIDPARAMS if Priority == NULL
1162 * For more details, see IWineD3DSurface::GetPriority
1164 *****************************************************************************/
1165 static HRESULT WINAPI
1166 IDirectDrawSurfaceImpl_GetPriority(IDirectDrawSurface7 *iface,
1169 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1170 TRACE("(%p)->(%p): Relay\n",This,Priority);
1174 return DDERR_INVALIDPARAMS;
1177 EnterCriticalSection(&ddraw_cs);
1178 *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1179 LeaveCriticalSection(&ddraw_cs);
1183 /*****************************************************************************
1184 * IDirectDrawSurface7::SetPrivateData
1186 * Stores some data in the surface that is intended for the application's
1190 * tag: GUID that identifies the data
1191 * Data: Pointer to the private data
1192 * Size: Size of the private data
1197 * For more details, see IWineD3DSurface::SetPrivateData
1199 *****************************************************************************/
1200 static HRESULT WINAPI
1201 IDirectDrawSurfaceImpl_SetPrivateData(IDirectDrawSurface7 *iface,
1207 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1209 TRACE("(%p)->(%s,%p,%d,%x): Relay\n", This, debugstr_guid(tag), Data, Size, Flags);
1211 EnterCriticalSection(&ddraw_cs);
1212 hr = IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1217 LeaveCriticalSection(&ddraw_cs);
1220 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1225 /*****************************************************************************
1226 * IDirectDrawSurface7::GetPrivateData
1228 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1231 * tag: GUID of the data to return
1232 * Data: Address where to write the data to
1233 * Size: Size of the buffer at Data
1237 * DDERR_INVALIDPARAMS if Data is NULL
1238 * For more details, see IWineD3DSurface::GetPrivateData
1240 *****************************************************************************/
1241 static HRESULT WINAPI
1242 IDirectDrawSurfaceImpl_GetPrivateData(IDirectDrawSurface7 *iface,
1247 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1249 TRACE("(%p)->(%s,%p,%p): Relay\n", This, debugstr_guid(tag), Data, Size);
1252 return DDERR_INVALIDPARAMS;
1254 EnterCriticalSection(&ddraw_cs);
1255 hr = IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1259 LeaveCriticalSection(&ddraw_cs);
1263 /*****************************************************************************
1264 * IDirectDrawSurface7::FreePrivateData
1266 * Frees private data stored in the surface
1269 * tag: Tag of the data to free
1273 * For more details, see IWineD3DSurface::FreePrivateData
1275 *****************************************************************************/
1276 static HRESULT WINAPI
1277 IDirectDrawSurfaceImpl_FreePrivateData(IDirectDrawSurface7 *iface,
1280 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1282 TRACE("(%p)->(%s): Relay\n", This, debugstr_guid(tag));
1284 EnterCriticalSection(&ddraw_cs);
1285 hr = IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1286 LeaveCriticalSection(&ddraw_cs);
1290 /*****************************************************************************
1291 * IDirectDrawSurface7::PageLock
1293 * Prevents a sysmem surface from being paged out
1296 * Flags: Not used, must be 0(unchecked)
1299 * DD_OK, because it's a stub
1301 *****************************************************************************/
1302 static HRESULT WINAPI
1303 IDirectDrawSurfaceImpl_PageLock(IDirectDrawSurface7 *iface,
1306 TRACE("(%p)->(%x)\n", iface, Flags);
1308 /* This is Windows memory management related - we don't need this */
1312 /*****************************************************************************
1313 * IDirectDrawSurface7::PageUnlock
1315 * Allows a sysmem surface to be paged out
1318 * Flags: Not used, must be 0(unckeched)
1321 * DD_OK, because it's a stub
1323 *****************************************************************************/
1324 static HRESULT WINAPI
1325 IDirectDrawSurfaceImpl_PageUnlock(IDirectDrawSurface7 *iface,
1328 TRACE("(%p)->(%x)\n", iface, Flags);
1333 /*****************************************************************************
1334 * IDirectDrawSurface7::BltBatch
1336 * An unimplemented function
1344 *****************************************************************************/
1345 static HRESULT WINAPI IDirectDrawSurfaceImpl_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1347 TRACE("(%p)->(%p,%d,%08x)\n",iface,Batch,Count,Flags);
1349 /* MSDN: "not currently implemented" */
1350 return DDERR_UNSUPPORTED;
1353 /*****************************************************************************
1354 * IDirectDrawSurface7::EnumAttachedSurfaces
1356 * Enumerates all surfaces attached to this surface
1359 * context: Pointer to pass unmodified to the callback
1360 * cb: Callback function to call for each surface
1364 * DDERR_INVALIDPARAMS if cb is NULL
1366 *****************************************************************************/
1367 static HRESULT WINAPI
1368 IDirectDrawSurfaceImpl_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1370 LPDDENUMSURFACESCALLBACK7 cb)
1372 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1373 IDirectDrawSurfaceImpl *surf;
1374 DDSURFACEDESC2 desc;
1377 /* Attached surfaces aren't handled in WineD3D */
1378 TRACE("(%p)->(%p,%p)\n",This,context,cb);
1381 return DDERR_INVALIDPARAMS;
1383 EnterCriticalSection(&ddraw_cs);
1384 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1386 surf = This->complex_array[i];
1389 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
1390 desc = surf->surface_desc;
1391 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1392 if (cb(ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, context) == DDENUMRET_CANCEL)
1394 LeaveCriticalSection(&ddraw_cs);
1399 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1401 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
1402 desc = surf->surface_desc;
1403 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1404 if (cb( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, context) == DDENUMRET_CANCEL)
1406 LeaveCriticalSection(&ddraw_cs);
1411 TRACE(" end of enumeration.\n");
1413 LeaveCriticalSection(&ddraw_cs);
1417 /*****************************************************************************
1418 * IDirectDrawSurface7::EnumOverlayZOrders
1420 * "Enumerates the overlay surfaces on the specified destination"
1423 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
1424 * context: context to pass back to the callback
1425 * cb: callback function to call for each enumerated surface
1428 * DD_OK, because it's a stub
1430 *****************************************************************************/
1431 static HRESULT WINAPI
1432 IDirectDrawSurfaceImpl_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1435 LPDDENUMSURFACESCALLBACK7 cb)
1437 FIXME("(%p)->(%x,%p,%p): Stub!\n", iface, Flags, context, cb);
1442 /*****************************************************************************
1443 * IDirectDrawSurface7::GetBltStatus
1445 * Returns the blitting status
1448 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1451 * See IWineD3DSurface::Blt
1453 *****************************************************************************/
1454 static HRESULT WINAPI
1455 IDirectDrawSurfaceImpl_GetBltStatus(IDirectDrawSurface7 *iface,
1458 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1460 TRACE("(%p)->(%x): Relay\n", This, Flags);
1462 EnterCriticalSection(&ddraw_cs);
1463 hr = IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1464 LeaveCriticalSection(&ddraw_cs);
1467 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1472 /*****************************************************************************
1473 * IDirectDrawSurface7::GetColorKey
1475 * Returns the color key assigned to the surface
1479 * CKey: Address to store the key to
1483 * DDERR_INVALIDPARAMS if CKey is NULL
1485 *****************************************************************************/
1486 static HRESULT WINAPI
1487 IDirectDrawSurfaceImpl_GetColorKey(IDirectDrawSurface7 *iface,
1491 /* There is a DDERR_NOCOLORKEY error, but how do we know if a color key
1492 * isn't there? That's like saying that an int isn't there. (Which MS
1493 * has done in other docs.) */
1494 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1495 TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
1498 return DDERR_INVALIDPARAMS;
1500 EnterCriticalSection(&ddraw_cs);
1503 case DDCKEY_DESTBLT:
1504 *CKey = This->surface_desc.ddckCKDestBlt;
1507 case DDCKEY_DESTOVERLAY:
1508 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1512 *CKey = This->surface_desc.ddckCKSrcBlt;
1515 case DDCKEY_SRCOVERLAY:
1516 *CKey = This->surface_desc.ddckCKSrcOverlay;
1520 LeaveCriticalSection(&ddraw_cs);
1521 return DDERR_INVALIDPARAMS;
1524 LeaveCriticalSection(&ddraw_cs);
1528 /*****************************************************************************
1529 * IDirectDrawSurface7::GetFlipStatus
1531 * Returns the flipping status of the surface
1534 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1537 * See IWineD3DSurface::GetFlipStatus
1539 *****************************************************************************/
1540 static HRESULT WINAPI
1541 IDirectDrawSurfaceImpl_GetFlipStatus(IDirectDrawSurface7 *iface,
1544 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1546 TRACE("(%p)->(%x): Relay\n", This, Flags);
1548 EnterCriticalSection(&ddraw_cs);
1549 hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1550 LeaveCriticalSection(&ddraw_cs);
1553 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1558 /*****************************************************************************
1559 * IDirectDrawSurface7::GetOverlayPosition
1561 * Returns the display coordinates of a visible and active overlay surface
1568 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
1569 *****************************************************************************/
1570 static HRESULT WINAPI
1571 IDirectDrawSurfaceImpl_GetOverlayPosition(IDirectDrawSurface7 *iface,
1574 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1576 TRACE("(%p)->(%p,%p): Relay\n", This, X, Y);
1578 EnterCriticalSection(&ddraw_cs);
1579 hr = IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1582 LeaveCriticalSection(&ddraw_cs);
1586 /*****************************************************************************
1587 * IDirectDrawSurface7::GetPixelFormat
1589 * Returns the pixel format of the Surface
1592 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1593 * format should be written
1597 * DDERR_INVALIDPARAMS if PixelFormat is NULL
1599 *****************************************************************************/
1600 static HRESULT WINAPI
1601 IDirectDrawSurfaceImpl_GetPixelFormat(IDirectDrawSurface7 *iface,
1602 DDPIXELFORMAT *PixelFormat)
1604 /* What is DDERR_INVALIDSURFACETYPE for here? */
1605 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1606 TRACE("(%p)->(%p)\n",This,PixelFormat);
1609 return DDERR_INVALIDPARAMS;
1611 EnterCriticalSection(&ddraw_cs);
1612 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1613 LeaveCriticalSection(&ddraw_cs);
1619 /*****************************************************************************
1620 * IDirectDrawSurface7::GetSurfaceDesc
1622 * Returns the description of this surface
1625 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1630 * DDERR_INVALIDPARAMS if DDSD is NULL
1632 *****************************************************************************/
1633 static HRESULT WINAPI
1634 IDirectDrawSurfaceImpl_GetSurfaceDesc(IDirectDrawSurface7 *iface,
1635 DDSURFACEDESC2 *DDSD)
1637 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1639 TRACE("(%p)->(%p)\n",This,DDSD);
1642 return DDERR_INVALIDPARAMS;
1644 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1646 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1647 return DDERR_INVALIDPARAMS;
1650 EnterCriticalSection(&ddraw_cs);
1651 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1652 TRACE("Returning surface desc:\n");
1653 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1655 LeaveCriticalSection(&ddraw_cs);
1659 /*****************************************************************************
1660 * IDirectDrawSurface7::Initialize
1662 * Initializes the surface. This is a no-op in Wine
1665 * DD: Pointer to an DirectDraw interface
1666 * DDSD: Surface description for initialization
1669 * DDERR_ALREADYINITIALIZED
1671 *****************************************************************************/
1672 static HRESULT WINAPI
1673 IDirectDrawSurfaceImpl_Initialize(IDirectDrawSurface7 *iface,
1675 DDSURFACEDESC2 *DDSD)
1677 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1678 IDirectDrawImpl *ddimpl = ICOM_OBJECT(IDirectDrawImpl, IDirectDraw, DD);
1679 TRACE("(%p)->(%p,%p)\n",This,ddimpl,DDSD);
1681 return DDERR_ALREADYINITIALIZED;
1684 /*****************************************************************************
1685 * IDirectDrawSurface7::IsLost
1687 * Checks if the surface is lost
1690 * DD_OK, if the surface is useable
1691 * DDERR_ISLOST if the surface is lost
1692 * See IWineD3DSurface::IsLost for more details
1694 *****************************************************************************/
1695 static HRESULT WINAPI
1696 IDirectDrawSurfaceImpl_IsLost(IDirectDrawSurface7 *iface)
1698 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1700 TRACE("(%p)\n", This);
1702 EnterCriticalSection(&ddraw_cs);
1703 /* We lose the surface if the implementation was changed */
1704 if(This->ImplType != This->ddraw->ImplType)
1706 /* But this shouldn't happen. When we change the implementation,
1707 * all surfaces are re-created automatically, and their content
1710 ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1711 LeaveCriticalSection(&ddraw_cs);
1712 return DDERR_SURFACELOST;
1715 hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
1716 LeaveCriticalSection(&ddraw_cs);
1719 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1720 * WineD3D uses the same error for surfaces
1722 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
1727 /*****************************************************************************
1728 * IDirectDrawSurface7::Restore
1730 * Restores a lost surface. This makes the surface usable again, but
1731 * doesn't reload its old contents
1735 * See IWineD3DSurface::Restore for more details
1737 *****************************************************************************/
1738 static HRESULT WINAPI
1739 IDirectDrawSurfaceImpl_Restore(IDirectDrawSurface7 *iface)
1741 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1743 TRACE("(%p)\n", This);
1745 EnterCriticalSection(&ddraw_cs);
1746 if(This->ImplType != This->ddraw->ImplType)
1748 /* Call the recreation callback. Make sure to AddRef first */
1749 IDirectDrawSurface_AddRef(iface);
1750 IDirectDrawImpl_RecreateSurfacesCallback(iface,
1751 &This->surface_desc,
1752 NULL /* Not needed */);
1754 hr = IWineD3DSurface_Restore(This->WineD3DSurface);
1755 LeaveCriticalSection(&ddraw_cs);
1759 /*****************************************************************************
1760 * IDirectDrawSurface7::SetOverlayPosition
1762 * Changes the display coordinates of an overlay surface
1769 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
1770 *****************************************************************************/
1771 static HRESULT WINAPI
1772 IDirectDrawSurfaceImpl_SetOverlayPosition(IDirectDrawSurface7 *iface,
1776 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1778 TRACE("(%p)->(%d,%d): Relay\n", This, X, Y);
1780 EnterCriticalSection(&ddraw_cs);
1781 hr = IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
1784 LeaveCriticalSection(&ddraw_cs);
1788 /*****************************************************************************
1789 * IDirectDrawSurface7::UpdateOverlay
1791 * Modifies the attributes of an overlay surface.
1794 * SrcRect: The section of the source being used for the overlay
1795 * DstSurface: Address of the surface that is overlaid
1796 * DstRect: Place of the overlay
1797 * Flags: some DDOVER_* flags
1800 * DDERR_UNSUPPORTED, because we don't support overlays
1802 *****************************************************************************/
1803 static HRESULT WINAPI
1804 IDirectDrawSurfaceImpl_UpdateOverlay(IDirectDrawSurface7 *iface,
1806 IDirectDrawSurface7 *DstSurface,
1811 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1812 IDirectDrawSurfaceImpl *Dst = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DstSurface);
1814 TRACE("(%p)->(%p,%p,%p,%x,%p): Relay\n", This, SrcRect, Dst, DstRect, Flags, FX);
1816 EnterCriticalSection(&ddraw_cs);
1817 hr = IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
1819 Dst ? Dst->WineD3DSurface : NULL,
1822 (WINEDDOVERLAYFX *) FX);
1823 LeaveCriticalSection(&ddraw_cs);
1827 /*****************************************************************************
1828 * IDirectDrawSurface7::UpdateOverlayDisplay
1830 * The DX7 sdk says that it's not implemented
1835 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
1837 *****************************************************************************/
1838 static HRESULT WINAPI
1839 IDirectDrawSurfaceImpl_UpdateOverlayDisplay(IDirectDrawSurface7 *iface,
1842 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1843 TRACE("(%p)->(%x)\n", This, Flags);
1844 return DDERR_UNSUPPORTED;
1847 /*****************************************************************************
1848 * IDirectDrawSurface7::UpdateOverlayZOrder
1850 * Sets an overlay's Z order
1853 * Flags: DDOVERZ_* flags
1854 * DDSRef: Defines the relative position in the overlay chain
1857 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
1859 *****************************************************************************/
1860 static HRESULT WINAPI
1861 IDirectDrawSurfaceImpl_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
1863 IDirectDrawSurface7 *DDSRef)
1865 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1867 IDirectDrawSurfaceImpl *Ref = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DDSRef);
1869 TRACE("(%p)->(%x,%p): Relay\n", This, Flags, Ref);
1870 EnterCriticalSection(&ddraw_cs);
1871 hr = IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
1873 Ref ? Ref->WineD3DSurface : NULL);
1874 LeaveCriticalSection(&ddraw_cs);
1878 /*****************************************************************************
1879 * IDirectDrawSurface7::GetDDInterface
1881 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
1882 * surface belongs to
1885 * DD: Address to write the interface pointer to
1889 * DDERR_INVALIDPARAMS if DD is NULL
1891 *****************************************************************************/
1892 static HRESULT WINAPI
1893 IDirectDrawSurfaceImpl_GetDDInterface(IDirectDrawSurface7 *iface,
1896 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1898 TRACE("(%p)->(%p)\n",This,DD);
1901 return DDERR_INVALIDPARAMS;
1903 switch(This->version)
1906 *((IDirectDraw7 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw7);
1907 IDirectDraw7_AddRef(*(IDirectDraw7 **) DD);
1911 *((IDirectDraw4 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw4);
1912 IDirectDraw4_AddRef(*(IDirectDraw4 **) DD);
1916 *((IDirectDraw2 **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw2);
1917 IDirectDraw_AddRef( *(IDirectDraw2 **) DD);
1921 *((IDirectDraw **) DD) = ICOM_INTERFACE(This->ddraw, IDirectDraw);
1922 IDirectDraw_AddRef( *(IDirectDraw **) DD);
1930 /* This seems also windows implementation specific - I don't think WineD3D needs this */
1931 static HRESULT WINAPI IDirectDrawSurfaceImpl_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
1933 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1934 volatile IDirectDrawSurfaceImpl* vThis = This;
1936 TRACE("(%p)\n",This);
1937 EnterCriticalSection(&ddraw_cs);
1938 /* A uniqueness value of 0 is apparently special.
1939 * This needs to be checked.
1940 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
1943 DWORD old_uniqueness_value = vThis->uniqueness_value;
1944 DWORD new_uniqueness_value = old_uniqueness_value+1;
1946 if (old_uniqueness_value == 0) break;
1947 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
1949 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
1950 old_uniqueness_value,
1951 new_uniqueness_value)
1952 == old_uniqueness_value)
1956 LeaveCriticalSection(&ddraw_cs);
1960 static HRESULT WINAPI IDirectDrawSurfaceImpl_GetUniquenessValue(IDirectDrawSurface7 *iface, LPDWORD pValue)
1962 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1964 TRACE("(%p)->(%p)\n",This,pValue);
1965 EnterCriticalSection(&ddraw_cs);
1966 *pValue = This->uniqueness_value;
1967 LeaveCriticalSection(&ddraw_cs);
1971 /*****************************************************************************
1972 * IDirectDrawSurface7::SetLOD
1974 * Sets the level of detail of a texture
1977 * MaxLOD: LOD to set
1981 * DDERR_INVALIDOBJECT if the surface is invalid for this method
1983 *****************************************************************************/
1984 static HRESULT WINAPI
1985 IDirectDrawSurfaceImpl_SetLOD(IDirectDrawSurface7 *iface,
1988 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
1990 TRACE("(%p)->(%d)\n", This, MaxLOD);
1992 EnterCriticalSection(&ddraw_cs);
1993 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
1995 LeaveCriticalSection(&ddraw_cs);
1996 return DDERR_INVALIDOBJECT;
1999 if(!This->wineD3DTexture)
2001 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2002 LeaveCriticalSection(&ddraw_cs);
2003 return DDERR_INVALIDOBJECT;
2006 hr = IWineD3DBaseTexture_SetLOD(This->wineD3DTexture,
2008 LeaveCriticalSection(&ddraw_cs);
2012 /*****************************************************************************
2013 * IDirectDrawSurface7::GetLOD
2015 * Returns the level of detail of a Direct3D texture
2018 * MaxLOD: Address to write the LOD to
2022 * DDERR_INVALIDPARAMS if MaxLOD is NULL
2023 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2025 *****************************************************************************/
2026 static HRESULT WINAPI
2027 IDirectDrawSurfaceImpl_GetLOD(IDirectDrawSurface7 *iface,
2030 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2031 TRACE("(%p)->(%p)\n", This, MaxLOD);
2034 return DDERR_INVALIDPARAMS;
2036 EnterCriticalSection(&ddraw_cs);
2037 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2039 LeaveCriticalSection(&ddraw_cs);
2040 return DDERR_INVALIDOBJECT;
2043 *MaxLOD = IWineD3DBaseTexture_GetLOD(This->wineD3DTexture);
2044 LeaveCriticalSection(&ddraw_cs);
2048 /*****************************************************************************
2049 * IDirectDrawSurface7::BltFast
2051 * Performs a fast Blit.
2054 * dstx: The x coordinate to blit to on the destination
2055 * dsty: The y coordinate to blit to on the destination
2056 * Source: The source surface
2057 * rsrc: The source rectangle
2058 * trans: Type of transfer. Some DDBLTFAST_* flags
2062 * For more details, see IWineD3DSurface::BltFast
2064 *****************************************************************************/
2065 static HRESULT WINAPI
2066 IDirectDrawSurfaceImpl_BltFast(IDirectDrawSurface7 *iface,
2069 IDirectDrawSurface7 *Source,
2073 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2075 IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Source);
2076 TRACE("(%p)->(%d,%d,%p,%p,%d): Relay\n", This, dstx, dsty, Source, rsrc, trans);
2078 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2083 if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2084 rsrc->right > src->surface_desc.dwWidth ||
2085 rsrc->bottom > src->surface_desc.dwHeight)
2087 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2088 return DDERR_INVALIDRECT;
2090 if(dstx + rsrc->right - rsrc->left > This->surface_desc.dwWidth ||
2091 dsty + rsrc->bottom - rsrc->top > This->surface_desc.dwHeight)
2093 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT\n");
2094 return DDERR_INVALIDRECT;
2099 if(dstx + src->surface_desc.dwWidth > This->surface_desc.dwWidth ||
2100 dsty + src->surface_desc.dwHeight > This->surface_desc.dwHeight)
2102 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT\n");
2103 return DDERR_INVALIDRECT;
2107 EnterCriticalSection(&ddraw_cs);
2108 hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
2110 src ? src->WineD3DSurface : NULL,
2113 LeaveCriticalSection(&ddraw_cs);
2116 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
2117 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
2122 /*****************************************************************************
2123 * IDirectDrawSurface7::GetClipper
2125 * Returns the IDirectDrawClipper interface of the clipper assigned to this
2129 * Clipper: Address to store the interface pointer at
2133 * DDERR_INVALIDPARAMS if Clipper is NULL
2134 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
2136 *****************************************************************************/
2137 static HRESULT WINAPI
2138 IDirectDrawSurfaceImpl_GetClipper(IDirectDrawSurface7 *iface,
2139 IDirectDrawClipper **Clipper)
2141 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2142 TRACE("(%p)->(%p)\n", This, Clipper);
2146 LeaveCriticalSection(&ddraw_cs);
2147 return DDERR_INVALIDPARAMS;
2150 EnterCriticalSection(&ddraw_cs);
2151 if(This->clipper == NULL)
2153 LeaveCriticalSection(&ddraw_cs);
2154 return DDERR_NOCLIPPERATTACHED;
2157 *Clipper = ICOM_INTERFACE(This->clipper, IDirectDrawClipper);
2158 IDirectDrawClipper_AddRef(*Clipper);
2159 LeaveCriticalSection(&ddraw_cs);
2163 /*****************************************************************************
2164 * IDirectDrawSurface7::SetClipper
2166 * Sets a clipper for the surface
2169 * Clipper: IDirectDrawClipper interface of the clipper to set
2174 *****************************************************************************/
2175 static HRESULT WINAPI
2176 IDirectDrawSurfaceImpl_SetClipper(IDirectDrawSurface7 *iface,
2177 IDirectDrawClipper *Clipper)
2179 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2180 IDirectDrawClipperImpl *oldClipper = This->clipper;
2182 TRACE("(%p)->(%p)\n",This,Clipper);
2184 EnterCriticalSection(&ddraw_cs);
2185 if (ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper) == This->clipper)
2187 LeaveCriticalSection(&ddraw_cs);
2191 This->clipper = ICOM_OBJECT(IDirectDrawClipperImpl, IDirectDrawClipper, Clipper);
2193 if (Clipper != NULL)
2194 IDirectDrawClipper_AddRef(Clipper);
2196 IDirectDrawClipper_Release(ICOM_INTERFACE(oldClipper, IDirectDrawClipper));
2198 hr = IWineD3DSurface_SetClipper(This->WineD3DSurface, This->clipper ? This->clipper->wineD3DClipper : NULL);
2199 LeaveCriticalSection(&ddraw_cs);
2203 /*****************************************************************************
2204 * IDirectDrawSurface7::SetSurfaceDesc
2206 * Sets the surface description. It can override the pixel format, the surface
2208 * It's not really tested.
2211 * DDSD: Pointer to the new surface description to set
2216 * DDERR_INVALIDPARAMS if DDSD is NULL
2218 *****************************************************************************/
2219 static HRESULT WINAPI
2220 IDirectDrawSurfaceImpl_SetSurfaceDesc(IDirectDrawSurface7 *iface,
2221 DDSURFACEDESC2 *DDSD,
2224 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2225 WINED3DFORMAT newFormat = WINED3DFMT_UNKNOWN;
2227 TRACE("(%p)->(%p,%x)\n", This, DDSD, Flags);
2230 return DDERR_INVALIDPARAMS;
2232 EnterCriticalSection(&ddraw_cs);
2233 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2235 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2237 if(newFormat == WINED3DFMT_UNKNOWN)
2239 ERR("Requested to set an unknown pixelformat\n");
2240 LeaveCriticalSection(&ddraw_cs);
2241 return DDERR_INVALIDPARAMS;
2243 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2245 hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2249 LeaveCriticalSection(&ddraw_cs);
2254 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2256 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2258 (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2260 if (DDSD->dwFlags & DDSD_CKDESTBLT)
2262 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2264 (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2266 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2268 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2270 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2272 if (DDSD->dwFlags & DDSD_CKSRCBLT)
2274 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2276 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2278 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2280 hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2281 if(hr != WINED3D_OK)
2283 /* No need for a trace here, wined3d does that for us */
2284 LeaveCriticalSection(&ddraw_cs);
2287 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2288 default: break; /* Go on */
2294 This->surface_desc = *DDSD;
2296 LeaveCriticalSection(&ddraw_cs);
2300 /*****************************************************************************
2301 * IDirectDrawSurface7::GetPalette
2303 * Returns the IDirectDrawPalette interface of the palette currently assigned
2307 * Pal: Address to write the interface pointer to
2311 * DDERR_INVALIDPARAMS if Pal is NULL
2313 *****************************************************************************/
2314 static HRESULT WINAPI
2315 IDirectDrawSurfaceImpl_GetPalette(IDirectDrawSurface7 *iface,
2316 IDirectDrawPalette **Pal)
2318 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2319 IWineD3DPalette *wPal;
2321 TRACE("(%p)->(%p): Relay\n", This, Pal);
2324 return DDERR_INVALIDPARAMS;
2326 EnterCriticalSection(&ddraw_cs);
2327 hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
2330 LeaveCriticalSection(&ddraw_cs);
2336 hr = IWineD3DPalette_GetParent(wPal, (IUnknown **) Pal);
2341 hr = DDERR_NOPALETTEATTACHED;
2344 LeaveCriticalSection(&ddraw_cs);
2348 /*****************************************************************************
2351 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2352 * recursively in the surface tree
2354 *****************************************************************************/
2358 WINEDDCOLORKEY *CKey;
2362 static HRESULT WINAPI
2363 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2364 DDSURFACEDESC2 *desc,
2367 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
2368 struct SCKContext *ctx = context;
2371 hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2376 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2380 IDirectDrawSurface7_EnumAttachedSurfaces(surface,
2383 IDirectDrawSurface7_Release(surface);
2384 return DDENUMRET_OK;
2387 /*****************************************************************************
2388 * IDirectDrawSurface7::SetColorKey
2390 * Sets the color keying options for the surface. Observations showed that
2391 * in case of complex surfaces the color key has to be assigned to all
2396 * CKey: The new color key
2400 * See IWineD3DSurface::SetColorKey for details
2402 *****************************************************************************/
2403 static HRESULT WINAPI
2404 IDirectDrawSurfaceImpl_SetColorKey(IDirectDrawSurface7 *iface,
2408 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2409 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) CKey, Flags };
2410 TRACE("(%p)->(%x,%p)\n", This, Flags, CKey);
2412 EnterCriticalSection(&ddraw_cs);
2415 switch (Flags & ~DDCKEY_COLORSPACE)
2417 case DDCKEY_DESTBLT:
2418 This->surface_desc.ddckCKDestBlt = *CKey;
2419 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2422 case DDCKEY_DESTOVERLAY:
2423 This->surface_desc.u3.ddckCKDestOverlay = *CKey;
2424 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2427 case DDCKEY_SRCOVERLAY:
2428 This->surface_desc.ddckCKSrcOverlay = *CKey;
2429 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2433 This->surface_desc.ddckCKSrcBlt = *CKey;
2434 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2438 LeaveCriticalSection(&ddraw_cs);
2439 return DDERR_INVALIDPARAMS;
2444 switch (Flags & ~DDCKEY_COLORSPACE)
2446 case DDCKEY_DESTBLT:
2447 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2450 case DDCKEY_DESTOVERLAY:
2451 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2454 case DDCKEY_SRCOVERLAY:
2455 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2459 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2463 LeaveCriticalSection(&ddraw_cs);
2464 return DDERR_INVALIDPARAMS;
2467 ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2470 IDirectDrawSurface7_EnumAttachedSurfaces(iface,
2473 LeaveCriticalSection(&ddraw_cs);
2476 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2477 default: return ctx.ret;
2481 /*****************************************************************************
2482 * IDirectDrawSurface7::SetPalette
2484 * Assigns a DirectDrawPalette object to the surface
2487 * Pal: Interface to the palette to set
2492 *****************************************************************************/
2493 static HRESULT WINAPI
2494 IDirectDrawSurfaceImpl_SetPalette(IDirectDrawSurface7 *iface,
2495 IDirectDrawPalette *Pal)
2497 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirectDrawSurface7, iface);
2498 IDirectDrawPalette *oldPal;
2499 IDirectDrawSurfaceImpl *surf;
2500 IDirectDrawPaletteImpl *PalImpl = ICOM_OBJECT(IDirectDrawPaletteImpl, IDirectDrawPalette, Pal);
2502 TRACE("(%p)->(%p)\n", This, Pal);
2504 /* Find the old palette */
2505 EnterCriticalSection(&ddraw_cs);
2506 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2507 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2509 LeaveCriticalSection(&ddraw_cs);
2512 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
2514 /* Set the new Palette */
2515 IWineD3DSurface_SetPalette(This->WineD3DSurface,
2516 PalImpl ? PalImpl->wineD3DPalette : NULL);
2517 /* AddRef the Palette */
2518 if(Pal) IDirectDrawPalette_AddRef(Pal);
2520 /* Release the old palette */
2521 if(oldPal) IDirectDrawPalette_Release(oldPal);
2523 /* If this is a front buffer, also update the back buffers
2524 * TODO: How do things work for palettized cube textures?
2526 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2528 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2529 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2534 IDirectDrawSurface7 *attach;
2536 hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
2543 TRACE("Setting palette on %p\n", attach);
2544 IDirectDrawSurface7_SetPalette(attach,
2546 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attach);
2547 IDirectDrawSurface7_Release(attach);
2551 LeaveCriticalSection(&ddraw_cs);
2555 /*****************************************************************************
2557 *****************************************************************************/
2559 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl =
2562 IDirectDrawSurfaceImpl_QueryInterface,
2563 IDirectDrawSurfaceImpl_AddRef,
2564 IDirectDrawSurfaceImpl_Release,
2565 /*** IDirectDrawSurface ***/
2566 IDirectDrawSurface7Impl_AddAttachedSurface,
2567 IDirectDrawSurfaceImpl_AddOverlayDirtyRect,
2568 IDirectDrawSurfaceImpl_Blt,
2569 IDirectDrawSurfaceImpl_BltBatch,
2570 IDirectDrawSurfaceImpl_BltFast,
2571 IDirectDrawSurfaceImpl_DeleteAttachedSurface,
2572 IDirectDrawSurfaceImpl_EnumAttachedSurfaces,
2573 IDirectDrawSurfaceImpl_EnumOverlayZOrders,
2574 IDirectDrawSurfaceImpl_Flip,
2575 IDirectDrawSurfaceImpl_GetAttachedSurface,
2576 IDirectDrawSurfaceImpl_GetBltStatus,
2577 IDirectDrawSurfaceImpl_GetCaps,
2578 IDirectDrawSurfaceImpl_GetClipper,
2579 IDirectDrawSurfaceImpl_GetColorKey,
2580 IDirectDrawSurfaceImpl_GetDC,
2581 IDirectDrawSurfaceImpl_GetFlipStatus,
2582 IDirectDrawSurfaceImpl_GetOverlayPosition,
2583 IDirectDrawSurfaceImpl_GetPalette,
2584 IDirectDrawSurfaceImpl_GetPixelFormat,
2585 IDirectDrawSurfaceImpl_GetSurfaceDesc,
2586 IDirectDrawSurfaceImpl_Initialize,
2587 IDirectDrawSurfaceImpl_IsLost,
2588 IDirectDrawSurfaceImpl_Lock,
2589 IDirectDrawSurfaceImpl_ReleaseDC,
2590 IDirectDrawSurfaceImpl_Restore,
2591 IDirectDrawSurfaceImpl_SetClipper,
2592 IDirectDrawSurfaceImpl_SetColorKey,
2593 IDirectDrawSurfaceImpl_SetOverlayPosition,
2594 IDirectDrawSurfaceImpl_SetPalette,
2595 IDirectDrawSurfaceImpl_Unlock,
2596 IDirectDrawSurfaceImpl_UpdateOverlay,
2597 IDirectDrawSurfaceImpl_UpdateOverlayDisplay,
2598 IDirectDrawSurfaceImpl_UpdateOverlayZOrder,
2599 /*** IDirectDrawSurface2 ***/
2600 IDirectDrawSurfaceImpl_GetDDInterface,
2601 IDirectDrawSurfaceImpl_PageLock,
2602 IDirectDrawSurfaceImpl_PageUnlock,
2603 /*** IDirectDrawSurface3 ***/
2604 IDirectDrawSurfaceImpl_SetSurfaceDesc,
2605 /*** IDirectDrawSurface4 ***/
2606 IDirectDrawSurfaceImpl_SetPrivateData,
2607 IDirectDrawSurfaceImpl_GetPrivateData,
2608 IDirectDrawSurfaceImpl_FreePrivateData,
2609 IDirectDrawSurfaceImpl_GetUniquenessValue,
2610 IDirectDrawSurfaceImpl_ChangeUniquenessValue,
2611 /*** IDirectDrawSurface7 ***/
2612 IDirectDrawSurfaceImpl_SetPriority,
2613 IDirectDrawSurfaceImpl_GetPriority,
2614 IDirectDrawSurfaceImpl_SetLOD,
2615 IDirectDrawSurfaceImpl_GetLOD