[Libreoffice-commits] core.git: 2 commits - vcl/inc vcl/osx vcl/quartz

Tomaž Vajngerl (via logerrit) logerrit at kemper.freedesktop.org
Fri May 17 08:39:07 UTC 2019


 vcl/inc/quartz/salgdi.h      |   47 ++++
 vcl/osx/salnativewidgets.cxx |   84 ++++----
 vcl/quartz/salgdi.cxx        |   52 ++---
 vcl/quartz/salgdicommon.cxx  |  414 ++++++++++++++++++++-----------------------
 vcl/quartz/salgdiutils.cxx   |   68 +++----
 5 files changed, 338 insertions(+), 327 deletions(-)

New commits:
commit 9e35b5a70844c8a0f6bc8e9dd8e0055cf5597b07
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Mon May 6 13:57:36 2019 +0900
Commit:     Tomaž Vajngerl <quikee at gmail.com>
CommitDate: Fri May 17 10:38:25 2019 +0200

    handle CG state save/restore in ContextHolder class
    
    Change-Id: I44ee257a8a196e8f2372dd01776c0c7c5193ad0a
    Reviewed-on: https://gerrit.libreoffice.org/72436
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <quikee at gmail.com>

diff --git a/vcl/inc/quartz/salgdi.h b/vcl/inc/quartz/salgdi.h
index ac906b339f9e..7d5df4bcbaf3 100644
--- a/vcl/inc/quartz/salgdi.h
+++ b/vcl/inc/quartz/salgdi.h
@@ -130,10 +130,16 @@ private:
 class CGContextHolder
 {
     CGContextRef mpContext;
+#if OSL_DEBUG_LEVEL > 0
+    int mnContextStackDepth;
+#endif
 public:
 
     CGContextHolder()
         : mpContext(nullptr)
+#if OSL_DEBUG_LEVEL > 0
+        , mnContextStackDepth( 0 )
+#endif
     {}
 
     CGContextRef get() const
@@ -150,6 +156,18 @@ public:
     {
         mpContext = pContext;
     }
+
+    void saveState()
+    {
+        SAL_INFO("vcl.cg", "CGContextSaveGState(" << mpContext << ") " << ++mnContextStackDepth );
+        CGContextSaveGState(mpContext);
+    }
+
+    void restoreState()
+    {
+        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mpContext << ") " << mnContextStackDepth-- );
+        CGContextRestoreGState(mpContext);
+    }
 };
 
 class AquaSalGraphics : public SalGraphics
@@ -159,7 +177,6 @@ class AquaSalGraphics : public SalGraphics
 #ifdef MACOSX
     AquaSalFrame*                           mpFrame;
 #endif
-    int                                     mnContextStackDepth;
     XorEmulation*                           mpXorEmulation;
     int                                     mnXorMode; // 0: off 1: on 2: invert only
     int                                     mnWidth;
diff --git a/vcl/osx/salnativewidgets.cxx b/vcl/osx/salnativewidgets.cxx
index 575abc976f8c..ad1362d438de 100644
--- a/vcl/osx/salnativewidgets.cxx
+++ b/vcl/osx/salnativewidgets.cxx
@@ -303,7 +303,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
     if( ! CheckContext() )
         return false;
 
-    CGContextSaveGState( maContextHolder.get() );
+    maContextHolder.saveState();
 
     tools::Rectangle buttonRect = rControlRegion;
     HIRect rc = ImplGetHIRectFromRectangle(buttonRect);
@@ -986,7 +986,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
     default: break;
     }
 
