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