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