Replace blink::Image by cc::PaintImage.
Dark mode image classifier needs PaintImage than Image for classifying image content. Removing blink::Image for this helps make dark mode module independent of blink code. Bug: 1091095 Change-Id: I3e19fbebc2e73bb707f134dbde15ee0d15784f93 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2182098 Reviewed-by: Philip Rogers <pdr@chromium.org> Commit-Queue: Prashant Nevase <prashant.n@samsung.com> Cr-Commit-Position: refs/heads/master@{#777512}
This commit is contained in:

committed by
Commit Bot

parent
09fcef03e2
commit
2aaf9ca8a2
third_party/blink/renderer/platform/graphics
@ -154,11 +154,11 @@ SkColor DarkModeFilter::InvertColorIfNeeded(SkColor color, ElementRole role) {
|
||||
|
||||
void DarkModeFilter::ApplyToImageFlagsIfNeeded(const FloatRect& src_rect,
|
||||
const FloatRect& dest_rect,
|
||||
Image* image,
|
||||
const PaintImage& paint_image,
|
||||
cc::PaintFlags* flags,
|
||||
ElementRole element_role) {
|
||||
if (!image_filter_ || !ShouldApplyToImage(settings(), src_rect, dest_rect,
|
||||
image, element_role)) {
|
||||
paint_image, element_role)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -233,7 +233,7 @@ size_t DarkModeFilter::GetInvertedColorCacheSizeForTesting() {
|
||||
bool DarkModeFilter::ShouldApplyToImage(const DarkModeSettings& settings,
|
||||
const FloatRect& src_rect,
|
||||
const FloatRect& dest_rect,
|
||||
Image* image,
|
||||
const PaintImage& paint_image,
|
||||
ElementRole role) {
|
||||
switch (settings.image_policy) {
|
||||
case DarkModeImagePolicy::kFilterSmart: {
|
||||
@ -254,7 +254,7 @@ bool DarkModeFilter::ShouldApplyToImage(const DarkModeSettings& settings,
|
||||
}
|
||||
|
||||
DarkModeClassification result =
|
||||
classifier->Classify(image, src_rect, dest_rect);
|
||||
classifier->Classify(paint_image, src_rect, dest_rect);
|
||||
return result == DarkModeClassification::kApplyFilter;
|
||||
}
|
||||
case DarkModeImagePolicy::kFilterNone:
|
||||
|
@ -11,7 +11,7 @@
|
||||
#include "third_party/blink/renderer/platform/geometry/float_rect.h"
|
||||
#include "third_party/blink/renderer/platform/graphics/color.h"
|
||||
#include "third_party/blink/renderer/platform/graphics/dark_mode_settings.h"
|
||||
#include "third_party/blink/renderer/platform/graphics/image.h"
|
||||
#include "third_party/blink/renderer/platform/graphics/paint/paint_image.h"
|
||||
#include "third_party/blink/renderer/platform/platform_export.h"
|
||||
#include "third_party/skia/include/core/SkRefCnt.h"
|
||||
|
||||
@ -19,6 +19,7 @@ class SkColorFilter;
|
||||
|
||||
namespace blink {
|
||||
|
||||
class GraphicsContext;
|
||||
class DarkModeColorClassifier;
|
||||
class DarkModeImageClassifier;
|
||||
class DarkModeColorFilter;
|
||||
@ -59,7 +60,7 @@ class PLATFORM_EXPORT DarkModeFilter {
|
||||
// |image| and |flags| must not be null.
|
||||
void ApplyToImageFlagsIfNeeded(const FloatRect& src_rect,
|
||||
const FloatRect& dest_rect,
|
||||
Image* image,
|
||||
const PaintImage& paint_image,
|
||||
cc::PaintFlags* flags,
|
||||
ElementRole element_role);
|
||||
|
||||
@ -75,7 +76,7 @@ class PLATFORM_EXPORT DarkModeFilter {
|
||||
bool ShouldApplyToImage(const DarkModeSettings& settings,
|
||||
const FloatRect& src_rect,
|
||||
const FloatRect& dest_rect,
|
||||
Image* image,
|
||||
const PaintImage& paint_image,
|
||||
ElementRole role);
|
||||
|
||||
std::unique_ptr<DarkModeColorClassifier> text_classifier_;
|
||||
|
@ -9,7 +9,6 @@
|
||||
#include "base/memory/singleton.h"
|
||||
#include "base/optional.h"
|
||||
#include "third_party/blink/renderer/platform/graphics/darkmode/darkmode_classifier.h"
|
||||
#include "third_party/blink/renderer/platform/graphics/image.h"
|
||||
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
|
||||
#include "third_party/blink/renderer/platform/wtf/hash_traits.h"
|
||||
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
|
||||
@ -161,12 +160,12 @@ DarkModeImageClassifier::MakeGradientGeneratedImageClassifier() {
|
||||
}
|
||||
|
||||
DarkModeClassification DarkModeImageClassifier::Classify(
|
||||
Image* image,
|
||||
const PaintImage& paint_image,
|
||||
const FloatRect& src_rect,
|
||||
const FloatRect& dest_rect) {
|
||||
DarkModeImageClassificationCache* cache =
|
||||
DarkModeImageClassificationCache::GetInstance();
|
||||
PaintImage::Id image_id = image->paint_image_id();
|
||||
PaintImage::Id image_id = paint_image.stable_id();
|
||||
DarkModeClassification result = cache->Get(image_id, src_rect);
|
||||
if (result != DarkModeClassification::kNotClassified)
|
||||
return result;
|
||||
@ -177,7 +176,7 @@ DarkModeClassification DarkModeImageClassifier::Classify(
|
||||
return result;
|
||||
}
|
||||
|
||||
auto features_or_null = GetFeatures(image, src_rect);
|
||||
auto features_or_null = GetFeatures(paint_image, src_rect);
|
||||
if (!features_or_null) {
|
||||
// Do not cache this classification.
|
||||
return DarkModeClassification::kDoNotApplyFilter;
|
||||
@ -194,10 +193,30 @@ void DarkModeImageClassifier::Reset() {
|
||||
blocks_count_vertical_ = kBlocksCount1D;
|
||||
}
|
||||
|
||||
bool DarkModeImageClassifier::GetBitmap(const PaintImage& paint_image,
|
||||
const SkRect& src,
|
||||
SkBitmap* bitmap) {
|
||||
if (!src.width() || !src.height())
|
||||
return false;
|
||||
|
||||
SkRect dst = {0, 0, src.width(), src.height()};
|
||||
|
||||
if (!bitmap || !bitmap->tryAllocPixels(SkImageInfo::MakeN32(
|
||||
static_cast<int>(src.width()),
|
||||
static_cast<int>(src.height()), kPremul_SkAlphaType)))
|
||||
return false;
|
||||
|
||||
SkCanvas canvas(*bitmap);
|
||||
canvas.clear(SK_ColorTRANSPARENT);
|
||||
canvas.drawImageRect(paint_image.GetSkImage(), src, dst, nullptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
base::Optional<DarkModeImageClassifier::Features>
|
||||
DarkModeImageClassifier::GetFeatures(Image* image, const FloatRect& src_rect) {
|
||||
DarkModeImageClassifier::GetFeatures(const PaintImage& paint_image,
|
||||
const FloatRect& src_rect) {
|
||||
SkBitmap bitmap;
|
||||
if (!image->GetBitmap(src_rect, &bitmap))
|
||||
if (!GetBitmap(paint_image, src_rect, &bitmap))
|
||||
return base::nullopt;
|
||||
|
||||
if (pixels_to_sample_ > src_rect.Width() * src_rect.Height())
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
namespace blink {
|
||||
|
||||
class Image;
|
||||
|
||||
FORWARD_DECLARE_TEST(DarkModeImageClassifierTest, FeaturesAndClassification);
|
||||
FORWARD_DECLARE_TEST(DarkModeImageClassifierTest, Caching);
|
||||
FORWARD_DECLARE_TEST(DarkModeImageClassifierTest, BlocksCount);
|
||||
@ -46,7 +44,9 @@ class PLATFORM_EXPORT DarkModeImageClassifier {
|
||||
float background_ratio;
|
||||
};
|
||||
|
||||
DarkModeClassification Classify(Image* image,
|
||||
// Performance warning: |paint_image| will be synchronously decoded if this
|
||||
// function is called in blink main thread.
|
||||
DarkModeClassification Classify(const PaintImage& paint_image,
|
||||
const FloatRect& src_rect,
|
||||
const FloatRect& dest_rect);
|
||||
|
||||
@ -62,7 +62,11 @@ class PLATFORM_EXPORT DarkModeImageClassifier {
|
||||
private:
|
||||
DarkModeClassification ClassifyWithFeatures(const Features& features);
|
||||
DarkModeClassification ClassifyUsingDecisionTree(const Features& features);
|
||||
base::Optional<Features> GetFeatures(Image* image, const FloatRect& src_rect);
|
||||
bool GetBitmap(const PaintImage& paint_image,
|
||||
const SkRect& rect,
|
||||
SkBitmap* bitmap);
|
||||
base::Optional<Features> GetFeatures(const PaintImage& paint_image,
|
||||
const FloatRect& src_rect);
|
||||
void Reset();
|
||||
|
||||
enum class ColorMode { kColor = 0, kGrayscale = 1 };
|
||||
|
@ -63,7 +63,7 @@ TEST_F(DarkModeImageClassifierTest, FeaturesAndClassification) {
|
||||
image_classifier()->Reset();
|
||||
image = GetImage("/images/resources/grid-large.png");
|
||||
features = image_classifier()
|
||||
->GetFeatures(image.get(),
|
||||
->GetFeatures(image->PaintImageForCurrentFrame(),
|
||||
FloatRect(0, 0, image->width(), image->height()))
|
||||
.value();
|
||||
EXPECT_EQ(image_classifier()->ClassifyWithFeatures(features),
|
||||
@ -83,7 +83,7 @@ TEST_F(DarkModeImageClassifierTest, FeaturesAndClassification) {
|
||||
image_classifier()->Reset();
|
||||
image = GetImage("/images/resources/apng08-ref.png");
|
||||
features = image_classifier()
|
||||
->GetFeatures(image.get(),
|
||||
->GetFeatures(image->PaintImageForCurrentFrame(),
|
||||
FloatRect(0, 0, image->width(), image->height()))
|
||||
.value();
|
||||
EXPECT_EQ(image_classifier()->ClassifyWithFeatures(features),
|
||||
@ -103,7 +103,7 @@ TEST_F(DarkModeImageClassifierTest, FeaturesAndClassification) {
|
||||
image_classifier()->Reset();
|
||||
image = GetImage("/images/resources/twitter_favicon.ico");
|
||||
features = image_classifier()
|
||||
->GetFeatures(image.get(),
|
||||
->GetFeatures(image->PaintImageForCurrentFrame(),
|
||||
FloatRect(0, 0, image->width(), image->height()))
|
||||
.value();
|
||||
EXPECT_EQ(image_classifier()->ClassifyWithFeatures(features),
|
||||
@ -123,7 +123,7 @@ TEST_F(DarkModeImageClassifierTest, FeaturesAndClassification) {
|
||||
image_classifier()->Reset();
|
||||
image = GetImage("/images/resources/blue-wheel-srgb-color-profile.png");
|
||||
features = image_classifier()
|
||||
->GetFeatures(image.get(),
|
||||
->GetFeatures(image->PaintImageForCurrentFrame(),
|
||||
FloatRect(0, 0, image->width(), image->height()))
|
||||
.value();
|
||||
EXPECT_EQ(image_classifier()->ClassifyWithFeatures(features),
|
||||
@ -143,7 +143,7 @@ TEST_F(DarkModeImageClassifierTest, FeaturesAndClassification) {
|
||||
image_classifier()->Reset();
|
||||
image = GetImage("/images/resources/ycbcr-444-float.jpg");
|
||||
features = image_classifier()
|
||||
->GetFeatures(image.get(),
|
||||
->GetFeatures(image->PaintImageForCurrentFrame(),
|
||||
FloatRect(0, 0, image->width(), image->height()))
|
||||
.value();
|
||||
EXPECT_EQ(image_classifier()->ClassifyWithFeatures(features),
|
||||
@ -205,7 +205,7 @@ TEST_F(DarkModeImageClassifierTest, BlocksCount) {
|
||||
image_classifier()->blocks_count_horizontal_ = image->width() - 1;
|
||||
image_classifier()->blocks_count_vertical_ = image->height() - 1;
|
||||
features = image_classifier()
|
||||
->GetFeatures(image.get(),
|
||||
->GetFeatures(image->PaintImageForCurrentFrame(),
|
||||
FloatRect(0, 0, image->width(), image->height()))
|
||||
.value();
|
||||
EXPECT_EQ(image_classifier()->blocks_count_horizontal_, image->width() - 1);
|
||||
@ -216,7 +216,7 @@ TEST_F(DarkModeImageClassifierTest, BlocksCount) {
|
||||
image_classifier()->blocks_count_horizontal_ = image->width();
|
||||
image_classifier()->blocks_count_vertical_ = image->height();
|
||||
features = image_classifier()
|
||||
->GetFeatures(image.get(),
|
||||
->GetFeatures(image->PaintImageForCurrentFrame(),
|
||||
FloatRect(0, 0, image->width(), image->height()))
|
||||
.value();
|
||||
EXPECT_EQ(image_classifier()->blocks_count_horizontal_, image->width());
|
||||
@ -227,7 +227,7 @@ TEST_F(DarkModeImageClassifierTest, BlocksCount) {
|
||||
image_classifier()->blocks_count_horizontal_ = image->width() + 1;
|
||||
image_classifier()->blocks_count_vertical_ = image->height() + 1;
|
||||
features = image_classifier()
|
||||
->GetFeatures(image.get(),
|
||||
->GetFeatures(image->PaintImageForCurrentFrame(),
|
||||
FloatRect(0, 0, image->width(), image->height()))
|
||||
.value();
|
||||
EXPECT_EQ(image_classifier()->blocks_count_horizontal_,
|
||||
|
@ -876,8 +876,9 @@ void GraphicsContext::DrawImage(
|
||||
|
||||
// Do not classify the image if the element has any CSS filters.
|
||||
if (!has_filter_property) {
|
||||
dark_mode_filter_.ApplyToImageFlagsIfNeeded(src, dest, image, &image_flags,
|
||||
GetElementRoleForImage(image));
|
||||
dark_mode_filter_.ApplyToImageFlagsIfNeeded(
|
||||
src, dest, image->PaintImageForCurrentFrame(), &image_flags,
|
||||
GetElementRoleForImage(image));
|
||||
}
|
||||
|
||||
image->Draw(canvas_, image_flags, dest, src, should_respect_image_orientation,
|
||||
@ -915,9 +916,9 @@ void GraphicsContext::DrawImageRRect(
|
||||
image_flags.setFilterQuality(
|
||||
ComputeFilterQuality(image, dest.Rect(), src_rect));
|
||||
|
||||
dark_mode_filter_.ApplyToImageFlagsIfNeeded(src_rect, dest.Rect(), image,
|
||||
&image_flags,
|
||||
GetElementRoleForImage(image));
|
||||
dark_mode_filter_.ApplyToImageFlagsIfNeeded(
|
||||
src_rect, dest.Rect(), image->PaintImageForCurrentFrame(), &image_flags,
|
||||
GetElementRoleForImage(image));
|
||||
|
||||
bool use_shader = (visible_src == src_rect) &&
|
||||
(respect_orientation == kDoNotRespectImageOrientation ||
|
||||
|
@ -362,29 +362,6 @@ SkBitmap Image::AsSkBitmapForCurrentFrame(
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
bool Image::GetBitmap(const FloatRect& src_rect, SkBitmap* bitmap) {
|
||||
if (!src_rect.Width() || !src_rect.Height())
|
||||
return false;
|
||||
|
||||
SkScalar sx = SkFloatToScalar(src_rect.X());
|
||||
SkScalar sy = SkFloatToScalar(src_rect.Y());
|
||||
SkScalar sw = SkFloatToScalar(src_rect.Width());
|
||||
SkScalar sh = SkFloatToScalar(src_rect.Height());
|
||||
SkRect src = {sx, sy, sx + sw, sy + sh};
|
||||
SkRect dest = {0, 0, sw, sh};
|
||||
|
||||
if (!bitmap || !bitmap->tryAllocPixels(SkImageInfo::MakeN32(
|
||||
static_cast<int>(src_rect.Width()),
|
||||
static_cast<int>(src_rect.Height()), kPremul_SkAlphaType)))
|
||||
return false;
|
||||
|
||||
SkCanvas canvas(*bitmap);
|
||||
canvas.clear(SK_ColorTRANSPARENT);
|
||||
canvas.drawImageRect(PaintImageForCurrentFrame().GetSkImage(), src, dest,
|
||||
nullptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
FloatRect Image::CorrectSrcRectForImageOrientation(FloatSize image_size,
|
||||
FloatRect src_rect) const {
|
||||
ImageOrientation orientation = CurrentFrameOrientation();
|
||||
|
@ -260,14 +260,6 @@ class PLATFORM_EXPORT Image : public ThreadSafeRefCounted<Image> {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// This function returns true if it can create the bitmap of the
|
||||
// image using |src_rect| for the location and dimensions of the image.
|
||||
// For Bitmap and SVG (and any other type) images the implementation
|
||||
// of this function differs when it comes to the implementation of
|
||||
// PaintImageForCurrentFrame(). Once the PaintImage is available,
|
||||
// the method used to extract the bitmap is the same for any image.
|
||||
bool GetBitmap(const FloatRect& src_rect, SkBitmap* bitmap);
|
||||
|
||||
PaintImage::Id paint_image_id() const { return stable_image_id_; }
|
||||
|
||||
// Returns an SkBitmap that is a copy of the image's current frame.
|
||||
|
Reference in New Issue
Block a user