Commit 440d2141 authored by v.paturi's avatar v.paturi Committed by Commit Bot

Split DarkModeImageClassifier for Bitmap and SVG images.

The classifier for SVG images is going to have to work with
layout objects. So the code for the SVG classifier cannot
reside in the current DarkModeImageClassifier in platform
as the code in platform cannot depend on core.

A separate class is implemented in core for classification
of SVG images in dark mode. The current DarkModeImageClassifier
is renamed to a more appropriate DarkModeBitmapImageClassifier.

The SVG classifier itself is a work in progress and will be
submitted in a follow-up CL.

Bug: 949943
Change-Id: I135a357c333655c11e197599466c314d2f20facf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1564276
Commit-Queue: Varun Chowdhary Paturi <v.paturi@samsung.com>
Reviewed-by: default avatarStephen Chenney <schenney@chromium.org>
Cr-Commit-Position: refs/heads/master@{#652495}
parent 0380fdb9
......@@ -23,6 +23,8 @@ blink_core_sources("svg") {
"color_distance.cc",
"color_distance.h",
"gradient_attributes.h",
"graphics/dark_mode_svg_image_classifier.cc",
"graphics/dark_mode_svg_image_classifier.h",
"graphics/filters/svg_fe_image.cc",
"graphics/filters/svg_fe_image.h",
"graphics/filters/svg_filter_builder.cc",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/svg/graphics/dark_mode_svg_image_classifier.h"
namespace blink {
DarkModeSVGImageClassifier::DarkModeSVGImageClassifier() {}
DarkModeClassification DarkModeSVGImageClassifier::Classify(
SVGImage* image,
const FloatRect& src_rect) {
return DarkModeClassification::kApplyDarkModeFilter;
}
} // namespace blink
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_SVG_GRAPHICS_DARK_MODE_SVG_IMAGE_CLASSIFIER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_SVG_GRAPHICS_DARK_MODE_SVG_IMAGE_CLASSIFIER_H_
#include "third_party/blink/renderer/core/svg/graphics/svg_image.h"
#include "third_party/blink/renderer/platform/geometry/float_rect.h"
#include "third_party/blink/renderer/platform/graphics/graphics_types.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
namespace blink {
class CORE_EXPORT DarkModeSVGImageClassifier {
DISALLOW_NEW();
public:
DarkModeSVGImageClassifier();
~DarkModeSVGImageClassifier() = default;
DarkModeClassification Classify(SVGImage* image, const FloatRect& src_rect);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_SVG_GRAPHICS_DARK_MODE_SVG_IMAGE_CLASSIFIER_H_
......@@ -46,6 +46,7 @@
#include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/core/svg/animation/smil_time_container.h"
#include "third_party/blink/renderer/core/svg/graphics/dark_mode_svg_image_classifier.h"
#include "third_party/blink/renderer/core/svg/graphics/svg_image_chrome_client.h"
#include "third_party/blink/renderer/core/svg/svg_document_extensions.h"
#include "third_party/blink/renderer/core/svg/svg_fe_image_element.h"
......@@ -838,4 +839,10 @@ String SVGImage::FilenameExtension() const {
return "svg";
}
DarkModeClassification SVGImage::ClassifyImageForDarkMode(
const FloatRect& src_rect) {
DarkModeSVGImageClassifier dark_mode_svg_image_classifier;
return dark_mode_svg_image_classifier.Classify(this, src_rect);
}
} // namespace blink
......@@ -216,9 +216,7 @@ class CORE_EXPORT SVGImage final : public Image {
// filter should be applied based on the image's content and it's
// visibility on a dark background.
DarkModeClassification ClassifyImageForDarkMode(
const FloatRect& src_rect) override {
return DarkModeClassification::kApplyDarkModeFilter;
}
const FloatRect& src_rect) override;
class SVGImageLocalFrameClient;
......
......@@ -20,6 +20,7 @@
#include "third_party/blink/renderer/core/svg/graphics/svg_image_for_container.h"
#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/core/svg/graphics/dark_mode_svg_image_classifier.h"
#include "third_party/blink/renderer/platform/geometry/float_rect.h"
#include "third_party/blink/renderer/platform/geometry/float_size.h"
#include "third_party/skia/include/core/SkBitmap.h"
......@@ -69,4 +70,10 @@ PaintImage SVGImageForContainer::PaintImageForCurrentFrame() {
return builder.TakePaintImage();
}
DarkModeClassification SVGImageForContainer::ClassifyImageForDarkMode(
const FloatRect& src_rect) {
DarkModeSVGImageClassifier dark_mode_svg_image_classifier;
return dark_mode_svg_image_classifier.Classify(image_, src_rect);
}
} // namespace blink
......@@ -113,9 +113,7 @@ class SVGImageForContainer final : public Image {
// filter should be applied based on the image's content and it's
// visibility on a dark background.
DarkModeClassification ClassifyImageForDarkMode(
const FloatRect& src_rect) override {
return DarkModeClassification::kApplyDarkModeFilter;
}
const FloatRect& src_rect) override;
SVGImage* image_;
const FloatSize container_size_;
......
......@@ -888,10 +888,10 @@ jumbo_component("platform") {
"graphics/cpu/x86/webgl_image_conversion_sse.h",
"graphics/crossfade_generated_image.cc",
"graphics/crossfade_generated_image.h",
"graphics/dark_mode_bitmap_image_classifier.cc",
"graphics/dark_mode_bitmap_image_classifier.h",
"graphics/dark_mode_color_classifier.cc",
"graphics/dark_mode_color_classifier.h",
"graphics/dark_mode_image_classifier.cc",
"graphics/dark_mode_image_classifier.h",
"graphics/dark_mode_settings.h",
"graphics/darkmode/darkmode_classifier.cc",
"graphics/darkmode/darkmode_classifier.h",
......@@ -1729,7 +1729,7 @@ jumbo_source_set("blink_platform_unittests_sources") {
"graphics/compositing/paint_chunks_to_cc_layer_test.cc",
"graphics/compositor_element_id_test.cc",
"graphics/contiguous_container_test.cc",
"graphics/dark_mode_image_classifier_test.cc",
"graphics/dark_mode_bitmap_image_classifier_test.cc",
"graphics/decoding_image_generator_test.cc",
"graphics/deferred_image_decoder_test_wo_platform.cc",
"graphics/filters/fe_composite_test.cc",
......
......@@ -34,7 +34,7 @@
#include "base/metrics/histogram_macros.h"
#include "third_party/blink/renderer/platform/geometry/float_rect.h"
#include "third_party/blink/renderer/platform/graphics/bitmap_image_metrics.h"
#include "third_party/blink/renderer/platform/graphics/dark_mode_image_classifier.h"
#include "third_party/blink/renderer/platform/graphics/dark_mode_bitmap_image_classifier.h"
#include "third_party/blink/renderer/platform/graphics/deferred_image_decoder.h"
#include "third_party/blink/renderer/platform/graphics/image_observer.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_canvas.h"
......@@ -445,9 +445,8 @@ void BitmapImage::SetAnimationPolicy(ImageAnimationPolicy policy) {
DarkModeClassification BitmapImage::ClassifyImageForDarkMode(
const FloatRect& src_rect) {
DarkModeImageClassifier dark_mode_image_classifier;
return dark_mode_image_classifier.ClassifyBitmapImageForDarkMode(*this,
src_rect);
DarkModeBitmapImageClassifier dark_mode_bitmap_image_classifier;
return dark_mode_bitmap_image_classifier.Classify(*this, src_rect);
}
} // namespace blink
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/graphics/dark_mode_image_classifier.h"
#include "third_party/blink/renderer/platform/graphics/dark_mode_bitmap_image_classifier.h"
#include "base/rand_util.h"
#include "third_party/blink/renderer/platform/geometry/int_rect.h"
......@@ -36,18 +36,18 @@ const float kHighColorCountThreshold[2] = {1, 0.025635};
namespace blink {
DarkModeImageClassifier::DarkModeImageClassifier()
DarkModeBitmapImageClassifier::DarkModeBitmapImageClassifier()
: pixels_to_sample_(kPixelsToSample) {}
DarkModeClassification DarkModeImageClassifier::ClassifyBitmapImageForDarkMode(
DarkModeClassification DarkModeBitmapImageClassifier::Classify(
Image& image,
const FloatRect& src_rect) {
std::vector<SkColor> sampled_pixels;
if (src_rect.Width() < kMinImageSizeForClassification1D ||
src_rect.Height() < kMinImageSizeForClassification1D)
return DarkModeClassification::kApplyDarkModeFilter;
std::vector<float> features;
std::vector<SkColor> sampled_pixels;
if (!ComputeImageFeatures(image, src_rect, &features, &sampled_pixels)) {
// TODO(https://crbug.com/945434): Do not cache the classification when
// the correct resource is not loaded
......@@ -61,7 +61,7 @@ DarkModeClassification DarkModeImageClassifier::ClassifyBitmapImageForDarkMode(
// This function computes a single feature vector based on a sample set of image
// pixels. Please refer to |GetSamples| function for description of the sampling
// method, and |GetFeatures| function for description of the features.
bool DarkModeImageClassifier::ComputeImageFeatures(
bool DarkModeBitmapImageClassifier::ComputeImageFeatures(
Image& image,
const FloatRect& src_rect,
std::vector<float>* features,
......@@ -85,9 +85,9 @@ bool DarkModeImageClassifier::ComputeImageFeatures(
return true;
}
bool DarkModeImageClassifier::GetBitmap(Image& image,
const FloatRect& src_rect,
SkBitmap* bitmap) {
bool DarkModeBitmapImageClassifier::GetBitmap(Image& image,
const FloatRect& src_rect,
SkBitmap* bitmap) {
DCHECK(image.IsBitmapImage());
if (!src_rect.Width() || !src_rect.Height())
return false;
......@@ -111,10 +111,11 @@ bool DarkModeImageClassifier::GetBitmap(Image& image,
// Extracts sample pixels from the image. The image is separated into uniformly
// distributed blocks through its width and height, each block is sampled, and
// checked to see if it seems to be background or foreground.
void DarkModeImageClassifier::GetSamples(const SkBitmap& bitmap,
std::vector<SkColor>* sampled_pixels,
float* transparency_ratio,
float* background_ratio) {
void DarkModeBitmapImageClassifier::GetSamples(
const SkBitmap& bitmap,
std::vector<SkColor>* sampled_pixels,
float* transparency_ratio,
float* background_ratio) {
int pixels_per_block = pixels_to_sample_ / (kBlocksCount1D * kBlocksCount1D);
int transparent_pixels = 0;
......@@ -164,7 +165,7 @@ void DarkModeImageClassifier::GetSamples(const SkBitmap& bitmap,
// Selects samples at regular intervals from a block of the image.
// Returns the opaque sampled pixels, and the number of transparent
// sampled pixels.
void DarkModeImageClassifier::GetBlockSamples(
void DarkModeBitmapImageClassifier::GetBlockSamples(
const SkBitmap& bitmap,
const IntRect& block,
const int required_samples_count,
......@@ -206,7 +207,7 @@ void DarkModeImageClassifier::GetBlockSamples(
// possiblities. Color buckets are represented with 4 bits per color channel.
// 2: Ratio of transparent area to the whole image.
// 3: Ratio of the background area to the whole image.
void DarkModeImageClassifier::GetFeatures(
void DarkModeBitmapImageClassifier::GetFeatures(
const std::vector<SkColor>& sampled_pixels,
const float transparency_ratio,
const float background_ratio,
......@@ -238,7 +239,7 @@ void DarkModeImageClassifier::GetFeatures(
(*features)[3] = background_ratio;
}
float DarkModeImageClassifier::ComputeColorBucketsRatio(
float DarkModeBitmapImageClassifier::ComputeColorBucketsRatio(
const std::vector<SkColor>& sampled_pixels,
const ColorMode color_mode) {
std::set<unsigned> buckets;
......@@ -268,7 +269,8 @@ float DarkModeImageClassifier::ComputeColorBucketsRatio(
max_buckets[color_mode == ColorMode::kColor];
}
DarkModeClassification DarkModeImageClassifier::ClassifyImageUsingDecisionTree(
DarkModeClassification
DarkModeBitmapImageClassifier::ClassifyImageUsingDecisionTree(
const std::vector<float>& features) {
DCHECK_EQ(features.size(), 4u);
......@@ -289,7 +291,7 @@ DarkModeClassification DarkModeImageClassifier::ClassifyImageUsingDecisionTree(
return DarkModeClassification::kNotClassified;
}
DarkModeClassification DarkModeImageClassifier::ClassifyImage(
DarkModeClassification DarkModeBitmapImageClassifier::ClassifyImage(
const std::vector<float>& features) {
DCHECK_EQ(features.size(), 4u);
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_DARK_MODE_IMAGE_CLASSIFIER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_DARK_MODE_IMAGE_CLASSIFIER_H_
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_DARK_MODE_BITMAP_IMAGE_CLASSIFIER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_DARK_MODE_BITMAP_IMAGE_CLASSIFIER_H_
#include <vector>
......@@ -16,16 +16,14 @@ namespace blink {
class IntRect;
class PLATFORM_EXPORT DarkModeImageClassifier {
class PLATFORM_EXPORT DarkModeBitmapImageClassifier {
DISALLOW_NEW();
public:
DarkModeImageClassifier();
~DarkModeImageClassifier() = default;
DarkModeBitmapImageClassifier();
~DarkModeBitmapImageClassifier() = default;
DarkModeClassification ClassifyBitmapImageForDarkMode(
Image& image,
const FloatRect& src_rect);
DarkModeClassification Classify(Image& image, const FloatRect& src_rect);
bool ComputeImageFeaturesForTesting(Image& image,
std::vector<float>* features) {
......@@ -96,4 +94,4 @@ class PLATFORM_EXPORT DarkModeImageClassifier {
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_DARK_MODE_IMAGE_CLASSIFIER_H_
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_DARK_MODE_BITMAP_IMAGE_CLASSIFIER_H_
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/graphics/dark_mode_image_classifier.h"
#include "third_party/blink/renderer/platform/graphics/dark_mode_bitmap_image_classifier.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/graphics/bitmap_image.h"
......@@ -50,7 +50,7 @@ class FakeImageForCacheTest : public Image {
ImageDecodingMode) override {}
};
class DarkModeImageClassifierTest : public testing::Test {
class DarkModeBitmapImageClassifierTest : public testing::Test {
public:
// Loads the image from |file_name|, computes features vector into |features|,
// and returns the classification result.
......@@ -59,7 +59,7 @@ class DarkModeImageClassifierTest : public testing::Test {
SCOPED_TRACE(file_name);
scoped_refptr<BitmapImage> image = LoadImage(file_name);
classifier_.ComputeImageFeaturesForTesting(*image.get(), features);
DarkModeClassification result = classifier_.ClassifyBitmapImageForDarkMode(
DarkModeClassification result = classifier_.Classify(
*image.get(), FloatRect(0, 0, image->width(), image->height()));
return result == DarkModeClassification::kApplyDarkModeFilter;
}
......@@ -73,7 +73,7 @@ class DarkModeImageClassifierTest : public testing::Test {
}
}
DarkModeImageClassifier* classifier() { return &classifier_; }
DarkModeBitmapImageClassifier* classifier() { return &classifier_; }
protected:
scoped_refptr<BitmapImage> LoadImage(const std::string& file_name) {
......@@ -88,10 +88,10 @@ class DarkModeImageClassifierTest : public testing::Test {
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform_;
DarkModeImageClassifier classifier_;
DarkModeBitmapImageClassifier classifier_;
};
TEST_F(DarkModeImageClassifierTest, FeaturesAndClassification) {
TEST_F(DarkModeBitmapImageClassifierTest, FeaturesAndClassification) {
std::vector<float> features;
// Test Case 1:
......@@ -150,7 +150,7 @@ TEST_F(DarkModeImageClassifierTest, FeaturesAndClassification) {
AssertFeaturesEqual(features, {1.0f, 0.0151367f, 0.0f, 0.0f});
}
TEST_F(DarkModeImageClassifierTest, Caching) {
TEST_F(DarkModeBitmapImageClassifierTest, Caching) {
scoped_refptr<FakeImageForCacheTest> image = FakeImageForCacheTest::Create();
FloatRect src_rect1(0, 0, 50, 50);
FloatRect src_rect2(5, 20, 100, 100);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment