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