Chromium Code Reviews| 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 // Depending on the memory state of the system, we limit the amount of items | |
| 41 // differently. | |
| 40 const size_t kMaxItemsInCache = 1000; | 42 const size_t kMaxItemsInCache = 1000; |
| 43 const size_t kThrottledMaxItemsInCache = 100; | |
| 44 const size_t kSuspectedMaxItemsInCache = 0; | |
|
ericrk
2016/09/23 20:18:52
I suspect you meant suspended :D
| |
| 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 689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 740 decoded_images_.Erase(image_it); | 744 decoded_images_.Erase(image_it); |
| 741 decoded_images_.Put(key, std::move(at_raster_image_it->second)); | 745 decoded_images_.Put(key, std::move(at_raster_image_it->second)); |
| 742 } | 746 } |
| 743 at_raster_decoded_images_.Erase(at_raster_image_it); | 747 at_raster_decoded_images_.Erase(at_raster_image_it); |
| 744 } | 748 } |
| 745 } | 749 } |
| 746 | 750 |
| 747 void SoftwareImageDecodeController::ReduceCacheUsage() { | 751 void SoftwareImageDecodeController::ReduceCacheUsage() { |
| 748 TRACE_EVENT0("cc", "SoftwareImageDecodeController::ReduceCacheUsage"); | 752 TRACE_EVENT0("cc", "SoftwareImageDecodeController::ReduceCacheUsage"); |
| 749 base::AutoLock lock(lock_); | 753 base::AutoLock lock(lock_); |
| 750 size_t num_to_remove = (decoded_images_.size() > kMaxItemsInCache) | 754 ReduceCacheUsageInternal(kMaxItemsInCache); |
| 751 ? (decoded_images_.size() - kMaxItemsInCache) | 755 } |
| 756 | |
| 757 void SoftwareImageDecodeController::ReduceCacheUsageInternal(size_t max_items) { | |
| 758 lock_.AssertAcquired(); | |
| 759 size_t num_to_remove = (decoded_images_.size() > max_items) | |
| 760 ? (decoded_images_.size() - max_items) | |
| 752 : 0; | 761 : 0; |
| 753 for (auto it = decoded_images_.rbegin(); | 762 for (auto it = decoded_images_.rbegin(); |
| 754 num_to_remove != 0 && it != decoded_images_.rend();) { | 763 num_to_remove != 0 && it != decoded_images_.rend();) { |
| 755 if (it->second->is_locked()) { | 764 if (it->second->is_locked()) { |
| 756 ++it; | 765 ++it; |
| 757 continue; | 766 continue; |
| 758 } | 767 } |
| 759 | 768 |
| 760 it = decoded_images_.Erase(it); | 769 it = decoded_images_.Erase(it); |
| 761 --num_to_remove; | 770 --num_to_remove; |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1078 current_usage_bytes_ = 0; | 1087 current_usage_bytes_ = 0; |
| 1079 } | 1088 } |
| 1080 | 1089 |
| 1081 size_t SoftwareImageDecodeController::MemoryBudget::GetCurrentUsageSafe() | 1090 size_t SoftwareImageDecodeController::MemoryBudget::GetCurrentUsageSafe() |
| 1082 const { | 1091 const { |
| 1083 return current_usage_bytes_.ValueOrDie(); | 1092 return current_usage_bytes_.ValueOrDie(); |
| 1084 } | 1093 } |
| 1085 | 1094 |
| 1086 void SoftwareImageDecodeController::OnMemoryStateChange( | 1095 void SoftwareImageDecodeController::OnMemoryStateChange( |
| 1087 base::MemoryState state) { | 1096 base::MemoryState state) { |
| 1097 size_t num_items_in_cache = kMaxItemsInCache; | |
| 1088 switch (state) { | 1098 switch (state) { |
| 1089 case base::MemoryState::NORMAL: | 1099 case base::MemoryState::NORMAL: |
| 1090 // TODO(tasak): go back to normal state. | 1100 // Nothing to do here. |
| 1091 break; | 1101 return; |
| 1092 case base::MemoryState::THROTTLED: | 1102 case base::MemoryState::THROTTLED: |
| 1093 // TODO(tasak): make the limits of this component's caches smaller to | 1103 num_items_in_cache = kThrottledMaxItemsInCache; |
|
ericrk
2016/09/23 20:18:52
One potential issue is that, if we continue operat
| |
| 1094 // save memory usage. | |
| 1095 break; | 1104 break; |
| 1096 case base::MemoryState::SUSPENDED: | 1105 case base::MemoryState::SUSPENDED: |
| 1097 // TODO(tasak): free this component's caches as much as possible before | 1106 num_items_in_cache = kSuspectedMaxItemsInCache; |
| 1098 // suspending renderer. | |
| 1099 break; | 1107 break; |
| 1100 case base::MemoryState::UNKNOWN: | 1108 case base::MemoryState::UNKNOWN: |
| 1101 // NOT_REACHED. | 1109 NOTREACHED(); |
| 1102 break; | 1110 return; |
| 1103 } | 1111 } |
| 1112 base::AutoLock hold(lock_); | |
| 1113 ReduceCacheUsageInternal(num_items_in_cache); | |
| 1104 } | 1114 } |
| 1105 | 1115 |
| 1106 } // namespace cc | 1116 } // namespace cc |
| OLD | NEW |