[CMAKE]
[reactos.git] / subsystems / win32 / win32k / objects / dclife.c
index d0c86c8..6b66f8c 100644 (file)
@@ -80,7 +80,22 @@ NTAPI
 DC_AllocDcWithHandle()
 {
     PDC pdc;
-    pdc = (PDC)GDIOBJ_AllocObjWithHandle(GDILoObjType_LO_DC_TYPE);
+
+    pdc = (PDC)GDIOBJ_AllocateObject(GDIObjType_DC_TYPE,
+                                     sizeof(DC),
+                                     BASEFLAG_LOOKASIDE);
+    if (!pdc)
+    {
+        DPRINT1("Could not allocate a DC.\n");
+        return NULL;
+    }
+
+    if (!GDIOBJ_hInsertObject(&pdc->BaseObject, GDI_OBJ_HMGR_POWNED))
+    {
+        DPRINT1("Could not insert DC into handle table.\n");
+        GDIOBJ_vFreeObject(&pdc->BaseObject);
+        return NULL;
+    }
 
     pdc->pdcattr = &pdc->dcattr;
 
@@ -232,7 +247,6 @@ DC_vInitDc(
     /* Allocate a Vis region */
     pdc->prgnVis = IntSysCreateRectpRgn(0, 0, pdc->dclevel.sizl.cx, pdc->dclevel.sizl.cy);
        ASSERT(pdc->prgnVis);
-       GDIOBJ_CopyOwnership(pdc->BaseObject.hHmgr, pdc->prgnVis->BaseObject.hHmgr);
 
     /* Setup palette */
     pdc->dclevel.hpal = StockObjects[DEFAULT_PALETTE];
@@ -354,12 +368,16 @@ DC_Cleanup(PVOID ObjectBody)
     EBRUSHOBJ_vCleanup(&pdc->eboBackground);
 
     /*  Free regions */
-    if (pdc->rosdc.hClipRgn)
+    if (pdc->rosdc.hClipRgn && GreIsHandleValid(pdc->rosdc.hClipRgn))
         GreDeleteObject(pdc->rosdc.hClipRgn);
     if (pdc->prgnVis)
-        REGION_FreeRgnByHandle(pdc->prgnVis->BaseObject.hHmgr);
-    if (pdc->rosdc.hGCClipRgn)
+    {
+        REGION_Delete(pdc->prgnVis);
+    }
+    if (pdc->rosdc.hGCClipRgn && GreIsHandleValid(pdc->rosdc.hGCClipRgn))
+    {
         GreDeleteObject(pdc->rosdc.hGCClipRgn);
+    }
     if (NULL != pdc->rosdc.CombinedClip)
         IntEngDeleteClipRegion(pdc->rosdc.CombinedClip);
 
@@ -373,72 +391,64 @@ DC_Cleanup(PVOID ObjectBody)
     return TRUE;
 }
 
-BOOL
-FASTCALL
-DC_SetOwnership(HDC hDC, PEPROCESS Owner)
+VOID
+NTAPI
+DC_vSetOwner(PDC pdc, ULONG ulOwner)
 {
-    INT Index;
-    PGDI_TABLE_ENTRY Entry;
-    PDC pDC;
-    BOOL ret = FALSE;
 
-    if (!GDIOBJ_SetOwnership(hDC, Owner))
+    if (pdc->rosdc.hClipRgn)
     {
-        DPRINT1("GDIOBJ_SetOwnership failed\n");
-        return FALSE;
+        IntGdiSetRegionOwner(pdc->rosdc.hClipRgn, ulOwner);
     }
 
-    pDC = DC_LockDc(hDC);
-    if (!pDC)
+    if (pdc->rosdc.hGCClipRgn)
     {
-        DPRINT1("Could not lock DC\n");
-        return FALSE;
+        IntGdiSetRegionOwner(pdc->rosdc.hGCClipRgn, ulOwner);
+    }
+
+    if (pdc->dclevel.hPath)
+    {
+        GreSetObjectOwner(pdc->dclevel.hPath, ulOwner);
+    }
+
+    IntGdiSetBrushOwner(pdc->dclevel.pbrFill, ulOwner);
+    IntGdiSetBrushOwner(pdc->dclevel.pbrLine, ulOwner);
+
+    /* Allocate or free DC attribute */
+    if (ulOwner == GDI_OBJ_HMGR_PUBLIC || ulOwner == GDI_OBJ_HMGR_NONE)
+    {
+        if (pdc->pdcattr != &pdc->dcattr)
+            DC_vFreeDcAttr(pdc);
+    }
+    else if (ulOwner == GDI_OBJ_HMGR_POWNED)
+    {
+        if (pdc->pdcattr == &pdc->dcattr)
+            DC_bAllocDcAttr(pdc);
     }
 
-    /*
-       System Regions:
-          These regions do not use attribute sections and when allocated, use
-          gdiobj level functions.
-    */
-    if (pDC->rosdc.hClipRgn)
-    {   // FIXME! HAX!!!
-        Index = GDI_HANDLE_GET_INDEX(pDC->rosdc.hClipRgn);
-        Entry = &GdiHandleTable->Entries[Index];
-        if (Entry->UserData) FreeObjectAttr(Entry->UserData);
-        Entry->UserData = NULL;
-        //
-        if (!GDIOBJ_SetOwnership(pDC->rosdc.hClipRgn, Owner)) goto leave;
-    }
-    if (pDC->prgnVis)
-    {   // FIXME! HAX!!!
-        Index = GDI_HANDLE_GET_INDEX(pDC->prgnVis->BaseObject.hHmgr);
-        Entry = &GdiHandleTable->Entries[Index];
-        if (Entry->UserData) FreeObjectAttr(Entry->UserData);
-        Entry->UserData = NULL;
-        //
-        if (!GDIOBJ_SetOwnership(pDC->prgnVis->BaseObject.hHmgr, Owner)) goto leave;
-    }
-    if (pDC->rosdc.hGCClipRgn)
-    {   // FIXME! HAX!!!
-        Index = GDI_HANDLE_GET_INDEX(pDC->rosdc.hGCClipRgn);
-        Entry = &GdiHandleTable->Entries[Index];
-        if (Entry->UserData) FreeObjectAttr(Entry->UserData);
-        Entry->UserData = NULL;
-        //
-        if (!GDIOBJ_SetOwnership(pDC->rosdc.hGCClipRgn, Owner)) goto leave;
-    }
-    if (pDC->dclevel.hPath)
-    {
-        if (!GDIOBJ_SetOwnership(pDC->dclevel.hPath, Owner)) goto leave;
-    }
-    ret = TRUE;
-
-leave:
-    DC_UnlockDc(pDC);
-
-    return ret;
+    /* Set the DC's ownership */
+    GDIOBJ_vSetObjectOwner(&pdc->BaseObject, ulOwner);
 }
 
+BOOL
+NTAPI
+GreSetDCOwner(HDC hdc, ULONG ulOwner)
+{
+    PDC pdc;
+
+    pdc = DC_LockDc(hdc);
+    if (!pdc)
+    {
+        DPRINT1("GreSetDCOwner: Could not lock DC\n");
+        return FALSE;
+    }
+
+    /* Call the internal DC function */
+    DC_vSetOwner(pdc, ulOwner);
+
+    DC_UnlockDc(pdc);
+    return TRUE;
+}
 
 int FASTCALL
 CLIPPING_UpdateGCRegion(DC* Dc);
@@ -609,7 +619,7 @@ GreOpenDCW(
     /* FIXME: HACK! */
     DC_InitHack(pdc);
 
-    DC_AllocDcAttr(pdc);
+    DC_bAllocDcAttr(pdc);
 
     DC_UnlockDc(pdc);
 
@@ -689,17 +699,17 @@ NtGdiOpenDCW(
     /* Call the internal function */
     hdc = GreOpenDCW(pustrDevice ? &ustrDevice : NULL,
                      pdmInit ? &dmInit : NULL,
-                     NULL, // fixme pwszLogAddress
+                     NULL, // FIXME: pwszLogAddress
                      iType,
                      bDisplay,
                      hspool,
-                     NULL, //FIXME: pDriverInfo2
+                     NULL, // FIXME: pDriverInfo2
                      pUMdhpdev ? &dhpdev : NULL);
 
     /* If we got a HDC and a UM dhpdev is requested,... */
     if (hdc && pUMdhpdev)
     {
-        /* Copy dhpdev to caller (FIXME: use dhpdev?? */
+        /* Copy dhpdev to caller (FIXME: use dhpdev?) */
         _SEH2_TRY
         {
             /* Pointer was already probed */
@@ -772,7 +782,7 @@ NtGdiCreateCompatibleDC(HDC hdc)
     DC_InitHack(pdcNew);
 
     /* Allocate a dc attribute */
-    DC_AllocDcAttr(pdcNew);
+    DC_bAllocDcAttr(pdcNew);
 
     // HACK!
     DC_vSelectSurface(pdcNew, psurfDefaultBitmap);
@@ -825,11 +835,9 @@ IntGdiDeleteDC(HDC hDC, BOOL Force)
 
     DC_UnlockDc(DCToDelete);
 
-    if (!IsObjectDead(hDC))
+    if (GreIsHandleValid(hDC))
     {
-        DC_vFreeDcAttr(DCToDelete); // Plug a leak see bug 6119!
-
-        if (!GDIOBJ_FreeObjByHandle(hDC, GDI_OBJECT_TYPE_DC))
+        if (!GreDeleteObject(hDC))
         {
             DPRINT1("DC_FreeDC failed\n");
         }
@@ -844,25 +852,24 @@ IntGdiDeleteDC(HDC hDC, BOOL Force)
 
 BOOL
 APIENTRY
-NtGdiDeleteObjectApp(HANDLE DCHandle)
+NtGdiDeleteObjectApp(HANDLE hobj)
 {
     /* Complete all pending operations */
-    NtGdiFlushUserBatch();
-
-    if (GDI_HANDLE_IS_STOCKOBJ(DCHandle)) return TRUE;
+    NtGdiFlushUserBatch(); // FIXME: we shouldn't need this
 
-    if (GDI_HANDLE_GET_TYPE(DCHandle) != GDI_OBJECT_TYPE_DC)
-        return GreDeleteObject((HGDIOBJ) DCHandle);
+    if (GDI_HANDLE_IS_STOCKOBJ(hobj)) return TRUE;
 
-    if (IsObjectDead((HGDIOBJ)DCHandle)) return TRUE;
-
-    if (!GDIOBJ_OwnedByCurrentProcess(DCHandle))
+    if (GreGetObjectOwner(hobj) != GDI_OBJ_HMGR_POWNED)
     {
         EngSetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }
 
-    return IntGdiDeleteDC(DCHandle, FALSE);
+    if (GDI_HANDLE_GET_TYPE(hobj) != GDI_OBJECT_TYPE_DC)
+        return GreDeleteObject(hobj);
+
+    // FIXME: everything should be callback based
+    return IntGdiDeleteDC(hobj, FALSE);
 }
 
 BOOL
@@ -975,45 +982,3 @@ IntGdiCreateDisplayDC(HDEV hDev, ULONG DcType, BOOL EmptyDC)
     return hDC;
 }
 
-BOOL
-FASTCALL
-IntGdiSetDCOwnerEx( HDC hDC, DWORD OwnerMask, BOOL NoSetBrush)
-{
-  PDC pDC;
-  BOOL Ret = FALSE;
-
-  if (!hDC || (GDI_HANDLE_GET_TYPE(hDC) != GDI_OBJECT_TYPE_DC)) return FALSE;
-
-  if ((OwnerMask == GDI_OBJ_HMGR_PUBLIC) || OwnerMask == GDI_OBJ_HMGR_NONE)
-  {
-     pDC = DC_LockDc ( hDC );
-     MmCopyFromCaller(&pDC->dcattr, pDC->pdcattr, sizeof(DC_ATTR));
-     DC_vFreeDcAttr(pDC);
-     DC_UnlockDc( pDC );
-
-     if (!DC_SetOwnership( hDC, NULL )) // This hDC is inaccessible!
-        return Ret;
-  }
-
-  if (OwnerMask == GDI_OBJ_HMGR_POWNED)
-  {
-     pDC = DC_LockDc ( hDC );
-     ASSERT(pDC->pdcattr == &pDC->dcattr);
-     DC_UnlockDc( pDC );
-
-     if (!DC_SetOwnership( hDC, PsGetCurrentProcess() )) return Ret;
-
-     DC_AllocateDcAttr( hDC );      // Allocate new dcattr
-
-     DCU_SynchDcAttrtoUser( hDC );  // Copy data from dc to dcattr
-  }
-
-  if ((OwnerMask != GDI_OBJ_HMGR_NONE) && !NoSetBrush)
-  {
-     pDC = DC_LockDc ( hDC );
-     if (IntGdiSetBrushOwner((PBRUSH)pDC->dclevel.pbrFill, OwnerMask))
-         IntGdiSetBrushOwner((PBRUSH)pDC->dclevel.pbrLine, OwnerMask);
-     DC_UnlockDc( pDC );
-  }
-  return TRUE;
-}