Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/codec/SkCodec.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkColorSpace.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkData.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 16 | #include "include/core/SkImageInfo.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/core/SkImagePriv.h" |
| 21 | #include "tools/Resources.h" |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 22 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 23 | #include <initializer_list> |
| 24 | #include <memory> |
| 25 | |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 26 | sk_sp<SkImage> make_raster_image(const char* path) { |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 27 | sk_sp<SkData> resourceData = GetResourceAsData(path); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 28 | std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(resourceData); |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 29 | |
| 30 | SkBitmap bitmap; |
| 31 | bitmap.allocPixels(codec->getInfo()); |
| 32 | |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 33 | codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes()); |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 34 | return SkImage::MakeFromBitmap(bitmap); |
| 35 | } |
| 36 | |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 37 | sk_sp<SkImage> make_color_space(sk_sp<SkImage> orig, sk_sp<SkColorSpace> colorSpace) { |
| 38 | sk_sp<SkImage> xform = orig->makeColorSpace(colorSpace); |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 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 | } |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 46 | return xform->reinterpretColorSpace(std::move(srgb)); |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 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(); |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 53 | |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 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; |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 60 | } |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 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); |
Matt Sarett | 9f3dcb3 | 2017-05-04 08:53:32 -0400 | [diff] [blame] | 68 | |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 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 | } |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 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() |
Brian Osman | d566e2e | 2019-08-14 13:19:04 -0400 | [diff] [blame] | 120 | ? image->makeTextureImage(canvas->getGrContext()) |
Brian Osman | f48c996 | 2019-01-14 11:15:50 -0500 | [diff] [blame] | 121 | : image->makeRasterImage(); |
| 122 | |
| 123 | canvas->translate(0, 128); |
| 124 | } |
| 125 | } |
| 126 | } |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 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 |
Brian Osman | bacb821 | 2019-08-15 10:38:26 -0400 | [diff] [blame] | 166 | if (auto gpuImage = image->makeTextureImage(canvas->getGrContext())) { |
| 167 | image = gpuImage; |
Brian Osman | d514837 | 2019-08-14 16:14:51 -0400 | [diff] [blame] | 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 | } |