1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
42 void AquaSalGraphics::SetPrinterGraphics( CGContextRef xContext
, long nDPIX
, long nDPIY
)
52 // a previously set clip path is now invalid
55 CGPathRelease( mxClipPath
);
61 CGContextSetFillColorSpace( mrContext
, GetSalData()->mxRGBSpace
);
62 CGContextSetStrokeColorSpace( mrContext
, GetSalData()->mxRGBSpace
);
63 CGContextSaveGState( mrContext
);
68 void AquaSalGraphics::InvalidateContext()
74 void AquaSalGraphics::UnsetState()
78 SAL_INFO( "vcl.cg", "CGContextRestoreGState(" << mrContext
<< ")" );
79 CGContextRestoreGState( mrContext
);
84 SAL_INFO( "vcl.cg", "CGPathRelease(" << mxClipPath
<< ")" );
85 CGPathRelease( mxClipPath
);
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
) )
109 // prepare to release the corresponding resources
110 rReleaseContext
= mrContext
;
111 rReleaseLayer
= mxLayer
;
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
);
125 mrContext
= CGLayerGetContext( mxLayer
);
126 SAL_INFO( "vcl.cg", "CGLayerGetContext(" << mxLayer
<< ") = " << mrContext
);
131 // copy original layer to resized layer
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
);
146 // re-enable XOR emulation for the new context
149 mpXorEmulation
->SetTarget( mnWidth
, mnHeight
, mnBitmapDepth
, mrContext
, mxLayer
);
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()
180 * Blit the contents of our internal mxLayer state to the
181 * associated window, if any; cf. drawRect event handling
184 void AquaSalGraphics::UpdateWindow( NSRect
& )
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();
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
);
210 SAL_INFO( "vcl.cg", "CGContextDrawLayerAtPoint(" << rCGContext
<< "," << CGPointZero
<< "," << mxLayer
<< ")" );
211 CGContextDrawLayerAtPoint( rCGContext
, CGPointZero
, mxLayer
);
212 if( rClip
) // cleanup clipping
214 CGContextRestoreGState( rCGContext
);
219 SAL_WARN_IF( !mpFrame
->mbInitShow
, "vcl", "UpdateWindow called on uneligible graphics" );
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */