Flutter Engine
The Flutter Engine
yuv420_odd_dim.cpp
Go to the documentation of this file.
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
21#include "src/base/SkRandom.h"
24#include "tools/DecodeUtils.h"
25#include "tools/Resources.h"
26#include "tools/gpu/YUVUtils.h"
27
28static constexpr int kScale = 10;
29static constexpr SkISize kImageDim = {5, 5};
30
32 skgpu::graphite::Recorder* recorder) {
33 // Generate a small jpeg with odd dimensions.
34 SkBitmap bmp;
36 SkRandom random;
37 // These random values won't compress well, but it doesn't matter. This test exists to
38 // compare the GPU YUV code path to the SW.
39 for (int y = 0; y < bmp.height(); ++y) {
40 for (int x = 0; x < bmp.width(); ++x) {
41 *bmp.getAddr32(x, y) = random.nextU() | 0xFF000000;
42 }
43 }
48 options.fQuality = 100;
50 return nullptr;
51 }
52 auto imageHelper = sk_gpu_test::LazyYUVImage::Make(stream.detachAsData());
53 if (!imageHelper) {
54 return nullptr;
55 }
56#if defined(SK_GRAPHITE)
57 if (recorder) {
58 return imageHelper->refImage(recorder, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps);
59 } else
60#endif
61 {
62 return imageHelper->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps);
63 }
64}
65
66// This GM tests that the YUVA image code path in the GPU backend handles odd sized images with
67// 420 chroma subsampling correctly.
68DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim, canvas, errMsg,
70 auto rContext = canvas->recordingContext();
71 skgpu::graphite::Recorder* recorder = nullptr;
72#if defined(SK_GRAPHITE)
73 recorder = canvas->recorder();
74#endif
75 if (!rContext && !recorder) {
76 // This GM exists to exercise GPU planar images.
78 }
79 auto image = make_image(rContext, recorder);
80 if (!image) {
81 if (rContext) {
82 return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail;
83 } else {
85 }
86 }
87 // We draw the image offscreen and then blow it up using nearest filtering by kScale.
88 // This avoids skbug.com/9693
90 if (auto origSurface = canvas->getSurface()) {
91 surface = origSurface->makeSurface(image->width(), image->height());
92 } else {
93 auto ct = canvas->imageInfo().colorType();
94 if (ct == kUnknown_SkColorType) {
95 ct = image->colorType();
96 }
97 auto info = canvas->imageInfo().makeColorType(ct);
98 info = info.makeAlphaType(kPremul_SkAlphaType);
100 }
101 surface->getCanvas()->drawImage(image, 0, 0);
102 canvas->scale(kScale, kScale);
103 canvas->drawImage(surface->makeImageSnapshot(), 0, 0);
105}
106
107// crbug.com/1210557 Subsampled planes weren't repeated at the correct frequency.
108DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim_repeat, canvas, errMsg,
109 1000,
110 500) {
111 auto rContext = canvas->recordingContext();
112 skgpu::graphite::Recorder* recorder = nullptr;
113#if defined(SK_GRAPHITE)
114 recorder = canvas->recorder();
115#endif
116 if (!rContext && !recorder) {
117 // This GM exists to exercise GPU planar images.
119 }
120 auto image = ToolUtils::GetResourceAsImage("images/mandrill_256.png");
121 if (!image) {
122 if (rContext) {
123 return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail;
124 } else {
126 }
127 }
128 // Make sure the image is odd dimensioned.
129 int w = image->width() & 0b1 ? image->width() : image->width() - 1;
130 int h = image->height() & 0b1 ? image->height() : image->height() - 1;
131 image = image->makeSubset(nullptr, SkIRect::MakeWH(w, h));
132
133 auto [planes, yuvaInfo] = sk_gpu_test::MakeYUVAPlanesAsA8(image.get(),
136 nullptr);
137 SkPixmap pixmaps[4];
138 for (int i = 0; i < yuvaInfo.numPlanes(); ++i) {
139 planes[i]->peekPixels(&pixmaps[i]);
140 }
141 auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, pixmaps);
142 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(yuvaPixmaps, skgpu::Mipmapped::kYes);
143
144#if defined(SK_GRAPHITE)
145 if (recorder) {
146 image = lazyYUV->refImage(recorder, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps);
147 } else
148#endif
149 {
150 image = lazyYUV->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps);
151 }
152 if (!image) {
153 *errMsg = "Could not make YUVA image";
154 if (rContext) {
155 return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail;
156 } else {
158 }
159 }
160 int i = 0;
162 int j = 0;
164 canvas->save();
165 canvas->clipRect(SkRect::MakeXYWH(500.f*j, 250.f*i, 500.f, 250.f));
166 canvas->rotate(30.f);
167 canvas->scale(0.4f, 0.4f); // so mipmaps sampling doesn't just use base level.
168 // Large translation so that if U/V planes aren't repeated correctly WRT to Y plane we
169 // accumulate a lot of error.
170 canvas->translate(-240000.f, -240000.f);
171 auto shader = image->makeShader(SkTileMode::kRepeat,
173 SkSamplingOptions(filter, mm));
175 paint.setShader(std::move(shader));
176 canvas->drawPaint(paint);
177 canvas->restore();
178 ++j;
179 }
180 ++i;
181 }
183}
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
@ kJPEG_SkYUVColorSpace
Definition: SkImageInfo.h:98
SkFilterMode
SkMipmapMode
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
void notifyPixelsChanged() const
Definition: SkBitmap.cpp:365
int width() const
Definition: SkBitmap.h:149
const SkPixmap & pixmap() const
Definition: SkBitmap.h:133
int height() const
Definition: SkBitmap.h:158
uint32_t * getAddr32(int x, int y) const
Definition: SkBitmap.h:1260
int width() const
Definition: SkImage.h:285
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
SkColorType colorType() const
Definition: SkImage.cpp:152
int height() const
Definition: SkImage.h:291
virtual sk_sp< SkImage > makeSubset(GrDirectContext *direct, const SkIRect &subset) const =0
uint32_t nextU()
Definition: SkRandom.h:42
@ k420
1 set of UV values for each 2x2 block of Y values.
static SkYUVAPixmaps FromExternalPixmaps(const SkYUVAInfo &, const SkPixmap[kMaxPlanes])
static std::unique_ptr< LazyYUVImage > Make(sk_sp< SkData > data, skgpu::Mipmapped=skgpu::Mipmapped::kNo, sk_sp< SkColorSpace >=nullptr)
Definition: YUVUtils.cpp:200
T * get() const
Definition: SkRefCnt.h:303
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
double y
double x
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
std::tuple< std::array< sk_sp< SkImage >, SkYUVAInfo::kMaxPlanes >, SkYUVAInfo > MakeYUVAPlanesAsA8(SkImage *src, SkYUVColorSpace cs, SkYUVAInfo::Subsampling ss, GrRecordingContext *rContext)
Definition: YUVUtils.cpp:152
SkSamplingOptions(SkFilterMode::kLinear))
SkScalar w
SkScalar h
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static sk_sp< SkImage > make_image(GrRecordingContext *rContext, skgpu::graphite::Recorder *recorder)
DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim, canvas, errMsg, kScale *kImageDim.width(), kScale *kImageDim.height())
static constexpr SkISize kImageDim
static constexpr int kScale