Commit c458c18b authored by jbbegue's avatar jbbegue Committed by Commit bot

Enable the snapshot cache on iPad when the tab switcher is enabled.

BUG=576269

Review URL: https://codereview.chromium.org/1587663002

Cr-Commit-Position: refs/heads/master@{#369418}
parent 87882ce3
...@@ -144,15 +144,17 @@ void ConvertAndSaveGreyImage( ...@@ -144,15 +144,17 @@ void ConvertAndSaveGreyImage(
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
propertyReleaser_SnapshotCache_.Init(self, [SnapshotCache class]); propertyReleaser_SnapshotCache_.Init(self, [SnapshotCache class]);
if (!IsIPadIdiom()) { // Always use the LRUCache when the tab switcher is enabled.
if (experimental_flags::IsLRUSnapshotCacheEnabled()) { if (experimental_flags::IsTabSwitcherEnabled() ||
lruCache_.reset( experimental_flags::IsLRUSnapshotCacheEnabled()) {
[[LRUCache alloc] initWithCacheSize:kLRUCacheMaxCapacity]); lruCache_.reset(
} else { [[LRUCache alloc] initWithCacheSize:kLRUCacheMaxCapacity]);
imageDictionary_.reset([[NSMutableDictionary alloc] } else {
initWithCapacity:kCacheInitialCapacity]); imageDictionary_.reset(
} [[NSMutableDictionary alloc] initWithCapacity:kCacheInitialCapacity]);
}
if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) {
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
addObserver:self addObserver:self
selector:@selector(handleLowMemory) selector:@selector(handleLowMemory)
...@@ -174,7 +176,7 @@ void ConvertAndSaveGreyImage( ...@@ -174,7 +176,7 @@ void ConvertAndSaveGreyImage(
} }
- (void)dealloc { - (void)dealloc {
if (!IsIPadIdiom()) { if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) {
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
removeObserver:self removeObserver:self
name:UIApplicationDidReceiveMemoryWarningNotification name:UIApplicationDidReceiveMemoryWarningNotification
...@@ -207,9 +209,10 @@ void ConvertAndSaveGreyImage( ...@@ -207,9 +209,10 @@ void ConvertAndSaveGreyImage(
callback:(void (^)(UIImage*))callback { callback:(void (^)(UIImage*))callback {
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
DCHECK(sessionID); DCHECK(sessionID);
// iPad does not cache images, so if there is no callback we can avoid an
// expensive read from storage. // Cache on iPad is enabled only when the tab switcher is enabled.
if (IsIPadIdiom() && !callback) if ((IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) &&
!callback)
return; return;
UIImage* img = nil; UIImage* img = nil;
...@@ -233,10 +236,9 @@ void ConvertAndSaveGreyImage( ...@@ -233,10 +236,9 @@ void ConvertAndSaveGreyImage(
[SnapshotCache imagePathForSessionID:sessionID]) retain]); [SnapshotCache imagePathForSessionID:sessionID]) retain]);
}), }),
base::BindBlock(^(base::scoped_nsobject<UIImage> image) { base::BindBlock(^(base::scoped_nsobject<UIImage> image) {
// The iPad tab switcher is currently using its own memory cache so the // Cache on iPad is enabled only when the tab switcher is enabled.
// image is not stored in memory here if running on iPad. if ((!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) &&
// The same logic is used on image writes (code below). image) {
if (!IsIPadIdiom() && image) {
if (lruCache_) if (lruCache_)
[lruCache_ setObject:image forKey:sessionID]; [lruCache_ setObject:image forKey:sessionID];
else else
...@@ -252,10 +254,8 @@ void ConvertAndSaveGreyImage( ...@@ -252,10 +254,8 @@ void ConvertAndSaveGreyImage(
if (!img || !sessionID) if (!img || !sessionID)
return; return;
// The iPad tab switcher is currently using its own memory cache so the image // Cache on iPad is enabled only when the tab switcher is enabled.
// is not stored in memory here if running on iPad. if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) {
// The same logic is used on image reads (code above).
if (!IsIPadIdiom()) {
if (lruCache_) if (lruCache_)
[lruCache_ setObject:img forKey:sessionID]; [lruCache_ setObject:img forKey:sessionID];
else else
...@@ -387,7 +387,7 @@ void ConvertAndSaveGreyImage( ...@@ -387,7 +387,7 @@ void ConvertAndSaveGreyImage(
} }
- (void)handleLowMemory { - (void)handleLowMemory {
DCHECK(!IsIPadIdiom()); DCHECK(!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
NSMutableDictionary* dictionary = NSMutableDictionary* dictionary =
[[NSMutableDictionary alloc] initWithCapacity:2]; [[NSMutableDictionary alloc] initWithCapacity:2];
...@@ -410,14 +410,14 @@ void ConvertAndSaveGreyImage( ...@@ -410,14 +410,14 @@ void ConvertAndSaveGreyImage(
} }
- (void)handleEnterBackground { - (void)handleEnterBackground {
DCHECK(!IsIPadIdiom()); DCHECK(!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
[imageDictionary_ removeAllObjects]; [imageDictionary_ removeAllObjects];
[lruCache_ removeAllObjects]; [lruCache_ removeAllObjects];
} }
- (void)handleBecomeActive { - (void)handleBecomeActive {
DCHECK(!IsIPadIdiom()); DCHECK(!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
for (NSString* sessionID in pinnedIDs_) for (NSString* sessionID in pinnedIDs_)
[self retrieveImageForSessionID:sessionID callback:nil]; [self retrieveImageForSessionID:sessionID callback:nil];
......
...@@ -216,7 +216,7 @@ class SnapshotCacheTest : public PlatformTest { ...@@ -216,7 +216,7 @@ class SnapshotCacheTest : public PlatformTest {
TEST_F(SnapshotCacheTest, Cache) { TEST_F(SnapshotCacheTest, Cache) {
// Don't run on tablets because color snapshots are not cached so this test // Don't run on tablets because color snapshots are not cached so this test
// can't compare the UIImage pointers directly. // can't compare the UIImage pointers directly.
if (IsIPadIdiom()) { if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) {
return; return;
} }
...@@ -368,13 +368,13 @@ TEST_F(SnapshotCacheTest, HandleLowMemory) { ...@@ -368,13 +368,13 @@ TEST_F(SnapshotCacheTest, HandleLowMemory) {
[set addObject:secondPinnedID]; [set addObject:secondPinnedID];
cache.pinnedIDs = set; cache.pinnedIDs = set;
if (!IsIPadIdiom()) if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory]; [cache handleLowMemory];
BOOL expectedValue = YES; BOOL expectedValue = YES;
if (IsIPadIdiom()) { if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled())
expectedValue = NO; expectedValue = NO;
}
EXPECT_EQ(expectedValue, [cache hasImageInMemory:firstPinnedID]); EXPECT_EQ(expectedValue, [cache hasImageInMemory:firstPinnedID]);
EXPECT_EQ(expectedValue, [cache hasImageInMemory:secondPinnedID]); EXPECT_EQ(expectedValue, [cache hasImageInMemory:secondPinnedID]);
...@@ -423,7 +423,7 @@ TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) { ...@@ -423,7 +423,7 @@ TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) {
// Remove color images from in-memory cache. // Remove color images from in-memory cache.
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
if (!IsIPadIdiom()) if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory]; [cache handleLowMemory];
// Request the creation of a grey image cache for all images. // Request the creation of a grey image cache for all images.
...@@ -465,7 +465,7 @@ TEST_F(SnapshotCacheTest, MostRecentGreyBlock) { ...@@ -465,7 +465,7 @@ TEST_F(SnapshotCacheTest, MostRecentGreyBlock) {
LoadColorImagesIntoCache(kNumImages, true); LoadColorImagesIntoCache(kNumImages, true);
// Make sure the color images are only on disk, to ensure the background // Make sure the color images are only on disk, to ensure the background
// thread is slow enough to queue up the requests. // thread is slow enough to queue up the requests.
if (!IsIPadIdiom()) if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory]; [cache handleLowMemory];
// Enable the grey image cache. // Enable the grey image cache.
...@@ -537,7 +537,7 @@ TEST_F(SnapshotCacheTest, SizeAndScalePreservation) { ...@@ -537,7 +537,7 @@ TEST_F(SnapshotCacheTest, SizeAndScalePreservation) {
NSString* const kSession = @"foo"; NSString* const kSession = @"foo";
[cache setImage:image withSessionID:kSession]; [cache setImage:image withSessionID:kSession];
FlushRunLoops(); // ensure the file is written to disk. FlushRunLoops(); // ensure the file is written to disk.
if (!IsIPadIdiom()) if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory]; [cache handleLowMemory];
// Retrive the image and have the callback verify the size and scale. // Retrive the image and have the callback verify the size and scale.
...@@ -574,7 +574,7 @@ TEST_F(SnapshotCacheTest, DeleteRetinaImages) { ...@@ -574,7 +574,7 @@ TEST_F(SnapshotCacheTest, DeleteRetinaImages) {
NSString* const kSession = @"foo"; NSString* const kSession = @"foo";
[cache setImage:image withSessionID:kSession]; [cache setImage:image withSessionID:kSession];
FlushRunLoops(); // ensure the file is written to disk. FlushRunLoops(); // ensure the file is written to disk.
if (!IsIPadIdiom()) if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory]; [cache handleLowMemory];
// Verify the file was writted with @2x in the file name. // Verify the file was writted with @2x in the file name.
......
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