-    CGContextRestoreGState( maContextHolder.get() );
+    maContextHolder.restoreState();
 
     /* #i90291# in most cases invalidating the whole control region instead
        of just the unclipped part of it is sufficient (and probably faster).
diff --git a/vcl/quartz/salgdi.cxx b/vcl/quartz/salgdi.cxx
index 826b4bff4339..a68f9e9736ef 100644
--- a/vcl/quartz/salgdi.cxx
+++ b/vcl/quartz/salgdi.cxx
@@ -185,9 +185,6 @@ AquaSalGraphics::AquaSalGraphics()
 #ifdef MACOSX
     , mpFrame( nullptr )
 #endif
-#if OSL_DEBUG_LEVEL > 0
-    , mnContextStackDepth( 0 )
-#endif
     , mpXorEmulation( nullptr )
     , mnXorMode( 0 )
     , mnWidth( 0 )
@@ -449,8 +446,7 @@ void AquaSalGraphics::DrawTextLayout(const GenericSalLayout& rLayout)
     std::cerr << "]\n";
 #endif
 
-    SAL_INFO("vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
-    CGContextSaveGState(maContextHolder.get());
+    maContextHolder.saveState();
 
     // The view is vertically flipped (no idea why), flip it back.
     SAL_INFO("vcl.cg", "CGContextScaleCTM(" << maContextHolder.get() << ",1,-1)");
@@ -478,8 +474,7 @@ void AquaSalGraphics::DrawTextLayout(const GenericSalLayout& rLayout)
         size_t nStartIndex = std::distance(aGlyphOrientation.cbegin(), aIt);
         size_t nLen = std::distance(aIt, aNext);
 
-        SAL_INFO("vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
-        CGContextSaveGState(maContextHolder.get());
+        maContextHolder.saveState();
         if (rStyle.mfFontRotation && !bUprightGlyph)
         {
             SAL_INFO("vcl.cg", "CGContextRotateCTM(" << maContextHolder.get() << "," << rStyle.mfFontRotation << ")");
@@ -487,14 +482,12 @@ void AquaSalGraphics::DrawTextLayout(const GenericSalLayout& rLayout)
         }
         SAL_INFO("vcl.cg", "CTFontDrawGlyphs() @" << nStartIndex << ":" << nLen << "," << maContextHolder.get());
         CTFontDrawGlyphs(pFont, &aGlyphIds[nStartIndex], &aGlyphPos[nStartIndex], nLen, maContextHolder.get());
-        SAL_INFO("vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState(maContextHolder.get());
+        maContextHolder.restoreState();
 
         aIt = aNext;
     }
 
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
-    CGContextRestoreGState(maContextHolder.get());
+    maContextHolder.restoreState();
 }
 
 void AquaSalGraphics::SetFont(LogicalFontInstance* pReqFont, int nFallbackLevel)
diff --git a/vcl/quartz/salgdicommon.cxx b/vcl/quartz/salgdicommon.cxx
index b2c0d921d08b..dc73d3c95b28 100644
--- a/vcl/quartz/salgdicommon.cxx
+++ b/vcl/quartz/salgdicommon.cxx
@@ -629,8 +629,7 @@ bool AquaSalGraphics::drawTransformedBitmap(
 
     // setup the image transformation
     // using the rNull,rX,rY points as destinations for the (0,0),(0,Width),(Height,0) source points
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( maContextHolder.get() );
+    maContextHolder.saveState();
     const basegfx::B2DVector aXRel = rX - rNull;
     const basegfx::B2DVector aYRel = rY - rNull;
     const CGAffineTransform aCGMat = CGAffineTransformMake(
@@ -649,8 +648,7 @@ bool AquaSalGraphics::drawTransformedBitmap(
     SAL_INFO( "vcl.cg", "CGImageRelease(" << xImage << ")" );
     CGImageRelease( xImage );
     // restore the Quartz graphics state
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
-    CGContextRestoreGState(maContextHolder.get());
+    maContextHolder.restoreState();
 
     // mark the destination as painted
     const CGRect aDstRect = CGRectApplyAffineTransform( aSrcRect, aCGMat );
@@ -666,8 +664,7 @@ bool AquaSalGraphics::drawAlphaRect( long nX, long nY, long nWidth,
         return true;
 
     // save the current state
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( maContextHolder.get() );
+    maContextHolder.saveState();
     SAL_INFO( "vcl.cg", "CGContextSetAlpha(" << maContextHolder.get() << "," << (100-nTransparency) * (1.0/100) << ")" );
     CGContextSetAlpha( maContextHolder.get(), (100-nTransparency) * (1.0/100) );
 
@@ -685,9 +682,7 @@ bool AquaSalGraphics::drawAlphaRect( long nX, long nY, long nWidth,
     SAL_INFO( "vcl.cg", "CGContextDrawPath(" << maContextHolder.get() << ",kCGPathFill)" );
     CGContextDrawPath( maContextHolder.get(), kCGPathFill );
 
-    // restore state
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
-    CGContextRestoreGState(maContextHolder.get());
+    maContextHolder.restoreState();
     RefreshRect( aRect );
 
     return true;
@@ -752,8 +747,7 @@ bool AquaSalGraphics::drawEPS( long nX, long nY, long nWidth, long nHeight,
         return false;
     }
     // NOTE: flip drawing, else the nsimage would be drawn upside down
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( maContextHolder.get() );
+    maContextHolder.saveState();
 //  CGContextTranslateCTM( maContextHolder.get(), 0, +mnHeight );
     SAL_INFO( "vcl.cg", "CGContextScaleCTM(" << maContextHolder.get() << ",+1,-1)" );
     CGContextScaleCTM( maContextHolder.get(), +1, -1 );
@@ -776,8 +770,7 @@ bool AquaSalGraphics::drawEPS( long nX, long nY, long nWidth, long nHeight,
     [NSGraphicsContext setCurrentContext: pOrigNSCtx];
     [pOrigNSCtx release]; // restore the original retain count
 
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
-    CGContextRestoreGState( maContextHolder.get() );
+    maContextHolder.restoreState();
     // mark the destination rectangle as updated
     RefreshRect( aDstRect );
 
@@ -927,8 +920,7 @@ bool AquaSalGraphics::drawPolyLine(
     if( ! ((aRefreshRect.size.width <= 0.125) && (aRefreshRect.size.height <= 0.125)) )
     {
         // use the path to prepare the graphics context
-        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
-        CGContextSaveGState( maContextHolder.get() );
+        maContextHolder.saveState();
         SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
         CGContextBeginPath( maContextHolder.get() );
         SAL_INFO( "vcl.cg", "CGContextAddPath(" << maContextHolder.get() << "," << xPath << ")" );
@@ -943,8 +935,7 @@ bool AquaSalGraphics::drawPolyLine(
         CGContextSetMiterLimit(maContextHolder.get(), fCGMiterLimit);
         SAL_INFO( "vcl.cg", "CGContextDrawPath(" << maContextHolder.get() << ",kCGPathStroke)" );
         CGContextDrawPath( maContextHolder.get(), kCGPathStroke );
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState( maContextHolder.get() );
+        maContextHolder.restoreState();
 
         // mark modified rectangle as updated
         RefreshRect( aRefreshRect );
@@ -1021,8 +1012,7 @@ bool AquaSalGraphics::drawPolyPolygon(
         }
 
         // use the path to prepare the graphics context
-        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
-        CGContextSaveGState( maContextHolder.get() );
+        maContextHolder.saveState();
         SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
         CGContextBeginPath( maContextHolder.get() );
         SAL_INFO( "vcl.cg", "CGContextAddPath(" << maContextHolder.get() << "," << xPath << ")" );
@@ -1034,8 +1024,7 @@ bool AquaSalGraphics::drawPolyPolygon(
         CGContextSetAlpha( maContextHolder.get(), 1.0 - fTransparency );
         SAL_INFO( "vcl.cg", "CGContextDrawPath(" << maContextHolder.get() << "," << eMode << ")" );
         CGContextDrawPath( maContextHolder.get(), eMode );
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState( maContextHolder.get() );
+        maContextHolder.restoreState();
 
         // mark modified rectangle as updated
         RefreshRect( aRefreshRect );
@@ -1555,9 +1544,7 @@ void AquaSalGraphics::invert( long nX, long nY, long nWidth, long nHeight, SalIn
     if ( CheckContext() )
     {
         CGRect aCGRect = CGRectMake( nX, nY, nWidth, nHeight);
-        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth);
-        CGContextSaveGState(maContextHolder.get());
-
+        maContextHolder.saveState();
         if ( nFlags & SalInvert::TrackFrame )
         {
             const CGFloat dashLengths[2]  = { 4.0, 4.0 };     // for drawing dashed line
@@ -1584,8 +1571,7 @@ void AquaSalGraphics::invert( long nX, long nY, long nWidth, long nHeight, SalIn
             SAL_INFO( "vcl.cg", "CGContextFillRect(" << maContextHolder.get() << "," << aCGRect << ")" );
             CGContextFillRect ( maContextHolder.get(), aCGRect );
         }
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState( maContextHolder.get());
+        maContextHolder.restoreState();
         RefreshRect( aCGRect );
     }
 }
@@ -1609,8 +1595,7 @@ void AquaSalGraphics::invert( sal_uInt32 nPoints, const SalPoint*  pPtAry, SalIn
 {
     if ( CheckContext() )
     {
-        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth);
-        CGContextSaveGState(maContextHolder.get());
+        maContextHolder.saveState();
         CGPoint* CGpoints = makeCGptArray(nPoints,pPtAry);
         CGContextAddLines ( maContextHolder.get(), CGpoints, nPoints );
         if ( nSalFlags & SalInvert::TrackFrame )
@@ -1638,8 +1623,7 @@ void AquaSalGraphics::invert( sal_uInt32 nPoints, const SalPoint*  pPtAry, SalIn
             CGContextFillPath( maContextHolder.get() );
         }
         const CGRect aRefreshRect = CGContextGetClipBoundingBox(maContextHolder.get());
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState( maContextHolder.get());
+        maContextHolder.restoreState();
         delete []  CGpoints;
         RefreshRect( aRefreshRect );
     }
@@ -1680,10 +1664,8 @@ void AquaSalGraphics::ResetClipRegion()
 
 void AquaSalGraphics::SetState()
 {
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
-    CGContextRestoreGState( maContextHolder.get() );
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( maContextHolder.get() );
+    maContextHolder.restoreState();
+    maContextHolder.saveState();
 
     // setup clipping
     if( mxClipPath )
@@ -2093,8 +2075,7 @@ void AquaSalGraphics::SetVirDevGraphics( CGLayerRef xLayer, CGContextRef xContex
         // We will return early a few lines lower.
         // Undo the "stack initialization" done at the initial call of
         // this method, see end.
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
-        CGContextRestoreGState( maContextHolder.get() );
+        maContextHolder.restoreState();
     }
 #endif
 
@@ -2146,8 +2127,7 @@ void AquaSalGraphics::SetVirDevGraphics( CGLayerRef xLayer, CGContextRef xContex
     }
 
     // initialize stack of CGContext states
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( maContextHolder.get() );
+    maContextHolder.saveState();
     SetState();
 }
 
diff --git a/vcl/quartz/salgdiutils.cxx b/vcl/quartz/salgdiutils.cxx
index 80abba2268de..98213b7c3344 100644
--- a/vcl/quartz/salgdiutils.cxx
+++ b/vcl/quartz/salgdiutils.cxx
@@ -76,8 +76,7 @@ void AquaSalGraphics::UnsetState()
 {
     if (maContextHolder.isSet())
     {
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ")" );
-        CGContextRestoreGState( maContextHolder.get() );
+        maContextHolder.restoreState();
         maContextHolder.set(nullptr);
     }
     if( mxClipPath )
@@ -164,8 +163,7 @@ bool AquaSalGraphics::CheckContext()
                 CGContextScaleCTM(maContextHolder.get(), 1.0, -1.0);
                 CGContextSetFillColorSpace(maContextHolder.get(), GetSalData()->mxRGBSpace);
                 CGContextSetStrokeColorSpace(maContextHolder.get(), GetSalData()->mxRGBSpace);
-                SAL_INFO("vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth);
-                CGContextSaveGState(maContextHolder.get());
+                maContextHolder.saveState();
                 SetState();
 
                 // re-enable XOR emulation for the new context
commit 06b46104cc8b35e668e4b1a6d26a6a8e828fe373
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Mon May 6 13:47:10 2019 +0900
Commit:     Tomaž Vajngerl <quikee at gmail.com>
CommitDate: Fri May 17 10:38:11 2019 +0200

    Wrap CGContext into a ContextHolder class
    
    Change-Id: I3088e0b2f6c54f272fd29d7a6069e8231b207666
    Reviewed-on: https://gerrit.libreoffice.org/72435
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <quikee at gmail.com>

diff --git a/vcl/inc/quartz/salgdi.h b/vcl/inc/quartz/salgdi.h
index d04b290a8560..ac906b339f9e 100644
--- a/vcl/inc/quartz/salgdi.h
+++ b/vcl/inc/quartz/salgdi.h
@@ -127,11 +127,35 @@ private:
     std::unordered_map<sal_IntPtr, rtl::Reference<CoreTextFontFace>> maFontContainer;
 };
 
+class CGContextHolder
+{
+    CGContextRef mpContext;
+public:
+
+    CGContextHolder()
+        : mpContext(nullptr)
+    {}
+
+    CGContextRef get() const
+    {
+        return mpContext;
+    }
+
+    bool isSet() const
+    {
+        return mpContext != nullptr;
+    }
+
+    void set(CGContextRef const & pContext)
+    {
+        mpContext = pContext;
+    }
+};
 
 class AquaSalGraphics : public SalGraphics
 {
-    CGLayerRef                              mxLayer;    // Quartz graphics layer
-    CGContextRef                            mrContext;  // Quartz drawing context
+    CGLayerRef mxLayer; // Quartz graphics layer
+    CGContextHolder maContextHolder;  // Quartz drawing context
 #ifdef MACOSX
     AquaSalFrame*                           mpFrame;
 #endif
diff --git a/vcl/osx/salnativewidgets.cxx b/vcl/osx/salnativewidgets.cxx
index 91c694bfcb45..575abc976f8c 100644
--- a/vcl/osx/salnativewidgets.cxx
+++ b/vcl/osx/salnativewidgets.cxx
@@ -303,7 +303,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
     if( ! CheckContext() )
         return false;
 
-    CGContextSaveGState( mrContext );
+    CGContextSaveGState( maContextHolder.get() );
 
     tools::Rectangle buttonRect = rControlRegion;
     HIRect rc = ImplGetHIRectFromRectangle(buttonRect);
@@ -325,7 +325,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
             if( nState & ControlState::FOCUSED )
                 aComboInfo.adornment |= kThemeAdornmentFocus;
 
-            HIThemeDrawButton(&rc, &aComboInfo, mrContext, kHIThemeOrientationNormal,&rc);
+            HIThemeDrawButton(&rc, &aComboInfo, maContextHolder.get(), kHIThemeOrientationNormal,&rc);
             bOK = true;
         }
         break;
@@ -337,14 +337,14 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
             aMenuItemDrawInfo.version = 0;
             aMenuItemDrawInfo.state = kThemeMenuActive;
             aMenuItemDrawInfo.itemType = kThemeMenuItemHierBackground;
-            HIThemeDrawMenuItem(&rc,&rc,&aMenuItemDrawInfo,mrContext,kHIThemeOrientationNormal,NULL);
+            HIThemeDrawMenuItem(&rc,&rc,&aMenuItemDrawInfo,maContextHolder.get(),kHIThemeOrientationNormal,NULL);
 #else
             if (rControlRegion.Top() == 0 && nPart == ControlPart::DrawBackgroundHorz)
             {
                 const bool bDrawActive = mpFrame == nullptr || [mpFrame->getNSWindow() isKeyWindow];
                 CGFloat unifiedHeight = rControlRegion.GetHeight();
                 CGRect drawRect = CGRectMake(rControlRegion.Left(), rControlRegion.Top(), rControlRegion.GetWidth(), rControlRegion.GetHeight());
-                CUIDraw([NSWindow coreUIRenderer], drawRect, mrContext,
+                CUIDraw([NSWindow coreUIRenderer], drawRect, maContextHolder.get(),
                         reinterpret_cast<CFDictionaryRef>([NSDictionary dictionaryWithObjectsAndKeys:
                         @"kCUIWidgetWindowFrame", @"widget",
                         @"regularwin", @"windowtype",
@@ -361,7 +361,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                 aMenuItemDrawInfo.version = 0;
                 aMenuItemDrawInfo.state = kThemeMenuActive;
                 aMenuItemDrawInfo.itemType = kThemeMenuItemHierBackground;
-                HIThemeDrawMenuItem(&rc, &rc, &aMenuItemDrawInfo, mrContext, kHIThemeOrientationNormal, nullptr);
+                HIThemeDrawMenuItem(&rc, &rc, &aMenuItemDrawInfo, maContextHolder.get(), kHIThemeOrientationNormal, nullptr);
             }
 #endif
             bOK = true;
@@ -378,8 +378,8 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
             rc.size.width += 2;
             rc.size.height += 2;
 
-            HIThemeApplyBackground( &rc, &aThemeBackgroundInfo, mrContext, kHIThemeOrientationNormal);
-            CGContextFillRect( mrContext, rc );
+            HIThemeApplyBackground( &rc, &aThemeBackgroundInfo, maContextHolder.get(), kHIThemeOrientationNormal);
+            CGContextFillRect( maContextHolder.get(), rc );
             bOK = true;
         }
         break;
@@ -393,8 +393,8 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
             rc.size.width += 2;
             rc.size.height += 2;
 
-            HIThemeApplyBackground( &rc, &aThemeBackgroundInfo, mrContext, kHIThemeOrientationNormal);
-            CGContextFillRect( mrContext, rc );
+            HIThemeApplyBackground( &rc, &aThemeBackgroundInfo, maContextHolder.get(), kHIThemeOrientationNormal);
+            CGContextFillRect( maContextHolder.get(), rc );
             bOK = true;
         }
         break;
@@ -427,10 +427,10 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                 }
 
                 // repaints the background of the pull down menu
-                HIThemeDrawMenuBackground(&rc,&aMenuInfo,mrContext,kHIThemeOrientationNormal);
+                HIThemeDrawMenuBackground(&rc,&aMenuInfo,maContextHolder.get(),kHIThemeOrientationNormal);
 
                 // repaints the item either blue (selected) and/or Aqua grey (active only)
-                HIThemeDrawMenuItem(&rc,&rc,&aMenuItemDrawInfo,mrContext,kHIThemeOrientationNormal,&rc);
+                HIThemeDrawMenuItem(&rc,&rc,&aMenuItemDrawInfo,maContextHolder.get(),kHIThemeOrientationNormal,&rc);
 
                 bOK = true;
             }
@@ -450,7 +450,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
 
                     UniChar mark=( nPart == ControlPart::MenuItemCheckMark ) ? kCheckUnicode: kBulletUnicode;//0x2713;
                     CFStringRef cfString = CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, &mark, 1, kCFAllocatorNull);
-                    HIThemeDrawTextBox(cfString, &rc, &aTextInfo, mrContext, kHIThemeOrientationNormal);
+                    HIThemeDrawTextBox(cfString, &rc, &aTextInfo, maContextHolder.get(), kHIThemeOrientationNormal);
                     if (cfString)
                         CFRelease(cfString);
 
@@ -510,7 +510,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
             if( nState & ControlState::FOCUSED )
                 aPushInfo.adornment |= kThemeAdornmentFocus;
 
-            HIThemeDrawButton( &rc, &aPushInfo, mrContext, kHIThemeOrientationNormal, nullptr );
+            HIThemeDrawButton( &rc, &aPushInfo, maContextHolder.get(), kHIThemeOrientationNormal, nullptr );
             bOK = true;
         }
         break;
@@ -541,7 +541,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
               kThemeAdornmentNone;
             if( nState & ControlState::FOCUSED )
                 aInfo.adornment |= kThemeAdornmentFocus;
-            HIThemeDrawButton( &rc, &aInfo, mrContext, kHIThemeOrientationNormal, nullptr );
+            HIThemeDrawButton( &rc, &aInfo, maContextHolder.get(), kHIThemeOrientationNormal, nullptr );
             bOK = true;
         }
         break;
@@ -558,19 +558,19 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                 // grey equilateral triangle here ourselves.
                 // Perhaps some other HIThemeButtonDrawInfo setting would do the trick ?
 
-                CGContextSetShouldAntialias( mrContext, true );
+                CGContextSetShouldAntialias( maContextHolder.get(), true );
                 CGFloat const aGrey[] = { 0.45, 0.45, 0.45, 1.0 };
-                CGContextSetFillColor( mrContext, aGrey );
-                CGContextBeginPath( mrContext );
+                CGContextSetFillColor( maContextHolder.get(), aGrey );
+                CGContextBeginPath( maContextHolder.get() );
                 float x = rc.origin.x + rc.size.width;
                 float y = rc.origin.y;
-                CGContextMoveToPoint( mrContext, x, y );
+                CGContextMoveToPoint( maContextHolder.get(), x, y );
                 y += rc.size.height;
-                CGContextAddLineToPoint( mrContext, x, y );
+                CGContextAddLineToPoint( maContextHolder.get(), x, y );
                 x -= rc.size.height * 0.866; // cos( 30 degree ) is approx. 0.866
                 y -= rc.size.height/2;
-                CGContextAddLineToPoint( mrContext, x, y );
-                CGContextDrawPath( mrContext, kCGPathEOFill );
+                CGContextAddLineToPoint( maContextHolder.get(), x, y );
+                CGContextDrawPath( maContextHolder.get(), kCGPathEOFill );
             }
             else
             {
@@ -598,7 +598,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                         break;
                 }
 
-                HIThemeDrawButton( &rc, &aInfo, mrContext, kHIThemeOrientationNormal, nullptr );
+                HIThemeDrawButton( &rc, &aInfo, maContextHolder.get(), kHIThemeOrientationNormal, nullptr );
             }
             bOK = true;
         }
@@ -628,7 +628,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
             aTrackInfo.filler1              = 0;
             aTrackInfo.trackInfo.progress.phase   = static_cast<long long>(CFAbsoluteTimeGetCurrent()*10.0);
 
-            HIThemeDrawTrack( &aTrackInfo, nullptr, mrContext, kHIThemeOrientationNormal );
+            HIThemeDrawTrack( &aTrackInfo, nullptr, maContextHolder.get(), kHIThemeOrientationNormal );
             bOK = true;
         }
         break;
@@ -657,7 +657,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                 aSlideInfo.pressState = 0;
                 aTrackDraw.trackInfo.slider = aSlideInfo;
 
-                HIThemeDrawTrack( &aTrackDraw, nullptr, mrContext, kHIThemeOrientationNormal );
+                HIThemeDrawTrack( &aTrackDraw, nullptr, maContextHolder.get(), kHIThemeOrientationNormal );
                 bOK = true;
             }
         }
@@ -712,7 +712,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
 
                 aTrackDraw.trackInfo.scrollbar = aScrollInfo;
 
-                HIThemeDrawTrack( &aTrackDraw, nullptr, mrContext, kHIThemeOrientationNormal );
+                HIThemeDrawTrack( &aTrackDraw, nullptr, maContextHolder.get(), kHIThemeOrientationNormal );
                 bOK = true;
             }
         }
@@ -734,7 +734,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
             rc.size.height+=TAB_HEIGHT_NORMAL/2;
             rc.size.width-=2;
 
-            HIThemeDrawTabPane(&rc, &aTabPaneDrawInfo, mrContext, kHIThemeOrientationNormal);
+            HIThemeDrawTabPane(&rc, &aTabPaneDrawInfo, maContextHolder.get(), kHIThemeOrientationNormal);
 
             bOK = true;
         }
@@ -786,7 +786,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
             rc.size.width+=2;//because VCL has 2 empty pixels between 2 tabs
             rc.origin.x-=1;
 
-            HIThemeDrawTab(&rc, &aTabItemDrawInfo, mrContext, kHIThemeOrientationNormal, &rc );
+            HIThemeDrawTab(&rc, &aTabItemDrawInfo, maContextHolder.get(), kHIThemeOrientationNormal, &rc );
 
             bOK=true;
         }
@@ -808,7 +808,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                 if( nState & ControlState::FOCUSED )
                     aListInfo.adornment |= kThemeAdornmentFocus;
 
-                HIThemeDrawButton(&rc, &aListInfo, mrContext, kHIThemeOrientationNormal,&rc);
+                HIThemeDrawButton(&rc, &aListInfo, maContextHolder.get(), kHIThemeOrientationNormal,&rc);
                 bOK = true;
                 break;
             }
@@ -822,9 +822,9 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
 
                 rc.size.width+=1; // else there's a white space because a macOS theme has no 3D border
                 rc.size.height+=1;
-                HIThemeDrawFrame(&rc, &aTextDrawInfo, mrContext, kHIThemeOrientationNormal);
+                HIThemeDrawFrame(&rc, &aTextDrawInfo, maContextHolder.get(), kHIThemeOrientationNormal);
 
-                if(nState & ControlState::FOCUSED) HIThemeDrawFocusRect(&rc, true, mrContext, kHIThemeOrientationNormal);
+                if(nState & ControlState::FOCUSED) HIThemeDrawFocusRect(&rc, true, maContextHolder.get(), kHIThemeOrientationNormal);
 
                 bOK=true;
                 break;
@@ -849,12 +849,12 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
             rc.size.height += 2;
 
             //CGContextSetFillColorWithColor
-            CGContextFillRect (mrContext, CGRectMake(rc.origin.x, rc.origin.y, rc.size.width, rc.size.height));
+            CGContextFillRect (maContextHolder.get(), CGRectMake(rc.origin.x, rc.origin.y, rc.size.width, rc.size.height));
             //fill a white background, because drawFrame only draws the border
 
-            HIThemeDrawFrame(&rc, &aTextDrawInfo, mrContext, kHIThemeOrientationNormal);
+            HIThemeDrawFrame(&rc, &aTextDrawInfo, maContextHolder.get(), kHIThemeOrientationNormal);
 
-            if(nState & ControlState::FOCUSED) HIThemeDrawFocusRect(&rc, true, mrContext, kHIThemeOrientationNormal);
+            if(nState & ControlState::FOCUSED) HIThemeDrawFocusRect(&rc, true, maContextHolder.get(), kHIThemeOrientationNormal);
 
             bOK=true;
         }
@@ -878,12 +878,12 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                 rc.origin.y += FOCUS_RING_WIDTH;
 
                 //CGContextSetFillColorWithColor
-                CGContextFillRect (mrContext, CGRectMake(rc.origin.x, rc.origin.y, rc.size.width, rc.size.height));
+                CGContextFillRect (maContextHolder.get(), CGRectMake(rc.origin.x, rc.origin.y, rc.size.width, rc.size.height));
                 //fill a white background, because drawFrame only draws the border
 
-                HIThemeDrawFrame(&rc, &aTextDrawInfo, mrContext, kHIThemeOrientationNormal);
+                HIThemeDrawFrame(&rc, &aTextDrawInfo, maContextHolder.get(), kHIThemeOrientationNormal);
 
-                if(nState & ControlState::FOCUSED) HIThemeDrawFocusRect(&rc, true, mrContext, kHIThemeOrientationNormal);
+                if(nState & ControlState::FOCUSED) HIThemeDrawFocusRect(&rc, true, maContextHolder.get(), kHIThemeOrientationNormal);
 
                 //buttons:
                 const SpinbuttonValue* pSpinButtonVal = (aValue.getType() == ControlType::SpinButtons) ? static_cast<const SpinbuttonValue*>(&aValue) : nullptr;
@@ -934,7 +934,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                     if( (nUpperState & ControlState::FOCUSED) || (nLowerState & ControlState::FOCUSED))
                         aSpinInfo.adornment |= kThemeAdornmentFocus;
 
-                    HIThemeDrawButton( &buttonRc, &aSpinInfo, mrContext, kHIThemeOrientationNormal, nullptr );
+                    HIThemeDrawButton( &buttonRc, &aSpinInfo, maContextHolder.get(), kHIThemeOrientationNormal, nullptr );
                 }
 
                 bOK=true;
@@ -957,9 +957,9 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                         CGMutablePathRef rPath = CGPathCreateMutable();
                         CGPathAddRect( rPath, nullptr, CGRectMake( 0, 0, mpFrame->maGeometry.nWidth-1, mpFrame->maGeometry.nHeight-1 ) );
 
-                        CGContextBeginPath( mrContext );
-                        CGContextAddPath( mrContext, rPath );
-                        CGContextClip( mrContext );
+                        CGContextBeginPath( maContextHolder.get() );
+                        CGContextAddPath( maContextHolder.get(), rPath );
+                        CGContextClip( maContextHolder.get() );
                         CGPathRelease( rPath );
                     }
 
@@ -969,7 +969,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                     aTextDrawInfo.state=kThemeStateActive;
                     aTextDrawInfo.isFocused=false;
 
-                    HIThemeDrawFrame(&rc, &aTextDrawInfo, mrContext, kHIThemeOrientationNormal);
+                    HIThemeDrawFrame(&rc, &aTextDrawInfo, maContextHolder.get(), kHIThemeOrientationNormal);
 
                     bOK=true;
                 }
@@ -986,7 +986,7 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
     default: break;
     }
 
-    CGContextRestoreGState( mrContext );
+    CGContextRestoreGState( maContextHolder.get() );
 
     /* #i90291# in most cases invalidating the whole control region instead
        of just the unclipped part of it is sufficient (and probably faster).
diff --git a/vcl/quartz/salgdi.cxx b/vcl/quartz/salgdi.cxx
index 59e2522e58e1..826b4bff4339 100644
--- a/vcl/quartz/salgdi.cxx
+++ b/vcl/quartz/salgdi.cxx
@@ -182,7 +182,6 @@ bool CoreTextFontFace::GetFontCapabilities(vcl::FontCapabilities &rFontCapabilit
 
 AquaSalGraphics::AquaSalGraphics()
     : mxLayer( nullptr )
-    , mrContext( nullptr )
 #ifdef MACOSX
     , mpFrame( nullptr )
 #endif
@@ -247,16 +246,16 @@ AquaSalGraphics::~AquaSalGraphics()
         SAL_INFO("vcl.cg", "CGLayerRelease(" << mxLayer << ")" );
         CGLayerRelease( mxLayer );
     }
-    else if( mrContext
+    else if (maContextHolder.isSet()
 #ifdef MACOSX
              && mbWindow
 #endif
              )
     {
         // destroy backbuffer bitmap context that we created ourself
-        SAL_INFO("vcl.cg", "CGContextRelease(" << mrContext << ")" );
-        CGContextRelease( mrContext );
-        mrContext = nullptr;
+        SAL_INFO("vcl.cg", "CGContextRelease(" << maContextHolder.get() << ")" );
+        CGContextRelease(maContextHolder.get());
+        maContextHolder.set(nullptr);
     }
 }
 
@@ -450,23 +449,23 @@ void AquaSalGraphics::DrawTextLayout(const GenericSalLayout& rLayout)
     std::cerr << "]\n";
 #endif
 
-    SAL_INFO("vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
-    CGContextSaveGState(mrContext);
+    SAL_INFO("vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
+    CGContextSaveGState(maContextHolder.get());
 
     // The view is vertically flipped (no idea why), flip it back.
-    SAL_INFO("vcl.cg", "CGContextScaleCTM(" << mrContext << ",1,-1)");
-    CGContextScaleCTM(mrContext, 1.0, -1.0);
-    CGContextSetShouldAntialias(mrContext, !mbNonAntialiasedText);
-    SAL_INFO("vcl.cg", "CGContextSetFillColor(" << mrContext << "," << maTextColor << ")");
-    CGContextSetFillColor(mrContext, maTextColor.AsArray());
+    SAL_INFO("vcl.cg", "CGContextScaleCTM(" << maContextHolder.get() << ",1,-1)");
+    CGContextScaleCTM(maContextHolder.get(), 1.0, -1.0);
+    CGContextSetShouldAntialias(maContextHolder.get(), !mbNonAntialiasedText);
+    SAL_INFO("vcl.cg", "CGContextSetFillColor(" << maContextHolder.get() << "," << maTextColor << ")");
+    CGContextSetFillColor(maContextHolder.get(), maTextColor.AsArray());
 
     if (rStyle.mbFauxBold)
     {
 
         float fSize = rFontSelect.mnHeight / 23.0f;
-        CGContextSetStrokeColor(mrContext, maTextColor.AsArray());
-        CGContextSetLineWidth(mrContext, fSize);
-        CGContextSetTextDrawingMode(mrContext, kCGTextFillStroke);
+        CGContextSetStrokeColor(maContextHolder.get(), maTextColor.AsArray());
+        CGContextSetLineWidth(maContextHolder.get(), fSize);
+        CGContextSetTextDrawingMode(maContextHolder.get(), kCGTextFillStroke);
     }
 
     auto aIt = aGlyphOrientation.cbegin();
@@ -479,23 +478,23 @@ void AquaSalGraphics::DrawTextLayout(const GenericSalLayout& rLayout)
         size_t nStartIndex = std::distance(aGlyphOrientation.cbegin(), aIt);
         size_t nLen = std::distance(aIt, aNext);
 
-        SAL_INFO("vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
-        CGContextSaveGState(mrContext);
+        SAL_INFO("vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
+        CGContextSaveGState(maContextHolder.get());
         if (rStyle.mfFontRotation && !bUprightGlyph)
         {
-            SAL_INFO("vcl.cg", "CGContextRotateCTM(" << mrContext << "," << rStyle.mfFontRotation << ")");
-            CGContextRotateCTM(mrContext, rStyle.mfFontRotation);
+            SAL_INFO("vcl.cg", "CGContextRotateCTM(" << maContextHolder.get() << "," << rStyle.mfFontRotation << ")");
+            CGContextRotateCTM(maContextHolder.get(), rStyle.mfFontRotation);
         }
-        SAL_INFO("vcl.cg", "CTFontDrawGlyphs() @" << nStartIndex << ":" << nLen << "," << mrContext);
-        CTFontDrawGlyphs(pFont, &aGlyphIds[nStartIndex], &aGlyphPos[nStartIndex], nLen, mrContext);
-        SAL_INFO("vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState(mrContext);
+        SAL_INFO("vcl.cg", "CTFontDrawGlyphs() @" << nStartIndex << ":" << nLen << "," << maContextHolder.get());
+        CTFontDrawGlyphs(pFont, &aGlyphIds[nStartIndex], &aGlyphPos[nStartIndex], nLen, maContextHolder.get());
+        SAL_INFO("vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
+        CGContextRestoreGState(maContextHolder.get());
 
         aIt = aNext;
     }
 
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth-- );
-    CGContextRestoreGState(mrContext);
+    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
+    CGContextRestoreGState(maContextHolder.get());
 }
 
 void AquaSalGraphics::SetFont(LogicalFontInstance* pReqFont, int nFallbackLevel)
@@ -891,10 +890,10 @@ bool AquaSalGraphics::CheckContext()
 
 CGContextRef AquaSalGraphics::GetContext()
 {
-    if ( !mrContext )
+    if (!maContextHolder.isSet())
         CheckContext();
 
-    return mrContext;
+    return maContextHolder.get();
 }
 
 #endif
diff --git a/vcl/quartz/salgdicommon.cxx b/vcl/quartz/salgdicommon.cxx
index 772c538c7a5d..b2c0d921d08b 100644
--- a/vcl/quartz/salgdicommon.cxx
+++ b/vcl/quartz/salgdicommon.cxx
@@ -315,8 +315,8 @@ void AquaSalGraphics::copyBits( const SalTwoRect& rPosAry, SalGraphics *pSrcGrap
     }
 
 #ifdef IOS
-    // If called from idle layout, mrContext is NULL, no idea what to do
-    if (!mrContext)
+    // If called from idle layout, maContextHolder.get() is NULL, no idea what to do
+    if (!maContextHolder.isSet())
         return;
 #endif
 
@@ -358,7 +358,7 @@ void AquaSalGraphics::copyBits( const SalTwoRect& rPosAry, SalGraphics *pSrcGrap
     {
         // in XOR mode the drawing context is redirected to the XOR mask
         // if source and target are identical then copyBits() paints onto the target context though
-        CGContextRef xCopyContext = mrContext;
+        CGContextRef xCopyContext = maContextHolder.get();
         if( mpXorEmulation && mpXorEmulation->IsEnabled() )
         {
             if( pSrcGraphics == this )
@@ -490,7 +490,7 @@ void AquaSalGraphics::copyArea( long nDstX, long nDstY,long nSrcX, long nSrcY,
 
     // in XOR mode the drawing context is redirected to the XOR mask
     // copyArea() always works on the target context though
-    CGContextRef xCopyContext = mrContext;
+    CGContextRef xCopyContext = maContextHolder.get();
     if( mpXorEmulation && mpXorEmulation->IsEnabled() )
     {
         xCopyContext = mpXorEmulation->GetTargetContext();
@@ -596,8 +596,8 @@ bool AquaSalGraphics::drawAlphaBitmap( const SalTwoRect& rTR,
     if ( CheckContext() )
     {
         const CGRect aDstRect = CGRectMake( rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
-        SAL_INFO( "vcl.cg", "CGContextDrawImage(" << mrContext << "," << aDstRect << "," << xMaskedImage << ")" );
-        CGContextDrawImage( mrContext, aDstRect, xMaskedImage );
+        SAL_INFO( "vcl.cg", "CGContextDrawImage(" << maContextHolder.get() << "," << aDstRect << "," << xMaskedImage << ")" );
+        CGContextDrawImage( maContextHolder.get(), aDstRect, xMaskedImage );
         RefreshRect( aDstRect );
     }
 
@@ -629,8 +629,8 @@ bool AquaSalGraphics::drawTransformedBitmap(
 
     // setup the image transformation
     // using the rNull,rX,rY points as destinations for the (0,0),(0,Width),(Height,0) source points
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( mrContext );
+    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
+    CGContextSaveGState( maContextHolder.get() );
     const basegfx::B2DVector aXRel = rX - rNull;
     const basegfx::B2DVector aYRel = rY - rNull;
     const CGAffineTransform aCGMat = CGAffineTransformMake(
@@ -638,19 +638,19 @@ bool AquaSalGraphics::drawTransformedBitmap(
         aYRel.getX()/aSize.Height(), aYRel.getY()/aSize.Height(),
         rNull.getX(), rNull.getY());
 
-    SAL_INFO( "vcl.cg", "CGContextConcatCTM(" << mrContext << "," << aCGMat << ")" );
-    CGContextConcatCTM( mrContext, aCGMat );
+    SAL_INFO( "vcl.cg", "CGContextConcatCTM(" << maContextHolder.get() << "," << aCGMat << ")" );
+    CGContextConcatCTM( maContextHolder.get(), aCGMat );
 
     // draw the transformed image
     const CGRect aSrcRect = CGRectMake(0, 0, aSize.Width(), aSize.Height());
-    SAL_INFO( "vcl.cg", "CGContextDrawImage(" << mrContext << "," << aSrcRect << "," << xImage << ")" );
-    CGContextDrawImage( mrContext, aSrcRect, xImage );
+    SAL_INFO( "vcl.cg", "CGContextDrawImage(" << maContextHolder.get() << "," << aSrcRect << "," << xImage << ")" );
+    CGContextDrawImage( maContextHolder.get(), aSrcRect, xImage );
 
     SAL_INFO( "vcl.cg", "CGImageRelease(" << xImage << ")" );
     CGImageRelease( xImage );
     // restore the Quartz graphics state
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth--);
-    CGContextRestoreGState(mrContext);
+    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
+    CGContextRestoreGState(maContextHolder.get());
 
     // mark the destination as painted
     const CGRect aDstRect = CGRectApplyAffineTransform( aSrcRect, aCGMat );
@@ -666,10 +666,10 @@ bool AquaSalGraphics::drawAlphaRect( long nX, long nY, long nWidth,
         return true;
 
     // save the current state
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( mrContext );
-    SAL_INFO( "vcl.cg", "CGContextSetAlpha(" << mrContext << "," << (100-nTransparency) * (1.0/100) << ")" );
-    CGContextSetAlpha( mrContext, (100-nTransparency) * (1.0/100) );
+    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
+    CGContextSaveGState( maContextHolder.get() );
+    SAL_INFO( "vcl.cg", "CGContextSetAlpha(" << maContextHolder.get() << "," << (100-nTransparency) * (1.0/100) << ")" );
+    CGContextSetAlpha( maContextHolder.get(), (100-nTransparency) * (1.0/100) );
 
     CGRect aRect = CGRectMake(nX, nY, nWidth-1, nHeight-1);
     if( IsPenVisible() )
@@ -678,16 +678,16 @@ bool AquaSalGraphics::drawAlphaRect( long nX, long nY, long nWidth,
         aRect.origin.y += 0.5;
     }
 
-    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << mrContext << ")" );
-    CGContextBeginPath( mrContext );
-    SAL_INFO( "vcl.cg", "CGContextAddRect(" << mrContext << "," << aRect << ")" );
-    CGContextAddRect( mrContext, aRect );
-    SAL_INFO( "vcl.cg", "CGContextDrawPath(" << mrContext << ",kCGPathFill)" );
-    CGContextDrawPath( mrContext, kCGPathFill );
+    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
+    CGContextBeginPath( maContextHolder.get() );
+    SAL_INFO( "vcl.cg", "CGContextAddRect(" << maContextHolder.get() << "," << aRect << ")" );
+    CGContextAddRect( maContextHolder.get(), aRect );
+    SAL_INFO( "vcl.cg", "CGContextDrawPath(" << maContextHolder.get() << ",kCGPathFill)" );
+    CGContextDrawPath( maContextHolder.get(), kCGPathFill );
 
     // restore state
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth--);
-    CGContextRestoreGState(mrContext);
+    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
+    CGContextRestoreGState(maContextHolder.get());
     RefreshRect( aRect );
 
     return true;
@@ -705,8 +705,8 @@ void AquaSalGraphics::drawBitmap( const SalTwoRect& rPosAry, const SalBitmap& rS
         return;
 
     const CGRect aDstRect = CGRectMake(rPosAry.mnDestX, rPosAry.mnDestY, rPosAry.mnDestWidth, rPosAry.mnDestHeight);
-    SAL_INFO( "vcl.cg", "CGContextDrawImage(" << mrContext << "," << aDstRect << "," << xImage << ")" );
-    CGContextDrawImage( mrContext, aDstRect, xImage );
+    SAL_INFO( "vcl.cg", "CGContextDrawImage(" << maContextHolder.get() << "," << aDstRect << "," << xImage << ")" );
+    CGContextDrawImage( maContextHolder.get(), aDstRect, xImage );
 
     SAL_INFO( "vcl.cg", "CGImageRelease(" << xImage << ")" );
     CGImageRelease( xImage );
@@ -727,8 +727,8 @@ void AquaSalGraphics::drawBitmap( const SalTwoRect& rPosAry, const SalBitmap& rS
         return;
 
     const CGRect aDstRect = CGRectMake(rPosAry.mnDestX, rPosAry.mnDestY, rPosAry.mnDestWidth, rPosAry.mnDestHeight);
-    SAL_INFO( "vcl.cg", "CGContextDrawImage(" << mrContext << "," << aDstRect << "," << xMaskedImage << ")" );
-    CGContextDrawImage( mrContext, aDstRect, xMaskedImage );
+    SAL_INFO( "vcl.cg", "CGContextDrawImage(" << maContextHolder.get() << "," << aDstRect << "," << xMaskedImage << ")" );
+    CGContextDrawImage( maContextHolder.get(), aDstRect, xMaskedImage );
     SAL_INFO( "vcl.cg", "CGImageRelease(" << xMaskedImage << ")" );
     CGImageRelease( xMaskedImage );
     RefreshRect( aDstRect );
@@ -752,11 +752,11 @@ bool AquaSalGraphics::drawEPS( long nX, long nY, long nWidth, long nHeight,
         return false;
     }
     // NOTE: flip drawing, else the nsimage would be drawn upside down
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( mrContext );
-//  CGContextTranslateCTM( mrContext, 0, +mnHeight );
-    SAL_INFO( "vcl.cg", "CGContextScaleCTM(" << mrContext << ",+1,-1)" );
-    CGContextScaleCTM( mrContext, +1, -1 );
+    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
+    CGContextSaveGState( maContextHolder.get() );
+//  CGContextTranslateCTM( maContextHolder.get(), 0, +mnHeight );
+    SAL_INFO( "vcl.cg", "CGContextScaleCTM(" << maContextHolder.get() << ",+1,-1)" );
+    CGContextScaleCTM( maContextHolder.get(), +1, -1 );
     nY = /*mnHeight*/ - (nY + nHeight);
 
     // prepare the target context
@@ -764,7 +764,7 @@ bool AquaSalGraphics::drawEPS( long nX, long nY, long nWidth, long nHeight,
     [pOrigNSCtx retain];
 
     // create new context
-    NSGraphicsContext* pDrawNSCtx = [NSGraphicsContext graphicsContextWithCGContext: mrContext flipped: IsFlipped()];
+    NSGraphicsContext* pDrawNSCtx = [NSGraphicsContext graphicsContextWithCGContext: maContextHolder.get() flipped: IsFlipped()];
     // set it, setCurrentContext also releases the prviously set one
     [NSGraphicsContext setCurrentContext: pDrawNSCtx];
 
@@ -776,8 +776,8 @@ bool AquaSalGraphics::drawEPS( long nX, long nY, long nWidth, long nHeight,
     [NSGraphicsContext setCurrentContext: pOrigNSCtx];
     [pOrigNSCtx release]; // restore the original retain count
 
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth--);
-    CGContextRestoreGState( mrContext );
+    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
+    CGContextRestoreGState( maContextHolder.get() );
     // mark the destination rectangle as updated
     RefreshRect( aDstRect );
 
@@ -799,14 +799,14 @@ void AquaSalGraphics::drawLine( long nX1, long nY1, long nX2, long nY2 )
     if( !CheckContext() )
         return;
 
-    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << mrContext << ")" );
-    CGContextBeginPath( mrContext );
-    SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << mrContext << "," << static_cast<float>(nX1)+0.5 << "," << static_cast<float>(nY1)+0.5 << ")" );
-    CGContextMoveToPoint( mrContext, static_cast<float>(nX1)+0.5, static_cast<float>(nY1)+0.5 );
-    SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << mrContext << "," << static_cast<float>(nX2)+0.5 << "," << static_cast<float>(nY2)+0.5 << ")" );
-    CGContextAddLineToPoint( mrContext, static_cast<float>(nX2)+0.5, static_cast<float>(nY2)+0.5 );
-    SAL_INFO( "vcl.cg", "CGContextDrawPath(" << mrContext << ",kCGPathStroke)" );
-    CGContextDrawPath( mrContext, kCGPathStroke );
+    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
+    CGContextBeginPath( maContextHolder.get() );
+    SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << maContextHolder.get() << "," << static_cast<float>(nX1)+0.5 << "," << static_cast<float>(nY1)+0.5 << ")" );
+    CGContextMoveToPoint( maContextHolder.get(), static_cast<float>(nX1)+0.5, static_cast<float>(nY1)+0.5 );
+    SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << maContextHolder.get() << "," << static_cast<float>(nX2)+0.5 << "," << static_cast<float>(nY2)+0.5 << ")" );
+    CGContextAddLineToPoint( maContextHolder.get(), static_cast<float>(nX2)+0.5, static_cast<float>(nY2)+0.5 );
+    SAL_INFO( "vcl.cg", "CGContextDrawPath(" << maContextHolder.get() << ",kCGPathStroke)" );
+    CGContextDrawPath( maContextHolder.get(), kCGPathStroke );
 
     tools::Rectangle aRefreshRect( nX1, nY1, nX2, nY2 );
     (void) aRefreshRect;
