| 1 |
/* |
| 2 |
* Copyright 2019 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 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkPixmap.h" |
| 14 |
#include "include/core/SkShader.h" |
| 15 |
#include "include/core/SkStream.h" |
| 16 |
#include "include/core/SkSurface.h" |
| 17 |
#include "include/core/SkTileMode.h" |
| 18 |
#include "include/encode/SkJpegEncoder.h" |
| 19 |
#include "src/base/SkRandom.h" |
| 20 |
#include "src/core/SkCachedData.h" |
| 21 |
#include "src/image/SkImage_Base.h" |
| 22 |
#include "tools/DecodeUtils.h" |
| 23 |
#include "tools/Resources.h" |
| 24 |
#include "tools/gpu/YUVUtils.h" |
| 25 |
|
| 26 |
#if defined(SK_GANESH) |
| 27 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 28 |
#include "include/gpu/ganesh/SkImageGanesh.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
static constexpr int kScale = 10; |
| 32 |
static constexpr SkISize kImageDim = {5, 5}; |
| 33 |
|
| 34 |
static sk_sp<SkImage> make_image(GrRecordingContext* rContext, |
| 35 |
skgpu::graphite::Recorder* recorder) { |
| 36 |
// Generate a small jpeg with odd dimensions. |
| 37 |
SkBitmap bmp; |
| 38 |
bmp.allocPixels(SkImageInfo::Make(kImageDim, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 39 |
SkRandom random; |
| 40 |
// These random values won't compress well, but it doesn't matter. This test exists to |
| 41 |
// compare the GPU YUV code path to the SW. |
| 42 |
for (int y = 0; y < bmp.height(); ++y) { |
| 43 |
for (int x = 0; x < bmp.width(); ++x) { |
| 44 |
*bmp.getAddr32(x, y) = random.nextU() | 0xFF000000; |
| 45 |
} |
| 46 |
} |
| 47 |
bmp.notifyPixelsChanged(); |
| 48 |
SkJpegEncoder::Options options; |
| 49 |
options.fDownsample = SkJpegEncoder::Downsample::k420; |
| 50 |
options.fQuality = 100; |
| 51 |
sk_sp<SkData> data = SkJpegEncoder::Encode(bmp.pixmap(), options); |
| 52 |
if (!data) { |
| 53 |
return nullptr; |
| 54 |
} |
| 55 |
auto imageHelper = sk_gpu_test::LazyYUVImage::Make(data); |
| 56 |
if (!imageHelper) { |
| 57 |
return nullptr; |
| 58 |
} |
| 59 |
#if defined(SK_GRAPHITE) |
| 60 |
if (recorder) { |
| 61 |
return imageHelper->refImage(recorder, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps); |
| 62 |
} |
| 63 |
#endif |
| 64 |
#if defined(SK_GANESH) |
| 65 |
if (rContext) { |
| 66 |
return imageHelper->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps); |
| 67 |
} |
| 68 |
#endif |
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
|
| 72 |
// This GM tests that the YUVA image code path in the GPU backend handles odd sized images with |
| 73 |
// 420 chroma subsampling correctly. |
| 74 |
DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim, canvas, errMsg, |
| 75 |
kScale* kImageDim.width(), kScale* kImageDim.height()) { |
| 76 |
auto rContext = canvas->recordingContext(); |
| 77 |
skgpu::graphite::Recorder* recorder = nullptr; |
| 78 |
#if defined(SK_GRAPHITE) |
| 79 |
recorder = canvas->recorder(); |
| 80 |
#endif |
| 81 |
if (!rContext && !recorder) { |
| 82 |
// This GM exists to exercise GPU planar images. |
| 83 |
return skiagm::DrawResult::kSkip; |
| 84 |
} |
| 85 |
auto image = make_image(rContext, recorder); |
| 86 |
if (!image) { |
| 87 |
#if defined(SK_GANESH) |
| 88 |
if (rContext) { |
| 89 |
return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail; |
| 90 |
} |
| 91 |
#endif |
| 92 |
return skiagm::DrawResult::kFail; |
| 93 |
} |
| 94 |
// We draw the image offscreen and then blow it up using nearest filtering by kScale. |
| 95 |
// This avoids skbug.com/40040903 |
| 96 |
sk_sp<SkSurface> surface; |
| 97 |
if (auto origSurface = canvas->getSurface()) { |
| 98 |
surface = origSurface->makeSurface(image->width(), image->height()); |
| 99 |
} else { |
| 100 |
auto ct = canvas->imageInfo().colorType(); |
| 101 |
if (ct == kUnknown_SkColorType) { |
| 102 |
ct = image->colorType(); |
| 103 |
} |
| 104 |
auto info = canvas->imageInfo().makeColorType(ct); |
| 105 |
info = info.makeAlphaType(kPremul_SkAlphaType); |
| 106 |
surface = SkSurfaces::Raster(info); |
| 107 |
} |
| 108 |
surface->getCanvas()->drawImage(image, 0, 0); |
| 109 |
canvas->scale(kScale, kScale); |
| 110 |
canvas->drawImage(surface->makeImageSnapshot(), 0, 0); |
| 111 |
return skiagm::DrawResult::kOk; |
| 112 |
} |
| 113 |
|
| 114 |
// crbug.com/1210557 Subsampled planes weren't repeated at the correct frequency. |
| 115 |
DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim_repeat, canvas, errMsg, |
| 116 |
1000, |
| 117 |
500) { |
| 118 |
auto rContext = canvas->recordingContext(); |
| 119 |
skgpu::graphite::Recorder* recorder = nullptr; |
| 120 |
#if defined(SK_GRAPHITE) |
| 121 |
recorder = canvas->recorder(); |
| 122 |
#endif |
| 123 |
if (!rContext && !recorder) { |
| 124 |
// This GM exists to exercise GPU planar images. |
| 125 |
return skiagm::DrawResult::kSkip; |
| 126 |
} |
| 127 |
auto image = ToolUtils::GetResourceAsImage("images/mandrill_256.png"); |
| 128 |
if (!image) { |
| 129 |
#if defined(SK_GANESH) |
| 130 |
if (rContext) { |
| 131 |
return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail; |
| 132 |
} |
| 133 |
#endif |
| 134 |
return skiagm::DrawResult::kFail; |
| 135 |
} |
| 136 |
// Make sure the image is odd dimensioned. |
| 137 |
int w = image->width() & 0b1 ? image->width() : image->width() - 1; |
| 138 |
int h = image->height() & 0b1 ? image->height() : image->height() - 1; |
| 139 |
image = image->makeSubset(nullptr, SkIRect::MakeWH(w, h), {}); |
| 140 |
|
| 141 |
auto [planes, yuvaInfo] = sk_gpu_test::MakeYUVAPlanesAsA8(image.get(), |
| 142 |
kJPEG_SkYUVColorSpace, |
| 143 |
SkYUVAInfo::Subsampling::k420, |
| 144 |
nullptr); |
| 145 |
SkPixmap pixmaps[4]; |
| 146 |
for (int i = 0; i < yuvaInfo.numPlanes(); ++i) { |
| 147 |
planes[i]->peekPixels(&pixmaps[i]); |
| 148 |
} |
| 149 |
auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, pixmaps); |
| 150 |
auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(yuvaPixmaps, skgpu::Mipmapped::kYes); |
| 151 |
|
| 152 |
#if defined(SK_GRAPHITE) |
| 153 |
if (recorder) { |
| 154 |
image = lazyYUV->refImage(recorder, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps); |
| 155 |
} else |
| 156 |
#endif |
| 157 |
#if defined(SK_GANESH) |
| 158 |
if (rContext) { |
| 159 |
image = lazyYUV->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps); |
| 160 |
} |
| 161 |
#endif |
| 162 |
if (!image) { |
| 163 |
*errMsg = "Could not make YUVA image"; |
| 164 |
#if defined(SK_GANESH) |
| 165 |
if (rContext) { |
| 166 |
return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail; |
| 167 |
} |
| 168 |
#endif |
| 169 |
return skiagm::DrawResult::kFail; |
| 170 |
} |
| 171 |
int i = 0; |
| 172 |
for (SkMipmapMode mm : {SkMipmapMode::kNone, SkMipmapMode::kLinear}) { |
| 173 |
int j = 0; |
| 174 |
for (SkFilterMode filter : {SkFilterMode::kNearest, SkFilterMode::kLinear}) { |
| 175 |
canvas->save(); |
| 176 |
canvas->clipRect(SkRect::MakeXYWH(500.f*j, 250.f*i, 500.f, 250.f)); |
| 177 |
canvas->rotate(30.f); |
| 178 |
canvas->scale(0.4f, 0.4f); // so mipmaps sampling doesn't just use base level. |
| 179 |
// Large translation so that if U/V planes aren't repeated correctly WRT to Y plane we |
| 180 |
// accumulate a lot of error. |
| 181 |
canvas->translate(-240000.f, -240000.f); |
| 182 |
auto shader = image->makeShader(SkTileMode::kRepeat, |
| 183 |
SkTileMode::kRepeat, |
| 184 |
SkSamplingOptions(filter, mm)); |
| 185 |
SkPaint paint; |
| 186 |
paint.setShader(std::move(shader)); |
| 187 |
canvas->drawPaint(paint); |
| 188 |
canvas->restore(); |
| 189 |
++j; |
| 190 |
} |
| 191 |
++i; |
| 192 |
} |
| 193 |
return skiagm::DrawResult::kOk; |
| 194 |
} |