[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / cc / resources / resource_pool.h
blobc1db254e052e78627fdcb791aa01f528a69b1177
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
3 // found in the LICENSE file.
5 #ifndef CC_RESOURCES_RESOURCE_POOL_H_
6 #define CC_RESOURCES_RESOURCE_POOL_H_
8 #include <deque>
10 #include "base/containers/scoped_ptr_map.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/trace_event/memory_dump_provider.h"
13 #include "cc/base/cc_export.h"
14 #include "cc/base/scoped_ptr_deque.h"
15 #include "cc/output/renderer.h"
16 #include "cc/resources/resource.h"
17 #include "cc/resources/resource_format.h"
18 #include "cc/resources/scoped_resource.h"
20 namespace cc {
22 class CC_EXPORT ResourcePool : public base::trace_event::MemoryDumpProvider {
23 public:
24 static scoped_ptr<ResourcePool> Create(
25 ResourceProvider* resource_provider,
26 base::SingleThreadTaskRunner* task_runner) {
27 return make_scoped_ptr(
28 new ResourcePool(resource_provider, task_runner, 0 /* target */));
31 static scoped_ptr<ResourcePool> Create(
32 ResourceProvider* resource_provider,
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));
40 ~ResourcePool() override;
42 Resource* AcquireResource(const gfx::Size& size, ResourceFormat format);
43 Resource* TryAcquireResourceWithContentId(uint64 content_id);
44 void ReleaseResource(Resource* resource, uint64_t content_id);
46 void SetResourceUsageLimits(size_t max_memory_usage_bytes,
47 size_t max_resource_count);
49 void ReduceResourceUsage();
50 void CheckBusyResources();
52 size_t memory_usage_bytes() const { return in_use_memory_usage_bytes_; }
53 size_t resource_count() const { return in_use_resources_.size(); }
55 // Overridden from base::trace_event::MemoryDumpProvider:
56 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
57 base::trace_event::ProcessMemoryDump* pmd) override;
59 size_t GetTotalMemoryUsageForTesting() const {
60 return total_memory_usage_bytes_;
62 size_t GetTotalResourceCountForTesting() const {
63 return total_resource_count_;
65 size_t GetBusyResourceCountForTesting() const {
66 return busy_resources_.size();
68 void SetResourceExpirationDelayForTesting(base::TimeDelta delay) {
69 resource_expiration_delay_ = delay;
72 protected:
73 ResourcePool(ResourceProvider* resource_provider,
74 base::SingleThreadTaskRunner* task_runner,
75 GLenum target);
77 bool ResourceUsageTooHigh();
79 private:
80 class PoolResource : public ScopedResource {
81 public:
82 static scoped_ptr<PoolResource> Create(
83 ResourceProvider* resource_provider) {
84 return make_scoped_ptr(new PoolResource(resource_provider));
86 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
87 const ResourceProvider* resource_provider,
88 bool is_free) const;
90 uint64_t content_id() const { return content_id_; }
91 void set_content_id(uint64_t content_id) { content_id_ = content_id; }
93 base::TimeTicks last_usage() const { return last_usage_; }
94 void set_last_usage(base::TimeTicks time) { last_usage_ = time; }
96 private:
97 explicit PoolResource(ResourceProvider* resource_provider)
98 : ScopedResource(resource_provider), content_id_(0) {}
99 uint64_t content_id_;
100 base::TimeTicks last_usage_;
103 void DidFinishUsingResource(scoped_ptr<PoolResource> resource);
104 void DeleteResource(scoped_ptr<PoolResource> resource);
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;
113 ResourceProvider* resource_provider_;
114 const GLenum target_;
115 size_t max_memory_usage_bytes_;
116 size_t max_resource_count_;
117 size_t in_use_memory_usage_bytes_;
118 size_t total_memory_usage_bytes_;
119 size_t total_resource_count_;
121 using ResourceDeque = ScopedPtrDeque<PoolResource>;
122 ResourceDeque unused_resources_;
123 ResourceDeque busy_resources_;
125 using ResourceMap = base::ScopedPtrMap<ResourceId, scoped_ptr<PoolResource>>;
126 ResourceMap in_use_resources_;
128 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
129 bool evict_expired_resources_pending_;
130 base::TimeDelta resource_expiration_delay_;
132 base::WeakPtrFactory<ResourcePool> weak_ptr_factory_;
134 DISALLOW_COPY_AND_ASSIGN(ResourcePool);
137 } // namespace cc
139 #endif // CC_RESOURCES_RESOURCE_POOL_H_