/*	DirectDrawSurface HAL driver
 *
 * Copyright 2001-2003 TransGaming Technologies Inc.
 */

#include "config.h"

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

#include "wine/debug.h"
#include "ddraw_private.h"
#include "ddraw/user.h"
#include "ddraw/hal.h"
#include "ddraw/main.h"
#include "dsurface/main.h"
#include "dsurface/dib.h"
#include "dsurface/user.h"
#include "dsurface/hal.h"
#include "d3ddevice/main.h"

WINE_DEFAULT_DEBUG_CHANNEL(ddraw);

static ICOM_VTABLE(IDirectDrawSurface7) HAL_IDirectDrawSurface7_VTable;

static inline BOOL HAL_IsUser(IDirectDrawSurfaceImpl* This)
{
    HAL_PRIV_VAR(priv, This);
    if (This->surface_desc.ddsCaps.dwCaps &
	(DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER | DDSCAPS_EXECUTEBUFFER))
	return FALSE;
    if (priv->hal.fb_addr)
	return FALSE;
#ifdef DO_USER
    return TRUE;
#else
    return FALSE;
#endif
}

static inline BOOL HAL_IsDIB(IDirectDrawSurfaceImpl* This)
{
    if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_EXECUTEBUFFER)
	return FALSE;
    if (This->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC)
	return FALSE;
    return TRUE;
}

static HRESULT HAL_DirectDrawSurface_create_surface(IDirectDrawSurfaceImpl* This,
						    IDirectDrawImpl* pDD)
{
    HAL_PRIV_VAR(priv, This);
    HAL_DDRAW_PRIV_VAR(ddpriv, pDD);
    LPDDRAWI_DIRECTDRAW_GBL dd_gbl = pDD->local.lpGbl;
    LPDDRAWI_DDRAWSURFACE_LCL local = &This->t.lcl;
    DDHAL_CREATESURFACEDATA data;
    HRESULT hr;

    data.lpDD = dd_gbl;
    data.lpDDSurfaceDesc = (LPDDSURFACEDESC)&This->surface_desc;
    data.lplpSList = &local;
    data.dwSCnt = 1;
    data.ddRVal = 0;
    data.CreateSurface = dd_gbl->lpDDCBtmp->HALDD.CreateSurface;
    hr = data.CreateSurface(&data);

    if (hr == DDHAL_DRIVER_HANDLED) {
	if (This->t.gbl.fpVidMem < 4) {
	    /* grab framebuffer data from current_mode */
	    priv->hal.fb_pitch = dd_gbl->vmiData.lDisplayPitch;
	    priv->hal.fb_vofs  = ddpriv->hal.next_vofs;
	    priv->hal.fb_addr  = ((LPBYTE)dd_gbl->vmiData.fpPrimary) +
				 dd_gbl->vmiData.lDisplayPitch * priv->hal.fb_vofs;
	    TRACE("vofs=%ld, addr=%p\n", priv->hal.fb_vofs, priv->hal.fb_addr);
	    ddpriv->hal.next_vofs += This->surface_desc.dwHeight;

	    This->t.gbl.fpVidMem = (FLATPTR)priv->hal.fb_addr;
	    This->t.gbl.u4.lPitch = priv->hal.fb_pitch;
	} else {
	    priv->hal.fb_pitch = This->t.gbl.u4.lPitch;
	    priv->hal.fb_addr  = (LPBYTE)This->t.gbl.fpVidMem;
	}
	This->surface_desc.lpSurface = (LPVOID)This->t.gbl.fpVidMem;
	if (HAL_IsDIB(This)) {
	    This->surface_desc.u1.lPitch = This->t.gbl.u4.lPitch;
	    This->surface_desc.dwFlags |= DDSD_PITCH;
	    TRACE("got HAL-allocated surfaced at %p, pitch %ld\n", This->surface_desc.lpSurface, This->surface_desc.u1.lPitch);
	} else {
	    This->surface_desc.u1.dwLinearSize = This->t.gbl.u4.dwLinearSize;
	    This->surface_desc.dwFlags |= DDSD_LINEARSIZE;
	    TRACE("got HAL-allocated surfaced at %p, size %ld\n", This->surface_desc.lpSurface, This->surface_desc.u1.dwLinearSize);
	}
    }
    else {
	priv->hal.need_late = TRUE;
	/* the HAL may allocate a DIB section even if it wants late allocation */
	if (HAL_DIB(This)) {
	    This->surface_desc.lpSurface = (LPVOID)This->t.gbl.fpVidMem;
	    This->surface_desc.u1.lPitch = This->t.gbl.u4.lPitch;
	    This->surface_desc.dwFlags |= DDSD_PITCH;
	    TRACE("got HAL-allocated DIB section at %p, pitch %ld\n", This->surface_desc.lpSurface, This->surface_desc.u1.lPitch);
	}
    }

    return data.ddRVal;
}

