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 #ifndef CC_RESOURCES_RESOURCE_POOL_H_ | 5 #ifndef CC_RESOURCES_RESOURCE_POOL_H_ |
6 #define CC_RESOURCES_RESOURCE_POOL_H_ | 6 #define CC_RESOURCES_RESOURCE_POOL_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/containers/scoped_ptr_map.h" | 10 #include "base/containers/scoped_ptr_map.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/trace_event/memory_dump_provider.h" | 12 #include "base/trace_event/memory_dump_provider.h" |
13 #include "cc/base/cc_export.h" | 13 #include "cc/base/cc_export.h" |
14 #include "cc/base/scoped_ptr_deque.h" | 14 #include "cc/base/scoped_ptr_deque.h" |
15 #include "cc/output/renderer.h" | 15 #include "cc/output/renderer.h" |
16 #include "cc/resources/resource.h" | 16 #include "cc/resources/resource.h" |
17 #include "cc/resources/resource_format.h" | 17 #include "cc/resources/resource_format.h" |
18 #include "cc/resources/scoped_resource.h" | 18 #include "cc/resources/scoped_resource.h" |
19 | 19 |
20 namespace cc { | 20 namespace cc { |
21 | 21 |
22 class CC_EXPORT ResourcePool : public base::trace_event::MemoryDumpProvider { | 22 class CC_EXPORT ResourcePool : public base::trace_event::MemoryDumpProvider { |
23 public: | 23 public: |
24 static scoped_ptr<ResourcePool> Create(ResourceProvider* resource_provider) { | 24 static scoped_ptr<ResourcePool> Create( |
25 return make_scoped_ptr(new ResourcePool(resource_provider)); | 25 ResourceProvider* resource_provider, |
| 26 base::SingleThreadTaskRunner* task_runner) { |
| 27 return make_scoped_ptr( |
| 28 new ResourcePool(resource_provider, task_runner, 0 /* target */)); |
26 } | 29 } |
27 | 30 |
28 static scoped_ptr<ResourcePool> Create(ResourceProvider* resource_provider, | 31 static scoped_ptr<ResourcePool> Create( |
29 GLenum target) { | 32 ResourceProvider* resource_provider, |
30 return make_scoped_ptr(new ResourcePool(resource_provider, target)); | 33 base::SingleThreadTaskRunner* task_runner, |
| 34 GLenum target) { |
| 35 DCHECK_NE(0u, target); |
| 36 return make_scoped_ptr( |
| 37 new ResourcePool(resource_provider, task_runner, target)); |
31 } | 38 } |
32 | 39 |
33 ~ResourcePool() override; | 40 ~ResourcePool() override; |
34 | 41 |
35 Resource* AcquireResource(const gfx::Size& size, ResourceFormat format); | 42 Resource* AcquireResource(const gfx::Size& size, ResourceFormat format); |
36 Resource* TryAcquireResourceWithContentId(uint64 content_id); | 43 Resource* TryAcquireResourceWithContentId(uint64 content_id); |
37 void ReleaseResource(Resource* resource, uint64_t content_id); | 44 void ReleaseResource(Resource* resource, uint64_t content_id); |
38 | 45 |
39 void SetResourceUsageLimits(size_t max_memory_usage_bytes, | 46 void SetResourceUsageLimits(size_t max_memory_usage_bytes, |
40 size_t max_unused_memory_usage_bytes, | |
41 size_t max_resource_count); | 47 size_t max_resource_count); |
42 | 48 |
43 void ReduceResourceUsage(); | 49 void ReduceResourceUsage(); |
44 void CheckBusyResources(); | 50 void CheckBusyResources(); |
45 | 51 |
46 size_t total_memory_usage_bytes() const { return memory_usage_bytes_; } | 52 size_t memory_usage_bytes() const { return in_use_memory_usage_bytes_; } |
47 size_t acquired_memory_usage_bytes() const { | 53 size_t resource_count() const { return in_use_resources_.size(); } |
48 return memory_usage_bytes_ - unused_memory_usage_bytes_; | |
49 } | |
50 size_t total_resource_count() const { return resource_count_; } | |
51 size_t acquired_resource_count() const { | |
52 return resource_count_ - unused_resources_.size(); | |
53 } | |
54 size_t busy_resource_count() const { return busy_resources_.size(); } | |
55 | 54 |
56 // Overridden from base::trace_event::MemoryDumpProvider: | 55 // Overridden from base::trace_event::MemoryDumpProvider: |
57 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, | 56 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |
58 base::trace_event::ProcessMemoryDump* pmd) override; | 57 base::trace_event::ProcessMemoryDump* pmd) override; |
59 | 58 |
| 59 size_t GetTotalMemoryUsageForTesting() const { |
| 60 return total_memory_usage_bytes_; |
| 61 } |
| 62 size_t GetTotalResourceCountForTesting() const { |
| 63 return total_resource_count_; |
| 64 } |
| 65 size_t GetBusyResourceCountForTesting() const { |
| 66 return busy_resources_.size(); |
| 67 } |
| 68 void SetResourceExpirationDelayForTesting(base::TimeDelta delay) { |
| 69 resource_expiration_delay_ = delay; |
| 70 } |
| 71 |
60 protected: | 72 protected: |
61 explicit ResourcePool(ResourceProvider* resource_provider); | 73 ResourcePool(ResourceProvider* resource_provider, |
62 ResourcePool(ResourceProvider* resource_provider, GLenum target); | 74 base::SingleThreadTaskRunner* task_runner, |
| 75 GLenum target); |
63 | 76 |
64 bool ResourceUsageTooHigh(); | 77 bool ResourceUsageTooHigh(); |
65 | 78 |
66 private: | 79 private: |
67 class PoolResource : public ScopedResource { | 80 class PoolResource : public ScopedResource { |
68 public: | 81 public: |
69 static scoped_ptr<PoolResource> Create( | 82 static scoped_ptr<PoolResource> Create( |
70 ResourceProvider* resource_provider) { | 83 ResourceProvider* resource_provider) { |
71 return make_scoped_ptr(new PoolResource(resource_provider)); | 84 return make_scoped_ptr(new PoolResource(resource_provider)); |
72 } | 85 } |
73 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 86 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
74 const ResourceProvider* resource_provider, | 87 const ResourceProvider* resource_provider, |
75 bool is_free) const; | 88 bool is_free) const; |
76 | 89 |
77 uint64_t content_id() const { return content_id_; } | 90 uint64_t content_id() const { return content_id_; } |
78 void set_content_id(uint64_t content_id) { content_id_ = content_id; } | 91 void set_content_id(uint64_t content_id) { content_id_ = content_id; } |
79 | 92 |
| 93 base::TimeTicks last_usage() const { return last_usage_; } |
| 94 void set_last_usage(base::TimeTicks time) { last_usage_ = time; } |
| 95 |
80 private: | 96 private: |
81 explicit PoolResource(ResourceProvider* resource_provider) | 97 explicit PoolResource(ResourceProvider* resource_provider) |
82 : ScopedResource(resource_provider), content_id_(0) {} | 98 : ScopedResource(resource_provider), content_id_(0) {} |
83 uint64_t content_id_; | 99 uint64_t content_id_; |
| 100 base::TimeTicks last_usage_; |
84 }; | 101 }; |
85 | 102 |
86 void DidFinishUsingResource(scoped_ptr<PoolResource> resource); | 103 void DidFinishUsingResource(scoped_ptr<PoolResource> resource); |
87 void DeleteResource(scoped_ptr<PoolResource> resource); | 104 void DeleteResource(scoped_ptr<PoolResource> resource); |
88 | 105 |
| 106 // Functions which manage periodic eviction of expired resources. |
| 107 void ScheduleEvictExpiredResourcesIn(base::TimeDelta time_from_now); |
| 108 void EvictExpiredResources(); |
| 109 void EvictResourcesNotUsedSince(base::TimeTicks time_limit); |
| 110 bool HasEvictableResources() const; |
| 111 base::TimeTicks GetUsageTimeForLRUResource() const; |
| 112 |
89 ResourceProvider* resource_provider_; | 113 ResourceProvider* resource_provider_; |
90 const GLenum target_; | 114 const GLenum target_; |
91 size_t max_memory_usage_bytes_; | 115 size_t max_memory_usage_bytes_; |
92 size_t max_unused_memory_usage_bytes_; | |
93 size_t max_resource_count_; | 116 size_t max_resource_count_; |
94 size_t memory_usage_bytes_; | 117 size_t in_use_memory_usage_bytes_; |
95 size_t unused_memory_usage_bytes_; | 118 size_t total_memory_usage_bytes_; |
96 size_t resource_count_; | 119 size_t total_resource_count_; |
97 | 120 |
98 using ResourceDeque = ScopedPtrDeque<PoolResource>; | 121 using ResourceDeque = ScopedPtrDeque<PoolResource>; |
99 ResourceDeque unused_resources_; | 122 ResourceDeque unused_resources_; |
100 ResourceDeque busy_resources_; | 123 ResourceDeque busy_resources_; |
101 | 124 |
102 using ResourceMap = base::ScopedPtrMap<ResourceId, scoped_ptr<PoolResource>>; | 125 using ResourceMap = base::ScopedPtrMap<ResourceId, scoped_ptr<PoolResource>>; |
103 ResourceMap in_use_resources_; | 126 ResourceMap in_use_resources_; |
104 | 127 |
| 128 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 129 bool evict_expired_resources_pending_; |
| 130 base::TimeDelta resource_expiration_delay_; |
| 131 |
| 132 base::WeakPtrFactory<ResourcePool> weak_ptr_factory_; |
| 133 |
105 DISALLOW_COPY_AND_ASSIGN(ResourcePool); | 134 DISALLOW_COPY_AND_ASSIGN(ResourcePool); |
106 }; | 135 }; |
107 | 136 |
108 } // namespace cc | 137 } // namespace cc |
109 | 138 |
110 #endif // CC_RESOURCES_RESOURCE_POOL_H_ | 139 #endif // CC_RESOURCES_RESOURCE_POOL_H_ |
OLD | NEW |