@@ -826,8 +826,8 @@ void AquaSalGraphics::drawMask( const SalTwoRect& rPosAry, const SalBitmap& rSal
         return;
 
     const CGRect aDstRect = CGRectMake(rPosAry.mnDestX, rPosAry.mnDestY, rPosAry.mnDestWidth, rPosAry.mnDestHeight);
-    SAL_INFO( "vcl.cg", "CGContextDrawImage(" << mrContext << "," << aDstRect << "," << xImage << ")" );
-    CGContextDrawImage( mrContext, aDstRect, xImage );
+    SAL_INFO( "vcl.cg", "CGContextDrawImage(" << maContextHolder.get() << "," << aDstRect << "," << xImage << ")" );
+    CGContextDrawImage( maContextHolder.get(), aDstRect, xImage );
     SAL_INFO( "vcl.cg", "CGImageRelease(" << xImage << ")" );
     CGImageRelease( xImage );
     RefreshRect( aDstRect );
@@ -927,24 +927,24 @@ bool AquaSalGraphics::drawPolyLine(
     if( ! ((aRefreshRect.size.width <= 0.125) && (aRefreshRect.size.height <= 0.125)) )
     {
         // use the path to prepare the graphics context
-        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
-        CGContextSaveGState( mrContext );
-        SAL_INFO( "vcl.cg", "CGContextBeginPath(" << mrContext << ")" );
-        CGContextBeginPath( mrContext );
-        SAL_INFO( "vcl.cg", "CGContextAddPath(" << mrContext << "," << xPath << ")" );
-        CGContextAddPath( mrContext, xPath );
+        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
+        CGContextSaveGState( maContextHolder.get() );
+        SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
+        CGContextBeginPath( maContextHolder.get() );
+        SAL_INFO( "vcl.cg", "CGContextAddPath(" << maContextHolder.get() << "," << xPath << ")" );
+        CGContextAddPath( maContextHolder.get(), xPath );
         // draw path with antialiased line
-        CGContextSetShouldAntialias( mrContext, true );
-        SAL_INFO( "vcl.cg", "CGContextSetAlpha(" << mrContext << "," << 1.0 - fTransparency << ")" );
-        CGContextSetAlpha( mrContext, 1.0 - fTransparency );
-        CGContextSetLineJoin( mrContext, aCGLineJoin );
-        CGContextSetLineCap( mrContext, aCGLineCap );
-        CGContextSetLineWidth( mrContext, aLineWidths.getX() );
-        CGContextSetMiterLimit(mrContext, fCGMiterLimit);
-        SAL_INFO( "vcl.cg", "CGContextDrawPath(" << mrContext << ",kCGPathStroke)" );
-        CGContextDrawPath( mrContext, kCGPathStroke );
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState( mrContext );
+        CGContextSetShouldAntialias( maContextHolder.get(), true );
+        SAL_INFO( "vcl.cg", "CGContextSetAlpha(" << maContextHolder.get() << "," << 1.0 - fTransparency << ")" );
+        CGContextSetAlpha( maContextHolder.get(), 1.0 - fTransparency );
+        CGContextSetLineJoin( maContextHolder.get(), aCGLineJoin );
+        CGContextSetLineCap( maContextHolder.get(), aCGLineCap );
+        CGContextSetLineWidth( maContextHolder.get(), aLineWidths.getX() );
+        CGContextSetMiterLimit(maContextHolder.get(), fCGMiterLimit);
+        SAL_INFO( "vcl.cg", "CGContextDrawPath(" << maContextHolder.get() << ",kCGPathStroke)" );
+        CGContextDrawPath( maContextHolder.get(), kCGPathStroke );
+        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
+        CGContextRestoreGState( maContextHolder.get() );
 
         // mark modified rectangle as updated
         RefreshRect( aRefreshRect );
@@ -967,7 +967,7 @@ bool AquaSalGraphics::drawPolyPolygon(
     double fTransparency)
 {
 #ifdef IOS
-    if (!mrContext)
+    if (!maContextHolder.isSet())
         return true;
 #endif
 
@@ -1021,21 +1021,21 @@ bool AquaSalGraphics::drawPolyPolygon(
         }
 
         // use the path to prepare the graphics context
-        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
-        CGContextSaveGState( mrContext );
-        SAL_INFO( "vcl.cg", "CGContextBeginPath(" << mrContext << ")" );
-        CGContextBeginPath( mrContext );
-        SAL_INFO( "vcl.cg", "CGContextAddPath(" << mrContext << "," << xPath << ")" );
-        CGContextAddPath( mrContext, xPath );
+        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
+        CGContextSaveGState( maContextHolder.get() );
+        SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
+        CGContextBeginPath( maContextHolder.get() );
+        SAL_INFO( "vcl.cg", "CGContextAddPath(" << maContextHolder.get() << "," << xPath << ")" );
+        CGContextAddPath( maContextHolder.get(), xPath );
 
         // draw path with antialiased polygon
-        CGContextSetShouldAntialias( mrContext, true );
-        SAL_INFO( "vcl.cg", "CGContextSetAlpha(" << mrContext << "," << 1.0 - fTransparency << ")" );
-        CGContextSetAlpha( mrContext, 1.0 - fTransparency );
-        SAL_INFO( "vcl.cg", "CGContextDrawPath(" << mrContext << "," << eMode << ")" );
-        CGContextDrawPath( mrContext, eMode );
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState( mrContext );
+        CGContextSetShouldAntialias( maContextHolder.get(), true );
+        SAL_INFO( "vcl.cg", "CGContextSetAlpha(" << maContextHolder.get() << "," << 1.0 - fTransparency << ")" );
+        CGContextSetAlpha( maContextHolder.get(), 1.0 - fTransparency );
+        SAL_INFO( "vcl.cg", "CGContextDrawPath(" << maContextHolder.get() << "," << eMode << ")" );
+        CGContextDrawPath( maContextHolder.get(), eMode );
+        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
+        CGContextRestoreGState( maContextHolder.get() );
 
         // mark modified rectangle as updated
         RefreshRect( aRefreshRect );
@@ -1104,8 +1104,8 @@ void AquaSalGraphics::drawPolyPolygon( sal_uInt32 nPolyCount, const sal_uInt32 *
     }
 
     // convert to CGPath
-    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << mrContext << ")" );
-    CGContextBeginPath( mrContext );
+    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
+    CGContextBeginPath( maContextHolder.get() );
     if( IsPenVisible() )
     {
         for( sal_uInt32 nPoly = 0; nPoly < nPolyCount; nPoly++ )
@@ -1117,18 +1117,18 @@ void AquaSalGraphics::drawPolyPolygon( sal_uInt32 nPolyCount, const sal_uInt32 *
                 float fX, fY;
 
                 alignLinePoint( pPtAry, fX, fY );
-                SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << mrContext << "," << fX << "," << fY << ")" );
-                CGContextMoveToPoint( mrContext, fX, fY );
+                SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << maContextHolder.get() << "," << fX << "," << fY << ")" );
+                CGContextMoveToPoint( maContextHolder.get(), fX, fY );
                 pPtAry++;
 
                 for( sal_uInt32 nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
                 {
                     alignLinePoint( pPtAry, fX, fY );
-                    SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << mrContext << "," << fX << "," << fY << ")" );
-                    CGContextAddLineToPoint( mrContext, fX, fY );
+                    SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << maContextHolder.get() << "," << fX << "," << fY << ")" );
+                    CGContextAddLineToPoint( maContextHolder.get(), fX, fY );
                 }
-                SAL_INFO( "vcl.cg", "CGContextClosePath(" << mrContext << ")");
-                CGContextClosePath(mrContext);
+                SAL_INFO( "vcl.cg", "CGContextClosePath(" << maContextHolder.get() << ")");
+                CGContextClosePath(maContextHolder.get());
             }
         }
     }