static HRESULT HAL_DirectDrawSurface_destroy_surface(IDirectDrawSurfaceImpl* This)
{
    LPDDRAWI_DIRECTDRAW_GBL dd_gbl = This->t.more.lpDD_lcl->lpGbl;
    DDHAL_DESTROYSURFACEDATA data;

    if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE) {
	Main_Direct3DDevice_req_demolish_texture(This);
    }

    /* destroy HAL surface */
    data.lpDD = dd_gbl;
    data.lpDDSurface = &This->t.lcl;
    data.ddRVal = 0;
    data.DestroySurface = dd_gbl->lpDDCBtmp->HALDDSurface.DestroySurface;
    data.DestroySurface(&data);

    return data.ddRVal;
}

HRESULT
HAL_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl* This,
				IDirectDrawImpl* pDD,
				const DDSURFACEDESC2* pDDSD)
{
    HAL_PRIV_VAR(priv, This);
    LPDDRAWI_DIRECTDRAW_GBL dd_gbl = pDD->local.lpGbl;
    HRESULT hr;

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

    /* copy surface_desc, we may want to modify it before DIB construction */
    This->surface_desc = *pDDSD;

    /* the driver may want to dereference these pointers */
    This->t.lcl.lpSurfMore = &This->t.more;
    This->t.lcl.lpGbl = &This->t.gbl;
    This->t.gmore = &This->t.gbl_more;

    if (This->surface_desc.ddsCaps.dwCaps &
	(DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER | DDSCAPS_OFFSCREENPLAIN)) {
	hr = HAL_DirectDrawSurface_create_surface(This, pDD);
	if (FAILED(hr)) return hr;

	if (HAL_IsDIB(This)) {
	    hr = DIB_DirectDrawSurface_Construct(This, pDD, &This->surface_desc);
	    if (FAILED(hr)) return hr;
	} else {
	    hr = Main_DirectDrawSurface_Construct(This, pDD, &This->surface_desc);
	    if (FAILED(hr)) return hr;

	    DirectDraw_Allocate_Memory(This->ddraw_owner, This->surface_desc.u1.dwLinearSize);

	    if (priv->hal.need_late && !HAL_DIB(This)) {
		/* allocate linear memory */
		LPVOID data = HeapAlloc(surface_heap, 0, This->surface_desc.u1.dwLinearSize);
		/* we no longer need the lpSurface, set it to NULL since it's still
		 * relocatable (because a DIB section isn't allocated for it yet) */
		This->surface_desc.lpSurface = NULL;
		This->t.gbl.fpVidMem = (FLATPTR)data;
		This->t.gbl.u4.dwLinearSize = This->surface_desc.u1.dwLinearSize;
		priv->hal.own_memory = TRUE;
	    }
	}
    }
    else if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_EXECUTEBUFFER) {
	FIXME("create execute buffer\n");
	return DDERR_GENERIC;
    }
    else {
	if ((pDD->local.dwLocalFlags & DDRAWILCL_ISFULLSCREEN) && !(dd_gbl->dwFlags & DDRAWI_MODECHANGED)) {
	    /* force a mode set (HALs like DGA may need it) */
	    hr = HAL_DirectDraw_SetDisplayMode(ICOM_INTERFACE(pDD, IDirectDraw7),
					       pDD->width, pDD->height,
					       pDD->pixelformat.u1.dwRGBBitCount,
					       0, 0);
	    if (FAILED(hr)) return hr;
	}

#ifdef BAD_HAL
	if (dd_gbl->vmiData.fpPrimary) {
#endif
	    hr = HAL_DirectDrawSurface_create_surface(This, pDD);
	    if (FAILED(hr)) return hr;

	    hr = DIB_DirectDrawSurface_Construct(This, pDD, &This->surface_desc);
	    if (FAILED(hr)) return hr;
#ifdef BAD_HAL
	} else {
#ifdef DO_USER
	    /* no framebuffer, construct User-based primary */
	    hr = User_DirectDrawSurface_Construct(This, pDD, pDDSD);
	    if (FAILED(hr)) return hr;
#else
	    /* no framebuffer, construct DIB primary */
	    hr = DIB_DirectDrawSurface_Construct(This, pDD, pDDSD);
	    if (FAILED(hr)) return hr;
#endif

	    /* must notify HAL *after* creating User-based primary */
	    /* (or use CreateSurfaceEx, which we don't yet) */
	    hr = HAL_DirectDrawSurface_create_surface(This, pDD);
	    if (FAILED(hr)) return hr;

	}
#endif
	priv->hal.need_late = FALSE;
	priv->hal.cache_dc = TRUE;
    }

    ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
			HAL_IDirectDrawSurface7_VTable);

    This->final_release = HAL_DirectDrawSurface_final_release;
    This->late_allocate = HAL_DirectDrawSurface_late_allocate;
    This->duplicate_surface = HAL_DirectDrawSurface_duplicate_surface;

    This->lock_update   = HAL_DirectDrawSurface_lock_update;
    This->unlock_update = HAL_DirectDrawSurface_unlock_update;

    This->flip_data   = HAL_DirectDrawSurface_flip_data;
    This->flip_update = HAL_DirectDrawSurface_flip_update;

    This->set_palette    = HAL_DirectDrawSurface_set_palette;

    This->get_display_window = HAL_DirectDrawSurface_get_display_window;

    This->lose_surface = HAL_DirectDrawSurface_lose_surface;

    // check if we want to cache the DC  
    if (priv->hal.cache_dc) {
      TRACE("HAL caching DC\n");
      This->get_dc = HAL_DirectDrawSurface_get_dc;
      This->release_dc = HAL_DirectDrawSurface_release_dc;
      return DIB_DirectDrawSurface_alloc_dc(This, &priv->hal.cached_dc);
    }

    return DD_OK;
}

