1 /* DirectDrawSurface base implementation
3 * Copyright 1997-2000 Marcus Meissner
4 * Copyright 1998-2000 Lionel Ulmer (most of Direct3D stuff)
5 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
29 #include "mesa_private.h"
30 #include "wine/debug.h"
31 #include "ddraw_private.h"
32 #include "dsurface/main.h"
33 #include "ddraw/main.h"
34 #include "dsurface/thunks.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
38 /** Creation/Destruction functions */
41 Main_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl
*This
,
43 const DDSURFACEDESC2
*pDDSD
)
45 TRACE("(%p)->(%p,%p)\n", This
, pDD
, pDDSD
);
47 if (pDDSD
!= &This
->surface_desc
) {
48 This
->surface_desc
.dwSize
= sizeof(This
->surface_desc
);
49 DD_STRUCT_COPY_BYSIZE(&(This
->surface_desc
),pDDSD
);
51 This
->uniqueness_value
= 1; /* unchecked */
54 This
->local
.lpSurfMore
= &This
->more
;
55 This
->local
.lpGbl
= &This
->global
;
56 This
->local
.dwProcessId
= GetCurrentProcessId();
57 This
->local
.dwFlags
= 0; /* FIXME */
58 This
->local
.ddsCaps
.dwCaps
= This
->surface_desc
.ddsCaps
.dwCaps
;
59 /* FIXME: more local stuff */
60 This
->more
.lpDD_lcl
= &pDD
->local
;
61 This
->more
.ddsCapsEx
.dwCaps2
= This
->surface_desc
.ddsCaps
.dwCaps2
;
62 This
->more
.ddsCapsEx
.dwCaps3
= This
->surface_desc
.ddsCaps
.dwCaps3
;
63 This
->more
.ddsCapsEx
.dwCaps4
= This
->surface_desc
.ddsCaps
.dwCaps4
;
64 /* FIXME: more more stuff */
65 This
->gmore
= &This
->global_more
;
66 This
->global
.u3
.lpDD
= pDD
->local
.lpGbl
;
67 /* FIXME: more global stuff */
69 This
->final_release
= Main_DirectDrawSurface_final_release
;
70 This
->late_allocate
= Main_DirectDrawSurface_late_allocate
;
71 This
->attach
= Main_DirectDrawSurface_attach
;
72 This
->detach
= Main_DirectDrawSurface_detach
;
73 This
->lock_update
= Main_DirectDrawSurface_lock_update
;
74 This
->unlock_update
= Main_DirectDrawSurface_unlock_update
;
75 This
->lose_surface
= Main_DirectDrawSurface_lose_surface
;
76 This
->set_palette
= Main_DirectDrawSurface_set_palette
;
77 This
->update_palette
= Main_DirectDrawSurface_update_palette
;
78 This
->get_display_window
= Main_DirectDrawSurface_get_display_window
;
79 This
->get_gamma_ramp
= Main_DirectDrawSurface_get_gamma_ramp
;
80 This
->set_gamma_ramp
= Main_DirectDrawSurface_set_gamma_ramp
;
82 ICOM_INIT_INTERFACE(This
, IDirectDrawSurface3
,
83 DDRAW_IDDS3_Thunk_VTable
);
84 ICOM_INIT_INTERFACE(This
, IDirectDrawGammaControl
,
87 /* There is no generic implementation of IDDS7 or texture */
89 Main_DirectDraw_AddSurface(pDD
, This
);
93 void Main_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl
* This
)
95 Main_DirectDraw_RemoveSurface(This
->ddraw_owner
, This
);
98 HRESULT
Main_DirectDrawSurface_late_allocate(IDirectDrawSurfaceImpl
* This
)
103 static void Main_DirectDrawSurface_Destroy(IDirectDrawSurfaceImpl
* This
)
106 IDirectDrawPalette_Release(ICOM_INTERFACE(This
->palette
, IDirectDrawPalette
));
107 This
->palette
= NULL
;
109 This
->final_release(This
);
110 if (This
->private != This
+1) HeapFree(GetProcessHeap(), 0, This
->private);
111 if (This
->tex_private
) HeapFree(GetProcessHeap(), 0, This
->tex_private
);
112 HeapFree(GetProcessHeap(), 0, This
);
115 void Main_DirectDrawSurface_ForceDestroy(IDirectDrawSurfaceImpl
* This
)
117 WARN("destroying surface %p with refcnt %lu\n", This
, This
->ref
);
118 Main_DirectDrawSurface_Destroy(This
);
121 ULONG WINAPI
Main_DirectDrawSurface_Release(LPDIRECTDRAWSURFACE7 iface
)
123 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
125 TRACE("(%p)->(): decreasing from %ld\n", This
, This
->ref
);
127 if (--This
->ref
== 0)
129 if (This
->aux_release
)
130 This
->aux_release(This
->aux_ctx
, This
->aux_data
);
131 Main_DirectDrawSurface_Destroy(This
);
133 TRACE("released surface %p\n", This
);
141 ULONG WINAPI
Main_DirectDrawSurface_AddRef(LPDIRECTDRAWSURFACE7 iface
)
143 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
145 TRACE("(%p)->(): increasing from %ld\n", This
, This
->ref
);
151 Main_DirectDrawSurface_QueryInterface(LPDIRECTDRAWSURFACE7 iface
, REFIID riid
,
154 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
155 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(riid
), ppObj
);
159 if (IsEqualGUID(&IID_IUnknown
, riid
)
160 || IsEqualGUID(&IID_IDirectDrawSurface7
, riid
)
161 || IsEqualGUID(&IID_IDirectDrawSurface4
, riid
))
164 *ppObj
= ICOM_INTERFACE(This
, IDirectDrawSurface7
);
167 else if (IsEqualGUID(&IID_IDirectDrawSurface
, riid
)
168 || IsEqualGUID(&IID_IDirectDrawSurface2
, riid
)
169 || IsEqualGUID(&IID_IDirectDrawSurface3
, riid
))
172 *ppObj
= ICOM_INTERFACE(This
, IDirectDrawSurface3
);
175 else if (IsEqualGUID(&IID_IDirectDrawGammaControl
, riid
))
178 *ppObj
= ICOM_INTERFACE(This
, IDirectDrawGammaControl
);
182 /* interfaces following here require OpenGL */
183 if( !opengl_initialized
)
184 return E_NOINTERFACE
;
186 if ( IsEqualGUID( &IID_D3DDEVICE_OpenGL
, riid
) ||
187 IsEqualGUID( &IID_IDirect3DHALDevice
, riid
) )
189 IDirect3DDeviceImpl
*d3ddevimpl
;
192 ret_value
= d3ddevice_create(&d3ddevimpl
, This
->ddraw_owner
, This
);
193 if (FAILED(ret_value
)) return ret_value
;
195 *ppObj
= ICOM_INTERFACE(d3ddevimpl
, IDirect3DDevice
);
196 TRACE(" returning Direct3DDevice interface at %p.\n", *ppObj
);
198 This
->ref
++; /* No idea if this is correct.. Need to check using real Windows */
201 else if (IsEqualGUID( &IID_IDirect3DTexture
, riid
) ||
202 IsEqualGUID( &IID_IDirect3DTexture2
, riid
))
204 HRESULT ret_value
= S_OK
;
206 if ((This
->surface_desc
.ddsCaps
.dwCaps
& DDSCAPS_TEXTURE
) == 0) return E_NOINTERFACE
;
208 /* In case the texture surface was created before the D3D creation */
209 if (This
->tex_private
== NULL
) {
210 if (This
->ddraw_owner
->d3d_private
== NULL
) {
211 ERR("Texture created with no D3D object yet.. Not supported !\n");
212 return E_NOINTERFACE
;
215 ret_value
= This
->ddraw_owner
->d3d_create_texture(This
->ddraw_owner
, This
, FALSE
, This
->mip_main
);
216 if (FAILED(ret_value
)) return ret_value
;
218 if (IsEqualGUID( &IID_IDirect3DTexture
, riid
)) {
219 *ppObj
= ICOM_INTERFACE(This
, IDirect3DTexture
);
220 TRACE(" returning Direct3DTexture interface at %p.\n", *ppObj
);
222 *ppObj
= ICOM_INTERFACE(This
, IDirect3DTexture2
);
223 TRACE(" returning Direct3DTexture2 interface at %p.\n", *ppObj
);
230 return E_NOINTERFACE
;
236 Main_DirectDrawSurface_attach(IDirectDrawSurfaceImpl
*This
,
237 IDirectDrawSurfaceImpl
*to
)
242 BOOL
Main_DirectDrawSurface_detach(IDirectDrawSurfaceImpl
*This
)
248 Main_DirectDrawSurface_lock_update(IDirectDrawSurfaceImpl
* This
, LPCRECT pRect
,
254 Main_DirectDrawSurface_unlock_update(IDirectDrawSurfaceImpl
* This
,
260 Main_DirectDrawSurface_lose_surface(IDirectDrawSurfaceImpl
* This
)
265 Main_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl
* This
,
266 IDirectDrawPaletteImpl
* pal
)
271 Main_DirectDrawSurface_update_palette(IDirectDrawSurfaceImpl
* This
,
272 IDirectDrawPaletteImpl
* pal
,
273 DWORD dwStart
, DWORD dwCount
,
274 LPPALETTEENTRY palent
)
279 Main_DirectDrawSurface_get_display_window(IDirectDrawSurfaceImpl
* This
)
285 Main_DirectDrawSurface_get_gamma_ramp(IDirectDrawSurfaceImpl
* This
,
287 LPDDGAMMARAMP lpGammaRamp
)
291 hr
= This
->get_dc(This
, &hDC
);
292 if (FAILED(hr
)) return hr
;
293 hr
= GetDeviceGammaRamp(hDC
, lpGammaRamp
) ? DD_OK
: DDERR_UNSUPPORTED
;
294 This
->release_dc(This
, hDC
);
299 Main_DirectDrawSurface_set_gamma_ramp(IDirectDrawSurfaceImpl
* This
,
301 LPDDGAMMARAMP lpGammaRamp
)
305 hr
= This
->get_dc(This
, &hDC
);
306 if (FAILED(hr
)) return hr
;
307 hr
= SetDeviceGammaRamp(hDC
, lpGammaRamp
) ? DD_OK
: DDERR_UNSUPPORTED
;
308 This
->release_dc(This
, hDC
);
313 /*** Interface functions */
316 Main_DirectDrawSurface_AddAttachedSurface(LPDIRECTDRAWSURFACE7 iface
,
317 LPDIRECTDRAWSURFACE7 pAttach
)
319 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
320 IDirectDrawSurfaceImpl
* surf
= ICOM_OBJECT(IDirectDrawSurfaceImpl
,
321 IDirectDrawSurface7
, pAttach
);
323 TRACE("(%p)->(%p)\n",This
,pAttach
);
325 /* Does windows check this? */
327 return DDERR_CANNOTATTACHSURFACE
; /* unchecked */
329 /* Does windows check this? */
330 if (surf
->ddraw_owner
!= This
->ddraw_owner
)
331 return DDERR_CANNOTATTACHSURFACE
; /* unchecked */
333 if (surf
->surface_owner
!= NULL
)
334 return DDERR_SURFACEALREADYATTACHED
; /* unchecked */
336 /* TODO MSDN: "You can attach only z-buffer surfaces with this method."
337 * But apparently backbuffers and mipmaps can be attached too. */
339 /* Set MIPMAPSUBLEVEL if this seems to be one */
340 if (This
->surface_desc
.ddsCaps
.dwCaps
&
341 surf
->surface_desc
.ddsCaps
.dwCaps
& DDSCAPS_MIPMAP
) {
342 surf
->surface_desc
.ddsCaps
.dwCaps2
|= DDSCAPS2_MIPMAPSUBLEVEL
;
343 /* FIXME: we should probably also add to dwMipMapCount of this
344 * and all parent surfaces (update create_texture if you do) */
347 /* Callback to allow the surface to do something special now that it is
348 * attached. (e.g. maybe the Z-buffer tells the renderer to use it.) */
349 if (!surf
->attach(surf
, This
))
350 return DDERR_CANNOTATTACHSURFACE
;
352 /* check: Where should it go in the chain? This puts it on the head. */
354 This
->attached
->prev_attached
= surf
;
355 surf
->next_attached
= This
->attached
;
356 surf
->prev_attached
= NULL
;
357 This
->attached
= surf
;
358 surf
->surface_owner
= This
;
360 IDirectDrawSurface7_AddRef(pAttach
);
365 /* MSDN: "not currently implemented." */
367 Main_DirectDrawSurface_AddOverlayDirtyRect(LPDIRECTDRAWSURFACE7 iface
,
370 TRACE("(%p)->(%p)\n",iface
,pRect
);
371 return DDERR_UNSUPPORTED
; /* unchecked */
374 /* MSDN: "not currently implemented." */
376 Main_DirectDrawSurface_BltBatch(LPDIRECTDRAWSURFACE7 iface
,
377 LPDDBLTBATCH pBatch
, DWORD dwCount
,
380 TRACE("(%p)->(%p,%ld,%08lx)\n",iface
,pBatch
,dwCount
,dwFlags
);
381 return DDERR_UNSUPPORTED
; /* unchecked */
385 Main_DirectDrawSurface_ChangeUniquenessValue(LPDIRECTDRAWSURFACE7 iface
)
387 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
388 volatile IDirectDrawSurfaceImpl
* vThis
= This
;
390 TRACE("(%p)\n",This
);
391 /* A uniquness value of 0 is apparently special.
392 * This needs to be checked. */
395 DWORD old_uniqueness_value
= vThis
->uniqueness_value
;
396 DWORD new_uniqueness_value
= old_uniqueness_value
+1;
398 if (old_uniqueness_value
== 0) break;
399 if (new_uniqueness_value
== 0) new_uniqueness_value
= 1;
401 if (InterlockedCompareExchange((LONG
*)&vThis
->uniqueness_value
,
402 old_uniqueness_value
,
403 new_uniqueness_value
)
404 == old_uniqueness_value
)
412 Main_DirectDrawSurface_DeleteAttachedSurface(LPDIRECTDRAWSURFACE7 iface
,
414 LPDIRECTDRAWSURFACE7 pAttach
)
416 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
417 IDirectDrawSurfaceImpl
* surf
= ICOM_OBJECT(IDirectDrawSurfaceImpl
,
418 IDirectDrawSurface7
, pAttach
);
420 TRACE("(%p)->(%08lx,%p)\n",This
,dwFlags
,pAttach
);
422 if (!surf
|| (surf
->surface_owner
!= This
))
423 return DDERR_SURFACENOTATTACHED
; /* unchecked */
427 /* Remove MIPMAPSUBLEVEL if this seemed to be one */
428 if (This
->surface_desc
.ddsCaps
.dwCaps
&
429 surf
->surface_desc
.ddsCaps
.dwCaps
& DDSCAPS_MIPMAP
) {
430 surf
->surface_desc
.ddsCaps
.dwCaps2
&= ~DDSCAPS2_MIPMAPSUBLEVEL
;
431 /* FIXME: we should probably also subtract from dwMipMapCount of this
432 * and all parent surfaces */
435 if (surf
->next_attached
)
436 surf
->next_attached
->prev_attached
= surf
->prev_attached
;
437 if (surf
->prev_attached
)
438 surf
->prev_attached
->next_attached
= surf
->next_attached
;
439 if (This
->attached
== surf
)
440 This
->attached
= surf
->next_attached
;
442 IDirectDrawSurface7_Release(pAttach
);
448 Main_DirectDrawSurface_EnumAttachedSurfaces(LPDIRECTDRAWSURFACE7 iface
,
450 LPDDENUMSURFACESCALLBACK7 cb
)
452 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
453 IDirectDrawSurfaceImpl
* surf
;
455 TRACE("(%p)->(%p,%p)\n",This
,context
,cb
);
457 for (surf
= This
->attached
; surf
!= NULL
; surf
= surf
->next_attached
)
459 /* check: != DDENUMRET_OK or == DDENUMRET_CANCEL? */
460 if (cb(ICOM_INTERFACE(surf
, IDirectDrawSurface7
), &surf
->surface_desc
,
461 context
) == DDENUMRET_CANCEL
)
469 Main_DirectDrawSurface_EnumOverlayZOrders(LPDIRECTDRAWSURFACE7 iface
,
470 DWORD dwFlags
, LPVOID context
,
471 LPDDENUMSURFACESCALLBACK7 cb
)
473 TRACE("(%p)->(%08lx,%p,%p)\n",iface
,dwFlags
,context
,cb
);
477 BOOL
Main_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl
* front
,
478 IDirectDrawSurfaceImpl
* back
,
481 /* uniqueness_value? */
482 /* This is necessary. But is it safe? */
484 HDC tmp
= front
->hDC
;
485 front
->hDC
= back
->hDC
;
490 BOOL tmp
= front
->dc_in_use
;
491 front
->dc_in_use
= back
->dc_in_use
;
492 back
->dc_in_use
= tmp
;
496 FLATPTR tmp
= front
->global
.fpVidMem
;
497 front
->global
.fpVidMem
= back
->global
.fpVidMem
;
498 back
->global
.fpVidMem
= tmp
;
502 ULONG_PTR tmp
= front
->global_more
.hKernelSurface
;
503 front
->global_more
.hKernelSurface
= back
->global_more
.hKernelSurface
;
504 back
->global_more
.hKernelSurface
= tmp
;
511 Main_DirectDrawSurface_Flip(LPDIRECTDRAWSURFACE7 iface
,
512 LPDIRECTDRAWSURFACE7 override
, DWORD dwFlags
)
514 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
515 IDirectDrawSurfaceImpl
* target
;
518 TRACE("(%p)->(%p,%08lx)\n",This
,override
,dwFlags
);
520 /* MSDN: "This method can be called only for a surface that has the
521 * DDSCAPS_FLIP and DDSCAPS_FRONTBUFFER capabilities." */
522 if ((This
->surface_desc
.ddsCaps
.dwCaps
&(DDSCAPS_FLIP
|DDSCAPS_FRONTBUFFER
))
523 != (DDSCAPS_FLIP
|DDSCAPS_FRONTBUFFER
))
524 return DDERR_NOTFLIPPABLE
;
527 if (This
->aux_flip(This
->aux_ctx
, This
->aux_data
))
530 /* 1. find the flip target */
531 /* XXX I don't think this algorithm works for more than 1 backbuffer. */
532 if (override
== NULL
)
534 static DDSCAPS2 back_caps
= { DDSCAPS_BACKBUFFER
};
535 LPDIRECTDRAWSURFACE7 tgt
;
537 hr
= IDirectDrawSurface7_GetAttachedSurface(iface
, &back_caps
, &tgt
);
538 if (FAILED(hr
)) return DDERR_NOTFLIPPABLE
; /* unchecked */
540 target
= ICOM_OBJECT(IDirectDrawSurfaceImpl
, IDirectDrawSurface7
,
542 IDirectDrawSurface7_Release(tgt
);
546 BOOL on_chain
= FALSE
;
547 IDirectDrawSurfaceImpl
* surf
;
549 /* MSDN: "The method fails if the specified [override] surface is not
550 * a member of the flipping chain." */
552 /* Verify that override is on this flip chain. We assume that
553 * surf is the head of the flipping chain, because it's the front
555 target
= ICOM_OBJECT(IDirectDrawSurfaceImpl
, IDirectDrawSurface7
,
558 /* Either target is (indirectly) attached to This or This is
559 * (indirectly) attached to target. */
560 for (surf
= target
; surf
!= NULL
; surf
= surf
->surface_owner
)
570 return DDERR_INVALIDPARAMS
; /* unchecked */
573 TRACE("flip to backbuffer: %p\n",target
);
574 if (This
->flip_data(This
, target
, dwFlags
))
575 This
->flip_update(This
, dwFlags
);
580 static PrivateData
* find_private_data(IDirectDrawSurfaceImpl
*This
,
584 for (data
= This
->private_data
; data
!= NULL
; data
= data
->next
)
586 if (IsEqualGUID(&data
->tag
, tag
)) break;
593 Main_DirectDrawSurface_FreePrivateData(LPDIRECTDRAWSURFACE7 iface
, REFGUID tag
)
595 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
598 data
= find_private_data(This
, tag
);
599 if (data
== NULL
) return DDERR_NOTFOUND
;
602 data
->prev
->next
= data
->next
;
604 data
->next
->prev
= data
->prev
;
606 if (data
->flags
& DDSPD_IUNKNOWNPTR
)
608 if (data
->ptr
.object
!= NULL
)
609 IUnknown_Release(data
->ptr
.object
);
612 HeapFree(GetProcessHeap(), 0, data
->ptr
.data
);
614 HeapFree(GetProcessHeap(), 0, data
);
620 Main_DirectDrawSurface_GetAttachedSurface(LPDIRECTDRAWSURFACE7 iface
,
622 LPDIRECTDRAWSURFACE7
* ppSurface
)
624 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
625 IDirectDrawSurfaceImpl
* surf
;
626 IDirectDrawSurfaceImpl
* found
= NULL
;
629 if (TRACE_ON(ddraw
)) {
630 TRACE("(%p)->Looking for caps: %lx,%lx,%lx,%lx output: %p\n",This
,pCaps
->dwCaps
, pCaps
->dwCaps2
,
631 pCaps
->dwCaps3
, pCaps
->dwCaps4
, ppSurface
);
632 TRACE(" Caps are : "); DDRAW_dump_DDSCAPS2(pCaps
); TRACE("\n");
636 if ((This
->ddraw_owner
->local
.dwLocalFlags
& DDRAWILCL_DIRECTDRAW7
) == 0) {
637 /* As this is not a DirectDraw7 application, remove the garbage that some games
638 put in the new fields of the DDSCAPS2 structure. */
639 our_caps
.dwCaps2
= 0;
640 our_caps
.dwCaps3
= 0;
641 our_caps
.dwCaps4
= 0;
642 if (TRACE_ON(ddraw
)) {
643 TRACE(" Real caps are : "); DDRAW_dump_DDSCAPS2(&our_caps
); TRACE("\n");
647 for (surf
= This
->attached
; surf
!= NULL
; surf
= surf
->next_attached
)
649 if (TRACE_ON(ddraw
)) {
650 TRACE("Surface: (%p) caps: %lx,%lx,%lx,%lx \n",surf
,
651 surf
->surface_desc
.ddsCaps
.dwCaps
,
652 surf
->surface_desc
.ddsCaps
.dwCaps2
,
653 surf
->surface_desc
.ddsCaps
.dwCaps3
,
654 surf
->surface_desc
.ddsCaps
.dwCaps4
);
655 TRACE(" Surface caps are : "); DDRAW_dump_DDSCAPS2(&(surf
->surface_desc
.ddsCaps
)); TRACE("\n");
657 if (((surf
->surface_desc
.ddsCaps
.dwCaps
& our_caps
.dwCaps
) == our_caps
.dwCaps
) &&
658 ((surf
->surface_desc
.ddsCaps
.dwCaps2
& our_caps
.dwCaps2
) == our_caps
.dwCaps2
))
660 /* MSDN: "This method fails if more than one surface is attached
661 * that matches the capabilities requested." */
664 FIXME("More than one attached surface matches requested caps. What should we do here?\n");
665 /* Previous code returned 'DDERR_NOTFOUND'. That appears not
666 to be correct, given what 3DMark expects from MipMapped surfaces.
667 We shall just continue instead. */
675 TRACE("Did not find any valid surface\n");
676 return DDERR_NOTFOUND
;
679 *ppSurface
= ICOM_INTERFACE(found
, IDirectDrawSurface7
);
681 if (TRACE_ON(ddraw
)) {
682 TRACE("Returning surface %p with description : \n", *ppSurface
);
683 DDRAW_dump_surface_desc(&(found
->surface_desc
));
686 /* XXX d3dframe.cpp sometimes AddRefs things that it gets from us. */
687 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(found
, IDirectDrawSurface7
));
692 Main_DirectDrawSurface_GetBltStatus(LPDIRECTDRAWSURFACE7 iface
, DWORD dwFlags
)
694 TRACE("(%p)->(%08lx)\n",iface
,dwFlags
);
699 case DDGBS_ISBLTDONE
:
703 return DDERR_INVALIDPARAMS
;
708 Main_DirectDrawSurface_GetCaps(LPDIRECTDRAWSURFACE7 iface
, LPDDSCAPS2 pCaps
)
710 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
712 TRACE("(%p)->(%p)\n",This
,pCaps
);
713 *pCaps
= This
->surface_desc
.ddsCaps
;
718 Main_DirectDrawSurface_GetClipper(LPDIRECTDRAWSURFACE7 iface
,
719 LPDIRECTDRAWCLIPPER
* ppClipper
)
721 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
723 TRACE("(%p)->(%p)\n",This
,ppClipper
);
724 if (This
->clipper
== NULL
)
725 return DDERR_NOCLIPPERATTACHED
;
727 *ppClipper
= ICOM_INTERFACE(This
->clipper
, IDirectDrawClipper
);
728 IDirectDrawClipper_AddRef(ICOM_INTERFACE(This
->clipper
,
729 IDirectDrawClipper
));
734 Main_DirectDrawSurface_GetColorKey(LPDIRECTDRAWSURFACE7 iface
, DWORD dwFlags
,
737 /* There is a DDERR_NOCOLORKEY error, but how do we know if a color key
738 * isn't there? That's like saying that an int isn't there. (Which MS
739 * has done in other docs.) */
741 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
743 TRACE("(%p)->(%08lx,%p)\n",This
,dwFlags
,pCKey
);
744 if (TRACE_ON(ddraw
)) {
745 TRACE(" - colorkey flags : ");
746 DDRAW_dump_colorkeyflag(dwFlags
);
752 *pCKey
= This
->surface_desc
.ddckCKDestBlt
;
755 case DDCKEY_DESTOVERLAY
:
756 *pCKey
= This
->surface_desc
.u3
.ddckCKDestOverlay
;
760 *pCKey
= This
->surface_desc
.ddckCKSrcBlt
;
763 case DDCKEY_SRCOVERLAY
:
764 *pCKey
= This
->surface_desc
.ddckCKSrcOverlay
;
768 return DDERR_INVALIDPARAMS
;
774 /* XXX We need to do something with the DC if the surface gets lost. */
776 Main_DirectDrawSurface_GetDC(LPDIRECTDRAWSURFACE7 iface
, HDC
*phDC
)
780 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
782 TRACE("(%p)->(%p)\n",This
,phDC
);
790 return DDERR_DCALREADYCREATED
;
794 * Strange: Lock lists DDERR_SURFACEBUSY as an error, meaning that another
795 * thread has it locked, but GetDC does not. */
796 ddsd
.dwSize
= sizeof(ddsd
);
797 hr
= IDirectDrawSurface7_Lock(iface
, NULL
, &ddsd
, 0, 0);
804 hr
= This
->get_dc(This
, &This
->hDC
);
807 TRACE("returning %p\n",This
->hDC
);
810 This
->dc_in_use
= TRUE
;
812 else WARN("No DC! Prepare for trouble\n");
819 Main_DirectDrawSurface_GetDDInterface(LPDIRECTDRAWSURFACE7 iface
, LPVOID
* pDD
)
821 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
823 TRACE("(%p)->(%p)\n",This
,pDD
);
824 *pDD
= ICOM_INTERFACE(This
->ddraw_owner
, IDirectDraw7
);
825 IDirectDraw7_AddRef(ICOM_INTERFACE(This
->ddraw_owner
, IDirectDraw7
));
830 Main_DirectDrawSurface_GetFlipStatus(LPDIRECTDRAWSURFACE7 iface
, DWORD dwFlags
)
832 /* XXX: DDERR_INVALIDSURFACETYPE */
834 TRACE("(%p)->(%08lx)\n",iface
,dwFlags
);
838 case DDGFS_ISFLIPDONE
:
842 return DDERR_INVALIDPARAMS
;
847 Main_DirectDrawSurface_GetLOD(LPDIRECTDRAWSURFACE7 iface
, LPDWORD pdwMaxLOD
)
849 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
851 TRACE("(%p)->(%p)\n",This
,pdwMaxLOD
);
854 *pdwMaxLOD
= This
->max_lod
;
859 Main_DirectDrawSurface_GetOverlayPosition(LPDIRECTDRAWSURFACE7 iface
,
860 LPLONG pX
, LPLONG pY
)
862 return DDERR_NOTAOVERLAYSURFACE
;
866 Main_DirectDrawSurface_GetPalette(LPDIRECTDRAWSURFACE7 iface
,
867 LPDIRECTDRAWPALETTE
* ppPalette
)
869 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
871 TRACE("(%p)->(%p)\n",This
,ppPalette
);
872 if (This
->palette
== NULL
)
873 return DDERR_NOPALETTEATTACHED
;
875 *ppPalette
= ICOM_INTERFACE(This
->palette
, IDirectDrawPalette
);
876 IDirectDrawPalette_AddRef(ICOM_INTERFACE(This
->palette
,
877 IDirectDrawPalette
));
882 Main_DirectDrawSurface_GetPixelFormat(LPDIRECTDRAWSURFACE7 iface
,
883 LPDDPIXELFORMAT pDDPixelFormat
)
885 /* What is DDERR_INVALIDSURFACETYPE for here? */
886 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
888 TRACE("(%p)->(%p)\n",This
,pDDPixelFormat
);
889 DD_STRUCT_COPY_BYSIZE(pDDPixelFormat
,&This
->surface_desc
.u4
.ddpfPixelFormat
);
894 Main_DirectDrawSurface_GetPriority(LPDIRECTDRAWSURFACE7 iface
,
897 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
899 TRACE("(%p)->(%p)\n",This
,pdwPriority
);
902 *pdwPriority
= This
->priority
;
907 Main_DirectDrawSurface_GetPrivateData(LPDIRECTDRAWSURFACE7 iface
,
908 REFGUID tag
, LPVOID pBuffer
,
909 LPDWORD pcbBufferSize
)
911 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
914 data
= find_private_data(This
, tag
);
915 if (data
== NULL
) return DDERR_NOTFOUND
;
917 /* This may not be right. */
918 if ((data
->flags
& DDSPD_VOLATILE
)
919 && data
->uniqueness_value
!= This
->uniqueness_value
)
920 return DDERR_EXPIRED
;
922 if (*pcbBufferSize
< data
->size
)
924 *pcbBufferSize
= data
->size
;
925 return DDERR_MOREDATA
;
928 if (data
->flags
& DDSPD_IUNKNOWNPTR
)
930 *(LPUNKNOWN
*)pBuffer
= data
->ptr
.object
;
931 IUnknown_AddRef(data
->ptr
.object
);
935 memcpy(pBuffer
, data
->ptr
.data
, data
->size
);
942 Main_DirectDrawSurface_GetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface
,
943 LPDDSURFACEDESC2 pDDSD
)
945 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
947 TRACE("(%p)->(%p)\n",This
,pDDSD
);
948 if ((pDDSD
->dwSize
< sizeof(DDSURFACEDESC
)) ||
949 (pDDSD
->dwSize
> sizeof(DDSURFACEDESC2
))) {
950 ERR("Impossible/Strange struct size %ld.\n",pDDSD
->dwSize
);
951 return DDERR_GENERIC
;
954 DD_STRUCT_COPY_BYSIZE(pDDSD
,&This
->surface_desc
);
955 if (TRACE_ON(ddraw
)) {
956 DDRAW_dump_surface_desc(pDDSD
);
962 Main_DirectDrawSurface_GetUniquenessValue(LPDIRECTDRAWSURFACE7 iface
,
965 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
967 TRACE("(%p)->(%p)\n",This
,pValue
);
968 *pValue
= This
->uniqueness_value
;
973 Main_DirectDrawSurface_Initialize(LPDIRECTDRAWSURFACE7 iface
,
974 LPDIRECTDRAW pDD
, LPDDSURFACEDESC2 pDDSD
)
976 TRACE("(%p)->(%p,%p)\n",iface
,pDD
,pDDSD
);
977 return DDERR_ALREADYINITIALIZED
;
981 Main_DirectDrawSurface_IsLost(LPDIRECTDRAWSURFACE7 iface
)
983 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
985 TRACE("(%p) is%s lost\n",This
, (This
->lost
? "" : " not"));
986 return This
->lost
? DDERR_SURFACELOST
: DD_OK
;
990 /* XXX This doesn't actually do any locking or keep track of the locked
991 * rectangles. The behaviour is poorly documented. */
993 Main_DirectDrawSurface_Lock(LPDIRECTDRAWSURFACE7 iface
, LPRECT prect
,
994 LPDDSURFACEDESC2 pDDSD
, DWORD flags
, HANDLE h
)
996 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
998 if (TRACE_ON(ddraw
)) {
999 TRACE("(%p)->Lock(%p,%p,%08lx,%08lx)\n",This
,prect
,pDDSD
,flags
,(DWORD
)h
);
1000 TRACE(" - locking flags : "); DDRAW_dump_lockflag(flags
);
1002 if (WARN_ON(ddraw
)) {
1003 if (flags
& ~(DDLOCK_WAIT
|DDLOCK_READONLY
|DDLOCK_WRITEONLY
)) {
1004 WARN(" - unsupported locking flag : "); DDRAW_dump_lockflag(flags
& ~(DDLOCK_WAIT
|DDLOCK_READONLY
|DDLOCK_WRITEONLY
));
1008 /* First, copy the Surface description */
1009 DD_STRUCT_COPY_BYSIZE(pDDSD
,&(This
->surface_desc
));
1011 /* Used to optimize the D3D Device locking */
1012 This
->lastlocktype
= flags
& (DDLOCK_READONLY
|DDLOCK_WRITEONLY
);
1014 /* If asked only for a part, change the surface pointer.
1015 * (Not documented.) */
1016 if (prect
!= NULL
) {
1017 TRACE(" lprect: %ldx%ld-%ldx%ld\n",
1018 prect
->left
,prect
->top
,prect
->right
,prect
->bottom
);
1019 /* First do some sanity checkings on the rectangle we receive.
1020 DungeonSiege seems to gives us once a very bad rectangle for example */
1021 if ((prect
->top
< 0) ||
1022 (prect
->left
< 0) ||
1023 (prect
->bottom
< 0) ||
1024 (prect
->right
< 0) ||
1025 (prect
->left
>= prect
->right
) ||
1026 (prect
->top
>= prect
->bottom
) ||
1027 (prect
->left
>= This
->surface_desc
.dwWidth
) ||
1028 (prect
->right
> This
->surface_desc
.dwWidth
) ||
1029 (prect
->top
>= This
->surface_desc
.dwHeight
) ||
1030 (prect
->bottom
> This
->surface_desc
.dwHeight
)) {
1031 ERR(" Invalid values in LPRECT !!!\n");
1032 return DDERR_INVALIDPARAMS
;
1035 This
->lock_update(This
, prect
, flags
);
1037 pDDSD
->lpSurface
= (char *)This
->surface_desc
.lpSurface
1038 + prect
->top
* This
->surface_desc
.u1
.lPitch
1039 + prect
->left
* GET_BPP(This
->surface_desc
);
1041 This
->lock_update(This
, NULL
, flags
);
1044 TRACE("locked surface returning description : \n");
1045 if (TRACE_ON(ddraw
)) DDRAW_dump_surface_desc(pDDSD
);
1051 Main_DirectDrawSurface_PageLock(LPDIRECTDRAWSURFACE7 iface
, DWORD dwFlags
)
1053 /* Some surface types should return DDERR_CANTPAGELOCK. */
1058 Main_DirectDrawSurface_PageUnlock(LPDIRECTDRAWSURFACE7 iface
, DWORD dwFlags
)
1060 /* Some surface types should return DDERR_CANTPAGEUNLOCK, and we should
1061 * keep track so we can return DDERR_NOTPAGELOCKED as appropriate. */
1066 Main_DirectDrawSurface_ReleaseDC(LPDIRECTDRAWSURFACE7 iface
, HDC hDC
)
1069 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
1071 TRACE("(%p)->(%p)\n",This
,hDC
);
1073 if (!This
->dc_in_use
|| This
->hDC
!= hDC
)
1074 return DDERR_INVALIDPARAMS
;
1076 This
->release_dc(This
, hDC
);
1078 hr
= IDirectDrawSurface7_Unlock(iface
, NULL
);
1079 if (FAILED(hr
)) return hr
;
1081 This
->dc_in_use
= FALSE
;
1090 Main_DirectDrawSurface_SetClipper(LPDIRECTDRAWSURFACE7 iface
,
1091 LPDIRECTDRAWCLIPPER pDDClipper
)
1093 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
1095 TRACE("(%p)->(%p)\n",This
,pDDClipper
);
1096 if (pDDClipper
== ICOM_INTERFACE(This
->clipper
, IDirectDrawClipper
))
1099 if (This
->clipper
!= NULL
)
1100 IDirectDrawClipper_Release(ICOM_INTERFACE(This
->clipper
,
1101 IDirectDrawClipper
));
1103 This
->clipper
= ICOM_OBJECT(IDirectDrawClipperImpl
, IDirectDrawClipper
,
1105 if (pDDClipper
!= NULL
)
1106 IDirectDrawClipper_AddRef(pDDClipper
);
1112 Main_DirectDrawSurface_SetColorKey(LPDIRECTDRAWSURFACE7 iface
,
1113 DWORD dwFlags
, LPDDCOLORKEY pCKey
)
1115 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
1117 TRACE("(%p)->(%08lx,%p)\n",This
,dwFlags
,pCKey
);
1119 if (TRACE_ON(ddraw
)) {
1120 TRACE(" - colorkey flags : ");
1121 DDRAW_dump_colorkeyflag(dwFlags
);
1124 if ((dwFlags
& DDCKEY_COLORSPACE
) != 0) {
1125 FIXME(" colorkey value not supported (%08lx) !\n", dwFlags
);
1126 return DDERR_INVALIDPARAMS
;
1129 /* TODO: investigate if this function can take multiple bits set at the same
1130 time (ie setting multiple colorkey values at the same time with only
1134 switch (dwFlags
& ~DDCKEY_COLORSPACE
) {
1135 case DDCKEY_DESTBLT
:
1136 This
->surface_desc
.ddckCKDestBlt
= *pCKey
;
1137 This
->surface_desc
.dwFlags
|= DDSD_CKDESTBLT
;
1140 case DDCKEY_DESTOVERLAY
:
1141 This
->surface_desc
.u3
.ddckCKDestOverlay
= *pCKey
;
1142 This
->surface_desc
.dwFlags
|= DDSD_CKDESTOVERLAY
;
1145 case DDCKEY_SRCOVERLAY
:
1146 This
->surface_desc
.ddckCKSrcOverlay
= *pCKey
;
1147 This
->surface_desc
.dwFlags
|= DDSD_CKSRCOVERLAY
;
1151 This
->surface_desc
.ddckCKSrcBlt
= *pCKey
;
1152 This
->surface_desc
.dwFlags
|= DDSD_CKSRCBLT
;
1156 return DDERR_INVALIDPARAMS
;
1159 switch (dwFlags
& ~DDCKEY_COLORSPACE
) {
1160 case DDCKEY_DESTBLT
:
1161 This
->surface_desc
.dwFlags
&= ~DDSD_CKDESTBLT
;
1164 case DDCKEY_DESTOVERLAY
:
1165 This
->surface_desc
.dwFlags
&= ~DDSD_CKDESTOVERLAY
;
1168 case DDCKEY_SRCOVERLAY
:
1169 This
->surface_desc
.dwFlags
&= ~DDSD_CKSRCOVERLAY
;
1173 This
->surface_desc
.dwFlags
&= ~DDSD_CKSRCBLT
;
1177 return DDERR_INVALIDPARAMS
;
1181 if (This
->aux_setcolorkey_cb
) This
->aux_setcolorkey_cb(This
, dwFlags
, pCKey
);
1187 Main_DirectDrawSurface_SetLOD(LPDIRECTDRAWSURFACE7 iface
, DWORD dwMaxLOD
)
1189 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
1191 TRACE("(%p)->(%08lx)\n",This
,dwMaxLOD
);
1192 CHECK_TEXTURE(This
);
1194 This
->max_lod
= dwMaxLOD
;
1199 Main_DirectDrawSurface_SetOverlayPosition(LPDIRECTDRAWSURFACE7 iface
,
1202 return DDERR_NOTAOVERLAYSURFACE
;
1206 Main_DirectDrawSurface_SetPalette(LPDIRECTDRAWSURFACE7 iface
,
1207 LPDIRECTDRAWPALETTE pPalette
)
1209 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
1211 TRACE("(%p)->(%p)\n",This
,pPalette
);
1212 if (pPalette
== ICOM_INTERFACE(This
->palette
, IDirectDrawPalette
))
1215 if (This
->palette
!= NULL
) {
1216 if (This
->surface_desc
.ddsCaps
.dwCaps
& DDSCAPS_PRIMARYSURFACE
)
1217 This
->palette
->global
.dwFlags
&= ~DDPCAPS_PRIMARYSURFACE
;
1218 IDirectDrawPalette_Release(ICOM_INTERFACE(This
->palette
,
1219 IDirectDrawPalette
));
1222 This
->palette
= ICOM_OBJECT(IDirectDrawPaletteImpl
, IDirectDrawPalette
,
1224 if (pPalette
!= NULL
) {
1225 IDirectDrawPalette_AddRef(pPalette
);
1226 if (This
->surface_desc
.ddsCaps
.dwCaps
& DDSCAPS_PRIMARYSURFACE
)
1227 This
->palette
->global
.dwFlags
|= DDPCAPS_PRIMARYSURFACE
;
1230 This
->set_palette(This
, This
->palette
);
1236 Main_DirectDrawSurface_SetPriority(LPDIRECTDRAWSURFACE7 iface
,
1239 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
1241 TRACE("(%p)->(%08lx)\n",This
,dwPriority
);
1242 CHECK_TEXTURE(This
);
1244 This
->priority
= dwPriority
;
1248 /* Be careful when locking this: it is risky to call the object's AddRef
1249 * or Release holding a lock. */
1251 Main_DirectDrawSurface_SetPrivateData(LPDIRECTDRAWSURFACE7 iface
,
1252 REFGUID tag
, LPVOID pData
,
1253 DWORD cbSize
, DWORD dwFlags
)
1256 ICOM_THIS(IDirectDrawSurfaceImpl
, iface
);
1258 data
= find_private_data(This
, tag
);
1261 data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
));
1262 if (data
== NULL
) return DDERR_OUTOFMEMORY
;
1265 data
->flags
= dwFlags
;
1266 data
->uniqueness_value
= This
->uniqueness_value
;
1268 if (dwFlags
& DDSPD_IUNKNOWNPTR
)
1270 data
->ptr
.object
= (LPUNKNOWN
)pData
;
1271 data
->size
= sizeof(LPUNKNOWN
);
1272 IUnknown_AddRef(data
->ptr
.object
);
1276 data
->ptr
.data
= HeapAlloc(GetProcessHeap(), 0, cbSize
);
1277 if (data
->ptr
.data
== NULL
)
1279 HeapFree(GetProcessHeap(), 0, data
);
1280 return DDERR_OUTOFMEMORY
;
1285 data
->next
= This
->private_data
;
1287 if (This
->private_data
)
1288 This
->private_data
->prev
= data
;
1289 This
->private_data
= data
;
1295 /* I don't actually know how windows handles this case. The only
1296 * reason I don't just call FreePrivateData is because I want to
1297 * guarantee SetPrivateData working when using LPUNKNOWN or data
1298 * that is no larger than the old data. */
1304 /* SetSurfaceDesc */
1307 Main_DirectDrawSurface_Unlock(LPDIRECTDRAWSURFACE7 iface
, LPRECT pRect
)
1309 ICOM_THIS(IDirectDrawSurfaceImpl
,iface
);
1311 TRACE("(%p)->Unlock(%p)\n",This
,pRect
);
1313 This
->unlock_update(This
, pRect
);
1314 if (This
->aux_unlock
)
1315 This
->aux_unlock(This
->aux_ctx
, This
->aux_data
, pRect
);
1321 Main_DirectDrawSurface_UpdateOverlay(LPDIRECTDRAWSURFACE7 iface
,
1323 LPDIRECTDRAWSURFACE7 pDstSurface
,
1324 LPRECT pDstRect
, DWORD dwFlags
,
1327 return DDERR_UNSUPPORTED
;
1330 /* MSDN: "not currently implemented." */
1332 Main_DirectDrawSurface_UpdateOverlayDisplay(LPDIRECTDRAWSURFACE7 iface
,
1335 return DDERR_UNSUPPORTED
;
1339 Main_DirectDrawSurface_UpdateOverlayZOrder(LPDIRECTDRAWSURFACE7 iface
,
1341 LPDIRECTDRAWSURFACE7 pDDSRef
)
1343 return DDERR_NOTAOVERLAYSURFACE
;