OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "cc/tiles/software_image_decode_controller.h" | 5 #include "cc/tiles/software_image_decode_controller.h" |
6 | 6 |
7 #include <inttypes.h> | 7 #include <inttypes.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 namespace cc { | 31 namespace cc { |
32 namespace { | 32 namespace { |
33 | 33 |
34 // The largest single high quality image to try and process. Images above this | 34 // The largest single high quality image to try and process. Images above this |
35 // size will drop down to medium quality. | 35 // size will drop down to medium quality. |
36 const size_t kMaxHighQualityImageSizeBytes = 64 * 1024 * 1024; | 36 const size_t kMaxHighQualityImageSizeBytes = 64 * 1024 * 1024; |
37 | 37 |
38 // The number of entries to keep around in the cache. This limit can be breached | 38 // The number of entries to keep around in the cache. This limit can be breached |
39 // if more items are locked. That is, locked items ignore this limit. | 39 // if more items are locked. That is, locked items ignore this limit. |
40 const size_t kMaxItemsInCache = 1000; | 40 // Depending on the memory state of the system, we limit the amount of items |
| 41 // differently. |
| 42 const size_t kNormalMaxItemsInCache = 1000; |
| 43 const size_t kThrottledMaxItemsInCache = 100; |
| 44 const size_t kSuspendedMaxItemsInCache = 0; |
41 | 45 |
42 // If the size of the original sized image breaches kMemoryRatioToSubrect but we | 46 // If the size of the original sized image breaches kMemoryRatioToSubrect but we |
43 // don't need to scale the image, consider caching only the needed subrect. | 47 // don't need to scale the image, consider caching only the needed subrect. |
44 // The second part that much be true is that we cache only the needed subrect if | 48 // The second part that much be true is that we cache only the needed subrect if |
45 // the total size needed for the subrect is at most kMemoryRatioToSubrect * | 49 // the total size needed for the subrect is at most kMemoryRatioToSubrect * |
46 // (size needed for the full original image). | 50 // (size needed for the full original image). |
47 const size_t kMemoryThresholdToSubrect = 64 * 1024 * 1024; | 51 const size_t kMemoryThresholdToSubrect = 64 * 1024 * 1024; |
48 const float kMemoryRatioToSubrect = 0.5f; | 52 const float kMemoryRatioToSubrect = 0.5f; |
49 | 53 |
50 class AutoRemoveKeyFromTaskMap { | 54 class AutoRemoveKeyFromTaskMap { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 } | 170 } |
167 | 171 |
168 } // namespace | 172 } // namespace |
169 | 173 |
170 SoftwareImageDecodeController::SoftwareImageDecodeController( | 174 SoftwareImageDecodeController::SoftwareImageDecodeController( |
171 ResourceFormat format, | 175 ResourceFormat format, |
172 size_t locked_memory_limit_bytes) | 176 size_t locked_memory_limit_bytes) |
173 : decoded_images_(ImageMRUCache::NO_AUTO_EVICT), | 177 : decoded_images_(ImageMRUCache::NO_AUTO_EVICT), |
174 at_raster_decoded_images_(ImageMRUCache::NO_AUTO_EVICT), | 178 at_raster_decoded_images_(ImageMRUCache::NO_AUTO_EVICT), |
175 locked_images_budget_(locked_memory_limit_bytes), | 179 locked_images_budget_(locked_memory_limit_bytes), |
176 format_(format) { | 180 format_(format), |
| 181 max_items_in_cache_(kNormalMaxItemsInCache) { |
177 // In certain cases, ThreadTaskRunnerHandle isn't set (Android Webview). | 182 // In certain cases, ThreadTaskRunnerHandle isn't set (Android Webview). |
178 // Don't register a dump provider in these cases. | 183 // Don't register a dump provider in these cases. |
179 if (base::ThreadTaskRunnerHandle::IsSet()) { | 184 if (base::ThreadTaskRunnerHandle::IsSet()) { |
180 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 185 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
181 this, "cc::SoftwareImageDecodeController", | 186 this, "cc::SoftwareImageDecodeController", |
182 base::ThreadTaskRunnerHandle::Get()); | 187 base::ThreadTaskRunnerHandle::Get()); |
183 } | 188 } |
184 // Register this component with base::MemoryCoordinatorClientRegistry. | 189 // Register this component with base::MemoryCoordinatorClientRegistry. |
185 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); | 190 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); |
186 } | 191 } |
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 decoded_images_.Erase(image_it); | 745 decoded_images_.Erase(image_it); |
741 decoded_images_.Put(key, std::move(at_raster_image_it->second)); | 746 decoded_images_.Put(key, std::move(at_raster_image_it->second)); |
742 } | 747 } |
743 at_raster_decoded_images_.Erase(at_raster_image_it); | 748 at_raster_decoded_images_.Erase(at_raster_image_it); |
744 } | 749 } |
745 } | 750 } |
746 | 751 |
747 void SoftwareImageDecodeController::ReduceCacheUsage() { | 752 void SoftwareImageDecodeController::ReduceCacheUsage() { |
748 TRACE_EVENT0("cc", "SoftwareImageDecodeController::ReduceCacheUsage"); | 753 TRACE_EVENT0("cc", "SoftwareImageDecodeController::ReduceCacheUsage"); |
749 base::AutoLock lock(lock_); | 754 base::AutoLock lock(lock_); |
750 size_t num_to_remove = (decoded_images_.size() > kMaxItemsInCache) | 755 size_t num_to_remove = (decoded_images_.size() > max_items_in_cache_) |
751 ? (decoded_images_.size() - kMaxItemsInCache) | 756 ? (decoded_images_.size() - max_items_in_cache_) |
752 : 0; | 757 : 0; |
753 for (auto it = decoded_images_.rbegin(); | 758 for (auto it = decoded_images_.rbegin(); |
754 num_to_remove != 0 && it != decoded_images_.rend();) { | 759 num_to_remove != 0 && it != decoded_images_.rend();) { |
755 if (it->second->is_locked()) { | 760 if (it->second->is_locked()) { |
756 ++it; | 761 ++it; |
757 continue; | 762 continue; |
758 } | 763 } |
759 | 764 |
760 it = decoded_images_.Erase(it); | 765 it = decoded_images_.Erase(it); |
761 --num_to_remove; | 766 --num_to_remove; |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1078 current_usage_bytes_ = 0; | 1083 current_usage_bytes_ = 0; |
1079 } | 1084 } |
1080 | 1085 |
1081 size_t SoftwareImageDecodeController::MemoryBudget::GetCurrentUsageSafe() | 1086 size_t SoftwareImageDecodeController::MemoryBudget::GetCurrentUsageSafe() |
1082 const { | 1087 const { |
1083 return current_usage_bytes_.ValueOrDie(); | 1088 return current_usage_bytes_.ValueOrDie(); |
1084 } | 1089 } |
1085 | 1090 |
1086 void SoftwareImageDecodeController::OnMemoryStateChange( | 1091 void SoftwareImageDecodeController::OnMemoryStateChange( |
1087 base::MemoryState state) { | 1092 base::MemoryState state) { |
1088 switch (state) { | 1093 { |
1089 case base::MemoryState::NORMAL: | 1094 base::AutoLock hold(lock_); |
1090 // TODO(tasak): go back to normal state. | 1095 switch (state) { |
1091 break; | 1096 case base::MemoryState::NORMAL: |
1092 case base::MemoryState::THROTTLED: | 1097 max_items_in_cache_ = kNormalMaxItemsInCache; |
1093 // TODO(tasak): make the limits of this component's caches smaller to | 1098 break; |
1094 // save memory usage. | 1099 case base::MemoryState::THROTTLED: |
1095 break; | 1100 max_items_in_cache_ = kThrottledMaxItemsInCache; |
1096 case base::MemoryState::SUSPENDED: | 1101 break; |
1097 // TODO(tasak): free this component's caches as much as possible before | 1102 case base::MemoryState::SUSPENDED: |
1098 // suspending renderer. | 1103 max_items_in_cache_ = kSuspendedMaxItemsInCache; |
1099 break; | 1104 break; |
1100 case base::MemoryState::UNKNOWN: | 1105 case base::MemoryState::UNKNOWN: |
1101 // NOT_REACHED. | 1106 NOTREACHED(); |
1102 break; | 1107 return; |
| 1108 } |
1103 } | 1109 } |
| 1110 ReduceCacheUsage(); |
1104 } | 1111 } |
1105 | 1112 |
1106 } // namespace cc | 1113 } // namespace cc |
OLD | NEW |