@@ -1140,28 +1140,28 @@ void AquaSalGraphics::drawPolyPolygon( sal_uInt32 nPolyCount, const sal_uInt32 *
             if( nPoints > 1 )
             {
                 const SalPoint *pPtAry = ppPtAry[nPoly];
-                SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << mrContext << "," << pPtAry->mnX << "," << pPtAry->mnY << ")" );
-                CGContextMoveToPoint( mrContext, pPtAry->mnX, pPtAry->mnY );
+                SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << maContextHolder.get() << "," << pPtAry->mnX << "," << pPtAry->mnY << ")" );
+                CGContextMoveToPoint( maContextHolder.get(), pPtAry->mnX, pPtAry->mnY );
                 pPtAry++;
                 for( sal_uInt32 nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
                 {
-                    SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << mrContext << "," << pPtAry->mnX << "," << pPtAry->mnY << ")" );
-                    CGContextAddLineToPoint( mrContext, pPtAry->mnX, pPtAry->mnY );
+                    SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << maContextHolder.get() << "," << pPtAry->mnX << "," << pPtAry->mnY << ")" );
+                    CGContextAddLineToPoint( maContextHolder.get(), pPtAry->mnX, pPtAry->mnY );
                 }
-                SAL_INFO( "vcl.cg", "CGContextClosePath(" << mrContext << ")");
-                CGContextClosePath(mrContext);
+                SAL_INFO( "vcl.cg", "CGContextClosePath(" << maContextHolder.get() << ")");
+                CGContextClosePath(maContextHolder.get());
             }
         }
     }
 
