From 2f8622029d265e79eb11df8eb06a29ff3faa7ac0 Mon Sep 17 00:00:00 2001 From: Leon Scroggins III Date: Mon, 5 Mar 2018 14:19:40 -0500 Subject: Fix drawing SkAnimatedImages with transparency Bug: b/74195953 Do not use SkBlendMode::kSrc, which overwrites the pixels that were already present. Instead, blend normally. Change-Id: Ie6843c6278212fddddd0ba0ae292fdb5eaf2342e Reviewed-on: https://skia-review.googlesource.com/112200 Reviewed-by: Mike Klein Commit-Queue: Leon Scroggins --- tests/AnimatedImageTest.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'tests/AnimatedImageTest.cpp') diff --git a/tests/AnimatedImageTest.cpp b/tests/AnimatedImageTest.cpp index e0a640b7d8..6c7b0e5a55 100644 --- a/tests/AnimatedImageTest.cpp +++ b/tests/AnimatedImageTest.cpp @@ -15,6 +15,7 @@ #include "SkColor.h" #include "SkData.h" #include "SkImageInfo.h" +#include "SkPicture.h" #include "SkRefCnt.h" #include "SkSize.h" #include "SkString.h" @@ -27,6 +28,50 @@ #include #include +DEF_TEST(AnimatedImage_scaled, r) { + if (GetResourcePath().isEmpty()) { + return; + } + + const char* file = "images/alphabetAnim.gif"; + auto data = GetResourceAsData(file); + if (!data) { + ERRORF(r, "Could not get %s", file); + return; + } + + auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(data)); + if (!codec) { + ERRORF(r, "Could not create codec for %s", file); + return; + } + + // Force the drawable follow its special case that requires scaling. + auto size = codec->getInfo().dimensions(); + size.set(size.width() - 5, size.height() - 5); + auto rect = SkIRect::MakeSize(size); + auto image = SkAnimatedImage::Make(std::move(codec), size, rect, nullptr); + if (!image) { + ERRORF(r, "Failed to create animated image for %s", file); + return; + } + + // Clear a bitmap to non-transparent and draw to it. pixels that are transparent + // in the image should not replace the original non-transparent color. + SkBitmap bm; + bm.allocPixels(SkImageInfo::MakeN32Premul(size.width(), size.height())); + bm.eraseColor(SK_ColorBLUE); + SkCanvas canvas(bm); + image->draw(&canvas); + for (int i = 0; i < size.width(); ++i) + for (int j = 0; j < size.height(); ++j) { + if (*bm.getAddr32(i, j) == SK_ColorTRANSPARENT) { + ERRORF(r, "Erased color underneath!"); + return; + } + } +} + DEF_TEST(AnimatedImage, r) { if (GetResourcePath().isEmpty()) { return; -- cgit v1.2.3