Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / quartz / salgdiutils.cxx
blob6149eceadae4504b3fd5188321b963d008858f72
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <basegfx/polygon/b2dpolygon.hxx>
23 #include <basegfx/polygon/b2dpolygontools.hxx>
24 #include <basegfx/range/b2drectangle.hxx>
25 #include <basegfx/range/b2irange.hxx>
26 #include <basegfx/vector/b2ivector.hxx>
27 #include <vcl/svapp.hxx>
29 #include "quartz/salgdi.h"
30 #include "quartz/utils.h"
31 #include "osx/salframe.h"
32 #include "osx/saldata.hxx"
34 void AquaSalGraphics::SetWindowGraphics( AquaSalFrame* pFrame )
36 mpFrame = pFrame;
37 mbWindow = true;
38 mbPrinter = false;
39 mbVirDev = false;
42 void AquaSalGraphics::SetPrinterGraphics( CGContextRef xContext, long nDPIX, long nDPIY )
44 mbWindow = false;
45 mbPrinter = true;
46 mbVirDev = false;
48 mrContext = xContext;
49 mnRealDPIX = nDPIX;
50 mnRealDPIY = nDPIY;
52 // a previously set clip path is now invalid
53 if( mxClipPath )
55 CGPathRelease( mxClipPath );
56 mxClipPath = nullptr;
59 if( mrContext )
61 CGContextSetFillColorSpace( mrContext, GetSalData()->mxRGBSpace );
62 CGContextSetStrokeColorSpace( mrContext, GetSalData()->mxRGBSpace );
63 CGContextSaveGState( mrContext );
64 SetState();
68 void AquaSalGraphics::InvalidateContext()
70 UnsetState();
71 mrContext = nullptr;
74 void AquaSalGraphics::UnsetState()
76 if( mrContext )
78 SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext << ")" );
79 CGContextRestoreGState( mrContext );
80 mrContext = nullptr;
82 if( mxClipPath )
84 SAL_INFO( "vcl.cg", "CGPathRelease(" << mxClipPath << ")" );
85 CGPathRelease( mxClipPath );
86 mxClipPath = nullptr;
90 /**
91 * (re-)create the off-screen mxLayer we render everything to if
92 * necessary: eg. not initialized yet, or it has an incorrect size.
94 bool AquaSalGraphics::CheckContext()
96 if( mbWindow && mpFrame && mpFrame->getNSWindow() )
98 const unsigned int nWidth = mpFrame->maGeometry.nWidth;
99 const unsigned int nHeight = mpFrame->maGeometry.nHeight;
101 CGContextRef rReleaseContext = nullptr;
102 CGLayerRef rReleaseLayer = nullptr;
104 // check if a new drawing context is needed (e.g. after a resize)
105 if( (unsigned(mnWidth) != nWidth) || (unsigned(mnHeight) != nHeight) )
107 mnWidth = nWidth;
108 mnHeight = nHeight;
109 // prepare to release the corresponding resources
110 rReleaseContext = mrContext;
111 rReleaseLayer = mxLayer;
112 mrContext = nullptr;
113 mxLayer = nullptr;
116 if( !mrContext )
118 const CGSize aLayerSize = { static_cast<CGFloat>(nWidth), static_cast<CGFloat>(nHeight) };
119 NSGraphicsContext* pNSGContext = [NSGraphicsContext graphicsContextWithWindow: mpFrame->getNSWindow()];
120 CGContextRef xCGContext = static_cast<CGContextRef>([pNSGContext graphicsPort]);
121 mxLayer = CGLayerCreateWithContext( xCGContext, aLayerSize, nullptr );
122 SAL_INFO( "vcl.cg", "CGLayerCreateWithContext(" << xCGContext << "," << aLayerSize << ",NULL) = " << mxLayer );
123 if( mxLayer )
125 mrContext = CGLayerGetContext( mxLayer );
126 SAL_INFO( "vcl.cg", "CGLayerGetContext(" << mxLayer << ") = " << mrContext );
129 if( mrContext )
131 // copy original layer to resized layer
132 if( rReleaseLayer )
134 SAL_INFO( "vcl.cg", "CGContextDrawLayerAtPoint(" << mrContext << "," << CGPointZero << "," << rReleaseLayer << ")" );
135 CGContextDrawLayerAtPoint( mrContext, CGPointZero, rReleaseLayer );
138 CGContextTranslateCTM( mrContext, 0, nHeight );
139 CGContextScaleCTM( mrContext, 1.0, -1.0 );
140 CGContextSetFillColorSpace( mrContext, GetSalData()->mxRGBSpace );
141 CGContextSetStrokeColorSpace( mrContext, GetSalData()->mxRGBSpace );
142 SAL_INFO( "vcl.cg", "CGContextSaveGState(" << mrContext << ") " << ++mnContextStackDepth );
143 CGContextSaveGState( mrContext );
144 SetState();
146 // re-enable XOR emulation for the new context
147 if( mpXorEmulation )
149 mpXorEmulation->SetTarget( mnWidth, mnHeight, mnBitmapDepth, mrContext, mxLayer );
154 if( rReleaseLayer )
156 SAL_INFO( "vcl.cg", "CGLayerRelease(" << rReleaseLayer << ")" );
157 CGLayerRelease( rReleaseLayer );
159 else if( rReleaseContext )
161 SAL_INFO( "vcl.cg", "CGContextRelease(" << rReleaseContext << ")" );
162 CGContextRelease( rReleaseContext );
166 SAL_WARN_IF( !mrContext && !mbPrinter, "vcl", "<<<WARNING>>> AquaSalGraphics::CheckContext() FAILED!!!!" );
167 return (mrContext != nullptr);
170 CGContextRef AquaSalGraphics::GetContext()
172 if(!mrContext)
174 CheckContext();
176 return mrContext;
180 * Blit the contents of our internal mxLayer state to the
181 * associated window, if any; cf. drawRect event handling
182 * on the frame.
184 void AquaSalGraphics::UpdateWindow( NSRect& )
186 if( !mpFrame )
188 return;
191 NSGraphicsContext* pContext = [NSGraphicsContext currentContext];
192 if( (mxLayer != nullptr) && (pContext != nullptr) )
194 CGContextRef rCGContext = static_cast<CGContextRef>([pContext graphicsPort]);
195 SAL_INFO( "vcl.cg", "[[NSGraphicsContext currentContext] graphicsPort] = " << rCGContext );
197 CGMutablePathRef rClip = mpFrame->getClipPath();
198 if( rClip )
200 CGContextSaveGState( rCGContext );
201 SAL_INFO( "vcl.cg", "CGContextBeginPath(" << rCGContext << ")" );
202 CGContextBeginPath( rCGContext );
203 SAL_INFO( "vcl.cg", "CGContextAddPath(" << rCGContext << "," << rClip << ")" );
204 CGContextAddPath( rCGContext, rClip );
205 SAL_INFO( "vcl.cg", "CGContextClip(" << rCGContext << ")" );
206 CGContextClip( rCGContext );
209 ApplyXorContext();
210 SAL_INFO( "vcl.cg", "CGContextDrawLayerAtPoint(" << rCGContext << "," << CGPointZero << "," << mxLayer << ")" );
211 CGContextDrawLayerAtPoint( rCGContext, CGPointZero, mxLayer );
212 if( rClip ) // cleanup clipping
214 CGContextRestoreGState( rCGContext );
217 else
219 SAL_WARN_IF( !mpFrame->mbInitShow, "vcl", "UpdateWindow called on uneligible graphics" );
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */