• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/SkImagePriv.h"
21 #include "tools/Resources.h"
22 
23 #include <initializer_list>
24 #include <memory>
25 
make_raster_image(const char * path)26 sk_sp<SkImage> make_raster_image(const char* path) {
27     sk_sp<SkData> resourceData = GetResourceAsData(path);
28     std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(resourceData);
29 
30     SkBitmap bitmap;
31     bitmap.allocPixels(codec->getInfo());
32 
33     codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes());
34     return SkImage::MakeFromBitmap(bitmap);
35 }
36 
make_color_space(sk_sp<SkImage> orig,sk_sp<SkColorSpace> colorSpace)37 sk_sp<SkImage> make_color_space(sk_sp<SkImage> orig, sk_sp<SkColorSpace> colorSpace) {
38     sk_sp<SkImage> xform = orig->makeColorSpace(colorSpace);
39 
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     }
46     return xform->reinterpretColorSpace(std::move(srgb));
47 }
48 
49 DEF_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();
53 
54     // 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;
60     }
61     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);
68 
69     // 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 }
80 
81 DEF_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()
120                     ? image->makeTextureImage(canvas->getGrContext())
121                     : 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)->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
166     if (auto gpuImage = image->makeTextureImage(canvas->getGrContext())) {
167         image = gpuImage;
168     }
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 }
176