OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/thumbnail/thumbnail_cache.h" | 5 #include "chrome/browser/android/thumbnail/thumbnail_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/android/application_status_listener.h" | |
11 #include "base/android/path_utils.h" | 12 #include "base/android/path_utils.h" |
12 #include "base/big_endian.h" | 13 #include "base/big_endian.h" |
13 #include "base/files/file.h" | 14 #include "base/files/file.h" |
14 #include "base/files/file_enumerator.h" | 15 #include "base/files/file_enumerator.h" |
15 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
17 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/threading/worker_pool.h" | 20 #include "base/threading/worker_pool.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 write_queue_max_size_(write_queue_max_size), | 125 write_queue_max_size_(write_queue_max_size), |
125 use_approximation_thumbnail_(use_approximation_thumbnail), | 126 use_approximation_thumbnail_(use_approximation_thumbnail), |
126 compression_tasks_count_(0), | 127 compression_tasks_count_(0), |
127 write_tasks_count_(0), | 128 write_tasks_count_(0), |
128 read_in_progress_(false), | 129 read_in_progress_(false), |
129 cache_(default_cache_size), | 130 cache_(default_cache_size), |
130 approximation_cache_(approximation_cache_size), | 131 approximation_cache_(approximation_cache_size), |
131 ui_resource_provider_(NULL), | 132 ui_resource_provider_(NULL), |
132 weak_factory_(this) { | 133 weak_factory_(this) { |
133 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 134 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
135 memory_pressure_.reset(new base::MemoryPressureListener( | |
136 base::Bind(&ThumbnailCache::OnMemoryPressure, base::Unretained(this)))); | |
134 } | 137 } |
135 | 138 |
136 ThumbnailCache::~ThumbnailCache() { | 139 ThumbnailCache::~ThumbnailCache() { |
137 SetUIResourceProvider(NULL); | 140 SetUIResourceProvider(NULL); |
138 } | 141 } |
139 | 142 |
140 void ThumbnailCache::SetUIResourceProvider( | 143 void ThumbnailCache::SetUIResourceProvider( |
141 ui::UIResourceProvider* ui_resource_provider) { | 144 ui::UIResourceProvider* ui_resource_provider) { |
142 if (ui_resource_provider_ == ui_resource_provider) | 145 if (ui_resource_provider_ == ui_resource_provider) |
143 return; | 146 return; |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
438 } | 441 } |
439 | 442 |
440 void ThumbnailCache::RemoveFromReadQueue(TabId tab_id) { | 443 void ThumbnailCache::RemoveFromReadQueue(TabId tab_id) { |
441 TabIdList::iterator read_iter = | 444 TabIdList::iterator read_iter = |
442 std::find(read_queue_.begin(), read_queue_.end(), tab_id); | 445 std::find(read_queue_.begin(), read_queue_.end(), tab_id); |
443 if (read_iter != read_queue_.end()) | 446 if (read_iter != read_queue_.end()) |
444 read_queue_.erase(read_iter); | 447 read_queue_.erase(read_iter); |
445 } | 448 } |
446 | 449 |
447 void ThumbnailCache::OnUIResourcesWereEvicted() { | 450 void ThumbnailCache::OnUIResourcesWereEvicted() { |
448 cache_.Clear(); | 451 TabId last_tab = visible_ids_.empty() ? 0 : visible_ids_.front(); |
David Trainor- moved to gerrit
2016/08/16 02:47:29
IIRC think 0 is a valid id. -1?
no sievers
2016/08/26 19:00:20
Done.
| |
449 approximation_cache_.Clear(); | 452 if (!last_tab) { |
453 cache_.Clear(); | |
454 approximation_cache_.Clear(); | |
455 } else { | |
456 std::unique_ptr<Thumbnail> thumbnail = cache_.Remove(last_tab); | |
457 cache_.Clear(); | |
458 std::unique_ptr<Thumbnail> approximation = | |
459 approximation_cache_.Remove(last_tab); | |
460 approximation_cache_.Clear(); | |
461 | |
462 // Keep the thumbnail for app resume if it wasn't uploaded yet. | |
463 if (thumbnail.get() && !thumbnail->ui_resource_id()) | |
464 cache_.Put(last_tab, std::move(thumbnail)); | |
465 if (approximation.get() && !approximation->ui_resource_id()) | |
466 approximation_cache_.Put(last_tab, std::move(approximation)); | |
467 } | |
450 } | 468 } |
451 | 469 |
452 void ThumbnailCache::InvalidateCachedThumbnail(Thumbnail* thumbnail) { | 470 void ThumbnailCache::InvalidateCachedThumbnail(Thumbnail* thumbnail) { |
453 DCHECK(thumbnail); | 471 DCHECK(thumbnail); |
454 TabId tab_id = thumbnail->tab_id(); | 472 TabId tab_id = thumbnail->tab_id(); |
455 cc::UIResourceId uid = thumbnail->ui_resource_id(); | 473 cc::UIResourceId uid = thumbnail->ui_resource_id(); |
456 | 474 |
457 Thumbnail* cached_thumbnail = cache_.Get(tab_id); | 475 Thumbnail* cached_thumbnail = cache_.Get(tab_id); |
458 if (cached_thumbnail && cached_thumbnail->ui_resource_id() == uid) | 476 if (cached_thumbnail && cached_thumbnail->ui_resource_id() == uid) |
459 cache_.Remove(tab_id); | 477 cache_.Remove(tab_id); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
606 if (!compressed_data) { | 624 if (!compressed_data) { |
607 RemoveOnMatchedTimeStamp(tab_id, time_stamp); | 625 RemoveOnMatchedTimeStamp(tab_id, time_stamp); |
608 return; | 626 return; |
609 } | 627 } |
610 | 628 |
611 Thumbnail* thumbnail = cache_.Get(tab_id); | 629 Thumbnail* thumbnail = cache_.Get(tab_id); |
612 if (thumbnail) { | 630 if (thumbnail) { |
613 if (thumbnail->time_stamp() != time_stamp) | 631 if (thumbnail->time_stamp() != time_stamp) |
614 return; | 632 return; |
615 thumbnail->SetCompressedBitmap(compressed_data, content_size); | 633 thumbnail->SetCompressedBitmap(compressed_data, content_size); |
616 thumbnail->CreateUIResource(); | 634 // Don't upload the texture if we are being paused/stopped because |
635 // the context will go away anyways. | |
636 if (base::android::ApplicationStatusListener::GetState() == | |
637 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | |
638 thumbnail->CreateUIResource(); | |
639 } | |
617 } | 640 } |
618 WriteThumbnailIfNecessary(tab_id, std::move(compressed_data), scale, | 641 WriteThumbnailIfNecessary(tab_id, std::move(compressed_data), scale, |
619 content_size); | 642 content_size); |
620 } | 643 } |
621 | 644 |
622 namespace { | 645 namespace { |
623 | 646 |
624 bool ReadFromFile(base::File& file, | 647 bool ReadFromFile(base::File& file, |
625 gfx::Size* out_content_size, | 648 gfx::Size* out_content_size, |
626 float* out_scale, | 649 float* out_scale, |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
902 dst_bitmap.eraseColor(0); | 925 dst_bitmap.eraseColor(0); |
903 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); | 926 SkAutoLockPixels dst_bitmap_lock(dst_bitmap); |
904 | 927 |
905 SkCanvas canvas(dst_bitmap); | 928 SkCanvas canvas(dst_bitmap); |
906 canvas.scale(new_scale, new_scale); | 929 canvas.scale(new_scale, new_scale); |
907 canvas.drawBitmap(bitmap, 0, 0, NULL); | 930 canvas.drawBitmap(bitmap, 0, 0, NULL); |
908 dst_bitmap.setImmutable(); | 931 dst_bitmap.setImmutable(); |
909 | 932 |
910 return std::make_pair(dst_bitmap, new_scale * scale); | 933 return std::make_pair(dst_bitmap, new_scale * scale); |
911 } | 934 } |
935 | |
936 void ThumbnailCache::OnMemoryPressure( | |
937 base::MemoryPressureListener::MemoryPressureLevel level) { | |
938 if (level == base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { | |
939 cache_.Clear(); | |
940 approximation_cache_.Clear(); | |
941 } | |
942 } | |
OLD | NEW |