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