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 kReuseThreshold = 2.0f; | |
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 |kReuseThreshold| times the | |
39 // requested pixel area, as we want to free unnecessarily large resources. | |
40 float actual_area = actual_size.GetArea(); | |
danakj
2017/03/10 18:55:40
This will crash if it overflows (GetArea uses safe
ericrk
2017/03/10 20:04:07
I'm assuming that this can't be a problem - the si
| |
41 float requested_area = requested_size.GetArea(); | |
42 if (actual_area / requested_area > kReuseThreshold) | |
43 return false; | |
44 | |
45 return true; | |
46 } | |
47 | |
48 } // namespace | |
49 | |
27 base::TimeDelta ResourcePool::kDefaultExpirationDelay = | 50 base::TimeDelta ResourcePool::kDefaultExpirationDelay = |
28 base::TimeDelta::FromSeconds(1); | 51 base::TimeDelta::FromSeconds(5); |
29 | 52 |
30 void ResourcePool::PoolResource::OnMemoryDump( | 53 void ResourcePool::PoolResource::OnMemoryDump( |
31 base::trace_event::ProcessMemoryDump* pmd, | 54 base::trace_event::ProcessMemoryDump* pmd, |
32 const ResourceProvider* resource_provider, | 55 const ResourceProvider* resource_provider, |
33 bool is_free) const { | 56 bool is_free) const { |
34 // Resource IDs are not process-unique, so log with the ResourceProvider's | 57 // Resource IDs are not process-unique, so log with the ResourceProvider's |
35 // unique id. | 58 // unique id. |
36 std::string parent_node = | 59 std::string parent_node = |
37 base::StringPrintf("cc/resource_memory/provider_%d/resource_%d", | 60 base::StringPrintf("cc/resource_memory/provider_%d/resource_%d", |
38 resource_provider->tracing_id(), id()); | 61 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 | 135 // Finding resources in |unused_resources_| from MRU to LRU direction, touches |
113 // LRU resources only if needed, which increases possibility of expiring more | 136 // LRU resources only if needed, which increases possibility of expiring more |
114 // LRU resources within kResourceExpirationDelayMs. | 137 // LRU resources within kResourceExpirationDelayMs. |
115 for (ResourceDeque::iterator it = unused_resources_.begin(); | 138 for (ResourceDeque::iterator it = unused_resources_.begin(); |
116 it != unused_resources_.end(); ++it) { | 139 it != unused_resources_.end(); ++it) { |
117 ScopedResource* resource = it->get(); | 140 ScopedResource* resource = it->get(); |
118 DCHECK(resource_provider_->CanLockForWrite(resource->id())); | 141 DCHECK(resource_provider_->CanLockForWrite(resource->id())); |
119 | 142 |
120 if (resource->format() != format) | 143 if (resource->format() != format) |
121 continue; | 144 continue; |
122 if (resource->size() != size) | 145 if (!ResourceMeetsSizeRequirements(size, resource->size())) |
123 continue; | 146 continue; |
124 if (resource->color_space() != color_space) | 147 if (resource->color_space() != color_space) |
125 continue; | 148 continue; |
126 | 149 |
127 // Transfer resource to |in_use_resources_|. | 150 // Transfer resource to |in_use_resources_|. |
128 in_use_resources_[resource->id()] = std::move(*it); | 151 in_use_resources_[resource->id()] = std::move(*it); |
129 unused_resources_.erase(it); | 152 unused_resources_.erase(it); |
130 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( | 153 in_use_memory_usage_bytes_ += ResourceUtil::UncheckedSizeInBytes<size_t>( |
131 resource->size(), resource->format()); | 154 resource->size(), resource->format()); |
132 return resource; | 155 return resource; |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 } | 502 } |
480 return true; | 503 return true; |
481 } | 504 } |
482 | 505 |
483 void ResourcePool::OnPurgeMemory() { | 506 void ResourcePool::OnPurgeMemory() { |
484 // Release all resources, regardless of how recently they were used. | 507 // Release all resources, regardless of how recently they were used. |
485 EvictResourcesNotUsedSince(base::TimeTicks() + base::TimeDelta::Max()); | 508 EvictResourcesNotUsedSince(base::TimeTicks() + base::TimeDelta::Max()); |
486 } | 509 } |
487 | 510 |
488 } // namespace cc | 511 } // namespace cc |
OLD | NEW |