1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26
27 #include <boost/bind.hpp>
28
29 #include "basebmp/scanlineformats.hxx"
30 #include "basebmp/color.hxx"
31
32 #include "basegfx/range/b2drectangle.hxx"
33 #include "basegfx/range/b2irange.hxx"
34 #include "basegfx/vector/b2ivector.hxx"
35 #include "basegfx/polygon/b2dpolygon.hxx"
36 #include "basegfx/polygon/b2dpolygontools.hxx"
37
38 #include "vcl/svapp.hxx"
39
40 #include "aqua/salgdi.h"
41 #include "aqua/salframe.h"
42 #include "aqua/saldata.hxx"
43
44 // ----------------------------------------------------------------------
45
SetWindowGraphics(AquaSalFrame * pFrame)46 void AquaSalGraphics::SetWindowGraphics( AquaSalFrame* pFrame )
47 {
48 mpFrame = pFrame;
49
50 mbWindow = true;
51 mbPrinter = false;
52 mbVirDev = false;
53 }
54
SetPrinterGraphics(CGContextRef xContext,long nDPIX,long nDPIY,double fScale)55 void AquaSalGraphics::SetPrinterGraphics( CGContextRef xContext, long nDPIX, long nDPIY, double fScale )
56 {
57 mbWindow = false;
58 mbPrinter = true;
59 mbVirDev = false;
60
61 mrContext = xContext;
62 mfFakeDPIScale = fScale;
63 mnRealDPIX = nDPIX;
64 mnRealDPIY = nDPIY;
65
66 // a previously set clip path is now invalid
67 if( mxClipPath )
68 {
69 CGPathRelease( mxClipPath );
70 mxClipPath = NULL;
71 }
72
73 if( mrContext )
74 {
75 CGContextSetFillColorSpace( mrContext, GetSalData()->mxRGBSpace );
76 CGContextSetStrokeColorSpace( mrContext, GetSalData()->mxRGBSpace );
77 CGContextSaveGState( mrContext );
78 SetState();
79 }
80 }
81
SetVirDevGraphics(const ScaledLayer & rLayer,CGContextRef xContext,int nBitmapDepth)82 void AquaSalGraphics::SetVirDevGraphics( const ScaledLayer& rLayer, CGContextRef xContext,
83 int nBitmapDepth )
84 {
85 mbWindow = false;
86 mbPrinter = false;
87 mbVirDev = true;
88
89 // set graphics properties
90 maLayer = rLayer.get() ? rLayer : ScaledLayer();
91 mrContext = xContext;
92 mnBitmapDepth = nBitmapDepth;
93
94 // return early if the virdev is being destroyed
95 if( !xContext )
96 return;
97
98 // get new graphics properties
99 if( !maLayer.get() )
100 {
101 mnWidth = CGBitmapContextGetWidth( mrContext );
102 mnHeight = CGBitmapContextGetHeight( mrContext );
103 }
104 else
105 {
106 const CGSize aSize = maLayer.getPointSize();
107 mnWidth = static_cast<int>(aSize.width);
108 mnHeight = static_cast<int>(aSize.height);
109 maLayer.scaleContext( mrContext );
110 }
111
112 // prepare graphics for drawing
113 const CGColorSpaceRef aCGColorSpace = GetSalData()->mxRGBSpace;
114 CGContextSetFillColorSpace( mrContext, aCGColorSpace );
115 CGContextSetStrokeColorSpace( mrContext, aCGColorSpace );
116
117 // re-enable XorEmulation for the new context
118 if( mpXorEmulation )
119 {
120 mpXorEmulation->SetTarget( mnWidth, mnHeight, mnBitmapDepth, mrContext, maLayer.get() );
121 if( mpXorEmulation->IsEnabled() )
122 mrContext = mpXorEmulation->GetMaskContext();
123 }
124
125 // initialize stack of CGContext states
126 CGContextSaveGState( mrContext );
127 SetState();
128 }
129
130 // ----------------------------------------------------------------------
131
InvalidateContext()132 void AquaSalGraphics::InvalidateContext()
133 {
134 UnsetState();
135 mrContext = 0;
136 }
137
138 // ----------------------------------------------------------------------
139
UnsetState()140 void AquaSalGraphics::UnsetState()
141 {
142 if( mrContext )
143 {
144 CGContextRestoreGState( mrContext );
145 mrContext = 0;
146 }
147 if( mxClipPath )
148 {
149 CGPathRelease( mxClipPath );
150 mxClipPath = NULL;
151 }
152 }
153
SetState()154 void AquaSalGraphics::SetState()
155 {
156 CGContextRestoreGState( mrContext );
157 CGContextSaveGState( mrContext );
158
159 // setup clipping
160 if( mxClipPath )
161 {
162 CGContextBeginPath( mrContext ); // discard any existing path
163 CGContextAddPath( mrContext, mxClipPath ); // set the current path to the clipping path
164 CGContextClip( mrContext ); // use it for clipping
165 }
166
167 // set RGB colorspace and line and fill colors
168 CGContextSetFillColor( mrContext, maFillColor.AsArray() );
169 CGContextSetStrokeColor( mrContext, maLineColor.AsArray() );
170 CGContextSetShouldAntialias( mrContext, false );
171 if( mnXorMode == 2 )
172 CGContextSetBlendMode( mrContext, kCGBlendModeDifference );
173 }
174
175 // ----------------------------------------------------------------------
176
CheckContext()177 bool AquaSalGraphics::CheckContext()
178 {
179 if( mbWindow && mpFrame && mpFrame->getNSWindow() )
180 {
181 const unsigned int nWidth = mpFrame->maGeometry.nWidth;
182 const unsigned int nHeight = mpFrame->maGeometry.nHeight;
183 const float fScale = mpFrame->getBackingScale();
184 const int nOldWidth = mnWidth, nOldHeight = mnHeight;
185
186 CGContextRef rReleaseContext = 0;
187 CGLayerRef rReleaseLayer = NULL;
188
189 // check if a new drawing context is needed (e.g. after a resize)
190 if( (unsigned(mnWidth) != nWidth) || (unsigned(mnHeight) != nHeight) || (maLayer.getScale() != fScale) )
191 {
192 mnWidth = nWidth;
193 mnHeight = nHeight;
194 // prepare to release the corresponding resources
195 rReleaseContext = mrContext;
196 rReleaseLayer = maLayer.get();
197 mrContext = NULL;
198 maLayer = ScaledLayer( NULL, fScale );
199 }
200
201 if( !mrContext )
202 {
203 const CGSize aLayerSize = maLayer.toPixels( nWidth, nHeight );
204 // a layer made from the window's context records a display list instead of pixels
205 CGContextRef xCGContext = CGBitmapContextCreate( NULL, 1, 1, 8, 4, GetSalData()->mxRGBSpace, kCGImageAlphaNoneSkipFirst );
206 maLayer = ScaledLayer( CGLayerCreateWithContext( xCGContext, aLayerSize, NULL ), maLayer.getScale() );
207 CGContextRelease( xCGContext );
208 if( maLayer.get() )
209 mrContext = CGLayerGetContext( maLayer.get() );
210
211 if( mrContext )
212 {
213 maLayer.scaleContext( mrContext );
214 // copy original layer to resized layer
215 if( rReleaseLayer )
216 CGContextDrawLayerInRect( mrContext, CGRectMake( 0, 0, nOldWidth, nOldHeight ), rReleaseLayer );
217
218 CGContextTranslateCTM( mrContext, 0, nHeight );
219 CGContextScaleCTM( mrContext, 1.0, -1.0 );
220 CGContextSetFillColorSpace( mrContext, GetSalData()->mxRGBSpace );
221 CGContextSetStrokeColorSpace( mrContext, GetSalData()->mxRGBSpace );
222 CGContextSaveGState( mrContext );
223 SetState();
224
225 // re-enable XOR emulation for the new context
226 if( mpXorEmulation )
227 {
228 mpXorEmulation->SetTarget( mnWidth, mnHeight, mnBitmapDepth, mrContext, maLayer.get() );
229 if( mpXorEmulation->IsEnabled() )
230 {
231 mrContext = mpXorEmulation->GetMaskContext();
232 SetState();
233 }
234 }
235 }
236 }
237
238 if( rReleaseLayer )
239 CGLayerRelease( rReleaseLayer );
240 else if( rReleaseContext )
241 CGContextRelease( rReleaseContext );
242 }
243
244 DBG_ASSERT( mrContext || mbPrinter, "<<<WARNING>>> AquaSalGraphics::CheckContext() FAILED!!!!\n" );
245 return (mrContext != NULL);
246 }
247
248
RefreshRect(float lX,float lY,float lWidth,float lHeight)249 void AquaSalGraphics::RefreshRect(float lX, float lY, float lWidth, float lHeight)
250 {
251 if( ! mbWindow ) // view only on Window graphics
252 return;
253
254 if( mpFrame )
255 {
256 // update a little more around the designated rectangle
257 // this helps with antialiased rendering
258 // Rounding down x and width can accumulate a rounding error of up to 2
259 // The decrementing of x, the rounding error and the antialiasing border
260 // require that the width and the height need to be increased by four
261 const Rectangle aVclRect(Point(static_cast<long int>(lX-1),
262 static_cast<long int>(lY-1) ),
263 Size( static_cast<long int>(lWidth+4),
264 static_cast<long int>(lHeight+4) ) );
265 mpFrame->maInvalidRect.Union( aVclRect );
266 }
267 }
268
makeCGptArray(sal_uLong nPoints,const SalPoint * pPtAry)269 CGPoint* AquaSalGraphics::makeCGptArray(sal_uLong nPoints, const SalPoint* pPtAry)
270 {
271 CGPoint *CGpoints = new CGPoint[ nPoints];
272 if ( CGpoints )
273 {
274 for(sal_uLong i=0;i<nPoints;i++)
275 {
276 CGpoints[i].x = (float)(pPtAry[i].mnX);
277 CGpoints[i].y = (float)(pPtAry[i].mnY);
278 }
279 }
280 return CGpoints;
281 }
282
283 // -----------------------------------------------------------------------
284
UpdateWindow(NSRect &)285 void AquaSalGraphics::UpdateWindow( NSRect& )
286 {
287 if( !mpFrame )
288 return;
289 NSGraphicsContext* pContext = [NSGraphicsContext currentContext];
290 if( (maLayer.get() != NULL) && (pContext != NULL) )
291 {
292 CGContextRef rCGContext = reinterpret_cast<CGContextRef>([pContext graphicsPort]);
293
294 CGMutablePathRef rClip = mpFrame->getClipPath();
295 if( rClip )
296 {
297 CGContextSaveGState( rCGContext );
298 CGContextBeginPath( rCGContext );
299 CGContextAddPath( rCGContext, rClip );
300 CGContextClip( rCGContext );
301 }
302
303 ApplyXorContext();
304 CGContextDrawLayerInRect( rCGContext, CGRectMake( 0, 0, mnWidth, mnHeight ), maLayer.get() );
305 if( rClip ) // cleanup clipping
306 CGContextRestoreGState( rCGContext );
307 }
308 else
309 DBG_ASSERT( mpFrame->mbInitShow, "UpdateWindow called on uneligible graphics" );
310 }
311
312 // -----------------------------------------------------------------------
313