Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 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" |
Brian Osman | 01e6d17 | 2020-03-30 15:57:14 -0400 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkPixmap.h" |
| 14 | #include "include/core/SkSurface.h" |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 15 | #include "include/encode/SkJpegEncoder.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 16 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 17 | #include "include/utils/SkRandom.h" |
| 18 | #include "src/core/SkCachedData.h" |
| 19 | #include "src/image/SkImage_Base.h" |
Brian Salomon | 7b0c8df | 2021-05-24 10:51:47 -0400 | [diff] [blame] | 20 | #include "tools/Resources.h" |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 21 | #include "tools/gpu/YUVUtils.h" |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 22 | |
| 23 | static constexpr int kScale = 10; |
| 24 | static constexpr SkISize kImageDim = {5, 5}; |
| 25 | |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 26 | static sk_sp<SkImage> make_image(GrRecordingContext* rContext) { |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 27 | // Generate a small jpeg with odd dimensions. |
| 28 | SkBitmap bmp; |
| 29 | bmp.allocPixels(SkImageInfo::Make(kImageDim, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 30 | SkRandom random; |
| 31 | // These random values won't compress well, but it doesn't matter. This test exists to |
| 32 | // compare the GPU YUV code path to the SW. |
| 33 | for (int y = 0; y < bmp.height(); ++y) { |
| 34 | for (int x = 0; x < bmp.width(); ++x) { |
| 35 | *bmp.getAddr32(x, y) = random.nextU() | 0xFF000000; |
| 36 | } |
| 37 | } |
| 38 | bmp.notifyPixelsChanged(); |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 39 | SkDynamicMemoryWStream stream; |
| 40 | SkJpegEncoder::Options options; |
| 41 | options.fDownsample = SkJpegEncoder::Downsample::k420; |
| 42 | options.fQuality = 100; |
| 43 | if (!SkJpegEncoder::Encode(&stream, bmp.pixmap(), options)) { |
| 44 | return nullptr; |
| 45 | } |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 46 | auto imageHelper = sk_gpu_test::LazyYUVImage::Make(stream.detachAsData()); |
| 47 | if (!imageHelper) { |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 48 | return nullptr; |
| 49 | } |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 50 | return imageHelper->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps); |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // This GM tests that the YUVA image code path in the GPU backend handles odd sized images with |
| 54 | // 420 chroma subsampling correctly. |
| 55 | DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim, canvas, errMsg, |
| 56 | kScale* kImageDim.width(), kScale* kImageDim.height()) { |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 57 | auto rContext = canvas->recordingContext(); |
| 58 | if (!rContext) { |
| 59 | // This GM exists to exercise GPU planar images. |
| 60 | return skiagm::DrawResult::kSkip; |
| 61 | } |
| 62 | auto image = make_image(rContext); |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 63 | if (!image) { |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 64 | return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail; |
Brian Salomon | 8a99a41 | 2019-12-04 11:05:35 -0500 | [diff] [blame] | 65 | } |
| 66 | // We draw the image offscreen and then blow it up using nearest filtering by kScale. |
| 67 | // This avoids skbug.com/9693 |
| 68 | sk_sp<SkSurface> surface; |
| 69 | if (auto origSurface = canvas->getSurface()) { |
| 70 | surface = origSurface->makeSurface(image->width(), image->height()); |
| 71 | } else { |
| 72 | auto ct = canvas->imageInfo().colorType(); |
| 73 | if (ct == kUnknown_SkColorType) { |
| 74 | ct = image->colorType(); |
| 75 | } |
| 76 | auto info = canvas->imageInfo().makeColorType(ct); |
| 77 | info = info.makeAlphaType(kPremul_SkAlphaType); |
| 78 | surface = SkSurface::MakeRaster(info); |
| 79 | } |
| 80 | surface->getCanvas()->drawImage(image, 0, 0); |
| 81 | canvas->scale(kScale, kScale); |
| 82 | canvas->drawImage(surface->makeImageSnapshot(), 0, 0); |
| 83 | return skiagm::DrawResult::kOk; |
| 84 | } |
Brian Salomon | 7b0c8df | 2021-05-24 10:51:47 -0400 | [diff] [blame] | 85 | |
| 86 | // crbug.com/1210557 Subsampled planes weren't repeated at the correct frequency. |
| 87 | DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim_repeat, canvas, errMsg, |
| 88 | 1000, |
| 89 | 500) { |
| 90 | auto rContext = canvas->recordingContext(); |
| 91 | if (!rContext) { |
| 92 | // This GM exists to exercise GPU planar images. |
| 93 | return skiagm::DrawResult::kSkip; |
| 94 | } |
| 95 | auto image = GetResourceAsImage("images/mandrill_256.png"); |
| 96 | if (!image) { |
| 97 | return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail; |
| 98 | } |
| 99 | // Make sure the image is odd dimensioned. |
| 100 | int w = image->width() & 0b1 ? image->width() : image->width() - 1; |
| 101 | int h = image->height() & 0b1 ? image->height() : image->height() - 1; |
| 102 | image = image->makeSubset(SkIRect::MakeWH(w, h)); |
| 103 | |
| 104 | auto [planes, yuvaInfo] = sk_gpu_test::MakeYUVAPlanesAsA8(image.get(), |
| 105 | kJPEG_SkYUVColorSpace, |
| 106 | SkYUVAInfo::Subsampling::k420, |
| 107 | nullptr); |
| 108 | SkPixmap pixmaps[4]; |
| 109 | for (int i = 0; i < yuvaInfo.numPlanes(); ++i) { |
| 110 | planes[i]->peekPixels(&pixmaps[i]); |
| 111 | } |
| 112 | auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, pixmaps); |
| 113 | image = SkImage::MakeFromYUVAPixmaps(canvas->recordingContext(), |
| 114 | yuvaPixmaps, |
| 115 | GrMipMapped::kYes, |
| 116 | /* limit to max tex size */ false, |
| 117 | /* color space */ nullptr); |
| 118 | if (!image) { |
| 119 | *errMsg = "Could not make YUVA image"; |
Brian Salomon | 6be4e45 | 2021-05-24 15:00:14 -0400 | [diff] [blame] | 120 | return rContext->abandoned() ? skiagm::DrawResult::kSkip : skiagm::DrawResult::kFail; |
Brian Salomon | 7b0c8df | 2021-05-24 10:51:47 -0400 | [diff] [blame] | 121 | } |
| 122 | int i = 0; |
| 123 | for (SkMipmapMode mm : {SkMipmapMode::kNone, SkMipmapMode::kLinear}) { |
| 124 | int j = 0; |
| 125 | for (SkFilterMode filter : {SkFilterMode::kNearest, SkFilterMode::kLinear}) { |
| 126 | canvas->save(); |
| 127 | canvas->clipRect(SkRect::MakeXYWH(500.f*j, 250.f*i, 500.f, 250.f)); |
| 128 | canvas->rotate(30.f); |
| 129 | canvas->scale(0.4f, 0.4f); // so mipmaps sampling doesn't just use base level. |
| 130 | // Large translation so that if U/V planes aren't repeated correctly WRT to Y plane we |
| 131 | // accumulate a lot of error. |
| 132 | canvas->translate(-240000.f, -240000.f); |
| 133 | auto shader = image->makeShader(SkTileMode::kRepeat, |
| 134 | SkTileMode::kRepeat, |
| 135 | SkSamplingOptions(filter, mm)); |
| 136 | SkPaint paint; |
| 137 | paint.setShader(std::move(shader)); |
| 138 | canvas->drawPaint(paint); |
| 139 | canvas->restore(); |
| 140 | ++j; |
| 141 | } |
| 142 | ++i; |
| 143 | } |
| 144 | return skiagm::DrawResult::kOk; |
| 145 | } |