Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
makecolorspace.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 Google Inc.
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"
14#include "include/core/SkData.h"
18#include "include/core/SkSize.h"
23#include "tools/DecodeUtils.h"
24#include "tools/Resources.h"
25
26#include <initializer_list>
27#include <memory>
28
29#if defined(SK_GRAPHITE)
31#endif
32
34 sk_sp<SkData> resourceData = GetResourceAsData(path);
35 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(resourceData);
36
37 return std::get<0>(codec->getImage());
38}
39
41 sk_sp<SkColorSpace> colorSpace,
42 SkCanvas* canvas) {
43 if (!orig) {
44 return nullptr;
45 }
46
47 sk_sp<SkImage> xform;
48#if defined(SK_GRAPHITE)
49 if (auto recorder = canvas->recorder()) {
50 xform = orig->makeColorSpace(recorder, colorSpace, {});
51 } else
52#endif
53 {
54 auto direct = GrAsDirectContext(canvas->recordingContext());
55 xform = orig->makeColorSpace(direct, colorSpace);
56 }
57
58 if (!xform) {
59 return nullptr;
60 }
61
62 // Assign an sRGB color space on the xformed image, so we can see the effects of the xform
63 // when we draw.
65 if (colorSpace->gammaIsLinear()) {
67 }
68 return xform->reinterpretColorSpace(std::move(srgb));
69}
70
71DEF_SIMPLE_GM_CAN_FAIL(makecolorspace, canvas, errorMsg, 128 * 3, 128 * 4) {
74 sk_sp<SkColorSpace> wideGamutLinear = wideGamut->makeLinearGamma();
75
76 // Lazy images
77 sk_sp<SkImage> opaqueImage = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
78 sk_sp<SkImage> premulImage = ToolUtils::GetResourceAsImage("images/color_wheel.png");
79 if (!opaqueImage || !premulImage) {
80 *errorMsg = "Failed to load images. Did you forget to set the resourcePath?";
82 }
83
84 canvas->drawImage(opaqueImage, 0.0f, 0.0f);
85 canvas->drawImage(make_color_space(opaqueImage, wideGamut, canvas), 128.0f, 0.0f);
86 canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear, canvas), 256.0f, 0.0f);
87 canvas->drawImage(premulImage, 0.0f, 128.0f);
88 canvas->drawImage(make_color_space(premulImage, wideGamut, canvas), 128.0f, 128.0f);
89 canvas->drawImage(make_color_space(premulImage, wideGamutLinear, canvas), 256.0f, 128.0f);
90 canvas->translate(0.0f, 256.0f);
91
92 // Raster images
93 opaqueImage = make_raster_image("images/mandrill_128.png");
94 premulImage = make_raster_image("images/color_wheel.png");
95 canvas->drawImage(opaqueImage, 0.0f, 0.0f);
96 canvas->drawImage(make_color_space(opaqueImage, wideGamut, canvas), 128.0f, 0.0f);
97 canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear, canvas), 256.0f, 0.0f);
98 canvas->drawImage(premulImage, 0.0f, 128.0f);
99 canvas->drawImage(make_color_space(premulImage, wideGamut, canvas), 128.0f, 128.0f);
100 canvas->drawImage(make_color_space(premulImage, wideGamutLinear, canvas), 256.0f, 128.0f);
102}
103
104DEF_SIMPLE_GM_BG(makecolortypeandspace, canvas, 128 * 3, 128 * 4, SK_ColorWHITE) {
106 ToolUtils::GetResourceAsImage("images/mandrill_128.png"),
107 ToolUtils::GetResourceAsImage("images/color_wheel.png"),
108 };
110
111 // Use the lazy images on the first iteration, and concrete (raster/GPU) images on the second
112 auto direct = GrAsDirectContext(canvas->recordingContext());
113 for (bool lazy : {true, false}) {
114 for (size_t j = 0; j < std::size(images); ++j) {
115 const SkImage* image = images[j].get();
116 if (!image) {
117 // Can happen on bots that abandon the GPU context
118 continue;
119 }
120
121 // Unmodified
122 canvas->drawImage(image, 0, 0);
123
124 // Change the color type/space of the image in a couple ways. In both cases, codec
125 // may fail, because we refuse to decode transparent sources to opaque color types.
126 // Guard against that, to avoid cascading failures in DDL.
127
128 // 565 in a wide color space (should be visibly quantized). Fails with the color_wheel,
129 // because of the codec issues mentioned above.
130 sk_sp<SkImage> image565;
131
132#if defined(SK_GRAPHITE)
133 if (auto recorder = canvas->recorder()) {
135 recorder, kRGB_565_SkColorType, rec2020, {});
136 } else
137#endif
138 {
140 }
141 if (image565) {
142 if (!lazy || image565->isTextureBacked() || image565->makeRasterImage()) {
143 canvas->drawImage(image565, 128, 0);
144 }
145 }
146
147 // Grayscale in the original color space. This fails in even more cases, due to the
148 // above opaque issue, and because Ganesh doesn't support drawing to gray, at all.
149 sk_sp<SkImage> imageGray;
150#if defined(SK_GRAPHITE)
151 if (auto recorder = canvas->recorder()) {
153 recorder, kGray_8_SkColorType, image->refColorSpace(), {});
154 } else
155#endif
156 {
159 }
160 if (imageGray) {
161 if (!lazy || imageGray->isTextureBacked() || imageGray->makeRasterImage()) {
162 canvas->drawImage(imageGray, 256, 0);
163 }
164 }
165
166#if defined(SK_GRAPHITE)
167 if (auto recorder = canvas->recorder()) {
168 images[j] = SkImages::TextureFromImage(recorder, image, {});
169 } else
170#endif
171 {
172 if (direct) {
174 } else {
175 images[j] = image->makeRasterImage(nullptr);
176 }
177 }
178
179 canvas->translate(0, 128);
180 }
181 }
182}
183
184DEF_SIMPLE_GM_CAN_FAIL(reinterpretcolorspace, canvas, errorMsg, 128 * 3, 128 * 3) {
185 // We draw a 3x3 grid. The three rows are lazy (encoded), raster, and GPU (or another copy of
186 // raster so all configs look similar). In each row, we draw three variants:
187 // - The original image (should look normal).
188 // - The image, reinterpreted as being in the color-spin space. The pixel data isn't changed,
189 // so in untagged configs, this looks like the first column. In tagged configs, this has the
190 // the effect of rotating the colors (RGB -> GBR).
191 // - The image converted to the color-spin space, then reinterpreted as being sRGB. In all
192 // configs, this appears to be spun backwards (RGB -> BRG), and tests the composition of
193 // these two APIs.
194
195 // In all cases, every column should be identical. In tagged configs, the 'R' in the columns
196 // should be Red, Green, Blue.
197
199 sk_sp<SkColorSpace> spin = srgb->makeColorSpin();
200 sk_sp<SkImage> image = ToolUtils::GetResourceAsImage("images/color_wheel.png");
201 if (!image) {
202 *errorMsg = "Failed to load image. Did you forget to set the resourcePath?";
204 }
205
206 // Lazy images
207 canvas->drawImage(image, 0.0f, 0.0f);
208 canvas->drawImage(image->reinterpretColorSpace(spin), 128.0f, 0.0f);
209 canvas->drawImage(image->makeColorSpace(nullptr, spin)->reinterpretColorSpace(srgb), 256.0f, 0.0f);
210
211 canvas->translate(0.0f, 128.0f);
212
213 // Raster images
215 canvas->drawImage(image, 0.0f, 0.0f);
216 canvas->drawImage(image->reinterpretColorSpace(spin), 128.0f, 0.0f);
217 canvas->drawImage(image->makeColorSpace(nullptr, spin)->reinterpretColorSpace(srgb), 256.0f, 0.0f);
218
219 canvas->translate(0.0f, 128.0f);
220
221 // GPU images
222 auto direct = GrAsDirectContext(canvas->recordingContext());
223
224 sk_sp<SkImage> gpuImage;
225#if defined(SK_GRAPHITE)
226 if (auto recorder = canvas->recorder()) {
227 gpuImage = SkImages::TextureFromImage(recorder, image, {});
228 } else
229#endif
230 {
231 gpuImage = SkImages::TextureFromImage(direct, image);
232 }
233 if (gpuImage) {
234 image = gpuImage;
235 }
236
237 canvas->drawImage(image, 0.0f, 0.0f);
238 canvas->drawImage(image->reinterpretColorSpace(spin), 128.0f, 0.0f);
239
240#if defined(SK_GRAPHITE)
241 if (auto recorder = canvas->recorder()) {
242 gpuImage = image->makeColorSpace(recorder, spin, {});
243 } else
244#endif
245 {
246 gpuImage = image->makeColorSpace(direct, spin);
247 }
248 if (gpuImage) {
249 canvas->drawImage(gpuImage->reinterpretColorSpace(srgb), 256.0f, 0.0f);
250 }
251
253}
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
static sk_sp< SkColorSpace > rec2020()
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition Resources.cpp:42
@ 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
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
virtual GrRecordingContext * recordingContext() const
virtual skgpu::graphite::Recorder * recorder() const
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
Definition SkCodec.cpp:241
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkColorSpace > MakeRGB(const skcms_TransferFunction &transferFn, const skcms_Matrix3x3 &toXYZ)
static sk_sp< SkColorSpace > MakeSRGBLinear()
sk_sp< SkImage > makeRasterImage(GrDirectContext *, CachingHint cachingHint=kDisallow_CachingHint) const
Definition SkImage.cpp:267
virtual sk_sp< SkImage > makeColorSpace(GrDirectContext *direct, sk_sp< SkColorSpace > target) const =0
virtual sk_sp< SkImage > makeColorTypeAndColorSpace(GrDirectContext *direct, SkColorType targetColorType, sk_sp< SkColorSpace > targetCS) const =0
sk_sp< SkImage > reinterpretColorSpace(sk_sp< SkColorSpace > newColorSpace) const
Definition SkImage.cpp:241
sk_sp< SkColorSpace > refColorSpace() const
Definition SkImage.cpp:158
sk_sp< SkImage > image
Definition examples.cpp:29
#define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR)
Definition gm.h:52
#define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H)
Definition gm.h:62
sk_sp< SkImage > make_raster_image(const char *path)
sk_sp< SkImage > make_color_space(sk_sp< SkImage > orig, sk_sp< SkColorSpace > colorSpace, SkCanvas *canvas)
std::array< MockImage, 3 > images
SK_API sk_sp< SkImage > TextureFromImage(GrDirectContext *, const SkImage *, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
static constexpr skcms_Matrix3x3 kAdobeRGB
static constexpr skcms_Matrix3x3 kRec2020
static constexpr skcms_TransferFunction kSRGB
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
static sk_sp< SkColorFilter > spin(sk_sp< SkColorFilter > cf)