2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * GDIOBJ.C - GDI object manipulation routines
22 * $Id: gdiobj.c,v 1.52 2003/11/26 21:48:35 gvg Exp $
26 #undef WIN32_LEAN_AND_MEAN
27 #define WIN32_NO_STATUS
29 #include <ddk/ntddk.h>
30 #include <include/dce.h>
31 #include <include/object.h>
32 #include <win32k/gdiobj.h>
33 #include <win32k/brush.h>
34 #include <win32k/pen.h>
35 #include <win32k/text.h>
36 #include <win32k/dc.h>
37 #include <win32k/bitmaps.h>
38 #include <win32k/region.h>
39 #include <win32k/cursoricon.h>
40 #include <include/palette.h>
42 #include <win32k/debug1.h>
44 /*! Size of the GDI handle table
45 * http://www.windevnet.com/documents/s=7290/wdj9902b/9902b.htm
46 * gdi handle table can hold 0x4000 handles
48 #define GDI_HANDLE_COUNT 0x4000
50 #define GDI_GLOBAL_PROCESS ((HANDLE) 0xffffffff)
52 #define GDI_HANDLE_INDEX_MASK (GDI_HANDLE_COUNT - 1)
53 #define GDI_HANDLE_TYPE_MASK 0x007f0000
54 #define GDI_HANDLE_STOCK_MASK 0x00800000
56 #define GDI_HANDLE_CREATE(i, t) ((HANDLE)(((i) & GDI_HANDLE_INDEX_MASK) | ((t) & GDI_HANDLE_TYPE_MASK)))
57 #define GDI_HANDLE_GET_INDEX(h) (((DWORD)(h)) & GDI_HANDLE_INDEX_MASK)
58 #define GDI_HANDLE_GET_TYPE(h) (((DWORD)(h)) & GDI_HANDLE_TYPE_MASK)
59 #define GDI_HANDLE_IS_TYPE(h, t) ((t) == (((DWORD)(h)) & GDI_HANDLE_TYPE_MASK))
60 #define GDI_HANDLE_IS_STOCKOBJ(h) (0 != (((DWORD)(h)) & GDI_HANDLE_STOCK_MASK))
61 #define GDI_HANDLE_SET_STOCKOBJ(h) ((h) = (HANDLE)(((DWORD)(h)) | GDI_HANDLE_STOCK_MASK))
63 #define GDI_TYPE_TO_MAGIC(t) ((WORD) ((t) >> 16))
64 #define GDI_MAGIC_TO_TYPE(m) ((DWORD)(m) << 16)
66 /* FIXME Ownership of GDI objects by processes not properly implemented yet */
68 #define GDI_VALID_OBJECT(h, obj, t, f) \
70 && (GDI_MAGIC_TO_TYPE((obj)->Magic) == (t) || GDI_OBJECT_TYPE_DONTCARE == (t)) \
71 && (GDI_HANDLE_GET_TYPE((h)) == GDI_MAGIC_TO_TYPE((obj)->Magic)) \
72 && (((obj)->hProcessId == PsGetCurrentProcessId()) \
73 || (GDI_GLOBAL_PROCESS == (obj)->hProcessId) \
74 || ((f) & GDIOBJFLAG_IGNOREPID)))
76 #define GDI_VALID_OBJECT(h, obj, t, f) \
78 && (GDI_MAGIC_TO_TYPE((obj)->Magic) == (t) || GDI_OBJECT_TYPE_DONTCARE == (t)) \
79 && (GDI_HANDLE_GET_TYPE((h)) == GDI_MAGIC_TO_TYPE((obj)->Magic)))
82 typedef struct _GDI_HANDLE_TABLE
85 PGDIOBJHDR Handles
[1];
86 } GDI_HANDLE_TABLE
, *PGDI_HANDLE_TABLE
;
88 /* GDI stock objects */
90 static LOGBRUSH WhiteBrush
=
91 { BS_SOLID
, RGB(255,255,255), 0 };
93 static LOGBRUSH LtGrayBrush
=
94 /* FIXME : this should perhaps be BS_HATCHED, at least for 1 bitperpixel */
95 { BS_SOLID
, RGB(192,192,192), 0 };
97 static LOGBRUSH GrayBrush
=
98 /* FIXME : this should perhaps be BS_HATCHED, at least for 1 bitperpixel */
99 { BS_SOLID
, RGB(128,128,128), 0 };
101 static LOGBRUSH DkGrayBrush
=
102 /* This is BS_HATCHED, for 1 bitperpixel. This makes the spray work in pbrush */
103 /* NB_HATCH_STYLES is an index into HatchBrushes */
104 { BS_HATCHED
, RGB(0,0,0), NB_HATCH_STYLES
};
106 static LOGBRUSH BlackBrush
=
107 { BS_SOLID
, RGB(0,0,0), 0 };
109 static LOGBRUSH NullBrush
=
112 static LOGPEN WhitePen
=
113 { PS_SOLID
, { 0, 0 }, RGB(255,255,255) };
115 static LOGPEN BlackPen
=
116 { PS_SOLID
, { 0, 0 }, RGB(0,0,0) };
118 static LOGPEN NullPen
=
119 { PS_NULL
, { 0, 0 }, 0 };
121 static LOGFONTW OEMFixedFont
=
122 { 14, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, OEM_CHARSET
,
123 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, L
"" };
125 static LOGFONTW AnsiFixedFont
=
126 { 14, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
127 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, L
"" };
129 /*static LOGFONTW AnsiVarFont =
130 *{ 14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
131 * 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, L"MS Sans Serif" }; */
133 static LOGFONTW SystemFont
=
134 { 14, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
135 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, L
"System" };
137 static LOGFONTW DeviceDefaultFont
=
138 { 14, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
139 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, L
"" };
141 static LOGFONTW SystemFixedFont
=
142 { 14, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
143 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, L
"" };
145 /* FIXME: Is this correct? */
146 static LOGFONTW DefaultGuiFont
=
147 { 14, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
148 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, L
"MS Sans Serif" };
150 #define NB_STOCK_OBJECTS (DEFAULT_GUI_FONT + 1)
152 static HGDIOBJ
*StockObjects
[NB_STOCK_OBJECTS
];
153 static PGDI_HANDLE_TABLE HandleTable
= 0;
154 static FAST_MUTEX HandleTableMutex
;
155 static FAST_MUTEX RefCountHandling
;
158 * Allocate GDI object table.
159 * \param Size - number of entries in the object table.
160 * Notes:: Must be called at IRQL < DISPATCH_LEVEL.
162 static PGDI_HANDLE_TABLE FASTCALL
163 GDIOBJ_iAllocHandleTable (WORD Size
)
165 PGDI_HANDLE_TABLE handleTable
;
167 /* prevent APC delivery for the *FastMutexUnsafe calls */
168 const KIRQL PrevIrql
= KfRaiseIrql(APC_LEVEL
);
169 ExAcquireFastMutexUnsafe (&HandleTableMutex
);
170 handleTable
= ExAllocatePool(PagedPool
,
171 sizeof(GDI_HANDLE_TABLE
) +
172 sizeof(PGDIOBJ
) * Size
);
173 ASSERT( handleTable
);
176 sizeof(GDI_HANDLE_TABLE
) + sizeof(PGDIOBJ
) * Size
);
177 handleTable
->wTableSize
= Size
;
178 ExReleaseFastMutexUnsafe (&HandleTableMutex
);
179 KfLowerIrql(PrevIrql
);
185 * Returns the entry into the handle table by index.
187 static PGDIOBJHDR FASTCALL
188 GDIOBJ_iGetObjectForIndex(WORD TableIndex
)
190 if (0 == TableIndex
|| HandleTable
->wTableSize
< TableIndex
)
192 DPRINT1("Invalid TableIndex %u\n", (unsigned) TableIndex
);
196 return HandleTable
->Handles
[TableIndex
];
200 * Finds next free entry in the GDI handle table.
201 * \return index into the table is successful, zero otherwise.
204 GDIOBJ_iGetNextOpenHandleIndex (void)
208 /* prevent APC delivery for the *FastMutexUnsafe calls */
209 const KIRQL PrevIrql
= KfRaiseIrql(APC_LEVEL
);
210 ExAcquireFastMutexUnsafe (&HandleTableMutex
);
211 for (tableIndex
= 1; tableIndex
< HandleTable
->wTableSize
; tableIndex
++)
213 if (NULL
== HandleTable
->Handles
[tableIndex
])
215 HandleTable
->Handles
[tableIndex
] = (PGDIOBJHDR
) -1;
219 ExReleaseFastMutexUnsafe (&HandleTableMutex
);
220 KfLowerIrql(PrevIrql
);
222 return (tableIndex
< HandleTable
->wTableSize
) ? tableIndex
: 0;
226 * Allocate memory for GDI object and return handle to it.
228 * \param Size - size of the GDI object. This shouldn't to include the size of GDIOBJHDR.
229 * The actual amount of allocated memory is sizeof(GDIOBJHDR)+Size
230 * \param ObjectType - type of object \ref GDI object types
231 * \param CleanupProcPtr - Routine to be called on destruction of object
233 * \return Handle of the allocated object.
235 * \note Use GDIOBJ_Lock() to obtain pointer to the new object.
238 GDIOBJ_AllocObj(WORD Size
, DWORD ObjectType
, GDICLEANUPPROC CleanupProc
)
240 PW32PROCESS W32Process
;
241 PGDIOBJHDR newObject
;
244 Index
= GDIOBJ_iGetNextOpenHandleIndex ();
247 DPRINT1("Out of GDI handles\n");
251 DPRINT("GDIOBJ_AllocObj: handle: %d, size: %d, type: 0x%08x\n", Index
, Size
, ObjectType
);
252 newObject
= ExAllocatePool(PagedPool
, Size
+ sizeof (GDIOBJHDR
));
253 if (newObject
== NULL
)
255 DPRINT1("GDIOBJ_AllocObj: failed\n");
258 RtlZeroMemory (newObject
, Size
+ sizeof(GDIOBJHDR
));
260 newObject
->wTableIndex
= Index
;
262 newObject
->dwCount
= 0;
263 newObject
->hProcessId
= PsGetCurrentProcessId ();
264 newObject
->CleanupProc
= CleanupProc
;
265 newObject
->Magic
= GDI_TYPE_TO_MAGIC(ObjectType
);
266 newObject
->lockfile
= NULL
;
267 newObject
->lockline
= 0;
268 HandleTable
->Handles
[Index
] = newObject
;
270 W32Process
= PsGetCurrentProcess()->Win32Process
;
273 W32Process
->GDIObjects
++;
276 //if (0x4001b == (DWORD) GDI_HANDLE_CREATE(Index, ObjectType)) __asm__("int $3\n");
277 return GDI_HANDLE_CREATE(Index
, ObjectType
);
281 * Free memory allocated for the GDI object. For each object type this function calls the
282 * appropriate cleanup routine.
284 * \param hObj - handle of the object to be deleted.
285 * \param ObjectType - one of the \ref GDI object types
286 * or GDI_OBJECT_TYPE_DONTCARE.
287 * \param Flag - if set to GDIOBJFLAG_IGNOREPID then the routine doesn't check if the process that
288 * tries to delete the object is the same one that created it.
290 * \return Returns TRUE if succesful.
292 * \note You should only use GDIOBJFLAG_IGNOREPID if you are cleaning up after the process that terminated.
293 * \note This function deferres object deletion if it is still in use.
296 GDIOBJ_FreeObj(HGDIOBJ hObj
, DWORD ObjectType
, DWORD Flag
)
298 PW32PROCESS W32Process
;
299 PGDIOBJHDR objectHeader
;
303 objectHeader
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
304 DPRINT("GDIOBJ_FreeObj: hObj: 0x%08x, object: %x\n", hObj
, objectHeader
);
306 if (! GDI_VALID_OBJECT(hObj
, objectHeader
, ObjectType
, Flag
)
307 || GDI_GLOBAL_PROCESS
== objectHeader
->hProcessId
)
310 DPRINT1("Can't delete hObj:0x%08x, type:0x%08x, flag:%d\n", hObj
, ObjectType
, Flag
);
314 DPRINT("FreeObj: locks: %x\n", objectHeader
->dwCount
);
315 if (!(Flag
& GDIOBJFLAG_IGNORELOCK
))
317 /* check that the reference count is zero. if not then set flag
318 * and delete object when releaseobj is called */
319 ExAcquireFastMutex(&RefCountHandling
);
320 if ((objectHeader
->dwCount
& ~0x80000000) > 0 )
322 DPRINT("GDIOBJ_FreeObj: delayed object deletion: count %d\n", objectHeader
->dwCount
);
323 objectHeader
->dwCount
|= 0x80000000;
324 ExReleaseFastMutex(&RefCountHandling
);
327 ExReleaseFastMutex(&RefCountHandling
);
330 /* allow object to delete internal data */
331 if (NULL
!= objectHeader
->CleanupProc
)
333 Obj
= (PGDIOBJ
)((PCHAR
)objectHeader
+ sizeof(GDIOBJHDR
));
334 bRet
= (*(objectHeader
->CleanupProc
))(Obj
);
337 ExFreePool(objectHeader
);
338 HandleTable
->Handles
[GDI_HANDLE_GET_INDEX(hObj
)] = NULL
;
340 W32Process
= PsGetCurrentProcess()->Win32Process
;
343 W32Process
->GDIObjects
--;
350 * Lock multiple objects. Use this function when you need to lock multiple objects and some of them may be
351 * duplicates. You should use this function to avoid trying to lock the same object twice!
353 * \param pList pointer to the list that contains handles to the objects. You should set hObj and ObjectType fields.
354 * \param nObj number of objects to lock
355 * \return for each entry in pList this function sets pObj field to point to the object.
357 * \note this function uses an O(n^2) algoritm because we shouldn't need to call it with more than 3 or 4 objects.
360 GDIOBJ_LockMultipleObj(PGDIMULTILOCK pList
, INT nObj
)
364 /* FIXME - check for "invalid" handles */
365 /* go through the list checking for duplicate objects */
366 for (i
= 0; i
< nObj
; i
++)
368 pList
[i
].pObj
= NULL
;
369 for (j
= 0; j
< i
; j
++)
371 if (pList
[i
].hObj
== pList
[j
].hObj
)
373 /* already locked, so just copy the pointer to the object */
374 pList
[i
].pObj
= pList
[j
].pObj
;
379 if (NULL
== pList
[i
].pObj
)
381 /* object hasn't been locked, so lock it. */
382 if (NULL
!= pList
[i
].hObj
)
384 pList
[i
].pObj
= GDIOBJ_LockObj(pList
[i
].hObj
, pList
[i
].ObjectType
);
393 * Unlock multiple objects. Use this function when you need to unlock multiple objects and some of them may be
396 * \param pList pointer to the list that contains handles to the objects. You should set hObj and ObjectType fields.
397 * \param nObj number of objects to lock
399 * \note this function uses O(n^2) algoritm because we shouldn't need to call it with more than 3 or 4 objects.
402 GDIOBJ_UnlockMultipleObj(PGDIMULTILOCK pList
, INT nObj
)
407 /* go through the list checking for duplicate objects */
408 for (i
= 0; i
< nObj
; i
++)
410 if (NULL
!= pList
[i
].pObj
)
412 for (j
= i
+ 1; j
< nObj
; j
++)
414 if ((pList
[i
].pObj
== pList
[j
].pObj
))
416 /* set the pointer to zero for all duplicates */
417 pList
[j
].pObj
= NULL
;
420 GDIOBJ_UnlockObj(pList
[i
].hObj
, pList
[i
].ObjectType
);
421 pList
[i
].pObj
= NULL
;
429 * Get the type of the object.
430 * \param ObjectHandle - handle of the object.
431 * \return One of the \ref GDI object types
434 GDIOBJ_GetObjectType(HGDIOBJ ObjectHandle
)
438 ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle
));
440 || ! GDI_VALID_OBJECT(ObjectHandle
, ObjHdr
, GDI_MAGIC_TO_TYPE(ObjHdr
->Magic
), 0))
442 DPRINT1("Invalid ObjectHandle 0x%08x\n", ObjectHandle
);
445 DPRINT("GDIOBJ_GetObjectType for handle 0x%08x returns 0x%08x\n", ObjectHandle
,
446 GDI_MAGIC_TO_TYPE(ObjHdr
->Magic
));
448 return GDI_MAGIC_TO_TYPE(ObjHdr
->Magic
);
452 * Initialization of the GDI object engine.
455 InitGdiObjectHandleTable (VOID
)
457 DPRINT("InitGdiObjectHandleTable\n");
458 ExInitializeFastMutex (&HandleTableMutex
);
459 ExInitializeFastMutex (&RefCountHandling
);
461 HandleTable
= GDIOBJ_iAllocHandleTable (GDI_HANDLE_COUNT
);
462 DPRINT("HandleTable: %x\n", HandleTable
);
464 InitEngHandleTable();
468 * Creates a bunch of stock objects: brushes, pens, fonts.
471 CreateStockObjects(void)
475 DPRINT("Beginning creation of stock objects\n");
477 /* Create GDI Stock Objects from the logical structures we've defined */
479 StockObjects
[WHITE_BRUSH
] = NtGdiCreateBrushIndirect(&WhiteBrush
);
480 StockObjects
[LTGRAY_BRUSH
] = NtGdiCreateBrushIndirect(&LtGrayBrush
);
481 StockObjects
[GRAY_BRUSH
] = NtGdiCreateBrushIndirect(&GrayBrush
);
482 StockObjects
[DKGRAY_BRUSH
] = NtGdiCreateBrushIndirect(&DkGrayBrush
);
483 StockObjects
[BLACK_BRUSH
] = NtGdiCreateBrushIndirect(&BlackBrush
);
484 StockObjects
[NULL_BRUSH
] = NtGdiCreateBrushIndirect(&NullBrush
);
486 StockObjects
[WHITE_PEN
] = NtGdiCreatePenIndirect(&WhitePen
);
487 StockObjects
[BLACK_PEN
] = NtGdiCreatePenIndirect(&BlackPen
);
488 StockObjects
[NULL_PEN
] = NtGdiCreatePenIndirect(&NullPen
);
490 (void) TextIntCreateFontIndirect(&OEMFixedFont
, (HFONT
*)&StockObjects
[OEM_FIXED_FONT
]);
491 (void) TextIntCreateFontIndirect(&AnsiFixedFont
, (HFONT
*)&StockObjects
[ANSI_FIXED_FONT
]);
492 (void) TextIntCreateFontIndirect(&SystemFont
, (HFONT
*)&StockObjects
[SYSTEM_FONT
]);
493 (void) TextIntCreateFontIndirect(&DeviceDefaultFont
, (HFONT
*)&StockObjects
[DEVICE_DEFAULT_FONT
]);
494 (void) TextIntCreateFontIndirect(&SystemFixedFont
, (HFONT
*)&StockObjects
[SYSTEM_FIXED_FONT
]);
495 (void) TextIntCreateFontIndirect(&DefaultGuiFont
, (HFONT
*)&StockObjects
[DEFAULT_GUI_FONT
]);
497 StockObjects
[DEFAULT_PALETTE
] = (HGDIOBJ
*)PALETTE_Init();
499 for (Object
= 0; Object
< NB_STOCK_OBJECTS
; Object
++)
501 if (NULL
!= StockObjects
[Object
])
503 GDIOBJ_SetOwnership(StockObjects
[Object
], NULL
);
504 /* GDI_HANDLE_SET_STOCKOBJ(StockObjects[Object]);*/
508 DPRINT("Completed creation of stock objects\n");
512 * Return stock object.
513 * \param Object - stock object id.
514 * \return Handle to the object.
517 NtGdiGetStockObject(INT Object
)
519 DPRINT("NtGdiGetStockObject index %d\n", Object
);
521 return ((Object
< 0) || (NB_STOCK_OBJECTS
<= Object
)) ? NULL
: StockObjects
[Object
];
526 * \param hObject object handle
527 * \return if the function fails the returned value is FALSE.
530 NtGdiDeleteObject(HGDIOBJ hObject
)
532 DPRINT("NtGdiDeleteObject handle 0x%08x\n", hObject
);
534 return GDIOBJ_FreeObj(hObject
, GDI_OBJECT_TYPE_DONTCARE
, GDIOBJFLAG_DEFAULT
);
538 * Internal function. Called when the process is destroyed to free the remaining GDI handles.
539 * \param Process - PID of the process that will be destroyed.
542 CleanupForProcess (struct _EPROCESS
*Process
, INT Pid
)
545 PGDIOBJHDR objectHeader
;
546 PEPROCESS CurrentProcess
;
548 DPRINT("Starting CleanupForProcess prochandle %x Pid %d\n", Process
, Pid
);
549 CurrentProcess
= PsGetCurrentProcess();
550 if (CurrentProcess
!= Process
)
552 KeAttachProcess(Process
);
555 for(i
= 1; i
< HandleTable
->wTableSize
; i
++)
557 objectHeader
= GDIOBJ_iGetObjectForIndex(i
);
558 if (NULL
!= objectHeader
&&
559 (INT
) objectHeader
->hProcessId
== Pid
)
561 DPRINT("CleanupForProcess: %d, process: %d, locks: %d, magic: 0x%x", i
, objectHeader
->hProcessId
, objectHeader
->dwCount
, objectHeader
->Magic
);
562 GDIOBJ_FreeObj(GDI_HANDLE_CREATE(i
, GDI_MAGIC_TO_TYPE(objectHeader
->Magic
)),
563 GDI_MAGIC_TO_TYPE(objectHeader
->Magic
),
564 GDIOBJFLAG_IGNOREPID
| GDIOBJFLAG_IGNORELOCK
);
568 if (CurrentProcess
!= Process
)
573 DPRINT("Completed cleanup for process %d\n", Pid
);
578 #define GDIOBJ_TRACKLOCKS
580 #ifdef GDIOBJ_LockObj
581 #undef GDIOBJ_LockObj
583 GDIOBJ_LockObjDbg (const char* file
, int line
, HGDIOBJ hObj
, DWORD ObjectType
)
586 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
588 if (! GDI_VALID_OBJECT(hObj
, ObjHdr
, ObjectType
, GDIOBJFLAG_DEFAULT
))
595 else if (GDI_MAGIC_TO_TYPE(ObjHdr
->Magic
) != ObjectType
&& ObjectType
!= GDI_OBJECT_TYPE_DONTCARE
)
599 else if (ObjHdr
->hProcessId
!= GDI_GLOBAL_PROCESS
600 && ObjHdr
->hProcessId
!= PsGetCurrentProcessId())
604 else if (GDI_HANDLE_GET_TYPE(hObj
) != ObjectType
&& ObjectType
!= GDI_OBJECT_TYPE_DONTCARE
)
608 DPRINT1("GDIOBJ_LockObj failed for 0x%08x, reqtype 0x%08x reason %d\n",
609 hObj
, ObjectType
, reason
);
610 DPRINT1("\tcalled from: %s:%i\n", file
, line
);
613 if (NULL
!= ObjHdr
->lockfile
)
615 DPRINT1("Caution! GDIOBJ_LockObj trying to lock object (0x%x) second time\n", hObj
);
616 DPRINT1("\tcalled from: %s:%i\n", file
, line
);
617 DPRINT1("\tpreviously locked from: %s:%i\n", ObjHdr
->lockfile
, ObjHdr
->lockline
);
619 DPRINT("(%s:%i) GDIOBJ_LockObj(0x%08x,0x%08x)\n", file
, line
, hObj
, ObjectType
);
620 rc
= GDIOBJ_LockObj(hObj
, ObjectType
);
621 if (rc
&& NULL
== ObjHdr
->lockfile
)
623 ObjHdr
->lockfile
= file
;
624 ObjHdr
->lockline
= line
;
629 #endif//GDIOBJ_LockObj
631 #ifdef GDIOBJ_UnlockObj
632 #undef GDIOBJ_UnlockObj
634 GDIOBJ_UnlockObjDbg (const char* file
, int line
, HGDIOBJ hObj
, DWORD ObjectType
)
636 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
638 if (! GDI_VALID_OBJECT(hObj
, ObjHdr
, ObjectType
, GDIOBJFLAG_DEFAULT
))
640 DPRINT1("GDIBOJ_UnlockObj failed for 0x%08x, reqtype 0x%08x\n",
642 DPRINT1("\tcalled from: %s:%i\n", file
, line
);
645 DPRINT("(%s:%i) GDIOBJ_UnlockObj(0x%08x,0x%08x)\n", file
, line
, hObj
, ObjectType
);
646 ObjHdr
->lockfile
= NULL
;
647 ObjHdr
->lockline
= 0;
649 return GDIOBJ_UnlockObj(hObj
, ObjectType
);
651 #endif//GDIOBJ_LockObj
654 * Return pointer to the object by handle.
656 * \param hObj Object handle
657 * \param ObjectType one of the object types defined in \ref GDI object types
658 * \return Pointer to the object.
660 * \note Process can only get pointer to the objects it created or global objects.
662 * \todo Don't allow to lock the objects twice! Synchronization!
665 GDIOBJ_LockObj(HGDIOBJ hObj
, DWORD ObjectType
)
667 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
669 DPRINT("GDIOBJ_LockObj: hObj: 0x%08x, type: 0x%08x, objhdr: %x\n", hObj
, ObjectType
, ObjHdr
);
670 if (! GDI_VALID_OBJECT(hObj
, ObjHdr
, ObjectType
, GDIOBJFLAG_DEFAULT
))
672 DPRINT1("GDIBOJ_LockObj failed for 0x%08x, type 0x%08x\n",
677 if(0 < ObjHdr
->dwCount
)
679 DPRINT1("Caution! GDIOBJ_LockObj trying to lock object (0x%x) second time\n", hObj
);
680 DPRINT1("\t called from: %x\n", __builtin_return_address(0));
683 ExAcquireFastMutex(&RefCountHandling
);
685 ExReleaseFastMutex(&RefCountHandling
);
686 return (PGDIOBJ
)((PCHAR
)ObjHdr
+ sizeof(GDIOBJHDR
));
690 * Release GDI object. Every object locked by GDIOBJ_LockObj() must be unlocked. You should unlock the object
691 * as soon as you don't need to have access to it's data.
693 * \param hObj Object handle
694 * \param ObjectType one of the object types defined in \ref GDI object types
696 * \note This function performs delayed cleanup. If the object is locked when GDI_FreeObj() is called
697 * then \em this function frees the object when reference count is zero.
699 * \todo Change synchronization algorithm.
701 #undef GDIOBJ_UnlockObj
703 GDIOBJ_UnlockObj(HGDIOBJ hObj
, DWORD ObjectType
)
705 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
707 DPRINT("GDIOBJ_UnlockObj: hObj: 0x%08x, type: 0x%08x, objhdr: %x\n", hObj
, ObjectType
, ObjHdr
);
708 if (! GDI_VALID_OBJECT(hObj
, ObjHdr
, ObjectType
, GDIOBJFLAG_DEFAULT
))
710 DPRINT1( "GDIOBJ_UnLockObj: failed\n");
714 ExAcquireFastMutex(&RefCountHandling
);
715 if (0 == (ObjHdr
->dwCount
& ~0x80000000))
717 ExReleaseFastMutex(&RefCountHandling
);
718 DPRINT1( "GDIOBJ_UnLockObj: unlock object (0x%x) that is not locked\n", hObj
);
724 if (ObjHdr
->dwCount
== 0x80000000)
726 //delayed object release
728 ExReleaseFastMutex(&RefCountHandling
);
729 DPRINT("GDIOBJ_UnlockObj: delayed delete\n");
730 return GDIOBJ_FreeObj(hObj
, ObjectType
, GDIOBJFLAG_DEFAULT
);
732 ExReleaseFastMutex(&RefCountHandling
);
738 GDIOBJ_OwnedByCurrentProcess(HGDIOBJ ObjectHandle
)
740 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle
));
742 DPRINT("GDIOBJ_OwnedByCurrentProcess: ObjectHandle: 0x%08x\n", ObjectHandle
);
743 ASSERT(GDI_VALID_OBJECT(ObjectHandle
, ObjHdr
, GDI_OBJECT_TYPE_DONTCARE
, GDIOBJFLAG_IGNOREPID
));
745 return ObjHdr
->hProcessId
== PsGetCurrentProcessId();
749 GDIOBJ_SetOwnership(HGDIOBJ ObjectHandle
, PEPROCESS NewOwner
)
751 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle
));
752 PEPROCESS OldProcess
;
753 PW32PROCESS W32Process
;
756 DPRINT("GDIOBJ_OwnedByCurrentProcess: ObjectHandle: 0x%08x\n", ObjectHandle
);
757 ASSERT(GDI_VALID_OBJECT(ObjectHandle
, ObjHdr
, GDI_OBJECT_TYPE_DONTCARE
, GDIOBJFLAG_IGNOREPID
));
759 if ((NULL
== NewOwner
&& GDI_GLOBAL_PROCESS
!= ObjHdr
->hProcessId
)
760 || (NULL
!= NewOwner
&& ObjHdr
->hProcessId
!= (HANDLE
) NewOwner
->UniqueProcessId
))
762 Status
= PsLookupProcessByProcessId((PVOID
)ObjHdr
->hProcessId
, &OldProcess
);
763 if (NT_SUCCESS(Status
))
765 W32Process
= OldProcess
->Win32Process
;
768 W32Process
->GDIObjects
--;
770 ObDereferenceObject(OldProcess
);
774 if (NULL
== NewOwner
)
776 ObjHdr
->hProcessId
= GDI_GLOBAL_PROCESS
;
778 else if (ObjHdr
->hProcessId
!= (HANDLE
) NewOwner
->UniqueProcessId
)
780 ObjHdr
->hProcessId
= (HANDLE
) NewOwner
->UniqueProcessId
;
781 W32Process
= NewOwner
->Win32Process
;
784 W32Process
->GDIObjects
++;
790 GDIOBJ_CopyOwnership(HGDIOBJ CopyFrom
, HGDIOBJ CopyTo
)
792 PGDIOBJHDR ObjHdrFrom
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(CopyFrom
));
793 PGDIOBJHDR ObjHdrTo
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(CopyTo
));
795 PEPROCESS ProcessFrom
;
796 PEPROCESS CurrentProcess
;
798 ASSERT(NULL
!= ObjHdrFrom
&& NULL
!= ObjHdrTo
);
799 if (NULL
!= ObjHdrFrom
&& NULL
!= ObjHdrTo
800 && ObjHdrTo
->hProcessId
!= ObjHdrFrom
->hProcessId
)
802 if (ObjHdrFrom
->hProcessId
== GDI_GLOBAL_PROCESS
)
804 GDIOBJ_SetOwnership(CopyTo
, NULL
);
808 /* Warning: ugly hack ahead
810 * During process cleanup, we can't call PsLookupProcessByProcessId
811 * for the current process, 'cause that function will try to
812 * reference the process, and since the process is closing down
813 * that will result in a bugcheck.
814 * So, instead, we call PsGetCurrentProcess, which doesn't reference
815 * the process. If the current process is indeed the one we're
816 * looking for, we use it, otherwise we can (safely) call
817 * PsLookupProcessByProcessId
819 CurrentProcess
= PsGetCurrentProcess();
820 if (ObjHdrFrom
->hProcessId
== (HANDLE
) CurrentProcess
->UniqueProcessId
)
822 GDIOBJ_SetOwnership(CopyTo
, CurrentProcess
);
826 Status
= PsLookupProcessByProcessId((PVOID
) ObjHdrFrom
->hProcessId
, &ProcessFrom
);
827 if (NT_SUCCESS(Status
))
829 GDIOBJ_SetOwnership(CopyTo
, ProcessFrom
);
830 ObDereferenceObject(ProcessFrom
);