blob: 93bb25bb71938726600dedf8d9bdeffa5faff998 [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 Salomonefb5f072020-07-28 21:06:43 -040020#include "tools/gpu/YUVUtils.h"
Brian Salomon8a99a412019-12-04 11:05:35 -050021
22static constexpr int kScale = 10;
23static constexpr SkISize kImageDim = {5, 5};
24
Brian Salomonefb5f072020-07-28 21:06:43 -040025static sk_sp<SkImage> make_image(GrRecordingContext* rContext) {
Brian Salomon8a99a412019-12-04 11:05:35 -050026 // Generate a small jpeg with odd dimensions.
27 SkBitmap bmp;
28 bmp.allocPixels(SkImageInfo::Make(kImageDim, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
29 SkRandom random;
30 // These random values won't compress well, but it doesn't matter. This test exists to
31 // compare the GPU YUV code path to the SW.
32 for (int y = 0; y < bmp.height(); ++y) {
33 for (int x = 0; x < bmp.width(); ++x) {
34 *bmp.getAddr32(x, y) = random.nextU() | 0xFF000000;
35 }
36 }
37 bmp.notifyPixelsChanged();
Brian Salomon8a99a412019-12-04 11:05:35 -050038 SkDynamicMemoryWStream stream;
39 SkJpegEncoder::Options options;
40 options.fDownsample = SkJpegEncoder::Downsample::k420;
41 options.fQuality = 100;
42 if (!SkJpegEncoder::Encode(&stream, bmp.pixmap(), options)) {
43 return nullptr;
44 }
Brian Salomonefb5f072020-07-28 21:06:43 -040045 auto imageHelper = sk_gpu_test::LazyYUVImage::Make(stream.detachAsData());
46 if (!imageHelper) {
Brian Salomon8a99a412019-12-04 11:05:35 -050047 return nullptr;
48 }
Brian Salomon7db71392020-10-16 10:05:21 -040049 return imageHelper->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps);
Brian Salomon8a99a412019-12-04 11:05:35 -050050}
51
52// This GM tests that the YUVA image code path in the GPU backend handles odd sized images with
53// 420 chroma subsampling correctly.
54DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim, canvas, errMsg,
55 kScale* kImageDim.width(), kScale* kImageDim.height()) {
Brian Salomonefb5f072020-07-28 21:06:43 -040056 auto rContext = canvas->recordingContext();
57 if (!rContext) {
58 // This GM exists to exercise GPU planar images.
59 return skiagm::DrawResult::kSkip;
60 }
61 auto image = make_image(rContext);
Brian Salomon8a99a412019-12-04 11:05:35 -050062 if (!image) {
Brian Salomonefb5f072020-07-28 21:06:43 -040063 return rContext->abandoned() ? skiagm::DrawResult::kOk : skiagm::DrawResult::kFail;
Brian Salomon8a99a412019-12-04 11:05:35 -050064 }
65 // We draw the image offscreen and then blow it up using nearest filtering by kScale.
66 // This avoids skbug.com/9693
67 sk_sp<SkSurface> surface;
68 if (auto origSurface = canvas->getSurface()) {
69 surface = origSurface->makeSurface(image->width(), image->height());
70 } else {
71 auto ct = canvas->imageInfo().colorType();
72 if (ct == kUnknown_SkColorType) {
73 ct = image->colorType();
74 }
75 auto info = canvas->imageInfo().makeColorType(ct);
76 info = info.makeAlphaType(kPremul_SkAlphaType);
77 surface = SkSurface::MakeRaster(info);
78 }
79 surface->getCanvas()->drawImage(image, 0, 0);
80 canvas->scale(kScale, kScale);
81 canvas->drawImage(surface->makeImageSnapshot(), 0, 0);
82 return skiagm::DrawResult::kOk;
83}