OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/resources/resource_pool.h" | 5 #include "cc/resources/resource_pool.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
14 #include "base/memory/memory_coordinator_client_registry.h" | 14 #include "base/memory/memory_coordinator_client_registry.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "base/trace_event/memory_dump_manager.h" | 17 #include "base/trace_event/memory_dump_manager.h" |
18 #include "cc/base/container_util.h" | 18 #include "cc/base/container_util.h" |
19 #include "cc/resources/resource_provider.h" | 19 #include "cc/resources/resource_provider.h" |
20 #include "cc/resources/resource_util.h" | 20 #include "cc/resources/resource_util.h" |
21 #include "cc/resources/scoped_resource.h" | 21 #include "cc/resources/scoped_resource.h" |
22 | 22 |
23 using base::trace_event::MemoryAllocatorDump; | 23 using base::trace_event::MemoryAllocatorDump; |
24 using base::trace_event::MemoryDumpLevelOfDetail; | 24 using base::trace_event::MemoryDumpLevelOfDetail; |
25 | 25 |
26 namespace cc { | 26 namespace cc { |
27 namespace { | |
28 bool ResourceMeetsSizeRequirements(const gfx::Size& requested_size, | |
29 const gfx::Size& actual_size) { | |
30 const float kReuseScaleThreshold = 2.0f; | |
danakj
2017/03/10 16:32:12
driveby constant suggestion of something <= sqrt(2
ericrk
2017/03/10 18:44:20
I like limiting to 2x memory, but decided to go wi
| |
31 | |
32 // Allocating new resources is expensive, and we'd like to re-use our | |
33 // existing ones within reason. Allow a larger resource to be used for a | |
34 // smaller request. | |
35 if (actual_size.width() < requested_size.width() || | |
36 actual_size.height() < requested_size.height()) | |
37 return false; | |
38 // But don't use a resource that is more than |kReuseScaleThreshold| times | |
39 // the requested size, as we want to free unnecessarily large resources. | |
40 if (actual_size.width() > requested_size.width() * kReuseScaleThreshold || | |
41 actual_size.height() > requested_size.height() * kReuseScaleThreshold) | |
42 return false; | |
43 | |
44 return true; | |
45 } | |
46 | |
47 } // namespace | |
48 | |
27 base::TimeDelta ResourcePool::kDefaultExpirationDelay = | 49 base::TimeDelta ResourcePool::kDefaultExpirationDelay = |
28 base::TimeDelta::FromSeconds(1); | 50 base::TimeDelta::FromSeconds(5); |
ericrk
2017/03/10 01:51:14
Any number here is just a heuristic. 1 seemed to b
| |
29 | 51 |
30 void ResourcePool::PoolResource::OnMemoryDump( | 52 void ResourcePool::PoolResource::OnMemoryDump( |
31 base::trace_event::ProcessMemoryDump* pmd, | 53 base::trace_event::ProcessMemoryDump* pmd, |
32 const ResourceProvider* resource_provider, | 54 const ResourceProvider* resource_provider, |
33 bool is_free) const { | 55 bool is_free) const { |
34 // Resource IDs are not process-unique, so log with the ResourceProvider's | 56 // Resource IDs are not process-unique, so log with the ResourceProvider's |
35 // unique id. | 57 // unique id. |
36 std::string parent_node = | 58 std::string parent_node = |
37 base::StringPrintf("cc/resource_memory/provider_%d/resource_%d", | 59 base::StringPrintf("cc/resource_memory/provider_%d/resource_%d", |
38 resource_provider->tracing_id(), id()); | 60 resource_provider->tracing_id(), id()); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 // Finding resources in |unused_resources_| from MRU to LRU direction, touches | 134 // Finding resources in |unused_resources_| from MRU to LRU direction, touches |
113 // LRU resources only if needed, which increases possibility of expiring more | 135 // LRU resources only if needed, which increases possibility of expiring more |
114 // LRU resources within kResourceExpirationDelayMs. | 136 // LRU resources within kResourceExpirationDelayMs. |
115 for (ResourceDeque::iterator it = unused_resources_.begin(); | 137 for (ResourceDeque::iterator it = unused_resources_.begin(); |
116 it != unused_resources_.end(); ++it) { | 138 it != unused_resources_.end(); ++it) { |
117 ScopedResource* resource = it->get(); | 139 ScopedResource* resource = it->get(); |
118 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 140 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
119 | 141 |
120 if (resource->format() != format) | 142 if (resource->format() != format) |
121 continue; | 143 continue; |
122 if (resource->size() != size) | 144 if (!ResourceMeetsSizeRequirements(size, resource->size())) |
123 continue; | 145 continue; |
124 if (resource->color_space() != color_space) | 146 if (resource->color_space() != color_space) |
125 continue; | 147 continue; |
126 | 148 |
127 // Transfer resource to |in_use_resources_|. | 149 // Transfer resource to |in_use_resources_|. |
128 in_use_resources_[resource->id()] = std::move(*it); | 150 in_use_resources_[resource->id()] = std::move(*it); |
129 unused_resources_.erase(it); | 151 unused_resources_.erase(it); |
130 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 152 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
131 resource->size(), resource->format()); | 153 resource->size(), resource->format()); |
132 return resource; | 154 return resource; |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 } | 501 } |
480 return true; | 502 return true; |
481 } | 503 } |
482 | 504 |
483 void ResourcePool::OnPurgeMemory() { | 505 void ResourcePool::OnPurgeMemory() { |
484 // Release all resources, regardless of how recently they were used. | 506 // Release all resources, regardless of how recently they were used. |
485 EvictResourcesNotUsedSince(base::TimeTicks() + base::TimeDelta::Max()); | 507 EvictResourcesNotUsedSince(base::TimeTicks() + base::TimeDelta::Max()); |
486 } | 508 } |
487 | 509 |
488 } // namespace cc | 510 } // namespace cc |
OLD | NEW |