blob: f66bbfef12d5e73b25e93f818d6d98b7f5b936ab [file] [log] [blame]
Brian Salomon8a99a412019-12-04 11:05:35 -05001/*
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 Osman01e6d172020-03-30 15:57:14 -040011#include "include/core/SkCanvas.h"
Brian Salomon8a99a412019-12-04 11:05:35 -050012#include "include/core/SkImage.h"
13#include "include/core/SkPixmap.h"
14#include "include/core/SkSurface.h"
Brian Salomon8a99a412019-12-04 11:05:35 -050015#include "include/encode/SkJpegEncoder.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040016#include "include/gpu/GrRecordingContext.h"
Brian Salomon8a99a412019-12-04 11:05:35 -050017#include "include/utils/SkRandom.h"
18#include "src/core/SkCachedData.h"
19#include "src/image/SkImage_Base.h"
Brian Salomon7b0c8df2021-05-24 10:51:47 -040020#include "tools/Resources.h"
Brian Salomonefb5f072020-07-28 21:06:43 -040021#include "tools/gpu/YUVUtils.h"
Brian Salomon8a99a412019-12-04 11:05:35 -050022
23static constexpr int kScale = 10;
24static constexpr SkISize kImageDim = {5, 5};
25
Brian Salomonefb5f072020-07-28 21:06:43 -040026static sk_sp<SkImage> make_image(GrRecordingContext* rContext) {
Brian Salomon8a99a412019-12-04 11:05:35 -050027 // 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 Salomon8a99a412019-12-04 11:05:35 -050039 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 Salomonefb5f072020-07-28 21:06:43 -040046 auto imageHelper = sk_gpu_test::LazyYUVImage::Make(stream.detachAsData());
47 if (!imageHelper) {
Brian Salomon8a99a412019-12-04 11:05:35 -050048 return nullptr;
49 }
Brian Salomon7db71392020-10-16 10:05:21 -040050 return imageHelper->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps);
Brian Salomon8a99a412019-12-04 11:05:35 -050051}
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.
55DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim, canvas, errMsg,
56 kScale* kImageDim.width(), kScale* kImageDim.height()) {
Brian Salomonefb5f072020-07-28 21:06:43 -040057 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 Salomon8a99a412019-12-04 11:05:35 -050063 if (!image) {
Brian Salomonefb5f072020-07-28 21:06:43 -040064 return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail;
Brian Salomon8a99a412019-12-04 11:05:35 -050065 }
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 Salomon7b0c8df2021-05-24 10:51:47 -040085
86// crbug.com/1210557 Subsampled planes weren't repeated at the correct frequency.
87DEF_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 Salomon6be4e452021-05-24 15:00:14 -0400120 return rContext->abandoned() ? skiagm::DrawResult::kSkip : skiagm::DrawResult::kFail;
Brian Salomon7b0c8df2021-05-24 10:51:47 -0400121 }
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}