blob: 049f825640e0f1881578987ab8641cbd975ed48b [file] [log] [blame]
Brian Salomonc75bc032019-11-11 13:47:25 -05001/*
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 Salomonbcde9ee2020-07-30 20:25:04 -040011#include "include/gpu/GrRecordingContext.h"
Brian Salomonc75bc032019-11-11 13:47:25 -050012#include "src/core/SkCachedData.h"
13#include "src/image/SkImage_Base.h"
14#include "tools/Resources.h"
15#include "tools/ToolUtils.h"
Brian Salomonefb5f072020-07-28 21:06:43 -040016#include "tools/gpu/YUVUtils.h"
Brian Salomonc75bc032019-11-11 13:47:25 -050017
18// Modeled on the layout test css3/blending/background-blend-mode-image-image.html to reproduce
19// skbug.com/9619
20DEF_SIMPLE_GM_CAN_FAIL(ducky_yuv_blend, canvas, errorMsg, 560, 1130) {
Mike Reed6e9b1792020-03-25 07:51:24 -040021 sk_sp<SkImage> duckyBG = GetResourceAsImage("images/ducky.png");
22 sk_sp<SkImage> duckyFG[2] = {GetResourceAsImage("images/ducky.jpg"), nullptr};
Brian Salomonc75bc032019-11-11 13:47:25 -050023 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 Salomonbcde9ee2020-07-30 20:25:04 -040030 if (auto* rContext = canvas->recordingContext(); rContext && !rContext->abandoned()) {
Brian Salomon6db78b82020-07-31 08:57:48 -040031 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(GetResourceAsData("images/ducky.jpg"),
32 GrMipmapped::kYes);
Brian Salomonefb5f072020-07-28 21:06:43 -040033 if (lazyYUV) {
Brian Salomon7db71392020-10-16 10:05:21 -040034 duckyFG[1] = lazyYUV->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps);
Brian Salomonc75bc032019-11-11 13:47:25 -050035 }
Brian Salomonefb5f072020-07-28 21:06:43 -040036 if (!duckyFG[1]) {
37 return skiagm::DrawResult::kFail;
38 }
Brian Salomonc75bc032019-11-11 13:47:25 -050039 } 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 Reedd396cd52021-01-23 21:14:47 -050055 SkSamplingOptions sampling(SkFilterMode::kLinear,
56 SkMipmapMode::kNearest);
Brian Salomonc75bc032019-11-11 13:47:25 -050057 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 Reedd396cd52021-01-23 21:14:47 -050063 canvas->drawImageRect(duckyBG, kDstRect, sampling, nullptr);
Brian Salomonc75bc032019-11-11 13:47:25 -050064 SkPaint paint;
Mike Reedd396cd52021-01-23 21:14:47 -050065 paint.setBlendMode(static_cast<SkBlendMode>(bm));
66 canvas->drawImageRect(fg, kDstRect, sampling, &paint);
Brian Salomonc75bc032019-11-11 13:47:25 -050067 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}