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 "include/gpu/GrRecordingContext.h" 12 #include "src/core/SkCachedData.h" 13 #include "src/image/SkImage_Base.h" 14 #include "tools/Resources.h" 15 #include "tools/ToolUtils.h" 16 #include "tools/gpu/YUVUtils.h" 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) { 21 sk_sp<SkImage> duckyBG = GetResourceAsImage("images/ducky.png"); 22 sk_sp<SkImage> duckyFG[2] = {GetResourceAsImage("images/ducky.jpg"), nullptr}; 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, 30 if (auto* rContext = canvas->recordingContext(); rContext && !rContext->abandoned()) { 31 auto lazyYUV = sk_gpu_test::LazyYUVImage::Make(GetResourceAsData("images/ducky.jpg"), 32 GrMipmapped::kYes); 33 if (lazyYUV) { 34 duckyFG[1] = lazyYUV->refImage(rContext, sk_gpu_test::LazyYUVImage::Type::kFromPixmaps); 35 } 36 if (!duckyFG[1]) { 37 return skiagm::DrawResult::kFail; 38 } 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(); __anon901d81a80102null49 auto newRow = [&] { 50 canvas->restore(); 51 canvas->translate(0, kDstRect.height() + kPad); 52 canvas->save(); 53 rowCnt = 0; 54 }; 55 SkSamplingOptions sampling(SkFilterMode::kLinear, 56 SkMipmapMode::kNearest); 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) { 63 canvas->drawImageRect(duckyBG, kDstRect, sampling, nullptr); 64 SkPaint paint; 65 paint.setBlendMode(static_cast<SkBlendMode>(bm)); 66 canvas->drawImageRect(fg, kDstRect, sampling, &paint); 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 } 78