-    SAL_INFO( "vcl.cg", "CGContextDrawPath(" << mrContext << "," <<
+    SAL_INFO( "vcl.cg", "CGContextDrawPath(" << maContextHolder.get() << "," <<
               (eMode == kCGPathFill ? "kCGPathFill" :
                (eMode == kCGPathEOFill ? "kCGPathEOFill" :
                 (eMode == kCGPathFillStroke ? "kCGPathFillStroke" :
                  (eMode == kCGPathEOFillStroke ? "kCGPathEOFillStroke" :
                   "???"))))
               << ")" );
-    CGContextDrawPath( mrContext, eMode );
+    CGContextDrawPath( maContextHolder.get(), eMode );
 
     RefreshRect( leftX, topY, maxWidth, maxHeight );
 }
@@ -1196,39 +1196,39 @@ void AquaSalGraphics::drawPolygon( sal_uInt32 nPoints, const SalPoint *pPtAry )
         return;
     }
 
-    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << mrContext << ")" );
-    CGContextBeginPath( mrContext );
+    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
+    CGContextBeginPath( maContextHolder.get() );
 
     if( IsPenVisible() )
     {
         float fX, fY;
         alignLinePoint( pPtAry, fX, fY );
-        SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << mrContext << "," << fX << "," << fY << ")" );
-        CGContextMoveToPoint( mrContext, fX, fY );
+        SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << maContextHolder.get() << "," << fX << "," << fY << ")" );
+        CGContextMoveToPoint( maContextHolder.get(), fX, fY );
         pPtAry++;
         for( sal_uInt32 nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
         {
             alignLinePoint( pPtAry, fX, fY );
-            SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << mrContext << "," << fX << "," << fY << ")" );
-            CGContextAddLineToPoint( mrContext, fX, fY );
+            SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << maContextHolder.get() << "," << fX << "," << fY << ")" );
+            CGContextAddLineToPoint( maContextHolder.get(), fX, fY );
         }
     }
     else
     {
-        SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << mrContext << "," << pPtAry->mnX << "," << pPtAry->mnY << ")" );
-        CGContextMoveToPoint( mrContext, pPtAry->mnX, pPtAry->mnY );
+        SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << maContextHolder.get() << "," << pPtAry->mnX << "," << pPtAry->mnY << ")" );
+        CGContextMoveToPoint( maContextHolder.get(), pPtAry->mnX, pPtAry->mnY );
         pPtAry++;
         for( sal_uInt32 nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
         {
-            SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << mrContext << "," << pPtAry->mnX << "," << pPtAry->mnY << ")" );
-            CGContextAddLineToPoint( mrContext, pPtAry->mnX, pPtAry->mnY );
+            SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << maContextHolder.get() << "," << pPtAry->mnX << "," << pPtAry->mnY << ")" );
+            CGContextAddLineToPoint( maContextHolder.get(), pPtAry->mnX, pPtAry->mnY );
         }
     }
 
-    SAL_INFO( "vcl.cg", "CGContextClosePath(" << mrContext << ")");
-    CGContextClosePath( mrContext );
-    SAL_INFO( "vcl.cg", "CGContextDrawPath(" << mrContext << "," << eMode << ")" );
-    CGContextDrawPath( mrContext, eMode );
+    SAL_INFO( "vcl.cg", "CGContextClosePath(" << maContextHolder.get() << ")");
+    CGContextClosePath( maContextHolder.get() );
+    SAL_INFO( "vcl.cg", "CGContextDrawPath(" << maContextHolder.get() << "," << eMode << ")" );
+    CGContextDrawPath( maContextHolder.get(), eMode );
     RefreshRect( nX, nY, nWidth, nHeight );
 }
 
@@ -1259,13 +1259,13 @@ void AquaSalGraphics::drawRect( long nX, long nY, long nWidth, long nHeight )
 
     if( IsBrushVisible() )
     {
-        SAL_INFO( "vcl.cg", "CGContextFillRect(" << mrContext << "," << aRect << ")" );
-        CGContextFillRect( mrContext, aRect );
+        SAL_INFO( "vcl.cg", "CGContextFillRect(" << maContextHolder.get() << "," << aRect << ")" );
+        CGContextFillRect( maContextHolder.get(), aRect );
     }
     if( IsPenVisible() )
     {
-        SAL_INFO( "vcl.cg", "CGContextStrokeRect(" << mrContext << "," << aRect << ")" );
-        CGContextStrokeRect( mrContext, aRect );
+        SAL_INFO( "vcl.cg", "CGContextStrokeRect(" << maContextHolder.get() << "," << aRect << ")" );
+        CGContextStrokeRect( maContextHolder.get(), aRect );
     }
     RefreshRect( nX, nY, nWidth, nHeight );
 }
@@ -1282,21 +1282,21 @@ void AquaSalGraphics::drawPolyLine( sal_uInt32 nPoints, const SalPoint *pPtAry )
     getBoundRect( nPoints, pPtAry, nX, nY, nWidth, nHeight );
 
     float fX, fY;
-    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << mrContext << ")" );
-    CGContextBeginPath( mrContext );
+    SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
+    CGContextBeginPath( maContextHolder.get() );
     alignLinePoint( pPtAry, fX, fY );
-    SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << mrContext << "," << fX << "," << fY << ")" );
-    CGContextMoveToPoint( mrContext, fX, fY );
+    SAL_INFO( "vcl.cg", "CGContextMoveToPoint(" << maContextHolder.get() << "," << fX << "," << fY << ")" );
+    CGContextMoveToPoint( maContextHolder.get(), fX, fY );
     pPtAry++;
 
     for( sal_uInt32 nPoint = 1; nPoint < nPoints; nPoint++, pPtAry++ )
     {
         alignLinePoint( pPtAry, fX, fY );
-        SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << mrContext << "," << fX << "," << fY << ")" );
-        CGContextAddLineToPoint( mrContext, fX, fY );
+        SAL_INFO( "vcl.cg", "CGContextAddLineToPoint(" << maContextHolder.get() << "," << fX << "," << fY << ")" );
+        CGContextAddLineToPoint( maContextHolder.get(), fX, fY );
     }
-    SAL_INFO( "vcl.cg", "CGContextStrokePath(" << mrContext << ")" );
-    CGContextStrokePath(mrContext);
+    SAL_INFO( "vcl.cg", "CGContextStrokePath(" << maContextHolder.get() << ")" );
+    CGContextStrokePath(maContextHolder.get());
 
     RefreshRect( nX, nY, nWidth, nHeight );
 }
@@ -1325,14 +1325,14 @@ SystemGraphicsData AquaSalGraphics::GetGraphicsData() const
 {
     SystemGraphicsData aRes;
     aRes.nSize = sizeof(aRes);
-    aRes.rCGContext = mrContext;
+    aRes.rCGContext = maContextHolder.get();
     return aRes;
 }
 
 long AquaSalGraphics::GetGraphicsWidth() const
 {
     long w = 0;
-    if( mrContext && (
+    if( maContextHolder.isSet() && (
 #ifndef IOS
                       mbWindow ||
 #endif
@@ -1428,16 +1428,16 @@ void AquaSalGraphics::ImplDrawPixel( long nX, long nY, const RGBAColor& rColor )
         return;
     }
     // overwrite the fill color
-    SAL_INFO( "vcl.cg", "CGContextSetFillColor(" << mrContext << "," << rColor << ")" );
-    CGContextSetFillColor( mrContext, rColor.AsArray() );
+    SAL_INFO( "vcl.cg", "CGContextSetFillColor(" << maContextHolder.get() << "," << rColor << ")" );
+    CGContextSetFillColor( maContextHolder.get(), rColor.AsArray() );
     // draw 1x1 rect, there is no pixel drawing in Quartz
     const CGRect aDstRect = CGRectMake(nX, nY, 1, 1);
-    SAL_INFO( "vcl.cg", "CGContextFillRect(" << mrContext << "," << aDstRect << ")" );
-    CGContextFillRect( mrContext, aDstRect );
+    SAL_INFO( "vcl.cg", "CGContextFillRect(" << maContextHolder.get() << "," << aDstRect << ")" );
+    CGContextFillRect( maContextHolder.get(), aDstRect );
     RefreshRect( aDstRect );
     // reset the fill color
-    SAL_INFO( "vcl.cg", "CGContextSetFillColor(" << mrContext << "," << maFillColor << ")" );
-    CGContextSetFillColor( mrContext, maFillColor.AsArray() );
+    SAL_INFO( "vcl.cg", "CGContextSetFillColor(" << maContextHolder.get() << "," << maFillColor << ")" );
+    CGContextSetFillColor( maContextHolder.get(), maFillColor.AsArray() );
 }
 
 #ifndef IOS
@@ -1555,37 +1555,37 @@ void AquaSalGraphics::invert( long nX, long nY, long nWidth, long nHeight, SalIn
     if ( CheckContext() )
     {
         CGRect aCGRect = CGRectMake( nX, nY, nWidth, nHeight);
-        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth);
-        CGContextSaveGState(mrContext);
+        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth);
+        CGContextSaveGState(maContextHolder.get());
 
         if ( nFlags & SalInvert::TrackFrame )
         {
             const CGFloat dashLengths[2]  = { 4.0, 4.0 };     // for drawing dashed line
-            CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
-            SAL_INFO( "vcl.cg", "CGContextSetRGBStrokeColor(" << mrContext << ",{1,1,1,1})" );
-            CGContextSetRGBStrokeColor ( mrContext, 1.0, 1.0, 1.0, 1.0 );
-            CGContextSetLineDash ( mrContext, 0, dashLengths, 2 );
-            CGContextSetLineWidth( mrContext, 2.0);
-            SAL_INFO( "vcl.cg", "CGContextStrokeRect(" << mrContext << "," << aCGRect << ")" );
-            CGContextStrokeRect ( mrContext, aCGRect );
+            CGContextSetBlendMode( maContextHolder.get(), kCGBlendModeDifference );
+            SAL_INFO( "vcl.cg", "CGContextSetRGBStrokeColor(" << maContextHolder.get() << ",{1,1,1,1})" );
+            CGContextSetRGBStrokeColor ( maContextHolder.get(), 1.0, 1.0, 1.0, 1.0 );
+            CGContextSetLineDash ( maContextHolder.get(), 0, dashLengths, 2 );
+            CGContextSetLineWidth( maContextHolder.get(), 2.0);
+            SAL_INFO( "vcl.cg", "CGContextStrokeRect(" << maContextHolder.get() << "," << aCGRect << ")" );
+            CGContextStrokeRect ( maContextHolder.get(), aCGRect );
         }
         else if ( nFlags & SalInvert::N50 )
         {
-            //CGContextSetAllowsAntialiasing( mrContext, false );
-            CGContextSetBlendMode(mrContext, kCGBlendModeDifference);
-            CGContextAddRect( mrContext, aCGRect );
+            //CGContextSetAllowsAntialiasing( maContextHolder.get(), false );
+            CGContextSetBlendMode(maContextHolder.get(), kCGBlendModeDifference);
+            CGContextAddRect( maContextHolder.get(), aCGRect );
             Pattern50Fill();
         }
         else // just invert
         {
-            CGContextSetBlendMode(mrContext, kCGBlendModeDifference);
-            SAL_INFO( "vcl.cg", "CGContextSetRGBFillColor(" << mrContext << ",{1,1,1,1})" );
-            CGContextSetRGBFillColor ( mrContext,1.0, 1.0, 1.0 , 1.0 );
-            SAL_INFO( "vcl.cg", "CGContextFillRect(" << mrContext << "," << aCGRect << ")" );
-            CGContextFillRect ( mrContext, aCGRect );
+            CGContextSetBlendMode(maContextHolder.get(), kCGBlendModeDifference);
+            SAL_INFO( "vcl.cg", "CGContextSetRGBFillColor(" << maContextHolder.get() << ",{1,1,1,1})" );
+            CGContextSetRGBFillColor ( maContextHolder.get(),1.0, 1.0, 1.0 , 1.0 );
+            SAL_INFO( "vcl.cg", "CGContextFillRect(" << maContextHolder.get() << "," << aCGRect << ")" );
+            CGContextFillRect ( maContextHolder.get(), aCGRect );
         }
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState( mrContext);
+        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
+        CGContextRestoreGState( maContextHolder.get());
         RefreshRect( aCGRect );
     }
 }
@@ -1609,37 +1609,37 @@ void AquaSalGraphics::invert( sal_uInt32 nPoints, const SalPoint*  pPtAry, SalIn
 {
     if ( CheckContext() )
     {
-        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth);
-        CGContextSaveGState(mrContext);
+        SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth);
+        CGContextSaveGState(maContextHolder.get());
         CGPoint* CGpoints = makeCGptArray(nPoints,pPtAry);
-        CGContextAddLines ( mrContext, CGpoints, nPoints );
+        CGContextAddLines ( maContextHolder.get(), CGpoints, nPoints );
         if ( nSalFlags & SalInvert::TrackFrame )
         {
             const CGFloat dashLengths[2]  = { 4.0, 4.0 };     // for drawing dashed line
-            CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
-            SAL_INFO( "vcl.cg", "CGContextSetRGBStrokeColor(" << mrContext << ",{1,1,1,1})" );
-            CGContextSetRGBStrokeColor ( mrContext, 1.0, 1.0, 1.0, 1.0 );
-            CGContextSetLineDash ( mrContext, 0, dashLengths, 2 );
-            CGContextSetLineWidth( mrContext, 2.0);
-            SAL_INFO( "vcl.cg", "CGContextStrokePath(" << mrContext << ")" );
-            CGContextStrokePath ( mrContext );
+            CGContextSetBlendMode( maContextHolder.get(), kCGBlendModeDifference );
+            SAL_INFO( "vcl.cg", "CGContextSetRGBStrokeColor(" << maContextHolder.get() << ",{1,1,1,1})" );
+            CGContextSetRGBStrokeColor ( maContextHolder.get(), 1.0, 1.0, 1.0, 1.0 );
+            CGContextSetLineDash ( maContextHolder.get(), 0, dashLengths, 2 );
+            CGContextSetLineWidth( maContextHolder.get(), 2.0);
+            SAL_INFO( "vcl.cg", "CGContextStrokePath(" << maContextHolder.get() << ")" );
+            CGContextStrokePath ( maContextHolder.get() );
         }
         else if ( nSalFlags & SalInvert::N50 )
         {
-            CGContextSetBlendMode(mrContext, kCGBlendModeDifference);
+            CGContextSetBlendMode(maContextHolder.get(), kCGBlendModeDifference);
             Pattern50Fill();
         }
         else // just invert
         {
-            CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
-            SAL_INFO( "vcl.cg", "CGContextSetRGBFillColor(" << mrContext << ",{1,1,1,1})" );
-            CGContextSetRGBFillColor( mrContext, 1.0, 1.0, 1.0, 1.0 );
-            SAL_INFO( "vcl.cg", "CGContextFillPath(" << mrContext << ")" );
-            CGContextFillPath( mrContext );
+            CGContextSetBlendMode( maContextHolder.get(), kCGBlendModeDifference );
+            SAL_INFO( "vcl.cg", "CGContextSetRGBFillColor(" << maContextHolder.get() << ",{1,1,1,1})" );
+            CGContextSetRGBFillColor( maContextHolder.get(), 1.0, 1.0, 1.0, 1.0 );
+            SAL_INFO( "vcl.cg", "CGContextFillPath(" << maContextHolder.get() << ")" );
+            CGContextFillPath( maContextHolder.get() );
         }
-        const CGRect aRefreshRect = CGContextGetClipBoundingBox(mrContext);
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth-- );
-        CGContextRestoreGState( mrContext);
+        const CGRect aRefreshRect = CGContextGetClipBoundingBox(maContextHolder.get());
+        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth-- );
+        CGContextRestoreGState( maContextHolder.get());
         delete []  CGpoints;
         RefreshRect( aRefreshRect );
     }