HRESULT
HAL_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(HAL_DirectDrawSurfaceImpl));
    if (This == NULL) return E_OUTOFMEMORY;

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

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

    return hr;
}

void HAL_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
{
    HAL_PRIV_VAR(priv, This);

    if (HAL_DIB(This)) {
        /* in case the surface data was relocated to a DIB section elsewhere */
        priv->hal.own_memory = FALSE;
    }

    HAL_DirectDrawSurface_destroy_surface(This);

    if (priv->hal.cache_dc) {
        TRACE("HAL freeing cached DC\n");
        DIB_DirectDrawSurface_free_dc(This, priv->hal.cached_dc);
	priv->hal.cache_dc = FALSE;
    }
      
    if (HAL_IsUser(This)) {
	User_DirectDrawSurface_final_release(This);
    }
    else if (HAL_IsDIB(This)) {
	DIB_DirectDrawSurface_final_release(This);
    }
    else {
	DirectDraw_Deallocate_Memory(This->ddraw_owner, This->surface_desc.u1.dwLinearSize);
	Main_DirectDrawSurface_final_release(This);
	if (priv->hal.own_memory) {
	    if (This->t.gbl.fpVidMem)
		HeapFree(surface_heap, 0, (LPVOID)This->t.gbl.fpVidMem);
	}
    }

    if( HAL_DIB(This) ) {
	DeleteObject( HAL_DIB(This) );
	HAL_DIB(This) = 0;
    }
}

