/*		DirectDraw/Direct3D ExeBuffer stand in
 *
 * Copyright 2001 TransGaming Technologies Inc.
 *
 * This class provides a DirectDrawSurface implementation that represents
 * an ExeBuffer surface. This surface is not supposed to be accessed directly
 * by the application, only through the IDirect3DExecuteBuffer interface, so
 * we don't need to do much here.
 */

#include "config.h"

#include <stdlib.h>
#include <assert.h>

#include "ddraw.h"
#include "d3d.h"

#include "wine/debug.h"

#include "ddcomimpl.h"
#include "ddraw_private.h"
#include "dsurface/main.h"
#include "dsurface/exebuffer.h"

WINE_DEFAULT_DEBUG_CHANNEL(ddraw);

static ICOM_VTABLE(IDirectDrawSurface7) ExeBuffer_IDirectDrawSurface7_VTable;

HRESULT ExeBuffer_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
					      IDirectDrawImpl *pDD,
					      const DDSURFACEDESC2 *pDDSD)
{
    EXEBUFFER_PRIV_VAR(priv, This);
    HRESULT hr;

    TRACE("(%p,%p,%p)\n",This,pDD,pDDSD);

    assert(pDDSD->ddsCaps.dwCaps & DDSCAPS_EXECUTEBUFFER);

    hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
    if (FAILED(hr)) return hr;

    ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
			ExeBuffer_IDirectDrawSurface7_VTable);

    This->final_release = ExeBuffer_DirectDrawSurface_final_release;
    This->duplicate_surface = ExeBuffer_DirectDrawSurface_duplicate_surface;

    if (This->surface_desc.dwFlags & DDSD_LPSURFACE)
    {
	priv->exebuffer.client_memory = TRUE;
    }
    else
    {
	This->surface_desc.lpSurface
	    = VirtualAlloc(NULL, This->surface_desc.u1.dwLinearSize,
			   MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

	if (This->surface_desc.lpSurface == NULL)
	{
	    Main_DirectDrawSurface_final_release(This);
	    return HRESULT_FROM_WIN32(GetLastError());
	}

	priv->exebuffer.client_memory = FALSE;
    }

    This->t.gbl.fpVidMem = (FLATPTR)This->surface_desc.lpSurface;
    This->t.gbl.u4.dwLinearSize = This->surface_desc.u1.dwLinearSize;

    return DD_OK;
}

/* Not an API */
HRESULT ExeBuffer_DirectDrawSurface_Create(IDirectDrawImpl* pDD,
					   const DDSURFACEDESC2* pDDSD,
					   LPDIRECTDRAWSURFACE7* ppSurf,
					   IUnknown* pUnkOuter)
{
    IDirectDrawSurfaceImpl* This;
    HRESULT hr;
    assert(pUnkOuter == NULL);

    This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
		     sizeof(*This) + sizeof(ExeBuffer_DirectDrawSurfaceImpl));
    if (This == NULL) return E_OUTOFMEMORY;

    This->private = (ExeBuffer_DirectDrawSurfaceImpl*)(This+1);

    hr = ExeBuffer_DirectDrawSurface_Construct(This, pDD, pDDSD);
    if (FAILED(hr))
	HeapFree(GetProcessHeap(), 0, This);
    else
	*ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);

    return hr;
}

void
ExeBuffer_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
{
    EXEBUFFER_PRIV_VAR(priv, This);

    if (!priv->exebuffer.client_memory)
	VirtualFree(This->surface_desc.lpSurface, 0, MEM_RELEASE);

    return Main_DirectDrawSurface_final_release(This);
}

HRESULT
ExeBuffer_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
					      LPDIRECTDRAWSURFACE7* ppDup)
{
    return ExeBuffer_DirectDrawSurface_Create(This->ddraw_owner,
					      &This->surface_desc, ppDup,
					      NULL);
}

/* put your breakpoint/abort call here */
static HRESULT cant_do_that(const char *s)
{
    FIXME("attempt to %s execute buffer\n", s);
    return DDERR_UNSUPPORTED;
}

