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 #ifndef INCLUDED_VCL_INC_SKIA_UTILS_H
21 #define INCLUDED_VCL_INC_SKIA_UTILS_H
23 #include <vcl/skia/SkiaHelper.hxx>
25 #include <tools/gen.hxx>
26 #include <driverblocklist.hxx>
27 #include <vcl/bitmap.hxx>
28 #include <vcl/salgtype.hxx>
30 #include <test/GraphicsRenderTests.hxx>
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"
40 #include <tools/sk_app/WindowContext.h>
41 #if defined __GNUC__ && !defined __clang__
42 #pragma GCC diagnostic pop
46 #include <string_view>
50 // Get the one shared GrDirectContext instance.
51 GrDirectContext
* getSharedGrDirectContext();
53 void disableRenderMethod(RenderMethod method
);
55 // Create SkSurface, GPU-backed if possible.
56 VCL_DLLPUBLIC sk_sp
<SkSurface
> createSkSurface(int width
, int height
,
57 SkColorType type
= kN32_SkColorType
,
58 SkAlphaType alpha
= kPremul_SkAlphaType
);
60 inline sk_sp
<SkSurface
> createSkSurface(const Size
& size
, SkColorType type
= kN32_SkColorType
,
61 SkAlphaType alpha
= kPremul_SkAlphaType
)
63 return createSkSurface(size
.Width(), size
.Height(), type
, alpha
);
66 inline sk_sp
<SkSurface
> createSkSurface(int width
, int height
, SkAlphaType alpha
)
68 return createSkSurface(width
, height
, kN32_SkColorType
, alpha
);
71 inline sk_sp
<SkSurface
> createSkSurface(const Size
& size
, SkAlphaType alpha
)
73 return createSkSurface(size
.Width(), size
.Height(), kN32_SkColorType
, alpha
);
76 // Create SkImage, GPU-backed if possible.
77 VCL_DLLPUBLIC sk_sp
<SkImage
> createSkImage(const SkBitmap
& bitmap
);
79 // Call surface->makeImageSnapshot() and abort on failure.
80 VCL_DLLPUBLIC sk_sp
<SkImage
> makeCheckedImageSnapshot(sk_sp
<SkSurface
> surface
);
81 VCL_DLLPUBLIC sk_sp
<SkImage
> makeCheckedImageSnapshot(sk_sp
<SkSurface
> surface
,
82 const SkIRect
& bounds
);
84 inline Size
imageSize(const sk_sp
<SkImage
>& image
) { return Size(image
->width(), image
->height()); }
86 inline SkColor
toSkColor(Color color
)
88 return SkColorSetARGB(color
.GetAlpha(), color
.GetRed(), color
.GetGreen(), color
.GetBlue());
91 inline SkColor
toSkColorWithTransparency(Color aColor
, double fTransparency
)
93 return SkColorSetA(toSkColor(aColor
), 255 * (1.0 - fTransparency
));
96 inline SkColor
toSkColorWithIntensity(Color color
, int intensity
)
98 return SkColorSetARGB(color
.GetAlpha(), color
.GetRed() * intensity
/ 100,
99 color
.GetGreen() * intensity
/ 100, color
.GetBlue() * intensity
/ 100);
102 inline Color
fromSkColor(SkColor color
)
104 return Color(ColorAlpha
, SkColorGetA(color
), SkColorGetR(color
), SkColorGetG(color
),
108 // Whether to use GetSkImage() that checks for delayed scaling or whether to access
109 // the stored image directly without checks.
116 // Sets SkBlender that will do an invert operation.
117 void setBlenderInvert(SkPaint
* paint
);
118 // Sets SkBlender that will do a xor operation.
119 void setBlenderXor(SkPaint
* paint
);
121 // Must be called in any VCL backend before any Skia functionality is used.
122 // If not set, Skia will be disabled.
124 prepareSkia(std::unique_ptr
<sk_app::WindowContext
> (*createGpuWindowContext
)(bool));
126 // Shared cache of images.
127 void addCachedImage(const OString
& key
, sk_sp
<SkImage
> image
);
128 sk_sp
<SkImage
> findCachedImage(const OString
& key
);
129 void removeCachedImage(sk_sp
<SkImage
> image
);
130 tools::Long
maxImageCacheSize();
132 // Get checksum of the image content, only for raster images. Is cached,
133 // but may still be somewhat expensive.
134 uint32_t getSkImageChecksum(sk_sp
<SkImage
> image
);
136 // SkSurfaceProps to be used by all Skia surfaces.
137 VCL_DLLPUBLIC
const SkSurfaceProps
* surfaceProps();
138 // Set pixel geometry to be used by SkSurfaceProps.
139 VCL_DLLPUBLIC
void setPixelGeometry(SkPixelGeometry pixelGeometry
);
141 inline bool isUnitTestRunning(const char* name
= nullptr)
145 static const char* const testname
= getenv("LO_TESTNAME");
146 if (testname
!= nullptr)
148 return !vcl::test::activeGraphicsRenderTest().isEmpty();
150 const char* const testname
= getenv("LO_TESTNAME");
151 if (testname
!= nullptr && std::string_view(name
) == testname
)
153 return vcl::test::activeGraphicsRenderTest().equalsAscii(name
);
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).
161 inline BmpScaleFlag
goodScalingQuality(bool isGPU
)
163 return isGPU
? BmpScaleFlag::BestQuality
: BmpScaleFlag::Default
;
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.
172 constexpr int downscaleRatioThreshold
= 4;
174 inline SkSamplingOptions
makeSamplingOptions(BmpScaleFlag scalingType
, SkMatrix matrix
,
179 case BmpScaleFlag::BestQuality
:
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());
186 case BmpScaleFlag::Default
:
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
);
190 case BmpScaleFlag::Fast
:
191 case BmpScaleFlag::NearestNeighbor
:
192 return SkSamplingOptions(SkFilterMode::kNearest
, SkMipmapMode::kNone
);
195 return SkSamplingOptions();
199 inline SkSamplingOptions
makeSamplingOptions(BmpScaleFlag scalingType
, const Size
& srcSize
,
200 Size destSize
, int scalingFactor
)
204 case BmpScaleFlag::BestQuality
:
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());
211 case BmpScaleFlag::Default
:
212 // As in the first overload, use kNearest.
213 return SkSamplingOptions(SkFilterMode::kLinear
, SkMipmapMode::kNearest
);
214 case BmpScaleFlag::Fast
:
215 case BmpScaleFlag::NearestNeighbor
:
216 return SkSamplingOptions(SkFilterMode::kNearest
, SkMipmapMode::kNone
);
219 return SkSamplingOptions();
223 inline SkSamplingOptions
makeSamplingOptions(const SalTwoRect
& rPosAry
, int scalingFactor
,
224 int srcScalingFactor
, bool isGPU
)
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
241 inline SkRect
scaleRect(const SkRect
& rect
, int scaling
)
243 return SkRect::MakeXYWH(rect
.x() * scaling
, rect
.y() * scaling
, rect
.width() * scaling
,
244 rect
.height() * scaling
);
247 inline SkIRect
scaleRect(const SkIRect
& rect
, int scaling
)
249 return SkIRect::MakeXYWH(rect
.x() * scaling
, rect
.y() * scaling
, rect
.width() * scaling
,
250 rect
.height() * scaling
);
254 void prefillSurface(const sk_sp
<SkSurface
>& surface
);
257 VCL_DLLPUBLIC
void dump(const SkBitmap
& bitmap
, const char* file
);
258 VCL_DLLPUBLIC
void dump(const sk_sp
<SkImage
>& image
, const char* file
);
259 VCL_DLLPUBLIC
void dump(const sk_sp
<SkSurface
>& surface
, const char* file
);
261 VCL_DLLPUBLIC
extern uint32_t vendorId
;
263 inline DriverBlocklist::DeviceVendor
getVendor()
265 return DriverBlocklist::GetVendorFromId(vendorId
);
268 } // namespace SkiaHelper
273 VCL_DLLPUBLIC
bool matrixNeedsHighQuality(const SkMatrix
& matrix
);
276 template <typename charT
, typename traits
>
277 inline std::basic_ostream
<charT
, traits
>& operator<<(std::basic_ostream
<charT
, traits
>& stream
,
278 const SkRect
& rectangle
)
280 if (rectangle
.isEmpty())
281 return stream
<< "EMPTY";
283 return stream
<< rectangle
.width() << 'x' << rectangle
.height() << "@(" << rectangle
.x()
284 << ',' << rectangle
.y() << ")";
287 template <typename charT
, typename traits
>
288 inline std::basic_ostream
<charT
, traits
>& operator<<(std::basic_ostream
<charT
, traits
>& stream
,
289 const SkIRect
& rectangle
)
291 if (rectangle
.isEmpty())
292 return stream
<< "EMPTY";
294 return stream
<< rectangle
.width() << 'x' << rectangle
.height() << "@(" << rectangle
.x()
295 << ',' << rectangle
.y() << ")";
298 template <typename charT
, typename traits
>
299 inline std::basic_ostream
<charT
, traits
>& operator<<(std::basic_ostream
<charT
, traits
>& stream
,
300 const SkRegion
& region
)
302 if (region
.isEmpty())
303 return stream
<< "EMPTY";
305 SkRegion::Iterator
it(region
);
306 for (int i
= 0; !it
.done(); it
.next(), ++i
)
307 stream
<< "[" << i
<< "] " << it
.rect();
312 template <typename charT
, typename traits
>
313 inline std::basic_ostream
<charT
, traits
>& operator<<(std::basic_ostream
<charT
, traits
>& stream
,
314 const SkMatrix
& matrix
)
316 return stream
<< "[" << matrix
[0] << " " << matrix
[1] << " " << matrix
[2] << "]"
317 << "[" << matrix
[3] << " " << matrix
[4] << " " << matrix
[5] << "]"
318 << "[" << matrix
[6] << " " << matrix
[7] << " " << matrix
[8] << "]";
321 template <typename charT
, typename traits
>
322 inline std::basic_ostream
<charT
, traits
>& operator<<(std::basic_ostream
<charT
, traits
>& stream
,
323 const SkImage
& image
)
326 return stream
<< static_cast<const void*>(&image
) << " " << Size(image
.width(), image
.height())
327 << "/" << (SkColorTypeBytesPerPixel(image
.imageInfo().colorType()) * 8)
328 << (image
.isTextureBacked() ? "G" : "");
330 template <typename charT
, typename traits
>
331 inline std::basic_ostream
<charT
, traits
>& operator<<(std::basic_ostream
<charT
, traits
>& stream
,
332 const sk_sp
<SkImage
>& image
)
334 if (image
== nullptr)
335 return stream
<< "(null)";
336 return stream
<< *image
;
339 #endif // INCLUDED_VCL_INC_SKIA_UTILS_H
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */