shlwapi/tests: Run more language dependent tests only on English locales.
[wine.git] / dlls / ddraw / surface.c
blob15ed3c425a24cfd2f642966c3e1f695cee321e25
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
26 #include "config.h"
27 #include "wine/port.h"
29 #include <assert.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <stdlib.h>
34 #define COBJMACROS
35 #define NONAMELESSUNION
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "wingdi.h"
41 #include "wine/exception.h"
43 #include "ddraw.h"
44 #include "d3d.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)
64 * Params:
65 * riid: The interface id queried for
66 * obj: Address to write the pointer to
68 * Returns:
69 * S_OK on success
70 * E_NOINTERFACE if the requested interface wasn't found
72 *****************************************************************************/
73 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
75 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
77 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
78 *obj = NULL;
80 if(!riid)
81 return DDERR_INVALIDPARAMS;
83 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),obj);
84 if (IsEqualGUID(riid, &IID_IUnknown)
85 || IsEqualGUID(riid, &IID_IDirectDrawSurface7)
86 || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
88 IUnknown_AddRef(iface);
89 *obj = iface;
90 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
91 return S_OK;
93 else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
94 || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
95 || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
97 IUnknown_AddRef(iface);
98 *obj = &This->IDirectDrawSurface3_vtbl;
99 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
100 return S_OK;
102 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
104 IUnknown_AddRef(iface);
105 *obj = &This->IDirectDrawGammaControl_vtbl;
106 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
107 return S_OK;
109 else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
110 IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
111 IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
113 IDirect3DDevice7 *d3d;
115 /* Call into IDirect3D7 for creation */
116 IDirect3D7_CreateDevice((IDirect3D7 *)&This->ddraw->IDirect3D7_vtbl, riid, (IDirectDrawSurface7 *)This, &d3d);
118 if (d3d)
120 *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
121 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
122 return S_OK;
125 WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
126 return E_NOINTERFACE;
128 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
129 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
131 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
133 *obj = &This->IDirect3DTexture_vtbl;
134 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
136 else
138 *obj = &This->IDirect3DTexture2_vtbl;
139 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
141 IUnknown_AddRef( (IUnknown *) *obj);
142 return S_OK;
145 ERR("No interface\n");
146 return E_NOINTERFACE;
149 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
151 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
153 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), riid, object);
156 /*****************************************************************************
157 * IDirectDrawSurface7::AddRef
159 * A normal addref implementation
161 * Returns:
162 * The new refcount
164 *****************************************************************************/
165 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
167 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
168 ULONG refCount = InterlockedIncrement(&This->ref);
170 if (refCount == 1 && This->WineD3DSurface)
172 EnterCriticalSection(&ddraw_cs);
173 IWineD3DSurface_AddRef(This->WineD3DSurface);
174 LeaveCriticalSection(&ddraw_cs);
177 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
178 return refCount;
181 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
183 TRACE("iface %p.\n", iface);
185 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_surface3(iface));
188 /*****************************************************************************
189 * ddraw_surface_destroy
191 * A helper function for IDirectDrawSurface7::Release
193 * Frees the surface, regardless of its refcount.
194 * See IDirectDrawSurface7::Release for more information
196 * Params:
197 * This: Surface to free
199 *****************************************************************************/
200 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
202 TRACE("(%p)\n", This);
204 /* Check the refcount and give a warning */
205 if(This->ref > 1)
207 /* This can happen when a complex surface is destroyed,
208 * because the 2nd surface was addref()ed when the app
209 * called GetAttachedSurface
211 WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
214 /* Check for attached surfaces and detach them */
215 if(This->first_attached != This)
217 /* Well, this shouldn't happen: The surface being attached is addref()ed
218 * in AddAttachedSurface, so it shouldn't be released until DeleteAttachedSurface
219 * is called, because the refcount is held. It looks like the app released()
220 * it often enough to force this
222 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)This->first_attached;
223 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)This;
225 FIXME("(%p) Freeing a surface that is attached to surface %p\n", This, This->first_attached);
227 /* The refcount will drop to -1 here */
228 if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
230 ERR("(%p) DeleteAttachedSurface failed!\n", This);
234 while(This->next_attached != NULL)
236 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)This;
237 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)This->next_attached;
239 if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
241 ERR("(%p) DeleteAttachedSurface failed!\n", This);
242 assert(0);
246 /* Now destroy the surface. Wait: It could have been released if we are a texture */
247 if(This->WineD3DSurface)
248 IWineD3DSurface_Release(This->WineD3DSurface);
250 /* Having a texture handle set implies that the device still exists */
251 if(This->Handle)
253 ddraw_free_handle(&This->ddraw->d3ddevice->handle_table, This->Handle - 1, DDRAW_HANDLE_SURFACE);
256 /* Reduce the ddraw surface count */
257 InterlockedDecrement(&This->ddraw->surfaces);
258 list_remove(&This->surface_list_entry);
260 HeapFree(GetProcessHeap(), 0, This);
263 /*****************************************************************************
264 * IDirectDrawSurface7::Release
266 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
267 * surface is destroyed.
269 * Destroying the surface is a bit tricky. For the connection between
270 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
271 * It has a nice graph explaining the connection.
273 * What happens here is basically this:
274 * When a surface is destroyed, its WineD3DSurface is released,
275 * and the refcount of the DirectDraw interface is reduced by 1. If it has
276 * complex surfaces attached to it, then these surfaces are destroyed too,
277 * regardless of their refcount. If any surface being destroyed has another
278 * surface attached to it (with a "soft" attachment, not complex), then
279 * this surface is detached with DeleteAttachedSurface.
281 * When the surface is a texture, the WineD3DTexture is released.
282 * If the surface is the Direct3D render target, then the D3D
283 * capabilities of the WineD3DDevice are uninitialized, which causes the
284 * swapchain to be released.
286 * When a complex sublevel falls to ref zero, then this is ignored.
288 * Returns:
289 * The new refcount
291 *****************************************************************************/
292 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
294 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
295 ULONG ref;
296 TRACE("(%p) : Releasing from %d\n", This, This->ref);
297 ref = InterlockedDecrement(&This->ref);
299 if (ref == 0)
302 IDirectDrawSurfaceImpl *surf;
303 IDirectDrawImpl *ddraw;
304 IUnknown *ifaceToRelease = This->ifaceToRelease;
305 UINT i;
307 /* Complex attached surfaces are destroyed implicitly when the root is released */
308 EnterCriticalSection(&ddraw_cs);
309 if(!This->is_complex_root)
311 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
312 LeaveCriticalSection(&ddraw_cs);
313 return ref;
315 ddraw = This->ddraw;
317 /* If it's a texture, destroy the WineD3DTexture.
318 * WineD3D will destroy the IParent interfaces
319 * of the sublevels, which destroys the WineD3DSurfaces.
320 * Set the surfaces to NULL to avoid destroying them again later
322 if(This->wineD3DTexture)
324 IWineD3DBaseTexture_Release(This->wineD3DTexture);
326 /* If it's the RenderTarget, destroy the d3ddevice */
327 else if(This->wineD3DSwapChain)
329 if((ddraw->d3d_initialized) && (This == ddraw->d3d_target)) {
330 TRACE("(%p) Destroying the render target, uninitializing D3D\n", This);
332 /* Unset any index buffer, just to be sure */
333 IWineD3DDevice_SetIndexBuffer(ddraw->wineD3DDevice, NULL, WINED3DFMT_UNKNOWN);
334 IWineD3DDevice_SetDepthStencilSurface(ddraw->wineD3DDevice, NULL);
335 IWineD3DDevice_SetVertexDeclaration(ddraw->wineD3DDevice, NULL);
336 for(i = 0; i < ddraw->numConvertedDecls; i++)
338 IWineD3DVertexDeclaration_Release(ddraw->decls[i].decl);
340 HeapFree(GetProcessHeap(), 0, ddraw->decls);
341 ddraw->numConvertedDecls = 0;
343 if (FAILED(IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain)))
345 /* Not good */
346 ERR("(%p) Failed to uninit 3D\n", This);
348 else
350 /* Free the d3d window if one was created */
351 if(ddraw->d3d_window != 0 && ddraw->d3d_window != ddraw->dest_window)
353 TRACE(" (%p) Destroying the hidden render window %p\n", This, ddraw->d3d_window);
354 DestroyWindow(ddraw->d3d_window);
355 ddraw->d3d_window = 0;
357 /* Unset the pointers */
360 This->wineD3DSwapChain = NULL; /* Uninit3D releases the swapchain */
361 ddraw->d3d_initialized = FALSE;
362 ddraw->d3d_target = NULL;
363 } else {
364 IWineD3DDevice_UninitGDI(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain);
365 This->wineD3DSwapChain = NULL;
368 /* Reset to the default surface implementation type. This is needed if apps use
369 * non render target surfaces and expect blits to work after destroying the render
370 * target.
372 * TODO: Recreate existing offscreen surfaces
374 ddraw->ImplType = DefaultSurfaceType;
376 /* Write a trace because D3D unloading was the reason for many
377 * crashes during development.
379 TRACE("(%p) D3D unloaded\n", This);
382 /* The refcount test shows that the palette is detached when the surface is destroyed */
383 IDirectDrawSurface7_SetPalette((IDirectDrawSurface7 *)This, NULL);
385 /* Loop through all complex attached surfaces,
386 * and destroy them.
388 * Yet again, only the root can have more than one complexly attached surface, all the others
389 * have a total of one;
391 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
393 if(!This->complex_array[i]) break;
395 surf = This->complex_array[i];
396 This->complex_array[i] = NULL;
397 while(surf)
399 IDirectDrawSurfaceImpl *destroy = surf;
400 surf = surf->complex_array[0]; /* Iterate through the "tree" */
401 ddraw_surface_destroy(destroy); /* Destroy it */
405 /* Destroy the root surface. */
406 ddraw_surface_destroy(This);
408 /* Reduce the ddraw refcount */
409 if(ifaceToRelease) IUnknown_Release(ifaceToRelease);
410 LeaveCriticalSection(&ddraw_cs);
413 return ref;
416 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
418 TRACE("iface %p.\n", iface);
420 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_surface3(iface));
423 /*****************************************************************************
424 * IDirectDrawSurface7::GetAttachedSurface
426 * Returns an attached surface with the requested caps. Surface attachment
427 * and complex surfaces are not clearly described by the MSDN or sdk,
428 * so this method is tricky and likely to contain problems.
429 * This implementation searches the complex list first, then the
430 * attachment chain.
432 * The chains are searched from This down to the last surface in the chain,
433 * not from the first element in the chain. The first surface found is
434 * returned. The MSDN says that this method fails if more than one surface
435 * matches the caps, but it is not sure if that is right. The attachment
436 * structure may not even allow two matching surfaces.
438 * The found surface is AddRef-ed before it is returned.
440 * Params:
441 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
442 * Surface: Address to store the found surface
444 * Returns:
445 * DD_OK on success
446 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
447 * DDERR_NOTFOUND if no surface was found
449 *****************************************************************************/
450 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
451 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
453 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
454 IDirectDrawSurfaceImpl *surf;
455 DDSCAPS2 our_caps;
456 int i;
458 TRACE("(%p)->(%p,%p)\n", This, Caps, Surface);
459 EnterCriticalSection(&ddraw_cs);
461 if(This->version < 7)
463 /* Earlier dx apps put garbage into these members, clear them */
464 our_caps.dwCaps = Caps->dwCaps;
465 our_caps.dwCaps2 = 0;
466 our_caps.dwCaps3 = 0;
467 our_caps.dwCaps4 = 0;
469 else
471 our_caps = *Caps;
474 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 */
476 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
478 surf = This->complex_array[i];
479 if(!surf) break;
481 if (TRACE_ON(ddraw))
483 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
484 surf->surface_desc.ddsCaps.dwCaps,
485 surf->surface_desc.ddsCaps.dwCaps2,
486 surf->surface_desc.ddsCaps.dwCaps3,
487 surf->surface_desc.ddsCaps.dwCaps4);
490 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
491 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
493 /* MSDN: "This method fails if more than one surface is attached
494 * that matches the capabilities requested."
496 * Not sure how to test this.
499 TRACE("(%p): Returning surface %p\n", This, surf);
500 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
501 *Surface = (IDirectDrawSurface7 *)surf;
502 ddraw_surface7_AddRef(*Surface);
503 LeaveCriticalSection(&ddraw_cs);
504 return DD_OK;
508 /* Next, look at the attachment chain */
509 surf = This;
511 while( (surf = surf->next_attached) )
513 if (TRACE_ON(ddraw))
515 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
516 surf->surface_desc.ddsCaps.dwCaps,
517 surf->surface_desc.ddsCaps.dwCaps2,
518 surf->surface_desc.ddsCaps.dwCaps3,
519 surf->surface_desc.ddsCaps.dwCaps4);
522 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
523 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
525 TRACE("(%p): Returning surface %p\n", This, surf);
526 *Surface = (IDirectDrawSurface7 *)surf;
527 ddraw_surface7_AddRef(*Surface);
528 LeaveCriticalSection(&ddraw_cs);
529 return DD_OK;
533 TRACE("(%p) Didn't find a valid surface\n", This);
534 LeaveCriticalSection(&ddraw_cs);
536 *Surface = NULL;
537 return DDERR_NOTFOUND;
540 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
541 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
543 IDirectDrawSurface7 *attachment7;
544 DDSCAPS2 caps2;
545 HRESULT hr;
547 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
549 caps2.dwCaps = caps->dwCaps;
550 caps2.dwCaps2 = 0;
551 caps2.dwCaps3 = 0;
552 caps2.dwCaps4 = 0;
554 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface),
555 &caps2, &attachment7);
556 if (FAILED(hr)) *attachment = NULL;
557 else *attachment = attachment7 ?
558 (IDirectDrawSurface3 *)&((IDirectDrawSurfaceImpl *)attachment7)->IDirectDrawSurface3_vtbl : NULL;
560 return hr;
563 /*****************************************************************************
564 * IDirectDrawSurface7::Lock
566 * Locks the surface and returns a pointer to the surface's memory
568 * Params:
569 * Rect: Rectangle to lock. If NULL, the whole surface is locked
570 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
571 * Flags: Locking flags, e.g Read only or write only
572 * h: An event handle that's not used and must be NULL
574 * Returns:
575 * DD_OK on success
576 * DDERR_INVALIDPARAMS if DDSD is NULL
577 * For more details, see IWineD3DSurface::LockRect
579 *****************************************************************************/
580 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
581 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
583 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
584 WINED3DLOCKED_RECT LockedRect;
585 HRESULT hr;
586 TRACE("(%p)->(%p,%p,%x,%p)\n", This, Rect, DDSD, Flags, h);
588 if(!DDSD)
589 return DDERR_INVALIDPARAMS;
591 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
592 EnterCriticalSection(&ddraw_cs);
594 /* Should I check for the handle to be NULL?
596 * The DDLOCK flags and the D3DLOCK flags are equal
597 * for the supported values. The others are ignored by WineD3D
600 if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
601 DDSD->dwSize != sizeof(DDSURFACEDESC2))
603 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
604 LeaveCriticalSection(&ddraw_cs);
605 return DDERR_INVALIDPARAMS;
608 /* Windows zeroes this if the rect is invalid */
609 DDSD->lpSurface = 0;
611 if (Rect)
613 if ((Rect->left < 0)
614 || (Rect->top < 0)
615 || (Rect->left > Rect->right)
616 || (Rect->top > Rect->bottom)
617 || (Rect->right > This->surface_desc.dwWidth)
618 || (Rect->bottom > This->surface_desc.dwHeight))
620 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
621 LeaveCriticalSection(&ddraw_cs);
622 return DDERR_INVALIDPARAMS;
626 hr = IWineD3DSurface_LockRect(This->WineD3DSurface,
627 &LockedRect,
628 Rect,
629 Flags);
630 if(hr != D3D_OK)
632 LeaveCriticalSection(&ddraw_cs);
633 switch(hr)
635 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
636 * specific error. But since IWineD3DSurface::LockRect returns that error in this
637 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
638 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
639 * is much easier to do it in one place in ddraw
641 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
642 default: return hr;
646 /* Override the memory area. The pitch should be set already. Strangely windows
647 * does not set the LPSURFACE flag on locked surfaces !?!.
648 * DDSD->dwFlags |= DDSD_LPSURFACE;
650 This->surface_desc.lpSurface = LockedRect.pBits;
651 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
653 TRACE("locked surface returning description :\n");
654 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
656 LeaveCriticalSection(&ddraw_cs);
657 return DD_OK;
660 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
661 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
663 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
664 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
666 return ddraw_surface7_Lock((IDirectDrawSurface7 *)surface_from_surface3(iface),
667 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
670 /*****************************************************************************
671 * IDirectDrawSurface7::Unlock
673 * Unlocks an locked surface
675 * Params:
676 * Rect: Not used by this implementation
678 * Returns:
679 * D3D_OK on success
680 * For more details, see IWineD3DSurface::UnlockRect
682 *****************************************************************************/
683 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
685 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
686 HRESULT hr;
687 TRACE("(%p)->(%p)\n", This, pRect);
689 EnterCriticalSection(&ddraw_cs);
690 hr = IWineD3DSurface_UnlockRect(This->WineD3DSurface);
691 if(SUCCEEDED(hr))
693 This->surface_desc.lpSurface = NULL;
695 LeaveCriticalSection(&ddraw_cs);
696 return hr;
699 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
701 TRACE("iface %p, data %p.\n", iface, data);
703 /* data might not be the LPRECT of later versions, so drop it. */
704 return ddraw_surface7_Unlock((IDirectDrawSurface7 *)surface_from_surface3(iface), NULL);
707 /*****************************************************************************
708 * IDirectDrawSurface7::Flip
710 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
711 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
712 * the flip target is passed to WineD3D, even if the app didn't specify one
714 * Params:
715 * DestOverride: Specifies the surface that will become the new front
716 * buffer. If NULL, the current back buffer is used
717 * Flags: some DirectDraw flags, see include/ddraw.h
719 * Returns:
720 * DD_OK on success
721 * DDERR_NOTFLIPPABLE if no flip target could be found
722 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
723 * For more details, see IWineD3DSurface::Flip
725 *****************************************************************************/
726 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
728 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
729 IDirectDrawSurfaceImpl *Override = (IDirectDrawSurfaceImpl *)DestOverride;
730 IDirectDrawSurface7 *Override7;
731 HRESULT hr;
732 TRACE("(%p)->(%p,%x)\n", This, DestOverride, Flags);
734 /* Flip has to be called from a front buffer
735 * What about overlay surfaces, AFAIK they can flip too?
737 if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
738 return DDERR_INVALIDOBJECT; /* Unchecked */
740 EnterCriticalSection(&ddraw_cs);
742 /* WineD3D doesn't keep track of attached surface, so find the target */
743 if(!Override)
745 DDSCAPS2 Caps;
747 memset(&Caps, 0, sizeof(Caps));
748 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
749 hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
750 if(hr != DD_OK)
752 ERR("Can't find a flip target\n");
753 LeaveCriticalSection(&ddraw_cs);
754 return DDERR_NOTFLIPPABLE; /* Unchecked */
756 Override = (IDirectDrawSurfaceImpl *)Override7;
758 /* For the GetAttachedSurface */
759 ddraw_surface7_Release(Override7);
762 hr = IWineD3DSurface_Flip(This->WineD3DSurface,
763 Override->WineD3DSurface,
764 Flags);
765 LeaveCriticalSection(&ddraw_cs);
766 return hr;
769 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
771 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
773 return ddraw_surface7_Flip((IDirectDrawSurface7 *)surface_from_surface3(iface),
774 dst ? (IDirectDrawSurface7 *)surface_from_surface3(dst) : NULL, flags);
777 /*****************************************************************************
778 * IDirectDrawSurface7::Blt
780 * Performs a blit on the surface
782 * Params:
783 * DestRect: Destination rectangle, can be NULL
784 * SrcSurface: Source surface, can be NULL
785 * SrcRect: Source rectangle, can be NULL
786 * Flags: Blt flags
787 * DDBltFx: Some extended blt parameters, connected to the flags
789 * Returns:
790 * D3D_OK on success
791 * See IWineD3DSurface::Blt for more details
793 *****************************************************************************/
794 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
795 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
797 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
798 IDirectDrawSurfaceImpl *Src = (IDirectDrawSurfaceImpl *)SrcSurface;
799 HRESULT hr;
800 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, Src, SrcRect, Flags, DDBltFx);
802 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
803 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
805 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
806 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
807 return DDERR_INVALIDPARAMS;
810 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
811 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
812 return DDERR_INVALIDPARAMS;
815 /* Sizes can change, therefore hold the lock when testing the rectangles */
816 EnterCriticalSection(&ddraw_cs);
817 if(DestRect)
819 if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
820 DestRect->right > This->surface_desc.dwWidth ||
821 DestRect->bottom > This->surface_desc.dwHeight)
823 WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
824 LeaveCriticalSection(&ddraw_cs);
825 return DDERR_INVALIDRECT;
828 if(Src && SrcRect)
830 if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
831 SrcRect->right > Src->surface_desc.dwWidth ||
832 SrcRect->bottom > Src->surface_desc.dwHeight)
834 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
835 LeaveCriticalSection(&ddraw_cs);
836 return DDERR_INVALIDRECT;
840 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
841 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
842 LeaveCriticalSection(&ddraw_cs);
843 return DDERR_INVALIDPARAMS;
846 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it does, copy the struct,
847 * and replace the ddraw surfaces with the wined3d surfaces
848 * So far no blitting operations using surfaces in the bltfx struct are supported anyway.
850 hr = IWineD3DSurface_Blt(This->WineD3DSurface,
851 DestRect,
852 Src ? Src->WineD3DSurface : NULL,
853 SrcRect,
854 Flags,
855 (WINEDDBLTFX *) DDBltFx,
856 WINED3DTEXF_POINT);
858 LeaveCriticalSection(&ddraw_cs);
859 switch(hr)
861 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
862 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
863 default: return hr;
867 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
868 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
870 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %p, flags %#x, fx %p.\n",
871 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
873 return ddraw_surface7_Blt((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_rect,
874 src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags, fx);
877 /*****************************************************************************
878 * IDirectDrawSurface7::AddAttachedSurface
880 * Attaches a surface to another surface. How the surface attachments work
881 * is not totally understood yet, and this method is prone to problems.
882 * he surface that is attached is AddRef-ed.
884 * Tests with complex surfaces suggest that the surface attachments form a
885 * tree, but no method to test this has been found yet.
887 * The attachment list consists of a first surface (first_attached) and
888 * for each surface a pointer to the next attached surface (next_attached).
889 * For the first surface, and a surface that has no attachments
890 * first_attached points to the surface itself. A surface that has
891 * no successors in the chain has next_attached set to NULL.
893 * Newly attached surfaces are attached right after the root surface.
894 * If a surface is attached to a complex surface compound, it's attached to
895 * the surface that the app requested, not the complex root. See
896 * GetAttachedSurface for a description how surfaces are found.
898 * This is how the current implementation works, and it was coded by looking
899 * at the needs of the applications.
901 * So far only Z-Buffer attachments are tested, and they are activated in
902 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
903 * Back buffers should work in 2D mode, but they are not tested(They can be
904 * attached in older iface versions). Rendering to the front buffer and
905 * switching between that and double buffering is not yet implemented in
906 * WineD3D, so for 3D it might have unexpected results.
908 * ddraw_surface_attach_surface is the real thing,
909 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
910 * performs additional checks. Version 7 of this interface is much more restrictive
911 * than its predecessors.
913 * Params:
914 * Attach: Surface to attach to iface
916 * Returns:
917 * DD_OK on success
918 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
920 *****************************************************************************/
921 static HRESULT WINAPI ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
923 TRACE("(%p)->(%p)\n", This, Surf);
925 if(Surf == This)
926 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
928 EnterCriticalSection(&ddraw_cs);
930 /* Check if the surface is already attached somewhere */
931 if( (Surf->next_attached != NULL) ||
932 (Surf->first_attached != Surf) )
934 /* TODO: Test for the structure of the manual attachment. Is it a chain or a list?
935 * What happens if one surface is attached to 2 different surfaces?
937 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);
938 LeaveCriticalSection(&ddraw_cs);
939 return DDERR_SURFACEALREADYATTACHED;
942 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
943 Surf->next_attached = This->next_attached;
944 Surf->first_attached = This->first_attached;
945 This->next_attached = Surf;
947 /* Check if the WineD3D depth stencil needs updating */
948 if(This->ddraw->d3ddevice)
950 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
953 ddraw_surface7_AddRef((IDirectDrawSurface7 *)Surf);
954 LeaveCriticalSection(&ddraw_cs);
955 return DD_OK;
958 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
960 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
961 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
963 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
964 if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
967 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
968 Surf->surface_desc.ddsCaps.dwCaps);
969 return DDERR_CANNOTATTACHSURFACE;
972 return ddraw_surface_attach_surface(This, Surf);
975 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
977 IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
978 IDirectDrawSurfaceImpl *attach_impl = surface_from_surface3(attachment);
980 TRACE("iface %p, attachment %p.\n", iface, attachment);
982 /* Tests suggest that
983 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
984 * -> offscreen plain surfaces can be attached to primaries
985 * -> primaries can be attached to offscreen plain surfaces
986 * -> z buffers can be attached to primaries */
987 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
988 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
990 /* Sizes have to match */
991 if (attach_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
992 || attach_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
994 WARN("Surface sizes do not match.\n");
995 return DDERR_CANNOTATTACHSURFACE;
997 /* OK */
999 else if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1000 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1002 /* OK */
1004 else
1006 WARN("Invalid attachment combination.\n");
1007 return DDERR_CANNOTATTACHSURFACE;
1010 return ddraw_surface_attach_surface(surface, attach_impl);
1013 /*****************************************************************************
1014 * IDirectDrawSurface7::DeleteAttachedSurface
1016 * Removes a surface from the attachment chain. The surface's refcount
1017 * is decreased by one after it has been removed
1019 * Params:
1020 * Flags: Some flags, not used by this implementation
1021 * Attach: Surface to detach
1023 * Returns:
1024 * DD_OK on success
1025 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1027 *****************************************************************************/
1028 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1029 DWORD Flags, IDirectDrawSurface7 *Attach)
1031 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1032 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
1033 IDirectDrawSurfaceImpl *Prev = This;
1034 TRACE("(%p)->(%08x,%p)\n", This, Flags, Surf);
1036 EnterCriticalSection(&ddraw_cs);
1037 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1039 LeaveCriticalSection(&ddraw_cs);
1040 return DDERR_CANNOTDETACHSURFACE;
1043 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1044 if (This->surface_desc.ddsCaps.dwCaps &
1045 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1047 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1048 /* FIXME: we should probably also subtract from dwMipMapCount of this
1049 * and all parent surfaces */
1052 /* Find the predecessor of the detached surface */
1053 while(Prev)
1055 if(Prev->next_attached == Surf) break;
1056 Prev = Prev->next_attached;
1059 /* There must be a surface, otherwise there's a bug */
1060 assert(Prev != NULL);
1062 /* Unchain the surface */
1063 Prev->next_attached = Surf->next_attached;
1064 Surf->next_attached = NULL;
1065 Surf->first_attached = Surf;
1067 /* Check if the WineD3D depth stencil needs updating */
1068 if(This->ddraw->d3ddevice)
1070 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1073 ddraw_surface7_Release(Attach);
1074 LeaveCriticalSection(&ddraw_cs);
1075 return DD_OK;
1078 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1079 DWORD flags, IDirectDrawSurface3 *attachment)
1081 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1083 return ddraw_surface7_DeleteAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
1084 attachment ? (IDirectDrawSurface7 *)surface_from_surface3(attachment) : NULL);
1087 /*****************************************************************************
1088 * IDirectDrawSurface7::AddOverlayDirtyRect
1090 * "This method is not currently implemented"
1092 * Params:
1093 * Rect: ?
1095 * Returns:
1096 * DDERR_UNSUPPORTED
1098 *****************************************************************************/
1099 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1101 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1102 TRACE("(%p)->(%p)\n",This,Rect);
1104 /* MSDN says it's not implemented. I could forward it to WineD3D,
1105 * then we'd implement it, but I don't think that's a good idea
1106 * (Stefan Dösinger)
1108 #if 0
1109 return IWineD3DSurface_AddOverlayDirtyRect(This->WineD3DSurface, pRect);
1110 #endif
1111 return DDERR_UNSUPPORTED; /* unchecked */
1114 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1116 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1118 return ddraw_surface7_AddOverlayDirtyRect((IDirectDrawSurface7 *)surface_from_surface3(iface), rect);
1121 /*****************************************************************************
1122 * IDirectDrawSurface7::GetDC
1124 * Returns a GDI device context for the surface
1126 * Params:
1127 * hdc: Address of a HDC variable to store the dc to
1129 * Returns:
1130 * DD_OK on success
1131 * DDERR_INVALIDPARAMS if hdc is NULL
1132 * For details, see IWineD3DSurface::GetDC
1134 *****************************************************************************/
1135 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1137 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1138 HRESULT hr;
1139 TRACE("(%p)->(%p): Relay\n", This, hdc);
1141 if(!hdc)
1142 return DDERR_INVALIDPARAMS;
1144 EnterCriticalSection(&ddraw_cs);
1145 hr = IWineD3DSurface_GetDC(This->WineD3DSurface,
1146 hdc);
1147 LeaveCriticalSection(&ddraw_cs);
1148 switch(hr)
1150 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1151 * touch *hdc
1153 case WINED3DERR_INVALIDCALL:
1154 if(hdc) *hdc = NULL;
1155 return DDERR_INVALIDPARAMS;
1157 default: return hr;
1161 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1163 TRACE("iface %p, dc %p.\n", iface, dc);
1165 return ddraw_surface7_GetDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1168 /*****************************************************************************
1169 * IDirectDrawSurface7::ReleaseDC
1171 * Releases the DC that was constructed with GetDC
1173 * Params:
1174 * hdc: HDC to release
1176 * Returns:
1177 * DD_OK on success
1178 * For more details, see IWineD3DSurface::ReleaseDC
1180 *****************************************************************************/
1181 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1183 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1184 HRESULT hr;
1185 TRACE("(%p)->(%p): Relay\n", This, hdc);
1187 EnterCriticalSection(&ddraw_cs);
1188 hr = IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
1189 LeaveCriticalSection(&ddraw_cs);
1190 return hr;
1193 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1195 TRACE("iface %p, dc %p.\n", iface, dc);
1197 return ddraw_surface7_ReleaseDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1200 /*****************************************************************************
1201 * IDirectDrawSurface7::GetCaps
1203 * Returns the surface's caps
1205 * Params:
1206 * Caps: Address to write the caps to
1208 * Returns:
1209 * DD_OK on success
1210 * DDERR_INVALIDPARAMS if Caps is NULL
1212 *****************************************************************************/
1213 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1215 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1216 TRACE("(%p)->(%p)\n",This,Caps);
1218 if(!Caps)
1219 return DDERR_INVALIDPARAMS;
1221 *Caps = This->surface_desc.ddsCaps;
1222 return DD_OK;
1225 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1227 DDSCAPS2 caps2;
1228 HRESULT hr;
1230 TRACE("iface %p, caps %p.\n", iface, caps);
1232 hr = ddraw_surface7_GetCaps((IDirectDrawSurface7 *)surface_from_surface3(iface), &caps2);
1233 if (FAILED(hr)) return hr;
1235 caps->dwCaps = caps2.dwCaps;
1236 return hr;
1239 /*****************************************************************************
1240 * IDirectDrawSurface7::SetPriority
1242 * Sets a texture priority for managed textures.
1244 * Params:
1245 * Priority: The new priority
1247 * Returns:
1248 * DD_OK on success
1249 * For more details, see IWineD3DSurface::SetPriority
1251 *****************************************************************************/
1252 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1254 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1255 HRESULT hr;
1256 TRACE("(%p)->(%d): Relay!\n",This,Priority);
1258 EnterCriticalSection(&ddraw_cs);
1259 hr = IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
1260 LeaveCriticalSection(&ddraw_cs);
1261 return hr;
1264 /*****************************************************************************
1265 * IDirectDrawSurface7::GetPriority
1267 * Returns the surface's priority
1269 * Params:
1270 * Priority: Address of a variable to write the priority to
1272 * Returns:
1273 * D3D_OK on success
1274 * DDERR_INVALIDPARAMS if Priority == NULL
1275 * For more details, see IWineD3DSurface::GetPriority
1277 *****************************************************************************/
1278 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
1280 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1281 TRACE("(%p)->(%p): Relay\n",This,Priority);
1283 if(!Priority)
1285 return DDERR_INVALIDPARAMS;
1288 EnterCriticalSection(&ddraw_cs);
1289 *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1290 LeaveCriticalSection(&ddraw_cs);
1291 return DD_OK;
1294 /*****************************************************************************
1295 * IDirectDrawSurface7::SetPrivateData
1297 * Stores some data in the surface that is intended for the application's
1298 * use.
1300 * Params:
1301 * tag: GUID that identifies the data
1302 * Data: Pointer to the private data
1303 * Size: Size of the private data
1304 * Flags: Some flags
1306 * Returns:
1307 * D3D_OK on success
1308 * For more details, see IWineD3DSurface::SetPrivateData
1310 *****************************************************************************/
1311 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
1312 REFGUID tag, void *Data, DWORD Size, DWORD Flags)
1314 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1315 HRESULT hr;
1316 TRACE("(%p)->(%s,%p,%d,%x): Relay\n", This, debugstr_guid(tag), Data, Size, Flags);
1318 EnterCriticalSection(&ddraw_cs);
1319 hr = IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1320 tag,
1321 Data,
1322 Size,
1323 Flags);
1324 LeaveCriticalSection(&ddraw_cs);
1325 switch(hr)
1327 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1328 default: return hr;
1332 /*****************************************************************************
1333 * IDirectDrawSurface7::GetPrivateData
1335 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1337 * Params:
1338 * tag: GUID of the data to return
1339 * Data: Address where to write the data to
1340 * Size: Size of the buffer at Data
1342 * Returns:
1343 * DD_OK on success
1344 * DDERR_INVALIDPARAMS if Data is NULL
1345 * For more details, see IWineD3DSurface::GetPrivateData
1347 *****************************************************************************/
1348 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
1350 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1351 HRESULT hr;
1352 TRACE("(%p)->(%s,%p,%p): Relay\n", This, debugstr_guid(tag), Data, Size);
1354 if(!Data)
1355 return DDERR_INVALIDPARAMS;
1357 EnterCriticalSection(&ddraw_cs);
1358 hr = IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1359 tag,
1360 Data,
1361 Size);
1362 LeaveCriticalSection(&ddraw_cs);
1363 return hr;
1366 /*****************************************************************************
1367 * IDirectDrawSurface7::FreePrivateData
1369 * Frees private data stored in the surface
1371 * Params:
1372 * tag: Tag of the data to free
1374 * Returns:
1375 * D3D_OK on success
1376 * For more details, see IWineD3DSurface::FreePrivateData
1378 *****************************************************************************/
1379 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
1381 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1382 HRESULT hr;
1383 TRACE("(%p)->(%s): Relay\n", This, debugstr_guid(tag));
1385 EnterCriticalSection(&ddraw_cs);
1386 hr = IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1387 LeaveCriticalSection(&ddraw_cs);
1388 return hr;
1391 /*****************************************************************************
1392 * IDirectDrawSurface7::PageLock
1394 * Prevents a sysmem surface from being paged out
1396 * Params:
1397 * Flags: Not used, must be 0(unchecked)
1399 * Returns:
1400 * DD_OK, because it's a stub
1402 *****************************************************************************/
1403 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
1405 TRACE("(%p)->(%x)\n", iface, Flags);
1407 /* This is Windows memory management related - we don't need this */
1408 return DD_OK;
1411 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
1413 TRACE("iface %p, flags %#x.\n", iface, flags);
1415 return ddraw_surface7_PageLock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1418 /*****************************************************************************
1419 * IDirectDrawSurface7::PageUnlock
1421 * Allows a sysmem surface to be paged out
1423 * Params:
1424 * Flags: Not used, must be 0(unchecked)
1426 * Returns:
1427 * DD_OK, because it's a stub
1429 *****************************************************************************/
1430 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
1432 TRACE("(%p)->(%x)\n", iface, Flags);
1434 return DD_OK;
1437 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
1439 TRACE("iface %p, flags %#x.\n", iface, flags);
1441 return ddraw_surface7_PageUnlock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1444 /*****************************************************************************
1445 * IDirectDrawSurface7::BltBatch
1447 * An unimplemented function
1449 * Params:
1452 * Returns:
1453 * DDERR_UNSUPPORTED
1455 *****************************************************************************/
1456 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1458 TRACE("(%p)->(%p,%d,%08x)\n",iface,Batch,Count,Flags);
1460 /* MSDN: "not currently implemented" */
1461 return DDERR_UNSUPPORTED;
1464 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
1466 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
1468 return ddraw_surface7_BltBatch((IDirectDrawSurface7 *)surface_from_surface3(iface), batch, count, flags);
1471 /*****************************************************************************
1472 * IDirectDrawSurface7::EnumAttachedSurfaces
1474 * Enumerates all surfaces attached to this surface
1476 * Params:
1477 * context: Pointer to pass unmodified to the callback
1478 * cb: Callback function to call for each surface
1480 * Returns:
1481 * DD_OK on success
1482 * DDERR_INVALIDPARAMS if cb is NULL
1484 *****************************************************************************/
1485 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1486 void *context, LPDDENUMSURFACESCALLBACK7 cb)
1488 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1489 IDirectDrawSurfaceImpl *surf;
1490 DDSURFACEDESC2 desc;
1491 int i;
1493 /* Attached surfaces aren't handled in WineD3D */
1494 TRACE("(%p)->(%p,%p)\n",This,context,cb);
1496 if(!cb)
1497 return DDERR_INVALIDPARAMS;
1499 EnterCriticalSection(&ddraw_cs);
1500 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1502 surf = This->complex_array[i];
1503 if(!surf) break;
1505 ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1506 desc = surf->surface_desc;
1507 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1508 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1510 LeaveCriticalSection(&ddraw_cs);
1511 return DD_OK;
1515 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1517 ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1518 desc = surf->surface_desc;
1519 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1520 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1522 LeaveCriticalSection(&ddraw_cs);
1523 return DD_OK;
1527 TRACE(" end of enumeration.\n");
1529 LeaveCriticalSection(&ddraw_cs);
1530 return DD_OK;
1533 struct callback_info
1535 LPDDENUMSURFACESCALLBACK callback;
1536 void *context;
1539 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
1541 const struct callback_info *info = context;
1543 return info->callback((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_vtbl,
1544 (DDSURFACEDESC *)surface_desc, info->context);
1547 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
1548 void *context, LPDDENUMSURFACESCALLBACK callback)
1550 struct callback_info info;
1552 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
1554 info.callback = callback;
1555 info.context = context;
1557 return ddraw_surface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)surface_from_surface3(iface),
1558 &info, EnumCallback);
1561 /*****************************************************************************
1562 * IDirectDrawSurface7::EnumOverlayZOrders
1564 * "Enumerates the overlay surfaces on the specified destination"
1566 * Params:
1567 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
1568 * context: context to pass back to the callback
1569 * cb: callback function to call for each enumerated surface
1571 * Returns:
1572 * DD_OK, because it's a stub
1574 *****************************************************************************/
1575 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1576 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
1578 FIXME("(%p)->(%x,%p,%p): Stub!\n", iface, Flags, context, cb);
1580 return DD_OK;
1583 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
1584 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
1586 struct callback_info info;
1588 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
1590 info.callback = callback;
1591 info.context = context;
1593 return ddraw_surface7_EnumOverlayZOrders((IDirectDrawSurface7 *)surface_from_surface3(iface),
1594 flags, &info, EnumCallback);
1597 /*****************************************************************************
1598 * IDirectDrawSurface7::GetBltStatus
1600 * Returns the blitting status
1602 * Params:
1603 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1605 * Returns:
1606 * See IWineD3DSurface::Blt
1608 *****************************************************************************/
1609 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1611 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1612 HRESULT hr;
1613 TRACE("(%p)->(%x): Relay\n", This, Flags);
1615 EnterCriticalSection(&ddraw_cs);
1616 hr = IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1617 LeaveCriticalSection(&ddraw_cs);
1618 switch(hr)
1620 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1621 default: return hr;
1625 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
1627 TRACE("iface %p, flags %#x.\n", iface, flags);
1629 return ddraw_surface7_GetBltStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1632 /*****************************************************************************
1633 * IDirectDrawSurface7::GetColorKey
1635 * Returns the color key assigned to the surface
1637 * Params:
1638 * Flags: Some flags
1639 * CKey: Address to store the key to
1641 * Returns:
1642 * DD_OK on success
1643 * DDERR_INVALIDPARAMS if CKey is NULL
1645 *****************************************************************************/
1646 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
1648 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1649 TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
1651 if(!CKey)
1652 return DDERR_INVALIDPARAMS;
1654 EnterCriticalSection(&ddraw_cs);
1656 switch (Flags)
1658 case DDCKEY_DESTBLT:
1659 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
1661 LeaveCriticalSection(&ddraw_cs);
1662 return DDERR_NOCOLORKEY;
1664 *CKey = This->surface_desc.ddckCKDestBlt;
1665 break;
1667 case DDCKEY_DESTOVERLAY:
1668 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
1670 LeaveCriticalSection(&ddraw_cs);
1671 return DDERR_NOCOLORKEY;
1673 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1674 break;
1676 case DDCKEY_SRCBLT:
1677 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
1679 LeaveCriticalSection(&ddraw_cs);
1680 return DDERR_NOCOLORKEY;
1682 *CKey = This->surface_desc.ddckCKSrcBlt;
1683 break;
1685 case DDCKEY_SRCOVERLAY:
1686 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
1688 LeaveCriticalSection(&ddraw_cs);
1689 return DDERR_NOCOLORKEY;
1691 *CKey = This->surface_desc.ddckCKSrcOverlay;
1692 break;
1694 default:
1695 LeaveCriticalSection(&ddraw_cs);
1696 return DDERR_INVALIDPARAMS;
1699 LeaveCriticalSection(&ddraw_cs);
1700 return DD_OK;
1703 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
1705 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
1707 return ddraw_surface7_GetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
1710 /*****************************************************************************
1711 * IDirectDrawSurface7::GetFlipStatus
1713 * Returns the flipping status of the surface
1715 * Params:
1716 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1718 * Returns:
1719 * See IWineD3DSurface::GetFlipStatus
1721 *****************************************************************************/
1722 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1724 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1725 HRESULT hr;
1726 TRACE("(%p)->(%x): Relay\n", This, Flags);
1728 EnterCriticalSection(&ddraw_cs);
1729 hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1730 LeaveCriticalSection(&ddraw_cs);
1731 switch(hr)
1733 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1734 default: return hr;
1738 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
1740 TRACE("iface %p, flags %#x.\n", iface, flags);
1742 return ddraw_surface7_GetFlipStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1745 /*****************************************************************************
1746 * IDirectDrawSurface7::GetOverlayPosition
1748 * Returns the display coordinates of a visible and active overlay surface
1750 * Params:
1754 * Returns:
1755 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
1756 *****************************************************************************/
1757 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
1759 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1760 HRESULT hr;
1761 TRACE("(%p)->(%p,%p): Relay\n", This, X, Y);
1763 EnterCriticalSection(&ddraw_cs);
1764 hr = IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1767 LeaveCriticalSection(&ddraw_cs);
1768 return hr;
1771 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
1773 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
1775 return ddraw_surface7_GetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
1778 /*****************************************************************************
1779 * IDirectDrawSurface7::GetPixelFormat
1781 * Returns the pixel format of the Surface
1783 * Params:
1784 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1785 * format should be written
1787 * Returns:
1788 * DD_OK on success
1789 * DDERR_INVALIDPARAMS if PixelFormat is NULL
1791 *****************************************************************************/
1792 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
1794 /* What is DDERR_INVALIDSURFACETYPE for here? */
1795 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1796 TRACE("(%p)->(%p)\n",This,PixelFormat);
1798 if(!PixelFormat)
1799 return DDERR_INVALIDPARAMS;
1801 EnterCriticalSection(&ddraw_cs);
1802 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1803 LeaveCriticalSection(&ddraw_cs);
1805 return DD_OK;
1808 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
1810 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
1812 return ddraw_surface7_GetPixelFormat((IDirectDrawSurface7 *)surface_from_surface3(iface), pixel_format);
1815 /*****************************************************************************
1816 * IDirectDrawSurface7::GetSurfaceDesc
1818 * Returns the description of this surface
1820 * Params:
1821 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1822 * surface desc
1824 * Returns:
1825 * DD_OK on success
1826 * DDERR_INVALIDPARAMS if DDSD is NULL
1828 *****************************************************************************/
1829 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
1831 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1833 TRACE("(%p)->(%p)\n",This,DDSD);
1835 if(!DDSD)
1836 return DDERR_INVALIDPARAMS;
1838 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1840 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1841 return DDERR_INVALIDPARAMS;
1844 EnterCriticalSection(&ddraw_cs);
1845 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1846 TRACE("Returning surface desc:\n");
1847 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1849 LeaveCriticalSection(&ddraw_cs);
1850 return DD_OK;
1853 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
1855 IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
1857 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1859 if (!surface_desc) return DDERR_INVALIDPARAMS;
1861 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
1863 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
1864 return DDERR_INVALIDPARAMS;
1867 EnterCriticalSection(&ddraw_cs);
1868 DD_STRUCT_COPY_BYSIZE(surface_desc, (DDSURFACEDESC *)&surface->surface_desc);
1869 TRACE("Returning surface desc:\n");
1870 if (TRACE_ON(ddraw))
1872 /* DDRAW_dump_surface_desc handles the smaller size */
1873 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
1876 LeaveCriticalSection(&ddraw_cs);
1877 return DD_OK;
1880 /*****************************************************************************
1881 * IDirectDrawSurface7::Initialize
1883 * Initializes the surface. This is a no-op in Wine
1885 * Params:
1886 * DD: Pointer to an DirectDraw interface
1887 * DDSD: Surface description for initialization
1889 * Returns:
1890 * DDERR_ALREADYINITIALIZED
1892 *****************************************************************************/
1893 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
1894 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
1896 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1898 return DDERR_ALREADYINITIALIZED;
1901 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
1902 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
1904 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1906 return ddraw_surface7_Initialize((IDirectDrawSurface7 *)surface_from_surface3(iface),
1907 ddraw, (DDSURFACEDESC2 *)surface_desc);
1910 /*****************************************************************************
1911 * IDirectDrawSurface7::IsLost
1913 * Checks if the surface is lost
1915 * Returns:
1916 * DD_OK, if the surface is usable
1917 * DDERR_ISLOST if the surface is lost
1918 * See IWineD3DSurface::IsLost for more details
1920 *****************************************************************************/
1921 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
1923 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1924 HRESULT hr;
1925 TRACE("(%p)\n", This);
1927 EnterCriticalSection(&ddraw_cs);
1928 /* We lose the surface if the implementation was changed */
1929 if(This->ImplType != This->ddraw->ImplType)
1931 /* But this shouldn't happen. When we change the implementation,
1932 * all surfaces are re-created automatically, and their content
1933 * is copied
1935 ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1936 LeaveCriticalSection(&ddraw_cs);
1937 return DDERR_SURFACELOST;
1940 hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
1941 LeaveCriticalSection(&ddraw_cs);
1942 switch(hr)
1944 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1945 * WineD3D uses the same error for surfaces
1947 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
1948 default: return hr;
1952 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
1954 TRACE("iface %p.\n", iface);
1956 return ddraw_surface7_IsLost((IDirectDrawSurface7 *)surface_from_surface3(iface));
1959 /*****************************************************************************
1960 * IDirectDrawSurface7::Restore
1962 * Restores a lost surface. This makes the surface usable again, but
1963 * doesn't reload its old contents
1965 * Returns:
1966 * DD_OK on success
1967 * See IWineD3DSurface::Restore for more details
1969 *****************************************************************************/
1970 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
1972 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1973 HRESULT hr;
1974 TRACE("(%p)\n", This);
1976 EnterCriticalSection(&ddraw_cs);
1977 if(This->ImplType != This->ddraw->ImplType)
1979 /* Call the recreation callback. Make sure to AddRef first */
1980 IDirectDrawSurface_AddRef(iface);
1981 ddraw_recreate_surfaces_cb(iface, &This->surface_desc, NULL /* Not needed */);
1983 hr = IWineD3DSurface_Restore(This->WineD3DSurface);
1984 LeaveCriticalSection(&ddraw_cs);
1985 return hr;
1988 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
1990 TRACE("iface %p.\n", iface);
1992 return ddraw_surface7_Restore((IDirectDrawSurface7 *)surface_from_surface3(iface));
1995 /*****************************************************************************
1996 * IDirectDrawSurface7::SetOverlayPosition
1998 * Changes the display coordinates of an overlay surface
2000 * Params:
2001 * X:
2002 * Y:
2004 * Returns:
2005 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
2006 *****************************************************************************/
2007 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
2009 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2010 HRESULT hr;
2011 TRACE("(%p)->(%d,%d): Relay\n", This, X, Y);
2013 EnterCriticalSection(&ddraw_cs);
2014 hr = IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
2017 LeaveCriticalSection(&ddraw_cs);
2018 return hr;
2021 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
2023 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
2025 return ddraw_surface7_SetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
2028 /*****************************************************************************
2029 * IDirectDrawSurface7::UpdateOverlay
2031 * Modifies the attributes of an overlay surface.
2033 * Params:
2034 * SrcRect: The section of the source being used for the overlay
2035 * DstSurface: Address of the surface that is overlaid
2036 * DstRect: Place of the overlay
2037 * Flags: some DDOVER_* flags
2039 * Returns:
2040 * DDERR_UNSUPPORTED, because we don't support overlays
2042 *****************************************************************************/
2043 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
2044 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
2046 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2047 IDirectDrawSurfaceImpl *Dst = (IDirectDrawSurfaceImpl *)DstSurface;
2048 HRESULT hr;
2049 TRACE("(%p)->(%p,%p,%p,%x,%p): Relay\n", This, SrcRect, Dst, DstRect, Flags, FX);
2051 EnterCriticalSection(&ddraw_cs);
2052 hr = IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
2053 SrcRect,
2054 Dst ? Dst->WineD3DSurface : NULL,
2055 DstRect,
2056 Flags,
2057 (WINEDDOVERLAYFX *) FX);
2058 LeaveCriticalSection(&ddraw_cs);
2059 switch(hr) {
2060 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2061 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
2062 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
2063 default:
2064 return hr;
2068 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
2069 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
2071 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2072 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
2074 return ddraw_surface7_UpdateOverlay((IDirectDrawSurface7 *)surface_from_surface3(iface), src_rect,
2075 dst_surface ? (IDirectDrawSurface7 *)surface_from_surface3(dst_surface) : NULL, dst_rect, flags, fx);
2078 /*****************************************************************************
2079 * IDirectDrawSurface7::UpdateOverlayDisplay
2081 * The DX7 sdk says that it's not implemented
2083 * Params:
2084 * Flags: ?
2086 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
2088 *****************************************************************************/
2089 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
2091 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2092 TRACE("(%p)->(%x)\n", This, Flags);
2093 return DDERR_UNSUPPORTED;
2096 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
2098 TRACE("iface %p, flags %#x.\n", iface, flags);
2100 return ddraw_surface7_UpdateOverlayDisplay((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
2103 /*****************************************************************************
2104 * IDirectDrawSurface7::UpdateOverlayZOrder
2106 * Sets an overlay's Z order
2108 * Params:
2109 * Flags: DDOVERZ_* flags
2110 * DDSRef: Defines the relative position in the overlay chain
2112 * Returns:
2113 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
2115 *****************************************************************************/
2116 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
2117 DWORD Flags, IDirectDrawSurface7 *DDSRef)
2119 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2120 IDirectDrawSurfaceImpl *Ref = (IDirectDrawSurfaceImpl *)DDSRef;
2121 HRESULT hr;
2123 TRACE("(%p)->(%x,%p): Relay\n", This, Flags, Ref);
2124 EnterCriticalSection(&ddraw_cs);
2125 hr = IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
2126 Flags,
2127 Ref ? Ref->WineD3DSurface : NULL);
2128 LeaveCriticalSection(&ddraw_cs);
2129 return hr;
2132 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
2133 DWORD flags, IDirectDrawSurface3 *reference)
2135 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
2137 return ddraw_surface7_UpdateOverlayZOrder((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
2138 reference ? (IDirectDrawSurface7 *)surface_from_surface3(reference) : NULL);
2141 /*****************************************************************************
2142 * IDirectDrawSurface7::GetDDInterface
2144 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
2145 * surface belongs to
2147 * Params:
2148 * DD: Address to write the interface pointer to
2150 * Returns:
2151 * DD_OK on success
2152 * DDERR_INVALIDPARAMS if DD is NULL
2154 *****************************************************************************/
2155 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
2157 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2159 TRACE("(%p)->(%p)\n",This,DD);
2161 if(!DD)
2162 return DDERR_INVALIDPARAMS;
2164 switch(This->version)
2166 case 7:
2167 *DD = This->ddraw;
2168 break;
2170 case 4:
2171 *DD = &This->ddraw->IDirectDraw4_vtbl;
2172 break;
2174 case 2:
2175 *DD = &This->ddraw->IDirectDraw2_vtbl;
2176 break;
2178 case 1:
2179 *DD = &This->ddraw->IDirectDraw_vtbl;
2180 break;
2183 IUnknown_AddRef((IUnknown *)*DD);
2185 return DD_OK;
2188 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
2190 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
2192 return ddraw_surface7_GetDDInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), ddraw);
2195 /* This seems also windows implementation specific - I don't think WineD3D needs this */
2196 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
2198 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2199 volatile IDirectDrawSurfaceImpl* vThis = This;
2201 TRACE("(%p)\n",This);
2202 EnterCriticalSection(&ddraw_cs);
2203 /* A uniqueness value of 0 is apparently special.
2204 * This needs to be checked.
2205 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
2207 while (1) {
2208 DWORD old_uniqueness_value = vThis->uniqueness_value;
2209 DWORD new_uniqueness_value = old_uniqueness_value+1;
2211 if (old_uniqueness_value == 0) break;
2212 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
2214 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
2215 old_uniqueness_value,
2216 new_uniqueness_value)
2217 == old_uniqueness_value)
2218 break;
2221 LeaveCriticalSection(&ddraw_cs);
2222 return DD_OK;
2225 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
2227 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2229 TRACE("(%p)->(%p)\n",This,pValue);
2230 EnterCriticalSection(&ddraw_cs);
2231 *pValue = This->uniqueness_value;
2232 LeaveCriticalSection(&ddraw_cs);
2233 return DD_OK;
2236 /*****************************************************************************
2237 * IDirectDrawSurface7::SetLOD
2239 * Sets the level of detail of a texture
2241 * Params:
2242 * MaxLOD: LOD to set
2244 * Returns:
2245 * DD_OK on success
2246 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2248 *****************************************************************************/
2249 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
2251 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2252 HRESULT hr;
2253 TRACE("(%p)->(%d)\n", This, MaxLOD);
2255 EnterCriticalSection(&ddraw_cs);
2256 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2258 LeaveCriticalSection(&ddraw_cs);
2259 return DDERR_INVALIDOBJECT;
2262 if(!This->wineD3DTexture)
2264 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2265 LeaveCriticalSection(&ddraw_cs);
2266 return DDERR_INVALIDOBJECT;
2269 hr = IWineD3DBaseTexture_SetLOD(This->wineD3DTexture,
2270 MaxLOD);
2271 LeaveCriticalSection(&ddraw_cs);
2272 return hr;
2275 /*****************************************************************************
2276 * IDirectDrawSurface7::GetLOD
2278 * Returns the level of detail of a Direct3D texture
2280 * Params:
2281 * MaxLOD: Address to write the LOD to
2283 * Returns:
2284 * DD_OK on success
2285 * DDERR_INVALIDPARAMS if MaxLOD is NULL
2286 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2288 *****************************************************************************/
2289 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
2291 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2292 TRACE("(%p)->(%p)\n", This, MaxLOD);
2294 if(!MaxLOD)
2295 return DDERR_INVALIDPARAMS;
2297 EnterCriticalSection(&ddraw_cs);
2298 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2300 LeaveCriticalSection(&ddraw_cs);
2301 return DDERR_INVALIDOBJECT;
2304 *MaxLOD = IWineD3DBaseTexture_GetLOD(This->wineD3DTexture);
2305 LeaveCriticalSection(&ddraw_cs);
2306 return DD_OK;
2309 /*****************************************************************************
2310 * IDirectDrawSurface7::BltFast
2312 * Performs a fast Blit.
2314 * Params:
2315 * dstx: The x coordinate to blit to on the destination
2316 * dsty: The y coordinate to blit to on the destination
2317 * Source: The source surface
2318 * rsrc: The source rectangle
2319 * trans: Type of transfer. Some DDBLTFAST_* flags
2321 * Returns:
2322 * DD_OK on success
2323 * For more details, see IWineD3DSurface::BltFast
2325 *****************************************************************************/
2326 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
2327 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
2329 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2330 IDirectDrawSurfaceImpl *src = (IDirectDrawSurfaceImpl *)Source;
2331 DWORD src_w, src_h, dst_w, dst_h;
2332 HRESULT hr;
2333 TRACE("(%p)->(%d,%d,%p,%p,%d): Relay\n", This, dstx, dsty, Source, rsrc, trans);
2335 dst_w = This->surface_desc.dwWidth;
2336 dst_h = This->surface_desc.dwHeight;
2338 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2339 * in that case
2341 if(rsrc)
2343 if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2344 rsrc->right > src->surface_desc.dwWidth ||
2345 rsrc->bottom > src->surface_desc.dwHeight)
2347 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2348 return DDERR_INVALIDRECT;
2351 src_w = rsrc->right - rsrc->left;
2352 src_h = rsrc->bottom - rsrc->top;
2354 else
2356 src_w = src->surface_desc.dwWidth;
2357 src_h = src->surface_desc.dwHeight;
2360 if (src_w > dst_w || dstx > dst_w - src_w
2361 || src_h > dst_h || dsty > dst_h - src_h)
2363 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
2364 return DDERR_INVALIDRECT;
2367 EnterCriticalSection(&ddraw_cs);
2368 hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
2369 dstx, dsty,
2370 src ? src->WineD3DSurface : NULL,
2371 rsrc,
2372 trans);
2373 LeaveCriticalSection(&ddraw_cs);
2374 switch(hr)
2376 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
2377 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
2378 default: return hr;
2382 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
2383 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
2385 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2386 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
2388 return ddraw_surface7_BltFast((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_x, dst_y,
2389 src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags);
2392 /*****************************************************************************
2393 * IDirectDrawSurface7::GetClipper
2395 * Returns the IDirectDrawClipper interface of the clipper assigned to this
2396 * surface
2398 * Params:
2399 * Clipper: Address to store the interface pointer at
2401 * Returns:
2402 * DD_OK on success
2403 * DDERR_INVALIDPARAMS if Clipper is NULL
2404 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
2406 *****************************************************************************/
2407 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
2409 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2410 TRACE("(%p)->(%p)\n", This, Clipper);
2412 if(!Clipper)
2414 LeaveCriticalSection(&ddraw_cs);
2415 return DDERR_INVALIDPARAMS;
2418 EnterCriticalSection(&ddraw_cs);
2419 if(This->clipper == NULL)
2421 LeaveCriticalSection(&ddraw_cs);
2422 return DDERR_NOCLIPPERATTACHED;
2425 *Clipper = (IDirectDrawClipper *)This->clipper;
2426 IDirectDrawClipper_AddRef(*Clipper);
2427 LeaveCriticalSection(&ddraw_cs);
2428 return DD_OK;
2431 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
2433 TRACE("iface %p, clipper %p.\n", iface, clipper);
2435 return ddraw_surface7_GetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2438 /*****************************************************************************
2439 * IDirectDrawSurface7::SetClipper
2441 * Sets a clipper for the surface
2443 * Params:
2444 * Clipper: IDirectDrawClipper interface of the clipper to set
2446 * Returns:
2447 * DD_OK on success
2449 *****************************************************************************/
2450 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper *Clipper)
2452 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2453 IDirectDrawClipperImpl *oldClipper = This->clipper;
2454 HWND clipWindow;
2455 HRESULT hr;
2456 TRACE("(%p)->(%p)\n",This,Clipper);
2458 EnterCriticalSection(&ddraw_cs);
2459 if ((IDirectDrawClipperImpl *)Clipper == This->clipper)
2461 LeaveCriticalSection(&ddraw_cs);
2462 return DD_OK;
2465 This->clipper = (IDirectDrawClipperImpl *)Clipper;
2467 if (Clipper != NULL)
2468 IDirectDrawClipper_AddRef(Clipper);
2469 if(oldClipper)
2470 IDirectDrawClipper_Release((IDirectDrawClipper *)oldClipper);
2472 hr = IWineD3DSurface_SetClipper(This->WineD3DSurface, This->clipper ? This->clipper->wineD3DClipper : NULL);
2474 if(This->wineD3DSwapChain) {
2475 clipWindow = NULL;
2476 if(Clipper) {
2477 IDirectDrawClipper_GetHWnd(Clipper, &clipWindow);
2480 if(clipWindow) {
2481 IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2482 clipWindow);
2483 } else {
2484 IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2485 This->ddraw->d3d_window);
2489 LeaveCriticalSection(&ddraw_cs);
2490 return hr;
2493 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
2495 TRACE("iface %p, clipper %p.\n", iface, clipper);
2497 return ddraw_surface7_SetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2500 /*****************************************************************************
2501 * IDirectDrawSurface7::SetSurfaceDesc
2503 * Sets the surface description. It can override the pixel format, the surface
2504 * memory, ...
2505 * It's not really tested.
2507 * Params:
2508 * DDSD: Pointer to the new surface description to set
2509 * Flags: Some flags
2511 * Returns:
2512 * DD_OK on success
2513 * DDERR_INVALIDPARAMS if DDSD is NULL
2515 *****************************************************************************/
2516 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
2518 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2519 WINED3DFORMAT newFormat = WINED3DFMT_UNKNOWN;
2520 HRESULT hr;
2521 TRACE("(%p)->(%p,%x)\n", This, DDSD, Flags);
2523 if(!DDSD)
2524 return DDERR_INVALIDPARAMS;
2526 EnterCriticalSection(&ddraw_cs);
2527 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2529 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2531 if(newFormat == WINED3DFMT_UNKNOWN)
2533 ERR("Requested to set an unknown pixelformat\n");
2534 LeaveCriticalSection(&ddraw_cs);
2535 return DDERR_INVALIDPARAMS;
2537 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2539 hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2540 newFormat);
2541 if(hr != DD_OK)
2543 LeaveCriticalSection(&ddraw_cs);
2544 return hr;
2548 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2550 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2551 DDCKEY_DESTOVERLAY,
2552 (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2554 if (DDSD->dwFlags & DDSD_CKDESTBLT)
2556 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2557 DDCKEY_DESTBLT,
2558 (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2560 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2562 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2563 DDCKEY_SRCOVERLAY,
2564 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2566 if (DDSD->dwFlags & DDSD_CKSRCBLT)
2568 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2569 DDCKEY_SRCBLT,
2570 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2572 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2574 hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2575 if(hr != WINED3D_OK)
2577 /* No need for a trace here, wined3d does that for us */
2578 switch(hr)
2580 case WINED3DERR_INVALIDCALL:
2581 LeaveCriticalSection(&ddraw_cs);
2582 return DDERR_INVALIDPARAMS;
2583 default:
2584 break; /* Go on */
2589 This->surface_desc = *DDSD;
2591 LeaveCriticalSection(&ddraw_cs);
2592 return DD_OK;
2595 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
2596 DDSURFACEDESC *surface_desc, DWORD flags)
2598 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
2600 return ddraw_surface7_SetSurfaceDesc((IDirectDrawSurface7 *)surface_from_surface3(iface),
2601 (DDSURFACEDESC2 *)surface_desc, flags);
2604 /*****************************************************************************
2605 * IDirectDrawSurface7::GetPalette
2607 * Returns the IDirectDrawPalette interface of the palette currently assigned
2608 * to the surface
2610 * Params:
2611 * Pal: Address to write the interface pointer to
2613 * Returns:
2614 * DD_OK on success
2615 * DDERR_INVALIDPARAMS if Pal is NULL
2617 *****************************************************************************/
2618 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
2620 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2621 IWineD3DPalette *wPal;
2622 HRESULT hr;
2623 TRACE("(%p)->(%p): Relay\n", This, Pal);
2625 if(!Pal)
2626 return DDERR_INVALIDPARAMS;
2628 EnterCriticalSection(&ddraw_cs);
2629 hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
2630 if(hr != DD_OK)
2632 LeaveCriticalSection(&ddraw_cs);
2633 return hr;
2636 if(wPal)
2638 hr = IWineD3DPalette_GetParent(wPal, (IUnknown **) Pal);
2640 else
2642 *Pal = NULL;
2643 hr = DDERR_NOPALETTEATTACHED;
2646 LeaveCriticalSection(&ddraw_cs);
2647 return hr;
2650 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
2652 TRACE("iface %p, palette %p.\n", iface, palette);
2654 return ddraw_surface7_GetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2657 /*****************************************************************************
2658 * SetColorKeyEnum
2660 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2661 * recursively in the surface tree
2663 *****************************************************************************/
2664 struct SCKContext
2666 HRESULT ret;
2667 WINEDDCOLORKEY *CKey;
2668 DWORD Flags;
2671 static HRESULT WINAPI
2672 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2673 DDSURFACEDESC2 *desc,
2674 void *context)
2676 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)surface;
2677 struct SCKContext *ctx = context;
2678 HRESULT hr;
2680 hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2681 ctx->Flags,
2682 ctx->CKey);
2683 if(hr != DD_OK)
2685 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2686 ctx->ret = hr;
2689 ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
2690 ddraw_surface7_Release(surface);
2692 return DDENUMRET_OK;
2695 /*****************************************************************************
2696 * IDirectDrawSurface7::SetColorKey
2698 * Sets the color keying options for the surface. Observations showed that
2699 * in case of complex surfaces the color key has to be assigned to all
2700 * sublevels.
2702 * Params:
2703 * Flags: DDCKEY_*
2704 * CKey: The new color key
2706 * Returns:
2707 * DD_OK on success
2708 * See IWineD3DSurface::SetColorKey for details
2710 *****************************************************************************/
2711 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2713 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2714 DDCOLORKEY FixedCKey;
2715 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
2716 TRACE("(%p)->(%x,%p)\n", This, Flags, CKey);
2718 EnterCriticalSection(&ddraw_cs);
2719 if (CKey)
2721 FixedCKey = *CKey;
2722 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
2723 if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
2724 FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
2726 switch (Flags & ~DDCKEY_COLORSPACE)
2728 case DDCKEY_DESTBLT:
2729 This->surface_desc.ddckCKDestBlt = FixedCKey;
2730 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2731 break;
2733 case DDCKEY_DESTOVERLAY:
2734 This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
2735 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2736 break;
2738 case DDCKEY_SRCOVERLAY:
2739 This->surface_desc.ddckCKSrcOverlay = FixedCKey;
2740 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2741 break;
2743 case DDCKEY_SRCBLT:
2744 This->surface_desc.ddckCKSrcBlt = FixedCKey;
2745 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2746 break;
2748 default:
2749 LeaveCriticalSection(&ddraw_cs);
2750 return DDERR_INVALIDPARAMS;
2753 else
2755 switch (Flags & ~DDCKEY_COLORSPACE)
2757 case DDCKEY_DESTBLT:
2758 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2759 break;
2761 case DDCKEY_DESTOVERLAY:
2762 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2763 break;
2765 case DDCKEY_SRCOVERLAY:
2766 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2767 break;
2769 case DDCKEY_SRCBLT:
2770 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2771 break;
2773 default:
2774 LeaveCriticalSection(&ddraw_cs);
2775 return DDERR_INVALIDPARAMS;
2778 ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface, Flags, ctx.CKey);
2779 ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
2780 LeaveCriticalSection(&ddraw_cs);
2781 switch(ctx.ret)
2783 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2784 default: return ctx.ret;
2788 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2790 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2792 return ddraw_surface7_SetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
2795 /*****************************************************************************
2796 * IDirectDrawSurface7::SetPalette
2798 * Assigns a DirectDrawPalette object to the surface
2800 * Params:
2801 * Pal: Interface to the palette to set
2803 * Returns:
2804 * DD_OK on success
2806 *****************************************************************************/
2807 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
2809 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2810 IDirectDrawPalette *oldPal;
2811 IDirectDrawSurfaceImpl *surf;
2812 IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
2813 HRESULT hr;
2814 TRACE("(%p)->(%p)\n", This, Pal);
2816 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
2817 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
2818 return DDERR_INVALIDPIXELFORMAT;
2821 /* Find the old palette */
2822 EnterCriticalSection(&ddraw_cs);
2823 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2824 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2826 LeaveCriticalSection(&ddraw_cs);
2827 return hr;
2829 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
2831 /* Set the new Palette */
2832 IWineD3DSurface_SetPalette(This->WineD3DSurface,
2833 PalImpl ? PalImpl->wineD3DPalette : NULL);
2834 /* AddRef the Palette */
2835 if(Pal) IDirectDrawPalette_AddRef(Pal);
2837 /* Release the old palette */
2838 if(oldPal) IDirectDrawPalette_Release(oldPal);
2840 /* If this is a front buffer, also update the back buffers
2841 * TODO: How do things work for palettized cube textures?
2843 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2845 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2846 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2848 surf = This;
2849 while(1)
2851 IDirectDrawSurface7 *attach;
2852 HRESULT hr;
2853 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &caps2, &attach);
2854 if(hr != DD_OK)
2856 break;
2859 TRACE("Setting palette on %p\n", attach);
2860 ddraw_surface7_SetPalette(attach, Pal);
2861 surf = (IDirectDrawSurfaceImpl *)attach;
2862 ddraw_surface7_Release(attach);
2866 LeaveCriticalSection(&ddraw_cs);
2867 return DD_OK;
2870 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
2872 TRACE("iface %p, palette %p.\n", iface, palette);
2874 return ddraw_surface7_SetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2877 /*****************************************************************************
2878 * The VTable
2879 *****************************************************************************/
2881 const IDirectDrawSurface7Vtbl IDirectDrawSurface7_Vtbl =
2883 /* IUnknown */
2884 ddraw_surface7_QueryInterface,
2885 ddraw_surface7_AddRef,
2886 ddraw_surface7_Release,
2887 /* IDirectDrawSurface */
2888 ddraw_surface7_AddAttachedSurface,
2889 ddraw_surface7_AddOverlayDirtyRect,
2890 ddraw_surface7_Blt,
2891 ddraw_surface7_BltBatch,
2892 ddraw_surface7_BltFast,
2893 ddraw_surface7_DeleteAttachedSurface,
2894 ddraw_surface7_EnumAttachedSurfaces,
2895 ddraw_surface7_EnumOverlayZOrders,
2896 ddraw_surface7_Flip,
2897 ddraw_surface7_GetAttachedSurface,
2898 ddraw_surface7_GetBltStatus,
2899 ddraw_surface7_GetCaps,
2900 ddraw_surface7_GetClipper,
2901 ddraw_surface7_GetColorKey,
2902 ddraw_surface7_GetDC,
2903 ddraw_surface7_GetFlipStatus,
2904 ddraw_surface7_GetOverlayPosition,
2905 ddraw_surface7_GetPalette,
2906 ddraw_surface7_GetPixelFormat,
2907 ddraw_surface7_GetSurfaceDesc,
2908 ddraw_surface7_Initialize,
2909 ddraw_surface7_IsLost,
2910 ddraw_surface7_Lock,
2911 ddraw_surface7_ReleaseDC,
2912 ddraw_surface7_Restore,
2913 ddraw_surface7_SetClipper,
2914 ddraw_surface7_SetColorKey,
2915 ddraw_surface7_SetOverlayPosition,
2916 ddraw_surface7_SetPalette,
2917 ddraw_surface7_Unlock,
2918 ddraw_surface7_UpdateOverlay,
2919 ddraw_surface7_UpdateOverlayDisplay,
2920 ddraw_surface7_UpdateOverlayZOrder,
2921 /* IDirectDrawSurface2 */
2922 ddraw_surface7_GetDDInterface,
2923 ddraw_surface7_PageLock,
2924 ddraw_surface7_PageUnlock,
2925 /* IDirectDrawSurface3 */
2926 ddraw_surface7_SetSurfaceDesc,
2927 /* IDirectDrawSurface4 */
2928 ddraw_surface7_SetPrivateData,
2929 ddraw_surface7_GetPrivateData,
2930 ddraw_surface7_FreePrivateData,
2931 ddraw_surface7_GetUniquenessValue,
2932 ddraw_surface7_ChangeUniquenessValue,
2933 /* IDirectDrawSurface7 */
2934 ddraw_surface7_SetPriority,
2935 ddraw_surface7_GetPriority,
2936 ddraw_surface7_SetLOD,
2937 ddraw_surface7_GetLOD,
2940 const IDirectDrawSurface3Vtbl IDirectDrawSurface3_Vtbl =
2942 /* IUnknown */
2943 ddraw_surface3_QueryInterface,
2944 ddraw_surface3_AddRef,
2945 ddraw_surface3_Release,
2946 /* IDirectDrawSurface */
2947 ddraw_surface3_AddAttachedSurface,
2948 ddraw_surface3_AddOverlayDirtyRect,
2949 ddraw_surface3_Blt,
2950 ddraw_surface3_BltBatch,
2951 ddraw_surface3_BltFast,
2952 ddraw_surface3_DeleteAttachedSurface,
2953 ddraw_surface3_EnumAttachedSurfaces,
2954 ddraw_surface3_EnumOverlayZOrders,
2955 ddraw_surface3_Flip,
2956 ddraw_surface3_GetAttachedSurface,
2957 ddraw_surface3_GetBltStatus,
2958 ddraw_surface3_GetCaps,
2959 ddraw_surface3_GetClipper,
2960 ddraw_surface3_GetColorKey,
2961 ddraw_surface3_GetDC,
2962 ddraw_surface3_GetFlipStatus,
2963 ddraw_surface3_GetOverlayPosition,
2964 ddraw_surface3_GetPalette,
2965 ddraw_surface3_GetPixelFormat,
2966 ddraw_surface3_GetSurfaceDesc,
2967 ddraw_surface3_Initialize,
2968 ddraw_surface3_IsLost,
2969 ddraw_surface3_Lock,
2970 ddraw_surface3_ReleaseDC,
2971 ddraw_surface3_Restore,
2972 ddraw_surface3_SetClipper,
2973 ddraw_surface3_SetColorKey,
2974 ddraw_surface3_SetOverlayPosition,
2975 ddraw_surface3_SetPalette,
2976 ddraw_surface3_Unlock,
2977 ddraw_surface3_UpdateOverlay,
2978 ddraw_surface3_UpdateOverlayDisplay,
2979 ddraw_surface3_UpdateOverlayZOrder,
2980 /* IDirectDrawSurface2 */
2981 ddraw_surface3_GetDDInterface,
2982 ddraw_surface3_PageLock,
2983 ddraw_surface3_PageUnlock,
2984 /* IDirectDrawSurface3 */
2985 ddraw_surface3_SetSurfaceDesc,