Brian Salomon | c75bc03 | 2019-11-11 13:47:25 -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 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkPaint.h" |
Brian Salomon | bcde9ee | 2020-07-30 20:25:04 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | c75bc03 | 2019-11-11 13:47:25 -0500 | [diff] [blame] | 12 | #include "src/core/SkCachedData.h" |
| 13 | #include "src/image/SkImage_Base.h" |
| 14 | #include "tools/Resources.h" |
| 15 | #include "tools/ToolUtils.h" |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 16 | #include "tools/gpu/YUVUtils.h" |
Brian Salomon | c75bc03 | 2019-11-11 13:47:25 -0500 | [diff] [blame] | 17 | |
| 18 | // Modeled on the layout test css3/blending/background-blend-mode-image-image.html to reproduce |
| 19 | // skbug.com/9619 |
| 20 | DEF_SIMPLE_GM_CAN_FAIL(ducky_yuv_blend, canvas, errorMsg, 560, 1130) { |
Mike Reed | 6e9b179 | 2020-03-25 07:51:24 -0400 | [diff] [blame] | 21 | sk_sp<SkImage> duckyBG = GetResourceAsImage("images/ducky.png"); |
| 22 | sk_sp<SkImage> duckyFG[2] = {GetResourceAsImage("images/ducky.jpg"), nullptr}; |
Brian Salomon | c75bc03 | 2019-11-11 13:47:25 -0500 | [diff] [blame] | 23 | if (!duckyFG[0] || !duckyBG) { |
| 24 | *errorMsg = "Image(s) failed to load."; |
| 25 | return skiagm::DrawResult::kFail; |
| 26 | } |
| 27 | |
| 28 | // If we're on the GPU we do a second round of draws where the source image is YUV planes. |
| 29 | // Otherwise we just draw the original again, |
Brian Salomon | bcde9ee | 2020-07-30 20:25:04 -0400 | [diff] [blame] | 30 | if (auto* rContext = canvas->recordingContext(); rContext && !rContext->abandoned()) { |
Brian Salomon | 6db78b8 | 2020-07-31 08:57:48 -0400 | [diff] [blame] | 31 | auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(GetResourceAsData("images/ducky.jpg"), |
| 32 | GrMipmapped::kYes); |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 33 | if (lazyYUV) { |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 34 | duckyFG[1] = lazyYUV->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps); |
Brian Salomon | c75bc03 | 2019-11-11 13:47:25 -0500 | [diff] [blame] | 35 | } |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 36 | if (!duckyFG[1]) { |
| 37 | return skiagm::DrawResult::kFail; |
| 38 | } |
Brian Salomon | c75bc03 | 2019-11-11 13:47:25 -0500 | [diff] [blame] | 39 | } else { |
| 40 | duckyFG[1] = duckyFG[0]; |
| 41 | } |
| 42 | |
| 43 | static constexpr int kNumPerRow = 4; |
| 44 | static constexpr int kPad = 10; |
| 45 | static constexpr auto kDstRect = SkRect::MakeWH(130, 130); |
| 46 | int rowCnt = 0; |
| 47 | canvas->translate(kPad, kPad); |
| 48 | canvas->save(); |
| 49 | auto newRow = [&] { |
| 50 | canvas->restore(); |
| 51 | canvas->translate(0, kDstRect.height() + kPad); |
| 52 | canvas->save(); |
| 53 | rowCnt = 0; |
| 54 | }; |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 55 | SkSamplingOptions sampling(SkFilterMode::kLinear, |
| 56 | SkMipmapMode::kNearest); |
Brian Salomon | c75bc03 | 2019-11-11 13:47:25 -0500 | [diff] [blame] | 57 | ToolUtils::draw_checkerboard( |
| 58 | canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, (kDstRect.height() + kPad)/5); |
| 59 | for (auto& fg : duckyFG) { |
| 60 | for (int bm = static_cast<int>(SkBlendMode::kLastCoeffMode) + 1; |
| 61 | bm < static_cast<int>(SkBlendMode::kLastMode); |
| 62 | ++bm) { |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 63 | canvas->drawImageRect(duckyBG, kDstRect, sampling, nullptr); |
Brian Salomon | c75bc03 | 2019-11-11 13:47:25 -0500 | [diff] [blame] | 64 | SkPaint paint; |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 65 | paint.setBlendMode(static_cast<SkBlendMode>(bm)); |
| 66 | canvas->drawImageRect(fg, kDstRect, sampling, &paint); |
Brian Salomon | c75bc03 | 2019-11-11 13:47:25 -0500 | [diff] [blame] | 67 | canvas->translate(kDstRect.width() + kPad, 0); |
| 68 | if (++rowCnt == kNumPerRow) { |
| 69 | newRow(); |
| 70 | } |
| 71 | } |
| 72 | // Force a new row between the two foreground images |
| 73 | newRow(); |
| 74 | } |
| 75 | canvas->restore(); |
| 76 | return skiagm::DrawResult::kOk; |
| 77 | } |