Completion of editing current documentation in dxg
[reactos.git] / reactos / drivers / directx / dxg / ddhmg.c
1
2
3 /*
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS kernel
6 * PURPOSE: Native driver for dxg implementation
7 * FILE: drivers/directx/dxg/ddhmg.c
8 * PROGRAMER: Magnus olsen (magnus@greatlord.com)
9 * REVISION HISTORY:
10 * 30/12-2007 Magnus Olsen
11 */
12
13 #include <dxg_int.h>
14
15 /* The DdHmgr manger stuff */
16 ULONG gcSizeDdHmgr = 64 * sizeof(DD_ENTRY);
17 PDD_ENTRY gpentDdHmgr = NULL;
18
19 ULONG gcMaxDdHmgr = 0;
20 PDD_ENTRY gpentDdHmgrLast = NULL;
21
22 HANDLE ghFreeDdHmgr = 0;
23 HSEMAPHORE ghsemHmgr = NULL;
24
25 BOOL
26 FASTCALL
27 VerifyObjectOwner(PDD_ENTRY pEntry)
28 {
29 DWORD Pid = (DWORD) PsGetCurrentProcessId() & 0xFFFFFFFC;
30 DWORD check = pEntry->ObjectOwner.ulObj & 0xFFFFFFFE;
31 return ( (check == Pid) || (!check));
32 }
33
34 /*++
35 * @name DdHmgCreate
36 * @implemented
37 *
38 * The function DdHmgCreate is used internally in dxg.sys
39 * It creates all DX kernel objects that are need it for creation of DX objects.
40 *
41 * @return
42 * Return FALSE for failure and TRUE for success in creating the DX object
43 *
44 * @remarks.
45 * Only used internally in dxg.sys
46 *--*/
47 BOOL
48 FASTCALL
49 DdHmgCreate()
50 {
51 gpentDdHmgr = EngAllocMem(FL_ZERO_MEMORY, gcSizeDdHmgr, TAG_THDD);
52 ghFreeDdHmgr = 0;
53 gcMaxDdHmgr = 1;
54
55 if (gpentDdHmgr)
56 {
57 ghsemHmgr = EngCreateSemaphore();
58
59 if (ghsemHmgr)
60 {
61 gpLockShortDelay = EngAllocMem(FL_ZERO_MEMORY | FL_NONPAGED_MEMORY, sizeof(LARGE_INTEGER), TAG_GINI);
62
63 if (gpLockShortDelay)
64 {
65 gpLockShortDelay->HighPart = -1;
66 return TRUE;
67 }
68
69 EngDeleteSemaphore(ghsemHmgr);
70 ghsemHmgr = NULL;
71 }
72
73 EngFreeMem(gpentDdHmgr);
74 gpentDdHmgr = NULL;
75 }
76
77 return FALSE;
78 }
79
80 /*++
81 * @name DdHmgDestroy
82 * @implemented
83 *
84 * The function DdHmgDestroy is used internally in dxg.sys
85 * It destroys all DX kernel objects
86 *
87 * @return
88 * Always returns true, as a failure here would result in a BSOD.
89 *
90 * @remarks.
91 * Only used internally in dxg.sys
92 *--*/
93 BOOL
94 FASTCALL
95 DdHmgDestroy()
96 {
97 gcMaxDdHmgr = 0;
98 gcSizeDdHmgr = 0;
99 ghFreeDdHmgr = 0;
100 gpentDdHmgrLast = NULL;
101
102 if (gpentDdHmgr)
103 {
104 EngFreeMem(gpentDdHmgr);
105 gpentDdHmgr = NULL;
106 }
107
108 if (ghsemHmgr)
109 {
110 EngDeleteSemaphore(ghsemHmgr);
111 ghsemHmgr = NULL;
112 }
113
114 return TRUE;
115 }
116
117 /*++
118 * @name DdHmgLock
119 * @implemented
120 *
121 * The function DdHmgLock is used internally in dxg.sys
122 * It locks a DX kernel object
123 *
124 * @param HANDLE DdHandle
125 * The handle we want locked
126 *
127 * @param UCHAR ObjectType
128 * The type of the object we expected the handle to contain
129 * value 0 is for ?
130 * value 1 is for EDD_DIRECTDRAW_LOCAL
131 * value 2 is for EDD_SURFACE
132 * value 3 is for ?
133 * value 4 is for EDD_VIDEOPORT
134 * value 5 is for EDD_MOTIONCOMP
135
136 * @param BOOLEAN LockOwned
137 * If it needs to call EngAcquireSemaphore or not
138 *
139 * @return
140 * Returns an EDD_* object, or NULL if it fails
141 *
142 * @remarks.
143 * Only used internally in dxg.sys
144 *--*/
145 PVOID
146 FASTCALL
147 DdHmgLock( HANDLE DdHandle, UCHAR ObjectType, BOOLEAN LockOwned)
148 {
149
150 DWORD Index = (DWORD)DdHandle & 0x1FFFFF;
151 PDD_ENTRY pEntry = NULL;
152 PVOID Object = NULL;
153
154 if ( !LockOwned )
155 {
156 EngAcquireSemaphore(ghsemHmgr);
157 }
158
159 if ( Index < gcMaxDdHmgr )
160 {
161 pEntry = (PDD_ENTRY)((PBYTE)gpentDdHmgr + (sizeof(DD_ENTRY) * Index));
162 if ( VerifyObjectOwner(pEntry) )
163 {
164 if ( (pEntry->Objt == ObjectType ) &&
165 (pEntry->FullUnique == (((DWORD)DdHandle >> 21) & 0x7FF) ) &&
166 (pEntry->pobj->cExclusiveLock == 0) &&
167 (pEntry->pobj->Tid == PsGetCurrentThread()))
168 {
169 InterlockedIncrement(&pEntry->pobj->cExclusiveLock);
170 pEntry->pobj->Tid = PsGetCurrentThread();
171 Object = pEntry->pobj;
172 }
173 }
174 }
175
176 if ( !LockOwned )
177 {
178 EngDeleteSemaphore(ghsemHmgr);
179 }
180
181 return Object;
182 }