2007-12-30 21:14:20 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* PURPOSE: Native driver for dxg implementation
|
2015-11-10 17:41:55 +00:00
|
|
|
* FILE: win32ss/reactx/dxg/ddhmg.c
|
2007-12-30 21:14:20 +00:00
|
|
|
* PROGRAMER: Magnus olsen (magnus@greatlord.com)
|
2017-03-16 14:37:27 +00:00
|
|
|
* Sebastian Gasiorek (sebastian.gasiorek@reactos.org)
|
2007-12-30 21:14:20 +00:00
|
|
|
* REVISION HISTORY:
|
|
|
|
* 30/12-2007 Magnus Olsen
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dxg_int.h>
|
|
|
|
|
|
|
|
/* The DdHmgr manger stuff */
|
2017-03-10 17:51:20 +00:00
|
|
|
ULONG gcSizeDdHmgr = 1024;
|
2007-12-30 21:14:20 +00:00
|
|
|
PDD_ENTRY gpentDdHmgr = NULL;
|
|
|
|
|
|
|
|
ULONG gcMaxDdHmgr = 0;
|
|
|
|
PDD_ENTRY gpentDdHmgrLast = NULL;
|
|
|
|
|
2017-03-10 17:51:20 +00:00
|
|
|
/* next free ddhmg handle number available to reuse */
|
|
|
|
ULONG ghFreeDdHmgr = 0;
|
2007-12-30 21:14:20 +00:00
|
|
|
HSEMAPHORE ghsemHmgr = NULL;
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
FASTCALL
|
|
|
|
VerifyObjectOwner(PDD_ENTRY pEntry)
|
|
|
|
{
|
2010-03-25 03:50:46 +00:00
|
|
|
DWORD Pid = (DWORD)(DWORD_PTR)PsGetCurrentProcessId() & 0xFFFFFFFC;
|
2018-03-04 20:58:15 +00:00
|
|
|
DWORD check = (DWORD_PTR)pEntry->Pid & 0xFFFFFFFE;
|
2007-12-30 21:14:20 +00:00
|
|
|
return ( (check == Pid) || (!check));
|
|
|
|
}
|
|
|
|
|
2007-12-31 14:31:06 +00:00
|
|
|
/*++
|
|
|
|
* @name DdHmgCreate
|
|
|
|
* @implemented
|
|
|
|
*
|
2008-01-02 05:44:59 +00:00
|
|
|
* The function DdHmgCreate is used internally in dxg.sys
|
|
|
|
* It creates all DX kernel objects that are need it for creation of DX objects.
|
2007-12-31 14:31:06 +00:00
|
|
|
*
|
|
|
|
* @return
|
2008-01-02 05:44:59 +00:00
|
|
|
* Return FALSE for failure and TRUE for success in creating the DX object
|
2007-12-31 14:31:06 +00:00
|
|
|
*
|
|
|
|
* @remarks.
|
2008-01-02 05:44:59 +00:00
|
|
|
* Only used internally in dxg.sys
|
2007-12-31 14:31:06 +00:00
|
|
|
*--*/
|
2007-12-30 21:14:20 +00:00
|
|
|
BOOL
|
|
|
|
FASTCALL
|
2009-08-24 20:10:03 +00:00
|
|
|
DdHmgCreate(VOID)
|
2007-12-30 21:14:20 +00:00
|
|
|
{
|
2017-03-10 17:51:20 +00:00
|
|
|
gpentDdHmgr = EngAllocMem(FL_ZERO_MEMORY, gcSizeDdHmgr * sizeof(DD_ENTRY), TAG_THDD);
|
2007-12-30 21:14:20 +00:00
|
|
|
ghFreeDdHmgr = 0;
|
|
|
|
gcMaxDdHmgr = 1;
|
|
|
|
|
|
|
|
if (gpentDdHmgr)
|
|
|
|
{
|
|
|
|
ghsemHmgr = EngCreateSemaphore();
|
|
|
|
|
|
|
|
if (ghsemHmgr)
|
|
|
|
{
|
|
|
|
gpLockShortDelay = EngAllocMem(FL_ZERO_MEMORY | FL_NONPAGED_MEMORY, sizeof(LARGE_INTEGER), TAG_GINI);
|
|
|
|
|
|
|
|
if (gpLockShortDelay)
|
|
|
|
{
|
|
|
|
gpLockShortDelay->HighPart = -1;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EngDeleteSemaphore(ghsemHmgr);
|
|
|
|
ghsemHmgr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
EngFreeMem(gpentDdHmgr);
|
|
|
|
gpentDdHmgr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-12-31 14:31:06 +00:00
|
|
|
/*++
|
|
|
|
* @name DdHmgDestroy
|
|
|
|
* @implemented
|
|
|
|
*
|
2008-01-02 05:44:59 +00:00
|
|
|
* The function DdHmgDestroy is used internally in dxg.sys
|
|
|
|
* It destroys all DX kernel objects
|
2007-12-31 14:31:06 +00:00
|
|
|
*
|
|
|
|
* @return
|
2008-01-02 05:44:59 +00:00
|
|
|
* Always returns true, as a failure here would result in a BSOD.
|
2007-12-31 14:31:06 +00:00
|
|
|
*
|
|
|
|
* @remarks.
|
2008-01-02 05:44:59 +00:00
|
|
|
* Only used internally in dxg.sys
|
2007-12-31 14:31:06 +00:00
|
|
|
*--*/
|
2007-12-30 21:14:20 +00:00
|
|
|
BOOL
|
|
|
|
FASTCALL
|
2009-08-24 20:10:03 +00:00
|
|
|
DdHmgDestroy(VOID)
|
2007-12-30 21:14:20 +00:00
|
|
|
{
|
|
|
|
gcMaxDdHmgr = 0;
|
|
|
|
gcSizeDdHmgr = 0;
|
|
|
|
ghFreeDdHmgr = 0;
|
|
|
|
gpentDdHmgrLast = NULL;
|
|
|
|
|
|
|
|
if (gpentDdHmgr)
|
|
|
|
{
|
|
|
|
EngFreeMem(gpentDdHmgr);
|
|
|
|
gpentDdHmgr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ghsemHmgr)
|
|
|
|
{
|
|
|
|
EngDeleteSemaphore(ghsemHmgr);
|
|
|
|
ghsemHmgr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-12-31 14:31:06 +00:00
|
|
|
/*++
|
|
|
|
* @name DdHmgLock
|
|
|
|
* @implemented
|
|
|
|
*
|
2008-01-02 05:44:59 +00:00
|
|
|
* The function DdHmgLock is used internally in dxg.sys
|
|
|
|
* It locks a DX kernel object
|
2007-12-31 14:31:06 +00:00
|
|
|
*
|
|
|
|
* @param HANDLE DdHandle
|
2008-01-02 05:44:59 +00:00
|
|
|
* The handle we want locked
|
2007-12-31 14:31:06 +00:00
|
|
|
*
|
|
|
|
* @param UCHAR ObjectType
|
2008-01-02 05:44:59 +00:00
|
|
|
* The type of the object we expected the handle to contain
|
|
|
|
* value 0 is for ?
|
|
|
|
* value 1 is for EDD_DIRECTDRAW_LOCAL
|
|
|
|
* value 2 is for EDD_SURFACE
|
|
|
|
* value 3 is for ?
|
|
|
|
* value 4 is for EDD_VIDEOPORT
|
|
|
|
* value 5 is for EDD_MOTIONCOMP
|
2007-12-31 14:31:06 +00:00
|
|
|
|
|
|
|
* @param BOOLEAN LockOwned
|
2008-01-02 05:44:59 +00:00
|
|
|
* If it needs to call EngAcquireSemaphore or not
|
2007-12-31 14:31:06 +00:00
|
|
|
*
|
|
|
|
* @return
|
2008-01-02 05:44:59 +00:00
|
|
|
* Returns an EDD_* object, or NULL if it fails
|
2007-12-31 14:31:06 +00:00
|
|
|
*
|
|
|
|
* @remarks.
|
2008-01-02 05:44:59 +00:00
|
|
|
* Only used internally in dxg.sys
|
2007-12-31 14:31:06 +00:00
|
|
|
*--*/
|
2007-12-30 21:14:20 +00:00
|
|
|
PVOID
|
|
|
|
FASTCALL
|
2009-08-24 20:10:03 +00:00
|
|
|
DdHmgLock(HANDLE DdHandle, UCHAR ObjectType, BOOLEAN LockOwned)
|
2007-12-30 21:14:20 +00:00
|
|
|
{
|
2017-03-10 17:51:20 +00:00
|
|
|
DWORD Index = DDHMG_HTOI(DdHandle);
|
2007-12-30 21:14:20 +00:00
|
|
|
|
|
|
|
PDD_ENTRY pEntry = NULL;
|
|
|
|
PVOID Object = NULL;
|
|
|
|
|
|
|
|
if ( !LockOwned )
|
|
|
|
{
|
|
|
|
EngAcquireSemaphore(ghsemHmgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Index < gcMaxDdHmgr )
|
|
|
|
{
|
2017-06-24 12:14:01 +00:00
|
|
|
pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * Index));
|
2017-03-10 17:51:20 +00:00
|
|
|
|
2007-12-30 21:14:20 +00:00
|
|
|
if ( VerifyObjectOwner(pEntry) )
|
|
|
|
{
|
2017-03-10 17:51:20 +00:00
|
|
|
if ( ( pEntry->Objt == ObjectType ) &&
|
2018-03-04 20:58:15 +00:00
|
|
|
( pEntry->FullUnique == (((ULONG_PTR)DdHandle >> 21) & 0x7FF) ) &&
|
2017-03-10 17:51:20 +00:00
|
|
|
( !pEntry->pobj->cExclusiveLock ) )
|
|
|
|
{
|
|
|
|
InterlockedIncrement((VOID*)&pEntry->pobj->cExclusiveLock);
|
|
|
|
pEntry->pobj->Tid = KeGetCurrentThread();
|
|
|
|
Object = pEntry->pobj;
|
|
|
|
}
|
2007-12-30 21:14:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !LockOwned )
|
|
|
|
{
|
2017-03-15 14:16:00 +00:00
|
|
|
EngReleaseSemaphore(ghsemHmgr);
|
2007-12-30 21:14:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Object;
|
|
|
|
}
|
2017-03-10 17:51:20 +00:00
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name DdAllocateObject
|
|
|
|
* @implemented
|
|
|
|
*
|
|
|
|
* The function DdAllocateObject is used internally in dxg.sys
|
|
|
|
* It allocates memory for a DX kernel object
|
|
|
|
*
|
|
|
|
* @param UINT32 oSize
|
|
|
|
* Size of memory to be allocated
|
|
|
|
* @param UCHAR oType
|
|
|
|
* Object type
|
|
|
|
* @param BOOLEAN oZeroMemory
|
|
|
|
* Zero memory
|
|
|
|
*
|
|
|
|
* @remarks.
|
|
|
|
* Only used internally in dxg.sys
|
|
|
|
*/
|
|
|
|
PVOID
|
|
|
|
FASTCALL
|
|
|
|
DdAllocateObject(ULONG objSize, UCHAR objType, BOOLEAN objZeroMemory)
|
|
|
|
{
|
|
|
|
PVOID pObject = NULL;
|
|
|
|
|
|
|
|
if (objZeroMemory)
|
|
|
|
pObject = EngAllocMem(FL_ZERO_MEMORY, objSize, ((ULONG)objType << 24) + TAG_DH_0);
|
|
|
|
else
|
|
|
|
pObject = EngAllocMem(0, objSize, ((ULONG)objType << 24) + TAG_DH_0);
|
|
|
|
|
|
|
|
if (!pObject)
|
|
|
|
{
|
|
|
|
EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name DdFreeObject
|
|
|
|
* @implemented
|
|
|
|
*
|
|
|
|
* The function DdFreeObject is used internally in dxg.sys
|
|
|
|
* It frees memory of DX kernel object
|
|
|
|
*
|
|
|
|
* @param PVOID pObject
|
|
|
|
* Object memory to be freed
|
|
|
|
*
|
|
|
|
* @remarks.
|
|
|
|
* Only used internally in dxg.sys
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
DdFreeObject(PVOID pObject)
|
|
|
|
{
|
|
|
|
EngFreeMem(pObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name DdGetFreeHandle
|
|
|
|
* @implemented
|
|
|
|
*
|
|
|
|
* The function DdGetFreeHandle is used internally in dxg.sys
|
|
|
|
* It allocates new handle for specified object type
|
|
|
|
*
|
|
|
|
* @param UCHAR oType
|
|
|
|
* Object type
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Returns handle or 0 if it fails.
|
|
|
|
*
|
|
|
|
* @remarks.
|
|
|
|
* Only used internally in dxg.sys
|
|
|
|
*--*/
|
|
|
|
HANDLE
|
|
|
|
FASTCALL
|
|
|
|
DdGetFreeHandle(UCHAR objType)
|
|
|
|
{
|
|
|
|
PVOID mAllocMem = NULL;
|
|
|
|
ULONG mAllocEntries = 0;
|
|
|
|
PDD_ENTRY pEntry = NULL;
|
2018-03-04 20:58:15 +00:00
|
|
|
ULONG_PTR retVal;
|
2017-03-10 17:51:20 +00:00
|
|
|
ULONG index;
|
|
|
|
|
|
|
|
// check if memory is allocated
|
|
|
|
if (!gpentDdHmgr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// check if we reached maximum handle index
|
|
|
|
if (gcMaxDdHmgr == DDHMG_HANDLE_LIMIT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// check if we have free handle to reuse
|
|
|
|
if (ghFreeDdHmgr)
|
|
|
|
{
|
|
|
|
index = ghFreeDdHmgr;
|
2017-06-24 12:14:01 +00:00
|
|
|
pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * index));
|
2017-03-10 17:51:20 +00:00
|
|
|
|
|
|
|
// put next free index to our global variable
|
|
|
|
ghFreeDdHmgr = pEntry->NextFree;
|
|
|
|
|
|
|
|
// build handle
|
|
|
|
pEntry->FullUnique = objType | 8;
|
|
|
|
retVal = (pEntry->FullUnique << 21) | index;
|
|
|
|
return (HANDLE)retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if all pre-allocated memory is already used then allocate more
|
|
|
|
if (gcSizeDdHmgr == gcMaxDdHmgr)
|
|
|
|
{
|
|
|
|
// allocate buffer for next 1024 handles
|
|
|
|
mAllocEntries = gcSizeDdHmgr + 1024;
|
|
|
|
mAllocMem = EngAllocMem(FL_ZERO_MEMORY, sizeof(DD_ENTRY) * (mAllocEntries), TAG_THDD);
|
|
|
|
if (!mAllocMem)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
memmove(&mAllocMem, gpentDdHmgr, sizeof(DD_ENTRY) * gcSizeDdHmgr);
|
|
|
|
gcSizeDdHmgr = mAllocEntries;
|
|
|
|
gpentDdHmgrLast = gpentDdHmgr;
|
|
|
|
EngFreeMem(gpentDdHmgr);
|
|
|
|
gpentDdHmgr = mAllocMem;
|
|
|
|
}
|
|
|
|
|
2017-06-24 12:14:01 +00:00
|
|
|
pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * gcMaxDdHmgr));
|
2017-03-10 17:51:20 +00:00
|
|
|
|
|
|
|
// build handle
|
|
|
|
pEntry->FullUnique = objType | 8;
|
|
|
|
retVal = (pEntry->FullUnique << 21) | gcMaxDdHmgr;
|
|
|
|
gcMaxDdHmgr = gcMaxDdHmgr + 1;
|
|
|
|
|
|
|
|
return (HANDLE)retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name DdHmgAlloc
|
|
|
|
* @implemented
|
|
|
|
*
|
|
|
|
* The function DdHmgAlloc is used internally in dxg.sys
|
|
|
|
* It allocates object
|
|
|
|
*
|
|
|
|
* @param ULONG objSize
|
|
|
|
* Size of memory to be allocated
|
|
|
|
* @param CHAR objType
|
|
|
|
* Object type
|
2017-03-14 23:05:49 +00:00
|
|
|
* @param UINT objLock
|
|
|
|
* Object lock flag
|
2017-03-10 17:51:20 +00:00
|
|
|
*
|
|
|
|
* @return
|
2017-03-14 23:05:49 +00:00
|
|
|
* Handle if object is not locked by objLock
|
|
|
|
* Object if lock is set in objLock
|
2017-03-10 17:51:20 +00:00
|
|
|
* 0 if it fails.
|
|
|
|
*
|
|
|
|
* @remarks.
|
|
|
|
* Only used internally in dxg.sys
|
|
|
|
*--*/
|
|
|
|
HANDLE
|
|
|
|
FASTCALL
|
2017-03-14 23:05:49 +00:00
|
|
|
DdHmgAlloc(ULONG objSize, CHAR objType, BOOLEAN objLock)
|
2017-03-10 17:51:20 +00:00
|
|
|
{
|
|
|
|
PVOID pObject = NULL;
|
|
|
|
HANDLE DdHandle = NULL;
|
|
|
|
PDD_ENTRY pEntry = NULL;
|
|
|
|
DWORD Index;
|
|
|
|
|
|
|
|
pObject = DdAllocateObject(objSize, objType, TRUE);
|
|
|
|
if (!pObject)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
EngAcquireSemaphore(ghsemHmgr);
|
|
|
|
|
|
|
|
/* Get next free handle */
|
|
|
|
DdHandle = DdGetFreeHandle(objType);
|
|
|
|
|
|
|
|
if (DdHandle)
|
|
|
|
{
|
|
|
|
Index = DDHMG_HTOI(DdHandle);
|
|
|
|
|
2017-06-24 12:14:01 +00:00
|
|
|
pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * Index));
|
2017-03-10 17:51:20 +00:00
|
|
|
|
|
|
|
pEntry->pobj = pObject;
|
|
|
|
pEntry->Objt = objType;
|
|
|
|
|
2018-03-04 20:58:15 +00:00
|
|
|
pEntry->Pid = (HANDLE)(((ULONG_PTR)PsGetCurrentProcessId() & 0xFFFFFFFC) | ((ULONG_PTR)(pEntry->Pid) & 1));
|
2017-03-10 17:51:20 +00:00
|
|
|
|
2017-03-14 23:05:49 +00:00
|
|
|
if (objLock)
|
|
|
|
{
|
|
|
|
InterlockedIncrement((VOID*)&pEntry->pobj->cExclusiveLock);
|
2017-03-10 17:51:20 +00:00
|
|
|
pEntry->pobj->Tid = KeGetCurrentThread();
|
2017-03-14 23:05:49 +00:00
|
|
|
}
|
2017-03-10 17:51:20 +00:00
|
|
|
pEntry->pobj->hHmgr = DdHandle;
|
|
|
|
|
|
|
|
EngReleaseSemaphore(ghsemHmgr);
|
|
|
|
|
|
|
|
/* Return handle if object not locked */
|
2017-03-14 23:05:49 +00:00
|
|
|
if (!objLock)
|
2017-03-10 17:51:20 +00:00
|
|
|
return DdHandle;
|
|
|
|
|
|
|
|
return (HANDLE)pEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
EngReleaseSemaphore(ghsemHmgr);
|
|
|
|
DdFreeObject(pObject);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name DdHmgFree
|
|
|
|
* @implemented
|
|
|
|
*
|
|
|
|
* The function DdHmgFree is used internally in dxg.sys
|
|
|
|
* It frees DX object and memory allocated to it
|
|
|
|
*
|
|
|
|
* @param HANDLE DdHandle
|
|
|
|
* DX object handle
|
|
|
|
*
|
|
|
|
* @remarks.
|
|
|
|
* Only used internally in dxg.sys
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
DdHmgFree(HANDLE DdHandle)
|
|
|
|
{
|
|
|
|
PDD_ENTRY pEntry = NULL;
|
|
|
|
|
|
|
|
DWORD Index = DDHMG_HTOI(DdHandle);
|
|
|
|
|
|
|
|
EngAcquireSemaphore(ghsemHmgr);
|
|
|
|
|
2017-06-24 12:14:01 +00:00
|
|
|
pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * Index));
|
2017-03-10 17:51:20 +00:00
|
|
|
|
|
|
|
// check if we have object that should be freed
|
|
|
|
if (pEntry->pobj)
|
|
|
|
DdFreeObject(pEntry->pobj);
|
|
|
|
|
|
|
|
pEntry->NextFree = ghFreeDdHmgr;
|
|
|
|
|
|
|
|
// reset process ID
|
2018-03-04 20:58:15 +00:00
|
|
|
pEntry->Pid = (HANDLE)((DWORD_PTR)pEntry->Pid & 1);
|
2017-03-10 17:51:20 +00:00
|
|
|
ghFreeDdHmgr = Index;
|
|
|
|
|
|
|
|
EngReleaseSemaphore(ghsemHmgr);
|
|
|
|
}
|