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.56 2004/01/11 01:42:08 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>
41 #include <include/intgdi.h>
43 #include <win32k/debug1.h>
45 /*! Size of the GDI handle table
46 * http://www.windevnet.com/documents/s=7290/wdj9902b/9902b.htm
47 * gdi handle table can hold 0x4000 handles
49 #define GDI_HANDLE_COUNT 0x4000
51 #define GDI_GLOBAL_PROCESS ((HANDLE) 0xffffffff)
53 #define GDI_HANDLE_INDEX_MASK (GDI_HANDLE_COUNT - 1)
54 #define GDI_HANDLE_TYPE_MASK 0x007f0000
55 #define GDI_HANDLE_STOCK_MASK 0x00800000
57 #define GDI_HANDLE_CREATE(i, t) ((HANDLE)(((i) & GDI_HANDLE_INDEX_MASK) | ((t) & GDI_HANDLE_TYPE_MASK)))
58 #define GDI_HANDLE_GET_INDEX(h) (((DWORD)(h)) & GDI_HANDLE_INDEX_MASK)
59 #define GDI_HANDLE_GET_TYPE(h) (((DWORD)(h)) & GDI_HANDLE_TYPE_MASK)
60 #define GDI_HANDLE_IS_TYPE(h, t) ((t) == (((DWORD)(h)) & GDI_HANDLE_TYPE_MASK))
61 #define GDI_HANDLE_IS_STOCKOBJ(h) (0 != (((DWORD)(h)) & GDI_HANDLE_STOCK_MASK))
62 #define GDI_HANDLE_SET_STOCKOBJ(h) ((h) = (HANDLE)(((DWORD)(h)) | GDI_HANDLE_STOCK_MASK))
64 #define GDI_TYPE_TO_MAGIC(t) ((WORD) ((t) >> 16))
65 #define GDI_MAGIC_TO_TYPE(m) ((DWORD)(m) << 16)
67 /* FIXME Ownership of GDI objects by processes not properly implemented yet */
69 #define GDI_VALID_OBJECT(h, obj, t, f) \
71 && (GDI_MAGIC_TO_TYPE((obj)->Magic) == (t) || GDI_OBJECT_TYPE_DONTCARE == (t)) \
72 && (GDI_HANDLE_GET_TYPE((h)) == GDI_MAGIC_TO_TYPE((obj)->Magic)) \
73 && (((obj)->hProcessId == PsGetCurrentProcessId()) \
74 || (GDI_GLOBAL_PROCESS == (obj)->hProcessId) \
75 || ((f) & GDIOBJFLAG_IGNOREPID)))
77 #define GDI_VALID_OBJECT(h, obj, t, f) \
79 && (GDI_MAGIC_TO_TYPE((obj)->Magic) == (t) || GDI_OBJECT_TYPE_DONTCARE == (t)) \
80 && (GDI_HANDLE_GET_TYPE((h)) == GDI_MAGIC_TO_TYPE((obj)->Magic)))
83 typedef struct _GDI_HANDLE_TABLE
86 PGDIOBJHDR Handles
[1];
87 } GDI_HANDLE_TABLE
, *PGDI_HANDLE_TABLE
;
89 /* GDI stock objects */
91 static LOGBRUSH WhiteBrush
=
92 { BS_SOLID
, RGB(255,255,255), 0 };
94 static LOGBRUSH LtGrayBrush
=
95 /* FIXME : this should perhaps be BS_HATCHED, at least for 1 bitperpixel */
96 { BS_SOLID
, RGB(192,192,192), 0 };
98 static LOGBRUSH GrayBrush
=
99 /* FIXME : this should perhaps be BS_HATCHED, at least for 1 bitperpixel */
100 { BS_SOLID
, RGB(128,128,128), 0 };
102 static LOGBRUSH DkGrayBrush
=
103 /* This is BS_HATCHED, for 1 bitperpixel. This makes the spray work in pbrush */
104 /* NB_HATCH_STYLES is an index into HatchBrushes */
105 { BS_HATCHED
, RGB(0,0,0), NB_HATCH_STYLES
};
107 static LOGBRUSH BlackBrush
=
108 { BS_SOLID
, RGB(0,0,0), 0 };
110 static LOGBRUSH NullBrush
=
113 static LOGPEN WhitePen
=
114 { PS_SOLID
, { 0, 0 }, RGB(255,255,255) };
116 static LOGPEN BlackPen
=
117 { PS_SOLID
, { 0, 0 }, RGB(0,0,0) };
119 static LOGPEN NullPen
=
120 { PS_NULL
, { 0, 0 }, 0 };
122 static LOGFONTW OEMFixedFont
=
123 { 11, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, OEM_CHARSET
,
124 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, L
"" };
126 static LOGFONTW AnsiFixedFont
=
127 { 11, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
128 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, L
"" };
130 /*static LOGFONTW AnsiVarFont =
131 *{ 10, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
132 * 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, L"MS Sans Serif" }; */
134 static LOGFONTW SystemFont
=
135 { 11, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
136 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, L
"System" };
138 static LOGFONTW DeviceDefaultFont
=
139 { 11, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
140 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, L
"" };
142 static LOGFONTW SystemFixedFont
=
143 { 11, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
144 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, L
"" };
146 /* FIXME: Is this correct? */
147 static LOGFONTW DefaultGuiFont
=
148 { 11, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
149 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, L
"Bitstream Vera Sans" };
151 #define NB_STOCK_OBJECTS (DEFAULT_GUI_FONT + 1)
153 static HGDIOBJ
*StockObjects
[NB_STOCK_OBJECTS
];
154 static PGDI_HANDLE_TABLE HandleTable
= 0;
155 static FAST_MUTEX HandleTableMutex
;
156 static FAST_MUTEX RefCountHandling
;
159 * Allocate GDI object table.
160 * \param Size - number of entries in the object table.
161 * Notes:: Must be called at IRQL < DISPATCH_LEVEL.
163 static PGDI_HANDLE_TABLE FASTCALL
164 GDIOBJ_iAllocHandleTable (WORD Size
)
166 PGDI_HANDLE_TABLE handleTable
;
168 /* prevent APC delivery for the *FastMutexUnsafe calls */
169 const KIRQL PrevIrql
= KfRaiseIrql(APC_LEVEL
);
170 ExAcquireFastMutexUnsafe (&HandleTableMutex
);
171 handleTable
= ExAllocatePool(PagedPool
,
172 sizeof(GDI_HANDLE_TABLE
) +
173 sizeof(PGDIOBJ
) * Size
);
174 ASSERT( handleTable
);
177 sizeof(GDI_HANDLE_TABLE
) + sizeof(PGDIOBJ
) * Size
);
178 handleTable
->wTableSize
= Size
;
179 ExReleaseFastMutexUnsafe (&HandleTableMutex
);
180 KfLowerIrql(PrevIrql
);
186 * Returns the entry into the handle table by index.
188 static PGDIOBJHDR FASTCALL
189 GDIOBJ_iGetObjectForIndex(WORD TableIndex
)
191 if (0 == TableIndex
|| HandleTable
->wTableSize
< TableIndex
)
193 DPRINT1("Invalid TableIndex %u\n", (unsigned) TableIndex
);
197 return HandleTable
->Handles
[TableIndex
];
201 * Finds next free entry in the GDI handle table.
202 * \return index into the table is successful, zero otherwise.
205 GDIOBJ_iGetNextOpenHandleIndex (void)
209 for (tableIndex
= 1; tableIndex
< HandleTable
->wTableSize
; tableIndex
++)
211 if (NULL
== HandleTable
->Handles
[tableIndex
])
217 return (tableIndex
< HandleTable
->wTableSize
) ? tableIndex
: 0;
221 * Allocate memory for GDI object and return handle to it.
223 * \param Size - size of the GDI object. This shouldn't to include the size of GDIOBJHDR.
224 * The actual amount of allocated memory is sizeof(GDIOBJHDR)+Size
225 * \param ObjectType - type of object \ref GDI object types
226 * \param CleanupProcPtr - Routine to be called on destruction of object
228 * \return Handle of the allocated object.
230 * \note Use GDIOBJ_Lock() to obtain pointer to the new object.
233 GDIOBJ_AllocObj(WORD Size
, DWORD ObjectType
, GDICLEANUPPROC CleanupProc
)
235 PW32PROCESS W32Process
;
236 PGDIOBJHDR newObject
;
239 ExAcquireFastMutex(&HandleTableMutex
);
240 Index
= GDIOBJ_iGetNextOpenHandleIndex ();
243 DPRINT1("Out of GDI handles\n");
247 DPRINT("GDIOBJ_AllocObj: handle: %d, size: %d, type: 0x%08x\n", Index
, Size
, ObjectType
);
248 newObject
= ExAllocatePool(PagedPool
, Size
+ sizeof (GDIOBJHDR
));
249 if (newObject
== NULL
)
251 DPRINT1("GDIOBJ_AllocObj: failed\n");
254 RtlZeroMemory (newObject
, Size
+ sizeof(GDIOBJHDR
));
256 newObject
->wTableIndex
= Index
;
258 newObject
->dwCount
= 0;
259 newObject
->hProcessId
= PsGetCurrentProcessId ();
260 newObject
->CleanupProc
= CleanupProc
;
261 newObject
->Magic
= GDI_TYPE_TO_MAGIC(ObjectType
);
262 newObject
->lockfile
= NULL
;
263 newObject
->lockline
= 0;
264 ExInitializeFastMutex(&newObject
->Lock
);
265 HandleTable
->Handles
[Index
] = newObject
;
266 ExReleaseFastMutexUnsafe (&HandleTableMutex
);
268 W32Process
= PsGetCurrentProcess()->Win32Process
;
271 W32Process
->GDIObjects
++;
274 return GDI_HANDLE_CREATE(Index
, ObjectType
);
278 * Free memory allocated for the GDI object. For each object type this function calls the
279 * appropriate cleanup routine.
281 * \param hObj - handle of the object to be deleted.
282 * \param ObjectType - one of the \ref GDI object types
283 * or GDI_OBJECT_TYPE_DONTCARE.
284 * \param Flag - if set to GDIOBJFLAG_IGNOREPID then the routine doesn't check if the process that
285 * tries to delete the object is the same one that created it.
287 * \return Returns TRUE if succesful.
289 * \note You should only use GDIOBJFLAG_IGNOREPID if you are cleaning up after the process that terminated.
290 * \note This function deferres object deletion if it is still in use.
293 GDIOBJ_FreeObj(HGDIOBJ hObj
, DWORD ObjectType
, DWORD Flag
)
295 PW32PROCESS W32Process
;
296 PGDIOBJHDR objectHeader
;
300 objectHeader
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
301 DPRINT("GDIOBJ_FreeObj: hObj: 0x%08x, object: %x\n", hObj
, objectHeader
);
303 if (! GDI_VALID_OBJECT(hObj
, objectHeader
, ObjectType
, Flag
)
304 || GDI_GLOBAL_PROCESS
== objectHeader
->hProcessId
)
307 DPRINT1("Can't delete hObj:0x%08x, type:0x%08x, flag:%d\n", hObj
, ObjectType
, Flag
);
311 DPRINT("FreeObj: locks: %x\n", objectHeader
->dwCount
);
312 if (!(Flag
& GDIOBJFLAG_IGNORELOCK
))
314 /* check that the reference count is zero. if not then set flag
315 * and delete object when releaseobj is called */
316 ExAcquireFastMutex(&RefCountHandling
);
317 if ((objectHeader
->dwCount
& ~0x80000000) > 0 )
319 DPRINT("GDIOBJ_FreeObj: delayed object deletion: count %d\n", objectHeader
->dwCount
);
320 objectHeader
->dwCount
|= 0x80000000;
321 ExReleaseFastMutex(&RefCountHandling
);
324 ExReleaseFastMutex(&RefCountHandling
);
327 /* allow object to delete internal data */
328 if (NULL
!= objectHeader
->CleanupProc
)
330 Obj
= (PGDIOBJ
)((PCHAR
)objectHeader
+ sizeof(GDIOBJHDR
));
331 bRet
= (*(objectHeader
->CleanupProc
))(Obj
);
334 ExFreePool(objectHeader
);
335 HandleTable
->Handles
[GDI_HANDLE_GET_INDEX(hObj
)] = NULL
;
337 W32Process
= PsGetCurrentProcess()->Win32Process
;
340 W32Process
->GDIObjects
--;
347 * Lock multiple objects. Use this function when you need to lock multiple objects and some of them may be
348 * duplicates. You should use this function to avoid trying to lock the same object twice!
350 * \param pList pointer to the list that contains handles to the objects. You should set hObj and ObjectType fields.
351 * \param nObj number of objects to lock
352 * \return for each entry in pList this function sets pObj field to point to the object.
354 * \note this function uses an O(n^2) algoritm because we shouldn't need to call it with more than 3 or 4 objects.
357 GDIOBJ_LockMultipleObj(PGDIMULTILOCK pList
, INT nObj
)
361 /* FIXME - check for "invalid" handles */
362 /* go through the list checking for duplicate objects */
363 for (i
= 0; i
< nObj
; i
++)
365 pList
[i
].pObj
= NULL
;
366 for (j
= 0; j
< i
; j
++)
368 if (pList
[i
].hObj
== pList
[j
].hObj
)
370 /* already locked, so just copy the pointer to the object */
371 pList
[i
].pObj
= pList
[j
].pObj
;
376 if (NULL
== pList
[i
].pObj
)
378 /* object hasn't been locked, so lock it. */
379 if (NULL
!= pList
[i
].hObj
)
381 pList
[i
].pObj
= GDIOBJ_LockObj(pList
[i
].hObj
, pList
[i
].ObjectType
);
390 * Unlock multiple objects. Use this function when you need to unlock multiple objects and some of them may be
393 * \param pList pointer to the list that contains handles to the objects. You should set hObj and ObjectType fields.
394 * \param nObj number of objects to lock
396 * \note this function uses O(n^2) algoritm because we shouldn't need to call it with more than 3 or 4 objects.
399 GDIOBJ_UnlockMultipleObj(PGDIMULTILOCK pList
, INT nObj
)
404 /* go through the list checking for duplicate objects */
405 for (i
= 0; i
< nObj
; i
++)
407 if (NULL
!= pList
[i
].pObj
)
409 for (j
= i
+ 1; j
< nObj
; j
++)
411 if ((pList
[i
].pObj
== pList
[j
].pObj
))
413 /* set the pointer to zero for all duplicates */
414 pList
[j
].pObj
= NULL
;
417 GDIOBJ_UnlockObj(pList
[i
].hObj
, pList
[i
].ObjectType
);
418 pList
[i
].pObj
= NULL
;
426 * Get the type of the object.
427 * \param ObjectHandle - handle of the object.
428 * \return One of the \ref GDI object types
431 GDIOBJ_GetObjectType(HGDIOBJ ObjectHandle
)
435 ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle
));
437 || ! GDI_VALID_OBJECT(ObjectHandle
, ObjHdr
, GDI_MAGIC_TO_TYPE(ObjHdr
->Magic
), 0))
439 DPRINT1("Invalid ObjectHandle 0x%08x\n", ObjectHandle
);
442 DPRINT("GDIOBJ_GetObjectType for handle 0x%08x returns 0x%08x\n", ObjectHandle
,
443 GDI_MAGIC_TO_TYPE(ObjHdr
->Magic
));
445 return GDI_MAGIC_TO_TYPE(ObjHdr
->Magic
);
449 * Initialization of the GDI object engine.
452 InitGdiObjectHandleTable (VOID
)
454 DPRINT("InitGdiObjectHandleTable\n");
455 ExInitializeFastMutex (&HandleTableMutex
);
456 ExInitializeFastMutex (&RefCountHandling
);
458 HandleTable
= GDIOBJ_iAllocHandleTable (GDI_HANDLE_COUNT
);
459 DPRINT("HandleTable: %x\n", HandleTable
);
461 InitEngHandleTable();
465 * Creates a bunch of stock objects: brushes, pens, fonts.
468 CreateStockObjects(void)
472 DPRINT("Beginning creation of stock objects\n");
474 /* Create GDI Stock Objects from the logical structures we've defined */
476 StockObjects
[WHITE_BRUSH
] = IntGdiCreateBrushIndirect(&WhiteBrush
);
477 StockObjects
[LTGRAY_BRUSH
] = IntGdiCreateBrushIndirect(&LtGrayBrush
);
478 StockObjects
[GRAY_BRUSH
] = IntGdiCreateBrushIndirect(&GrayBrush
);
479 StockObjects
[DKGRAY_BRUSH
] = IntGdiCreateBrushIndirect(&DkGrayBrush
);
480 StockObjects
[BLACK_BRUSH
] = IntGdiCreateBrushIndirect(&BlackBrush
);
481 StockObjects
[NULL_BRUSH
] = IntGdiCreateBrushIndirect(&NullBrush
);
483 StockObjects
[WHITE_PEN
] = IntGdiCreatePenIndirect(&WhitePen
);
484 StockObjects
[BLACK_PEN
] = IntGdiCreatePenIndirect(&BlackPen
);
485 StockObjects
[NULL_PEN
] = IntGdiCreatePenIndirect(&NullPen
);
487 (void) TextIntCreateFontIndirect(&OEMFixedFont
, (HFONT
*)&StockObjects
[OEM_FIXED_FONT
]);
488 (void) TextIntCreateFontIndirect(&AnsiFixedFont
, (HFONT
*)&StockObjects
[ANSI_FIXED_FONT
]);
489 (void) TextIntCreateFontIndirect(&SystemFont
, (HFONT
*)&StockObjects
[SYSTEM_FONT
]);
490 (void) TextIntCreateFontIndirect(&DeviceDefaultFont
, (HFONT
*)&StockObjects
[DEVICE_DEFAULT_FONT
]);
491 (void) TextIntCreateFontIndirect(&SystemFixedFont
, (HFONT
*)&StockObjects
[SYSTEM_FIXED_FONT
]);
492 (void) TextIntCreateFontIndirect(&DefaultGuiFont
, (HFONT
*)&StockObjects
[DEFAULT_GUI_FONT
]);
494 StockObjects
[DEFAULT_PALETTE
] = (HGDIOBJ
*)PALETTE_Init();
496 for (Object
= 0; Object
< NB_STOCK_OBJECTS
; Object
++)
498 if (NULL
!= StockObjects
[Object
])
500 GDIOBJ_SetOwnership(StockObjects
[Object
], NULL
);
501 /* GDI_HANDLE_SET_STOCKOBJ(StockObjects[Object]);*/
505 DPRINT("Completed creation of stock objects\n");
509 * Return stock object.
510 * \param Object - stock object id.
511 * \return Handle to the object.
514 NtGdiGetStockObject(INT Object
)
516 DPRINT("NtGdiGetStockObject index %d\n", Object
);
518 return ((Object
< 0) || (NB_STOCK_OBJECTS
<= Object
)) ? NULL
: StockObjects
[Object
];
523 * \param hObject object handle
524 * \return if the function fails the returned value is FALSE.
527 NtGdiDeleteObject(HGDIOBJ hObject
)
529 DPRINT("NtGdiDeleteObject handle 0x%08x\n", hObject
);
531 return NULL
!= hObject
532 ? GDIOBJ_FreeObj(hObject
, GDI_OBJECT_TYPE_DONTCARE
, GDIOBJFLAG_DEFAULT
) : FALSE
;
536 * Internal function. Called when the process is destroyed to free the remaining GDI handles.
537 * \param Process - PID of the process that will be destroyed.
540 CleanupForProcess (struct _EPROCESS
*Process
, INT Pid
)
543 PGDIOBJHDR objectHeader
;
544 PEPROCESS CurrentProcess
;
546 DPRINT("Starting CleanupForProcess prochandle %x Pid %d\n", Process
, Pid
);
547 CurrentProcess
= PsGetCurrentProcess();
548 if (CurrentProcess
!= Process
)
550 KeAttachProcess(Process
);
553 for(i
= 1; i
< HandleTable
->wTableSize
; i
++)
555 objectHeader
= GDIOBJ_iGetObjectForIndex(i
);
556 if (NULL
!= objectHeader
&&
557 (INT
) objectHeader
->hProcessId
== Pid
)
559 DPRINT("CleanupForProcess: %d, process: %d, locks: %d, magic: 0x%x", i
, objectHeader
->hProcessId
, objectHeader
->dwCount
, objectHeader
->Magic
);
560 GDIOBJ_FreeObj(GDI_HANDLE_CREATE(i
, GDI_MAGIC_TO_TYPE(objectHeader
->Magic
)),
561 GDI_MAGIC_TO_TYPE(objectHeader
->Magic
),
562 GDIOBJFLAG_IGNOREPID
| GDIOBJFLAG_IGNORELOCK
);
566 if (CurrentProcess
!= Process
)
571 DPRINT("Completed cleanup for process %d\n", Pid
);
576 #define GDIOBJ_TRACKLOCKS
578 #ifdef GDIOBJ_LockObj
579 #undef GDIOBJ_LockObj
581 GDIOBJ_LockObjDbg (const char* file
, int line
, HGDIOBJ hObj
, DWORD ObjectType
)
583 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
585 DPRINT("(%s:%i) GDIOBJ_LockObjDbg(0x%08x,0x%08x)\n", file
, line
, hObj
, ObjectType
);
586 if (! GDI_VALID_OBJECT(hObj
, ObjHdr
, ObjectType
, GDIOBJFLAG_DEFAULT
))
593 else if (GDI_MAGIC_TO_TYPE(ObjHdr
->Magic
) != ObjectType
&& ObjectType
!= GDI_OBJECT_TYPE_DONTCARE
)
597 else if (ObjHdr
->hProcessId
!= GDI_GLOBAL_PROCESS
598 && ObjHdr
->hProcessId
!= PsGetCurrentProcessId())
602 else if (GDI_HANDLE_GET_TYPE(hObj
) != ObjectType
&& ObjectType
!= GDI_OBJECT_TYPE_DONTCARE
)
606 DPRINT1("GDIOBJ_LockObj failed for 0x%08x, reqtype 0x%08x reason %d\n",
607 hObj
, ObjectType
, reason
);
608 DPRINT1("\tcalled from: %s:%i\n", file
, line
);
612 if (! ExTryToAcquireFastMutex(&ObjHdr
->Lock
))
614 DPRINT1("Caution! GDIOBJ_LockObj trying to lock object 0x%x second time\n", hObj
);
615 DPRINT1(" called from: %s:%i\n", file
, line
);
616 if (NULL
!= ObjHdr
->lockfile
)
618 DPRINT1(" previously locked from: %s:%i\n", ObjHdr
->lockfile
, ObjHdr
->lockline
);
620 ExAcquireFastMutex(&ObjHdr
->Lock
);
621 DPRINT1(" Disregard previous message about object 0x%x, it's ok\n", hObj
);
624 ExAcquireFastMutex(&RefCountHandling
);
626 ExReleaseFastMutex(&RefCountHandling
);
628 if (NULL
== ObjHdr
->lockfile
)
630 ObjHdr
->lockfile
= file
;
631 ObjHdr
->lockline
= line
;
634 return (PGDIOBJ
)((PCHAR
)ObjHdr
+ sizeof(GDIOBJHDR
));
636 #endif//GDIOBJ_LockObj
638 #ifdef GDIOBJ_UnlockObj
639 #undef GDIOBJ_UnlockObj
641 GDIOBJ_UnlockObjDbg (const char* file
, int line
, HGDIOBJ hObj
, DWORD ObjectType
)
643 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
645 if (! GDI_VALID_OBJECT(hObj
, ObjHdr
, ObjectType
, GDIOBJFLAG_DEFAULT
))
647 DPRINT1("GDIBOJ_UnlockObj failed for 0x%08x, reqtype 0x%08x\n",
649 DPRINT1("\tcalled from: %s:%i\n", file
, line
);
652 DPRINT("(%s:%i) GDIOBJ_UnlockObj(0x%08x,0x%08x)\n", file
, line
, hObj
, ObjectType
);
653 ObjHdr
->lockfile
= NULL
;
654 ObjHdr
->lockline
= 0;
656 return GDIOBJ_UnlockObj(hObj
, ObjectType
);
658 #endif//GDIOBJ_LockObj
661 * Return pointer to the object by handle.
663 * \param hObj Object handle
664 * \param ObjectType one of the object types defined in \ref GDI object types
665 * \return Pointer to the object.
667 * \note Process can only get pointer to the objects it created or global objects.
669 * \todo Don't allow to lock the objects twice! Synchronization!
672 GDIOBJ_LockObj(HGDIOBJ hObj
, DWORD ObjectType
)
674 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
676 DPRINT("GDIOBJ_LockObj: hObj: 0x%08x, type: 0x%08x, objhdr: %x\n", hObj
, ObjectType
, ObjHdr
);
677 if (! GDI_VALID_OBJECT(hObj
, ObjHdr
, ObjectType
, GDIOBJFLAG_DEFAULT
))
679 DPRINT1("GDIBOJ_LockObj failed for 0x%08x, type 0x%08x\n",
684 ExAcquireFastMutex(&ObjHdr
->Lock
);
686 ExAcquireFastMutex(&RefCountHandling
);
688 ExReleaseFastMutex(&RefCountHandling
);
689 return (PGDIOBJ
)((PCHAR
)ObjHdr
+ sizeof(GDIOBJHDR
));
693 * Release GDI object. Every object locked by GDIOBJ_LockObj() must be unlocked. You should unlock the object
694 * as soon as you don't need to have access to it's data.
696 * \param hObj Object handle
697 * \param ObjectType one of the object types defined in \ref GDI object types
699 * \note This function performs delayed cleanup. If the object is locked when GDI_FreeObj() is called
700 * then \em this function frees the object when reference count is zero.
702 * \todo Change synchronization algorithm.
704 #undef GDIOBJ_UnlockObj
706 GDIOBJ_UnlockObj(HGDIOBJ hObj
, DWORD ObjectType
)
708 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(hObj
));
710 DPRINT("GDIOBJ_UnlockObj: hObj: 0x%08x, type: 0x%08x, objhdr: %x\n", hObj
, ObjectType
, ObjHdr
);
711 if (! GDI_VALID_OBJECT(hObj
, ObjHdr
, ObjectType
, GDIOBJFLAG_DEFAULT
))
713 DPRINT1( "GDIOBJ_UnLockObj: failed\n");
717 ExReleaseFastMutex(&ObjHdr
->Lock
);
719 ExAcquireFastMutex(&RefCountHandling
);
720 if (0 == (ObjHdr
->dwCount
& ~0x80000000))
722 ExReleaseFastMutex(&RefCountHandling
);
723 DPRINT1( "GDIOBJ_UnLockObj: unlock object (0x%x) that is not locked\n", hObj
);
729 if (ObjHdr
->dwCount
== 0x80000000)
731 //delayed object release
733 ExReleaseFastMutex(&RefCountHandling
);
734 DPRINT("GDIOBJ_UnlockObj: delayed delete\n");
735 return GDIOBJ_FreeObj(hObj
, ObjectType
, GDIOBJFLAG_DEFAULT
);
737 ExReleaseFastMutex(&RefCountHandling
);
743 GDIOBJ_OwnedByCurrentProcess(HGDIOBJ ObjectHandle
)
745 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle
));
747 DPRINT("GDIOBJ_OwnedByCurrentProcess: ObjectHandle: 0x%08x\n", ObjectHandle
);
748 ASSERT(GDI_VALID_OBJECT(ObjectHandle
, ObjHdr
, GDI_OBJECT_TYPE_DONTCARE
, GDIOBJFLAG_IGNOREPID
));
750 return ObjHdr
->hProcessId
== PsGetCurrentProcessId();
754 GDIOBJ_SetOwnership(HGDIOBJ ObjectHandle
, PEPROCESS NewOwner
)
756 PGDIOBJHDR ObjHdr
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(ObjectHandle
));
757 PEPROCESS OldProcess
;
758 PW32PROCESS W32Process
;
761 DPRINT("GDIOBJ_OwnedByCurrentProcess: ObjectHandle: 0x%08x\n", ObjectHandle
);
762 ASSERT(GDI_VALID_OBJECT(ObjectHandle
, ObjHdr
, GDI_OBJECT_TYPE_DONTCARE
, GDIOBJFLAG_IGNOREPID
));
764 if ((NULL
== NewOwner
&& GDI_GLOBAL_PROCESS
!= ObjHdr
->hProcessId
)
765 || (NULL
!= NewOwner
&& ObjHdr
->hProcessId
!= (HANDLE
) NewOwner
->UniqueProcessId
))
767 Status
= PsLookupProcessByProcessId((PVOID
)ObjHdr
->hProcessId
, &OldProcess
);
768 if (NT_SUCCESS(Status
))
770 W32Process
= OldProcess
->Win32Process
;
773 W32Process
->GDIObjects
--;
775 ObDereferenceObject(OldProcess
);
779 if (NULL
== NewOwner
)
781 ObjHdr
->hProcessId
= GDI_GLOBAL_PROCESS
;
783 else if (ObjHdr
->hProcessId
!= (HANDLE
) NewOwner
->UniqueProcessId
)
785 ObjHdr
->hProcessId
= (HANDLE
) NewOwner
->UniqueProcessId
;
786 W32Process
= NewOwner
->Win32Process
;
789 W32Process
->GDIObjects
++;
795 GDIOBJ_CopyOwnership(HGDIOBJ CopyFrom
, HGDIOBJ CopyTo
)
797 PGDIOBJHDR ObjHdrFrom
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(CopyFrom
));
798 PGDIOBJHDR ObjHdrTo
= GDIOBJ_iGetObjectForIndex(GDI_HANDLE_GET_INDEX(CopyTo
));
800 PEPROCESS ProcessFrom
;
801 PEPROCESS CurrentProcess
;
803 ASSERT(NULL
!= ObjHdrFrom
&& NULL
!= ObjHdrTo
);
804 if (NULL
!= ObjHdrFrom
&& NULL
!= ObjHdrTo
805 && ObjHdrTo
->hProcessId
!= ObjHdrFrom
->hProcessId
)
807 if (ObjHdrFrom
->hProcessId
== GDI_GLOBAL_PROCESS
)
809 GDIOBJ_SetOwnership(CopyTo
, NULL
);
813 /* Warning: ugly hack ahead
815 * During process cleanup, we can't call PsLookupProcessByProcessId
816 * for the current process, 'cause that function will try to
817 * reference the process, and since the process is closing down
818 * that will result in a bugcheck.
819 * So, instead, we call PsGetCurrentProcess, which doesn't reference
820 * the process. If the current process is indeed the one we're
821 * looking for, we use it, otherwise we can (safely) call
822 * PsLookupProcessByProcessId
824 CurrentProcess
= PsGetCurrentProcess();
825 if (ObjHdrFrom
->hProcessId
== (HANDLE
) CurrentProcess
->UniqueProcessId
)
827 GDIOBJ_SetOwnership(CopyTo
, CurrentProcess
);
831 Status
= PsLookupProcessByProcessId((PVOID
) ObjHdrFrom
->hProcessId
, &ProcessFrom
);
832 if (NT_SUCCESS(Status
))
834 GDIOBJ_SetOwnership(CopyTo
, ProcessFrom
);
835 ObDereferenceObject(ProcessFrom
);