Commit c89cd88e authored by Aditya Keerthi's avatar Aditya Keerthi Committed by Commit Bot

Support memory pressure handler in ImageDecodingStore

ImageDecodingStore should be responsible for its own memory pressure
policy.

Bug: 926186
Change-Id: If89cef567afd99b4274b74c3148bd45ac2d4cc5c
Reviewed-on: https://chromium-review.googlesource.com/c/1440509Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarStephen Chenney <schenney@chromium.org>
Commit-Queue: Aditya Keerthi <adityakeerthi@google.com>
Cr-Commit-Position: refs/heads/master@{#627701}
parent 861591b9
......@@ -26,6 +26,7 @@
#include "third_party/blink/renderer/platform/graphics/image_decoding_store.h"
#include <memory>
#include "base/bind.h"
#include "third_party/blink/renderer/platform/graphics/image_frame_generator.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/wtf/threading.h"
......@@ -40,7 +41,10 @@ static const size_t kDefaultMaxTotalSizeOfHeapEntries = 32 * 1024 * 1024;
ImageDecodingStore::ImageDecodingStore()
: heap_limit_in_bytes_(kDefaultMaxTotalSizeOfHeapEntries),
heap_memory_usage_in_bytes_(0) {}
heap_memory_usage_in_bytes_(0),
memory_pressure_listener_(
base::BindRepeating(&ImageDecodingStore::OnMemoryPressure,
base::Unretained(this))) {}
ImageDecodingStore::~ImageDecodingStore() {
#if DCHECK_IS_ON()
......@@ -220,6 +224,18 @@ void ImageDecodingStore::Prune() {
}
}
void ImageDecodingStore::OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel level) {
switch (level) {
case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
break;
case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
Clear();
break;
}
}
template <class T, class U, class V>
void ImageDecodingStore::InsertCacheInternal(std::unique_ptr<T> cache_entry,
U* cache_map,
......
......@@ -32,6 +32,7 @@
#include "SkSize.h"
#include "SkTypes.h"
#include "base/macros.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/ptr_util.h"
#include "cc/paint/paint_image_generator.h"
#include "third_party/blink/renderer/platform/graphics/image_frame_generator.h"
......@@ -290,6 +291,10 @@ class PLATFORM_EXPORT ImageDecodingStore final {
void Prune();
// Called by the memory pressure listener when the memory pressure rises.
void OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel level);
// These helper methods are called while m_mutex is locked.
template <class T, class U, class V>
void InsertCacheInternal(std::unique_ptr<T> cache_entry,
......@@ -347,6 +352,9 @@ class PLATFORM_EXPORT ImageDecodingStore final {
size_t heap_limit_in_bytes_;
size_t heap_memory_usage_in_bytes_;
// A listener to global memory pressure events.
base::MemoryPressureListener memory_pressure_listener_;
// Protect concurrent access to these members:
// m_orderedCacheList
// m_decoderCacheMap and all CacheEntrys stored in it
......
......@@ -26,6 +26,7 @@
#include "third_party/blink/renderer/platform/graphics/image_decoding_store.h"
#include <memory>
#include "base/memory/memory_pressure_listener.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/graphics/image_frame_generator.h"
#include "third_party/blink/renderer/platform/graphics/test/mock_image_decoder.h"
......@@ -233,4 +234,28 @@ TEST_F(ImageDecodingStoreTest, MultipleClientsForSameGenerator) {
EXPECT_EQ(ImageDecodingStore::Instance().CacheEntries(), 0);
}
TEST_F(ImageDecodingStoreTest, OnMemoryPressure) {
std::unique_ptr<ImageDecoder> decoder = MockImageDecoder::Create(this);
decoder->SetSize(1, 1);
ImageDecodingStore::Instance().InsertDecoder(
generator_.get(), cc::PaintImage::kDefaultGeneratorClientId,
std::move(decoder));
EXPECT_EQ(1, ImageDecodingStore::Instance().CacheEntries());
EXPECT_EQ(4u, ImageDecodingStore::Instance().MemoryUsageInBytes());
base::MemoryPressureListener::SimulatePressureNotification(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, ImageDecodingStore::Instance().CacheEntries());
EXPECT_EQ(4u, ImageDecodingStore::Instance().MemoryUsageInBytes());
base::MemoryPressureListener::SimulatePressureNotification(
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, ImageDecodingStore::Instance().CacheEntries());
EXPECT_EQ(0u, ImageDecodingStore::Instance().MemoryUsageInBytes());
}
} // namespace blink
......@@ -124,10 +124,8 @@ void MemoryCoordinator::OnPurgeMemory() {
}
void MemoryCoordinator::ClearMemory() {
// Clear the image cache.
// TODO(tasak|bashi): Make ImageDecodingStore and FontCache be
// MemoryCoordinatorClients rather than clearing caches here.
ImageDecodingStore::Instance().Clear();
// TODO(tasak|bashi): Make FontCache a MemoryCoordinatorClient rather than
// clearing caches here.
FontGlobalContext::ClearMemory();
}
......
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