1 /* DirectDraw Surface Implementation
3 * Copyright (c) 1997-2000 Marcus Meissner
4 * Copyright (c) 1998-2000 Lionel Ulmer
5 * Copyright (c) 2000-2001 TransGaming Technologies Inc.
6 * Copyright (c) 2006 Stefan Dösinger
8 * This file contains the (internal) driver registration functions,
9 * driver enumeration APIs and DirectDraw creation functions.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/port.h"
29 #include "ddraw_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
33 static inline IDirectDrawSurfaceImpl *surface_from_gamma_control(IDirectDrawGammaControl *iface)
35 return (IDirectDrawSurfaceImpl *)((char*)iface
36 - FIELD_OFFSET(IDirectDrawSurfaceImpl, IDirectDrawGammaControl_vtbl));
39 /*****************************************************************************
40 * IUnknown parts follow
41 *****************************************************************************/
43 /*****************************************************************************
44 * IDirectDrawSurface7::QueryInterface
46 * A normal QueryInterface implementation. For QueryInterface rules
47 * see ddraw.c, IDirectDraw7::QueryInterface. This method
48 * can Query IDirectDrawSurface interfaces in all version, IDirect3DTexture
49 * in all versions, the IDirectDrawGammaControl interface and it can
50 * create an IDirect3DDevice. (Uses IDirect3D7::CreateDevice)
53 * riid: The interface id queried for
54 * obj: Address to write the pointer to
58 * E_NOINTERFACE if the requested interface wasn't found
60 *****************************************************************************/
61 static HRESULT WINAPI ddraw_surface7_QueryInterface(IDirectDrawSurface7 *iface, REFIID riid, void **obj)
63 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
65 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
67 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
71 return DDERR_INVALIDPARAMS;
73 if (IsEqualGUID(riid, &IID_IUnknown)
74 || IsEqualGUID(riid, &IID_IDirectDrawSurface7)
75 || IsEqualGUID(riid, &IID_IDirectDrawSurface4) )
77 IUnknown_AddRef(iface);
79 TRACE("(%p) returning IDirectDrawSurface7 interface at %p\n", This, *obj);
82 else if( IsEqualGUID(riid, &IID_IDirectDrawSurface3)
83 || IsEqualGUID(riid, &IID_IDirectDrawSurface2)
84 || IsEqualGUID(riid, &IID_IDirectDrawSurface) )
86 IUnknown_AddRef(iface);
87 *obj = &This->IDirectDrawSurface3_vtbl;
88 TRACE("(%p) returning IDirectDrawSurface3 interface at %p\n", This, *obj);
91 else if( IsEqualGUID(riid, &IID_IDirectDrawGammaControl) )
93 IUnknown_AddRef(iface);
94 *obj = &This->IDirectDrawGammaControl_vtbl;
95 TRACE("(%p) returning IDirectDrawGammaControl interface at %p\n", This, *obj);
98 else if( IsEqualGUID(riid, &IID_D3DDEVICE_WineD3D) ||
99 IsEqualGUID(riid, &IID_IDirect3DHALDevice)||
100 IsEqualGUID(riid, &IID_IDirect3DRGBDevice) )
102 IDirect3DDevice7 *d3d;
104 /* Call into IDirect3D7 for creation */
105 IDirect3D7_CreateDevice((IDirect3D7 *)&This->ddraw->IDirect3D7_vtbl, riid, (IDirectDrawSurface7 *)This, &d3d);
109 *obj = (IDirect3DDevice *)&((IDirect3DDeviceImpl *)d3d)->IDirect3DDevice_vtbl;
110 TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
114 WARN("Unable to create a IDirect3DDevice instance, returning E_NOINTERFACE\n");
115 return E_NOINTERFACE;
117 else if (IsEqualGUID( &IID_IDirect3DTexture, riid ) ||
118 IsEqualGUID( &IID_IDirect3DTexture2, riid ))
120 if (IsEqualGUID( &IID_IDirect3DTexture, riid ))
122 *obj = &This->IDirect3DTexture_vtbl;
123 TRACE(" returning Direct3DTexture interface at %p.\n", *obj);
127 *obj = &This->IDirect3DTexture2_vtbl;
128 TRACE(" returning Direct3DTexture2 interface at %p.\n", *obj);
130 IUnknown_AddRef( (IUnknown *) *obj);
134 ERR("No interface\n");
135 return E_NOINTERFACE;
138 static HRESULT WINAPI ddraw_surface3_QueryInterface(IDirectDrawSurface3 *iface, REFIID riid, void **object)
140 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
142 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), riid, object);
145 static HRESULT WINAPI ddraw_gamma_control_QueryInterface(IDirectDrawGammaControl *iface, REFIID riid, void **object)
147 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
149 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_gamma_control(iface), riid, object);
152 static HRESULT WINAPI d3d_texture2_QueryInterface(IDirect3DTexture2 *iface, REFIID riid, void **object)
154 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
156 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_texture2(iface), riid, object);
159 static HRESULT WINAPI d3d_texture1_QueryInterface(IDirect3DTexture *iface, REFIID riid, void **object)
161 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
163 return ddraw_surface7_QueryInterface((IDirectDrawSurface7 *)surface_from_texture1(iface), riid, object);
166 /*****************************************************************************
167 * IDirectDrawSurface7::AddRef
169 * A normal addref implementation
174 *****************************************************************************/
175 static ULONG WINAPI ddraw_surface7_AddRef(IDirectDrawSurface7 *iface)
177 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
178 ULONG refCount = InterlockedIncrement(&This->ref);
180 TRACE("%p increasing refcount to %u.\n", This, refCount);
182 if (refCount == 1 && This->WineD3DSurface)
184 EnterCriticalSection(&ddraw_cs);
185 IWineD3DSurface_AddRef(This->WineD3DSurface);
186 LeaveCriticalSection(&ddraw_cs);
192 static ULONG WINAPI ddraw_surface3_AddRef(IDirectDrawSurface3 *iface)
194 TRACE("iface %p.\n", iface);
196 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_surface3(iface));
199 static ULONG WINAPI ddraw_gamma_control_AddRef(IDirectDrawGammaControl *iface)
201 TRACE("iface %p.\n", iface);
203 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_gamma_control(iface));
206 static ULONG WINAPI d3d_texture2_AddRef(IDirect3DTexture2 *iface)
208 TRACE("iface %p.\n", iface);
210 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_texture2(iface));
213 static ULONG WINAPI d3d_texture1_AddRef(IDirect3DTexture *iface)
215 TRACE("iface %p.\n", iface);
217 return ddraw_surface7_AddRef((IDirectDrawSurface7 *)surface_from_texture1(iface));
220 /*****************************************************************************
221 * ddraw_surface_destroy
223 * A helper function for IDirectDrawSurface7::Release
225 * Frees the surface, regardless of its refcount.
226 * See IDirectDrawSurface7::Release for more information
229 * This: Surface to free
231 *****************************************************************************/
232 void ddraw_surface_destroy(IDirectDrawSurfaceImpl *This)
234 TRACE("surface %p.\n", This);
236 /* Check the refcount and give a warning */
239 /* This can happen when a complex surface is destroyed,
240 * because the 2nd surface was addref()ed when the app
241 * called GetAttachedSurface
243 WARN("(%p): Destroying surface with refount %d\n", This, This->ref);
246 /* Check for attached surfaces and detach them */
247 if(This->first_attached != This)
249 /* Well, this shouldn't happen: The surface being attached is addref()ed
250 * in AddAttachedSurface, so it shouldn't be released until DeleteAttachedSurface
251 * is called, because the refcount is held. It looks like the app released()
252 * it often enough to force this
254 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)This->first_attached;
255 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)This;
257 FIXME("(%p) Freeing a surface that is attached to surface %p\n", This, This->first_attached);
259 /* The refcount will drop to -1 here */
260 if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
262 ERR("(%p) DeleteAttachedSurface failed!\n", This);
266 while(This->next_attached != NULL)
268 IDirectDrawSurface7 *root = (IDirectDrawSurface7 *)This;
269 IDirectDrawSurface7 *detach = (IDirectDrawSurface7 *)This->next_attached;
271 if(IDirectDrawSurface7_DeleteAttachedSurface(root, 0, detach) != DD_OK)
273 ERR("(%p) DeleteAttachedSurface failed!\n", This);
278 /* Now destroy the surface. Wait: It could have been released if we are a texture */
279 if(This->WineD3DSurface)
280 IWineD3DSurface_Release(This->WineD3DSurface);
282 /* Having a texture handle set implies that the device still exists */
285 ddraw_free_handle(&This->ddraw->d3ddevice->handle_table, This->Handle - 1, DDRAW_HANDLE_SURFACE);
288 /* Reduce the ddraw surface count */
289 InterlockedDecrement(&This->ddraw->surfaces);
290 list_remove(&This->surface_list_entry);
292 HeapFree(GetProcessHeap(), 0, This);
295 /*****************************************************************************
296 * IDirectDrawSurface7::Release
298 * Reduces the surface's refcount by 1. If the refcount falls to 0, the
299 * surface is destroyed.
301 * Destroying the surface is a bit tricky. For the connection between
302 * WineD3DSurfaces and DirectDrawSurfaces see IDirectDraw7::CreateSurface
303 * It has a nice graph explaining the connection.
305 * What happens here is basically this:
306 * When a surface is destroyed, its WineD3DSurface is released,
307 * and the refcount of the DirectDraw interface is reduced by 1. If it has
308 * complex surfaces attached to it, then these surfaces are destroyed too,
309 * regardless of their refcount. If any surface being destroyed has another
310 * surface attached to it (with a "soft" attachment, not complex), then
311 * this surface is detached with DeleteAttachedSurface.
313 * When the surface is a texture, the WineD3DTexture is released.
314 * If the surface is the Direct3D render target, then the D3D
315 * capabilities of the WineD3DDevice are uninitialized, which causes the
316 * swapchain to be released.
318 * When a complex sublevel falls to ref zero, then this is ignored.
323 *****************************************************************************/
324 static ULONG WINAPI ddraw_surface7_Release(IDirectDrawSurface7 *iface)
326 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
327 ULONG ref = InterlockedDecrement(&This->ref);
329 TRACE("%p decreasing refcount to %u.\n", This, ref);
334 IDirectDrawSurfaceImpl *surf;
335 IDirectDrawImpl *ddraw;
336 IUnknown *ifaceToRelease = This->ifaceToRelease;
339 /* Complex attached surfaces are destroyed implicitly when the root is released */
340 EnterCriticalSection(&ddraw_cs);
341 if(!This->is_complex_root)
343 WARN("(%p) Attempt to destroy a surface that is not a complex root\n", This);
344 LeaveCriticalSection(&ddraw_cs);
349 /* If it's a texture, destroy the WineD3DTexture.
350 * WineD3D will destroy the IParent interfaces
351 * of the sublevels, which destroys the WineD3DSurfaces.
352 * Set the surfaces to NULL to avoid destroying them again later
354 if(This->wineD3DTexture)
356 IWineD3DBaseTexture_Release(This->wineD3DTexture);
358 /* If it's the RenderTarget, destroy the d3ddevice */
359 else if(This->wineD3DSwapChain)
361 if((ddraw->d3d_initialized) && (This == ddraw->d3d_target)) {
362 TRACE("(%p) Destroying the render target, uninitializing D3D\n", This);
364 /* Unset any index buffer, just to be sure */
365 IWineD3DDevice_SetIndexBuffer(ddraw->wineD3DDevice, NULL, WINED3DFMT_UNKNOWN);
366 IWineD3DDevice_SetDepthStencilSurface(ddraw->wineD3DDevice, NULL);
367 IWineD3DDevice_SetVertexDeclaration(ddraw->wineD3DDevice, NULL);
368 for(i = 0; i < ddraw->numConvertedDecls; i++)
370 IWineD3DVertexDeclaration_Release(ddraw->decls[i].decl);
372 HeapFree(GetProcessHeap(), 0, ddraw->decls);
373 ddraw->numConvertedDecls = 0;
375 if (FAILED(IWineD3DDevice_Uninit3D(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain)))
378 ERR("(%p) Failed to uninit 3D\n", This);
382 /* Free the d3d window if one was created */
383 if(ddraw->d3d_window != 0 && ddraw->d3d_window != ddraw->dest_window)
385 TRACE(" (%p) Destroying the hidden render window %p\n", This, ddraw->d3d_window);
386 DestroyWindow(ddraw->d3d_window);
387 ddraw->d3d_window = 0;
389 /* Unset the pointers */
392 This->wineD3DSwapChain = NULL; /* Uninit3D releases the swapchain */
393 ddraw->d3d_initialized = FALSE;
394 ddraw->d3d_target = NULL;
396 IWineD3DDevice_UninitGDI(ddraw->wineD3DDevice, D3D7CB_DestroySwapChain);
397 This->wineD3DSwapChain = NULL;
400 /* Reset to the default surface implementation type. This is needed if apps use
401 * non render target surfaces and expect blits to work after destroying the render
404 * TODO: Recreate existing offscreen surfaces
406 ddraw->ImplType = DefaultSurfaceType;
408 /* Write a trace because D3D unloading was the reason for many
409 * crashes during development.
411 TRACE("(%p) D3D unloaded\n", This);
414 /* The refcount test shows that the palette is detached when the surface is destroyed */
415 IDirectDrawSurface7_SetPalette((IDirectDrawSurface7 *)This, NULL);
417 /* Loop through all complex attached surfaces,
420 * Yet again, only the root can have more than one complexly attached surface, all the others
421 * have a total of one;
423 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
425 if(!This->complex_array[i]) break;
427 surf = This->complex_array[i];
428 This->complex_array[i] = NULL;
431 IDirectDrawSurfaceImpl *destroy = surf;
432 surf = surf->complex_array[0]; /* Iterate through the "tree" */
433 ddraw_surface_destroy(destroy); /* Destroy it */
437 /* Destroy the root surface. */
438 ddraw_surface_destroy(This);
440 /* Reduce the ddraw refcount */
441 if(ifaceToRelease) IUnknown_Release(ifaceToRelease);
442 LeaveCriticalSection(&ddraw_cs);
448 static ULONG WINAPI ddraw_surface3_Release(IDirectDrawSurface3 *iface)
450 TRACE("iface %p.\n", iface);
452 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_surface3(iface));
455 static ULONG WINAPI ddraw_gamma_control_Release(IDirectDrawGammaControl *iface)
457 TRACE("iface %p.\n", iface);
459 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_gamma_control(iface));
462 static ULONG WINAPI d3d_texture2_Release(IDirect3DTexture2 *iface)
464 TRACE("iface %p.\n", iface);
466 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_texture2(iface));
469 static ULONG WINAPI d3d_texture1_Release(IDirect3DTexture *iface)
471 TRACE("iface %p.\n", iface);
473 return ddraw_surface7_Release((IDirectDrawSurface7 *)surface_from_texture1(iface));
476 /*****************************************************************************
477 * IDirectDrawSurface7::GetAttachedSurface
479 * Returns an attached surface with the requested caps. Surface attachment
480 * and complex surfaces are not clearly described by the MSDN or sdk,
481 * so this method is tricky and likely to contain problems.
482 * This implementation searches the complex list first, then the
485 * The chains are searched from This down to the last surface in the chain,
486 * not from the first element in the chain. The first surface found is
487 * returned. The MSDN says that this method fails if more than one surface
488 * matches the caps, but it is not sure if that is right. The attachment
489 * structure may not even allow two matching surfaces.
491 * The found surface is AddRef-ed before it is returned.
494 * Caps: Pointer to a DDCAPS2 structure describing the caps asked for
495 * Surface: Address to store the found surface
499 * DDERR_INVALIDPARAMS if Caps or Surface is NULL
500 * DDERR_NOTFOUND if no surface was found
502 *****************************************************************************/
503 static HRESULT WINAPI ddraw_surface7_GetAttachedSurface(IDirectDrawSurface7 *iface,
504 DDSCAPS2 *Caps, IDirectDrawSurface7 **Surface)
506 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
507 IDirectDrawSurfaceImpl *surf;
511 TRACE("iface %p, caps %p, attachment %p.\n", iface, Caps, Surface);
513 EnterCriticalSection(&ddraw_cs);
515 if(This->version < 7)
517 /* Earlier dx apps put garbage into these members, clear them */
518 our_caps.dwCaps = Caps->dwCaps;
519 our_caps.dwCaps2 = 0;
520 our_caps.dwCaps3 = 0;
521 our_caps.dwCaps4 = 0;
528 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 */
530 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
532 surf = This->complex_array[i];
537 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
538 surf->surface_desc.ddsCaps.dwCaps,
539 surf->surface_desc.ddsCaps.dwCaps2,
540 surf->surface_desc.ddsCaps.dwCaps3,
541 surf->surface_desc.ddsCaps.dwCaps4);
544 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
545 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
547 /* MSDN: "This method fails if more than one surface is attached
548 * that matches the capabilities requested."
550 * Not sure how to test this.
553 TRACE("(%p): Returning surface %p\n", This, surf);
554 TRACE("(%p): mipmapcount=%d\n", This, surf->mipmap_level);
555 *Surface = (IDirectDrawSurface7 *)surf;
556 ddraw_surface7_AddRef(*Surface);
557 LeaveCriticalSection(&ddraw_cs);
562 /* Next, look at the attachment chain */
565 while( (surf = surf->next_attached) )
569 TRACE("Surface: (%p) caps: %x,%x,%x,%x\n", surf,
570 surf->surface_desc.ddsCaps.dwCaps,
571 surf->surface_desc.ddsCaps.dwCaps2,
572 surf->surface_desc.ddsCaps.dwCaps3,
573 surf->surface_desc.ddsCaps.dwCaps4);
576 if (((surf->surface_desc.ddsCaps.dwCaps & our_caps.dwCaps) == our_caps.dwCaps) &&
577 ((surf->surface_desc.ddsCaps.dwCaps2 & our_caps.dwCaps2) == our_caps.dwCaps2)) {
579 TRACE("(%p): Returning surface %p\n", This, surf);
580 *Surface = (IDirectDrawSurface7 *)surf;
581 ddraw_surface7_AddRef(*Surface);
582 LeaveCriticalSection(&ddraw_cs);
587 TRACE("(%p) Didn't find a valid surface\n", This);
588 LeaveCriticalSection(&ddraw_cs);
591 return DDERR_NOTFOUND;
594 static HRESULT WINAPI ddraw_surface3_GetAttachedSurface(IDirectDrawSurface3 *iface,
595 DDSCAPS *caps, IDirectDrawSurface3 **attachment)
597 IDirectDrawSurface7 *attachment7;
601 TRACE("iface %p, caps %p, attachment %p.\n", iface, caps, attachment);
603 caps2.dwCaps = caps->dwCaps;
608 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface),
609 &caps2, &attachment7);
610 if (FAILED(hr)) *attachment = NULL;
611 else *attachment = attachment7 ?
612 (IDirectDrawSurface3 *)&((IDirectDrawSurfaceImpl *)attachment7)->IDirectDrawSurface3_vtbl : NULL;
617 /*****************************************************************************
618 * IDirectDrawSurface7::Lock
620 * Locks the surface and returns a pointer to the surface's memory
623 * Rect: Rectangle to lock. If NULL, the whole surface is locked
624 * DDSD: Pointer to a DDSURFACEDESC2 which shall receive the surface's desc.
625 * Flags: Locking flags, e.g Read only or write only
626 * h: An event handle that's not used and must be NULL
630 * DDERR_INVALIDPARAMS if DDSD is NULL
631 * For more details, see IWineD3DSurface::LockRect
633 *****************************************************************************/
634 static HRESULT WINAPI ddraw_surface7_Lock(IDirectDrawSurface7 *iface,
635 RECT *Rect, DDSURFACEDESC2 *DDSD, DWORD Flags, HANDLE h)
637 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
638 WINED3DLOCKED_RECT LockedRect;
641 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
642 iface, wine_dbgstr_rect(Rect), DDSD, Flags, h);
645 return DDERR_INVALIDPARAMS;
647 /* This->surface_desc.dwWidth and dwHeight are changeable, thus lock */
648 EnterCriticalSection(&ddraw_cs);
650 /* Should I check for the handle to be NULL?
652 * The DDLOCK flags and the D3DLOCK flags are equal
653 * for the supported values. The others are ignored by WineD3D
656 if(DDSD->dwSize != sizeof(DDSURFACEDESC) &&
657 DDSD->dwSize != sizeof(DDSURFACEDESC2))
659 WARN("Invalid structure size %d, returning DDERR_INVALIDPARAMS\n", DDERR_INVALIDPARAMS);
660 LeaveCriticalSection(&ddraw_cs);
661 return DDERR_INVALIDPARAMS;
664 /* Windows zeroes this if the rect is invalid */
671 || (Rect->left > Rect->right)
672 || (Rect->top > Rect->bottom)
673 || (Rect->right > This->surface_desc.dwWidth)
674 || (Rect->bottom > This->surface_desc.dwHeight))
676 WARN("Trying to lock an invalid rectangle, returning DDERR_INVALIDPARAMS\n");
677 LeaveCriticalSection(&ddraw_cs);
678 return DDERR_INVALIDPARAMS;
682 hr = IWineD3DSurface_Map(This->WineD3DSurface, &LockedRect, Rect, Flags);
685 LeaveCriticalSection(&ddraw_cs);
688 /* D3D8 and D3D9 return the general D3DERR_INVALIDCALL error, but ddraw has a more
689 * specific error. But since IWineD3DSurface::LockRect returns that error in this
690 * only occasion, keep d3d8 and d3d9 free from the return value override. There are
691 * many different places where d3d8/9 would have to catch the DDERR_SURFACEBUSY, it
692 * is much easier to do it in one place in ddraw
694 case WINED3DERR_INVALIDCALL: return DDERR_SURFACEBUSY;
699 /* Override the memory area. The pitch should be set already. Strangely windows
700 * does not set the LPSURFACE flag on locked surfaces !?!.
701 * DDSD->dwFlags |= DDSD_LPSURFACE;
703 This->surface_desc.lpSurface = LockedRect.pBits;
704 DD_STRUCT_COPY_BYSIZE(DDSD,&(This->surface_desc));
706 TRACE("locked surface returning description :\n");
707 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
709 LeaveCriticalSection(&ddraw_cs);
713 static HRESULT WINAPI ddraw_surface3_Lock(IDirectDrawSurface3 *iface, RECT *rect,
714 DDSURFACEDESC *surface_desc, DWORD flags, HANDLE h)
716 TRACE("iface %p, rect %s, surface_desc %p, flags %#x, h %p.\n",
717 iface, wine_dbgstr_rect(rect), surface_desc, flags, h);
719 return ddraw_surface7_Lock((IDirectDrawSurface7 *)surface_from_surface3(iface),
720 rect, (DDSURFACEDESC2 *)surface_desc, flags, h);
723 /*****************************************************************************
724 * IDirectDrawSurface7::Unlock
726 * Unlocks an locked surface
729 * Rect: Not used by this implementation
733 * For more details, see IWineD3DSurface::UnlockRect
735 *****************************************************************************/
736 static HRESULT WINAPI ddraw_surface7_Unlock(IDirectDrawSurface7 *iface, RECT *pRect)
738 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
741 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(pRect));
743 EnterCriticalSection(&ddraw_cs);
744 hr = IWineD3DSurface_Unmap(This->WineD3DSurface);
747 This->surface_desc.lpSurface = NULL;
749 LeaveCriticalSection(&ddraw_cs);
753 static HRESULT WINAPI ddraw_surface3_Unlock(IDirectDrawSurface3 *iface, void *data)
755 TRACE("iface %p, data %p.\n", iface, data);
757 /* data might not be the LPRECT of later versions, so drop it. */
758 return ddraw_surface7_Unlock((IDirectDrawSurface7 *)surface_from_surface3(iface), NULL);
761 /*****************************************************************************
762 * IDirectDrawSurface7::Flip
764 * Flips a surface with the DDSCAPS_FLIP flag. The flip is relayed to
765 * IWineD3DSurface::Flip. Because WineD3D doesn't handle attached surfaces,
766 * the flip target is passed to WineD3D, even if the app didn't specify one
769 * DestOverride: Specifies the surface that will become the new front
770 * buffer. If NULL, the current back buffer is used
771 * Flags: some DirectDraw flags, see include/ddraw.h
775 * DDERR_NOTFLIPPABLE if no flip target could be found
776 * DDERR_INVALIDOBJECT if the surface isn't a front buffer
777 * For more details, see IWineD3DSurface::Flip
779 *****************************************************************************/
780 static HRESULT WINAPI ddraw_surface7_Flip(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *DestOverride, DWORD Flags)
782 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
783 IDirectDrawSurfaceImpl *Override = (IDirectDrawSurfaceImpl *)DestOverride;
784 IDirectDrawSurface7 *Override7;
787 TRACE("iface %p, dst %p, flags %#x.\n", iface, DestOverride, Flags);
789 /* Flip has to be called from a front buffer
790 * What about overlay surfaces, AFAIK they can flip too?
792 if( !(This->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_OVERLAY)) )
793 return DDERR_INVALIDOBJECT; /* Unchecked */
795 EnterCriticalSection(&ddraw_cs);
797 /* WineD3D doesn't keep track of attached surface, so find the target */
802 memset(&Caps, 0, sizeof(Caps));
803 Caps.dwCaps |= DDSCAPS_BACKBUFFER;
804 hr = ddraw_surface7_GetAttachedSurface(iface, &Caps, &Override7);
807 ERR("Can't find a flip target\n");
808 LeaveCriticalSection(&ddraw_cs);
809 return DDERR_NOTFLIPPABLE; /* Unchecked */
811 Override = (IDirectDrawSurfaceImpl *)Override7;
813 /* For the GetAttachedSurface */
814 ddraw_surface7_Release(Override7);
817 hr = IWineD3DSurface_Flip(This->WineD3DSurface,
818 Override->WineD3DSurface,
820 LeaveCriticalSection(&ddraw_cs);
824 static HRESULT WINAPI ddraw_surface3_Flip(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *dst, DWORD flags)
826 TRACE("iface %p, dst %p, flags %#x.\n", iface, dst, flags);
828 return ddraw_surface7_Flip((IDirectDrawSurface7 *)surface_from_surface3(iface),
829 dst ? (IDirectDrawSurface7 *)surface_from_surface3(dst) : NULL, flags);
832 /*****************************************************************************
833 * IDirectDrawSurface7::Blt
835 * Performs a blit on the surface
838 * DestRect: Destination rectangle, can be NULL
839 * SrcSurface: Source surface, can be NULL
840 * SrcRect: Source rectangle, can be NULL
842 * DDBltFx: Some extended blt parameters, connected to the flags
846 * See IWineD3DSurface::Blt for more details
848 *****************************************************************************/
849 static HRESULT WINAPI ddraw_surface7_Blt(IDirectDrawSurface7 *iface, RECT *DestRect,
850 IDirectDrawSurface7 *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx)
852 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
853 IDirectDrawSurfaceImpl *Src = (IDirectDrawSurfaceImpl *)SrcSurface;
856 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %p, flags %#x, fx %p.\n",
857 iface, wine_dbgstr_rect(DestRect), SrcSurface, wine_dbgstr_rect(SrcRect), Flags, DDBltFx);
859 /* Check for validity of the flags here. WineD3D Has the software-opengl selection path and would have
860 * to check at 2 places, and sometimes do double checks. This also saves the call to wined3d :-)
862 if((Flags & DDBLT_KEYSRCOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYSRC)) {
863 WARN("Invalid source color key parameters, returning DDERR_INVALIDPARAMS\n");
864 return DDERR_INVALIDPARAMS;
867 if((Flags & DDBLT_KEYDESTOVERRIDE) && (!DDBltFx || Flags & DDBLT_KEYDEST)) {
868 WARN("Invalid destination color key parameters, returning DDERR_INVALIDPARAMS\n");
869 return DDERR_INVALIDPARAMS;
872 /* Sizes can change, therefore hold the lock when testing the rectangles */
873 EnterCriticalSection(&ddraw_cs);
876 if(DestRect->top >= DestRect->bottom || DestRect->left >= DestRect->right ||
877 DestRect->right > This->surface_desc.dwWidth ||
878 DestRect->bottom > This->surface_desc.dwHeight)
880 WARN("Destination rectangle is invalid, returning DDERR_INVALIDRECT\n");
881 LeaveCriticalSection(&ddraw_cs);
882 return DDERR_INVALIDRECT;
887 if(SrcRect->top >= SrcRect->bottom || SrcRect->left >=SrcRect->right ||
888 SrcRect->right > Src->surface_desc.dwWidth ||
889 SrcRect->bottom > Src->surface_desc.dwHeight)
891 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
892 LeaveCriticalSection(&ddraw_cs);
893 return DDERR_INVALIDRECT;
897 if(Flags & DDBLT_KEYSRC && (!Src || !(Src->surface_desc.dwFlags & DDSD_CKSRCBLT))) {
898 WARN("DDBLT_KEYDEST blit without color key in surface, returning DDERR_INVALIDPARAMS\n");
899 LeaveCriticalSection(&ddraw_cs);
900 return DDERR_INVALIDPARAMS;
903 /* TODO: Check if the DDBltFx contains any ddraw surface pointers. If it does, copy the struct,
904 * and replace the ddraw surfaces with the wined3d surfaces
905 * So far no blitting operations using surfaces in the bltfx struct are supported anyway.
907 hr = IWineD3DSurface_Blt(This->WineD3DSurface, DestRect, Src ? Src->WineD3DSurface : NULL,
908 SrcRect, Flags, (WINEDDBLTFX *)DDBltFx, WINED3DTEXF_LINEAR);
910 LeaveCriticalSection(&ddraw_cs);
913 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
914 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
919 static HRESULT WINAPI ddraw_surface3_Blt(IDirectDrawSurface3 *iface, RECT *dst_rect,
920 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags, DDBLTFX *fx)
922 TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %p, flags %#x, fx %p.\n",
923 iface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect), flags, fx);
925 return ddraw_surface7_Blt((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_rect,
926 src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags, fx);
929 /*****************************************************************************
930 * IDirectDrawSurface7::AddAttachedSurface
932 * Attaches a surface to another surface. How the surface attachments work
933 * is not totally understood yet, and this method is prone to problems.
934 * he surface that is attached is AddRef-ed.
936 * Tests with complex surfaces suggest that the surface attachments form a
937 * tree, but no method to test this has been found yet.
939 * The attachment list consists of a first surface (first_attached) and
940 * for each surface a pointer to the next attached surface (next_attached).
941 * For the first surface, and a surface that has no attachments
942 * first_attached points to the surface itself. A surface that has
943 * no successors in the chain has next_attached set to NULL.
945 * Newly attached surfaces are attached right after the root surface.
946 * If a surface is attached to a complex surface compound, it's attached to
947 * the surface that the app requested, not the complex root. See
948 * GetAttachedSurface for a description how surfaces are found.
950 * This is how the current implementation works, and it was coded by looking
951 * at the needs of the applications.
953 * So far only Z-Buffer attachments are tested, and they are activated in
954 * WineD3D. Mipmaps could be tricky to activate in WineD3D.
955 * Back buffers should work in 2D mode, but they are not tested(They can be
956 * attached in older iface versions). Rendering to the front buffer and
957 * switching between that and double buffering is not yet implemented in
958 * WineD3D, so for 3D it might have unexpected results.
960 * ddraw_surface_attach_surface is the real thing,
961 * ddraw_surface7_AddAttachedSurface is a wrapper around it that
962 * performs additional checks. Version 7 of this interface is much more restrictive
963 * than its predecessors.
966 * Attach: Surface to attach to iface
970 * DDERR_CANNOTATTACHSURFACE if the surface can't be attached for some reason
972 *****************************************************************************/
973 static HRESULT ddraw_surface_attach_surface(IDirectDrawSurfaceImpl *This, IDirectDrawSurfaceImpl *Surf)
975 TRACE("surface %p, attachment %p.\n", This, Surf);
978 return DDERR_CANNOTATTACHSURFACE; /* unchecked */
980 EnterCriticalSection(&ddraw_cs);
982 /* Check if the surface is already attached somewhere */
983 if (Surf->next_attached || Surf->first_attached != Surf)
985 /* TODO: Test for the structure of the manual attachment. Is it a
986 * chain or a list? What happens if one surface is attached to 2
987 * different surfaces? */
988 WARN("Surface %p is already attached somewhere. next_attached %p, first_attached %p.\n",
989 Surf, Surf->next_attached, Surf->first_attached);
991 LeaveCriticalSection(&ddraw_cs);
992 return DDERR_SURFACEALREADYATTACHED;
995 /* This inserts the new surface at the 2nd position in the chain, right after the root surface */
996 Surf->next_attached = This->next_attached;
997 Surf->first_attached = This->first_attached;
998 This->next_attached = Surf;
1000 /* Check if the WineD3D depth stencil needs updating */
1001 if(This->ddraw->d3ddevice)
1003 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1006 ddraw_surface7_AddRef((IDirectDrawSurface7 *)Surf);
1007 LeaveCriticalSection(&ddraw_cs);
1011 static HRESULT WINAPI ddraw_surface7_AddAttachedSurface(IDirectDrawSurface7 *iface, IDirectDrawSurface7 *Attach)
1013 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1014 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
1016 TRACE("iface %p, attachment %p.\n", iface, Attach);
1018 /* Version 7 of this interface seems to refuse everything except z buffers, as per msdn */
1019 if(!(Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
1022 WARN("Application tries to attach a non Z buffer surface. caps %08x\n",
1023 Surf->surface_desc.ddsCaps.dwCaps);
1024 return DDERR_CANNOTATTACHSURFACE;
1027 return ddraw_surface_attach_surface(This, Surf);
1030 static HRESULT WINAPI ddraw_surface3_AddAttachedSurface(IDirectDrawSurface3 *iface, IDirectDrawSurface3 *attachment)
1032 IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
1033 IDirectDrawSurfaceImpl *attach_impl = surface_from_surface3(attachment);
1035 TRACE("iface %p, attachment %p.\n", iface, attachment);
1037 /* Tests suggest that
1038 * -> offscreen plain surfaces can be attached to other offscreen plain surfaces
1039 * -> offscreen plain surfaces can be attached to primaries
1040 * -> primaries can be attached to offscreen plain surfaces
1041 * -> z buffers can be attached to primaries */
1042 if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN)
1043 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_OFFSCREENPLAIN))
1045 /* Sizes have to match */
1046 if (attach_impl->surface_desc.dwWidth != surface->surface_desc.dwWidth
1047 || attach_impl->surface_desc.dwHeight != surface->surface_desc.dwHeight)
1049 WARN("Surface sizes do not match.\n");
1050 return DDERR_CANNOTATTACHSURFACE;
1054 else if (surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)
1055 && attach_impl->surface_desc.ddsCaps.dwCaps & (DDSCAPS_ZBUFFER))
1061 WARN("Invalid attachment combination.\n");
1062 return DDERR_CANNOTATTACHSURFACE;
1065 return ddraw_surface_attach_surface(surface, attach_impl);
1068 /*****************************************************************************
1069 * IDirectDrawSurface7::DeleteAttachedSurface
1071 * Removes a surface from the attachment chain. The surface's refcount
1072 * is decreased by one after it has been removed
1075 * Flags: Some flags, not used by this implementation
1076 * Attach: Surface to detach
1080 * DDERR_SURFACENOTATTACHED if the surface isn't attached to
1082 *****************************************************************************/
1083 static HRESULT WINAPI ddraw_surface7_DeleteAttachedSurface(IDirectDrawSurface7 *iface,
1084 DWORD Flags, IDirectDrawSurface7 *Attach)
1086 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1087 IDirectDrawSurfaceImpl *Surf = (IDirectDrawSurfaceImpl *)Attach;
1088 IDirectDrawSurfaceImpl *Prev = This;
1090 TRACE("iface %p, flags %#x, attachment %p.\n", iface, Flags, Attach);
1092 EnterCriticalSection(&ddraw_cs);
1093 if (!Surf || (Surf->first_attached != This) || (Surf == This) )
1095 LeaveCriticalSection(&ddraw_cs);
1096 return DDERR_CANNOTDETACHSURFACE;
1099 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
1100 if (This->surface_desc.ddsCaps.dwCaps &
1101 Surf->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
1103 Surf->surface_desc.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
1104 /* FIXME: we should probably also subtract from dwMipMapCount of this
1105 * and all parent surfaces */
1108 /* Find the predecessor of the detached surface */
1111 if(Prev->next_attached == Surf) break;
1112 Prev = Prev->next_attached;
1115 /* There must be a surface, otherwise there's a bug */
1116 assert(Prev != NULL);
1118 /* Unchain the surface */
1119 Prev->next_attached = Surf->next_attached;
1120 Surf->next_attached = NULL;
1121 Surf->first_attached = Surf;
1123 /* Check if the WineD3D depth stencil needs updating */
1124 if(This->ddraw->d3ddevice)
1126 IDirect3DDeviceImpl_UpdateDepthStencil(This->ddraw->d3ddevice);
1129 ddraw_surface7_Release(Attach);
1130 LeaveCriticalSection(&ddraw_cs);
1134 static HRESULT WINAPI ddraw_surface3_DeleteAttachedSurface(IDirectDrawSurface3 *iface,
1135 DWORD flags, IDirectDrawSurface3 *attachment)
1137 TRACE("iface %p, flags %#x, attachment %p.\n", iface, flags, attachment);
1139 return ddraw_surface7_DeleteAttachedSurface((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
1140 attachment ? (IDirectDrawSurface7 *)surface_from_surface3(attachment) : NULL);
1143 /*****************************************************************************
1144 * IDirectDrawSurface7::AddOverlayDirtyRect
1146 * "This method is not currently implemented"
1154 *****************************************************************************/
1155 static HRESULT WINAPI ddraw_surface7_AddOverlayDirtyRect(IDirectDrawSurface7 *iface, RECT *Rect)
1157 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(Rect));
1159 return DDERR_UNSUPPORTED; /* unchecked */
1162 static HRESULT WINAPI ddraw_surface3_AddOverlayDirtyRect(IDirectDrawSurface3 *iface, RECT *rect)
1164 TRACE("iface %p, rect %s.\n", iface, wine_dbgstr_rect(rect));
1166 return ddraw_surface7_AddOverlayDirtyRect((IDirectDrawSurface7 *)surface_from_surface3(iface), rect);
1169 /*****************************************************************************
1170 * IDirectDrawSurface7::GetDC
1172 * Returns a GDI device context for the surface
1175 * hdc: Address of a HDC variable to store the dc to
1179 * DDERR_INVALIDPARAMS if hdc is NULL
1180 * For details, see IWineD3DSurface::GetDC
1182 *****************************************************************************/
1183 static HRESULT WINAPI ddraw_surface7_GetDC(IDirectDrawSurface7 *iface, HDC *hdc)
1185 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1188 TRACE("iface %p, dc %p.\n", iface, hdc);
1191 return DDERR_INVALIDPARAMS;
1193 EnterCriticalSection(&ddraw_cs);
1194 hr = IWineD3DSurface_GetDC(This->WineD3DSurface,
1196 LeaveCriticalSection(&ddraw_cs);
1199 /* Some, but not all errors set *hdc to NULL. E.g. DCALREADYCREATED does not
1202 case WINED3DERR_INVALIDCALL:
1203 if(hdc) *hdc = NULL;
1204 return DDERR_INVALIDPARAMS;
1210 static HRESULT WINAPI ddraw_surface3_GetDC(IDirectDrawSurface3 *iface, HDC *dc)
1212 TRACE("iface %p, dc %p.\n", iface, dc);
1214 return ddraw_surface7_GetDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1217 /*****************************************************************************
1218 * IDirectDrawSurface7::ReleaseDC
1220 * Releases the DC that was constructed with GetDC
1223 * hdc: HDC to release
1227 * For more details, see IWineD3DSurface::ReleaseDC
1229 *****************************************************************************/
1230 static HRESULT WINAPI ddraw_surface7_ReleaseDC(IDirectDrawSurface7 *iface, HDC hdc)
1232 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1235 TRACE("iface %p, dc %p.\n", iface, hdc);
1237 EnterCriticalSection(&ddraw_cs);
1238 hr = IWineD3DSurface_ReleaseDC(This->WineD3DSurface, hdc);
1239 LeaveCriticalSection(&ddraw_cs);
1243 static HRESULT WINAPI ddraw_surface3_ReleaseDC(IDirectDrawSurface3 *iface, HDC dc)
1245 TRACE("iface %p, dc %p.\n", iface, dc);
1247 return ddraw_surface7_ReleaseDC((IDirectDrawSurface7 *)surface_from_surface3(iface), dc);
1250 /*****************************************************************************
1251 * IDirectDrawSurface7::GetCaps
1253 * Returns the surface's caps
1256 * Caps: Address to write the caps to
1260 * DDERR_INVALIDPARAMS if Caps is NULL
1262 *****************************************************************************/
1263 static HRESULT WINAPI ddraw_surface7_GetCaps(IDirectDrawSurface7 *iface, DDSCAPS2 *Caps)
1265 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1267 TRACE("iface %p, caps %p.\n", iface, Caps);
1270 return DDERR_INVALIDPARAMS;
1272 *Caps = This->surface_desc.ddsCaps;
1276 static HRESULT WINAPI ddraw_surface3_GetCaps(IDirectDrawSurface3 *iface, DDSCAPS *caps)
1281 TRACE("iface %p, caps %p.\n", iface, caps);
1283 hr = ddraw_surface7_GetCaps((IDirectDrawSurface7 *)surface_from_surface3(iface), &caps2);
1284 if (FAILED(hr)) return hr;
1286 caps->dwCaps = caps2.dwCaps;
1290 /*****************************************************************************
1291 * IDirectDrawSurface7::SetPriority
1293 * Sets a texture priority for managed textures.
1296 * Priority: The new priority
1300 * For more details, see IWineD3DSurface::SetPriority
1302 *****************************************************************************/
1303 static HRESULT WINAPI ddraw_surface7_SetPriority(IDirectDrawSurface7 *iface, DWORD Priority)
1305 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1308 TRACE("iface %p, priority %u.\n", iface, Priority);
1310 EnterCriticalSection(&ddraw_cs);
1311 hr = IWineD3DSurface_SetPriority(This->WineD3DSurface, Priority);
1312 LeaveCriticalSection(&ddraw_cs);
1316 /*****************************************************************************
1317 * IDirectDrawSurface7::GetPriority
1319 * Returns the surface's priority
1322 * Priority: Address of a variable to write the priority to
1326 * DDERR_INVALIDPARAMS if Priority == NULL
1327 * For more details, see IWineD3DSurface::GetPriority
1329 *****************************************************************************/
1330 static HRESULT WINAPI ddraw_surface7_GetPriority(IDirectDrawSurface7 *iface, DWORD *Priority)
1332 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1334 TRACE("iface %p, priority %p.\n", iface, Priority);
1338 return DDERR_INVALIDPARAMS;
1341 EnterCriticalSection(&ddraw_cs);
1342 *Priority = IWineD3DSurface_GetPriority(This->WineD3DSurface);
1343 LeaveCriticalSection(&ddraw_cs);
1347 /*****************************************************************************
1348 * IDirectDrawSurface7::SetPrivateData
1350 * Stores some data in the surface that is intended for the application's
1354 * tag: GUID that identifies the data
1355 * Data: Pointer to the private data
1356 * Size: Size of the private data
1361 * For more details, see IWineD3DSurface::SetPrivateData
1363 *****************************************************************************/
1364 static HRESULT WINAPI ddraw_surface7_SetPrivateData(IDirectDrawSurface7 *iface,
1365 REFGUID tag, void *Data, DWORD Size, DWORD Flags)
1367 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1370 TRACE("iface %p, tag %s, data %p, data_size %u, flags %#x.\n",
1371 iface, debugstr_guid(tag), Data, Size, Flags);
1373 EnterCriticalSection(&ddraw_cs);
1374 hr = IWineD3DSurface_SetPrivateData(This->WineD3DSurface,
1379 LeaveCriticalSection(&ddraw_cs);
1382 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1387 /*****************************************************************************
1388 * IDirectDrawSurface7::GetPrivateData
1390 * Returns the private data set with IDirectDrawSurface7::SetPrivateData
1393 * tag: GUID of the data to return
1394 * Data: Address where to write the data to
1395 * Size: Size of the buffer at Data
1399 * DDERR_INVALIDPARAMS if Data is NULL
1400 * For more details, see IWineD3DSurface::GetPrivateData
1402 *****************************************************************************/
1403 static HRESULT WINAPI ddraw_surface7_GetPrivateData(IDirectDrawSurface7 *iface, REFGUID tag, void *Data, DWORD *Size)
1405 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1408 TRACE("iface %p, tag %s, data %p, data_size %p.\n",
1409 iface, debugstr_guid(tag), Data, Size);
1412 return DDERR_INVALIDPARAMS;
1414 EnterCriticalSection(&ddraw_cs);
1415 hr = IWineD3DSurface_GetPrivateData(This->WineD3DSurface,
1419 LeaveCriticalSection(&ddraw_cs);
1423 /*****************************************************************************
1424 * IDirectDrawSurface7::FreePrivateData
1426 * Frees private data stored in the surface
1429 * tag: Tag of the data to free
1433 * For more details, see IWineD3DSurface::FreePrivateData
1435 *****************************************************************************/
1436 static HRESULT WINAPI ddraw_surface7_FreePrivateData(IDirectDrawSurface7 *iface, REFGUID tag)
1438 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1441 TRACE("iface %p, tag %s.\n", iface, debugstr_guid(tag));
1443 EnterCriticalSection(&ddraw_cs);
1444 hr = IWineD3DSurface_FreePrivateData(This->WineD3DSurface, tag);
1445 LeaveCriticalSection(&ddraw_cs);
1449 /*****************************************************************************
1450 * IDirectDrawSurface7::PageLock
1452 * Prevents a sysmem surface from being paged out
1455 * Flags: Not used, must be 0(unchecked)
1458 * DD_OK, because it's a stub
1460 *****************************************************************************/
1461 static HRESULT WINAPI ddraw_surface7_PageLock(IDirectDrawSurface7 *iface, DWORD Flags)
1463 TRACE("iface %p, flags %#x.\n", iface, Flags);
1465 /* This is Windows memory management related - we don't need this */
1469 static HRESULT WINAPI ddraw_surface3_PageLock(IDirectDrawSurface3 *iface, DWORD flags)
1471 TRACE("iface %p, flags %#x.\n", iface, flags);
1473 return ddraw_surface7_PageLock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1476 /*****************************************************************************
1477 * IDirectDrawSurface7::PageUnlock
1479 * Allows a sysmem surface to be paged out
1482 * Flags: Not used, must be 0(unchecked)
1485 * DD_OK, because it's a stub
1487 *****************************************************************************/
1488 static HRESULT WINAPI ddraw_surface7_PageUnlock(IDirectDrawSurface7 *iface, DWORD Flags)
1490 TRACE("iface %p, flags %#x.\n", iface, Flags);
1495 static HRESULT WINAPI ddraw_surface3_PageUnlock(IDirectDrawSurface3 *iface, DWORD flags)
1497 TRACE("iface %p, flags %#x.\n", iface, flags);
1499 return ddraw_surface7_PageUnlock((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1502 /*****************************************************************************
1503 * IDirectDrawSurface7::BltBatch
1505 * An unimplemented function
1513 *****************************************************************************/
1514 static HRESULT WINAPI ddraw_surface7_BltBatch(IDirectDrawSurface7 *iface, DDBLTBATCH *Batch, DWORD Count, DWORD Flags)
1516 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, Batch, Count, Flags);
1518 /* MSDN: "not currently implemented" */
1519 return DDERR_UNSUPPORTED;
1522 static HRESULT WINAPI ddraw_surface3_BltBatch(IDirectDrawSurface3 *iface, DDBLTBATCH *batch, DWORD count, DWORD flags)
1524 TRACE("iface %p, batch %p, count %u, flags %#x.\n", iface, batch, count, flags);
1526 return ddraw_surface7_BltBatch((IDirectDrawSurface7 *)surface_from_surface3(iface), batch, count, flags);
1529 /*****************************************************************************
1530 * IDirectDrawSurface7::EnumAttachedSurfaces
1532 * Enumerates all surfaces attached to this surface
1535 * context: Pointer to pass unmodified to the callback
1536 * cb: Callback function to call for each surface
1540 * DDERR_INVALIDPARAMS if cb is NULL
1542 *****************************************************************************/
1543 static HRESULT WINAPI ddraw_surface7_EnumAttachedSurfaces(IDirectDrawSurface7 *iface,
1544 void *context, LPDDENUMSURFACESCALLBACK7 cb)
1546 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1547 IDirectDrawSurfaceImpl *surf;
1548 DDSURFACEDESC2 desc;
1551 /* Attached surfaces aren't handled in WineD3D */
1552 TRACE("iface %p, context %p, callback %p.\n", iface, context, cb);
1555 return DDERR_INVALIDPARAMS;
1557 EnterCriticalSection(&ddraw_cs);
1558 for(i = 0; i < MAX_COMPLEX_ATTACHED; i++)
1560 surf = This->complex_array[i];
1563 ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1564 desc = surf->surface_desc;
1565 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1566 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1568 LeaveCriticalSection(&ddraw_cs);
1573 for (surf = This->next_attached; surf != NULL; surf = surf->next_attached)
1575 ddraw_surface7_AddRef((IDirectDrawSurface7 *)surf);
1576 desc = surf->surface_desc;
1577 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
1578 if (cb((IDirectDrawSurface7 *)surf, &desc, context) == DDENUMRET_CANCEL)
1580 LeaveCriticalSection(&ddraw_cs);
1585 TRACE(" end of enumeration.\n");
1587 LeaveCriticalSection(&ddraw_cs);
1591 struct callback_info
1593 LPDDENUMSURFACESCALLBACK callback;
1597 static HRESULT CALLBACK EnumCallback(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *surface_desc, void *context)
1599 const struct callback_info *info = context;
1601 return info->callback((IDirectDrawSurface *)&((IDirectDrawSurfaceImpl *)surface)->IDirectDrawSurface3_vtbl,
1602 (DDSURFACEDESC *)surface_desc, info->context);
1605 static HRESULT WINAPI ddraw_surface3_EnumAttachedSurfaces(IDirectDrawSurface3 *iface,
1606 void *context, LPDDENUMSURFACESCALLBACK callback)
1608 struct callback_info info;
1610 TRACE("iface %p, context %p, callback %p.\n", iface, context, callback);
1612 info.callback = callback;
1613 info.context = context;
1615 return ddraw_surface7_EnumAttachedSurfaces((IDirectDrawSurface7 *)surface_from_surface3(iface),
1616 &info, EnumCallback);
1619 /*****************************************************************************
1620 * IDirectDrawSurface7::EnumOverlayZOrders
1622 * "Enumerates the overlay surfaces on the specified destination"
1625 * Flags: DDENUMOVERLAYZ_BACKTOFRONT or DDENUMOVERLAYZ_FRONTTOBACK
1626 * context: context to pass back to the callback
1627 * cb: callback function to call for each enumerated surface
1630 * DD_OK, because it's a stub
1632 *****************************************************************************/
1633 static HRESULT WINAPI ddraw_surface7_EnumOverlayZOrders(IDirectDrawSurface7 *iface,
1634 DWORD Flags, void *context, LPDDENUMSURFACESCALLBACK7 cb)
1636 FIXME("iface %p, flags %#x, context %p, callback %p stub!\n", iface, Flags, context, cb);
1641 static HRESULT WINAPI ddraw_surface3_EnumOverlayZOrders(IDirectDrawSurface3 *iface,
1642 DWORD flags, void *context, LPDDENUMSURFACESCALLBACK callback)
1644 struct callback_info info;
1646 TRACE("iface %p, flags %#x, context %p, callback %p.\n", iface, flags, context, callback);
1648 info.callback = callback;
1649 info.context = context;
1651 return ddraw_surface7_EnumOverlayZOrders((IDirectDrawSurface7 *)surface_from_surface3(iface),
1652 flags, &info, EnumCallback);
1655 /*****************************************************************************
1656 * IDirectDrawSurface7::GetBltStatus
1658 * Returns the blitting status
1661 * Flags: DDGBS_CANBLT or DDGBS_ISBLTDONE
1664 * See IWineD3DSurface::Blt
1666 *****************************************************************************/
1667 static HRESULT WINAPI ddraw_surface7_GetBltStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1669 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1672 TRACE("iface %p, flags %#x.\n", iface, Flags);
1674 EnterCriticalSection(&ddraw_cs);
1675 hr = IWineD3DSurface_GetBltStatus(This->WineD3DSurface, Flags);
1676 LeaveCriticalSection(&ddraw_cs);
1679 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1684 static HRESULT WINAPI ddraw_surface3_GetBltStatus(IDirectDrawSurface3 *iface, DWORD flags)
1686 TRACE("iface %p, flags %#x.\n", iface, flags);
1688 return ddraw_surface7_GetBltStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1691 /*****************************************************************************
1692 * IDirectDrawSurface7::GetColorKey
1694 * Returns the color key assigned to the surface
1698 * CKey: Address to store the key to
1702 * DDERR_INVALIDPARAMS if CKey is NULL
1704 *****************************************************************************/
1705 static HRESULT WINAPI ddraw_surface7_GetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
1707 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1709 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
1712 return DDERR_INVALIDPARAMS;
1714 EnterCriticalSection(&ddraw_cs);
1718 case DDCKEY_DESTBLT:
1719 if (!(This->surface_desc.dwFlags & DDSD_CKDESTBLT))
1721 LeaveCriticalSection(&ddraw_cs);
1722 return DDERR_NOCOLORKEY;
1724 *CKey = This->surface_desc.ddckCKDestBlt;
1727 case DDCKEY_DESTOVERLAY:
1728 if (!(This->surface_desc.dwFlags & DDSD_CKDESTOVERLAY))
1730 LeaveCriticalSection(&ddraw_cs);
1731 return DDERR_NOCOLORKEY;
1733 *CKey = This->surface_desc.u3.ddckCKDestOverlay;
1737 if (!(This->surface_desc.dwFlags & DDSD_CKSRCBLT))
1739 LeaveCriticalSection(&ddraw_cs);
1740 return DDERR_NOCOLORKEY;
1742 *CKey = This->surface_desc.ddckCKSrcBlt;
1745 case DDCKEY_SRCOVERLAY:
1746 if (!(This->surface_desc.dwFlags & DDSD_CKSRCOVERLAY))
1748 LeaveCriticalSection(&ddraw_cs);
1749 return DDERR_NOCOLORKEY;
1751 *CKey = This->surface_desc.ddckCKSrcOverlay;
1755 LeaveCriticalSection(&ddraw_cs);
1756 return DDERR_INVALIDPARAMS;
1759 LeaveCriticalSection(&ddraw_cs);
1763 static HRESULT WINAPI ddraw_surface3_GetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
1765 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
1767 return ddraw_surface7_GetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
1770 /*****************************************************************************
1771 * IDirectDrawSurface7::GetFlipStatus
1773 * Returns the flipping status of the surface
1776 * Flags: DDGFS_CANFLIP of DDGFS_ISFLIPDONE
1779 * See IWineD3DSurface::GetFlipStatus
1781 *****************************************************************************/
1782 static HRESULT WINAPI ddraw_surface7_GetFlipStatus(IDirectDrawSurface7 *iface, DWORD Flags)
1784 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1787 TRACE("iface %p, flags %#x.\n", iface, Flags);
1789 EnterCriticalSection(&ddraw_cs);
1790 hr = IWineD3DSurface_GetFlipStatus(This->WineD3DSurface, Flags);
1791 LeaveCriticalSection(&ddraw_cs);
1794 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
1799 static HRESULT WINAPI ddraw_surface3_GetFlipStatus(IDirectDrawSurface3 *iface, DWORD flags)
1801 TRACE("iface %p, flags %#x.\n", iface, flags);
1803 return ddraw_surface7_GetFlipStatus((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
1806 /*****************************************************************************
1807 * IDirectDrawSurface7::GetOverlayPosition
1809 * Returns the display coordinates of a visible and active overlay surface
1816 * DDERR_NOTAOVERLAYSURFACE, because it's a stub
1817 *****************************************************************************/
1818 static HRESULT WINAPI ddraw_surface7_GetOverlayPosition(IDirectDrawSurface7 *iface, LONG *X, LONG *Y)
1820 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1823 TRACE("iface %p, x %p, y %p.\n", iface, X, Y);
1825 EnterCriticalSection(&ddraw_cs);
1826 hr = IWineD3DSurface_GetOverlayPosition(This->WineD3DSurface,
1829 LeaveCriticalSection(&ddraw_cs);
1833 static HRESULT WINAPI ddraw_surface3_GetOverlayPosition(IDirectDrawSurface3 *iface, LONG *x, LONG *y)
1835 TRACE("iface %p, x %p, y %p.\n", iface, x, y);
1837 return ddraw_surface7_GetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
1840 /*****************************************************************************
1841 * IDirectDrawSurface7::GetPixelFormat
1843 * Returns the pixel format of the Surface
1846 * PixelFormat: Pointer to a DDPIXELFORMAT structure to which the pixel
1847 * format should be written
1851 * DDERR_INVALIDPARAMS if PixelFormat is NULL
1853 *****************************************************************************/
1854 static HRESULT WINAPI ddraw_surface7_GetPixelFormat(IDirectDrawSurface7 *iface, DDPIXELFORMAT *PixelFormat)
1856 /* What is DDERR_INVALIDSURFACETYPE for here? */
1857 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1859 TRACE("iface %p, pixel_format %p.\n", iface, PixelFormat);
1862 return DDERR_INVALIDPARAMS;
1864 EnterCriticalSection(&ddraw_cs);
1865 DD_STRUCT_COPY_BYSIZE(PixelFormat,&This->surface_desc.u4.ddpfPixelFormat);
1866 LeaveCriticalSection(&ddraw_cs);
1871 static HRESULT WINAPI ddraw_surface3_GetPixelFormat(IDirectDrawSurface3 *iface, DDPIXELFORMAT *pixel_format)
1873 TRACE("iface %p, pixel_format %p.\n", iface, pixel_format);
1875 return ddraw_surface7_GetPixelFormat((IDirectDrawSurface7 *)surface_from_surface3(iface), pixel_format);
1878 /*****************************************************************************
1879 * IDirectDrawSurface7::GetSurfaceDesc
1881 * Returns the description of this surface
1884 * DDSD: Address of a DDSURFACEDESC2 structure that is to be filled with the
1889 * DDERR_INVALIDPARAMS if DDSD is NULL
1891 *****************************************************************************/
1892 static HRESULT WINAPI ddraw_surface7_GetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD)
1894 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
1896 TRACE("iface %p, surface_desc %p.\n", iface, DDSD);
1899 return DDERR_INVALIDPARAMS;
1901 if (DDSD->dwSize != sizeof(DDSURFACEDESC2))
1903 WARN("Incorrect struct size %d, returning DDERR_INVALIDPARAMS\n",DDSD->dwSize);
1904 return DDERR_INVALIDPARAMS;
1907 EnterCriticalSection(&ddraw_cs);
1908 DD_STRUCT_COPY_BYSIZE(DDSD,&This->surface_desc);
1909 TRACE("Returning surface desc:\n");
1910 if (TRACE_ON(ddraw)) DDRAW_dump_surface_desc(DDSD);
1912 LeaveCriticalSection(&ddraw_cs);
1916 static HRESULT WINAPI ddraw_surface3_GetSurfaceDesc(IDirectDrawSurface3 *iface, DDSURFACEDESC *surface_desc)
1918 IDirectDrawSurfaceImpl *surface = surface_from_surface3(iface);
1920 TRACE("iface %p, surface_desc %p.\n", iface, surface_desc);
1922 if (!surface_desc) return DDERR_INVALIDPARAMS;
1924 if (surface_desc->dwSize != sizeof(DDSURFACEDESC))
1926 WARN("Incorrect structure size %u, returning DDERR_INVALIDPARAMS.\n", surface_desc->dwSize);
1927 return DDERR_INVALIDPARAMS;
1930 EnterCriticalSection(&ddraw_cs);
1931 DD_STRUCT_COPY_BYSIZE(surface_desc, (DDSURFACEDESC *)&surface->surface_desc);
1932 TRACE("Returning surface desc:\n");
1933 if (TRACE_ON(ddraw))
1935 /* DDRAW_dump_surface_desc handles the smaller size */
1936 DDRAW_dump_surface_desc((DDSURFACEDESC2 *)surface_desc);
1939 LeaveCriticalSection(&ddraw_cs);
1943 /*****************************************************************************
1944 * IDirectDrawSurface7::Initialize
1946 * Initializes the surface. This is a no-op in Wine
1949 * DD: Pointer to an DirectDraw interface
1950 * DDSD: Surface description for initialization
1953 * DDERR_ALREADYINITIALIZED
1955 *****************************************************************************/
1956 static HRESULT WINAPI ddraw_surface7_Initialize(IDirectDrawSurface7 *iface,
1957 IDirectDraw *ddraw, DDSURFACEDESC2 *surface_desc)
1959 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1961 return DDERR_ALREADYINITIALIZED;
1964 static HRESULT WINAPI ddraw_surface3_Initialize(IDirectDrawSurface3 *iface,
1965 IDirectDraw *ddraw, DDSURFACEDESC *surface_desc)
1967 TRACE("iface %p, ddraw %p, surface_desc %p.\n", iface, ddraw, surface_desc);
1969 return ddraw_surface7_Initialize((IDirectDrawSurface7 *)surface_from_surface3(iface),
1970 ddraw, (DDSURFACEDESC2 *)surface_desc);
1973 /*****************************************************************************
1974 * IDirect3DTexture1::Initialize
1976 * The sdk says it's not implemented
1984 *****************************************************************************/
1985 static HRESULT WINAPI d3d_texture1_Initialize(IDirect3DTexture *iface,
1986 IDirect3DDevice *device, IDirectDrawSurface *surface)
1988 TRACE("iface %p, device %p, surface %p.\n", iface, device, surface);
1990 return DDERR_UNSUPPORTED; /* Unchecked */
1993 /*****************************************************************************
1994 * IDirectDrawSurface7::IsLost
1996 * Checks if the surface is lost
1999 * DD_OK, if the surface is usable
2000 * DDERR_ISLOST if the surface is lost
2001 * See IWineD3DSurface::IsLost for more details
2003 *****************************************************************************/
2004 static HRESULT WINAPI ddraw_surface7_IsLost(IDirectDrawSurface7 *iface)
2006 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2009 TRACE("iface %p.\n", iface);
2011 EnterCriticalSection(&ddraw_cs);
2012 /* We lose the surface if the implementation was changed */
2013 if(This->ImplType != This->ddraw->ImplType)
2015 /* But this shouldn't happen. When we change the implementation,
2016 * all surfaces are re-created automatically, and their content
2019 ERR(" (%p) Implementation was changed from %d to %d\n", This, This->ImplType, This->ddraw->ImplType);
2020 LeaveCriticalSection(&ddraw_cs);
2021 return DDERR_SURFACELOST;
2024 hr = IWineD3DSurface_IsLost(This->WineD3DSurface);
2025 LeaveCriticalSection(&ddraw_cs);
2028 /* D3D8 and 9 loose full devices, thus there's only a DEVICELOST error.
2029 * WineD3D uses the same error for surfaces
2031 case WINED3DERR_DEVICELOST: return DDERR_SURFACELOST;
2036 static HRESULT WINAPI ddraw_surface3_IsLost(IDirectDrawSurface3 *iface)
2038 TRACE("iface %p.\n", iface);
2040 return ddraw_surface7_IsLost((IDirectDrawSurface7 *)surface_from_surface3(iface));
2043 /*****************************************************************************
2044 * IDirectDrawSurface7::Restore
2046 * Restores a lost surface. This makes the surface usable again, but
2047 * doesn't reload its old contents
2051 * See IWineD3DSurface::Restore for more details
2053 *****************************************************************************/
2054 static HRESULT WINAPI ddraw_surface7_Restore(IDirectDrawSurface7 *iface)
2056 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2059 TRACE("iface %p.\n", iface);
2061 EnterCriticalSection(&ddraw_cs);
2062 if(This->ImplType != This->ddraw->ImplType)
2064 /* Call the recreation callback. Make sure to AddRef first */
2065 IDirectDrawSurface_AddRef(iface);
2066 ddraw_recreate_surfaces_cb(iface, &This->surface_desc, NULL /* Not needed */);
2068 hr = IWineD3DSurface_Restore(This->WineD3DSurface);
2069 LeaveCriticalSection(&ddraw_cs);
2073 static HRESULT WINAPI ddraw_surface3_Restore(IDirectDrawSurface3 *iface)
2075 TRACE("iface %p.\n", iface);
2077 return ddraw_surface7_Restore((IDirectDrawSurface7 *)surface_from_surface3(iface));
2080 /*****************************************************************************
2081 * IDirectDrawSurface7::SetOverlayPosition
2083 * Changes the display coordinates of an overlay surface
2090 * DDERR_NOTAOVERLAYSURFACE, because we don't support overlays right now
2091 *****************************************************************************/
2092 static HRESULT WINAPI ddraw_surface7_SetOverlayPosition(IDirectDrawSurface7 *iface, LONG X, LONG Y)
2094 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2097 TRACE("iface %p, x %d, y %d.\n", iface, X, Y);
2099 EnterCriticalSection(&ddraw_cs);
2100 hr = IWineD3DSurface_SetOverlayPosition(This->WineD3DSurface,
2103 LeaveCriticalSection(&ddraw_cs);
2107 static HRESULT WINAPI ddraw_surface3_SetOverlayPosition(IDirectDrawSurface3 *iface, LONG x, LONG y)
2109 TRACE("iface %p, x %d, y %d.\n", iface, x, y);
2111 return ddraw_surface7_SetOverlayPosition((IDirectDrawSurface7 *)surface_from_surface3(iface), x, y);
2114 /*****************************************************************************
2115 * IDirectDrawSurface7::UpdateOverlay
2117 * Modifies the attributes of an overlay surface.
2120 * SrcRect: The section of the source being used for the overlay
2121 * DstSurface: Address of the surface that is overlaid
2122 * DstRect: Place of the overlay
2123 * Flags: some DDOVER_* flags
2126 * DDERR_UNSUPPORTED, because we don't support overlays
2128 *****************************************************************************/
2129 static HRESULT WINAPI ddraw_surface7_UpdateOverlay(IDirectDrawSurface7 *iface, RECT *SrcRect,
2130 IDirectDrawSurface7 *DstSurface, RECT *DstRect, DWORD Flags, DDOVERLAYFX *FX)
2132 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2133 IDirectDrawSurfaceImpl *Dst = (IDirectDrawSurfaceImpl *)DstSurface;
2136 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2137 iface, wine_dbgstr_rect(SrcRect), DstSurface, wine_dbgstr_rect(DstRect), Flags, FX);
2139 EnterCriticalSection(&ddraw_cs);
2140 hr = IWineD3DSurface_UpdateOverlay(This->WineD3DSurface,
2142 Dst ? Dst->WineD3DSurface : NULL,
2145 (WINEDDOVERLAYFX *) FX);
2146 LeaveCriticalSection(&ddraw_cs);
2148 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2149 case WINEDDERR_NOTAOVERLAYSURFACE: return DDERR_NOTAOVERLAYSURFACE;
2150 case WINEDDERR_OVERLAYNOTVISIBLE: return DDERR_OVERLAYNOTVISIBLE;
2156 static HRESULT WINAPI ddraw_surface3_UpdateOverlay(IDirectDrawSurface3 *iface, RECT *src_rect,
2157 IDirectDrawSurface3 *dst_surface, RECT *dst_rect, DWORD flags, DDOVERLAYFX *fx)
2159 TRACE("iface %p, src_rect %s, dst_surface %p, dst_rect %s, flags %#x, fx %p.\n",
2160 iface, wine_dbgstr_rect(src_rect), dst_surface, wine_dbgstr_rect(dst_rect), flags, fx);
2162 return ddraw_surface7_UpdateOverlay((IDirectDrawSurface7 *)surface_from_surface3(iface), src_rect,
2163 dst_surface ? (IDirectDrawSurface7 *)surface_from_surface3(dst_surface) : NULL, dst_rect, flags, fx);
2166 /*****************************************************************************
2167 * IDirectDrawSurface7::UpdateOverlayDisplay
2169 * The DX7 sdk says that it's not implemented
2174 * Returns: DDERR_UNSUPPORTED, because we don't support overlays
2176 *****************************************************************************/
2177 static HRESULT WINAPI ddraw_surface7_UpdateOverlayDisplay(IDirectDrawSurface7 *iface, DWORD Flags)
2179 TRACE("iface %p, flags %#x.\n", iface, Flags);
2181 return DDERR_UNSUPPORTED;
2184 static HRESULT WINAPI ddraw_surface3_UpdateOverlayDisplay(IDirectDrawSurface3 *iface, DWORD flags)
2186 TRACE("iface %p, flags %#x.\n", iface, flags);
2188 return ddraw_surface7_UpdateOverlayDisplay((IDirectDrawSurface7 *)surface_from_surface3(iface), flags);
2191 /*****************************************************************************
2192 * IDirectDrawSurface7::UpdateOverlayZOrder
2194 * Sets an overlay's Z order
2197 * Flags: DDOVERZ_* flags
2198 * DDSRef: Defines the relative position in the overlay chain
2201 * DDERR_NOTOVERLAYSURFACE, because we don't support overlays
2203 *****************************************************************************/
2204 static HRESULT WINAPI ddraw_surface7_UpdateOverlayZOrder(IDirectDrawSurface7 *iface,
2205 DWORD Flags, IDirectDrawSurface7 *DDSRef)
2207 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2208 IDirectDrawSurfaceImpl *Ref = (IDirectDrawSurfaceImpl *)DDSRef;
2211 TRACE("iface %p, flags %#x, reference %p.\n", iface, Flags, DDSRef);
2213 EnterCriticalSection(&ddraw_cs);
2214 hr = IWineD3DSurface_UpdateOverlayZOrder(This->WineD3DSurface,
2216 Ref ? Ref->WineD3DSurface : NULL);
2217 LeaveCriticalSection(&ddraw_cs);
2221 static HRESULT WINAPI ddraw_surface3_UpdateOverlayZOrder(IDirectDrawSurface3 *iface,
2222 DWORD flags, IDirectDrawSurface3 *reference)
2224 TRACE("iface %p, flags %#x, reference %p.\n", iface, flags, reference);
2226 return ddraw_surface7_UpdateOverlayZOrder((IDirectDrawSurface7 *)surface_from_surface3(iface), flags,
2227 reference ? (IDirectDrawSurface7 *)surface_from_surface3(reference) : NULL);
2230 /*****************************************************************************
2231 * IDirectDrawSurface7::GetDDInterface
2233 * Returns the IDirectDraw7 interface pointer of the DirectDraw object this
2234 * surface belongs to
2237 * DD: Address to write the interface pointer to
2241 * DDERR_INVALIDPARAMS if DD is NULL
2243 *****************************************************************************/
2244 static HRESULT WINAPI ddraw_surface7_GetDDInterface(IDirectDrawSurface7 *iface, void **DD)
2246 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2248 TRACE("iface %p, ddraw %p.\n", iface, DD);
2251 return DDERR_INVALIDPARAMS;
2253 switch(This->version)
2260 *DD = &This->ddraw->IDirectDraw4_vtbl;
2264 *DD = &This->ddraw->IDirectDraw2_vtbl;
2268 *DD = &This->ddraw->IDirectDraw_vtbl;
2272 IUnknown_AddRef((IUnknown *)*DD);
2277 static HRESULT WINAPI ddraw_surface3_GetDDInterface(IDirectDrawSurface3 *iface, void **ddraw)
2279 TRACE("iface %p, ddraw %p.\n", iface, ddraw);
2281 return ddraw_surface7_GetDDInterface((IDirectDrawSurface7 *)surface_from_surface3(iface), ddraw);
2284 /* This seems also windows implementation specific - I don't think WineD3D needs this */
2285 static HRESULT WINAPI ddraw_surface7_ChangeUniquenessValue(IDirectDrawSurface7 *iface)
2287 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2288 volatile IDirectDrawSurfaceImpl* vThis = This;
2290 TRACE("iface %p.\n", iface);
2292 EnterCriticalSection(&ddraw_cs);
2293 /* A uniqueness value of 0 is apparently special.
2294 * This needs to be checked.
2295 * TODO: Write tests for this code and check if the volatile, interlocked stuff is really needed
2298 DWORD old_uniqueness_value = vThis->uniqueness_value;
2299 DWORD new_uniqueness_value = old_uniqueness_value+1;
2301 if (old_uniqueness_value == 0) break;
2302 if (new_uniqueness_value == 0) new_uniqueness_value = 1;
2304 if (InterlockedCompareExchange((LONG*)&vThis->uniqueness_value,
2305 old_uniqueness_value,
2306 new_uniqueness_value)
2307 == old_uniqueness_value)
2311 LeaveCriticalSection(&ddraw_cs);
2315 static HRESULT WINAPI ddraw_surface7_GetUniquenessValue(IDirectDrawSurface7 *iface, DWORD *pValue)
2317 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2319 TRACE("iface %p, value %p.\n", iface, pValue);
2321 EnterCriticalSection(&ddraw_cs);
2322 *pValue = This->uniqueness_value;
2323 LeaveCriticalSection(&ddraw_cs);
2327 /*****************************************************************************
2328 * IDirectDrawSurface7::SetLOD
2330 * Sets the level of detail of a texture
2333 * MaxLOD: LOD to set
2337 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2339 *****************************************************************************/
2340 static HRESULT WINAPI ddraw_surface7_SetLOD(IDirectDrawSurface7 *iface, DWORD MaxLOD)
2342 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2345 TRACE("iface %p, lod %u.\n", iface, MaxLOD);
2347 EnterCriticalSection(&ddraw_cs);
2348 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2350 LeaveCriticalSection(&ddraw_cs);
2351 return DDERR_INVALIDOBJECT;
2354 if(!This->wineD3DTexture)
2356 ERR("(%p) The DirectDraw texture has no WineD3DTexture!\n", This);
2357 LeaveCriticalSection(&ddraw_cs);
2358 return DDERR_INVALIDOBJECT;
2361 hr = IWineD3DBaseTexture_SetLOD(This->wineD3DTexture,
2363 LeaveCriticalSection(&ddraw_cs);
2367 /*****************************************************************************
2368 * IDirectDrawSurface7::GetLOD
2370 * Returns the level of detail of a Direct3D texture
2373 * MaxLOD: Address to write the LOD to
2377 * DDERR_INVALIDPARAMS if MaxLOD is NULL
2378 * DDERR_INVALIDOBJECT if the surface is invalid for this method
2380 *****************************************************************************/
2381 static HRESULT WINAPI ddraw_surface7_GetLOD(IDirectDrawSurface7 *iface, DWORD *MaxLOD)
2383 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2385 TRACE("iface %p, lod %p.\n", iface, MaxLOD);
2388 return DDERR_INVALIDPARAMS;
2390 EnterCriticalSection(&ddraw_cs);
2391 if (!(This->surface_desc.ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE))
2393 LeaveCriticalSection(&ddraw_cs);
2394 return DDERR_INVALIDOBJECT;
2397 *MaxLOD = IWineD3DBaseTexture_GetLOD(This->wineD3DTexture);
2398 LeaveCriticalSection(&ddraw_cs);
2402 /*****************************************************************************
2403 * IDirectDrawSurface7::BltFast
2405 * Performs a fast Blit.
2408 * dstx: The x coordinate to blit to on the destination
2409 * dsty: The y coordinate to blit to on the destination
2410 * Source: The source surface
2411 * rsrc: The source rectangle
2412 * trans: Type of transfer. Some DDBLTFAST_* flags
2416 * For more details, see IWineD3DSurface::BltFast
2418 *****************************************************************************/
2419 static HRESULT WINAPI ddraw_surface7_BltFast(IDirectDrawSurface7 *iface, DWORD dstx, DWORD dsty,
2420 IDirectDrawSurface7 *Source, RECT *rsrc, DWORD trans)
2422 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2423 IDirectDrawSurfaceImpl *src = (IDirectDrawSurfaceImpl *)Source;
2424 DWORD src_w, src_h, dst_w, dst_h;
2427 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2428 iface, dstx, dsty, Source, wine_dbgstr_rect(rsrc), trans);
2430 dst_w = This->surface_desc.dwWidth;
2431 dst_h = This->surface_desc.dwHeight;
2433 /* Source must be != NULL, This is not checked by windows. Windows happily throws a 0xc0000005
2438 if(rsrc->top > rsrc->bottom || rsrc->left > rsrc->right ||
2439 rsrc->right > src->surface_desc.dwWidth ||
2440 rsrc->bottom > src->surface_desc.dwHeight)
2442 WARN("Source rectangle is invalid, returning DDERR_INVALIDRECT\n");
2443 return DDERR_INVALIDRECT;
2446 src_w = rsrc->right - rsrc->left;
2447 src_h = rsrc->bottom - rsrc->top;
2451 src_w = src->surface_desc.dwWidth;
2452 src_h = src->surface_desc.dwHeight;
2455 if (src_w > dst_w || dstx > dst_w - src_w
2456 || src_h > dst_h || dsty > dst_h - src_h)
2458 WARN("Destination area out of bounds, returning DDERR_INVALIDRECT.\n");
2459 return DDERR_INVALIDRECT;
2462 EnterCriticalSection(&ddraw_cs);
2463 hr = IWineD3DSurface_BltFast(This->WineD3DSurface,
2465 src ? src->WineD3DSurface : NULL,
2468 LeaveCriticalSection(&ddraw_cs);
2471 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
2472 case WINED3DERR_WRONGTEXTUREFORMAT: return DDERR_INVALIDPIXELFORMAT;
2477 static HRESULT WINAPI ddraw_surface3_BltFast(IDirectDrawSurface3 *iface, DWORD dst_x, DWORD dst_y,
2478 IDirectDrawSurface3 *src_surface, RECT *src_rect, DWORD flags)
2480 TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
2481 iface, dst_x, dst_y, src_surface, wine_dbgstr_rect(src_rect), flags);
2483 return ddraw_surface7_BltFast((IDirectDrawSurface7 *)surface_from_surface3(iface), dst_x, dst_y,
2484 src_surface ? (IDirectDrawSurface7 *)surface_from_surface3(src_surface) : NULL, src_rect, flags);
2487 /*****************************************************************************
2488 * IDirectDrawSurface7::GetClipper
2490 * Returns the IDirectDrawClipper interface of the clipper assigned to this
2494 * Clipper: Address to store the interface pointer at
2498 * DDERR_INVALIDPARAMS if Clipper is NULL
2499 * DDERR_NOCLIPPERATTACHED if there's no clipper attached
2501 *****************************************************************************/
2502 static HRESULT WINAPI ddraw_surface7_GetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper **Clipper)
2504 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2506 TRACE("iface %p, clipper %p.\n", iface, Clipper);
2510 LeaveCriticalSection(&ddraw_cs);
2511 return DDERR_INVALIDPARAMS;
2514 EnterCriticalSection(&ddraw_cs);
2515 if(This->clipper == NULL)
2517 LeaveCriticalSection(&ddraw_cs);
2518 return DDERR_NOCLIPPERATTACHED;
2521 *Clipper = (IDirectDrawClipper *)This->clipper;
2522 IDirectDrawClipper_AddRef(*Clipper);
2523 LeaveCriticalSection(&ddraw_cs);
2527 static HRESULT WINAPI ddraw_surface3_GetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper **clipper)
2529 TRACE("iface %p, clipper %p.\n", iface, clipper);
2531 return ddraw_surface7_GetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2534 /*****************************************************************************
2535 * IDirectDrawSurface7::SetClipper
2537 * Sets a clipper for the surface
2540 * Clipper: IDirectDrawClipper interface of the clipper to set
2545 *****************************************************************************/
2546 static HRESULT WINAPI ddraw_surface7_SetClipper(IDirectDrawSurface7 *iface, IDirectDrawClipper *Clipper)
2548 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2549 IDirectDrawClipperImpl *oldClipper = This->clipper;
2553 TRACE("iface %p, clipper %p.\n", iface, Clipper);
2555 EnterCriticalSection(&ddraw_cs);
2556 if ((IDirectDrawClipperImpl *)Clipper == This->clipper)
2558 LeaveCriticalSection(&ddraw_cs);
2562 This->clipper = (IDirectDrawClipperImpl *)Clipper;
2564 if (Clipper != NULL)
2565 IDirectDrawClipper_AddRef(Clipper);
2567 IDirectDrawClipper_Release((IDirectDrawClipper *)oldClipper);
2569 hr = IWineD3DSurface_SetClipper(This->WineD3DSurface, This->clipper ? This->clipper->wineD3DClipper : NULL);
2571 if(This->wineD3DSwapChain) {
2574 IDirectDrawClipper_GetHWnd(Clipper, &clipWindow);
2578 IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2581 IWineD3DSwapChain_SetDestWindowOverride(This->wineD3DSwapChain,
2582 This->ddraw->d3d_window);
2586 LeaveCriticalSection(&ddraw_cs);
2590 static HRESULT WINAPI ddraw_surface3_SetClipper(IDirectDrawSurface3 *iface, IDirectDrawClipper *clipper)
2592 TRACE("iface %p, clipper %p.\n", iface, clipper);
2594 return ddraw_surface7_SetClipper((IDirectDrawSurface7 *)surface_from_surface3(iface), clipper);
2597 /*****************************************************************************
2598 * IDirectDrawSurface7::SetSurfaceDesc
2600 * Sets the surface description. It can override the pixel format, the surface
2602 * It's not really tested.
2605 * DDSD: Pointer to the new surface description to set
2610 * DDERR_INVALIDPARAMS if DDSD is NULL
2612 *****************************************************************************/
2613 static HRESULT WINAPI ddraw_surface7_SetSurfaceDesc(IDirectDrawSurface7 *iface, DDSURFACEDESC2 *DDSD, DWORD Flags)
2615 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2616 enum wined3d_format_id newFormat = WINED3DFMT_UNKNOWN;
2619 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, DDSD, Flags);
2622 return DDERR_INVALIDPARAMS;
2624 EnterCriticalSection(&ddraw_cs);
2625 if (DDSD->dwFlags & DDSD_PIXELFORMAT)
2627 newFormat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
2629 if(newFormat == WINED3DFMT_UNKNOWN)
2631 ERR("Requested to set an unknown pixelformat\n");
2632 LeaveCriticalSection(&ddraw_cs);
2633 return DDERR_INVALIDPARAMS;
2635 if(newFormat != PixelFormat_DD2WineD3D(&This->surface_desc.u4.ddpfPixelFormat) )
2637 hr = IWineD3DSurface_SetFormat(This->WineD3DSurface,
2641 LeaveCriticalSection(&ddraw_cs);
2646 if (DDSD->dwFlags & DDSD_CKDESTOVERLAY)
2648 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2650 (WINEDDCOLORKEY *) &DDSD->u3.ddckCKDestOverlay);
2652 if (DDSD->dwFlags & DDSD_CKDESTBLT)
2654 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2656 (WINEDDCOLORKEY *) &DDSD->ddckCKDestBlt);
2658 if (DDSD->dwFlags & DDSD_CKSRCOVERLAY)
2660 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2662 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcOverlay);
2664 if (DDSD->dwFlags & DDSD_CKSRCBLT)
2666 IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2668 (WINEDDCOLORKEY *) &DDSD->ddckCKSrcBlt);
2670 if (DDSD->dwFlags & DDSD_LPSURFACE && DDSD->lpSurface)
2672 hr = IWineD3DSurface_SetMem(This->WineD3DSurface, DDSD->lpSurface);
2673 if(hr != WINED3D_OK)
2675 /* No need for a trace here, wined3d does that for us */
2678 case WINED3DERR_INVALIDCALL:
2679 LeaveCriticalSection(&ddraw_cs);
2680 return DDERR_INVALIDPARAMS;
2687 This->surface_desc = *DDSD;
2689 LeaveCriticalSection(&ddraw_cs);
2693 static HRESULT WINAPI ddraw_surface3_SetSurfaceDesc(IDirectDrawSurface3 *iface,
2694 DDSURFACEDESC *surface_desc, DWORD flags)
2696 TRACE("iface %p, surface_desc %p, flags %#x.\n", iface, surface_desc, flags);
2698 return ddraw_surface7_SetSurfaceDesc((IDirectDrawSurface7 *)surface_from_surface3(iface),
2699 (DDSURFACEDESC2 *)surface_desc, flags);
2702 /*****************************************************************************
2703 * IDirectDrawSurface7::GetPalette
2705 * Returns the IDirectDrawPalette interface of the palette currently assigned
2709 * Pal: Address to write the interface pointer to
2713 * DDERR_INVALIDPARAMS if Pal is NULL
2715 *****************************************************************************/
2716 static HRESULT WINAPI ddraw_surface7_GetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette **Pal)
2718 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2719 IWineD3DPalette *wPal;
2722 TRACE("iface %p, palette %p.\n", iface, Pal);
2725 return DDERR_INVALIDPARAMS;
2727 EnterCriticalSection(&ddraw_cs);
2728 hr = IWineD3DSurface_GetPalette(This->WineD3DSurface, &wPal);
2731 LeaveCriticalSection(&ddraw_cs);
2737 *Pal = IWineD3DPalette_GetParent(wPal);
2738 IDirectDrawPalette_AddRef(*Pal);
2743 hr = DDERR_NOPALETTEATTACHED;
2746 LeaveCriticalSection(&ddraw_cs);
2750 static HRESULT WINAPI ddraw_surface3_GetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette **palette)
2752 TRACE("iface %p, palette %p.\n", iface, palette);
2754 return ddraw_surface7_GetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2757 /*****************************************************************************
2760 * EnumAttachedSurface callback for SetColorKey. Used to set color keys
2761 * recursively in the surface tree
2763 *****************************************************************************/
2767 WINEDDCOLORKEY *CKey;
2771 static HRESULT WINAPI
2772 SetColorKeyEnum(IDirectDrawSurface7 *surface,
2773 DDSURFACEDESC2 *desc,
2776 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)surface;
2777 struct SCKContext *ctx = context;
2780 hr = IWineD3DSurface_SetColorKey(This->WineD3DSurface,
2785 WARN("IWineD3DSurface_SetColorKey failed, hr = %08x\n", hr);
2789 ddraw_surface7_EnumAttachedSurfaces(surface, context, SetColorKeyEnum);
2790 ddraw_surface7_Release(surface);
2792 return DDENUMRET_OK;
2795 /*****************************************************************************
2796 * IDirectDrawSurface7::SetColorKey
2798 * Sets the color keying options for the surface. Observations showed that
2799 * in case of complex surfaces the color key has to be assigned to all
2804 * CKey: The new color key
2808 * See IWineD3DSurface::SetColorKey for details
2810 *****************************************************************************/
2811 static HRESULT WINAPI ddraw_surface7_SetColorKey(IDirectDrawSurface7 *iface, DWORD Flags, DDCOLORKEY *CKey)
2813 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2814 DDCOLORKEY FixedCKey;
2815 struct SCKContext ctx = { DD_OK, (WINEDDCOLORKEY *) (CKey ? &FixedCKey : NULL), Flags };
2817 TRACE("iface %p, flags %#x, color_key %p.\n", iface, Flags, CKey);
2819 EnterCriticalSection(&ddraw_cs);
2823 /* Handle case where dwColorSpaceHighValue < dwColorSpaceLowValue */
2824 if (FixedCKey.dwColorSpaceHighValue < FixedCKey.dwColorSpaceLowValue)
2825 FixedCKey.dwColorSpaceHighValue = FixedCKey.dwColorSpaceLowValue;
2827 switch (Flags & ~DDCKEY_COLORSPACE)
2829 case DDCKEY_DESTBLT:
2830 This->surface_desc.ddckCKDestBlt = FixedCKey;
2831 This->surface_desc.dwFlags |= DDSD_CKDESTBLT;
2834 case DDCKEY_DESTOVERLAY:
2835 This->surface_desc.u3.ddckCKDestOverlay = FixedCKey;
2836 This->surface_desc.dwFlags |= DDSD_CKDESTOVERLAY;
2839 case DDCKEY_SRCOVERLAY:
2840 This->surface_desc.ddckCKSrcOverlay = FixedCKey;
2841 This->surface_desc.dwFlags |= DDSD_CKSRCOVERLAY;
2845 This->surface_desc.ddckCKSrcBlt = FixedCKey;
2846 This->surface_desc.dwFlags |= DDSD_CKSRCBLT;
2850 LeaveCriticalSection(&ddraw_cs);
2851 return DDERR_INVALIDPARAMS;
2856 switch (Flags & ~DDCKEY_COLORSPACE)
2858 case DDCKEY_DESTBLT:
2859 This->surface_desc.dwFlags &= ~DDSD_CKDESTBLT;
2862 case DDCKEY_DESTOVERLAY:
2863 This->surface_desc.dwFlags &= ~DDSD_CKDESTOVERLAY;
2866 case DDCKEY_SRCOVERLAY:
2867 This->surface_desc.dwFlags &= ~DDSD_CKSRCOVERLAY;
2871 This->surface_desc.dwFlags &= ~DDSD_CKSRCBLT;
2875 LeaveCriticalSection(&ddraw_cs);
2876 return DDERR_INVALIDPARAMS;
2879 ctx.ret = IWineD3DSurface_SetColorKey(This->WineD3DSurface, Flags, ctx.CKey);
2880 ddraw_surface7_EnumAttachedSurfaces(iface, &ctx, SetColorKeyEnum);
2881 LeaveCriticalSection(&ddraw_cs);
2884 case WINED3DERR_INVALIDCALL: return DDERR_INVALIDPARAMS;
2885 default: return ctx.ret;
2889 static HRESULT WINAPI ddraw_surface3_SetColorKey(IDirectDrawSurface3 *iface, DWORD flags, DDCOLORKEY *color_key)
2891 TRACE("iface %p, flags %#x, color_key %p.\n", iface, flags, color_key);
2893 return ddraw_surface7_SetColorKey((IDirectDrawSurface7 *)surface_from_surface3(iface), flags, color_key);
2896 /*****************************************************************************
2897 * IDirectDrawSurface7::SetPalette
2899 * Assigns a DirectDrawPalette object to the surface
2902 * Pal: Interface to the palette to set
2907 *****************************************************************************/
2908 static HRESULT WINAPI ddraw_surface7_SetPalette(IDirectDrawSurface7 *iface, IDirectDrawPalette *Pal)
2910 IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
2911 IDirectDrawPalette *oldPal;
2912 IDirectDrawSurfaceImpl *surf;
2913 IDirectDrawPaletteImpl *PalImpl = (IDirectDrawPaletteImpl *)Pal;
2916 TRACE("iface %p, palette %p.\n", iface, Pal);
2918 if (!(This->surface_desc.u4.ddpfPixelFormat.dwFlags & (DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2 |
2919 DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_PALETTEINDEXEDTO8))) {
2920 return DDERR_INVALIDPIXELFORMAT;
2923 /* Find the old palette */
2924 EnterCriticalSection(&ddraw_cs);
2925 hr = IDirectDrawSurface_GetPalette(iface, &oldPal);
2926 if(hr != DD_OK && hr != DDERR_NOPALETTEATTACHED)
2928 LeaveCriticalSection(&ddraw_cs);
2931 if(oldPal) IDirectDrawPalette_Release(oldPal); /* For the GetPalette */
2933 /* Set the new Palette */
2934 IWineD3DSurface_SetPalette(This->WineD3DSurface,
2935 PalImpl ? PalImpl->wineD3DPalette : NULL);
2936 /* AddRef the Palette */
2937 if(Pal) IDirectDrawPalette_AddRef(Pal);
2939 /* Release the old palette */
2940 if(oldPal) IDirectDrawPalette_Release(oldPal);
2942 /* If this is a front buffer, also update the back buffers
2943 * TODO: How do things work for palettized cube textures?
2945 if(This->surface_desc.ddsCaps.dwCaps & DDSCAPS_FRONTBUFFER)
2947 /* For primary surfaces the tree is just a list, so the simpler scheme fits too */
2948 DDSCAPS2 caps2 = { DDSCAPS_PRIMARYSURFACE, 0, 0, 0 };
2953 IDirectDrawSurface7 *attach;
2955 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surf, &caps2, &attach);
2961 TRACE("Setting palette on %p\n", attach);
2962 ddraw_surface7_SetPalette(attach, Pal);
2963 surf = (IDirectDrawSurfaceImpl *)attach;
2964 ddraw_surface7_Release(attach);
2968 LeaveCriticalSection(&ddraw_cs);
2972 static HRESULT WINAPI ddraw_surface3_SetPalette(IDirectDrawSurface3 *iface, IDirectDrawPalette *palette)
2974 TRACE("iface %p, palette %p.\n", iface, palette);
2976 return ddraw_surface7_SetPalette((IDirectDrawSurface7 *)surface_from_surface3(iface), palette);
2979 /**********************************************************
2980 * IDirectDrawGammaControl::GetGammaRamp
2982 * Returns the current gamma ramp for a surface
2986 * gamma_ramp: Address to write the ramp to
2990 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
2992 **********************************************************/
2993 static HRESULT WINAPI ddraw_gamma_control_GetGammaRamp(IDirectDrawGammaControl *iface,
2994 DWORD flags, DDGAMMARAMP *gamma_ramp)
2996 IDirectDrawSurfaceImpl *surface = surface_from_gamma_control(iface);
2998 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
3002 WARN("Invalid gamma_ramp passed.\n");
3003 return DDERR_INVALIDPARAMS;
3006 EnterCriticalSection(&ddraw_cs);
3007 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3009 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP. */
3010 IWineD3DDevice_GetGammaRamp(surface->ddraw->wineD3DDevice, 0, (WINED3DGAMMARAMP *)gamma_ramp);
3014 ERR("Not implemented for non-primary surfaces.\n");
3016 LeaveCriticalSection(&ddraw_cs);
3021 /**********************************************************
3022 * IDirectDrawGammaControl::SetGammaRamp
3024 * Sets the red, green and blue gamma ramps for
3027 * flags: Can be DDSGR_CALIBRATE to request calibration
3028 * gamma_ramp: Structure containing the new gamma ramp
3032 * DDERR_INVALIDPARAMS if gamma_ramp is NULL
3034 **********************************************************/
3035 static HRESULT WINAPI ddraw_gamma_control_SetGammaRamp(IDirectDrawGammaControl *iface,
3036 DWORD flags, DDGAMMARAMP *gamma_ramp)
3038 IDirectDrawSurfaceImpl *surface = surface_from_gamma_control(iface);
3040 TRACE("iface %p, flags %#x, gamma_ramp %p.\n", iface, flags, gamma_ramp);
3044 WARN("Invalid gamma_ramp passed.\n");
3045 return DDERR_INVALIDPARAMS;
3048 EnterCriticalSection(&ddraw_cs);
3049 if (surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
3051 /* Note: DDGAMMARAMP is compatible with WINED3DGAMMARAMP */
3052 IWineD3DDevice_SetGammaRamp(surface->ddraw->wineD3DDevice, 0, flags, (WINED3DGAMMARAMP *)gamma_ramp);
3056 ERR("Not implemented for non-primary surfaces.\n");
3058 LeaveCriticalSection(&ddraw_cs);
3063 /*****************************************************************************
3064 * IDirect3DTexture2::PaletteChanged
3066 * Informs the texture about a palette change
3069 * start: Start index of the change
3070 * count: The number of changed entries
3073 * D3D_OK, because it's a stub
3075 *****************************************************************************/
3076 static HRESULT WINAPI d3d_texture2_PaletteChanged(IDirect3DTexture2 *iface, DWORD start, DWORD count)
3078 FIXME("iface %p, start %u, count %u stub!\n", iface, start, count);
3083 static HRESULT WINAPI d3d_texture1_PaletteChanged(IDirect3DTexture *iface, DWORD start, DWORD count)
3085 IDirectDrawSurfaceImpl *surface = surface_from_texture1(iface);
3087 TRACE("iface %p, start %u, count %u.\n", iface, start, count);
3089 return d3d_texture2_PaletteChanged((IDirect3DTexture2 *)&surface->IDirect3DTexture2_vtbl, start, count);
3092 /*****************************************************************************
3093 * IDirect3DTexture::Unload
3095 * DX5 SDK: "The IDirect3DTexture2::Unload method is not implemented
3101 *****************************************************************************/
3102 static HRESULT WINAPI d3d_texture1_Unload(IDirect3DTexture *iface)
3104 WARN("iface %p. Not implemented.\n", iface);
3106 return DDERR_UNSUPPORTED;
3109 /*****************************************************************************
3110 * IDirect3DTexture2::GetHandle
3112 * Returns handle for the texture. At the moment, the interface
3113 * to the IWineD3DTexture is used.
3116 * device: Device this handle is assigned to
3117 * handle: Address to store the handle at.
3122 *****************************************************************************/
3123 static HRESULT WINAPI d3d_texture2_GetHandle(IDirect3DTexture2 *iface,
3124 IDirect3DDevice2 *device, D3DTEXTUREHANDLE *handle)
3126 IDirectDrawSurfaceImpl *surface = surface_from_texture2(iface);
3128 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
3130 EnterCriticalSection(&ddraw_cs);
3132 if (!surface->Handle)
3134 DWORD h = ddraw_allocate_handle(&device_from_device2(device)->handle_table, surface, DDRAW_HANDLE_SURFACE);
3135 if (h == DDRAW_INVALID_HANDLE)
3137 ERR("Failed to allocate a texture handle.\n");
3138 LeaveCriticalSection(&ddraw_cs);
3139 return DDERR_OUTOFMEMORY;
3142 surface->Handle = h + 1;
3145 TRACE("Returning handle %08x.\n", surface->Handle);
3146 *handle = surface->Handle;
3148 LeaveCriticalSection(&ddraw_cs);
3153 static HRESULT WINAPI d3d_texture1_GetHandle(IDirect3DTexture *iface,
3154 IDirect3DDevice *device, D3DTEXTUREHANDLE *handle)
3156 IDirect3DTexture2 *texture2 = (IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl;
3157 IDirect3DDevice2 *device2 = (IDirect3DDevice2 *)&device_from_device1(device)->IDirect3DDevice2_vtbl;
3159 TRACE("iface %p, device %p, handle %p.\n", iface, device, handle);
3161 return d3d_texture2_GetHandle(texture2, device2, handle);
3164 /*****************************************************************************
3165 * get_sub_mimaplevel
3167 * Helper function that returns the next mipmap level
3169 * tex_ptr: Surface of which to return the next level
3171 *****************************************************************************/
3172 static IDirectDrawSurfaceImpl *get_sub_mimaplevel(IDirectDrawSurfaceImpl *surface)
3174 /* Now go down the mipmap chain to the next surface */
3175 static DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
3176 IDirectDrawSurface7 *next_level;
3179 hr = ddraw_surface7_GetAttachedSurface((IDirectDrawSurface7 *)surface, &mipmap_caps, &next_level);
3180 if (FAILED(hr)) return NULL;
3182 ddraw_surface7_Release(next_level);
3184 return (IDirectDrawSurfaceImpl *)next_level;
3187 /*****************************************************************************
3188 * IDirect3DTexture2::Load
3190 * Loads a texture created with the DDSCAPS_ALLOCONLOAD
3192 * This function isn't relayed to WineD3D because the whole interface is
3193 * implemented in DDraw only. For speed improvements a implementation which
3194 * takes OpenGL more into account could be placed into WineD3D.
3197 * src_texture: Address of the texture to load
3201 * D3DERR_TEXTURE_LOAD_FAILED.
3203 *****************************************************************************/
3204 static HRESULT WINAPI d3d_texture2_Load(IDirect3DTexture2 *iface, IDirect3DTexture2 *src_texture)
3206 IDirectDrawSurfaceImpl *dst_surface = surface_from_texture2(iface);
3207 IDirectDrawSurfaceImpl *src_surface = surface_from_texture2(src_texture);
3210 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
3212 if (src_surface == dst_surface)
3214 TRACE("copying surface %p to surface %p, why?\n", src_surface, dst_surface);
3218 EnterCriticalSection(&ddraw_cs);
3220 if (((src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3221 != (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP))
3222 || (src_surface->surface_desc.u2.dwMipMapCount != dst_surface->surface_desc.u2.dwMipMapCount))
3224 ERR("Trying to load surfaces with different mip-map counts.\n");
3229 IWineD3DPalette *wined3d_dst_pal, *wined3d_src_pal;
3230 IDirectDrawPalette *dst_pal = NULL, *src_pal = NULL;
3231 DDSURFACEDESC *src_desc, *dst_desc;
3233 TRACE("Copying surface %p to surface %p (mipmap level %d).\n",
3234 src_surface, dst_surface, src_surface->mipmap_level);
3236 /* Suppress the ALLOCONLOAD flag */
3237 dst_surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
3239 /* Get the palettes */
3240 hr = IWineD3DSurface_GetPalette(dst_surface->WineD3DSurface, &wined3d_dst_pal);
3243 ERR("Failed to get destination palette, hr %#x.\n", hr);
3244 LeaveCriticalSection(&ddraw_cs);
3245 return D3DERR_TEXTURE_LOAD_FAILED;
3247 if (wined3d_dst_pal) dst_pal = IWineD3DPalette_GetParent(wined3d_dst_pal);
3249 hr = IWineD3DSurface_GetPalette(src_surface->WineD3DSurface, &wined3d_src_pal);
3252 ERR("Failed to get source palette, hr %#x.\n", hr);
3253 LeaveCriticalSection(&ddraw_cs);
3254 return D3DERR_TEXTURE_LOAD_FAILED;
3256 if (wined3d_src_pal) src_pal = IWineD3DPalette_GetParent(wined3d_src_pal);
3260 PALETTEENTRY palent[256];
3264 LeaveCriticalSection(&ddraw_cs);
3265 return DDERR_NOPALETTEATTACHED;
3267 IDirectDrawPalette_GetEntries(src_pal, 0, 0, 256, palent);
3268 IDirectDrawPalette_SetEntries(dst_pal, 0, 0, 256, palent);
3271 /* Copy one surface on the other */
3272 dst_desc = (DDSURFACEDESC *)&(dst_surface->surface_desc);
3273 src_desc = (DDSURFACEDESC *)&(src_surface->surface_desc);
3275 if ((src_desc->dwWidth != dst_desc->dwWidth) || (src_desc->dwHeight != dst_desc->dwHeight))
3277 /* Should also check for same pixel format, u1.lPitch, ... */
3278 ERR("Error in surface sizes.\n");
3279 LeaveCriticalSection(&ddraw_cs);
3280 return D3DERR_TEXTURE_LOAD_FAILED;
3284 WINED3DLOCKED_RECT src_rect, dst_rect;
3286 /* Copy also the ColorKeying stuff */
3287 if (src_desc->dwFlags & DDSD_CKSRCBLT)
3289 dst_desc->dwFlags |= DDSD_CKSRCBLT;
3290 dst_desc->ddckCKSrcBlt.dwColorSpaceLowValue = src_desc->ddckCKSrcBlt.dwColorSpaceLowValue;
3291 dst_desc->ddckCKSrcBlt.dwColorSpaceHighValue = src_desc->ddckCKSrcBlt.dwColorSpaceHighValue;
3294 /* Copy the main memory texture into the surface that corresponds
3295 * to the OpenGL texture object. */
3297 hr = IWineD3DSurface_Map(src_surface->WineD3DSurface, &src_rect, NULL, 0);
3300 ERR("Failed to lock source surface, hr %#x.\n", hr);
3301 LeaveCriticalSection(&ddraw_cs);
3302 return D3DERR_TEXTURE_LOAD_FAILED;
3305 hr = IWineD3DSurface_Map(dst_surface->WineD3DSurface, &dst_rect, NULL, 0);
3308 ERR("Failed to lock destination surface, hr %#x.\n", hr);
3309 IWineD3DSurface_Unmap(src_surface->WineD3DSurface);
3310 LeaveCriticalSection(&ddraw_cs);
3311 return D3DERR_TEXTURE_LOAD_FAILED;
3314 if (dst_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
3315 memcpy(dst_rect.pBits, src_rect.pBits, src_surface->surface_desc.u1.dwLinearSize);
3317 memcpy(dst_rect.pBits, src_rect.pBits, src_rect.Pitch * src_desc->dwHeight);
3319 IWineD3DSurface_Unmap(src_surface->WineD3DSurface);
3320 IWineD3DSurface_Unmap(dst_surface->WineD3DSurface);
3323 if (src_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3324 src_surface = get_sub_mimaplevel(src_surface);
3328 if (dst_surface->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
3329 dst_surface = get_sub_mimaplevel(dst_surface);
3333 if (!src_surface || !dst_surface)
3335 if (src_surface != dst_surface)
3336 ERR("Loading surface with different mipmap structure.\n");
3341 LeaveCriticalSection(&ddraw_cs);
3346 static HRESULT WINAPI d3d_texture1_Load(IDirect3DTexture *iface, IDirect3DTexture *src_texture)
3348 TRACE("iface %p, src_texture %p.\n", iface, src_texture);
3350 return d3d_texture2_Load((IDirect3DTexture2 *)&surface_from_texture1(iface)->IDirect3DTexture2_vtbl,
3351 src_texture ? (IDirect3DTexture2 *)&surface_from_texture1(src_texture)->IDirect3DTexture2_vtbl : NULL);
3354 /*****************************************************************************
3356 *****************************************************************************/
3358 static const struct IDirectDrawSurface7Vtbl ddraw_surface7_vtbl =
3361 ddraw_surface7_QueryInterface,
3362 ddraw_surface7_AddRef,
3363 ddraw_surface7_Release,
3364 /* IDirectDrawSurface */
3365 ddraw_surface7_AddAttachedSurface,
3366 ddraw_surface7_AddOverlayDirtyRect,
3368 ddraw_surface7_BltBatch,
3369 ddraw_surface7_BltFast,
3370 ddraw_surface7_DeleteAttachedSurface,
3371 ddraw_surface7_EnumAttachedSurfaces,
3372 ddraw_surface7_EnumOverlayZOrders,
3373 ddraw_surface7_Flip,
3374 ddraw_surface7_GetAttachedSurface,
3375 ddraw_surface7_GetBltStatus,
3376 ddraw_surface7_GetCaps,
3377 ddraw_surface7_GetClipper,
3378 ddraw_surface7_GetColorKey,
3379 ddraw_surface7_GetDC,
3380 ddraw_surface7_GetFlipStatus,
3381 ddraw_surface7_GetOverlayPosition,
3382 ddraw_surface7_GetPalette,
3383 ddraw_surface7_GetPixelFormat,
3384 ddraw_surface7_GetSurfaceDesc,
3385 ddraw_surface7_Initialize,
3386 ddraw_surface7_IsLost,
3387 ddraw_surface7_Lock,
3388 ddraw_surface7_ReleaseDC,
3389 ddraw_surface7_Restore,
3390 ddraw_surface7_SetClipper,
3391 ddraw_surface7_SetColorKey,
3392 ddraw_surface7_SetOverlayPosition,
3393 ddraw_surface7_SetPalette,
3394 ddraw_surface7_Unlock,
3395 ddraw_surface7_UpdateOverlay,
3396 ddraw_surface7_UpdateOverlayDisplay,
3397 ddraw_surface7_UpdateOverlayZOrder,
3398 /* IDirectDrawSurface2 */
3399 ddraw_surface7_GetDDInterface,
3400 ddraw_surface7_PageLock,
3401 ddraw_surface7_PageUnlock,
3402 /* IDirectDrawSurface3 */
3403 ddraw_surface7_SetSurfaceDesc,
3404 /* IDirectDrawSurface4 */
3405 ddraw_surface7_SetPrivateData,
3406 ddraw_surface7_GetPrivateData,
3407 ddraw_surface7_FreePrivateData,
3408 ddraw_surface7_GetUniquenessValue,
3409 ddraw_surface7_ChangeUniquenessValue,
3410 /* IDirectDrawSurface7 */
3411 ddraw_surface7_SetPriority,
3412 ddraw_surface7_GetPriority,
3413 ddraw_surface7_SetLOD,
3414 ddraw_surface7_GetLOD,
3417 static const struct IDirectDrawSurface3Vtbl ddraw_surface3_vtbl =
3420 ddraw_surface3_QueryInterface,
3421 ddraw_surface3_AddRef,
3422 ddraw_surface3_Release,
3423 /* IDirectDrawSurface */
3424 ddraw_surface3_AddAttachedSurface,
3425 ddraw_surface3_AddOverlayDirtyRect,
3427 ddraw_surface3_BltBatch,
3428 ddraw_surface3_BltFast,
3429 ddraw_surface3_DeleteAttachedSurface,
3430 ddraw_surface3_EnumAttachedSurfaces,
3431 ddraw_surface3_EnumOverlayZOrders,
3432 ddraw_surface3_Flip,
3433 ddraw_surface3_GetAttachedSurface,
3434 ddraw_surface3_GetBltStatus,
3435 ddraw_surface3_GetCaps,
3436 ddraw_surface3_GetClipper,
3437 ddraw_surface3_GetColorKey,
3438 ddraw_surface3_GetDC,
3439 ddraw_surface3_GetFlipStatus,
3440 ddraw_surface3_GetOverlayPosition,
3441 ddraw_surface3_GetPalette,
3442 ddraw_surface3_GetPixelFormat,
3443 ddraw_surface3_GetSurfaceDesc,
3444 ddraw_surface3_Initialize,
3445 ddraw_surface3_IsLost,
3446 ddraw_surface3_Lock,
3447 ddraw_surface3_ReleaseDC,
3448 ddraw_surface3_Restore,
3449 ddraw_surface3_SetClipper,
3450 ddraw_surface3_SetColorKey,
3451 ddraw_surface3_SetOverlayPosition,
3452 ddraw_surface3_SetPalette,
3453 ddraw_surface3_Unlock,
3454 ddraw_surface3_UpdateOverlay,
3455 ddraw_surface3_UpdateOverlayDisplay,
3456 ddraw_surface3_UpdateOverlayZOrder,
3457 /* IDirectDrawSurface2 */
3458 ddraw_surface3_GetDDInterface,
3459 ddraw_surface3_PageLock,
3460 ddraw_surface3_PageUnlock,
3461 /* IDirectDrawSurface3 */
3462 ddraw_surface3_SetSurfaceDesc,
3465 static const struct IDirectDrawGammaControlVtbl ddraw_gamma_control_vtbl =
3467 ddraw_gamma_control_QueryInterface,
3468 ddraw_gamma_control_AddRef,
3469 ddraw_gamma_control_Release,
3470 ddraw_gamma_control_GetGammaRamp,
3471 ddraw_gamma_control_SetGammaRamp,
3474 static const struct IDirect3DTexture2Vtbl d3d_texture2_vtbl =
3476 d3d_texture2_QueryInterface,
3477 d3d_texture2_AddRef,
3478 d3d_texture2_Release,
3479 d3d_texture2_GetHandle,
3480 d3d_texture2_PaletteChanged,
3484 static const struct IDirect3DTextureVtbl d3d_texture1_vtbl =
3486 d3d_texture1_QueryInterface,
3487 d3d_texture1_AddRef,
3488 d3d_texture1_Release,
3489 d3d_texture1_Initialize,
3490 d3d_texture1_GetHandle,
3491 d3d_texture1_PaletteChanged,
3493 d3d_texture1_Unload,
3496 HRESULT ddraw_surface_init(IDirectDrawSurfaceImpl *surface, IDirectDrawImpl *ddraw,
3497 DDSURFACEDESC2 *desc, UINT mip_level, WINED3DSURFTYPE surface_type)
3499 WINED3DPOOL pool = WINED3DPOOL_DEFAULT;
3500 WINED3DSURFACE_DESC wined3d_desc;
3501 enum wined3d_format_id format;
3505 if (!(desc->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY))
3506 && !((desc->ddsCaps.dwCaps & DDSCAPS_TEXTURE)
3507 && (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)))
3509 /* Tests show surfaces without memory flags get these flags added
3510 * right after creation. */
3511 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
3514 if (desc->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE))
3516 usage |= WINED3DUSAGE_RENDERTARGET;
3517 desc->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
3520 if (desc->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
3522 usage |= WINED3DUSAGE_OVERLAY;
3525 if (ddraw->depthstencil || (desc->ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
3527 /* The depth stencil creation callback sets this flag. Set the
3528 * wined3d usage to let it know it's a depth/stencil surface. */
3529 usage |= WINED3DUSAGE_DEPTHSTENCIL;
3532 if (desc->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
3534 pool = WINED3DPOOL_SYSTEMMEM;
3536 else if (desc->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
3538 pool = WINED3DPOOL_MANAGED;
3539 /* Managed textures have the system memory flag set. */
3540 desc->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
3542 else if (desc->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
3544 /* Videomemory adds localvidmem. This is mutually exclusive with
3545 * systemmemory and texturemanage. */
3546 desc->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
3549 format = PixelFormat_DD2WineD3D(&desc->u4.ddpfPixelFormat);
3550 if (format == WINED3DFMT_UNKNOWN)
3552 WARN("Unsupported / unknown pixelformat.\n");
3553 return DDERR_INVALIDPIXELFORMAT;
3556 surface->lpVtbl = &ddraw_surface7_vtbl;
3557 surface->IDirectDrawSurface3_vtbl = &ddraw_surface3_vtbl;
3558 surface->IDirectDrawGammaControl_vtbl = &ddraw_gamma_control_vtbl;
3559 surface->IDirect3DTexture2_vtbl = &d3d_texture2_vtbl;
3560 surface->IDirect3DTexture_vtbl = &d3d_texture1_vtbl;
3562 surface->version = 7;
3563 surface->ddraw = ddraw;
3565 surface->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
3566 surface->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3567 DD_STRUCT_COPY_BYSIZE(&surface->surface_desc, desc);
3569 surface->first_attached = surface;
3570 surface->ImplType = surface_type;
3572 hr = IWineD3DDevice_CreateSurface(ddraw->wineD3DDevice, desc->dwWidth, desc->dwHeight, format,
3573 TRUE /* Lockable */, FALSE /* Discard */, mip_level, usage, pool,
3574 WINED3DMULTISAMPLE_NONE, 0 /* MultiSampleQuality */, surface_type, surface,
3575 &ddraw_null_wined3d_parent_ops, &surface->WineD3DSurface);
3578 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
3582 surface->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
3583 IWineD3DSurface_GetDesc(surface->WineD3DSurface, &wined3d_desc);
3585 format = wined3d_desc.format;
3586 if (format == WINED3DFMT_UNKNOWN)
3588 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN.\n");
3590 PixelFormat_WineD3DtoDD(&surface->surface_desc.u4.ddpfPixelFormat, format);
3592 /* Anno 1602 stores the pitch right after surface creation, so make sure
3593 * it's there. TODO: Test other fourcc formats. */
3594 if (format == WINED3DFMT_DXT1 || format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3
3595 || format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5)
3597 surface->surface_desc.dwFlags |= DDSD_LINEARSIZE;
3598 if (format == WINED3DFMT_DXT1)
3600 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height) / 2;
3604 surface->surface_desc.u1.dwLinearSize = max(4, wined3d_desc.width) * max(4, wined3d_desc.height);
3609 surface->surface_desc.dwFlags |= DDSD_PITCH;
3610 surface->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch(surface->WineD3DSurface);
3613 if (desc->dwFlags & DDSD_CKDESTOVERLAY)
3615 IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3616 DDCKEY_DESTOVERLAY, (WINEDDCOLORKEY *)&desc->u3.ddckCKDestOverlay);
3618 if (desc->dwFlags & DDSD_CKDESTBLT)
3620 IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3621 DDCKEY_DESTBLT, (WINEDDCOLORKEY *)&desc->ddckCKDestBlt);
3623 if (desc->dwFlags & DDSD_CKSRCOVERLAY)
3625 IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3626 DDCKEY_SRCOVERLAY, (WINEDDCOLORKEY *)&desc->ddckCKSrcOverlay);
3628 if (desc->dwFlags & DDSD_CKSRCBLT)
3630 IWineD3DSurface_SetColorKey(surface->WineD3DSurface,
3631 DDCKEY_SRCBLT, (WINEDDCOLORKEY *)&desc->ddckCKSrcBlt);
3633 if (desc->dwFlags & DDSD_LPSURFACE)
3635 hr = IWineD3DSurface_SetMem(surface->WineD3DSurface, desc->lpSurface);
3638 ERR("Failed to set surface memory, hr %#x.\n", hr);
3639 IWineD3DSurface_Release(surface->WineD3DSurface);