@@ -1654,13 +1654,13 @@ void AquaSalGraphics::Pattern50Fill()
                                                               CGAffineTransformIdentity, 4, 4,
                                                               kCGPatternTilingConstantSpacing,
                                                               false, &aCallback );
-    SAL_WARN_IF( !mrContext, "vcl.quartz", "mrContext is NULL" );
-    SAL_INFO( "vcl.cg", "CGContextSetFillColorSpace(" << mrContext << "," << mxP50Space << ")" );
-    CGContextSetFillColorSpace( mrContext, mxP50Space );
-    SAL_INFO( "vcl.cg", "CGContextSetFillPattern(" << mrContext << "," << mxP50Pattern << ",{1,1,1,1})" );
-    CGContextSetFillPattern( mrContext, mxP50Pattern, aFillCol );
-    SAL_INFO( "vcl.cg", "CGContextFillPath(" << mrContext << ")" );
-    CGContextFillPath( mrContext );
+    SAL_WARN_IF( !maContextHolder.get(), "vcl.quartz", "maContextHolder.get() is NULL" );
+    SAL_INFO( "vcl.cg", "CGContextSetFillColorSpace(" << maContextHolder.get() << "," << mxP50Space << ")" );
+    CGContextSetFillColorSpace( maContextHolder.get(), mxP50Space );
+    SAL_INFO( "vcl.cg", "CGContextSetFillPattern(" << maContextHolder.get() << "," << mxP50Pattern << ",{1,1,1,1})" );
+    CGContextSetFillPattern( maContextHolder.get(), mxP50Pattern, aFillCol );
+    SAL_INFO( "vcl.cg", "CGContextFillPath(" << maContextHolder.get() << ")" );
+    CGContextFillPath( maContextHolder.get() );
 }
 
 void AquaSalGraphics::ResetClipRegion()
@@ -1680,32 +1680,32 @@ void AquaSalGraphics::ResetClipRegion()
 
 void AquaSalGraphics::SetState()
 {
-    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth--);
-    CGContextRestoreGState( mrContext );
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( mrContext );
+    SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
+    CGContextRestoreGState( maContextHolder.get() );
+    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
+    CGContextSaveGState( maContextHolder.get() );
 
     // setup clipping
     if( mxClipPath )
     {
-        SAL_INFO( "vcl.cg", "CGContextBeginPath(" << mrContext << ")" );
-        CGContextBeginPath( mrContext );            // discard any existing path
-        SAL_INFO( "vcl.cg", "CGContextAddPath(" << mrContext << "," << mxClipPath << ")" );
-        CGContextAddPath( mrContext, mxClipPath );  // set the current path to the clipping path
-        SAL_INFO( "vcl.cg", "CGContextClip(" << mrContext << ")" );
-        CGContextClip( mrContext );                 // use it for clipping
+        SAL_INFO( "vcl.cg", "CGContextBeginPath(" << maContextHolder.get() << ")" );
+        CGContextBeginPath( maContextHolder.get() );            // discard any existing path
+        SAL_INFO( "vcl.cg", "CGContextAddPath(" << maContextHolder.get() << "," << mxClipPath << ")" );
+        CGContextAddPath( maContextHolder.get(), mxClipPath );  // set the current path to the clipping path
+        SAL_INFO( "vcl.cg", "CGContextClip(" << maContextHolder.get() << ")" );
+        CGContextClip( maContextHolder.get() );                 // use it for clipping
     }
 
     // set RGB colorspace and line and fill colors
-    SAL_INFO( "vcl.cg", "CGContextSetFillColor(" << mrContext << "," << maFillColor << ")" );
-    CGContextSetFillColor( mrContext, maFillColor.AsArray() );
+    SAL_INFO( "vcl.cg", "CGContextSetFillColor(" << maContextHolder.get() << "," << maFillColor << ")" );
+    CGContextSetFillColor( maContextHolder.get(), maFillColor.AsArray() );
 
-    SAL_INFO( "vcl.cg", "CGContextSetStrokeColor(" << mrContext << "," << maLineColor << ")" );
-    CGContextSetStrokeColor( mrContext, maLineColor.AsArray() );
-    CGContextSetShouldAntialias( mrContext, false );
+    SAL_INFO( "vcl.cg", "CGContextSetStrokeColor(" << maContextHolder.get() << "," << maLineColor << ")" );
+    CGContextSetStrokeColor( maContextHolder.get(), maLineColor.AsArray() );
+    CGContextSetShouldAntialias( maContextHolder.get(), false );
     if( mnXorMode == 2 )
     {
-        CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
+        CGContextSetBlendMode( maContextHolder.get(), kCGBlendModeDifference );
     }
 }
 
@@ -1714,8 +1714,8 @@ void AquaSalGraphics::SetLineColor()
     maLineColor.SetAlpha( 0.0 );   // transparent
     if( CheckContext() )
     {
-        SAL_INFO( "vcl.cg", "CGContextSetRGBStrokeColor(" << mrContext << "," << maLineColor << ")" );
-        CGContextSetRGBStrokeColor( mrContext, maLineColor.GetRed(), maLineColor.GetGreen(),
+        SAL_INFO( "vcl.cg", "CGContextSetRGBStrokeColor(" << maContextHolder.get() << "," << maLineColor << ")" );
+        CGContextSetRGBStrokeColor( maContextHolder.get(), maLineColor.GetRed(), maLineColor.GetGreen(),
                                     maLineColor.GetBlue(), maLineColor.GetAlpha() );
     }
 }
@@ -1725,8 +1725,8 @@ void AquaSalGraphics::SetLineColor( Color nColor )
     maLineColor = RGBAColor( nColor );
     if( CheckContext() )
     {
-        SAL_INFO( "vcl.cg", "CGContextSetRGBStrokeColor(" << mrContext << "," << maLineColor << ")" );
-        CGContextSetRGBStrokeColor( mrContext, maLineColor.GetRed(), maLineColor.GetGreen(),
+        SAL_INFO( "vcl.cg", "CGContextSetRGBStrokeColor(" << maContextHolder.get() << "," << maLineColor << ")" );
+        CGContextSetRGBStrokeColor( maContextHolder.get(), maLineColor.GetRed(), maLineColor.GetGreen(),
                                    maLineColor.GetBlue(), maLineColor.GetAlpha() );
     }
 }
@@ -1736,8 +1736,8 @@ void AquaSalGraphics::SetFillColor()
     maFillColor.SetAlpha( 0.0 );   // transparent
     if( CheckContext() )
     {
-        SAL_INFO( "vcl.cg", "CGContextSetRGBFillColor(" << mrContext << "," << maFillColor << ")" );
-        CGContextSetRGBFillColor( mrContext, maFillColor.GetRed(), maFillColor.GetGreen(),
+        SAL_INFO( "vcl.cg", "CGContextSetRGBFillColor(" << maContextHolder.get() << "," << maFillColor << ")" );
+        CGContextSetRGBFillColor( maContextHolder.get(), maFillColor.GetRed(), maFillColor.GetGreen(),
                                   maFillColor.GetBlue(), maFillColor.GetAlpha() );
     }
 }
@@ -1747,8 +1747,8 @@ void AquaSalGraphics::SetFillColor( Color nColor )
     maFillColor = RGBAColor( nColor );
     if( CheckContext() )
     {
-        SAL_INFO( "vcl.cg", "CGContextSetRGBFillColor(" << mrContext << "," << maFillColor << ")" );
-        CGContextSetRGBFillColor( mrContext, maFillColor.GetRed(), maFillColor.GetGreen(),
+        SAL_INFO( "vcl.cg", "CGContextSetRGBFillColor(" << maContextHolder.get() << "," << maFillColor << ")" );
+        CGContextSetRGBFillColor( maContextHolder.get(), maFillColor.GetRed(), maFillColor.GetGreen(),
                                  maFillColor.GetBlue(), maFillColor.GetAlpha() );
     }
 }
