1 /* DirectDraw Surface Implementation
3 * Copyright (c) 1997-2000 Marcus Meissner
4 * Copyright (c) 1998-2000 Lionel Ulmer
5 * Copyright (c) 2000-2001 TransGaming Technologies Inc.
6 * Copyright (c) 2006 Stefan Dösinger
8 * This file contains the (internal) driver registration functions,
9 * driver enumeration APIs and DirectDraw creation functions.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/port.h"
29 #include "ddraw_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
33 static inline IDirectDrawSurfaceImpl *surface_from_gamma_control(IDirectDrawGammaControl *iface)
35 return (IDirectDrawSurfaceImpl *)((char*)iface
36 - FIELD_OFFSET(IDirectDrawSurfaceImpl, IDirectDrawGammaControl_vtbl));
39 /*****************************************************************************
40 * IUnknown parts follow
41 *****************************************************************************/
43 /*****************************************************************************
44 * IDirectDrawSurface7::QueryInterface
46 * A normal QueryInterface implementation. For QueryInterface rules
47 * see ddraw.c, IDirectDraw7::QueryInterface. This method
48 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
49 * in all versions, the IDirectDrawGammaControl interface and it can
50 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
53 * riid: The interface id queried for
54 * obj: Address to write the pointer to
58 * E_NOINTERFACE if the requested interface wasn't found
60 *****************************************************************************/
61 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
63 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
65 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
67 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
71 return DDERR_INVALIDPARAMS;
73 if (IsEqualGUID(riid, &IID_IUnknown)
74 || IsEqualGUID(riid, &IID_IDirectDrawSurface7)
75 || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
77 IUnknown_AddRef(iface);
79 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
82 else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
83 || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
84 || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
86 IUnknown_AddRef(iface);
87 *obj = &This->IDirectDrawSurface3_iface;
88 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
91 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
93 IUnknown_AddRef(iface);
94 *obj = &This->IDirectDrawGammaControl_vtbl;
95 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
98 else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
99 IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
100 IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
102 IDirect3DDevice7 *d3d;
104 /* Call into IDirect3D7 for creation */
105 IDirect3D7_CreateDevice(&This->ddraw->IDirect3D7_iface, riid, (IDirectDrawSurface7 *)This,
110 *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
111 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
115 WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
116 return E_NOINTERFACE;
118 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
119 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
121 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
123 *obj = &This->IDirect3DTexture_vtbl;
124 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
128 *obj = &This->IDirect3DTexture2_vtbl;
129 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
131 IUnknown_AddRef( (IUnknown *) *obj);
135 ERR("No interface\n");
136 return E_NOINTERFACE;
139 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
141 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
142 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
144 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)This, riid, object);
147 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface, REFIID riid, void **object)
149 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
151 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_gamma_control(iface), riid, object);
154 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
156 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
158 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_texture2(iface), riid, object);
161 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
163 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
165 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_texture1(iface), riid, object);
168 /*****************************************************************************
169 * IDirectDrawSurface7::AddRef
171 * A normal addref implementation
176 *****************************************************************************/
177 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
179 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
180 ULONG refCount = InterlockedIncrement(&This->ref);
182 TRACE("%p increasing refcount to %u.\n", This, refCount);
186 EnterCriticalSection(&ddraw_cs);
187 if (This->wined3d_surface)
188 wined3d_surface_incref(This->wined3d_surface);
189 if (This->wined3d_texture)
190 wined3d_texture_incref(This->wined3d_texture);
191 LeaveCriticalSection(&ddraw_cs);
197 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
199 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
200 TRACE("iface %p.\n", iface);
202 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)This);
205 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
207 TRACE("iface %p.\n", iface);
209 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_gamma_control(iface));
212 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
214 TRACE("iface %p.\n", iface);
216 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_texture2(iface));
219 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
221 TRACE("iface %p.\n", iface);
223 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_texture1(iface));
226 /*****************************************************************************
227 * ddraw_surface_destroy
229 * A helper function for IDirectDrawSurface7::Release
231 * Frees the surface, regardless of its refcount.
232 * See IDirectDrawSurface7::Release for more information
235 * This: Surface to free
237 *****************************************************************************/
238 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
240 TRACE("surface %p.\n", This);
242 /* Check the refcount and give a warning */
245 /* This can happen when a complex surface is destroyed,
246 * because the 2nd surface was addref()ed when the app
247 * called GetAttachedSurface
249 WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
252 if (This->wined3d_surface)
253 wined3d_surface_decref(This->wined3d_surface);
256 static void ddraw_surface_cleanup(IDirectDrawSurfaceImpl *surface)
258 IDirectDrawSurfaceImpl *surf;
259 IUnknown *ifaceToRelease;
262 TRACE("surface %p.\n", surface);
264 if (surface->wined3d_swapchain)
266 IDirectDrawImpl *ddraw = surface->ddraw;
268 /* If it's the render target, destroy the D3D device. */
269 if (ddraw->d3d_initialized && surface == ddraw->d3d_target)
271 TRACE("Destroying the render target, uninitializing D3D.\n");
273 for (i = 0; i < ddraw->numConvertedDecls; ++i)
275 wined3d_vertex_declaration_decref(ddraw->decls[i].decl);
277 HeapFree(GetProcessHeap(), 0, ddraw->decls);
278 ddraw->numConvertedDecls = 0;
280 if (FAILED(wined3d_device_uninit_3d(ddraw->wined3d_device)))
282 ERR("Failed to uninit 3D.\n");
286 /* Free the d3d window if one was created. */
287 if (ddraw->d3d_window && ddraw->d3d_window != ddraw->dest_window)
289 TRACE("Destroying the hidden render window %p.\n", ddraw->d3d_window);
290 DestroyWindow(ddraw->d3d_window);
291 ddraw->d3d_window = 0;
295 ddraw->d3d_initialized = FALSE;
296 ddraw->d3d_target = NULL;
300 wined3d_device_uninit_gdi(ddraw->wined3d_device);
303 surface->wined3d_swapchain = NULL;
305 /* Reset to the default surface implementation type. This is needed
306 * if applications use non render target surfaces and expect blits to
307 * work after destroying the render target.
309 * TODO: Recreate existing offscreen surfaces. */
310 ddraw->ImplType = DefaultSurfaceType;
312 TRACE("D3D unloaded.\n");
315 /* The refcount test shows that the palette is detached when the surface
317 IDirectDrawSurface7_SetPalette((IDirectDrawSurface7 *)surface, NULL);
319 /* Loop through all complex attached surfaces and destroy them.
321 * Yet again, only the root can have more than one complexly attached
322 * surface, all the others have a total of one. */
323 for (i = 0; i < MAX_COMPLEX_ATTACHED; ++i)
325 if (!surface->complex_array[i])
328 surf = surface->complex_array[i];
329 surface->complex_array[i] = NULL;
332 IDirectDrawSurfaceImpl *destroy = surf;
333 surf = surf->complex_array[0]; /* Iterate through the "tree" */
334 ddraw_surface_destroy(destroy); /* Destroy it */
338 ifaceToRelease = surface->ifaceToRelease;
340 /* Destroy the root surface. */
341 ddraw_surface_destroy(surface);
343 /* Reduce the ddraw refcount */
345 IUnknown_Release(ifaceToRelease);
348 /*****************************************************************************
349 * IDirectDrawSurface7::Release
351 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
352 * surface is destroyed.
354 * Destroying the surface is a bit tricky. For the connection between
355 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
356 * It has a nice graph explaining the connection.
358 * What happens here is basically this:
359 * When a surface is destroyed, its WineD3DSurface is released,
360 * and the refcount of the DirectDraw interface is reduced by 1. If it has
361 * complex surfaces attached to it, then these surfaces are destroyed too,
362 * regardless of their refcount. If any surface being destroyed has another
363 * surface attached to it (with a "soft" attachment, not complex), then
364 * this surface is detached with DeleteAttachedSurface.
366 * When the surface is a texture, the WineD3DTexture is released.
367 * If the surface is the Direct3D render target, then the D3D
368 * capabilities of the WineD3DDevice are uninitialized, which causes the
369 * swapchain to be released.
371 * When a complex sublevel falls to ref zero, then this is ignored.
376 *****************************************************************************/
377 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
379 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
380 ULONG ref = InterlockedDecrement(&This->ref);
382 TRACE("%p decreasing refcount to %u.\n", This, ref);
386 /* Complex attached surfaces are destroyed implicitly when the root is released */
387 EnterCriticalSection(&ddraw_cs);
388 if(!This->is_complex_root)
390 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
391 LeaveCriticalSection(&ddraw_cs);
394 if (This->wined3d_texture) /* If it's a texture, destroy the wined3d texture. */
395 wined3d_texture_decref(This->wined3d_texture);
397 ddraw_surface_cleanup(This);
398 LeaveCriticalSection(&ddraw_cs);
404 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
406 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
407 TRACE("iface %p.\n", iface);
409 return ddraw_surface7_Release((IDirectDrawSurface7 *)This);
412 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
414 TRACE("iface %p.\n", iface);
416 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_gamma_control(iface));
419 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
421 TRACE("iface %p.\n", iface);
423 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_texture2(iface));
426 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
428 TRACE("iface %p.\n", iface);
430 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_texture1(iface));
433 /*****************************************************************************
434 * IDirectDrawSurface7::GetAttachedSurface
436 * Returns an attached surface with the requested caps. Surface attachment
437 * and complex surfaces are not clearly described by the MSDN or sdk,
438 * so this method is tricky and likely to contain problems.
439 * This implementation searches the complex list first, then the
442 * The chains are searched from This down to the last surface in the chain,
443 * not from the first element in the chain. The first surface found is
444 * returned. The MSDN says that this method fails if more than one surface
445 * matches the caps, but it is not sure if that is right. The attachment
446 * structure may not even allow two matching surfaces.
448 * The found surface is AddRef-ed before it is returned.
451 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
452 * Surface: Address to store the found surface
456 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
457 * DDERR_NOTFOUND if no surface was found
459 *****************************************************************************/
460 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
461 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
463 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
464 IDirectDrawSurfaceImpl *surf;
468 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
470 EnterCriticalSection(&ddraw_cs);
472 if(This->version < 7)
474 /* Earlier dx apps put garbage into these members, clear them */
475 our_caps.dwCaps = Caps->dwCaps;
476 our_caps.dwCaps2 = 0;
477 our_caps.dwCaps3 = 0;
478 our_caps.dwCaps4 = 0;
485 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 */
487 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
489 surf = This->complex_array[i];
494 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
495 surf->surface_desc.ddsCaps.dwCaps,
496 surf->surface_desc.ddsCaps.dwCaps2,
497 surf->surface_desc.ddsCaps.dwCaps3,
498 surf->surface_desc.ddsCaps.dwCaps4);
501 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
502 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
504 /* MSDN: "This method fails if more than one surface is attached
505 * that matches the capabilities requested."
507 * Not sure how to test this.
510 TRACE("(%p): Returning surface %p\n", This, surf);
511 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
512 *Surface = (IDirectDrawSurface7 *)surf;
513 ddraw_surface7_AddRef(*Surface);
514 LeaveCriticalSection(&ddraw_cs);
519 /* Next, look at the attachment chain */
522 while( (surf = surf->next_attached) )
526 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
527 surf->surface_desc.ddsCaps.dwCaps,
528 surf->surface_desc.ddsCaps.dwCaps2,
529 surf->surface_desc.ddsCaps.dwCaps3,
530 surf->surface_desc.ddsCaps.dwCaps4);
533 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
534 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
536 TRACE("(%p): Returning surface %p\n", This, surf);
537 *Surface = (IDirectDrawSurface7 *)surf;
538 ddraw_surface7_AddRef(*Surface);
539 LeaveCriticalSection(&ddraw_cs);
544 TRACE("(%p) Didn't find a valid surface\n", This);
545 LeaveCriticalSection(&ddraw_cs);
548 return DDERR_NOTFOUND;
551 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
552 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
554 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
555 IDirectDrawSurface7 *attachment7;
559 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
561 caps2.dwCaps = caps->dwCaps;
566 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)This,
567 &caps2, &attachment7);
568 if (FAILED(hr)) *attachment = NULL;
569 else *attachment = attachment7 ?
570 &((IDirectDrawSurfaceImpl *)attachment7)->IDirectDrawSurface3_iface : NULL;
575 /*****************************************************************************
576 * IDirectDrawSurface7::Lock
578 * Locks the surface and returns a pointer to the surface's memory
581 * Rect: Rectangle to lock. If NULL, the whole surface is locked
582 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
583 * Flags: Locking flags, e.g Read only or write only
584 * h: An event handle that's not used and must be NULL
588 * DDERR_INVALIDPARAMS if DDSD is NULL
589 * For more details, see IWineD3DSurface::LockRect
591 *****************************************************************************/
592 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
593 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
595 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
596 WINED3DLOCKED_RECT LockedRect;
599 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
600 iface, wine_dbgstr_rect(Rect), DDSD, Flags, h);
603 return DDERR_INVALIDPARAMS;
605 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
606 EnterCriticalSection(&ddraw_cs);
608 /* Should I check for the handle to be NULL?
610 * The DDLOCK flags and the D3DLOCK flags are equal
611 * for the supported values. The others are ignored by WineD3D
614 if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
615 DDSD->dwSize != sizeof(DDSURFACEDESC2))
617 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
618 LeaveCriticalSection(&ddraw_cs);
619 return DDERR_INVALIDPARAMS;
622 /* Windows zeroes this if the rect is invalid */
629 || (Rect->left > Rect->right)
630 || (Rect->top > Rect->bottom)
631 || (Rect->right > This->surface_desc.dwWidth)
632 || (Rect->bottom > This->surface_desc.dwHeight))
634 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
635 LeaveCriticalSection(&ddraw_cs);
636 return DDERR_INVALIDPARAMS;
640 hr = wined3d_surface_map(This->wined3d_surface, &LockedRect, Rect, Flags);
643 LeaveCriticalSection(&ddraw_cs);
646 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
647 * specific error. But since IWineD3DSurface::LockRect returns that error in this
648 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
649 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
650 * is much easier to do it in one place in ddraw
652 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
657 /* Override the memory area. The pitch should be set already. Strangely windows
658 * does not set the LPSURFACE flag on locked surfaces !?!.
659 * DDSD->dwFlags |= DDSD_LPSURFACE;
661 This->surface_desc.lpSurface = LockedRect.pBits;
662 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
664 TRACE("locked surface returning description :\n");
665 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
667 LeaveCriticalSection(&ddraw_cs);
671 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
672 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
674 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
675 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
676 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
678 return ddraw_surface7_Lock((IDirectDrawSurface7 *)This,
679 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
682 /*****************************************************************************
683 * IDirectDrawSurface7::Unlock
685 * Unlocks an locked surface
688 * Rect: Not used by this implementation
692 * For more details, see IWineD3DSurface::UnlockRect
694 *****************************************************************************/
695 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
697 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
700 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
702 EnterCriticalSection(&ddraw_cs);
703 hr = wined3d_surface_unmap(This->wined3d_surface);
706 This->surface_desc.lpSurface = NULL;
708 LeaveCriticalSection(&ddraw_cs);
712 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
714 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
715 TRACE("iface %p, data %p.\n", iface, data);
717 /* data might not be the LPRECT of later versions, so drop it. */
718 return ddraw_surface7_Unlock((IDirectDrawSurface7 *)This, NULL);
721 /*****************************************************************************
722 * IDirectDrawSurface7::Flip
724 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
725 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
726 * the flip target is passed to WineD3D, even if the app didn't specify one
729 * DestOverride: Specifies the surface that will become the new front
730 * buffer. If NULL, the current back buffer is used
731 * Flags: some DirectDraw flags, see include/ddraw.h
735 * DDERR_NOTFLIPPABLE if no flip target could be found
736 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
737 * For more details, see IWineD3DSurface::Flip
739 *****************************************************************************/
740 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
742 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
743 IDirectDrawSurfaceImpl *Override = (IDirectDrawSurfaceImpl *)DestOverride;
744 IDirectDrawSurface7 *Override7;
747 TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
749 /* Flip has to be called from a front buffer
750 * What about overlay surfaces, AFAIK they can flip too?
752 if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
753 return DDERR_INVALIDOBJECT; /* Unchecked */
755 EnterCriticalSection(&ddraw_cs);
757 /* WineD3D doesn't keep track of attached surface, so find the target */
762 memset(&Caps, 0, sizeof(Caps));
763 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
764 hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
767 ERR("Can't find a flip target\n");
768 LeaveCriticalSection(&ddraw_cs);
769 return DDERR_NOTFLIPPABLE; /* Unchecked */
771 Override = (IDirectDrawSurfaceImpl *)Override7;
773 /* For the GetAttachedSurface */
774 ddraw_surface7_Release(Override7);
777 hr = wined3d_surface_flip(This->wined3d_surface, Override->wined3d_surface, Flags);
778 LeaveCriticalSection(&ddraw_cs);
782 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
784 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
785 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst);
786 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
788 return ddraw_surface7_Flip((IDirectDrawSurface7 *)This,
789 dst_impl ? (IDirectDrawSurface7 *)dst_impl : NULL, flags);
792 /*****************************************************************************
793 * IDirectDrawSurface7::Blt
795 * Performs a blit on the surface
798 * DestRect: Destination rectangle, can be NULL
799 * SrcSurface: Source surface, can be NULL
800 * SrcRect: Source rectangle, can be NULL
802 * DDBltFx: Some extended blt parameters, connected to the flags
806 * See IWineD3DSurface::Blt for more details
808 *****************************************************************************/
809 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
810 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
812 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
813 IDirectDrawSurfaceImpl *Src = (IDirectDrawSurfaceImpl *)SrcSurface;
816 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
817 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
819 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
820 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
822 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
823 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
824 return DDERR_INVALIDPARAMS;
827 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
828 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
829 return DDERR_INVALIDPARAMS;
832 /* Sizes can change, therefore hold the lock when testing the rectangles */
833 EnterCriticalSection(&ddraw_cs);
836 if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
837 DestRect->right > This->surface_desc.dwWidth ||
838 DestRect->bottom > This->surface_desc.dwHeight)
840 WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
841 LeaveCriticalSection(&ddraw_cs);
842 return DDERR_INVALIDRECT;
847 if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
848 SrcRect->right > Src->surface_desc.dwWidth ||
849 SrcRect->bottom > Src->surface_desc.dwHeight)
851 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
852 LeaveCriticalSection(&ddraw_cs);
853 return DDERR_INVALIDRECT;
857 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
858 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
859 LeaveCriticalSection(&ddraw_cs);
860 return DDERR_INVALIDPARAMS;
863 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it
864 * does, copy the struct, and replace the ddraw surfaces with the wined3d
865 * surfaces. So far no blitting operations using surfaces in the bltfx
866 * struct are supported anyway. */
867 hr = wined3d_surface_blt(This->wined3d_surface, DestRect, Src ? Src->wined3d_surface : NULL,
868 SrcRect, Flags, (WINEDDBLTFX *)DDBltFx, WINED3DTEXF_LINEAR);
870 LeaveCriticalSection(&ddraw_cs);
873 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
874 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
879 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
880 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
882 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
883 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
884 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p.\n",
885 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
887 return ddraw_surface7_Blt((IDirectDrawSurface7 *)This, dst_rect,
888 src_impl ? (IDirectDrawSurface7 *)src_impl : NULL, src_rect, flags, fx);
891 /*****************************************************************************
892 * IDirectDrawSurface7::AddAttachedSurface
894 * Attaches a surface to another surface. How the surface attachments work
895 * is not totally understood yet, and this method is prone to problems.
896 * he surface that is attached is AddRef-ed.
898 * Tests with complex surfaces suggest that the surface attachments form a
899 * tree, but no method to test this has been found yet.
901 * The attachment list consists of a first surface (first_attached) and
902 * for each surface a pointer to the next attached surface (next_attached).
903 * For the first surface, and a surface that has no attachments
904 * first_attached points to the surface itself. A surface that has
905 * no successors in the chain has next_attached set to NULL.
907 * Newly attached surfaces are attached right after the root surface.
908 * If a surface is attached to a complex surface compound, it's attached to
909 * the surface that the app requested, not the complex root. See
910 * GetAttachedSurface for a description how surfaces are found.
912 * This is how the current implementation works, and it was coded by looking
913 * at the needs of the applications.
915 * So far only Z-Buffer attachments are tested, and they are activated in
916 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
917 * Back buffers should work in 2D mode, but they are not tested(They can be
918 * attached in older iface versions). Rendering to the front buffer and
919 * switching between that and double buffering is not yet implemented in
920 * WineD3D, so for 3D it might have unexpected results.
922 * ddraw_surface_attach_surface is the real thing,
923 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
924 * performs additional checks. Version 7 of this interface is much more restrictive
925 * than its predecessors.
928 * Attach: Surface to attach to iface
932 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
934 *****************************************************************************/
935 static HRESULT ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
937 TRACE("surface %p, attachment %p.\n", This, Surf);
940 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
942 EnterCriticalSection(&ddraw_cs);
944 /* Check if the surface is already attached somewhere */
945 if (Surf->next_attached || Surf->first_attached != Surf)
947 /* TODO: Test for the structure of the manual attachment. Is it a
948 * chain or a list? What happens if one surface is attached to 2
949 * different surfaces? */
950 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
951 Surf, Surf->next_attached, Surf->first_attached);
953 LeaveCriticalSection(&ddraw_cs);
954 return DDERR_SURFACEALREADYATTACHED;
957 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
958 Surf->next_attached = This->next_attached;
959 Surf->first_attached = This->first_attached;
960 This->next_attached = Surf;
962 /* Check if the WineD3D depth stencil needs updating */
963 if(This->ddraw->d3ddevice)
965 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
968 ddraw_surface7_AddRef((IDirectDrawSurface7 *)Surf);
969 LeaveCriticalSection(&ddraw_cs);
973 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
975 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
976 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
978 TRACE("iface %p, attachment %p.\n", iface, Attach);
980 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
981 if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
984 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
985 Surf->surface_desc.ddsCaps.dwCaps);
986 return DDERR_CANNOTATTACHSURFACE;
989 return ddraw_surface_attach_surface(This, Surf);
992 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
994 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
995 IDirectDrawSurfaceImpl *attach_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
997 TRACE("iface %p, attachment %p.\n", iface, attachment);
999 /* Tests suggest that
1000 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1001 * -> offscreen plain surfaces can be attached to primaries
1002 * -> primaries can be attached to offscreen plain surfaces
1003 * -> z buffers can be attached to primaries */
1004 if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1005 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1007 /* Sizes have to match */
1008 if (attach_impl->surface_desc.dwWidth != This->surface_desc.dwWidth
1009 || attach_impl->surface_desc.dwHeight != This->surface_desc.dwHeight)
1011 WARN("Surface sizes do not match.\n");
1012 return DDERR_CANNOTATTACHSURFACE;
1016 else if (This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1017 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1023 WARN("Invalid attachment combination.\n");
1024 return DDERR_CANNOTATTACHSURFACE;
1027 return ddraw_surface_attach_surface(This, attach_impl);
1030 /*****************************************************************************
1031 * IDirectDrawSurface7::DeleteAttachedSurface
1033 * Removes a surface from the attachment chain. The surface's refcount
1034 * is decreased by one after it has been removed
1037 * Flags: Some flags, not used by this implementation
1038 * Attach: Surface to detach
1042 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1044 *****************************************************************************/
1045 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1046 DWORD Flags, IDirectDrawSurface7 *Attach)
1048 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1049 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
1050 IDirectDrawSurfaceImpl *Prev = This;
1052 TRACE("iface %p, flags %#x, attachment %p.\n", iface, Flags, Attach);
1054 EnterCriticalSection(&ddraw_cs);
1055 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1057 LeaveCriticalSection(&ddraw_cs);
1058 return DDERR_CANNOTDETACHSURFACE;
1061 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1062 if (This->surface_desc.ddsCaps.dwCaps &
1063 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1065 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1066 /* FIXME: we should probably also subtract from dwMipMapCount of this
1067 * and all parent surfaces */
1070 /* Find the predecessor of the detached surface */
1073 if(Prev->next_attached == Surf) break;
1074 Prev = Prev->next_attached;
1077 /* There must be a surface, otherwise there's a bug */
1078 assert(Prev != NULL);
1080 /* Unchain the surface */
1081 Prev->next_attached = Surf->next_attached;
1082 Surf->next_attached = NULL;
1083 Surf->first_attached = Surf;
1085 /* Check if the WineD3D depth stencil needs updating */
1086 if(This->ddraw->d3ddevice)
1088 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1091 ddraw_surface7_Release(Attach);
1092 LeaveCriticalSection(&ddraw_cs);
1096 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1097 DWORD flags, IDirectDrawSurface3 *attachment)
1099 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1100 IDirectDrawSurfaceImpl *attachment_impl = unsafe_impl_from_IDirectDrawSurface3(attachment);
1101 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1103 return ddraw_surface7_DeleteAttachedSurface((IDirectDrawSurface7 *)This, flags,
1104 attachment_impl ? (IDirectDrawSurface7 *)attachment_impl : NULL);
1107 /*****************************************************************************
1108 * IDirectDrawSurface7::AddOverlayDirtyRect
1110 * "This method is not currently implemented"
1118 *****************************************************************************/
1119 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1121 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1123 return DDERR_UNSUPPORTED; /* unchecked */
1126 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1128 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1129 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1131 return ddraw_surface7_AddOverlayDirtyRect((IDirectDrawSurface7 *)This, rect);
1134 /*****************************************************************************
1135 * IDirectDrawSurface7::GetDC
1137 * Returns a GDI device context for the surface
1140 * hdc: Address of a HDC variable to store the dc to
1144 * DDERR_INVALIDPARAMS if hdc is NULL
1145 * For details, see IWineD3DSurface::GetDC
1147 *****************************************************************************/
1148 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1150 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1153 TRACE("iface %p, dc %p.\n", iface, hdc);
1156 return DDERR_INVALIDPARAMS;
1158 EnterCriticalSection(&ddraw_cs);
1159 hr = wined3d_surface_getdc(This->wined3d_surface, hdc);
1160 LeaveCriticalSection(&ddraw_cs);
1163 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1166 case WINED3DERR_INVALIDCALL:
1167 if(hdc) *hdc = NULL;
1168 return DDERR_INVALIDPARAMS;
1174 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1176 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1177 TRACE("iface %p, dc %p.\n", iface, dc);
1179 return ddraw_surface7_GetDC((IDirectDrawSurface7 *)This, dc);
1182 /*****************************************************************************
1183 * IDirectDrawSurface7::ReleaseDC
1185 * Releases the DC that was constructed with GetDC
1188 * hdc: HDC to release
1192 * For more details, see IWineD3DSurface::ReleaseDC
1194 *****************************************************************************/
1195 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1197 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1200 TRACE("iface %p, dc %p.\n", iface, hdc);
1202 EnterCriticalSection(&ddraw_cs);
1203 hr = wined3d_surface_releasedc(This->wined3d_surface, hdc);
1204 LeaveCriticalSection(&ddraw_cs);
1208 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1210 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1211 TRACE("iface %p, dc %p.\n", iface, dc);
1213 return ddraw_surface7_ReleaseDC((IDirectDrawSurface7 *)This, dc);
1216 /*****************************************************************************
1217 * IDirectDrawSurface7::GetCaps
1219 * Returns the surface's caps
1222 * Caps: Address to write the caps to
1226 * DDERR_INVALIDPARAMS if Caps is NULL
1228 *****************************************************************************/
1229 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1231 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1233 TRACE("iface %p, caps %p.\n", iface, Caps);
1236 return DDERR_INVALIDPARAMS;
1238 *Caps = This->surface_desc.ddsCaps;
1242 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1244 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1248 TRACE("iface %p, caps %p.\n", iface, caps);
1250 hr = ddraw_surface7_GetCaps((IDirectDrawSurface7 *)This, &caps2);
1251 if (FAILED(hr)) return hr;
1253 caps->dwCaps = caps2.dwCaps;
1257 /*****************************************************************************
1258 * IDirectDrawSurface7::SetPriority
1260 * Sets a texture priority for managed textures.
1263 * Priority: The new priority
1267 * For more details, see IWineD3DSurface::SetPriority
1269 *****************************************************************************/
1270 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1272 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1275 TRACE("iface %p, priority %u.\n", iface, Priority);
1277 EnterCriticalSection(&ddraw_cs);
1278 hr = wined3d_surface_set_priority(This->wined3d_surface, Priority);
1279 LeaveCriticalSection(&ddraw_cs);
1283 /*****************************************************************************
1284 * IDirectDrawSurface7::GetPriority
1286 * Returns the surface's priority
1289 * Priority: Address of a variable to write the priority to
1293 * DDERR_INVALIDPARAMS if Priority == NULL
1294 * For more details, see IWineD3DSurface::GetPriority
1296 *****************************************************************************/
1297 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
1299 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1301 TRACE("iface %p, priority %p.\n", iface, Priority);
1305 return DDERR_INVALIDPARAMS;
1308 EnterCriticalSection(&ddraw_cs);
1309 *Priority = wined3d_surface_get_priority(This->wined3d_surface);
1310 LeaveCriticalSection(&ddraw_cs);
1314 /*****************************************************************************
1315 * IDirectDrawSurface7::SetPrivateData
1317 * Stores some data in the surface that is intended for the application's
1321 * tag: GUID that identifies the data
1322 * Data: Pointer to the private data
1323 * Size: Size of the private data
1328 * For more details, see IWineD3DSurface::SetPrivateData
1330 *****************************************************************************/
1331 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
1332 REFGUID tag, void *Data, DWORD Size, DWORD Flags)
1334 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1337 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
1338 iface, debugstr_guid(tag), Data, Size, Flags);
1340 EnterCriticalSection(&ddraw_cs);
1341 hr = wined3d_surface_set_private_data(This->wined3d_surface, tag, Data, Size, Flags);
1342 LeaveCriticalSection(&ddraw_cs);
1345 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1350 /*****************************************************************************
1351 * IDirectDrawSurface7::GetPrivateData
1353 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1356 * tag: GUID of the data to return
1357 * Data: Address where to write the data to
1358 * Size: Size of the buffer at Data
1362 * DDERR_INVALIDPARAMS if Data is NULL
1363 * For more details, see IWineD3DSurface::GetPrivateData
1365 *****************************************************************************/
1366 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
1368 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1371 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
1372 iface, debugstr_guid(tag), Data, Size);
1375 return DDERR_INVALIDPARAMS;
1377 EnterCriticalSection(&ddraw_cs);
1378 hr = wined3d_surface_get_private_data(This->wined3d_surface, tag, Data, Size);
1379 LeaveCriticalSection(&ddraw_cs);
1383 /*****************************************************************************
1384 * IDirectDrawSurface7::FreePrivateData
1386 * Frees private data stored in the surface
1389 * tag: Tag of the data to free
1393 * For more details, see IWineD3DSurface::FreePrivateData
1395 *****************************************************************************/
1396 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
1398 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1401 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
1403 EnterCriticalSection(&ddraw_cs);
1404 hr = wined3d_surface_free_private_data(This->wined3d_surface, tag);
1405 LeaveCriticalSection(&ddraw_cs);
1409 /*****************************************************************************
1410 * IDirectDrawSurface7::PageLock
1412 * Prevents a sysmem surface from being paged out
1415 * Flags: Not used, must be 0(unchecked)
1418 * DD_OK, because it's a stub
1420 *****************************************************************************/
1421 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
1423 TRACE("iface %p, flags %#x.\n", iface, Flags);
1425 /* This is Windows memory management related - we don't need this */
1429 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
1431 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1432 TRACE("iface %p, flags %#x.\n", iface, flags);
1434 return ddraw_surface7_PageLock((IDirectDrawSurface7 *)This, flags);
1437 /*****************************************************************************
1438 * IDirectDrawSurface7::PageUnlock
1440 * Allows a sysmem surface to be paged out
1443 * Flags: Not used, must be 0(unchecked)
1446 * DD_OK, because it's a stub
1448 *****************************************************************************/
1449 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
1451 TRACE("iface %p, flags %#x.\n", iface, Flags);
1456 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
1458 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1459 TRACE("iface %p, flags %#x.\n", iface, flags);
1461 return ddraw_surface7_PageUnlock((IDirectDrawSurface7 *)This, flags);
1464 /*****************************************************************************
1465 * IDirectDrawSurface7::BltBatch
1467 * An unimplemented function
1475 *****************************************************************************/
1476 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1478 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
1480 /* MSDN: "not currently implemented" */
1481 return DDERR_UNSUPPORTED;
1484 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
1486 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1487 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
1489 return ddraw_surface7_BltBatch((IDirectDrawSurface7 *)This, batch, count, flags);
1492 /*****************************************************************************
1493 * IDirectDrawSurface7::EnumAttachedSurfaces
1495 * Enumerates all surfaces attached to this surface
1498 * context: Pointer to pass unmodified to the callback
1499 * cb: Callback function to call for each surface
1503 * DDERR_INVALIDPARAMS if cb is NULL
1505 *****************************************************************************/
1506 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1507 void *context, LPDDENUMSURFACESCALLBACK7 cb)
1509 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1510 IDirectDrawSurfaceImpl *surf;
1511 DDSURFACEDESC2 desc;
1514 /* Attached surfaces aren't handled in WineD3D */
1515 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
1518 return DDERR_INVALIDPARAMS;
1520 EnterCriticalSection(&ddraw_cs);
1521 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1523 surf = This->complex_array[i];
1526 ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1527 desc = surf->surface_desc;
1528 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1529 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1531 LeaveCriticalSection(&ddraw_cs);
1536 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1538 ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1539 desc = surf->surface_desc;
1540 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1541 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1543 LeaveCriticalSection(&ddraw_cs);
1548 TRACE(" end of enumeration.\n");
1550 LeaveCriticalSection(&ddraw_cs);
1554 struct callback_info
1556 LPDDENUMSURFACESCALLBACK callback;
1560 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
1562 const struct callback_info *info = context;
1564 return info->callback((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_iface,
1565 (DDSURFACEDESC *)surface_desc, info->context);
1568 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
1569 void *context, LPDDENUMSURFACESCALLBACK callback)
1571 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1572 struct callback_info info;
1574 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
1576 info.callback = callback;
1577 info.context = context;
1579 return ddraw_surface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)This,
1580 &info, EnumCallback);
1583 /*****************************************************************************
1584 * IDirectDrawSurface7::EnumOverlayZOrders
1586 * "Enumerates the overlay surfaces on the specified destination"
1589 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
1590 * context: context to pass back to the callback
1591 * cb: callback function to call for each enumerated surface
1594 * DD_OK, because it's a stub
1596 *****************************************************************************/
1597 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1598 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
1600 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
1605 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
1606 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
1608 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1609 struct callback_info info;
1611 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
1613 info.callback = callback;
1614 info.context = context;
1616 return ddraw_surface7_EnumOverlayZOrders((IDirectDrawSurface7 *)This,
1617 flags, &info, EnumCallback);
1620 /*****************************************************************************
1621 * IDirectDrawSurface7::GetBltStatus
1623 * Returns the blitting status
1626 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1629 * See IWineD3DSurface::Blt
1631 *****************************************************************************/
1632 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1634 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1637 TRACE("iface %p, flags %#x.\n", iface, Flags);
1639 EnterCriticalSection(&ddraw_cs);
1640 hr = wined3d_surface_get_blt_status(This->wined3d_surface, Flags);
1641 LeaveCriticalSection(&ddraw_cs);
1644 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1649 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
1651 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1652 TRACE("iface %p, flags %#x.\n", iface, flags);
1654 return ddraw_surface7_GetBltStatus((IDirectDrawSurface7 *)This, flags);
1657 /*****************************************************************************
1658 * IDirectDrawSurface7::GetColorKey
1660 * Returns the color key assigned to the surface
1664 * CKey: Address to store the key to
1668 * DDERR_INVALIDPARAMS if CKey is NULL
1670 *****************************************************************************/
1671 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
1673 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1675 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
1678 return DDERR_INVALIDPARAMS;
1680 EnterCriticalSection(&ddraw_cs);
1684 case DDCKEY_DESTBLT:
1685 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
1687 LeaveCriticalSection(&ddraw_cs);
1688 return DDERR_NOCOLORKEY;
1690 *CKey = This->surface_desc.ddckCKDestBlt;
1693 case DDCKEY_DESTOVERLAY:
1694 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
1696 LeaveCriticalSection(&ddraw_cs);
1697 return DDERR_NOCOLORKEY;
1699 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1703 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
1705 LeaveCriticalSection(&ddraw_cs);
1706 return DDERR_NOCOLORKEY;
1708 *CKey = This->surface_desc.ddckCKSrcBlt;
1711 case DDCKEY_SRCOVERLAY:
1712 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
1714 LeaveCriticalSection(&ddraw_cs);
1715 return DDERR_NOCOLORKEY;
1717 *CKey = This->surface_desc.ddckCKSrcOverlay;
1721 LeaveCriticalSection(&ddraw_cs);
1722 return DDERR_INVALIDPARAMS;
1725 LeaveCriticalSection(&ddraw_cs);
1729 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
1731 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1732 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
1734 return ddraw_surface7_GetColorKey((IDirectDrawSurface7 *)This, flags, color_key);
1737 /*****************************************************************************
1738 * IDirectDrawSurface7::GetFlipStatus
1740 * Returns the flipping status of the surface
1743 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1746 * See IWineD3DSurface::GetFlipStatus
1748 *****************************************************************************/
1749 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1751 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1754 TRACE("iface %p, flags %#x.\n", iface, Flags);
1756 EnterCriticalSection(&ddraw_cs);
1757 hr = wined3d_surface_get_flip_status(This->wined3d_surface, Flags);
1758 LeaveCriticalSection(&ddraw_cs);
1761 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1766 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
1768 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1769 TRACE("iface %p, flags %#x.\n", iface, flags);
1771 return ddraw_surface7_GetFlipStatus((IDirectDrawSurface7 *)This, flags);
1774 /*****************************************************************************
1775 * IDirectDrawSurface7::GetOverlayPosition
1777 * Returns the display coordinates of a visible and active overlay surface
1784 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
1785 *****************************************************************************/
1786 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
1788 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1791 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
1793 EnterCriticalSection(&ddraw_cs);
1794 hr = wined3d_surface_get_overlay_position(This->wined3d_surface, X, Y);
1795 LeaveCriticalSection(&ddraw_cs);
1799 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
1801 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1802 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
1804 return ddraw_surface7_GetOverlayPosition((IDirectDrawSurface7 *)This, x, y);
1807 /*****************************************************************************
1808 * IDirectDrawSurface7::GetPixelFormat
1810 * Returns the pixel format of the Surface
1813 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1814 * format should be written
1818 * DDERR_INVALIDPARAMS if PixelFormat is NULL
1820 *****************************************************************************/
1821 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
1823 /* What is DDERR_INVALIDSURFACETYPE for here? */
1824 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1826 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
1829 return DDERR_INVALIDPARAMS;
1831 EnterCriticalSection(&ddraw_cs);
1832 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1833 LeaveCriticalSection(&ddraw_cs);
1838 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
1840 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1841 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
1843 return ddraw_surface7_GetPixelFormat((IDirectDrawSurface7 *)This, pixel_format);
1846 /*****************************************************************************
1847 * IDirectDrawSurface7::GetSurfaceDesc
1849 * Returns the description of this surface
1852 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1857 * DDERR_INVALIDPARAMS if DDSD is NULL
1859 *****************************************************************************/
1860 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
1862 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1864 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1867 return DDERR_INVALIDPARAMS;
1869 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1871 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1872 return DDERR_INVALIDPARAMS;
1875 EnterCriticalSection(&ddraw_cs);
1876 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1877 TRACE("Returning surface desc:\n");
1878 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1880 LeaveCriticalSection(&ddraw_cs);
1884 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
1886 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1888 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1890 if (!surface_desc) return DDERR_INVALIDPARAMS;
1892 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
1894 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
1895 return DDERR_INVALIDPARAMS;
1898 EnterCriticalSection(&ddraw_cs);
1899 DD_STRUCT_COPY_BYSIZE(surface_desc, (DDSURFACEDESC *)&This->surface_desc);
1900 TRACE("Returning surface desc:\n");
1901 if (TRACE_ON(ddraw))
1903 /* DDRAW_dump_surface_desc handles the smaller size */
1904 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
1907 LeaveCriticalSection(&ddraw_cs);
1911 /*****************************************************************************
1912 * IDirectDrawSurface7::Initialize
1914 * Initializes the surface. This is a no-op in Wine
1917 * DD: Pointer to an DirectDraw interface
1918 * DDSD: Surface description for initialization
1921 * DDERR_ALREADYINITIALIZED
1923 *****************************************************************************/
1924 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
1925 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
1927 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1929 return DDERR_ALREADYINITIALIZED;
1932 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
1933 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
1935 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
1936 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1938 return ddraw_surface7_Initialize((IDirectDrawSurface7 *)This,
1939 ddraw, (DDSURFACEDESC2 *)surface_desc);
1942 /*****************************************************************************
1943 * IDirect3DTexture1::Initialize
1945 * The sdk says it's not implemented
1953 *****************************************************************************/
1954 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
1955 IDirect3DDevice *device, IDirectDrawSurface *surface)
1957 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
1959 return DDERR_UNSUPPORTED; /* Unchecked */
1962 /*****************************************************************************
1963 * IDirectDrawSurface7::IsLost
1965 * Checks if the surface is lost
1968 * DD_OK, if the surface is usable
1969 * DDERR_ISLOST if the surface is lost
1970 * See IWineD3DSurface::IsLost for more details
1972 *****************************************************************************/
1973 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
1975 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1978 TRACE("iface %p.\n", iface);
1980 EnterCriticalSection(&ddraw_cs);
1981 /* We lose the surface if the implementation was changed */
1982 if(This->ImplType != This->ddraw->ImplType)
1984 /* But this shouldn't happen. When we change the implementation,
1985 * all surfaces are re-created automatically, and their content
1988 ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
1989 LeaveCriticalSection(&ddraw_cs);
1990 return DDERR_SURFACELOST;
1993 hr = wined3d_surface_is_lost(This->wined3d_surface);
1994 LeaveCriticalSection(&ddraw_cs);
1997 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
1998 * WineD3D uses the same error for surfaces
2000 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
2005 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
2007 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2008 TRACE("iface %p.\n", iface);
2010 return ddraw_surface7_IsLost((IDirectDrawSurface7 *)This);
2013 /*****************************************************************************
2014 * IDirectDrawSurface7::Restore
2016 * Restores a lost surface. This makes the surface usable again, but
2017 * doesn't reload its old contents
2021 * See IWineD3DSurface::Restore for more details
2023 *****************************************************************************/
2024 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
2026 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2029 TRACE("iface %p.\n", iface);
2031 EnterCriticalSection(&ddraw_cs);
2032 if(This->ImplType != This->ddraw->ImplType)
2034 /* Call the recreation callback. Make sure to AddRef first */
2035 IDirectDrawSurface_AddRef(iface);
2036 ddraw_recreate_surfaces_cb(iface, &This->surface_desc, NULL /* Not needed */);
2038 hr = wined3d_surface_restore(This->wined3d_surface);
2039 LeaveCriticalSection(&ddraw_cs);
2043 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
2045 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2046 TRACE("iface %p.\n", iface);
2048 return ddraw_surface7_Restore((IDirectDrawSurface7 *)This);
2051 /*****************************************************************************
2052 * IDirectDrawSurface7::SetOverlayPosition
2054 * Changes the display coordinates of an overlay surface
2061 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
2062 *****************************************************************************/
2063 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
2065 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2068 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
2070 EnterCriticalSection(&ddraw_cs);
2071 hr = wined3d_surface_set_overlay_position(This->wined3d_surface, X, Y);
2072 LeaveCriticalSection(&ddraw_cs);
2076 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
2078 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2079 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
2081 return ddraw_surface7_SetOverlayPosition((IDirectDrawSurface7 *)This, x, y);
2084 /*****************************************************************************
2085 * IDirectDrawSurface7::UpdateOverlay
2087 * Modifies the attributes of an overlay surface.
2090 * SrcRect: The section of the source being used for the overlay
2091 * DstSurface: Address of the surface that is overlaid
2092 * DstRect: Place of the overlay
2093 * Flags: some DDOVER_* flags
2096 * DDERR_UNSUPPORTED, because we don't support overlays
2098 *****************************************************************************/
2099 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
2100 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
2102 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2103 IDirectDrawSurfaceImpl *Dst = (IDirectDrawSurfaceImpl *)DstSurface;
2106 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2107 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
2109 EnterCriticalSection(&ddraw_cs);
2110 hr = wined3d_surface_update_overlay(This->wined3d_surface, SrcRect,
2111 Dst ? Dst->wined3d_surface : NULL, DstRect, Flags, (WINEDDOVERLAYFX *)FX);
2112 LeaveCriticalSection(&ddraw_cs);
2114 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2115 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
2116 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
2122 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
2123 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
2125 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2126 IDirectDrawSurfaceImpl *dst_impl = unsafe_impl_from_IDirectDrawSurface3(dst_surface);
2127 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2128 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
2130 return ddraw_surface7_UpdateOverlay((IDirectDrawSurface7 *)This, src_rect,
2131 dst_impl ? (IDirectDrawSurface7 *)dst_impl : NULL, dst_rect, flags, fx);
2134 /*****************************************************************************
2135 * IDirectDrawSurface7::UpdateOverlayDisplay
2137 * The DX7 sdk says that it's not implemented
2142 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
2144 *****************************************************************************/
2145 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
2147 TRACE("iface %p, flags %#x.\n", iface, Flags);
2149 return DDERR_UNSUPPORTED;
2152 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
2154 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2155 TRACE("iface %p, flags %#x.\n", iface, flags);
2157 return ddraw_surface7_UpdateOverlayDisplay((IDirectDrawSurface7 *)This, flags);
2160 /*****************************************************************************
2161 * IDirectDrawSurface7::UpdateOverlayZOrder
2163 * Sets an overlay's Z order
2166 * Flags: DDOVERZ_* flags
2167 * DDSRef: Defines the relative position in the overlay chain
2170 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
2172 *****************************************************************************/
2173 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
2174 DWORD Flags, IDirectDrawSurface7 *DDSRef)
2176 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2177 IDirectDrawSurfaceImpl *Ref = (IDirectDrawSurfaceImpl *)DDSRef;
2180 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
2182 EnterCriticalSection(&ddraw_cs);
2183 hr = wined3d_surface_update_overlay_z_order(This->wined3d_surface,
2184 Flags, Ref ? Ref->wined3d_surface : NULL);
2185 LeaveCriticalSection(&ddraw_cs);
2189 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
2190 DWORD flags, IDirectDrawSurface3 *reference)
2192 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2193 IDirectDrawSurfaceImpl *reference_impl = unsafe_impl_from_IDirectDrawSurface3(reference);
2194 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
2196 return ddraw_surface7_UpdateOverlayZOrder((IDirectDrawSurface7 *)This, flags,
2197 reference_impl ? (IDirectDrawSurface7 *)reference_impl : NULL);
2200 /*****************************************************************************
2201 * IDirectDrawSurface7::GetDDInterface
2203 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
2204 * surface belongs to
2207 * DD: Address to write the interface pointer to
2211 * DDERR_INVALIDPARAMS if DD is NULL
2213 *****************************************************************************/
2214 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
2216 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2218 TRACE("iface %p, ddraw %p.\n", iface, DD);
2221 return DDERR_INVALIDPARAMS;
2223 switch(This->version)
2226 *DD = &This->ddraw->IDirectDraw7_iface;
2230 *DD = &This->ddraw->IDirectDraw4_iface;
2234 *DD = &This->ddraw->IDirectDraw2_iface;
2238 *DD = &This->ddraw->IDirectDraw_iface;
2242 IUnknown_AddRef((IUnknown *)*DD);
2247 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
2249 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2250 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
2252 return ddraw_surface7_GetDDInterface((IDirectDrawSurface7 *)This, ddraw);
2255 /* This seems also windows implementation specific - I don't think WineD3D needs this */
2256 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
2258 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2259 volatile IDirectDrawSurfaceImpl* vThis = This;
2261 TRACE("iface %p.\n", iface);
2263 EnterCriticalSection(&ddraw_cs);
2264 /* A uniqueness value of 0 is apparently special.
2265 * This needs to be checked.
2266 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
2269 DWORD old_uniqueness_value = vThis->uniqueness_value;
2270 DWORD new_uniqueness_value = old_uniqueness_value+1;
2272 if (old_uniqueness_value == 0) break;
2273 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
2275 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
2276 old_uniqueness_value,
2277 new_uniqueness_value)
2278 == old_uniqueness_value)
2282 LeaveCriticalSection(&ddraw_cs);
2286 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
2288 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2290 TRACE("iface %p, value %p.\n", iface, pValue);
2292 EnterCriticalSection(&ddraw_cs);
2293 *pValue = This->uniqueness_value;
2294 LeaveCriticalSection(&ddraw_cs);
2298 /*****************************************************************************
2299 * IDirectDrawSurface7::SetLOD
2301 * Sets the level of detail of a texture
2304 * MaxLOD: LOD to set
2308 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2310 *****************************************************************************/
2311 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
2313 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2316 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
2318 EnterCriticalSection(&ddraw_cs);
2319 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2321 LeaveCriticalSection(&ddraw_cs);
2322 return DDERR_INVALIDOBJECT;
2325 if (!This->wined3d_texture)
2327 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2328 LeaveCriticalSection(&ddraw_cs);
2329 return DDERR_INVALIDOBJECT;
2332 hr = wined3d_texture_set_lod(This->wined3d_texture, MaxLOD);
2333 LeaveCriticalSection(&ddraw_cs);
2337 /*****************************************************************************
2338 * IDirectDrawSurface7::GetLOD
2340 * Returns the level of detail of a Direct3D texture
2343 * MaxLOD: Address to write the LOD to
2347 * DDERR_INVALIDPARAMS if MaxLOD is NULL
2348 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2350 *****************************************************************************/
2351 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
2353 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2355 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
2358 return DDERR_INVALIDPARAMS;
2360 EnterCriticalSection(&ddraw_cs);
2361 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2363 LeaveCriticalSection(&ddraw_cs);
2364 return DDERR_INVALIDOBJECT;
2367 *MaxLOD = wined3d_texture_get_lod(This->wined3d_texture);
2368 LeaveCriticalSection(&ddraw_cs);
2372 /*****************************************************************************
2373 * IDirectDrawSurface7::BltFast
2375 * Performs a fast Blit.
2378 * dstx: The x coordinate to blit to on the destination
2379 * dsty: The y coordinate to blit to on the destination
2380 * Source: The source surface
2381 * rsrc: The source rectangle
2382 * trans: Type of transfer. Some DDBLTFAST_* flags
2386 * For more details, see IWineD3DSurface::BltFast
2388 *****************************************************************************/
2389 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
2390 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
2392 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2393 IDirectDrawSurfaceImpl *src = (IDirectDrawSurfaceImpl *)Source;
2394 DWORD src_w, src_h, dst_w, dst_h;
2397 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2398 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
2400 dst_w = This->surface_desc.dwWidth;
2401 dst_h = This->surface_desc.dwHeight;
2403 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2408 if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2409 rsrc->right > src->surface_desc.dwWidth ||
2410 rsrc->bottom > src->surface_desc.dwHeight)
2412 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2413 return DDERR_INVALIDRECT;
2416 src_w = rsrc->right - rsrc->left;
2417 src_h = rsrc->bottom - rsrc->top;
2421 src_w = src->surface_desc.dwWidth;
2422 src_h = src->surface_desc.dwHeight;
2425 if (src_w > dst_w || dstx > dst_w - src_w
2426 || src_h > dst_h || dsty > dst_h - src_h)
2428 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
2429 return DDERR_INVALIDRECT;
2432 EnterCriticalSection(&ddraw_cs);
2433 hr = wined3d_surface_bltfast(This->wined3d_surface, dstx, dsty,
2434 src ? src->wined3d_surface : NULL, rsrc, trans);
2435 LeaveCriticalSection(&ddraw_cs);
2438 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
2439 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
2444 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
2445 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
2447 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2448 IDirectDrawSurfaceImpl *src_impl = unsafe_impl_from_IDirectDrawSurface3(src_surface);
2449 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2450 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
2452 return ddraw_surface7_BltFast((IDirectDrawSurface7 *)This, dst_x, dst_y,
2453 src_impl ? (IDirectDrawSurface7 *)src_impl : NULL, src_rect, flags);
2456 /*****************************************************************************
2457 * IDirectDrawSurface7::GetClipper
2459 * Returns the IDirectDrawClipper interface of the clipper assigned to this
2463 * Clipper: Address to store the interface pointer at
2467 * DDERR_INVALIDPARAMS if Clipper is NULL
2468 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
2470 *****************************************************************************/
2471 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
2473 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2475 TRACE("iface %p, clipper %p.\n", iface, Clipper);
2479 LeaveCriticalSection(&ddraw_cs);
2480 return DDERR_INVALIDPARAMS;
2483 EnterCriticalSection(&ddraw_cs);
2484 if(This->clipper == NULL)
2486 LeaveCriticalSection(&ddraw_cs);
2487 return DDERR_NOCLIPPERATTACHED;
2490 *Clipper = (IDirectDrawClipper *)This->clipper;
2491 IDirectDrawClipper_AddRef(*Clipper);
2492 LeaveCriticalSection(&ddraw_cs);
2496 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
2498 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2499 TRACE("iface %p, clipper %p.\n", iface, clipper);
2501 return ddraw_surface7_GetClipper((IDirectDrawSurface7 *)This, clipper);
2504 /*****************************************************************************
2505 * IDirectDrawSurface7::SetClipper
2507 * Sets a clipper for the surface
2510 * Clipper: IDirectDrawClipper interface of the clipper to set
2515 *****************************************************************************/
2516 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper *Clipper)
2518 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2519 IDirectDrawClipperImpl *oldClipper = This->clipper;
2523 TRACE("iface %p, clipper %p.\n", iface, Clipper);
2525 EnterCriticalSection(&ddraw_cs);
2526 if ((IDirectDrawClipperImpl *)Clipper == This->clipper)
2528 LeaveCriticalSection(&ddraw_cs);
2532 This->clipper = (IDirectDrawClipperImpl *)Clipper;
2534 if (Clipper != NULL)
2535 IDirectDrawClipper_AddRef(Clipper);
2537 IDirectDrawClipper_Release((IDirectDrawClipper *)oldClipper);
2539 hr = wined3d_surface_set_clipper(This->wined3d_surface,
2540 This->clipper ? This->clipper->wineD3DClipper : NULL);
2542 if (This->wined3d_swapchain)
2546 IDirectDrawClipper_GetHWnd(Clipper, &clipWindow);
2550 wined3d_swapchain_set_window(This->wined3d_swapchain, clipWindow);
2552 wined3d_swapchain_set_window(This->wined3d_swapchain, This->ddraw->d3d_window);
2555 LeaveCriticalSection(&ddraw_cs);
2559 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
2561 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2562 TRACE("iface %p, clipper %p.\n", iface, clipper);
2564 return ddraw_surface7_SetClipper((IDirectDrawSurface7 *)This, clipper);
2567 /*****************************************************************************
2568 * IDirectDrawSurface7::SetSurfaceDesc
2570 * Sets the surface description. It can override the pixel format, the surface
2572 * It's not really tested.
2575 * DDSD: Pointer to the new surface description to set
2580 * DDERR_INVALIDPARAMS if DDSD is NULL
2582 *****************************************************************************/
2583 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
2585 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2586 enum wined3d_format_id newFormat = WINED3DFMT_UNKNOWN;
2589 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
2592 return DDERR_INVALIDPARAMS;
2594 EnterCriticalSection(&ddraw_cs);
2595 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2597 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2599 if(newFormat == WINED3DFMT_UNKNOWN)
2601 ERR("Requested to set an unknown pixelformat\n");
2602 LeaveCriticalSection(&ddraw_cs);
2603 return DDERR_INVALIDPARAMS;
2605 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2607 hr = wined3d_surface_set_format(This->wined3d_surface, newFormat);
2610 LeaveCriticalSection(&ddraw_cs);
2615 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2617 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTOVERLAY,
2618 (WINEDDCOLORKEY *)&DDSD->u3.ddckCKDestOverlay);
2620 if (DDSD->dwFlags & DDSD_CKDESTBLT)
2622 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_DESTBLT,
2623 (WINEDDCOLORKEY *)&DDSD->ddckCKDestBlt);
2625 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2627 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCOVERLAY,
2628 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcOverlay);
2630 if (DDSD->dwFlags & DDSD_CKSRCBLT)
2632 wined3d_surface_set_color_key(This->wined3d_surface, DDCKEY_SRCBLT,
2633 (WINEDDCOLORKEY *)&DDSD->ddckCKSrcBlt);
2635 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2637 hr = wined3d_surface_set_mem(This->wined3d_surface, DDSD->lpSurface);
2640 /* No need for a trace here, wined3d does that for us */
2643 case WINED3DERR_INVALIDCALL:
2644 LeaveCriticalSection(&ddraw_cs);
2645 return DDERR_INVALIDPARAMS;
2652 This->surface_desc = *DDSD;
2654 LeaveCriticalSection(&ddraw_cs);
2658 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
2659 DDSURFACEDESC *surface_desc, DWORD flags)
2661 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2662 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
2664 return ddraw_surface7_SetSurfaceDesc((IDirectDrawSurface7 *)This,
2665 (DDSURFACEDESC2 *)surface_desc, flags);
2668 /*****************************************************************************
2669 * IDirectDrawSurface7::GetPalette
2671 * Returns the IDirectDrawPalette interface of the palette currently assigned
2675 * Pal: Address to write the interface pointer to
2679 * DDERR_INVALIDPARAMS if Pal is NULL
2681 *****************************************************************************/
2682 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
2684 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2685 struct wined3d_palette *wined3d_palette;
2688 TRACE("iface %p, palette %p.\n", iface, Pal);
2691 return DDERR_INVALIDPARAMS;
2693 EnterCriticalSection(&ddraw_cs);
2694 wined3d_palette = wined3d_surface_get_palette(This->wined3d_surface);
2695 if (wined3d_palette)
2697 *Pal = wined3d_palette_get_parent(wined3d_palette);
2698 IDirectDrawPalette_AddRef(*Pal);
2703 hr = DDERR_NOPALETTEATTACHED;
2706 LeaveCriticalSection(&ddraw_cs);
2710 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
2712 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2713 TRACE("iface %p, palette %p.\n", iface, palette);
2715 return ddraw_surface7_GetPalette((IDirectDrawSurface7 *)This, palette);
2718 /*****************************************************************************
2721 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2722 * recursively in the surface tree
2724 *****************************************************************************/
2728 WINEDDCOLORKEY *CKey;
2732 static HRESULT WINAPI
2733 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2734 DDSURFACEDESC2 *desc,
2737 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)surface;
2738 struct SCKContext *ctx = context;
2741 hr = wined3d_surface_set_color_key(This->wined3d_surface, ctx->Flags, ctx->CKey);
2744 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2748 ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
2749 ddraw_surface7_Release(surface);
2751 return DDENUMRET_OK;
2754 /*****************************************************************************
2755 * IDirectDrawSurface7::SetColorKey
2757 * Sets the color keying options for the surface. Observations showed that
2758 * in case of complex surfaces the color key has to be assigned to all
2763 * CKey: The new color key
2767 * See IWineD3DSurface::SetColorKey for details
2769 *****************************************************************************/
2770 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2772 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2773 DDCOLORKEY FixedCKey;
2774 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
2776 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2778 EnterCriticalSection(&ddraw_cs);
2782 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
2783 if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
2784 FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
2786 switch (Flags & ~DDCKEY_COLORSPACE)
2788 case DDCKEY_DESTBLT:
2789 This->surface_desc.ddckCKDestBlt = FixedCKey;
2790 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2793 case DDCKEY_DESTOVERLAY:
2794 This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
2795 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2798 case DDCKEY_SRCOVERLAY:
2799 This->surface_desc.ddckCKSrcOverlay = FixedCKey;
2800 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2804 This->surface_desc.ddckCKSrcBlt = FixedCKey;
2805 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2809 LeaveCriticalSection(&ddraw_cs);
2810 return DDERR_INVALIDPARAMS;
2815 switch (Flags & ~DDCKEY_COLORSPACE)
2817 case DDCKEY_DESTBLT:
2818 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2821 case DDCKEY_DESTOVERLAY:
2822 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2825 case DDCKEY_SRCOVERLAY:
2826 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2830 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2834 LeaveCriticalSection(&ddraw_cs);
2835 return DDERR_INVALIDPARAMS;
2838 ctx.ret = wined3d_surface_set_color_key(This->wined3d_surface, Flags, ctx.CKey);
2839 ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
2840 LeaveCriticalSection(&ddraw_cs);
2843 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2844 default: return ctx.ret;
2848 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2850 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2851 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2853 return ddraw_surface7_SetColorKey((IDirectDrawSurface7 *)This, flags, color_key);
2856 /*****************************************************************************
2857 * IDirectDrawSurface7::SetPalette
2859 * Assigns a DirectDrawPalette object to the surface
2862 * Pal: Interface to the palette to set
2867 *****************************************************************************/
2868 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
2870 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2871 IDirectDrawPalette *oldPal;
2872 IDirectDrawSurfaceImpl *surf;
2873 IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
2876 TRACE("iface %p, palette %p.\n", iface, Pal);
2878 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
2879 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
2880 return DDERR_INVALIDPIXELFORMAT;
2883 /* Find the old palette */
2884 EnterCriticalSection(&ddraw_cs);
2885 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2886 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2888 LeaveCriticalSection(&ddraw_cs);
2891 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
2893 /* Set the new Palette */
2894 wined3d_surface_set_palette(This->wined3d_surface, PalImpl ? PalImpl->wineD3DPalette : NULL);
2895 /* AddRef the Palette */
2896 if(Pal) IDirectDrawPalette_AddRef(Pal);
2898 /* Release the old palette */
2899 if(oldPal) IDirectDrawPalette_Release(oldPal);
2901 /* If this is a front buffer, also update the back buffers
2902 * TODO: How do things work for palettized cube textures?
2904 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2906 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2907 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2912 IDirectDrawSurface7 *attach;
2914 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &caps2, &attach);
2920 TRACE("Setting palette on %p\n", attach);
2921 ddraw_surface7_SetPalette(attach, Pal);
2922 surf = (IDirectDrawSurfaceImpl *)attach;
2923 ddraw_surface7_Release(attach);
2927 LeaveCriticalSection(&ddraw_cs);
2931 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
2933 IDirectDrawSurfaceImpl *This = impl_from_IDirectDrawSurface3(iface);
2934 TRACE("iface %p, palette %p.\n", iface, palette);
2936 return ddraw_surface7_SetPalette((IDirectDrawSurface7 *)This, palette);
2939 /**********************************************************
2940 * IDirectDrawGammaControl::GetGammaRamp
2942 * Returns the current gamma ramp for a surface
2946 * gamma_ramp: Address to write the ramp to
2950 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
2952 **********************************************************/
2953 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
2954 DWORD flags, DDGAMMARAMP *gamma_ramp)
2956 IDirectDrawSurfaceImpl *surface = surface_from_gamma_control(iface);
2958 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
2962 WARN("Invalid gamma_ramp passed.\n");
2963 return DDERR_INVALIDPARAMS;
2966 EnterCriticalSection(&ddraw_cs);
2967 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
2969 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP. */
2970 wined3d_device_get_gamma_ramp(surface->ddraw->wined3d_device, 0, (WINED3DGAMMARAMP *)gamma_ramp);
2974 ERR("Not implemented for non-primary surfaces.\n");
2976 LeaveCriticalSection(&ddraw_cs);
2981 /**********************************************************
2982 * IDirectDrawGammaControl::SetGammaRamp
2984 * Sets the red, green and blue gamma ramps for
2987 * flags: Can be DDSGR_CALIBRATE to request calibration
2988 * gamma_ramp: Structure containing the new gamma ramp
2992 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
2994 **********************************************************/
2995 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
2996 DWORD flags, DDGAMMARAMP *gamma_ramp)
2998 IDirectDrawSurfaceImpl *surface = surface_from_gamma_control(iface);
3000 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
3004 WARN("Invalid gamma_ramp passed.\n");
3005 return DDERR_INVALIDPARAMS;
3008 EnterCriticalSection(&ddraw_cs);
3009 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3011 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP */
3012 wined3d_device_set_gamma_ramp(surface->ddraw->wined3d_device, 0, flags, (WINED3DGAMMARAMP *)gamma_ramp);
3016 ERR("Not implemented for non-primary surfaces.\n");
3018 LeaveCriticalSection(&ddraw_cs);
3023 /*****************************************************************************
3024 * IDirect3DTexture2::PaletteChanged
3026 * Informs the texture about a palette change
3029 * start: Start index of the change
3030 * count: The number of changed entries
3033 * D3D_OK, because it's a stub
3035 *****************************************************************************/
3036 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
3038 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
3043 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
3045 IDirectDrawSurfaceImpl *surface = surface_from_texture1(iface);
3047 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
3049 return d3d_texture2_PaletteChanged((IDirect3DTexture2 *)&surface->IDirect3DTexture2_vtbl, start, count);
3052 /*****************************************************************************
3053 * IDirect3DTexture::Unload
3055 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
3061 *****************************************************************************/
3062 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
3064 WARN("iface %p. Not implemented.\n", iface);
3066 return DDERR_UNSUPPORTED;
3069 /*****************************************************************************
3070 * IDirect3DTexture2::GetHandle
3072 * Returns handle for the texture. At the moment, the interface
3073 * to the IWineD3DTexture is used.
3076 * device: Device this handle is assigned to
3077 * handle: Address to store the handle at.
3082 *****************************************************************************/
3083 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
3084 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
3086 IDirectDrawSurfaceImpl *surface = surface_from_texture2(iface);
3088 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
3090 EnterCriticalSection(&ddraw_cs);
3092 if (!surface->Handle)
3094 DWORD h = ddraw_allocate_handle(&device_from_device2(device)->handle_table, surface, DDRAW_HANDLE_SURFACE);
3095 if (h == DDRAW_INVALID_HANDLE)
3097 ERR("Failed to allocate a texture handle.\n");
3098 LeaveCriticalSection(&ddraw_cs);
3099 return DDERR_OUTOFMEMORY;
3102 surface->Handle = h + 1;
3105 TRACE("Returning handle %08x.\n", surface->Handle);
3106 *handle = surface->Handle;
3108 LeaveCriticalSection(&ddraw_cs);
3113 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
3114 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
3116 IDirect3DTexture2 *texture2 = (IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl;
3117 IDirect3DDevice2 *device2 = (IDirect3DDevice2 *)&device_from_device1(device)->IDirect3DDevice2_vtbl;
3119 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
3121 return d3d_texture2_GetHandle(texture2, device2, handle);
3124 /*****************************************************************************
3125 * get_sub_mimaplevel
3127 * Helper function that returns the next mipmap level
3129 * tex_ptr: Surface of which to return the next level
3131 *****************************************************************************/
3132 static IDirectDrawSurfaceImpl *get_sub_mimaplevel(IDirectDrawSurfaceImpl *surface)
3134 /* Now go down the mipmap chain to the next surface */
3135 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
3136 IDirectDrawSurface7 *next_level;
3139 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surface, &mipmap_caps, &next_level);
3140 if (FAILED(hr)) return NULL;
3142 ddraw_surface7_Release(next_level);
3144 return (IDirectDrawSurfaceImpl *)next_level;
3147 /*****************************************************************************
3148 * IDirect3DTexture2::Load
3150 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
3152 * This function isn't relayed to WineD3D because the whole interface is
3153 * implemented in DDraw only. For speed improvements a implementation which
3154 * takes OpenGL more into account could be placed into WineD3D.
3157 * src_texture: Address of the texture to load
3161 * D3DERR_TEXTURE_LOAD_FAILED.
3163 *****************************************************************************/
3164 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
3166 IDirectDrawSurfaceImpl *dst_surface = surface_from_texture2(iface);
3167 IDirectDrawSurfaceImpl *src_surface = surface_from_texture2(src_texture);
3170 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
3172 if (src_surface == dst_surface)
3174 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
3178 EnterCriticalSection(&ddraw_cs);
3180 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3181 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
3182 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
3184 ERR("Trying to load surfaces with different mip-map counts.\n");
3189 struct wined3d_palette *wined3d_dst_pal, *wined3d_src_pal;
3190 IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
3191 DDSURFACEDESC *src_desc, *dst_desc;
3193 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
3194 src_surface, dst_surface, src_surface->mipmap_level);
3196 /* Suppress the ALLOCONLOAD flag */
3197 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
3199 /* Get the palettes */
3200 wined3d_dst_pal = wined3d_surface_get_palette(dst_surface->wined3d_surface);
3201 if (wined3d_dst_pal)
3202 dst_pal = wined3d_palette_get_parent(wined3d_dst_pal);
3204 wined3d_src_pal = wined3d_surface_get_palette(src_surface->wined3d_surface);
3205 if (wined3d_src_pal)
3206 src_pal = wined3d_palette_get_parent(wined3d_src_pal);
3210 PALETTEENTRY palent[256];
3214 LeaveCriticalSection(&ddraw_cs);
3215 return DDERR_NOPALETTEATTACHED;
3217 IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
3218 IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
3221 /* Copy one surface on the other */
3222 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
3223 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
3225 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
3227 /* Should also check for same pixel format, u1.lPitch, ... */
3228 ERR("Error in surface sizes.\n");
3229 LeaveCriticalSection(&ddraw_cs);
3230 return D3DERR_TEXTURE_LOAD_FAILED;
3234 WINED3DLOCKED_RECT src_rect, dst_rect;
3236 /* Copy also the ColorKeying stuff */
3237 if (src_desc->dwFlags & DDSD_CKSRCBLT)
3239 dst_desc->dwFlags |= DDSD_CKSRCBLT;
3240 dst_desc->ddckCKSrcBlt.dwColorSpaceLowValue = src_desc->ddckCKSrcBlt.dwColorSpaceLowValue;
3241 dst_desc->ddckCKSrcBlt.dwColorSpaceHighValue = src_desc->ddckCKSrcBlt.dwColorSpaceHighValue;
3244 /* Copy the main memory texture into the surface that corresponds
3245 * to the OpenGL texture object. */
3247 hr = wined3d_surface_map(src_surface->wined3d_surface, &src_rect, NULL, 0);
3250 ERR("Failed to lock source surface, hr %#x.\n", hr);
3251 LeaveCriticalSection(&ddraw_cs);
3252 return D3DERR_TEXTURE_LOAD_FAILED;
3255 hr = wined3d_surface_map(dst_surface->wined3d_surface, &dst_rect, NULL, 0);
3258 ERR("Failed to lock destination surface, hr %#x.\n", hr);
3259 wined3d_surface_unmap(src_surface->wined3d_surface);
3260 LeaveCriticalSection(&ddraw_cs);
3261 return D3DERR_TEXTURE_LOAD_FAILED;
3264 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
3265 memcpy(dst_rect.pBits, src_rect.pBits, src_surface->surface_desc.u1.dwLinearSize);
3267 memcpy(dst_rect.pBits, src_rect.pBits, src_rect.Pitch * src_desc->dwHeight);
3269 wined3d_surface_unmap(src_surface->wined3d_surface);
3270 wined3d_surface_unmap(dst_surface->wined3d_surface);
3273 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3274 src_surface = get_sub_mimaplevel(src_surface);
3278 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3279 dst_surface = get_sub_mimaplevel(dst_surface);
3283 if (!src_surface || !dst_surface)
3285 if (src_surface != dst_surface)
3286 ERR("Loading surface with different mipmap structure.\n");
3291 LeaveCriticalSection(&ddraw_cs);
3296 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
3298 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
3300 return d3d_texture2_Load((IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl,
3301 src_texture ? (IDirect3DTexture2 *)&surface_from_texture1(src_texture)->IDirect3DTexture2_vtbl : NULL);
3304 /*****************************************************************************
3306 *****************************************************************************/
3308 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
3311 ddraw_surface7_QueryInterface,
3312 ddraw_surface7_AddRef,
3313 ddraw_surface7_Release,
3314 /* IDirectDrawSurface */
3315 ddraw_surface7_AddAttachedSurface,
3316 ddraw_surface7_AddOverlayDirtyRect,
3318 ddraw_surface7_BltBatch,
3319 ddraw_surface7_BltFast,
3320 ddraw_surface7_DeleteAttachedSurface,
3321 ddraw_surface7_EnumAttachedSurfaces,
3322 ddraw_surface7_EnumOverlayZOrders,
3323 ddraw_surface7_Flip,
3324 ddraw_surface7_GetAttachedSurface,
3325 ddraw_surface7_GetBltStatus,
3326 ddraw_surface7_GetCaps,
3327 ddraw_surface7_GetClipper,
3328 ddraw_surface7_GetColorKey,
3329 ddraw_surface7_GetDC,
3330 ddraw_surface7_GetFlipStatus,
3331 ddraw_surface7_GetOverlayPosition,
3332 ddraw_surface7_GetPalette,
3333 ddraw_surface7_GetPixelFormat,
3334 ddraw_surface7_GetSurfaceDesc,
3335 ddraw_surface7_Initialize,
3336 ddraw_surface7_IsLost,
3337 ddraw_surface7_Lock,
3338 ddraw_surface7_ReleaseDC,
3339 ddraw_surface7_Restore,
3340 ddraw_surface7_SetClipper,
3341 ddraw_surface7_SetColorKey,
3342 ddraw_surface7_SetOverlayPosition,
3343 ddraw_surface7_SetPalette,
3344 ddraw_surface7_Unlock,
3345 ddraw_surface7_UpdateOverlay,
3346 ddraw_surface7_UpdateOverlayDisplay,
3347 ddraw_surface7_UpdateOverlayZOrder,
3348 /* IDirectDrawSurface2 */
3349 ddraw_surface7_GetDDInterface,
3350 ddraw_surface7_PageLock,
3351 ddraw_surface7_PageUnlock,
3352 /* IDirectDrawSurface3 */
3353 ddraw_surface7_SetSurfaceDesc,
3354 /* IDirectDrawSurface4 */
3355 ddraw_surface7_SetPrivateData,
3356 ddraw_surface7_GetPrivateData,
3357 ddraw_surface7_FreePrivateData,
3358 ddraw_surface7_GetUniquenessValue,
3359 ddraw_surface7_ChangeUniquenessValue,
3360 /* IDirectDrawSurface7 */
3361 ddraw_surface7_SetPriority,
3362 ddraw_surface7_GetPriority,
3363 ddraw_surface7_SetLOD,
3364 ddraw_surface7_GetLOD,
3367 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
3370 ddraw_surface3_QueryInterface,
3371 ddraw_surface3_AddRef,
3372 ddraw_surface3_Release,
3373 /* IDirectDrawSurface */
3374 ddraw_surface3_AddAttachedSurface,
3375 ddraw_surface3_AddOverlayDirtyRect,
3377 ddraw_surface3_BltBatch,
3378 ddraw_surface3_BltFast,
3379 ddraw_surface3_DeleteAttachedSurface,
3380 ddraw_surface3_EnumAttachedSurfaces,
3381 ddraw_surface3_EnumOverlayZOrders,
3382 ddraw_surface3_Flip,
3383 ddraw_surface3_GetAttachedSurface,
3384 ddraw_surface3_GetBltStatus,
3385 ddraw_surface3_GetCaps,
3386 ddraw_surface3_GetClipper,
3387 ddraw_surface3_GetColorKey,
3388 ddraw_surface3_GetDC,
3389 ddraw_surface3_GetFlipStatus,
3390 ddraw_surface3_GetOverlayPosition,
3391 ddraw_surface3_GetPalette,
3392 ddraw_surface3_GetPixelFormat,
3393 ddraw_surface3_GetSurfaceDesc,
3394 ddraw_surface3_Initialize,
3395 ddraw_surface3_IsLost,
3396 ddraw_surface3_Lock,
3397 ddraw_surface3_ReleaseDC,
3398 ddraw_surface3_Restore,
3399 ddraw_surface3_SetClipper,
3400 ddraw_surface3_SetColorKey,
3401 ddraw_surface3_SetOverlayPosition,
3402 ddraw_surface3_SetPalette,
3403 ddraw_surface3_Unlock,
3404 ddraw_surface3_UpdateOverlay,
3405 ddraw_surface3_UpdateOverlayDisplay,
3406 ddraw_surface3_UpdateOverlayZOrder,
3407 /* IDirectDrawSurface2 */
3408 ddraw_surface3_GetDDInterface,
3409 ddraw_surface3_PageLock,
3410 ddraw_surface3_PageUnlock,
3411 /* IDirectDrawSurface3 */
3412 ddraw_surface3_SetSurfaceDesc,
3415 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
3417 ddraw_gamma_control_QueryInterface,
3418 ddraw_gamma_control_AddRef,
3419 ddraw_gamma_control_Release,
3420 ddraw_gamma_control_GetGammaRamp,
3421 ddraw_gamma_control_SetGammaRamp,
3424 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
3426 d3d_texture2_QueryInterface,
3427 d3d_texture2_AddRef,
3428 d3d_texture2_Release,
3429 d3d_texture2_GetHandle,
3430 d3d_texture2_PaletteChanged,
3434 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
3436 d3d_texture1_QueryInterface,
3437 d3d_texture1_AddRef,
3438 d3d_texture1_Release,
3439 d3d_texture1_Initialize,
3440 d3d_texture1_GetHandle,
3441 d3d_texture1_PaletteChanged,
3443 d3d_texture1_Unload,
3446 IDirectDrawSurfaceImpl *unsafe_impl_from_IDirectDrawSurface3(IDirectDrawSurface3 *iface)
3448 if (!iface) return NULL;
3449 assert(iface->lpVtbl == &ddraw_surface3_vtbl);
3450 return CONTAINING_RECORD(iface, IDirectDrawSurfaceImpl, IDirectDrawSurface3_iface);
3453 static void STDMETHODCALLTYPE ddraw_surface_wined3d_object_destroyed(void *parent)
3455 IDirectDrawSurfaceImpl *surface = parent;
3457 TRACE("surface %p.\n", surface);
3459 /* Check for attached surfaces and detach them. */
3460 if (surface->first_attached != surface)
3462 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)surface->first_attached;
3463 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)surface;
3465 /* Well, this shouldn't happen: The surface being attached is
3466 * referenced in AddAttachedSurface(), so it shouldn't be released
3467 * until DeleteAttachedSurface() is called, because the refcount is
3468 * held. It looks like the application released it often enough to
3470 WARN("Surface is still attached to surface %p.\n", surface->first_attached);
3472 /* The refcount will drop to -1 here */
3473 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
3474 ERR("DeleteAttachedSurface failed.\n");
3477 while (surface->next_attached)
3479 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)surface;
3480 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)surface->next_attached;
3482 if (FAILED(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach)))
3483 ERR("DeleteAttachedSurface failed.\n");
3486 /* Having a texture handle set implies that the device still exists. */
3487 if (surface->Handle)
3488 ddraw_free_handle(&surface->ddraw->d3ddevice->handle_table, surface->Handle - 1, DDRAW_HANDLE_SURFACE);
3490 /* Reduce the ddraw surface count. */
3491 InterlockedDecrement(&surface->ddraw->surfaces);
3492 list_remove(&surface->surface_list_entry);
3494 HeapFree(GetProcessHeap(), 0, surface);
3497 const struct wined3d_parent_ops ddraw_surface_wined3d_parent_ops =
3499 ddraw_surface_wined3d_object_destroyed,
3502 static void STDMETHODCALLTYPE ddraw_texture_wined3d_object_destroyed(void *parent)
3504 IDirectDrawSurfaceImpl *surface = parent;
3506 TRACE("surface %p.\n", surface);
3508 ddraw_surface_cleanup(surface);
3511 static const struct wined3d_parent_ops ddraw_texture_wined3d_parent_ops =
3513 ddraw_texture_wined3d_object_destroyed,
3516 HRESULT ddraw_surface_create_texture(IDirectDrawSurfaceImpl *surface)
3518 const DDSURFACEDESC2 *desc = &surface->surface_desc;
3519 enum wined3d_format_id format;
3523 if (desc->ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3524 levels = desc->u2.dwMipMapCount;
3528 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM.
3529 * Should I forward the MANAGED cap to the managed pool? */
3530 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
3531 pool = WINED3DPOOL_SYSTEMMEM;
3533 pool = WINED3DPOOL_DEFAULT;
3535 format = PixelFormat_DD2WineD3D(&surface->surface_desc.u4.ddpfPixelFormat);
3536 if (desc->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
3537 return wined3d_texture_create_cube(surface->ddraw->wined3d_device, desc->dwWidth,
3538 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
3540 return wined3d_texture_create_2d(surface->ddraw->wined3d_device, desc->dwWidth, desc->dwHeight,
3541 levels, 0, format, pool, surface, &ddraw_texture_wined3d_parent_ops, &surface->wined3d_texture);
3544 HRESULT ddraw_surface_init(IDirectDrawSurfaceImpl *surface, IDirectDrawImpl *ddraw,
3545 DDSURFACEDESC2 *desc, UINT mip_level, WINED3DSURFTYPE surface_type)
3547 struct wined3d_resource_desc wined3d_desc;
3548 struct wined3d_resource *wined3d_resource;
3549 WINED3DPOOL pool = WINED3DPOOL_DEFAULT;
3550 enum wined3d_format_id format;
3554 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
3555 && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3556 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
3558 /* Tests show surfaces without memory flags get these flags added
3559 * right after creation. */
3560 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
3563 if (desc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3565 usage |= WINED3DUSAGE_RENDERTARGET;
3566 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
3569 if ((desc->ddsCaps.dwCaps & DDSCAPS_3DDEVICE) && !(desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
3571 usage |= WINED3DUSAGE_RENDERTARGET;
3574 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
3576 usage |= WINED3DUSAGE_OVERLAY;
3579 if (ddraw->depthstencil || (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
3581 /* The depth stencil creation callback sets this flag. Set the
3582 * wined3d usage to let it know it's a depth/stencil surface. */
3583 usage |= WINED3DUSAGE_DEPTHSTENCIL;
3586 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
3588 pool = WINED3DPOOL_SYSTEMMEM;
3590 else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
3592 pool = WINED3DPOOL_MANAGED;
3593 /* Managed textures have the system memory flag set. */
3594 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
3596 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
3598 /* Videomemory adds localvidmem. This is mutually exclusive with
3599 * systemmemory and texturemanage. */
3600 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
3603 format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
3604 if (format == WINED3DFMT_UNKNOWN)
3606 WARN("Unsupported / unknown pixelformat.\n");
3607 return DDERR_INVALIDPIXELFORMAT;
3610 surface->lpVtbl = &ddraw_surface7_vtbl;
3611 surface->IDirectDrawSurface3_iface.lpVtbl = &ddraw_surface3_vtbl;
3612 surface->IDirectDrawGammaControl_vtbl = &ddraw_gamma_control_vtbl;
3613 surface->IDirect3DTexture2_vtbl = &d3d_texture2_vtbl;
3614 surface->IDirect3DTexture_vtbl = &d3d_texture1_vtbl;
3616 surface->version = 7;
3617 surface->ddraw = ddraw;
3619 copy_to_surfacedesc2(&surface->surface_desc, desc);
3621 surface->first_attached = surface;
3622 surface->ImplType = surface_type;
3624 hr = wined3d_surface_create(ddraw->wined3d_device, desc->dwWidth, desc->dwHeight, format,
3625 TRUE /* Lockable */, FALSE /* Discard */, mip_level, usage, pool,
3626 WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, surface_type, surface,
3627 &ddraw_surface_wined3d_parent_ops, &surface->wined3d_surface);
3630 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
3634 surface->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
3635 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
3636 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
3638 format = wined3d_desc.format;
3639 if (format == WINED3DFMT_UNKNOWN)
3641 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN.\n");
3643 PixelFormat_WineD3DtoDD(&surface->surface_desc.u4.ddpfPixelFormat, format);
3645 /* Anno 1602 stores the pitch right after surface creation, so make sure
3646 * it's there. TODO: Test other fourcc formats. */
3647 if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
3648 || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
3650 surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
3651 if (format == WINED3DFMT_DXT1)
3653 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height) / 2;
3657 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height);
3662 surface->surface_desc.dwFlags |= DDSD_PITCH;
3663 surface->surface_desc.u1.lPitch = wined3d_surface_get_pitch(surface->wined3d_surface);
3666 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
3668 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTOVERLAY,
3669 (WINEDDCOLORKEY *)&desc->u3.ddckCKDestOverlay);
3671 if (desc->dwFlags & DDSD_CKDESTBLT)
3673 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_DESTBLT,
3674 (WINEDDCOLORKEY *)&desc->ddckCKDestBlt);
3676 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
3678 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCOVERLAY,
3679 (WINEDDCOLORKEY *)&desc->ddckCKSrcOverlay);
3681 if (desc->dwFlags & DDSD_CKSRCBLT)
3683 wined3d_surface_set_color_key(surface->wined3d_surface, DDCKEY_SRCBLT,
3684 (WINEDDCOLORKEY *)&desc->ddckCKSrcBlt);
3686 if (desc->dwFlags & DDSD_LPSURFACE)
3688 hr = wined3d_surface_set_mem(surface->wined3d_surface, desc->lpSurface);
3691 ERR("Failed to set surface memory, hr %#x.\n", hr);
3692 wined3d_surface_decref(surface->wined3d_surface);