Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
encode_color_types.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm/gm.h"
12#include "include/core/SkData.h"
15#include "include/core/SkSize.h"
19#include "tools/DecodeUtils.h"
20#include "tools/Resources.h"
21
22namespace skiagm {
23
24static const int imageWidth = 128;
25static const int imageHeight = 128;
26
28 const char* resource;
29 switch (colorType) {
31 if (alphaType != kOpaque_SkAlphaType) {
32 return nullptr;
33 }
34 resource = "images/grayscale.jpg";
35 break;
40 if (alphaType != kOpaque_SkAlphaType) {
41 return nullptr;
42 }
43 resource = "images/color_wheel.jpg";
44 break;
45 default:
46 resource = (kOpaque_SkAlphaType == alphaType) ? "images/color_wheel.jpg"
47 : "images/rainbow-gradient.png";
48 break;
49 }
50
51 auto image = ToolUtils::GetResourceAsImage(resource);
52 if (!image) {
53 return nullptr;
54 }
55
57 image->width(), image->height(), colorType, alphaType, image->refColorSpace()));
58 surface->getCanvas()->drawImage(image, 0, 0);
59 return surface->makeImageSnapshot();
60}
61
62// This GM looks different depending on the colortype used, so we use different
63// names to treat them as different GMs.
64// All Variants draw images in pairs:
65// - First as an image of the SkColorType of the destination
66// - Next as the result of encoding and decoding the first image
67enum class Variant {
68 // One pair, using an opaque image.
69 kOpaque,
70 // One pair, using a grayscale image.
71 kGray,
72 // An opaque pair followed by two more for premul and unpremul.
73 kNormal,
74};
75
76class EncodeColorTypesGM : public GM {
77public:
78 EncodeColorTypesGM(SkEncodedImageFormat format, int quality, Variant variant, const char* name)
79 : fFormat(format)
80 , fQuality(quality)
81 , fVariant(variant)
82 , fName(name)
83 {}
84
85protected:
86 SkString getName() const override {
87 const char* variant = fVariant == Variant::kOpaque ? "opaque-":
88 fVariant == Variant::kGray ? "gray-" :
89 "" ;
90 return SkStringPrintf("encode-%scolor-types-%s", variant, fName);
91 }
92
93 SkISize getISize() override {
94 const int width = fVariant == Variant::kNormal ? imageWidth * 7 : imageWidth * 2;
96 }
97
98 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
99 const auto colorType = canvas->imageInfo().colorType();
100 switch (fVariant) {
101 case Variant::kGray:
103 return DrawResult::kSkip;
104 }
105 break;
106 case Variant::kOpaque:
111 {
112 return DrawResult::kSkip;
113 }
114 break;
115 case Variant::kNormal:
124 {
125 return DrawResult::kSkip;
126 }
127 break;
128 }
129 const SkAlphaType alphaTypes[] = {
131 };
132
133 for (SkAlphaType alphaType : alphaTypes) {
134 auto src = make_image(colorType, alphaType);
135 if (!src) {
136 break;
137 }
140 if (fQuality < 100) {
142 options.fQuality = fQuality;
143 } else {
145 // in lossless mode, this is effort. 70 is the default effort in SkImageEncoder,
146 // which follows Blink and WebPConfigInit.
147 options.fQuality = 70;
148 }
149 auto data = SkWebpEncoder::Encode(nullptr, src.get(), options);
150 SkASSERT(data);
151 auto decoded = SkImages::DeferredFromEncodedData(data);
152 if (!decoded) {
153 break;
154 }
155
156 canvas->drawImage(src, 0.0f, 0.0f);
157 canvas->translate((float) imageWidth, 0.0f);
158
159 canvas->drawImage(decoded, 0.0f, 0.0f);
160 canvas->translate((float) imageWidth * 1.5, 0.0f);
161 }
162 return DrawResult::kOk;
163 }
164
165private:
166 const SkEncodedImageFormat fFormat;
167 const int fQuality;
168 const Variant fVariant;
169 const char* fName;
170
171 using INHERITED = GM;
172};
173
174
175#define DEF_ENCODE_GM(format, quality, variant, name) \
176 static skiagm::GMRegistry SK_MACRO_CONCAT(SK_MACRO_APPEND_LINE(REG_), variant)(\
177 [](){return std::unique_ptr<skiagm::GM>([](){\
178 return new EncodeColorTypesGM(format, quality, Variant::variant, name);\
179 }());});
180
181#define DEF_VARIANTS(format, quality, name) \
182 DEF_ENCODE_GM(format, quality, kNormal, name); \
183 DEF_ENCODE_GM(format, quality, kOpaque, name); \
184 DEF_ENCODE_GM(format, quality, kGray, name);
185
186DEF_VARIANTS(SkEncodedImageFormat::kWEBP, 100, "webp-lossless")
187DEF_VARIANTS(SkEncodedImageFormat::kWEBP, 80, "webp-lossy")
188} // namespace skiagm
const char * options
kUnpremul_SkAlphaType
SkAlphaType
Definition SkAlphaType.h:26
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT_RELEASE(cond)
Definition SkAssert.h:100
#define SkASSERT(cond)
Definition SkAssert.h:116
SkColorType
Definition SkColorType.h:19
@ kBGR_101010x_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word
Definition SkColorType.h:30
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition SkColorType.h:23
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:29
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:25
@ kBGRA_1010102_SkColorType
10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
Definition SkColorType.h:28
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition SkColorType.h:40
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition SkColorType.h:27
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition SkColorType.h:36
SkEncodedImageFormat
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
void translate(SkScalar dx, SkScalar dy)
SkImageInfo imageInfo() const
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
int width() const
Definition SkImage.h:285
int height() const
Definition SkImage.h:291
sk_sp< SkColorSpace > refColorSpace() const
Definition SkImage.cpp:158
SkString getName() const override
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
EncodeColorTypesGM(SkEncodedImageFormat format, int quality, Variant variant, const char *name)
SkScalar width()
Definition gm.h:159
#define DEF_VARIANTS(format, quality, name)
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
uint32_t uint32_t * format
const char * name
Definition fuchsia.cc:50
static sk_sp< SkImage > make_image()
Definition mipmap.cpp:21
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
static const int imageHeight
static const int imageWidth
DrawResult
Definition gm.h:104
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkColorType colorType() const