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_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/trace_event/memory_dump_provider.h"
12 #include "cc/base/cc_export.h"
13 #include "cc/output/renderer.h"
14 #include "cc/resources/resource.h"
15 #include "cc/resources/resource_format.h"
20 class CC_EXPORT ResourcePool
: public base::trace_event::MemoryDumpProvider
{
22 static scoped_ptr
<ResourcePool
> Create(ResourceProvider
* resource_provider
) {
23 return make_scoped_ptr(new ResourcePool(resource_provider
));
26 static scoped_ptr
<ResourcePool
> Create(ResourceProvider
* resource_provider
,
28 return make_scoped_ptr(new ResourcePool(resource_provider
, target
));
31 ~ResourcePool() override
;
33 scoped_ptr
<ScopedResource
> AcquireResource(const gfx::Size
& size
,
34 ResourceFormat format
);
35 scoped_ptr
<ScopedResource
> TryAcquireResourceWithContentId(uint64 content_id
);
36 void ReleaseResource(scoped_ptr
<ScopedResource
> resource
,
39 void SetResourceUsageLimits(size_t max_memory_usage_bytes
,
40 size_t max_unused_memory_usage_bytes
,
41 size_t max_resource_count
);
43 void ReduceResourceUsage();
44 // This might block if |wait_if_needed| is true and one of the currently
45 // busy resources has a read lock fence that needs to be waited upon before
46 // it can be locked for write again.
47 void CheckBusyResources(bool wait_if_needed
);
49 size_t total_memory_usage_bytes() const { return memory_usage_bytes_
; }
50 size_t acquired_memory_usage_bytes() const {
51 return memory_usage_bytes_
- unused_memory_usage_bytes_
;
53 size_t total_resource_count() const { return resource_count_
; }
54 size_t acquired_resource_count() const {
55 return resource_count_
- unused_resources_
.size();
57 size_t busy_resource_count() const { return busy_resources_
.size(); }
59 // Overridden from base::trace_event::MemoryDumpProvider:
60 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs
& args
,
61 base::trace_event::ProcessMemoryDump
* pmd
) override
;
64 explicit ResourcePool(ResourceProvider
* resource_provider
);
65 ResourcePool(ResourceProvider
* resource_provider
, GLenum target
);
67 bool ResourceUsageTooHigh();
70 void DidFinishUsingResource(ScopedResource
* resource
, uint64_t content_id
);
71 void DeleteResource(ScopedResource
* resource
);
73 ResourceProvider
* resource_provider_
;
75 size_t max_memory_usage_bytes_
;
76 size_t max_unused_memory_usage_bytes_
;
77 size_t max_resource_count_
;
78 size_t memory_usage_bytes_
;
79 size_t unused_memory_usage_bytes_
;
80 size_t resource_count_
;
83 PoolResource(ScopedResource
* resource
, uint64_t content_id
)
84 : resource(resource
), content_id(content_id
) {}
85 void OnMemoryDump(base::trace_event::ProcessMemoryDump
* pmd
,
86 const ResourceProvider
* resource_provider
,
89 ScopedResource
* resource
;
92 typedef std::deque
<PoolResource
> ResourceList
;
93 ResourceList unused_resources_
;
94 ResourceList busy_resources_
;
96 DISALLOW_COPY_AND_ASSIGN(ResourcePool
);
101 #endif // CC_RESOURCES_RESOURCE_POOL_H_