blob: 54d8f4d5e8a9465bc05bfa2f829df332d7b13a3b [file] [log] [blame]
Matt Sarett9f3dcb32017-05-04 08:53:32 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/codec/SkCodec.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkBitmap.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorSpace.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkData.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkImageInfo.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkImagePriv.h"
21#include "tools/Resources.h"
Matt Sarett9f3dcb32017-05-04 08:53:32 -040022
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include <initializer_list>
24#include <memory>
25
Brian Osmanb62f50c2018-07-12 14:44:27 -040026sk_sp<SkImage> make_raster_image(const char* path) {
Mike Reed0933bc92017-12-09 01:27:41 +000027 sk_sp<SkData> resourceData = GetResourceAsData(path);
Mike Reedede7bac2017-07-23 15:30:02 -040028 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(resourceData);
Matt Sarett9f3dcb32017-05-04 08:53:32 -040029
30 SkBitmap bitmap;
31 bitmap.allocPixels(codec->getInfo());
32
Brian Osmanb62f50c2018-07-12 14:44:27 -040033 codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes());
Matt Sarett9f3dcb32017-05-04 08:53:32 -040034 return SkImage::MakeFromBitmap(bitmap);
35}
36
Brian Osmanb62f50c2018-07-12 14:44:27 -040037sk_sp<SkImage> make_color_space(sk_sp<SkImage> orig, sk_sp<SkColorSpace> colorSpace) {
38 sk_sp<SkImage> xform = orig->makeColorSpace(colorSpace);
Matt Sarett9f3dcb32017-05-04 08:53:32 -040039
40 // Assign an sRGB color space on the xformed image, so we can see the effects of the xform
41 // when we draw.
42 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
43 if (colorSpace->gammaIsLinear()) {
44 srgb = SkColorSpace::MakeSRGBLinear();
45 }
Brian Osmand5148372019-08-14 16:14:51 -040046 return xform->reinterpretColorSpace(std::move(srgb));
Matt Sarett9f3dcb32017-05-04 08:53:32 -040047}
48
Brian Osmand5148372019-08-14 16:14:51 -040049DEF_SIMPLE_GM_CAN_FAIL(makecolorspace, canvas, errorMsg, 128 * 3, 128 * 4) {
50 sk_sp<SkColorSpace> wideGamut = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
51 SkNamedGamut::kAdobeRGB);
52 sk_sp<SkColorSpace> wideGamutLinear = wideGamut->makeLinearGamma();
Matt Sarett9f3dcb32017-05-04 08:53:32 -040053
Brian Osmand5148372019-08-14 16:14:51 -040054 // Lazy images
55 sk_sp<SkImage> opaqueImage = GetResourceAsImage("images/mandrill_128.png");
56 sk_sp<SkImage> premulImage = GetResourceAsImage("images/color_wheel.png");
57 if (!opaqueImage || !premulImage) {
58 *errorMsg = "Failed to load images. Did you forget to set the resourcePath?";
59 return skiagm::DrawResult::kFail;
Matt Sarett9f3dcb32017-05-04 08:53:32 -040060 }
Brian Osmand5148372019-08-14 16:14:51 -040061 canvas->drawImage(opaqueImage, 0.0f, 0.0f);
62 canvas->drawImage(make_color_space(opaqueImage, wideGamut), 128.0f, 0.0f);
63 canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear), 256.0f, 0.0f);
64 canvas->drawImage(premulImage, 0.0f, 128.0f);
65 canvas->drawImage(make_color_space(premulImage, wideGamut), 128.0f, 128.0f);
66 canvas->drawImage(make_color_space(premulImage, wideGamutLinear), 256.0f, 128.0f);
67 canvas->translate(0.0f, 256.0f);
Matt Sarett9f3dcb32017-05-04 08:53:32 -040068
Brian Osmand5148372019-08-14 16:14:51 -040069 // Raster images
70 opaqueImage = make_raster_image("images/mandrill_128.png");
71 premulImage = make_raster_image("images/color_wheel.png");
72 canvas->drawImage(opaqueImage, 0.0f, 0.0f);
73 canvas->drawImage(make_color_space(opaqueImage, wideGamut), 128.0f, 0.0f);
74 canvas->drawImage(make_color_space(opaqueImage, wideGamutLinear), 256.0f, 0.0f);
75 canvas->drawImage(premulImage, 0.0f, 128.0f);
76 canvas->drawImage(make_color_space(premulImage, wideGamut), 128.0f, 128.0f);
77 canvas->drawImage(make_color_space(premulImage, wideGamutLinear), 256.0f, 128.0f);
78 return skiagm::DrawResult::kOk;
79}
Brian Osmanf48c9962019-01-14 11:15:50 -050080
81DEF_SIMPLE_GM_BG(makecolortypeandspace, canvas, 128 * 3, 128 * 4, SK_ColorWHITE) {
82 sk_sp<SkImage> images[] = {
83 GetResourceAsImage("images/mandrill_128.png"),
84 GetResourceAsImage("images/color_wheel.png"),
85 };
86 auto rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kRec2020);
87
88 // Use the lazy images on the first iteration, and concrete (raster/GPU) images on the second
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, rec2020);
107 if (!lazy || image565->makeRasterImage()) {
108 canvas->drawImage(image565, 128, 0);
109 }
110
111 // Grayscale in the original color space. This fails in even more cases, due to the
112 // above opaque issue, and because Ganesh doesn't support drawing to gray, at all.
113 auto imageGray = image->makeColorTypeAndColorSpace(kGray_8_SkColorType,
114 image->refColorSpace());
115 if (!lazy || imageGray->makeRasterImage()) {
116 canvas->drawImage(imageGray, 256, 0);
117 }
118
119 images[j] = canvas->getGrContext()
Brian Osmand566e2e2019-08-14 13:19:04 -0400120 ? image->makeTextureImage(canvas->getGrContext())
Brian Osmanf48c9962019-01-14 11:15:50 -0500121 : image->makeRasterImage();
122
123 canvas->translate(0, 128);
124 }
125 }
126}
Brian Osmand5148372019-08-14 16:14:51 -0400127
128DEF_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)->reinterpretColorSpace(srgb), 256.0f, 0.0f);
154
155 canvas->translate(0.0f, 128.0f);
156
157 // Raster images
158 image = image->makeRasterImage();
159 canvas->drawImage(image, 0.0f, 0.0f);
160 canvas->drawImage(image->reinterpretColorSpace(spin), 128.0f, 0.0f);
161 canvas->drawImage(image->makeColorSpace(spin)->reinterpretColorSpace(srgb), 256.0f, 0.0f);
162
163 canvas->translate(0.0f, 128.0f);
164
165 // GPU images
Brian Osmanbacb8212019-08-15 10:38:26 -0400166 if (auto gpuImage = image->makeTextureImage(canvas->getGrContext())) {
167 image = gpuImage;
Brian Osmand5148372019-08-14 16:14:51 -0400168 }
169
170 canvas->drawImage(image, 0.0f, 0.0f);
171 canvas->drawImage(image->reinterpretColorSpace(spin), 128.0f, 0.0f);
172 canvas->drawImage(image->makeColorSpace(spin)->reinterpretColorSpace(srgb), 256.0f, 0.0f);
173
174 return skiagm::DrawResult::kOk;
175}