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 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)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);
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 = &This->IDirectDrawSurface3_vtbl;
102 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
105 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
107 IUnknown_AddRef(iface);
108 *obj = &This->IDirectDrawGammaControl_vtbl;
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((IDirect3D7 *)&This->ddraw->IDirect3D7_vtbl, riid, (IDirectDrawSurface7 *)This, &d3d);
121 *obj = d3d ? (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl : NULL;
122 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
126 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
127 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
129 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
131 *obj = &This->IDirect3DTexture_vtbl;
132 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
136 *obj = &This->IDirect3DTexture2_vtbl;
137 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
139 IUnknown_AddRef( (IUnknown *) *obj);
143 ERR("No interface\n");
144 return E_NOINTERFACE;
147 /*****************************************************************************
148 * IDirectDrawSurface7::AddRef
150 * A normal addref implementation
155 *****************************************************************************/
157 IDirectDrawSurfaceImpl_AddRef(IDirectDrawSurface7 *iface)
159 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
160 ULONG refCount = InterlockedIncrement(&This->ref);
162 if (refCount == 1 && This->WineD3DSurface)
164 EnterCriticalSection(&ddraw_cs);
165 IWineD3DSurface_AddRef(This->WineD3DSurface);
166 LeaveCriticalSection(&ddraw_cs);
169 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
173 /*****************************************************************************
174 * IDirectDrawSurfaceImpl_Destroy
176 * A helper function for IDirectDrawSurface7::Release
178 * Frees the surface, regardless of its refcount.
179 * See IDirectDrawSurface7::Release for more information
182 * This: Surface to free
184 *****************************************************************************/
185 void IDirectDrawSurfaceImpl_Destroy(IDirectDrawSurfaceImpl *This)
187 TRACE("(%p)\n", This);
189 /* Check the refcount and give a warning */
192 /* This can happen when a complex surface is destroyed,
193 * because the 2nd surface was addref()ed when the app
194 * called GetAttachedSurface
196 WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
199 /* Check for attached surfaces and detach them */
200 if(This->first_attached != This)
202 /* Well, this shouldn't happen: The surface being attached is addref()ed
203 * in AddAttachedSurface, so it shouldn't be released until DeleteAttachedSurface
204 * is called, because the refcount is held. It looks like the app released()
205 * it often enough to force this
207 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)This->first_attached;
208 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)This;
210 FIXME("(%p) Freeing a surface that is attached to surface %p\n", This, This->first_attached);
212 /* The refcount will drop to -1 here */
213 if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
215 ERR("(%p) DeleteAttachedSurface failed!\n", This);
219 while(This->next_attached != NULL)
221 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)This;
222 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)This->next_attached;
224 if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
226 ERR("(%p) DeleteAttachedSurface failed!\n", This);
231 /* Now destroy the surface. Wait: It could have been released if we are a texture */
232 if(This->WineD3DSurface)
233 IWineD3DSurface_Release(This->WineD3DSurface);
235 /* Having a texture handle set implies that the device still exists */
238 This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
239 This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
242 /* Reduce the ddraw surface count */
243 InterlockedDecrement(&This->ddraw->surfaces);
244 list_remove(&This->surface_list_entry);
246 HeapFree(GetProcessHeap(), 0, This);
249 /*****************************************************************************
250 * IDirectDrawSurface7::Release
252 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
253 * surface is destroyed.
255 * Destroying the surface is a bit tricky. For the connection between
256 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
257 * It has a nice graph explaining the connection.
259 * What happens here is basically this:
260 * When a surface is destroyed, its WineD3DSurface is released,
261 * and the refcount of the DirectDraw interface is reduced by 1. If it has
262 * complex surfaces attached to it, then these surfaces are destroyed too,
263 * regardless of their refcount. If any surface being destroyed has another
264 * surface attached to it (with a "soft" attachment, not complex), then
265 * this surface is detached with DeleteAttachedSurface.
267 * When the surface is a texture, the WineD3DTexture is released.
268 * If the surface is the Direct3D render target, then the D3D
269 * capabilities of the WineD3DDevice are uninitialized, which causes the
270 * swapchain to be released.
272 * When a complex sublevel falls to ref zero, then this is ignored.
277 *****************************************************************************/
279 IDirectDrawSurfaceImpl_Release(IDirectDrawSurface7 *iface)
281 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
283 TRACE("(%p) : Releasing from %d\n", This, This->ref);
284 ref = InterlockedDecrement(&This->ref);
289 IDirectDrawSurfaceImpl *surf;
290 IDirectDrawImpl *ddraw;
291 IUnknown *ifaceToRelease = This->ifaceToRelease;
294 /* Complex attached surfaces are destroyed implicitly when the root is released */
295 EnterCriticalSection(&ddraw_cs);
296 if(!This->is_complex_root)
298 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
299 LeaveCriticalSection(&ddraw_cs);
304 /* If it's a texture, destroy the WineD3DTexture.
305 * WineD3D will destroy the IParent interfaces
306 * of the sublevels, which destroys the WineD3DSurfaces.
307 * Set the surfaces to NULL to avoid destroying them again later
309 if(This->wineD3DTexture)
311 IWineD3DBaseTexture_Release(This->wineD3DTexture);
313 /* If it's the RenderTarget, destroy the d3ddevice */
314 else if(This->wineD3DSwapChain)
316 if((ddraw->d3d_initialized) && (This == ddraw->d3d_target)) {
317 TRACE("(%p) Destroying the render target, uninitializing D3D\n", This);
319 /* Unset any index buffer, just to be sure */
320 IWineD3DDevice_SetIndices(ddraw->wineD3DDevice, NULL, WINED3DFMT_UNKNOWN);
321 IWineD3DDevice_SetDepthStencilSurface(ddraw->wineD3DDevice, NULL);
322 IWineD3DDevice_SetVertexDeclaration(ddraw->wineD3DDevice, NULL);
323 for(i = 0; i < ddraw->numConvertedDecls; i++)
325 IWineD3DVertexDeclaration_Release(ddraw->decls[i].decl);
327 HeapFree(GetProcessHeap(), 0, ddraw->decls);
328 ddraw->numConvertedDecls = 0;
330 if (FAILED(IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain)))
333 ERR("(%p) Failed to uninit 3D\n", This);
337 /* Free the d3d window if one was created */
338 if(ddraw->d3d_window != 0 && ddraw->d3d_window != ddraw->dest_window)
340 TRACE(" (%p) Destroying the hidden render window %p\n", This, ddraw->d3d_window);
341 DestroyWindow(ddraw->d3d_window);
342 ddraw->d3d_window = 0;
344 /* Unset the pointers */
347 This->wineD3DSwapChain = NULL; /* Uninit3D releases the swapchain */
348 ddraw->d3d_initialized = FALSE;
349 ddraw->d3d_target = NULL;
351 IWineD3DDevice_UninitGDI(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain);
352 This->wineD3DSwapChain = NULL;
355 /* Reset to the default surface implementation type. This is needed if apps use
356 * non render target surfaces and expect blits to work after destroying the render
359 * TODO: Recreate existing offscreen surfaces
361 ddraw->ImplType = DefaultSurfaceType;
363 /* Write a trace because D3D unloading was the reason for many
364 * crashes during development.
366 TRACE("(%p) D3D unloaded\n", This);
368 else if(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
372 /* It's a render target, but no swapchain was created.
373 * The IParent interfaces have to be released manually.
374 * The same applies for textures without an
375 * IWineD3DTexture object attached
379 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
381 if(This->complex_array[i])
383 /* Only the topmost level can have more than 1 surfaces in the complex
384 * attachment array(Cube texture roots), for all others there is only
387 surf = This->complex_array[i];
390 IWineD3DSurface_GetParent(surf->WineD3DSurface,
391 (IUnknown **) &Parent);
392 IParent_Release(Parent); /* For the getParent */
393 IParent_Release(Parent); /* To release it */
394 surf = surf->complex_array[0];
399 /* Now the top-level surface */
400 IWineD3DSurface_GetParent(This->WineD3DSurface,
401 (IUnknown **) &Parent);
402 IParent_Release(Parent); /* For the getParent */
403 IParent_Release(Parent); /* To release it */
406 /* The refcount test shows that the palette is detached when the surface is destroyed */
407 IDirectDrawSurface7_SetPalette((IDirectDrawSurface7 *)This, NULL);
409 /* Loop through all complex attached surfaces,
412 * Yet again, only the root can have more than one complexly attached surface, all the others
413 * have a total of one;
415 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
417 if(!This->complex_array[i]) break;
419 surf = This->complex_array[i];
420 This->complex_array[i] = NULL;
423 IDirectDrawSurfaceImpl *destroy = surf;
424 surf = surf->complex_array[0]; /* Iterate through the "tree" */
425 IDirectDrawSurfaceImpl_Destroy(destroy); /* Destroy it */
429 /* Destroy the root surface.
431 IDirectDrawSurfaceImpl_Destroy(This);
433 /* Reduce the ddraw refcount */
434 if(ifaceToRelease) IUnknown_Release(ifaceToRelease);
435 LeaveCriticalSection(&ddraw_cs);
441 /*****************************************************************************
442 * IDirectDrawSurface7::GetAttachedSurface
444 * Returns an attached surface with the requested caps. Surface attachment
445 * and complex surfaces are not clearly described by the MSDN or sdk,
446 * so this method is tricky and likely to contain problems.
447 * This implementation searches the complex list first, then the
450 * The chains are searched from This down to the last surface in the chain,
451 * not from the first element in the chain. The first surface found is
452 * returned. The MSDN says that this method fails if more than one surface
453 * matches the caps, but it is not sure if that is right. The attachment
454 * structure may not even allow two matching surfaces.
456 * The found surface is AddRef-ed before it is returned.
459 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
460 * Surface: Address to store the found surface
464 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
465 * DDERR_NOTFOUND if no surface was found
467 *****************************************************************************/
468 static HRESULT WINAPI
469 IDirectDrawSurfaceImpl_GetAttachedSurface(IDirectDrawSurface7 *iface,
471 IDirectDrawSurface7 **Surface)
473 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
474 IDirectDrawSurfaceImpl *surf;
478 TRACE("(%p)->(%p,%p)\n", This, Caps, Surface);
479 EnterCriticalSection(&ddraw_cs);
481 if(This->version < 7)
483 /* Earlier dx apps put garbage into these members, clear them */
484 our_caps.dwCaps = Caps->dwCaps;
485 our_caps.dwCaps2 = 0;
486 our_caps.dwCaps3 = 0;
487 our_caps.dwCaps4 = 0;
494 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 */
496 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
498 surf = This->complex_array[i];
503 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
504 surf->surface_desc.ddsCaps.dwCaps,
505 surf->surface_desc.ddsCaps.dwCaps2,
506 surf->surface_desc.ddsCaps.dwCaps3,
507 surf->surface_desc.ddsCaps.dwCaps4);
510 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
511 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
513 /* MSDN: "This method fails if more than one surface is attached
514 * that matches the capabilities requested."
516 * Not sure how to test this.
519 TRACE("(%p): Returning surface %p\n", This, surf);
520 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
521 *Surface = (IDirectDrawSurface7 *)surf;
522 IDirectDrawSurface7_AddRef(*Surface);
523 LeaveCriticalSection(&ddraw_cs);
528 /* Next, look at the attachment chain */
531 while( (surf = surf->next_attached) )
535 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
536 surf->surface_desc.ddsCaps.dwCaps,
537 surf->surface_desc.ddsCaps.dwCaps2,
538 surf->surface_desc.ddsCaps.dwCaps3,
539 surf->surface_desc.ddsCaps.dwCaps4);
542 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
543 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
545 TRACE("(%p): Returning surface %p\n", This, surf);
546 *Surface = (IDirectDrawSurface7 *)surf;
547 IDirectDrawSurface7_AddRef(*Surface);
548 LeaveCriticalSection(&ddraw_cs);
553 TRACE("(%p) Didn't find a valid surface\n", This);
554 LeaveCriticalSection(&ddraw_cs);
557 return DDERR_NOTFOUND;
560 /*****************************************************************************
561 * IDirectDrawSurface7::Lock
563 * Locks the surface and returns a pointer to the surface's memory
566 * Rect: Rectangle to lock. If NULL, the whole surface is locked
567 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
568 * Flags: Locking flags, e.g Read only or write only
569 * h: An event handle that's not used and must be NULL
573 * DDERR_INVALIDPARAMS if DDSD is NULL
574 * For more details, see IWineD3DSurface::LockRect
576 *****************************************************************************/
577 static HRESULT WINAPI
578 IDirectDrawSurfaceImpl_Lock(IDirectDrawSurface7 *iface,
580 DDSURFACEDESC2 *DDSD,
584 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
585 WINED3DLOCKED_RECT LockedRect;
587 TRACE("(%p)->(%p,%p,%x,%p)\n", This, Rect, DDSD, Flags, h);
590 return DDERR_INVALIDPARAMS;
592 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
593 EnterCriticalSection(&ddraw_cs);
595 /* Should I check for the handle to be NULL?
597 * The DDLOCK flags and the D3DLOCK flags are equal
598 * for the supported values. The others are ignored by WineD3D
601 if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
602 DDSD->dwSize != sizeof(DDSURFACEDESC2))
604 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
605 LeaveCriticalSection(&ddraw_cs);
606 return DDERR_INVALIDPARAMS;
609 /* Windows zeroes this if the rect is invalid */
616 || (Rect->left > Rect->right)
617 || (Rect->top > Rect->bottom)
618 || (Rect->right > This->surface_desc.dwWidth)
619 || (Rect->bottom > This->surface_desc.dwHeight))
621 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
622 LeaveCriticalSection(&ddraw_cs);
623 return DDERR_INVALIDPARAMS;
627 hr = IWineD3DSurface_LockRect(This->WineD3DSurface,
633 LeaveCriticalSection(&ddraw_cs);
636 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
637 * specific error. But since IWineD3DSurface::LockRect returns that error in this
638 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
639 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
640 * is much easier to do it in one place in ddraw
642 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
647 /* Override the memory area. The pitch should be set already. Strangely windows
648 * does not set the LPSURFACE flag on locked surfaces !?!.
649 * DDSD->dwFlags |= DDSD_LPSURFACE;
651 This->surface_desc.lpSurface = LockedRect.pBits;
652 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
654 TRACE("locked surface returning description :\n");
655 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
657 LeaveCriticalSection(&ddraw_cs);
661 /*****************************************************************************
662 * IDirectDrawSurface7::Unlock
664 * Unlocks an locked surface
667 * Rect: Not used by this implementation
671 * For more details, see IWineD3DSurface::UnlockRect
673 *****************************************************************************/
674 static HRESULT WINAPI
675 IDirectDrawSurfaceImpl_Unlock(IDirectDrawSurface7 *iface,
678 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
680 TRACE("(%p)->(%p)\n", This, pRect);
682 EnterCriticalSection(&ddraw_cs);
683 hr = IWineD3DSurface_UnlockRect(This->WineD3DSurface);
686 This->surface_desc.lpSurface = NULL;
688 LeaveCriticalSection(&ddraw_cs);
692 /*****************************************************************************
693 * IDirectDrawSurface7::Flip
695 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
696 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
697 * the flip target is passed to WineD3D, even if the app didn't specify one
700 * DestOverride: Specifies the surface that will become the new front
701 * buffer. If NULL, the current back buffer is used
702 * Flags: some DirectDraw flags, see include/ddraw.h
706 * DDERR_NOTFLIPPABLE if no flip target could be found
707 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
708 * For more details, see IWineD3DSurface::Flip
710 *****************************************************************************/
711 static HRESULT WINAPI
712 IDirectDrawSurfaceImpl_Flip(IDirectDrawSurface7 *iface,
713 IDirectDrawSurface7 *DestOverride,
716 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
717 IDirectDrawSurfaceImpl *Override = (IDirectDrawSurfaceImpl *)DestOverride;
718 IDirectDrawSurface7 *Override7;
720 TRACE("(%p)->(%p,%x)\n", This, DestOverride, Flags);
722 /* Flip has to be called from a front buffer
723 * What about overlay surfaces, AFAIK they can flip too?
725 if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
726 return DDERR_INVALIDOBJECT; /* Unchecked */
728 EnterCriticalSection(&ddraw_cs);
730 /* WineD3D doesn't keep track of attached surface, so find the target */
735 memset(&Caps, 0, sizeof(Caps));
736 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
737 hr = IDirectDrawSurface7_GetAttachedSurface(iface, &Caps, &Override7);
740 ERR("Can't find a flip target\n");
741 LeaveCriticalSection(&ddraw_cs);
742 return DDERR_NOTFLIPPABLE; /* Unchecked */
744 Override = (IDirectDrawSurfaceImpl *)Override7;
746 /* For the GetAttachedSurface */
747 IDirectDrawSurface7_Release(Override7);
750 hr = IWineD3DSurface_Flip(This->WineD3DSurface,
751 Override->WineD3DSurface,
753 LeaveCriticalSection(&ddraw_cs);
757 /*****************************************************************************
758 * IDirectDrawSurface7::Blt
760 * Performs a blit on the surface
763 * DestRect: Destination rectangle, can be NULL
764 * SrcSurface: Source surface, can be NULL
765 * SrcRect: Source rectangle, can be NULL
767 * DDBltFx: Some extended blt parameters, connected to the flags
771 * See IWineD3DSurface::Blt for more details
773 *****************************************************************************/
774 static HRESULT WINAPI
775 IDirectDrawSurfaceImpl_Blt(IDirectDrawSurface7 *iface,
777 IDirectDrawSurface7 *SrcSurface,
782 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
783 IDirectDrawSurfaceImpl *Src = (IDirectDrawSurfaceImpl *)SrcSurface;
785 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
787 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
788 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
790 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
791 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
792 return DDERR_INVALIDPARAMS;
795 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
796 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
797 return DDERR_INVALIDPARAMS;
800 /* Sizes can change, therefore hold the lock when testing the rectangles */
801 EnterCriticalSection(&ddraw_cs);
804 if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
805 DestRect->right > This->surface_desc.dwWidth ||
806 DestRect->bottom > This->surface_desc.dwHeight)
808 WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
809 LeaveCriticalSection(&ddraw_cs);
810 return DDERR_INVALIDRECT;
815 if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
816 SrcRect->right > Src->surface_desc.dwWidth ||
817 SrcRect->bottom > Src->surface_desc.dwHeight)
819 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
820 LeaveCriticalSection(&ddraw_cs);
821 return DDERR_INVALIDRECT;
825 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
826 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
827 LeaveCriticalSection(&ddraw_cs);
828 return DDERR_INVALIDPARAMS;
831 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it does, copy the struct,
832 * and replace the ddraw surfaces with the wined3d surfaces
833 * So far no blitting operations using surfaces in the bltfx struct are supported anyway.
835 hr = IWineD3DSurface_Blt(This->WineD3DSurface,
837 Src ? Src->WineD3DSurface : NULL,
840 (WINEDDBLTFX *) DDBltFx,
843 LeaveCriticalSection(&ddraw_cs);
846 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
847 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
852 /*****************************************************************************
853 * IDirectDrawSurface7::AddAttachedSurface
855 * Attaches a surface to another surface. How the surface attachments work
856 * is not totally understood yet, and this method is prone to problems.
857 * he surface that is attached is AddRef-ed.
859 * Tests with complex surfaces suggest that the surface attachments form a
860 * tree, but no method to test this has been found yet.
862 * The attachment list consists of a first surface (first_attached) and
863 * for each surface a pointer to the next attached surface (next_attached).
864 * For the first surface, and a surface that has no attachments
865 * first_attached points to the surface itself. A surface that has
866 * no successors in the chain has next_attached set to NULL.
868 * Newly attached surfaces are attached right after the root surface.
869 * If a surface is attached to a complex surface compound, it's attached to
870 * the surface that the app requested, not the complex root. See
871 * GetAttachedSurface for a description how surfaces are found.
873 * This is how the current implementation works, and it was coded by looking
874 * at the needs of the applications.
876 * So far only Z-Buffer attachments are tested, and they are activated in
877 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
878 * Back buffers should work in 2D mode, but they are not tested(They can be
879 * attached in older iface versions). Rendering to the front buffer and
880 * switching between that and double buffering is not yet implemented in
881 * WineD3D, so for 3D it might have unexpected results.
883 * IDirectDrawSurfaceImpl_AddAttachedSurface is the real thing,
884 * IDirectDrawSurface7Impl_AddAttachedSurface is a wrapper around it that
885 * performs additional checks. Version 7 of this interface is much more restrictive
886 * than its predecessors.
889 * Attach: Surface to attach to iface
893 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
895 *****************************************************************************/
897 IDirectDrawSurfaceImpl_AddAttachedSurface(IDirectDrawSurfaceImpl *This,
898 IDirectDrawSurfaceImpl *Surf)
900 TRACE("(%p)->(%p)\n", This, Surf);
903 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
905 EnterCriticalSection(&ddraw_cs);
907 /* Check if the surface is already attached somewhere */
908 if( (Surf->next_attached != NULL) ||
909 (Surf->first_attached != Surf) )
911 /* TODO: Test for the structure of the manual attachment. Is it a chain or a list?
912 * What happens if one surface is attached to 2 different surfaces?
914 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);
915 LeaveCriticalSection(&ddraw_cs);
916 return DDERR_SURFACEALREADYATTACHED;
919 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
920 Surf->next_attached = This->next_attached;
921 Surf->first_attached = This->first_attached;
922 This->next_attached = Surf;
924 /* Check if the WineD3D depth stencil needs updating */
925 if(This->ddraw->d3ddevice)
927 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
930 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)Surf);
931 LeaveCriticalSection(&ddraw_cs);
935 static HRESULT WINAPI
936 IDirectDrawSurface7Impl_AddAttachedSurface(IDirectDrawSurface7 *iface,
937 IDirectDrawSurface7 *Attach)
939 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
940 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
942 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
943 if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
946 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
947 Surf->surface_desc.ddsCaps.dwCaps);
948 return DDERR_CANNOTATTACHSURFACE;
951 return IDirectDrawSurfaceImpl_AddAttachedSurface(This,
954 /*****************************************************************************
955 * IDirectDrawSurface7::DeleteAttachedSurface
957 * Removes a surface from the attachment chain. The surface's refcount
958 * is decreased by one after it has been removed
961 * Flags: Some flags, not used by this implementation
962 * Attach: Surface to detach
966 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
968 *****************************************************************************/
969 static HRESULT WINAPI
970 IDirectDrawSurfaceImpl_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
972 IDirectDrawSurface7 *Attach)
974 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
975 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
976 IDirectDrawSurfaceImpl *Prev = This;
977 TRACE("(%p)->(%08x,%p)\n", This, Flags, Surf);
979 EnterCriticalSection(&ddraw_cs);
980 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
982 LeaveCriticalSection(&ddraw_cs);
983 return DDERR_CANNOTDETACHSURFACE;
986 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
987 if (This->surface_desc.ddsCaps.dwCaps &
988 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
990 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
991 /* FIXME: we should probably also subtract from dwMipMapCount of this
992 * and all parent surfaces */
995 /* Find the predecessor of the detached surface */
998 if(Prev->next_attached == Surf) break;
999 Prev = Prev->next_attached;
1002 /* There must be a surface, otherwise there's a bug */
1003 assert(Prev != NULL);
1005 /* Unchain the surface */
1006 Prev->next_attached = Surf->next_attached;
1007 Surf->next_attached = NULL;
1008 Surf->first_attached = Surf;
1010 /* Check if the WineD3D depth stencil needs updating */
1011 if(This->ddraw->d3ddevice)
1013 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1016 IDirectDrawSurface7_Release(Attach);
1017 LeaveCriticalSection(&ddraw_cs);
1021 /*****************************************************************************
1022 * IDirectDrawSurface7::AddOverlayDirtyRect
1024 * "This method is not currently implemented"
1032 *****************************************************************************/
1033 static HRESULT WINAPI
1034 IDirectDrawSurfaceImpl_AddOverlayDirtyRect(IDirectDrawSurface7 *iface,
1037 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1038 TRACE("(%p)->(%p)\n",This,Rect);
1040 /* MSDN says it's not implemented. I could forward it to WineD3D,
1041 * then we'd implement it, but I don't think that's a good idea
1042 * (Stefan Dösinger)
1045 return IWineD3DSurface_AddOverlayDirtyRect(This->WineD3DSurface, pRect);
1047 return DDERR_UNSUPPORTED; /* unchecked */
1050 /*****************************************************************************
1051 * IDirectDrawSurface7::GetDC
1053 * Returns a GDI device context for the surface
1056 * hdc: Address of a HDC variable to store the dc to
1060 * DDERR_INVALIDPARAMS if hdc is NULL
1061 * For details, see IWineD3DSurface::GetDC
1063 *****************************************************************************/
1064 static HRESULT WINAPI
1065 IDirectDrawSurfaceImpl_GetDC(IDirectDrawSurface7 *iface,
1068 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1070 TRACE("(%p)->(%p): Relay\n", This, hdc);
1073 return DDERR_INVALIDPARAMS;
1075 EnterCriticalSection(&ddraw_cs);
1076 hr = IWineD3DSurface_GetDC(This->WineD3DSurface,
1078 LeaveCriticalSection(&ddraw_cs);
1081 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1084 case WINED3DERR_INVALIDCALL:
1085 if(hdc) *hdc = NULL;
1086 return DDERR_INVALIDPARAMS;
1092 /*****************************************************************************
1093 * IDirectDrawSurface7::ReleaseDC
1095 * Releases the DC that was constructed with GetDC
1098 * hdc: HDC to release
1102 * For more details, see IWineD3DSurface::ReleaseDC
1104 *****************************************************************************/
1105 static HRESULT WINAPI
1106 IDirectDrawSurfaceImpl_ReleaseDC(IDirectDrawSurface7 *iface,
1109 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1111 TRACE("(%p)->(%p): Relay\n", This, hdc);
1113 EnterCriticalSection(&ddraw_cs);
1114 hr = IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
1115 LeaveCriticalSection(&ddraw_cs);
1119 /*****************************************************************************
1120 * IDirectDrawSurface7::GetCaps
1122 * Returns the surface's caps
1125 * Caps: Address to write the caps to
1129 * DDERR_INVALIDPARAMS if Caps is NULL
1131 *****************************************************************************/
1132 static HRESULT WINAPI
1133 IDirectDrawSurfaceImpl_GetCaps(IDirectDrawSurface7 *iface,
1136 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1137 TRACE("(%p)->(%p)\n",This,Caps);
1140 return DDERR_INVALIDPARAMS;
1142 *Caps = This->surface_desc.ddsCaps;
1146 /*****************************************************************************
1147 * IDirectDrawSurface7::SetPriority
1149 * Sets a texture priority for managed textures.
1152 * Priority: The new priority
1156 * For more details, see IWineD3DSurface::SetPriority
1158 *****************************************************************************/
1159 static HRESULT WINAPI
1160 IDirectDrawSurfaceImpl_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1162 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1164 TRACE("(%p)->(%d): Relay!\n",This,Priority);
1166 EnterCriticalSection(&ddraw_cs);
1167 hr = IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
1168 LeaveCriticalSection(&ddraw_cs);
1172 /*****************************************************************************
1173 * IDirectDrawSurface7::GetPriority
1175 * Returns the surface's priority
1178 * Priority: Address of a variable to write the priority to
1182 * DDERR_INVALIDPARAMS if Priority == NULL
1183 * For more details, see IWineD3DSurface::GetPriority
1185 *****************************************************************************/
1186 static HRESULT WINAPI
1187 IDirectDrawSurfaceImpl_GetPriority(IDirectDrawSurface7 *iface,
1190 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1191 TRACE("(%p)->(%p): Relay\n",This,Priority);
1195 return DDERR_INVALIDPARAMS;
1198 EnterCriticalSection(&ddraw_cs);
1199 *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1200 LeaveCriticalSection(&ddraw_cs);
1204 /*****************************************************************************
1205 * IDirectDrawSurface7::SetPrivateData
1207 * Stores some data in the surface that is intended for the application's
1211 * tag: GUID that identifies the data
1212 * Data: Pointer to the private data
1213 * Size: Size of the private data
1218 * For more details, see IWineD3DSurface::SetPrivateData
1220 *****************************************************************************/
1221 static HRESULT WINAPI
1222 IDirectDrawSurfaceImpl_SetPrivateData(IDirectDrawSurface7 *iface,
1228 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1230 TRACE("(%p)->(%s,%p,%d,%x): Relay\n", This, debugstr_guid(tag), Data, Size, Flags);
1232 EnterCriticalSection(&ddraw_cs);
1233 hr = IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1238 LeaveCriticalSection(&ddraw_cs);
1241 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1246 /*****************************************************************************
1247 * IDirectDrawSurface7::GetPrivateData
1249 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1252 * tag: GUID of the data to return
1253 * Data: Address where to write the data to
1254 * Size: Size of the buffer at Data
1258 * DDERR_INVALIDPARAMS if Data is NULL
1259 * For more details, see IWineD3DSurface::GetPrivateData
1261 *****************************************************************************/
1262 static HRESULT WINAPI
1263 IDirectDrawSurfaceImpl_GetPrivateData(IDirectDrawSurface7 *iface,
1268 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1270 TRACE("(%p)->(%s,%p,%p): Relay\n", This, debugstr_guid(tag), Data, Size);
1273 return DDERR_INVALIDPARAMS;
1275 EnterCriticalSection(&ddraw_cs);
1276 hr = IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1280 LeaveCriticalSection(&ddraw_cs);
1284 /*****************************************************************************
1285 * IDirectDrawSurface7::FreePrivateData
1287 * Frees private data stored in the surface
1290 * tag: Tag of the data to free
1294 * For more details, see IWineD3DSurface::FreePrivateData
1296 *****************************************************************************/
1297 static HRESULT WINAPI
1298 IDirectDrawSurfaceImpl_FreePrivateData(IDirectDrawSurface7 *iface,
1301 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1303 TRACE("(%p)->(%s): Relay\n", This, debugstr_guid(tag));
1305 EnterCriticalSection(&ddraw_cs);
1306 hr = IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1307 LeaveCriticalSection(&ddraw_cs);
1311 /*****************************************************************************
1312 * IDirectDrawSurface7::PageLock
1314 * Prevents a sysmem surface from being paged out
1317 * Flags: Not used, must be 0(unchecked)
1320 * DD_OK, because it's a stub
1322 *****************************************************************************/
1323 static HRESULT WINAPI
1324 IDirectDrawSurfaceImpl_PageLock(IDirectDrawSurface7 *iface,
1327 TRACE("(%p)->(%x)\n", iface, Flags);
1329 /* This is Windows memory management related - we don't need this */
1333 /*****************************************************************************
1334 * IDirectDrawSurface7::PageUnlock
1336 * Allows a sysmem surface to be paged out
1339 * Flags: Not used, must be 0(unchecked)
1342 * DD_OK, because it's a stub
1344 *****************************************************************************/
1345 static HRESULT WINAPI
1346 IDirectDrawSurfaceImpl_PageUnlock(IDirectDrawSurface7 *iface,
1349 TRACE("(%p)->(%x)\n", iface, Flags);
1354 /*****************************************************************************
1355 * IDirectDrawSurface7::BltBatch
1357 * An unimplemented function
1365 *****************************************************************************/
1366 static HRESULT WINAPI IDirectDrawSurfaceImpl_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1368 TRACE("(%p)->(%p,%d,%08x)\n",iface,Batch,Count,Flags);
1370 /* MSDN: "not currently implemented" */
1371 return DDERR_UNSUPPORTED;
1374 /*****************************************************************************
1375 * IDirectDrawSurface7::EnumAttachedSurfaces
1377 * Enumerates all surfaces attached to this surface
1380 * context: Pointer to pass unmodified to the callback
1381 * cb: Callback function to call for each surface
1385 * DDERR_INVALIDPARAMS if cb is NULL
1387 *****************************************************************************/
1388 static HRESULT WINAPI
1389 IDirectDrawSurfaceImpl_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1391 LPDDENUMSURFACESCALLBACK7 cb)
1393 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1394 IDirectDrawSurfaceImpl *surf;
1395 DDSURFACEDESC2 desc;
1398 /* Attached surfaces aren't handled in WineD3D */
1399 TRACE("(%p)->(%p,%p)\n",This,context,cb);
1402 return DDERR_INVALIDPARAMS;
1404 EnterCriticalSection(&ddraw_cs);
1405 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1407 surf = This->complex_array[i];
1410 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
1411 desc = surf->surface_desc;
1412 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1413 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1415 LeaveCriticalSection(&ddraw_cs);
1420 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1422 IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)surf);
1423 desc = surf->surface_desc;
1424 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1425 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1427 LeaveCriticalSection(&ddraw_cs);
1432 TRACE(" end of enumeration.\n");
1434 LeaveCriticalSection(&ddraw_cs);
1438 /*****************************************************************************
1439 * IDirectDrawSurface7::EnumOverlayZOrders
1441 * "Enumerates the overlay surfaces on the specified destination"
1444 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
1445 * context: context to pass back to the callback
1446 * cb: callback function to call for each enumerated surface
1449 * DD_OK, because it's a stub
1451 *****************************************************************************/
1452 static HRESULT WINAPI
1453 IDirectDrawSurfaceImpl_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1456 LPDDENUMSURFACESCALLBACK7 cb)
1458 FIXME("(%p)->(%x,%p,%p): Stub!\n", iface, Flags, context, cb);
1463 /*****************************************************************************
1464 * IDirectDrawSurface7::GetBltStatus
1466 * Returns the blitting status
1469 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1472 * See IWineD3DSurface::Blt
1474 *****************************************************************************/
1475 static HRESULT WINAPI
1476 IDirectDrawSurfaceImpl_GetBltStatus(IDirectDrawSurface7 *iface,
1479 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1481 TRACE("(%p)->(%x): Relay\n", This, Flags);
1483 EnterCriticalSection(&ddraw_cs);
1484 hr = IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1485 LeaveCriticalSection(&ddraw_cs);
1488 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1493 /*****************************************************************************
1494 * IDirectDrawSurface7::GetColorKey
1496 * Returns the color key assigned to the surface
1500 * CKey: Address to store the key to
1504 * DDERR_INVALIDPARAMS if CKey is NULL
1506 *****************************************************************************/
1507 static HRESULT WINAPI
1508 IDirectDrawSurfaceImpl_GetColorKey(IDirectDrawSurface7 *iface,
1512 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1513 TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
1516 return DDERR_INVALIDPARAMS;
1518 EnterCriticalSection(&ddraw_cs);
1522 case DDCKEY_DESTBLT:
1523 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
1525 LeaveCriticalSection(&ddraw_cs);
1526 return DDERR_NOCOLORKEY;
1528 *CKey = This->surface_desc.ddckCKDestBlt;
1531 case DDCKEY_DESTOVERLAY:
1532 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
1534 LeaveCriticalSection(&ddraw_cs);
1535 return DDERR_NOCOLORKEY;
1537 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1541 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
1543 LeaveCriticalSection(&ddraw_cs);
1544 return DDERR_NOCOLORKEY;
1546 *CKey = This->surface_desc.ddckCKSrcBlt;
1549 case DDCKEY_SRCOVERLAY:
1550 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
1552 LeaveCriticalSection(&ddraw_cs);
1553 return DDERR_NOCOLORKEY;
1555 *CKey = This->surface_desc.ddckCKSrcOverlay;
1559 LeaveCriticalSection(&ddraw_cs);
1560 return DDERR_INVALIDPARAMS;
1563 LeaveCriticalSection(&ddraw_cs);
1567 /*****************************************************************************
1568 * IDirectDrawSurface7::GetFlipStatus
1570 * Returns the flipping status of the surface
1573 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1576 * See IWineD3DSurface::GetFlipStatus
1578 *****************************************************************************/
1579 static HRESULT WINAPI
1580 IDirectDrawSurfaceImpl_GetFlipStatus(IDirectDrawSurface7 *iface,
1583 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1585 TRACE("(%p)->(%x): Relay\n", This, Flags);
1587 EnterCriticalSection(&ddraw_cs);
1588 hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1589 LeaveCriticalSection(&ddraw_cs);
1592 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1597 /*****************************************************************************
1598 * IDirectDrawSurface7::GetOverlayPosition
1600 * Returns the display coordinates of a visible and active overlay surface
1607 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
1608 *****************************************************************************/
1609 static HRESULT WINAPI
1610 IDirectDrawSurfaceImpl_GetOverlayPosition(IDirectDrawSurface7 *iface,
1613 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1615 TRACE("(%p)->(%p,%p): Relay\n", This, X, Y);
1617 EnterCriticalSection(&ddraw_cs);
1618 hr = IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1621 LeaveCriticalSection(&ddraw_cs);
1625 /*****************************************************************************
1626 * IDirectDrawSurface7::GetPixelFormat
1628 * Returns the pixel format of the Surface
1631 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1632 * format should be written
1636 * DDERR_INVALIDPARAMS if PixelFormat is NULL
1638 *****************************************************************************/
1639 static HRESULT WINAPI
1640 IDirectDrawSurfaceImpl_GetPixelFormat(IDirectDrawSurface7 *iface,
1641 DDPIXELFORMAT *PixelFormat)
1643 /* What is DDERR_INVALIDSURFACETYPE for here? */
1644 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1645 TRACE("(%p)->(%p)\n",This,PixelFormat);
1648 return DDERR_INVALIDPARAMS;
1650 EnterCriticalSection(&ddraw_cs);
1651 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1652 LeaveCriticalSection(&ddraw_cs);
1658 /*****************************************************************************
1659 * IDirectDrawSurface7::GetSurfaceDesc
1661 * Returns the description of this surface
1664 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1669 * DDERR_INVALIDPARAMS if DDSD is NULL
1671 *****************************************************************************/
1672 static HRESULT WINAPI
1673 IDirectDrawSurfaceImpl_GetSurfaceDesc(IDirectDrawSurface7 *iface,
1674 DDSURFACEDESC2 *DDSD)
1676 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1678 TRACE("(%p)->(%p)\n",This,DDSD);
1681 return DDERR_INVALIDPARAMS;
1683 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1685 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1686 return DDERR_INVALIDPARAMS;
1689 EnterCriticalSection(&ddraw_cs);
1690 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1691 TRACE("Returning surface desc:\n");
1692 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1694 LeaveCriticalSection(&ddraw_cs);
1698 /*****************************************************************************
1699 * IDirectDrawSurface7::Initialize
1701 * Initializes the surface. This is a no-op in Wine
1704 * DD: Pointer to an DirectDraw interface
1705 * DDSD: Surface description for initialization
1708 * DDERR_ALREADYINITIALIZED
1710 *****************************************************************************/
1711 static HRESULT WINAPI
1712 IDirectDrawSurfaceImpl_Initialize(IDirectDrawSurface7 *iface,
1714 DDSURFACEDESC2 *DDSD)
1716 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1717 IDirectDrawImpl *ddimpl = DD ? ddraw_from_ddraw1(DD) : NULL;
1718 TRACE("(%p)->(%p,%p)\n",This,ddimpl,DDSD);
1720 return DDERR_ALREADYINITIALIZED;
1723 /*****************************************************************************
1724 * IDirectDrawSurface7::IsLost
1726 * Checks if the surface is lost
1729 * DD_OK, if the surface is usable
1730 * DDERR_ISLOST if the surface is lost
1731 * See IWineD3DSurface::IsLost for more details
1733 *****************************************************************************/
1734 static HRESULT WINAPI
1735 IDirectDrawSurfaceImpl_IsLost(IDirectDrawSurface7 *iface)
1737 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1739 TRACE("(%p)\n", This);
1741 EnterCriticalSection(&ddraw_cs);
1742 /* We lose the surface if the implementation was changed */
1743 if(This->ImplType != This->ddraw->ImplType)
1745 /* But this shouldn't happen. When we change the implementation,
1746 * all surfaces are re-created automatically, and their content
1749 ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1750 LeaveCriticalSection(&ddraw_cs);
1751 return DDERR_SURFACELOST;
1754 hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
1755 LeaveCriticalSection(&ddraw_cs);
1758 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1759 * WineD3D uses the same error for surfaces
1761 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
1766 /*****************************************************************************
1767 * IDirectDrawSurface7::Restore
1769 * Restores a lost surface. This makes the surface usable again, but
1770 * doesn't reload its old contents
1774 * See IWineD3DSurface::Restore for more details
1776 *****************************************************************************/
1777 static HRESULT WINAPI
1778 IDirectDrawSurfaceImpl_Restore(IDirectDrawSurface7 *iface)
1780 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1782 TRACE("(%p)\n", This);
1784 EnterCriticalSection(&ddraw_cs);
1785 if(This->ImplType != This->ddraw->ImplType)
1787 /* Call the recreation callback. Make sure to AddRef first */
1788 IDirectDrawSurface_AddRef(iface);
1789 IDirectDrawImpl_RecreateSurfacesCallback(iface,
1790 &This->surface_desc,
1791 NULL /* Not needed */);
1793 hr = IWineD3DSurface_Restore(This->WineD3DSurface);
1794 LeaveCriticalSection(&ddraw_cs);
1798 /*****************************************************************************
1799 * IDirectDrawSurface7::SetOverlayPosition
1801 * Changes the display coordinates of an overlay surface
1808 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
1809 *****************************************************************************/
1810 static HRESULT WINAPI
1811 IDirectDrawSurfaceImpl_SetOverlayPosition(IDirectDrawSurface7 *iface,
1815 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1817 TRACE("(%p)->(%d,%d): Relay\n", This, X, Y);
1819 EnterCriticalSection(&ddraw_cs);
1820 hr = IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
1823 LeaveCriticalSection(&ddraw_cs);
1827 /*****************************************************************************
1828 * IDirectDrawSurface7::UpdateOverlay
1830 * Modifies the attributes of an overlay surface.
1833 * SrcRect: The section of the source being used for the overlay
1834 * DstSurface: Address of the surface that is overlaid
1835 * DstRect: Place of the overlay
1836 * Flags: some DDOVER_* flags
1839 * DDERR_UNSUPPORTED, because we don't support overlays
1841 *****************************************************************************/
1842 static HRESULT WINAPI
1843 IDirectDrawSurfaceImpl_UpdateOverlay(IDirectDrawSurface7 *iface,
1845 IDirectDrawSurface7 *DstSurface,
1850 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1851 IDirectDrawSurfaceImpl *Dst = (IDirectDrawSurfaceImpl *)DstSurface;
1853 TRACE("(%p)->(%p,%p,%p,%x,%p): Relay\n", This, SrcRect, Dst, DstRect, Flags, FX);
1855 EnterCriticalSection(&ddraw_cs);
1856 hr = IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
1858 Dst ? Dst->WineD3DSurface : NULL,
1861 (WINEDDOVERLAYFX *) FX);
1862 LeaveCriticalSection(&ddraw_cs);
1864 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1865 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
1866 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
1872 /*****************************************************************************
1873 * IDirectDrawSurface7::UpdateOverlayDisplay
1875 * The DX7 sdk says that it's not implemented
1880 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
1882 *****************************************************************************/
1883 static HRESULT WINAPI
1884 IDirectDrawSurfaceImpl_UpdateOverlayDisplay(IDirectDrawSurface7 *iface,
1887 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1888 TRACE("(%p)->(%x)\n", This, Flags);
1889 return DDERR_UNSUPPORTED;
1892 /*****************************************************************************
1893 * IDirectDrawSurface7::UpdateOverlayZOrder
1895 * Sets an overlay's Z order
1898 * Flags: DDOVERZ_* flags
1899 * DDSRef: Defines the relative position in the overlay chain
1902 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
1904 *****************************************************************************/
1905 static HRESULT WINAPI
1906 IDirectDrawSurfaceImpl_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
1908 IDirectDrawSurface7 *DDSRef)
1910 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1911 IDirectDrawSurfaceImpl *Ref = (IDirectDrawSurfaceImpl *)DDSRef;
1914 TRACE("(%p)->(%x,%p): Relay\n", This, Flags, Ref);
1915 EnterCriticalSection(&ddraw_cs);
1916 hr = IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
1918 Ref ? Ref->WineD3DSurface : NULL);
1919 LeaveCriticalSection(&ddraw_cs);
1923 /*****************************************************************************
1924 * IDirectDrawSurface7::GetDDInterface
1926 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
1927 * surface belongs to
1930 * DD: Address to write the interface pointer to
1934 * DDERR_INVALIDPARAMS if DD is NULL
1936 *****************************************************************************/
1937 static HRESULT WINAPI
1938 IDirectDrawSurfaceImpl_GetDDInterface(IDirectDrawSurface7 *iface,
1941 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1943 TRACE("(%p)->(%p)\n",This,DD);
1946 return DDERR_INVALIDPARAMS;
1948 switch(This->version)
1955 *DD = &This->ddraw->IDirectDraw4_vtbl;
1959 *DD = &This->ddraw->IDirectDraw2_vtbl;
1963 *DD = &This->ddraw->IDirectDraw_vtbl;
1967 IUnknown_AddRef((IUnknown *)*DD);
1972 /* This seems also windows implementation specific - I don't think WineD3D needs this */
1973 static HRESULT WINAPI IDirectDrawSurfaceImpl_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
1975 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1976 volatile IDirectDrawSurfaceImpl* vThis = This;
1978 TRACE("(%p)\n",This);
1979 EnterCriticalSection(&ddraw_cs);
1980 /* A uniqueness value of 0 is apparently special.
1981 * This needs to be checked.
1982 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
1985 DWORD old_uniqueness_value = vThis->uniqueness_value;
1986 DWORD new_uniqueness_value = old_uniqueness_value+1;
1988 if (old_uniqueness_value == 0) break;
1989 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
1991 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
1992 old_uniqueness_value,
1993 new_uniqueness_value)
1994 == old_uniqueness_value)
1998 LeaveCriticalSection(&ddraw_cs);
2002 static HRESULT WINAPI IDirectDrawSurfaceImpl_GetUniquenessValue(IDirectDrawSurface7 *iface, LPDWORD pValue)
2004 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2006 TRACE("(%p)->(%p)\n",This,pValue);
2007 EnterCriticalSection(&ddraw_cs);
2008 *pValue = This->uniqueness_value;
2009 LeaveCriticalSection(&ddraw_cs);
2013 /*****************************************************************************
2014 * IDirectDrawSurface7::SetLOD
2016 * Sets the level of detail of a texture
2019 * MaxLOD: LOD to set
2023 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2025 *****************************************************************************/
2026 static HRESULT WINAPI
2027 IDirectDrawSurfaceImpl_SetLOD(IDirectDrawSurface7 *iface,
2030 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2032 TRACE("(%p)->(%d)\n", This, MaxLOD);
2034 EnterCriticalSection(&ddraw_cs);
2035 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2037 LeaveCriticalSection(&ddraw_cs);
2038 return DDERR_INVALIDOBJECT;
2041 if(!This->wineD3DTexture)
2043 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2044 LeaveCriticalSection(&ddraw_cs);
2045 return DDERR_INVALIDOBJECT;
2048 hr = IWineD3DBaseTexture_SetLOD(This->wineD3DTexture,
2050 LeaveCriticalSection(&ddraw_cs);
2054 /*****************************************************************************
2055 * IDirectDrawSurface7::GetLOD
2057 * Returns the level of detail of a Direct3D texture
2060 * MaxLOD: Address to write the LOD to
2064 * DDERR_INVALIDPARAMS if MaxLOD is NULL
2065 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2067 *****************************************************************************/
2068 static HRESULT WINAPI
2069 IDirectDrawSurfaceImpl_GetLOD(IDirectDrawSurface7 *iface,
2072 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2073 TRACE("(%p)->(%p)\n", This, MaxLOD);
2076 return DDERR_INVALIDPARAMS;
2078 EnterCriticalSection(&ddraw_cs);
2079 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2081 LeaveCriticalSection(&ddraw_cs);
2082 return DDERR_INVALIDOBJECT;
2085 *MaxLOD = IWineD3DBaseTexture_GetLOD(This->wineD3DTexture);
2086 LeaveCriticalSection(&ddraw_cs);
2090 /*****************************************************************************
2091 * IDirectDrawSurface7::BltFast
2093 * Performs a fast Blit.
2096 * dstx: The x coordinate to blit to on the destination
2097 * dsty: The y coordinate to blit to on the destination
2098 * Source: The source surface
2099 * rsrc: The source rectangle
2100 * trans: Type of transfer. Some DDBLTFAST_* flags
2104 * For more details, see IWineD3DSurface::BltFast
2106 *****************************************************************************/
2107 static HRESULT WINAPI
2108 IDirectDrawSurfaceImpl_BltFast(IDirectDrawSurface7 *iface,
2111 IDirectDrawSurface7 *Source,
2115 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2116 IDirectDrawSurfaceImpl *src = (IDirectDrawSurfaceImpl *)Source;
2118 TRACE("(%p)->(%d,%d,%p,%p,%d): Relay\n", This, dstx, dsty, Source, rsrc, trans);
2120 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2125 if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2126 rsrc->right > src->surface_desc.dwWidth ||
2127 rsrc->bottom > src->surface_desc.dwHeight)
2129 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2130 return DDERR_INVALIDRECT;
2132 if(dstx + rsrc->right - rsrc->left > This->surface_desc.dwWidth ||
2133 dsty + rsrc->bottom - rsrc->top > This->surface_desc.dwHeight)
2135 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT\n");
2136 return DDERR_INVALIDRECT;
2141 if(dstx + src->surface_desc.dwWidth > This->surface_desc.dwWidth ||
2142 dsty + src->surface_desc.dwHeight > This->surface_desc.dwHeight)
2144 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT\n");
2145 return DDERR_INVALIDRECT;
2149 EnterCriticalSection(&ddraw_cs);
2150 hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
2152 src ? src->WineD3DSurface : NULL,
2155 LeaveCriticalSection(&ddraw_cs);
2158 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
2159 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
2164 /*****************************************************************************
2165 * IDirectDrawSurface7::GetClipper
2167 * Returns the IDirectDrawClipper interface of the clipper assigned to this
2171 * Clipper: Address to store the interface pointer at
2175 * DDERR_INVALIDPARAMS if Clipper is NULL
2176 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
2178 *****************************************************************************/
2179 static HRESULT WINAPI
2180 IDirectDrawSurfaceImpl_GetClipper(IDirectDrawSurface7 *iface,
2181 IDirectDrawClipper **Clipper)
2183 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2184 TRACE("(%p)->(%p)\n", This, Clipper);
2188 LeaveCriticalSection(&ddraw_cs);
2189 return DDERR_INVALIDPARAMS;
2192 EnterCriticalSection(&ddraw_cs);
2193 if(This->clipper == NULL)
2195 LeaveCriticalSection(&ddraw_cs);
2196 return DDERR_NOCLIPPERATTACHED;
2199 *Clipper = (IDirectDrawClipper *)This->clipper;
2200 IDirectDrawClipper_AddRef(*Clipper);
2201 LeaveCriticalSection(&ddraw_cs);
2205 /*****************************************************************************
2206 * IDirectDrawSurface7::SetClipper
2208 * Sets a clipper for the surface
2211 * Clipper: IDirectDrawClipper interface of the clipper to set
2216 *****************************************************************************/
2217 static HRESULT WINAPI
2218 IDirectDrawSurfaceImpl_SetClipper(IDirectDrawSurface7 *iface,
2219 IDirectDrawClipper *Clipper)
2221 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2222 IDirectDrawClipperImpl *oldClipper = This->clipper;
2225 TRACE("(%p)->(%p)\n",This,Clipper);
2227 EnterCriticalSection(&ddraw_cs);
2228 if ((IDirectDrawClipperImpl *)Clipper == This->clipper)
2230 LeaveCriticalSection(&ddraw_cs);
2234 This->clipper = (IDirectDrawClipperImpl *)Clipper;
2236 if (Clipper != NULL)
2237 IDirectDrawClipper_AddRef(Clipper);
2239 IDirectDrawClipper_Release((IDirectDrawClipper *)oldClipper);
2241 hr = IWineD3DSurface_SetClipper(This->WineD3DSurface, This->clipper ? This->clipper->wineD3DClipper : NULL);
2243 if(This->wineD3DSwapChain) {
2246 IDirectDrawClipper_GetHWnd(Clipper, &clipWindow);
2250 IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2253 IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2254 This->ddraw->d3d_window);
2258 LeaveCriticalSection(&ddraw_cs);
2262 /*****************************************************************************
2263 * IDirectDrawSurface7::SetSurfaceDesc
2265 * Sets the surface description. It can override the pixel format, the surface
2267 * It's not really tested.
2270 * DDSD: Pointer to the new surface description to set
2275 * DDERR_INVALIDPARAMS if DDSD is NULL
2277 *****************************************************************************/
2278 static HRESULT WINAPI
2279 IDirectDrawSurfaceImpl_SetSurfaceDesc(IDirectDrawSurface7 *iface,
2280 DDSURFACEDESC2 *DDSD,
2283 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2284 WINED3DFORMAT newFormat = WINED3DFMT_UNKNOWN;
2286 TRACE("(%p)->(%p,%x)\n", This, DDSD, Flags);
2289 return DDERR_INVALIDPARAMS;
2291 EnterCriticalSection(&ddraw_cs);
2292 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2294 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2296 if(newFormat == WINED3DFMT_UNKNOWN)
2298 ERR("Requested to set an unknown pixelformat\n");
2299 LeaveCriticalSection(&ddraw_cs);
2300 return DDERR_INVALIDPARAMS;
2302 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2304 hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2308 LeaveCriticalSection(&ddraw_cs);
2313 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2315 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2317 (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2319 if (DDSD->dwFlags & DDSD_CKDESTBLT)
2321 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2323 (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2325 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2327 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2329 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2331 if (DDSD->dwFlags & DDSD_CKSRCBLT)
2333 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2335 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2337 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2339 hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2340 if(hr != WINED3D_OK)
2342 /* No need for a trace here, wined3d does that for us */
2345 case WINED3DERR_INVALIDCALL:
2346 LeaveCriticalSection(&ddraw_cs);
2347 return DDERR_INVALIDPARAMS;
2354 This->surface_desc = *DDSD;
2356 LeaveCriticalSection(&ddraw_cs);
2360 /*****************************************************************************
2361 * IDirectDrawSurface7::GetPalette
2363 * Returns the IDirectDrawPalette interface of the palette currently assigned
2367 * Pal: Address to write the interface pointer to
2371 * DDERR_INVALIDPARAMS if Pal is NULL
2373 *****************************************************************************/
2374 static HRESULT WINAPI
2375 IDirectDrawSurfaceImpl_GetPalette(IDirectDrawSurface7 *iface,
2376 IDirectDrawPalette **Pal)
2378 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2379 IWineD3DPalette *wPal;
2381 TRACE("(%p)->(%p): Relay\n", This, Pal);
2384 return DDERR_INVALIDPARAMS;
2386 EnterCriticalSection(&ddraw_cs);
2387 hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
2390 LeaveCriticalSection(&ddraw_cs);
2396 hr = IWineD3DPalette_GetParent(wPal, (IUnknown **) Pal);
2401 hr = DDERR_NOPALETTEATTACHED;
2404 LeaveCriticalSection(&ddraw_cs);
2408 /*****************************************************************************
2411 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2412 * recursively in the surface tree
2414 *****************************************************************************/
2418 WINEDDCOLORKEY *CKey;
2422 static HRESULT WINAPI
2423 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2424 DDSURFACEDESC2 *desc,
2427 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)surface;
2428 struct SCKContext *ctx = context;
2431 hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2436 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2440 IDirectDrawSurface7_EnumAttachedSurfaces(surface,
2443 IDirectDrawSurface7_Release(surface);
2444 return DDENUMRET_OK;
2447 /*****************************************************************************
2448 * IDirectDrawSurface7::SetColorKey
2450 * Sets the color keying options for the surface. Observations showed that
2451 * in case of complex surfaces the color key has to be assigned to all
2456 * CKey: The new color key
2460 * See IWineD3DSurface::SetColorKey for details
2462 *****************************************************************************/
2463 static HRESULT WINAPI
2464 IDirectDrawSurfaceImpl_SetColorKey(IDirectDrawSurface7 *iface,
2468 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2469 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) CKey, Flags };
2470 TRACE("(%p)->(%x,%p)\n", This, Flags, CKey);
2472 EnterCriticalSection(&ddraw_cs);
2475 switch (Flags & ~DDCKEY_COLORSPACE)
2477 case DDCKEY_DESTBLT:
2478 This->surface_desc.ddckCKDestBlt = *CKey;
2479 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2482 case DDCKEY_DESTOVERLAY:
2483 This->surface_desc.u3.ddckCKDestOverlay = *CKey;
2484 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2487 case DDCKEY_SRCOVERLAY:
2488 This->surface_desc.ddckCKSrcOverlay = *CKey;
2489 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2493 This->surface_desc.ddckCKSrcBlt = *CKey;
2494 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2498 LeaveCriticalSection(&ddraw_cs);
2499 return DDERR_INVALIDPARAMS;
2504 switch (Flags & ~DDCKEY_COLORSPACE)
2506 case DDCKEY_DESTBLT:
2507 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2510 case DDCKEY_DESTOVERLAY:
2511 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2514 case DDCKEY_SRCOVERLAY:
2515 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2519 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2523 LeaveCriticalSection(&ddraw_cs);
2524 return DDERR_INVALIDPARAMS;
2527 ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2530 IDirectDrawSurface7_EnumAttachedSurfaces(iface,
2533 LeaveCriticalSection(&ddraw_cs);
2536 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2537 default: return ctx.ret;
2541 /*****************************************************************************
2542 * IDirectDrawSurface7::SetPalette
2544 * Assigns a DirectDrawPalette object to the surface
2547 * Pal: Interface to the palette to set
2552 *****************************************************************************/
2553 static HRESULT WINAPI
2554 IDirectDrawSurfaceImpl_SetPalette(IDirectDrawSurface7 *iface,
2555 IDirectDrawPalette *Pal)
2557 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2558 IDirectDrawPalette *oldPal;
2559 IDirectDrawSurfaceImpl *surf;
2560 IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
2562 TRACE("(%p)->(%p)\n", This, Pal);
2564 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
2565 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
2566 return DDERR_INVALIDPIXELFORMAT;
2569 /* Find the old palette */
2570 EnterCriticalSection(&ddraw_cs);
2571 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2572 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2574 LeaveCriticalSection(&ddraw_cs);
2577 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
2579 /* Set the new Palette */
2580 IWineD3DSurface_SetPalette(This->WineD3DSurface,
2581 PalImpl ? PalImpl->wineD3DPalette : NULL);
2582 /* AddRef the Palette */
2583 if(Pal) IDirectDrawPalette_AddRef(Pal);
2585 /* Release the old palette */
2586 if(oldPal) IDirectDrawPalette_Release(oldPal);
2588 /* If this is a front buffer, also update the back buffers
2589 * TODO: How do things work for palettized cube textures?
2591 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2593 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2594 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2599 IDirectDrawSurface7 *attach;
2601 hr = IDirectDrawSurface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &caps2, &attach);
2607 TRACE("Setting palette on %p\n", attach);
2608 IDirectDrawSurface7_SetPalette(attach,
2610 surf = (IDirectDrawSurfaceImpl *)attach;
2611 IDirectDrawSurface7_Release(attach);
2615 LeaveCriticalSection(&ddraw_cs);
2619 /*****************************************************************************
2621 *****************************************************************************/
2623 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl =
2626 IDirectDrawSurfaceImpl_QueryInterface,
2627 IDirectDrawSurfaceImpl_AddRef,
2628 IDirectDrawSurfaceImpl_Release,
2629 /*** IDirectDrawSurface ***/
2630 IDirectDrawSurface7Impl_AddAttachedSurface,
2631 IDirectDrawSurfaceImpl_AddOverlayDirtyRect,
2632 IDirectDrawSurfaceImpl_Blt,
2633 IDirectDrawSurfaceImpl_BltBatch,
2634 IDirectDrawSurfaceImpl_BltFast,
2635 IDirectDrawSurfaceImpl_DeleteAttachedSurface,
2636 IDirectDrawSurfaceImpl_EnumAttachedSurfaces,
2637 IDirectDrawSurfaceImpl_EnumOverlayZOrders,
2638 IDirectDrawSurfaceImpl_Flip,
2639 IDirectDrawSurfaceImpl_GetAttachedSurface,
2640 IDirectDrawSurfaceImpl_GetBltStatus,
2641 IDirectDrawSurfaceImpl_GetCaps,
2642 IDirectDrawSurfaceImpl_GetClipper,
2643 IDirectDrawSurfaceImpl_GetColorKey,
2644 IDirectDrawSurfaceImpl_GetDC,
2645 IDirectDrawSurfaceImpl_GetFlipStatus,
2646 IDirectDrawSurfaceImpl_GetOverlayPosition,
2647 IDirectDrawSurfaceImpl_GetPalette,
2648 IDirectDrawSurfaceImpl_GetPixelFormat,
2649 IDirectDrawSurfaceImpl_GetSurfaceDesc,
2650 IDirectDrawSurfaceImpl_Initialize,
2651 IDirectDrawSurfaceImpl_IsLost,
2652 IDirectDrawSurfaceImpl_Lock,
2653 IDirectDrawSurfaceImpl_ReleaseDC,
2654 IDirectDrawSurfaceImpl_Restore,
2655 IDirectDrawSurfaceImpl_SetClipper,
2656 IDirectDrawSurfaceImpl_SetColorKey,
2657 IDirectDrawSurfaceImpl_SetOverlayPosition,
2658 IDirectDrawSurfaceImpl_SetPalette,
2659 IDirectDrawSurfaceImpl_Unlock,
2660 IDirectDrawSurfaceImpl_UpdateOverlay,
2661 IDirectDrawSurfaceImpl_UpdateOverlayDisplay,
2662 IDirectDrawSurfaceImpl_UpdateOverlayZOrder,
2663 /*** IDirectDrawSurface2 ***/
2664 IDirectDrawSurfaceImpl_GetDDInterface,
2665 IDirectDrawSurfaceImpl_PageLock,
2666 IDirectDrawSurfaceImpl_PageUnlock,
2667 /*** IDirectDrawSurface3 ***/
2668 IDirectDrawSurfaceImpl_SetSurfaceDesc,
2669 /*** IDirectDrawSurface4 ***/
2670 IDirectDrawSurfaceImpl_SetPrivateData,
2671 IDirectDrawSurfaceImpl_GetPrivateData,
2672 IDirectDrawSurfaceImpl_FreePrivateData,
2673 IDirectDrawSurfaceImpl_GetUniquenessValue,
2674 IDirectDrawSurfaceImpl_ChangeUniquenessValue,
2675 /*** IDirectDrawSurface7 ***/
2676 IDirectDrawSurfaceImpl_SetPriority,
2677 IDirectDrawSurfaceImpl_GetPriority,
2678 IDirectDrawSurfaceImpl_SetLOD,
2679 IDirectDrawSurfaceImpl_GetLOD