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