LibreOffice Module vcl (master) 1
utils.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_VCL_INC_SKIA_UTILS_H
21#define INCLUDED_VCL_INC_SKIA_UTILS_H
22
24
25#include <tools/gen.hxx>
26#include <driverblocklist.hxx>
27#include <vcl/bitmap.hxx>
28#include <vcl/salgtype.hxx>
29
30#include <test/GraphicsRenderTests.hxx>
31
32#include <premac.h>
33#include <SkRegion.h>
34#include <SkSurface.h>
35#if defined __GNUC__ && !defined __clang__
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wattributes"
38#pragma GCC diagnostic ignored "-Wshadow"
39#endif
40#include <tools/sk_app/WindowContext.h>
41#if defined __GNUC__ && !defined __clang__
42#pragma GCC diagnostic pop
43#endif
44#include <postmac.h>
45
46#include <string_view>
47
48namespace SkiaHelper
49{
50// Get the one shared GrDirectContext instance.
51GrDirectContext* getSharedGrDirectContext();
52
53void disableRenderMethod(RenderMethod method);
54
55// Create SkSurface, GPU-backed if possible.
56VCL_DLLPUBLIC sk_sp<SkSurface> createSkSurface(int width, int height,
57 SkColorType type = kN32_SkColorType,
58 SkAlphaType alpha = kPremul_SkAlphaType);
59
60inline sk_sp<SkSurface> createSkSurface(const Size& size, SkColorType type = kN32_SkColorType,
61 SkAlphaType alpha = kPremul_SkAlphaType)
62{
63 return createSkSurface(size.Width(), size.Height(), type, alpha);
64}
65
66inline sk_sp<SkSurface> createSkSurface(int width, int height, SkAlphaType alpha)
67{
68 return createSkSurface(width, height, kN32_SkColorType, alpha);
69}
70
71inline sk_sp<SkSurface> createSkSurface(const Size& size, SkAlphaType alpha)
72{
73 return createSkSurface(size.Width(), size.Height(), kN32_SkColorType, alpha);
74}
75
76// Create SkImage, GPU-backed if possible.
77VCL_DLLPUBLIC sk_sp<SkImage> createSkImage(const SkBitmap& bitmap);
78
79// Call surface->makeImageSnapshot() and abort on failure.
80VCL_DLLPUBLIC sk_sp<SkImage> makeCheckedImageSnapshot(sk_sp<SkSurface> surface);
81VCL_DLLPUBLIC sk_sp<SkImage> makeCheckedImageSnapshot(sk_sp<SkSurface> surface,
82 const SkIRect& bounds);
83
84inline Size imageSize(const sk_sp<SkImage>& image) { return Size(image->width(), image->height()); }
85
86inline SkColor toSkColor(Color color)
87{
88 return SkColorSetARGB(color.GetAlpha(), color.GetRed(), color.GetGreen(), color.GetBlue());
89}
90
91inline SkColor toSkColorWithTransparency(Color aColor, double fTransparency)
92{
93 return SkColorSetA(toSkColor(aColor), 255 * (1.0 - fTransparency));
94}
95
96inline SkColor toSkColorWithIntensity(Color color, int intensity)
97{
98 return SkColorSetARGB(color.GetAlpha(), color.GetRed() * intensity / 100,
99 color.GetGreen() * intensity / 100, color.GetBlue() * intensity / 100);
100}
101
102inline Color fromSkColor(SkColor color)
103{
104 return Color(ColorAlpha, SkColorGetA(color), SkColorGetR(color), SkColorGetG(color),
105 SkColorGetB(color));
106}
107
108// Whether to use GetSkImage() that checks for delayed scaling or whether to access
109// the stored image directly without checks.
111{
113 No
115
116// Sets SkBlender that will do an invert operation.
117void setBlenderInvert(SkPaint* paint);
118// Sets SkBlender that will do a xor operation.
119void setBlenderXor(SkPaint* paint);
120
121// Must be called in any VCL backend before any Skia functionality is used.
122// If not set, Skia will be disabled.
123VCL_DLLPUBLIC void
124 prepareSkia(std::unique_ptr<sk_app::WindowContext> (*createGpuWindowContext)(bool));
125
126// Shared cache of images.
127void addCachedImage(const OString& key, sk_sp<SkImage> image);
128sk_sp<SkImage> findCachedImage(const OString& key);
129void removeCachedImage(sk_sp<SkImage> image);
131
132// Get checksum of the image content, only for raster images. Is cached,
133// but may still be somewhat expensive.
134uint32_t getSkImageChecksum(sk_sp<SkImage> image);
135
136// SkSurfaceProps to be used by all Skia surfaces.
137VCL_DLLPUBLIC const SkSurfaceProps* surfaceProps();
138// Set pixel geometry to be used by SkSurfaceProps.
139VCL_DLLPUBLIC void setPixelGeometry(SkPixelGeometry pixelGeometry);
140
141inline bool isUnitTestRunning(const char* name = nullptr)
142{
143 if (name == nullptr)
144 {
145 static const char* const testname = getenv("LO_TESTNAME");
146 if (testname != nullptr)
147 return true;
148 return !vcl::test::activeGraphicsRenderTest().isEmpty();
149 }
150 const char* const testname = getenv("LO_TESTNAME");
151 if (testname != nullptr && std::string_view(name) == testname)
152 return true;
153 return vcl::test::activeGraphicsRenderTest().equalsAscii(name);
154}
155
156// Scaling done on the GPU is fast, but bicubic done in raster mode can be slow
157// if done too much, and it generally shouldn't be needed for to-screen drawing.
158// In that case use only BmpScaleFlag::Default, which is bilinear+mipmap,
159// which should be good enough (and that's what the "super" bitmap scaling
160// algorithm done by VCL does as well).
162{
164}
165
166// Normal scaling algorithms have a poor quality when downscaling a lot.
167// https://bugs.chromium.org/p/skia/issues/detail?id=11810 suggests to use mipmaps
168// in such a case, which is annoying to do explicitly instead of Skia deciding which
169// algorithm would be the best, but now with Skia removing SkFilterQuality and requiring
170// explicitly being told what algorithm to use this appears to be the best we can do.
171// Anything scaled down at least this ratio will use linear+mipmaps.
172constexpr int downscaleRatioThreshold = 4;
173
174inline SkSamplingOptions makeSamplingOptions(BmpScaleFlag scalingType, SkMatrix matrix,
175 int scalingFactor)
176{
177 switch (scalingType)
178 {
180 if (scalingFactor != 1)
181 matrix.postScale(scalingFactor, scalingFactor);
182 if (matrix.getScaleX() <= 1.0 / downscaleRatioThreshold
183 || matrix.getScaleY() <= 1.0 / downscaleRatioThreshold)
184 return SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear);
185 return SkSamplingOptions(SkCubicResampler::Mitchell());
187 // Use SkMipmapMode::kNearest for better quality when downscaling. SkMipmapMode::kLinear
188 // would be even better, but it is not specially optimized in raster mode.
189 return SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNearest);
192 return SkSamplingOptions(SkFilterMode::kNearest, SkMipmapMode::kNone);
193 default:
194 assert(false);
195 return SkSamplingOptions();
196 }
197}
198
199inline SkSamplingOptions makeSamplingOptions(BmpScaleFlag scalingType, const Size& srcSize,
200 Size destSize, int scalingFactor)
201{
202 switch (scalingType)
203 {
205 if (scalingFactor != 1)
206 destSize *= scalingFactor;
207 if (srcSize.Width() / destSize.Width() >= downscaleRatioThreshold
208 || srcSize.Height() / destSize.Height() >= downscaleRatioThreshold)
209 return SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear);
210 return SkSamplingOptions(SkCubicResampler::Mitchell());
212 // As in the first overload, use kNearest.
213 return SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNearest);
216 return SkSamplingOptions(SkFilterMode::kNearest, SkMipmapMode::kNone);
217 default:
218 assert(false);
219 return SkSamplingOptions();
220 }
221}
222
223inline SkSamplingOptions makeSamplingOptions(const SalTwoRect& rPosAry, int scalingFactor,
224 int srcScalingFactor, bool isGPU)
225{
226 // If there will be scaling, make it smooth, but not in unittests, as those often
227 // require exact color values and would be confused by this.
228 if (isUnitTestRunning())
229 return SkSamplingOptions(); // none
230 Size srcSize(rPosAry.mnSrcWidth, rPosAry.mnSrcHeight);
231 Size destSize(rPosAry.mnDestWidth, rPosAry.mnDestHeight);
232 if (scalingFactor != 1)
233 destSize *= scalingFactor;
234 if (srcScalingFactor != 1)
235 srcSize *= srcScalingFactor;
236 if (srcSize != destSize)
237 return makeSamplingOptions(goodScalingQuality(isGPU), srcSize, destSize, 1);
238 return SkSamplingOptions(); // none
239}
240
241inline SkRect scaleRect(const SkRect& rect, int scaling)
242{
243 return SkRect::MakeXYWH(rect.x() * scaling, rect.y() * scaling, rect.width() * scaling,
244 rect.height() * scaling);
245}
246
247inline SkIRect scaleRect(const SkIRect& rect, int scaling)
248{
249 return SkIRect::MakeXYWH(rect.x() * scaling, rect.y() * scaling, rect.width() * scaling,
250 rect.height() * scaling);
251}
252
253#ifdef DBG_UTIL
254void prefillSurface(const sk_sp<SkSurface>& surface);
255#endif
256
257VCL_DLLPUBLIC void dump(const SkBitmap& bitmap, const char* file);
258VCL_DLLPUBLIC void dump(const sk_sp<SkImage>& image, const char* file);
259VCL_DLLPUBLIC void dump(const sk_sp<SkSurface>& surface, const char* file);
260
261VCL_DLLPUBLIC extern uint32_t vendorId;
262
264{
266}
267
268} // namespace SkiaHelper
269
270// For unittests.
271namespace SkiaTests
272{
273VCL_DLLPUBLIC bool matrixNeedsHighQuality(const SkMatrix& matrix);
274}
275
276template <typename charT, typename traits>
277inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
278 const SkRect& rectangle)
279{
280 if (rectangle.isEmpty())
281 return stream << "EMPTY";
282 else
283 return stream << rectangle.width() << 'x' << rectangle.height() << "@(" << rectangle.x()
284 << ',' << rectangle.y() << ")";
285}
286
287template <typename charT, typename traits>
288inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
289 const SkIRect& rectangle)
290{
291 if (rectangle.isEmpty())
292 return stream << "EMPTY";
293 else
294 return stream << rectangle.width() << 'x' << rectangle.height() << "@(" << rectangle.x()
295 << ',' << rectangle.y() << ")";
296}
297
298template <typename charT, typename traits>
299inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
300 const SkRegion& region)
301{
302 if (region.isEmpty())
303 return stream << "EMPTY";
304 stream << "(";
305 SkRegion::Iterator it(region);
306 for (int i = 0; !it.done(); it.next(), ++i)
307 stream << "[" << i << "] " << it.rect();
308 stream << ")";
309 return stream;
310}
311
312template <typename charT, typename traits>
313inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
314 const SkMatrix& matrix)
315{
316 return stream << "[" << matrix[0] << " " << matrix[1] << " " << matrix[2] << "]"
317 << "[" << matrix[3] << " " << matrix[4] << " " << matrix[5] << "]"
318 << "[" << matrix[6] << " " << matrix[7] << " " << matrix[8] << "]";
319}
320
321template <typename charT, typename traits>
322inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
323 const SkImage& image)
324{
325 // G - on GPU
326 return stream << static_cast<const void*>(&image) << " " << Size(image.width(), image.height())
327 << "/" << (SkColorTypeBytesPerPixel(image.imageInfo().colorType()) * 8)
328 << (image.isTextureBacked() ? "G" : "");
329}
330template <typename charT, typename traits>
331inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
332 const sk_sp<SkImage>& image)
333{
334 if (image == nullptr)
335 return stream << "(null)";
336 return stream << *image;
337}
338
339#endif // INCLUDED_VCL_INC_SKIA_UTILS_H
340
341/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr tools::Long Height() const
constexpr tools::Long Width() const
ColorAlpha
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
Reference< XOutputStream > stream
const char * name
DeviceVendor GetVendorFromId(uint32_t id)
Returns vendor for the given vendor ID, or VendorAll if not known.
def rectangle(l)
Size imageSize(const sk_sp< SkImage > &image)
Definition: utils.hxx:84
void setBlenderXor(SkPaint *paint)
BmpScaleFlag goodScalingQuality(bool isGPU)
Definition: utils.hxx:161
Color fromSkColor(SkColor color)
Definition: utils.hxx:102
constexpr int downscaleRatioThreshold
Definition: utils.hxx:172
SkColor toSkColorWithTransparency(Color aColor, double fTransparency)
Definition: utils.hxx:91
VCL_DLLPUBLIC uint32_t vendorId
void disableRenderMethod(RenderMethod method)
GrDirectContext * getSharedGrDirectContext()
SkRect scaleRect(const SkRect &rect, int scaling)
Definition: utils.hxx:241
void addCachedImage(const OString &key, sk_sp< SkImage > image)
sk_sp< SkImage > findCachedImage(const OString &key)
bool isUnitTestRunning(const char *name=nullptr)
Definition: utils.hxx:141
VCL_DLLPUBLIC sk_sp< SkImage > createSkImage(const SkBitmap &bitmap)
VCL_DLLPUBLIC sk_sp< SkSurface > createSkSurface(int width, int height, SkColorType type=kN32_SkColorType, SkAlphaType alpha=kPremul_SkAlphaType)
VCL_DLLPUBLIC void setPixelGeometry(SkPixelGeometry pixelGeometry)
void prefillSurface(const sk_sp< SkSurface > &surface)
SkSamplingOptions makeSamplingOptions(BmpScaleFlag scalingType, SkMatrix matrix, int scalingFactor)
Definition: utils.hxx:174
VCL_DLLPUBLIC sk_sp< SkImage > makeCheckedImageSnapshot(sk_sp< SkSurface > surface)
DriverBlocklist::DeviceVendor getVendor()
Definition: utils.hxx:263
tools::Long maxImageCacheSize()
void setBlenderInvert(SkPaint *paint)
void removeCachedImage(sk_sp< SkImage > image)
VCL_DLLPUBLIC const SkSurfaceProps * surfaceProps()
VCL_DLLPUBLIC void dump(const SkBitmap &bitmap, const char *file)
uint32_t getSkImageChecksum(sk_sp< SkImage > image)
VCL_DLLPUBLIC void prepareSkia(std::unique_ptr< sk_app::WindowContext >(*createGpuWindowContext)(bool))
SkColor toSkColor(Color color)
Definition: utils.hxx:86
SkColor toSkColorWithIntensity(Color color, int intensity)
Definition: utils.hxx:96
VCL_DLLPUBLIC bool matrixNeedsHighQuality(const SkMatrix &matrix)
size
int i
long Long
const OUString & activeGraphicsRenderTest()
tools::Long mnDestWidth
Definition: salgtype.hxx:44
tools::Long mnSrcHeight
Definition: salgtype.hxx:41
tools::Long mnSrcWidth
Definition: salgtype.hxx:40
tools::Long mnDestHeight
Definition: salgtype.hxx:45
ResultType type
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &stream, const SkRect &rectangle)
Definition: utils.hxx:277