HRESULT HAL_DirectDrawSurface_late_allocate(IDirectDrawSurfaceImpl* This)
{
    HAL_PRIV_VAR(priv, This);
    if (priv->hal.need_late) {
	priv->hal.need_late = FALSE;
	return HAL_DirectDrawSurface_create_surface(This, This->ddraw_owner);
    }
    return DD_OK;
}

void HAL_DirectDrawSurface_set_palette(IDirectDrawSurfaceImpl* This,
				       IDirectDrawPaletteImpl* pal)
{
    LPDDRAWI_DIRECTDRAW_GBL dd_gbl = This->t.more.lpDD_lcl->lpGbl;
    DDHAL_SETPALETTEDATA data;

    DIB_DirectDrawSurface_set_palette(This, pal);
    data.lpDD = dd_gbl;
    data.lpDDSurface = &This->t.lcl;
    data.lpDDPalette = pal ? &pal->global : NULL;
    data.ddRVal = 0;
    data.Attach = TRUE; /* what's this? */
    data.SetPalette = dd_gbl->lpDDCBtmp->HALDDSurface.SetPalette;
    if (data.SetPalette)
	data.SetPalette(&data);
}

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

LPVOID HAL_DirectDrawSurface_lock_update(IDirectDrawSurfaceImpl* This,
					 LPCRECT pRect, DWORD dwFlags)
{
    LPDDRAWI_DIRECTDRAW_GBL dd_gbl = This->t.more.lpDD_lcl->lpGbl;
    DDHAL_LOCKDATA	data;

    data.lpDD		= dd_gbl;
    data.lpDDSurface	= &This->t.lcl;
    data.ddRVal		= 0;
    data.lpSurfData	= (LPVOID)This->t.gbl.fpVidMem; /* FIXME: correct? */
    if (pRect) {
	data.rArea.top	= pRect->top;
	data.rArea.bottom	= pRect->bottom;
	data.rArea.left	= pRect->left;
	data.rArea.right	= pRect->right;
	data.bHasRect 	= TRUE;
    } else {
	data.bHasRect 	= FALSE;
    }
    data.dwFlags	= dwFlags;

    data.Lock		= dd_gbl->lpDDCBtmp->HALDDSurface.Lock;
    if (data.Lock && (data.Lock(&data) == DDHAL_DRIVER_HANDLED))
	return data.lpSurfData;

    if (HAL_IsUser(This)) {
	return User_DirectDrawSurface_lock_update(This, pRect, dwFlags);
    } else {
	return Main_DirectDrawSurface_lock_update(This, pRect, dwFlags);
    }
}

void HAL_DirectDrawSurface_unlock_update(IDirectDrawSurfaceImpl* This,
					 LPCRECT pRect)
{
    LPDDRAWI_DIRECTDRAW_GBL dd_gbl = This->t.more.lpDD_lcl->lpGbl;
    DDHAL_UNLOCKDATA	data;

    if (This->surface_desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE) {
	Main_Direct3DDevice_req_invalidate_texture(This, pRect);
    }

    data.lpDD		= dd_gbl;
    data.lpDDSurface	= &This->t.lcl;
    data.ddRVal		= 0;
    data.Unlock		= dd_gbl->lpDDCBtmp->HALDDSurface.Unlock;
    if (data.Unlock && (data.Unlock(&data) == DDHAL_DRIVER_HANDLED))
	return;

    if (HAL_IsUser(This)) {
	User_DirectDrawSurface_unlock_update(This, pRect);
    } else {
	Main_DirectDrawSurface_unlock_update(This, pRect);
    }
}

