1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <cstdint>
#include <sal/log.hxx>
#include <basegfx/polygon/b2dpolygon.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/range/b2drectangle.hxx>
#include <basegfx/range/b2irange.hxx>
#include <basegfx/vector/b2ivector.hxx>
#include <vcl/svapp.hxx>
#include <quartz/salgdi.h>
#include <quartz/utils.h>
#include <osx/salframe.h>
#include <osx/saldata.hxx>
#if HAVE_FEATURE_SKIA
#include <tools/sk_app/mac/WindowContextFactory_mac.h>
#include <vcl/skia/SkiaHelper.hxx>
#endif
static bool bTotalScreenBounds = false;
static NSRect aTotalScreenBounds = NSZeroRect;
// TODO: Scale will be set to 2.0f as default after implementation of full scaled display support . This will allow moving of
// windows between non retina and retina displays without blurry text and graphics. Static variables have to be removed thereafter.
// Currently scaled display support is not implemented for bitmaps. This will cause a slight performance degradation on displays
// with single precision. To preserve performance for now, window scaling is only activated if at least one display with double
// precision is present. Moving windows between displays is then possible without blurry text and graphics too. Adapting window
// scaling when displays are added while application is running is not supported.
static bool bWindowScaling = false;
static float fWindowScale = 1.0f;
namespace sal::aqua
{
NSRect getTotalScreenBounds()
{
if (!bTotalScreenBounds)
{
aTotalScreenBounds = NSZeroRect;
NSArray *aScreens = [NSScreen screens];
if (aScreens != nullptr)
{
for (NSScreen *aScreen : aScreens)
{
// Calculate total screen bounds
NSRect aScreenFrame = [aScreen frame];
if (!NSIsEmptyRect(aScreenFrame))
{
if (NSIsEmptyRect(aTotalScreenBounds))
aTotalScreenBounds = aScreenFrame;
else
aTotalScreenBounds = NSUnionRect( aScreenFrame, aTotalScreenBounds );
}
}
bTotalScreenBounds = true;
}
}
return aTotalScreenBounds;
}
void resetTotalScreenBounds()
{
bTotalScreenBounds = false;
getTotalScreenBounds();
}
float getWindowScaling()
{
// Related: tdf#147342 Any changes to this function must be copied to the
// sk_app::GetBackingScaleFactor() function in the following file:
// workdir/UnpackedTarball/skia/tools/sk_app/mac/WindowContextFactory_mac.h
if (!bWindowScaling)
{
NSArray *aScreens = [NSScreen screens];
if (aScreens != nullptr)
{
for (NSScreen *aScreen : aScreens)
{
float fScale = [aScreen backingScaleFactor];
if (fScale > fWindowScale)
fWindowScale = fScale;
}
bWindowScaling = true;
}
if( const char* env = getenv("SAL_FORCE_HIDPI_SCALING"))
{
fWindowScale = atof(env);
bWindowScaling = true;
}
}
return fWindowScale;
}
void resetWindowScaling()
{
// Related: tdf#147342 Force recalculation of the window scaling but keep
// the previous window scaling as the minimum so that we don't lose the
// resolution in cached images if a HiDPI monitor is disconnected and
// then reconnected.
#if HAVE_FEATURE_SKIA
if ( SkiaHelper::isVCLSkiaEnabled() )
sk_app::ResetBackingScaleFactor();
#endif
bWindowScaling = false;
getWindowScaling();
}
} // end aqua
void AquaSalGraphics::SetWindowGraphics( AquaSalFrame* pFrame )
{
maShared.mpFrame = pFrame;
maShared.mbWindow = true;
maShared.mbPrinter = false;
maShared.mbVirDev = false;
mpBackend->UpdateGeometryProvider(pFrame);
}
void AquaSalGraphics::SetPrinterGraphics( CGContextRef xContext, sal_Int32 nDPIX, sal_Int32 nDPIY )
{
maShared.mbWindow = false;
maShared.mbPrinter = true;
maShared.mbVirDev = false;
maShared.maContextHolder.set(xContext);
mnRealDPIX = nDPIX;
mnRealDPIY = nDPIY;
// a previously set clip path is now invalid
maShared.unsetClipPath();
if (maShared.maContextHolder.isSet())
{
CGContextSetFillColorSpace( maShared.maContextHolder.get(), GetSalData()->mxRGBSpace );
CGContextSetStrokeColorSpace( maShared.maContextHolder.get(), GetSalData()->mxRGBSpace );
CGContextSaveGState( maShared.maContextHolder.get() );
maShared.setState();
}
mpBackend->UpdateGeometryProvider(nullptr);
}
void AquaSalGraphics::InvalidateContext()
{
UnsetState();
CGContextRelease(maShared.maContextHolder.get());
CGContextRelease(maShared.maBGContextHolder.get());
CGContextRelease(maShared.maCSContextHolder.get());
maShared.maContextHolder.set(nullptr);
maShared.maCSContextHolder.set(nullptr);
maShared.maBGContextHolder.set(nullptr);
}
void AquaSalGraphics::UnsetState()
{
if (maShared.maBGContextHolder.isSet())
{
CGContextRelease(maShared.maBGContextHolder.get());
maShared.maBGContextHolder.set(nullptr);
}
if (maShared.maCSContextHolder.isSet())
{
CGContextRelease(maShared.maCSContextHolder.get());
maShared.maBGContextHolder.set(nullptr);
}
if (maShared.maContextHolder.isSet())
{
maShared.maContextHolder.restoreState();
maShared.maContextHolder.set(nullptr);
}
maShared.unsetState();
}
/**
* (re-)create the off-screen maLayer we render everything to if
* necessary: eg. not initialized yet, or it has an incorrect size.
*/
bool AquaSharedAttributes::checkContext()
{
if (mbWindow && mpFrame && (mpFrame->getNSWindow() || Application::IsBitmapRendering()))
{
const unsigned int nWidth = mpFrame->maGeometry.width();
const unsigned int nHeight = mpFrame->maGeometry.height();
const float fScale = sal::aqua::getWindowScaling();
CGLayerRef rReleaseLayer = nullptr;
// check if a new drawing context is needed (e.g. after a resize)
if( (unsigned(mnWidth) != nWidth) || (unsigned(mnHeight) != nHeight) )
{
mnWidth = nWidth;
mnHeight = nHeight;
// prepare to release the corresponding resources
if (maLayer.isSet())
{
rReleaseLayer = maLayer.get();
}
else if (maContextHolder.isSet())
{
CGContextRelease(maContextHolder.get());
}
CGContextRelease(maBGContextHolder.get());
CGContextRelease(maCSContextHolder.get());
maContextHolder.set(nullptr);
maBGContextHolder.set(nullptr);
maCSContextHolder.set(nullptr);
maLayer.set(nullptr);
}
if (!maContextHolder.isSet())
{
const int nBitmapDepth = 32;
float nScaledWidth = mnWidth * fScale;
float nScaledHeight = mnHeight * fScale;
const CGSize aLayerSize = { static_cast<CGFloat>(nScaledWidth), static_cast<CGFloat>(nScaledHeight) };
const int nBytesPerRow = (nBitmapDepth * nScaledWidth) / 8;
std::uint32_t nFlags = std::uint32_t(kCGImageAlphaNoneSkipFirst)
| std::uint32_t(kCGBitmapByteOrder32Host);
maBGContextHolder.set(CGBitmapContextCreate(
nullptr, nScaledWidth, nScaledHeight, 8, nBytesPerRow, GetSalData()->mxRGBSpace, nFlags));
maLayer.set(CGLayerCreateWithContext(maBGContextHolder.get(), aLayerSize, nullptr));
maLayer.setScale(fScale);
nFlags = std::uint32_t(kCGImageAlphaPremultipliedFirst)
| std::uint32_t(kCGBitmapByteOrder32Host);
maCSContextHolder.set(CGBitmapContextCreate(
nullptr, nScaledWidth, nScaledHeight, 8, nBytesPerRow, GetSalData()->mxRGBSpace, nFlags));
CGContextRef xDrawContext = CGLayerGetContext(maLayer.get());
maContextHolder = xDrawContext;
if (rReleaseLayer)
{
// copy original layer to resized layer
if (maContextHolder.isSet())
{
CGContextDrawLayerAtPoint(maContextHolder.get(), CGPointZero, rReleaseLayer);
}
CGLayerRelease(rReleaseLayer);
}
if (maContextHolder.isSet())
{
CGContextTranslateCTM(maContextHolder.get(), 0, nScaledHeight);
CGContextScaleCTM(maContextHolder.get(), 1.0, -1.0);
CGContextSetFillColorSpace(maContextHolder.get(), GetSalData()->mxRGBSpace);
CGContextSetStrokeColorSpace(maContextHolder.get(), GetSalData()->mxRGBSpace);
// apply a scale matrix so everything is auto-magically scaled
CGContextScaleCTM(maContextHolder.get(), fScale, fScale);
maContextHolder.saveState();
setState();
// re-enable XOR emulation for the new context
if (mpXorEmulation)
mpXorEmulation->SetTarget(mnWidth, mnHeight, mnBitmapDepth, maContextHolder.get(), maLayer.get());
}
}
}
SAL_WARN_IF(!maContextHolder.isSet() && !mbPrinter, "vcl", "<<<WARNING>>> AquaSalGraphics::CheckContext() FAILED!!!!");
return maContextHolder.isSet();
}
/**
* Blit the contents of our internal maLayer state to the
* associated window, if any; cf. drawRect event handling
* on the frame.
*/
void AquaSalGraphics::UpdateWindow( NSRect& )
{
if (!maShared.mpFrame)
{
return;
}
NSGraphicsContext* pContext = [NSGraphicsContext currentContext];
if (maShared.maLayer.isSet() && pContext != nullptr)
{
CGContextHolder rCGContextHolder([pContext CGContext]);
rCGContextHolder.saveState();
// Related: tdf#155092 translate Y coordinate for height differences
// When in live resize, the NSView's height may have changed before
// the CGLayer has been resized. This causes the CGLayer's content
// to be drawn just above or below the top left corner of the view
// so translate the Y coordinate by any difference between the
// NSView's height and the CGLayer's height.
NSView *pView = maShared.mpFrame->mpNSView;
if (pView)
{
// Use the NSView's bounds, not its frame, to properly handle
// any rotation and/or scaling that might have been already
// applied to the view
CGFloat fTranslateY = [pView bounds].size.height - maShared.maLayer.getSizePoints().height;
CGContextTranslateCTM(rCGContextHolder.get(), 0, fTranslateY);
}
CGMutablePathRef rClip = maShared.mpFrame->getClipPath();
if (rClip)
{
CGContextBeginPath(rCGContextHolder.get());
CGContextAddPath(rCGContextHolder.get(), rClip );
CGContextClip(rCGContextHolder.get());
}
maShared.applyXorContext();
const CGSize aSize = maShared.maLayer.getSizePoints();
const CGRect aRect = CGRectMake(0, 0, aSize.width, aSize.height);
const CGRect aRectPoints = { CGPointZero, maShared.maLayer.getSizePixels() };
CGContextSetBlendMode(maShared.maCSContextHolder.get(), kCGBlendModeCopy);
CGContextDrawLayerInRect(maShared.maCSContextHolder.get(), aRectPoints, maShared.maLayer.get());
CGImageRef img = CGBitmapContextCreateImage(maShared.maCSContextHolder.get());
CGImageRef displayColorSpaceImage = CGImageCreateCopyWithColorSpace(img, [[maShared.mpFrame->getNSWindow() colorSpace] CGColorSpace]);
CGContextSetBlendMode(rCGContextHolder.get(), kCGBlendModeCopy);
CGContextDrawImage(rCGContextHolder.get(), aRect, displayColorSpaceImage);
CGImageRelease(img);
CGImageRelease(displayColorSpaceImage);
rCGContextHolder.restoreState();
}
else
{
SAL_WARN_IF(!maShared.mpFrame->mbInitShow, "vcl", "UpdateWindow called on uneligible graphics");
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|