Setup a experiment to enable background tracing.
[chromium-blink-merge.git] / cc / resources / resource_pool.h
blob53f4135e4d084482e8606d8333c75c97c88a9590
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/memory/scoped_ptr.h"
11 #include "cc/base/cc_export.h"
12 #include "cc/output/renderer.h"
13 #include "cc/resources/resource.h"
14 #include "cc/resources/resource_format.h"
16 namespace cc {
17 class ScopedResource;
19 class CC_EXPORT ResourcePool {
20 public:
21 static scoped_ptr<ResourcePool> Create(ResourceProvider* resource_provider,
22 GLenum target) {
23 return make_scoped_ptr(new ResourcePool(resource_provider, target));
26 virtual ~ResourcePool();
28 scoped_ptr<ScopedResource> AcquireResource(const gfx::Size& size,
29 ResourceFormat format);
30 scoped_ptr<ScopedResource> TryAcquireResourceWithContentId(uint64 content_id);
31 void ReleaseResource(scoped_ptr<ScopedResource> resource,
32 uint64_t content_id);
34 void SetResourceUsageLimits(size_t max_memory_usage_bytes,
35 size_t max_unused_memory_usage_bytes,
36 size_t max_resource_count);
38 void ReduceResourceUsage();
39 // This might block if |wait_if_needed| is true and one of the currently
40 // busy resources has a read lock fence that needs to be waited upon before
41 // it can be locked for write again.
42 void CheckBusyResources(bool wait_if_needed);
44 size_t total_memory_usage_bytes() const { return memory_usage_bytes_; }
45 size_t acquired_memory_usage_bytes() const {
46 return memory_usage_bytes_ - unused_memory_usage_bytes_;
48 size_t total_resource_count() const { return resource_count_; }
49 size_t acquired_resource_count() const {
50 return resource_count_ - unused_resources_.size();
52 size_t busy_resource_count() const { return busy_resources_.size(); }
54 protected:
55 ResourcePool(ResourceProvider* resource_provider, GLenum target);
57 bool ResourceUsageTooHigh();
59 private:
60 void DidFinishUsingResource(ScopedResource* resource, uint64_t content_id);
62 ResourceProvider* resource_provider_;
63 const GLenum target_;
64 size_t max_memory_usage_bytes_;
65 size_t max_unused_memory_usage_bytes_;
66 size_t max_resource_count_;
67 size_t memory_usage_bytes_;
68 size_t unused_memory_usage_bytes_;
69 size_t resource_count_;
71 struct PoolResource {
72 PoolResource(ScopedResource* resource, uint64_t content_id)
73 : resource(resource), content_id(content_id) {}
74 ScopedResource* resource;
75 uint64_t content_id;
77 typedef std::deque<PoolResource> ResourceList;
78 ResourceList unused_resources_;
79 ResourceList busy_resources_;
81 DISALLOW_COPY_AND_ASSIGN(ResourcePool);
84 } // namespace cc
86 #endif // CC_RESOURCES_RESOURCE_POOL_H_