BOOL HAL_DirectDrawSurface_flip_data(IDirectDrawSurfaceImpl* front,
				     IDirectDrawSurfaceImpl* back,
				     DWORD dwFlags)
{
    HAL_PRIV_VAR(front_priv, front);
    HAL_PRIV_VAR(back_priv, back);
    LPDDRAWI_DIRECTDRAW_GBL dd_gbl = front->t.more.lpDD_lcl->lpGbl;
    DDHAL_FLIPDATA data;
    BOOL ret;

    /* system memory primary surfaces don't actually flip,
     * they just copy the back buffer to the display */   
    if ((front->surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY) &&
	(front->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)) {
	LPVOID front_data;

	if (HAL_IsUser(front))
	    return User_DirectDrawSurface_flip_data(front, back, dwFlags);

	TRACE("performing blit to surface %p from %p\n", front, back);
	front_data = front->lock_update(front, NULL, DDLOCK_WAIT | DDLOCK_WRITEONLY);
	memcpy(front_data,
	       (LPVOID)back->t.gbl.fpVidMem,
	       front->t.gbl.wHeight *
	       front->t.gbl.u4.lPitch);
	front->unlock_update(front, NULL);
	return TRUE;
    }

    {
	DWORD tmp;
	tmp = front_priv->hal.fb_vofs;
	front_priv->hal.fb_vofs = back_priv->hal.fb_vofs;
	back_priv->hal.fb_vofs = tmp;
    }
    {
	LPVOID tmp;
	tmp = front_priv->hal.fb_addr;
	front_priv->hal.fb_addr = back_priv->hal.fb_addr;
	back_priv->hal.fb_addr = tmp;
    }

    assert( !(front_priv->hal.cache_dc ^ back_priv->hal.cache_dc) );
    {   
        HDC tmp;
	tmp = front_priv->hal.cached_dc;
	front_priv->hal.cached_dc = back_priv->hal.cached_dc;
	back_priv->hal.cached_dc = tmp;
    }

    if (HAL_IsUser(front)) {
	ret = User_DirectDrawSurface_flip_data(front, back, dwFlags);
    }
    else if (HAL_IsDIB(front)) {
	ret = DIB_DirectDrawSurface_flip_data(front, back, dwFlags);
    }
    else {
	ret = Main_DirectDrawSurface_flip_data(front, back, dwFlags);
    }

    TRACE("(%p,%p)\n",front,back);
    data.lpDD = dd_gbl;
    data.lpSurfCurr = &front->t.lcl;
    data.lpSurfTarg = &back->t.lcl;
    data.lpSurfCurrLeft = NULL;
    data.lpSurfTargLeft = NULL;
    data.dwFlags = dwFlags;
    data.ddRVal = 0;
    data.Flip = dd_gbl->lpDDCBtmp->HALDDSurface.Flip;
    if (data.Flip)
	if (data.Flip(&data) == DDHAL_DRIVER_HANDLED) ret = FALSE;

    return ret;
}

void HAL_DirectDrawSurface_flip_update(IDirectDrawSurfaceImpl* This, DWORD dwFlags)
{
    if (HAL_IsUser(This)) {
	User_DirectDrawSurface_flip_update(This, dwFlags);
    }
}

HRESULT HAL_DirectDrawSurface_get_dc(IDirectDrawSurfaceImpl* This, HDC* phDC)
{
    HAL_PRIV_VAR(priv, This);

    *phDC = priv->hal.cached_dc;
    return S_OK;
}

HRESULT HAL_DirectDrawSurface_release_dc(IDirectDrawSurfaceImpl* This,
					  HDC hDC)
{
    return S_OK;
}

HWND HAL_DirectDrawSurface_get_display_window(IDirectDrawSurfaceImpl* This)
{
    /* there's a potential drawable in the ddraw object's current_mode->pixmap...
     * perhaps it's possible to use it for the Direct3D rendering as well? */
    return 0;
}

static void lose_surface( IDirectDrawSurfaceImpl* This )
{
    /* Don't lose surfaces more than once (just in case) */
    if( This->lost ) return; 

    /* FIXME: Is this sufficient? */
    HAL_DirectDrawSurface_destroy_surface(This);

    This->lost = TRUE;
}

void HAL_DirectDrawSurface_lose_surface(IDirectDrawSurfaceImpl* This)
{
    LPATTACHLIST next;

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

    if( This == NULL ) return;

    /* FIXME: Toast appropriate surfaces - what's appropriate to lose? Video only? */

    next = This->t.lcl.lpAttachList;
    while (next)
    {
        IDirectDrawSurfaceImpl* surf = OBJECT_FROM_FIELD(IDirectDrawSurfaceImpl, t.lcl, next->lpAttached);
        next = next->lpLink;
        if (surf == This) break;
        if( surf->implicit )
        {
            TRACE( "Next surface is: %p\n", surf );
            lose_surface( surf );
        }
    }

    lose_surface( This );
}

static void restore_surface(IDirectDrawSurfaceImpl* This)
{
    HAL_DirectDrawSurface_create_surface( This, This->ddraw_owner );
    This->lost = FALSE;
}

HRESULT WINAPI
HAL_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
{
    ICOM_THIS(IDirectDrawSurfaceImpl,iface);
    HRESULT hr;
    LPATTACHLIST next;

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

    /* Are we in the right mode to restore? */
    hr = Main_DirectDraw_TestCooperativeLevel( ICOM_INTERFACE(This->ddraw_owner, IDirectDraw7) );
    if( FAILED(hr) ) return hr;

    /* Restore implicit attached surfaces only. FIXME: Better way of keeping track of what's implicit and what is not */

    next = This->t.lcl.lpAttachList;
    while (next)
    {
	IDirectDrawSurfaceImpl* surf = OBJECT_FROM_FIELD(IDirectDrawSurfaceImpl, t.lcl, next->lpAttached);
	next = next->lpLink;
	if (surf == This) break;
        if( surf->implicit && surf->lost )
        {
            TRACE( "Next surface is: %p\n", surf );
            restore_surface( surf );
        }
    }

    /* Restore only implicit surfaces */
    restore_surface( This );

    return DD_OK;
}


HRESULT WINAPI
HAL_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
				     LPDDSURFACEDESC2 pDDSD, DWORD dwFlags)
{
    ICOM_THIS(IDirectDrawSurfaceImpl,iface);
    HRESULT hr = DD_OK;

    TRACE("(%p)->(%p,%08lx)\n",iface,pDDSD,dwFlags);
    if (pDDSD->dwFlags & DDSD_LPSURFACE) {
	hr = HAL_DirectDrawSurface_destroy_surface(This);
	if (FAILED(hr)) return hr;
	hr = DIB_DirectDrawSurface_SetSurfaceDesc(iface, pDDSD, dwFlags);
	if (FAILED(hr)) ERR("DIB SetSurfaceDesc failed\n");
	This->surface_desc.dwFlags &= ~(DDSD_PITCH | DDSD_LINEARSIZE);
	This->surface_desc.dwFlags |= DDSD_LPSURFACE;
	hr = HAL_DirectDrawSurface_create_surface(This, This->ddraw_owner);
	return hr;
    }
    else {
	FIXME("flags=%08lx\n",pDDSD->dwFlags);
	abort();
	hr = E_FAIL;
    }
    return hr;
}

static ICOM_VTABLE(IDirectDrawSurface7) HAL_IDirectDrawSurface7_VTable =
{
    Main_DirectDrawSurface_QueryInterface,
    Main_DirectDrawSurface_AddRef,
    Main_DirectDrawSurface_Release,
    Main_DirectDrawSurface_AddAttachedSurface,
    Main_DirectDrawSurface_AddOverlayDirtyRect,
    DIB_DirectDrawSurface_Blt,
    Main_DirectDrawSurface_BltBatch,
    DIB_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,
    Main_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,
    Main_DirectDrawSurface_ReleaseDC,
    HAL_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,
    HAL_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
};
