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