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" |
| 15 | #include "include/core/SkYUVAIndex.h" |
| 16 | #include "include/core/SkYUVASizeInfo.h" |
| 17 | #include "include/encode/SkJpegEncoder.h" |
| 18 | #include "include/gpu/GrContext.h" |
| 19 | #include "include/utils/SkRandom.h" |
| 20 | #include "src/core/SkCachedData.h" |
| 21 | #include "src/image/SkImage_Base.h" |
| 22 | |
| 23 | static constexpr int kScale = 10; |
| 24 | static constexpr SkISize kImageDim = {5, 5}; |
| 25 | |
| 26 | static sk_sp<SkImage> make_image(GrContext* context) { |
| 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(); |
| 39 | SkImage::MakeFromBitmap(bmp); |
| 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 image = SkImage::MakeFromEncoded(stream.detachAsData()); |
| 48 | if (!context) { |
| 49 | return image; |
| 50 | } |
| 51 | SkYUVASizeInfo info; |
| 52 | SkYUVAIndex indices[4]; |
| 53 | SkYUVColorSpace cs; |
| 54 | const void* planes[4]; |
| 55 | if (!as_IB(image)->getPlanes(&info, indices, &cs, planes)) { |
| 56 | return nullptr; |
| 57 | } |
| 58 | SkPixmap pixmaps[4]; |
| 59 | #ifdef SK_DEBUG |
| 60 | static constexpr SkISize kUVDim = {kImageDim.width() / 2 + 1, kImageDim.height() / 2 + 1}; |
| 61 | #endif |
| 62 | SkASSERT(info.fSizes[0] == kImageDim); |
| 63 | SkASSERT(info.fSizes[1] == kUVDim); |
| 64 | SkASSERT(info.fSizes[2] == kUVDim); |
| 65 | for (int i = 0; i < 4; ++i) { |
| 66 | if (!info.fSizes[i].isZero()) { |
| 67 | pixmaps[i].reset(SkImageInfo::MakeA8(info.fSizes[i]), planes[i], info.fWidthBytes[i]); |
| 68 | } |
| 69 | } |
| 70 | return SkImage::MakeFromYUVAPixmaps(context, cs, pixmaps, indices, image->dimensions(), |
| 71 | kTopLeft_GrSurfaceOrigin, false); |
| 72 | } |
| 73 | |
| 74 | // This GM tests that the YUVA image code path in the GPU backend handles odd sized images with |
| 75 | // 420 chroma subsampling correctly. |
| 76 | DEF_SIMPLE_GM_CAN_FAIL(yuv420_odd_dim, canvas, errMsg, |
| 77 | kScale* kImageDim.width(), kScale* kImageDim.height()) { |
| 78 | auto image = make_image(canvas->getGrContext()); |
| 79 | if (!image) { |
| 80 | if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) { |
| 81 | return skiagm::DrawResult::kOk; |
| 82 | } |
| 83 | return skiagm::DrawResult::kFail; |
| 84 | } |
| 85 | // We draw the image offscreen and then blow it up using nearest filtering by kScale. |
| 86 | // This avoids skbug.com/9693 |
| 87 | sk_sp<SkSurface> surface; |
| 88 | if (auto origSurface = canvas->getSurface()) { |
| 89 | surface = origSurface->makeSurface(image->width(), image->height()); |
| 90 | } else { |
| 91 | auto ct = canvas->imageInfo().colorType(); |
| 92 | if (ct == kUnknown_SkColorType) { |
| 93 | ct = image->colorType(); |
| 94 | } |
| 95 | auto info = canvas->imageInfo().makeColorType(ct); |
| 96 | info = info.makeAlphaType(kPremul_SkAlphaType); |
| 97 | surface = SkSurface::MakeRaster(info); |
| 98 | } |
| 99 | surface->getCanvas()->drawImage(image, 0, 0); |
| 100 | canvas->scale(kScale, kScale); |
| 101 | canvas->drawImage(surface->makeImageSnapshot(), 0, 0); |
| 102 | return skiagm::DrawResult::kOk; |
| 103 | } |