HRESULT WINAPI
ExeBuffer_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
				LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
				DWORD dwFlags, LPDDBLTFX lpbltfx)
{
    return cant_do_that("blt to a");
}

HRESULT WINAPI
ExeBuffer_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
				    DWORD dsty, LPDIRECTDRAWSURFACE7 src,
				    LPRECT rsrc, DWORD trans)
{
    return cant_do_that("bltfast to a");
}

HRESULT WINAPI
ExeBuffer_DirectDrawSurface_GetDC(LPDIRECTDRAWSURFACE7 iface, HDC *phDC)
{
    return cant_do_that("get a DC for a");
}

HRESULT WINAPI
ExeBuffer_DirectDrawSurface_ReleaseDC(LPDIRECTDRAWSURFACE7 iface, HDC hDC)
{
    return cant_do_that("release a DC for a");
}

HRESULT WINAPI
ExeBuffer_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
{
    return DD_OK;
}

HRESULT WINAPI
ExeBuffer_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
					   LPDDSURFACEDESC2 pDDSD,
					   DWORD dwFlags)
{
    /* XXX */
    abort();
    return E_FAIL;
}


static ICOM_VTABLE(IDirectDrawSurface7) ExeBuffer_IDirectDrawSurface7_VTable=
{
    Main_DirectDrawSurface_QueryInterface,
    Main_DirectDrawSurface_AddRef,
    Main_DirectDrawSurface_Release,
    Main_DirectDrawSurface_AddAttachedSurface,
    Main_DirectDrawSurface_AddOverlayDirtyRect,
    ExeBuffer_DirectDrawSurface_Blt,
    Main_DirectDrawSurface_BltBatch,
    ExeBuffer_DirectDrawSurface_BltFast,
    Main_DirectDrawSurface_DeleteAttachedSurface,
    Main_DirectDrawSurface_EnumAttachedSurfaces,
    Main_DirectDrawSurface_EnumOverlayZOrders,
    Main_DirectDrawSurface_Flip,
    Main_DirectDrawSurface_GetAttachedSurface,
    Main_DirectDrawSurface_GetBltStatus,
    Main_DirectDrawSurface_GetCaps,
    Main_DirectDrawSurface_GetClipper,
    Main_DirectDrawSurface_GetColorKey,
    ExeBuffer_DirectDrawSurface_GetDC,
    Main_DirectDrawSurface_GetFlipStatus,
    Main_DirectDrawSurface_GetOverlayPosition,
    Main_DirectDrawSurface_GetPalette,
    Main_DirectDrawSurface_GetPixelFormat,
    Main_DirectDrawSurface_GetSurfaceDesc,
    Main_DirectDrawSurface_Initialize,
    Main_DirectDrawSurface_IsLost,
    Main_DirectDrawSurface_Lock,
    ExeBuffer_DirectDrawSurface_ReleaseDC,
    ExeBuffer_DirectDrawSurface_Restore,
    Main_DirectDrawSurface_SetClipper,
    Main_DirectDrawSurface_SetColorKey,
    Main_DirectDrawSurface_SetOverlayPosition,
    Main_DirectDrawSurface_SetPalette,
    Main_DirectDrawSurface_Unlock,
    Main_DirectDrawSurface_UpdateOverlay,
    Main_DirectDrawSurface_UpdateOverlayDisplay,
    Main_DirectDrawSurface_UpdateOverlayZOrder,
    Main_DirectDrawSurface_GetDDInterface,
    Main_DirectDrawSurface_PageLock,
    Main_DirectDrawSurface_PageUnlock,
    ExeBuffer_DirectDrawSurface_SetSurfaceDesc,
    Main_DirectDrawSurface_SetPrivateData,
    Main_DirectDrawSurface_GetPrivateData,
    Main_DirectDrawSurface_FreePrivateData,
    Main_DirectDrawSurface_GetUniquenessValue,
    Main_DirectDrawSurface_ChangeUniquenessValue,
    Main_DirectDrawSurface_SetPriority,
    Main_DirectDrawSurface_GetPriority,
    Main_DirectDrawSurface_SetLOD,
    Main_DirectDrawSurface_GetLOD
};
