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"
9 #include "include/codec/SkCodec.h"
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorSpace.h"
14 #include "include/core/SkData.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/gpu/GrDirectContext.h"
21 #include "src/core/SkImagePriv.h"
22 #include "tools/Resources.h"
23
24 #include <initializer_list>
25 #include <memory>
26
make_raster_image(const char * path)27 sk_sp<SkImage> make_raster_image(const char* path) {
28 sk_sp<SkData> resourceData = GetResourceAsData(path);
29 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(resourceData);
30
31 return std::get<0>(codec->getImage());
32 }
33
make_color_space(sk_sp<SkImage> orig,sk_sp<SkColorSpace> colorSpace,GrDirectContext * direct)34 sk_sp<SkImage> make_color_space(
35 sk_sp<SkImage> orig, sk_sp<SkColorSpace> colorSpace, GrDirectContext* direct) {
36 sk_sp<SkImage> xform = orig->makeColorSpace(colorSpace, direct);
37
38 // Assign an sRGB color space on the xformed image, so we can see the effects of the xform
39 // when we draw.
40 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
41 if (colorSpace->gammaIsLinear()) {
42 srgb = SkColorSpace::MakeSRGBLinear();
43 }
44 return xform->reinterpretColorSpace(std::move(srgb));
45 }
46
47 DEF_SIMPLE_GM_CAN_FAIL(makecolorspace, canvas, errorMsg, 128 * 3, 128 * 4) {
48 sk_sp<SkColorSpace> wideGamut = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
49 SkNamedGamut::kAdobeRGB);
50 sk_sp<SkColorSpace> wideGamutLinear = wideGamut->makeLinearGamma();
51
52 // Lazy images
53 sk_sp<SkImage> opaqueImage = GetResourceAsImage("images/mandrill_128.png");
54 sk_sp<SkImage> premulImage = GetResourceAsImage("images/color_wheel.png");
55 if (!opaqueImage || !premulImage) {
56 *errorMsg = "Failed to load images. Did you forget to set the resourcePath?";
57 return skiagm::DrawResult::kFail;
58 }
59 auto direct = GrAsDirectContext(canvas->recordingContext());
60 canvas->drawImage(opaqueImage, 0.0f, 0.0f);
61 canvas->drawImage(make_color_space(opaqueImage, wideGamut, direct), 128.0f, 0.0f);
62 canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear, direct), 256.0f, 0.0f);
63 canvas->drawImage(premulImage, 0.0f, 128.0f);
64 canvas->drawImage(make_color_space(premulImage, wideGamut, direct), 128.0f, 128.0f);
65 canvas->drawImage(make_color_space(premulImage, wideGamutLinear, direct), 256.0f, 128.0f);
66 canvas->translate(0.0f, 256.0f);
67
68 // Raster images
69 opaqueImage = make_raster_image("images/mandrill_128.png");
70 premulImage = make_raster_image("images/color_wheel.png");
71 canvas->drawImage(opaqueImage, 0.0f, 0.0f);
72 canvas->drawImage(make_color_space(opaqueImage, wideGamut, direct), 128.0f, 0.0f);
73 canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear, direct), 256.0f, 0.0f);
74 canvas->drawImage(premulImage, 0.0f, 128.0f);
75 canvas->drawImage(make_color_space(premulImage, wideGamut, direct), 128.0f, 128.0f);
76 canvas->drawImage(make_color_space(premulImage, wideGamutLinear, direct), 256.0f, 128.0f);
77 return skiagm::DrawResult::kOk;
78 }
79
80 DEF_SIMPLE_GM_BG(makecolortypeandspace, canvas, 128 * 3, 128 * 4, SK_ColorWHITE) {
81 sk_sp<SkImage> images[] = {
82 GetResourceAsImage("images/mandrill_128.png"),
83 GetResourceAsImage("images/color_wheel.png"),
84 };
85 auto rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kRec2020);
86
87 // Use the lazy images on the first iteration, and concrete (raster/GPU) images on the second
88 auto direct = GrAsDirectContext(canvas->recordingContext());
89 for (bool lazy : {true, false}) {
90 for (int j = 0; j < 2; ++j) {
91 const SkImage* image = images[j].get();
92 if (!image) {
93 // Can happen on bots that abandon the GPU context
94 continue;
95 }
96
97 // Unmodified
98 canvas->drawImage(image, 0, 0);
99
100 // Change the color type/space of the image in a couple ways. In both cases, codec
101 // may fail, because we refude to decode transparent sources to opaque color types.
102 // Guard against that, to avoid cascading failures in DDL.
103
104 // 565 in a wide color space (should be visibly quantized). Fails with the color_wheel,
105 // because of the codec issues mentioned above.
106 auto image565 = image->makeColorTypeAndColorSpace(kRGB_565_SkColorType,
107 rec2020, direct);
108 if (!lazy || image565->makeRasterImage()) {
109 canvas->drawImage(image565, 128, 0);
110 }
111
112 // Grayscale in the original color space. This fails in even more cases, due to the
113 // above opaque issue, and because Ganesh doesn't support drawing to gray, at all.
114 auto imageGray = image->makeColorTypeAndColorSpace(kGray_8_SkColorType,
115 image->refColorSpace(),
116 direct);
117 if (!lazy || imageGray->makeRasterImage()) {
118 canvas->drawImage(imageGray, 256, 0);
119 }
120
121 images[j] = direct ? image->makeTextureImage(direct) : image->makeRasterImage();
122
123 canvas->translate(0, 128);
124 }
125 }
126 }
127
128 DEF_SIMPLE_GM_CAN_FAIL(reinterpretcolorspace, canvas, errorMsg, 128 * 3, 128 * 3) {
129 // We draw a 3x3 grid. The three rows are lazy (encoded), raster, and GPU (or another copy of
130 // raster so all configs look similar). In each row, we draw three variants:
131 // - The original image (should look normal).
132 // - The image, reinterpreted as being in the color-spin space. The pixel data isn't changed,
133 // so in untagged configs, this looks like the first column. In tagged configs, this has the
134 // the effect of rotating the colors (RGB -> GBR).
135 // - The image converted to the color-spin space, then reinterpreted as being sRGB. In all
136 // configs, this appears to be spun backwards (RGB -> BRG), and tests the composition of
137 // these two APIs.
138
139 // In all cases, every column should be identical. In tagged configs, the 'R' in the columns
140 // should be Red, Green, Blue.
141
142 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
143 sk_sp<SkColorSpace> spin = srgb->makeColorSpin();
144 sk_sp<SkImage> image = GetResourceAsImage("images/color_wheel.png");
145 if (!image) {
146 *errorMsg = "Failed to load image. Did you forget to set the resourcePath?";
147 return skiagm::DrawResult::kFail;
148 }
149
150 // Lazy images
151 canvas->drawImage(image, 0.0f, 0.0f);
152 canvas->drawImage(image->reinterpretColorSpace(spin), 128.0f, 0.0f);
153 canvas->drawImage(image->makeColorSpace(spin, nullptr)->reinterpretColorSpace(srgb),
154 256.0f, 0.0f);
155
156 canvas->translate(0.0f, 128.0f);
157
158 // Raster images
159 image = image->makeRasterImage();
160 canvas->drawImage(image, 0.0f, 0.0f);
161 canvas->drawImage(image->reinterpretColorSpace(spin), 128.0f, 0.0f);
162 canvas->drawImage(image->makeColorSpace(spin, nullptr)->reinterpretColorSpace(srgb),
163 256.0f, 0.0f);
164
165 canvas->translate(0.0f, 128.0f);
166
167 // GPU images
168 auto direct = GrAsDirectContext(canvas->recordingContext());
169 if (auto gpuImage = image->makeTextureImage(direct)) {
170 image = gpuImage;
171 }
172
173 canvas->drawImage(image, 0.0f, 0.0f);
174 canvas->drawImage(image->reinterpretColorSpace(spin), 128.0f, 0.0f);
175 canvas->drawImage(image->makeColorSpace(spin, direct)->reinterpretColorSpace(srgb),
176 256.0f, 0.0f);
177
178 return skiagm::DrawResult::kOk;
179 }
180