@@ -1842,13 +1842,13 @@ void AquaSalGraphics::SetXORMode( bool bSet, bool bInvertOnly )
     }
     if( ! bSet && mnXorMode == 2 )
     {
-        CGContextSetBlendMode( mrContext, kCGBlendModeNormal );
+        CGContextSetBlendMode( maContextHolder.get(), kCGBlendModeNormal );
         mnXorMode = 0;
         return;
     }
     else if( bSet && bInvertOnly && mnXorMode == 0)
     {
-        CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
+        CGContextSetBlendMode( maContextHolder.get(), kCGBlendModeDifference );
         mnXorMode = 2;
         return;
     }
@@ -1869,21 +1869,21 @@ void AquaSalGraphics::SetXORMode( bool bSet, bool bInvertOnly )
     if( !mpXorEmulation )
     {
         mpXorEmulation = new XorEmulation();
-        mpXorEmulation->SetTarget( mnWidth, mnHeight, mnBitmapDepth, mrContext, mxLayer );
+        mpXorEmulation->SetTarget( mnWidth, mnHeight, mnBitmapDepth, maContextHolder.get(), mxLayer );
     }
 
     // change the XOR mode
     if( bSet )
     {
         mpXorEmulation->Enable();
-        mrContext = mpXorEmulation->GetMaskContext();
+        maContextHolder.set(mpXorEmulation->GetMaskContext());
         mnXorMode = 1;
     }
     else
     {
         mpXorEmulation->UpdateTarget();
         mpXorEmulation->Disable();
-        mrContext = mpXorEmulation->GetTargetContext();
+        maContextHolder.set(mpXorEmulation->GetTargetContext());
         mnXorMode = 0;
     }
 }
@@ -2093,14 +2093,14 @@ void AquaSalGraphics::SetVirDevGraphics( CGLayerRef xLayer, CGContextRef xContex
         // We will return early a few lines lower.
         // Undo the "stack initialization" done at the initial call of
         // this method, see end.
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ") " << mnContextStackDepth--);
-        CGContextRestoreGState( mrContext );
+        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ") " << mnContextStackDepth--);
+        CGContextRestoreGState( maContextHolder.get() );
     }
 #endif
 
     // set graphics properties
     mxLayer = xLayer;
-    mrContext = xContext;
+    maContextHolder.set(xContext);
 
 #ifndef IOS
     mnBitmapDepth = nBitmapDepth;
@@ -2117,9 +2117,9 @@ void AquaSalGraphics::SetVirDevGraphics( CGLayerRef xLayer, CGContextRef xContex
     // get new graphics properties
     if( !mxLayer )
     {
-        mnWidth = CGBitmapContextGetWidth( mrContext );
-        mnHeight = CGBitmapContextGetHeight( mrContext );
-        SAL_INFO( "vcl.cg", "CGBitmapContextGetWidth&Height(" << mrContext <<
+        mnWidth = CGBitmapContextGetWidth( maContextHolder.get() );
+        mnHeight = CGBitmapContextGetHeight( maContextHolder.get() );
+        SAL_INFO( "vcl.cg", "CGBitmapContextGetWidth&Height(" << maContextHolder.get() <<
                   ") = " << mnWidth << "x" << mnHeight );
     }
     else
@@ -2132,22 +2132,22 @@ void AquaSalGraphics::SetVirDevGraphics( CGLayerRef xLayer, CGContextRef xContex
 
     // prepare graphics for drawing
     const CGColorSpaceRef aCGColorSpace = GetSalData()->mxRGBSpace;
-    CGContextSetFillColorSpace( mrContext, aCGColorSpace );
-    CGContextSetStrokeColorSpace( mrContext, aCGColorSpace );
+    CGContextSetFillColorSpace( maContextHolder.get(), aCGColorSpace );
+    CGContextSetStrokeColorSpace( maContextHolder.get(), aCGColorSpace );
 
     // re-enable XorEmulation for the new context
     if( mpXorEmulation )
     {
-        mpXorEmulation->SetTarget( mnWidth, mnHeight, mnBitmapDepth, mrContext, mxLayer );
+        mpXorEmulation->SetTarget( mnWidth, mnHeight, mnBitmapDepth, maContextHolder.get(), mxLayer );
         if( mpXorEmulation->IsEnabled() )
         {
-            mrContext = mpXorEmulation->GetMaskContext();
+            maContextHolder.set(mpXorEmulation->GetMaskContext());
         }
     }
 
     // initialize stack of CGContext states
-    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
-    CGContextSaveGState( mrContext );
+    SAL_INFO( "vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth );
+    CGContextSaveGState( maContextHolder.get() );
     SetState();
 }
 
diff --git a/vcl/quartz/salgdiutils.cxx b/vcl/quartz/salgdiutils.cxx
index ba94be73f132..80abba2268de 100644
--- a/vcl/quartz/salgdiutils.cxx
+++ b/vcl/quartz/salgdiutils.cxx
@@ -46,7 +46,7 @@ void AquaSalGraphics::SetPrinterGraphics( CGContextRef xContext, long nDPIX, lon
     mbPrinter = true;
     mbVirDev = false;
 
-    mrContext = xContext;
+    maContextHolder.set(xContext);
     mnRealDPIX = nDPIX;
     mnRealDPIY = nDPIY;
 
@@ -57,11 +57,11 @@ void AquaSalGraphics::SetPrinterGraphics( CGContextRef xContext, long nDPIX, lon
         mxClipPath = nullptr;
     }
 
-    if( mrContext )
+    if (maContextHolder.isSet())
     {
-        CGContextSetFillColorSpace( mrContext, GetSalData()->mxRGBSpace );
-        CGContextSetStrokeColorSpace( mrContext, GetSalData()->mxRGBSpace );
-        CGContextSaveGState( mrContext );
+        CGContextSetFillColorSpace( maContextHolder.get(), GetSalData()->mxRGBSpace );
+        CGContextSetStrokeColorSpace( maContextHolder.get(), GetSalData()->mxRGBSpace );
+        CGContextSaveGState( maContextHolder.get() );
         SetState();
     }
 }
@@ -69,16 +69,16 @@ void AquaSalGraphics::SetPrinterGraphics( CGContextRef xContext, long nDPIX, lon
 void AquaSalGraphics::InvalidateContext()
 {
     UnsetState();
-    mrContext = nullptr;
+    maContextHolder.set(nullptr);
 }
 
 void AquaSalGraphics::UnsetState()
 {
-    if( mrContext )
+    if (maContextHolder.isSet())
     {
-        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ")" );
-        CGContextRestoreGState( mrContext );
-        mrContext = nullptr;
+        SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << maContextHolder.get() << ")" );
+        CGContextRestoreGState( maContextHolder.get() );
+        maContextHolder.set(nullptr);
     }
     if( mxClipPath )
     {
@@ -109,16 +109,16 @@ bool AquaSalGraphics::CheckContext()
             // prepare to release the corresponding resources
             if (mxLayer)
                 rReleaseLayer = mxLayer;
-            else if (mrContext)
+            else if (maContextHolder.isSet())
             {
-                SAL_INFO("vcl.cg", "CGContextRelease(" << mrContext << ")");
-                CGContextRelease(mrContext);
+                SAL_INFO("vcl.cg", "CGContextRelease(" << maContextHolder.get() << ")");
+                CGContextRelease(maContextHolder.get());
             }
-            mrContext = nullptr;
+            maContextHolder.set(nullptr);
             mxLayer = nullptr;
         }
 
-        if (!mrContext)
+        if (!maContextHolder.isSet())
         {
             if (mpFrame->getNSWindow())
             {
@@ -129,17 +129,17 @@ bool AquaSalGraphics::CheckContext()
                 SAL_INFO("vcl.cg", "CGLayerCreateWithContext(" << xCGContext << "," << aLayerSize << ",NULL) = " << mxLayer);
                 if (mxLayer)
                 {
-                    mrContext = CGLayerGetContext( mxLayer );
-                    SAL_INFO( "vcl.cg", "CGLayerGetContext(" << mxLayer << ") = " << mrContext );
+                    maContextHolder.set(CGLayerGetContext(mxLayer));
+                    SAL_INFO( "vcl.cg", "CGLayerGetContext(" << mxLayer << ") = " << maContextHolder.get() );
                 }
 
                 if (rReleaseLayer)
                 {
                     // copy original layer to resized layer
-                    if (mrContext)
+                    if (maContextHolder.isSet())
                     {
-                        SAL_INFO("vcl.cg", "CGContextDrawLayerAtPoint(" << mrContext << "," << CGPointZero << "," << rReleaseLayer << ")");
-                        CGContextDrawLayerAtPoint(mrContext, CGPointZero, rReleaseLayer);
+                        SAL_INFO("vcl.cg", "CGContextDrawLayerAtPoint(" << maContextHolder.get() << "," << CGPointZero << "," << rReleaseLayer << ")");
+                        CGContextDrawLayerAtPoint(maContextHolder.get(), CGPointZero, rReleaseLayer);
                     }
                     SAL_INFO("vcl.cg", "CGLayerRelease(" << rReleaseLayer << ")");
                     CGLayerRelease(rReleaseLayer);
@@ -152,40 +152,40 @@ bool AquaSalGraphics::CheckContext()
                 const int nBytesPerRow = (nBitmapDepth * mnWidth) / 8;
                 void* pRawData = std::malloc(nBytesPerRow * mnHeight);
                 const int nFlags = kCGImageAlphaNoneSkipFirst;
-                mrContext = CGBitmapContextCreate(pRawData, mnWidth, mnHeight, 8, nBytesPerRow,
-                                                  GetSalData()->mxRGBSpace, nFlags);
+                maContextHolder.set(CGBitmapContextCreate(pRawData, mnWidth, mnHeight, 8, nBytesPerRow,
+                                                  GetSalData()->mxRGBSpace, nFlags));
                 SAL_INFO("vcl.cg", "CGBitmapContextCreate(" << mnWidth << "x" << mnHeight
-                                   << "x" << nBitmapDepth << ") = " << mrContext);
+                                   << "x" << nBitmapDepth << ") = " << maContextHolder.get());
             }
 
-            if (mrContext)
+            if (maContextHolder.isSet())
             {
-                CGContextTranslateCTM(mrContext, 0, nHeight);
-                CGContextScaleCTM(mrContext, 1.0, -1.0);
-                CGContextSetFillColorSpace(mrContext, GetSalData()->mxRGBSpace);
-                CGContextSetStrokeColorSpace(mrContext, GetSalData()->mxRGBSpace);
-                SAL_INFO("vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth);
-                CGContextSaveGState(mrContext);
+                CGContextTranslateCTM(maContextHolder.get(), 0, nHeight);
+                CGContextScaleCTM(maContextHolder.get(), 1.0, -1.0);
+                CGContextSetFillColorSpace(maContextHolder.get(), GetSalData()->mxRGBSpace);
+                CGContextSetStrokeColorSpace(maContextHolder.get(), GetSalData()->mxRGBSpace);
+                SAL_INFO("vcl.cg", "CGContextSaveGState(" << maContextHolder.get() << ") " << ++mnContextStackDepth);
+                CGContextSaveGState(maContextHolder.get());
                 SetState();
 
                 // re-enable XOR emulation for the new context
                 if (mpXorEmulation)
-                    mpXorEmulation->SetTarget(mnWidth, mnHeight, mnBitmapDepth, mrContext, mxLayer);
+                    mpXorEmulation->SetTarget(mnWidth, mnHeight, mnBitmapDepth, maContextHolder.get(), mxLayer);
             }
         }
     }
 
-    SAL_WARN_IF( !mrContext && !mbPrinter, "vcl", "<<<WARNING>>> AquaSalGraphics::CheckContext() FAILED!!!!" );
-    return (mrContext != nullptr);
+    SAL_WARN_IF( !maContextHolder.get() && !mbPrinter, "vcl", "<<<WARNING>>> AquaSalGraphics::CheckContext() FAILED!!!!" );
+    return maContextHolder.isSet();
 }
 
 CGContextRef AquaSalGraphics::GetContext()
 {
-    if(!mrContext)
+    if (!maContextHolder.isSet())
     {
         CheckContext();
     }
-    return mrContext;
+    return maContextHolder.get();
 }
 
 /**


More information about the Libreoffice-commits mailing list