Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: cc/resource_pool.cc

Issue 12471007: Part 8 of cc/ directory shuffles: resources (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « cc/resource_pool.h ('k') | cc/resource_provider.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "cc/resource_pool.h"
6
7 #include "cc/resource_provider.h"
8
9 namespace cc {
10
11 ResourcePool::Resource::Resource(cc::ResourceProvider* resource_provider,
12 const gfx::Size& size,
13 GLenum format)
14 : cc::Resource(resource_provider->CreateManagedResource(
15 size,
16 format,
17 ResourceProvider::TextureUsageAny),
18 size,
19 format),
20 resource_provider_(resource_provider) {
21 DCHECK(id());
22 }
23
24 ResourcePool::Resource::~Resource() {
25 DCHECK(id());
26 DCHECK(resource_provider_);
27 resource_provider_->DeleteResource(id());
28 }
29
30 ResourcePool::ResourcePool(ResourceProvider* resource_provider)
31 : resource_provider_(resource_provider),
32 max_memory_usage_bytes_(0),
33 memory_usage_bytes_(0) {
34 }
35
36 ResourcePool::~ResourcePool() {
37 SetMaxMemoryUsageBytes(0);
38 }
39
40 scoped_ptr<ResourcePool::Resource> ResourcePool::AcquireResource(
41 const gfx::Size& size, GLenum format) {
42 for (ResourceList::iterator it = resources_.begin();
43 it != resources_.end(); ++it) {
44 Resource* resource = *it;
45
46 // TODO(epenner): It would be nice to DCHECK that this
47 // doesn't happen two frames in a row for any resource
48 // in this pool.
49 if (!resource_provider_->CanLockForWrite(resource->id()))
50 continue;
51
52 if (resource->size() != size)
53 continue;
54 if (resource->format() != format)
55 continue;
56
57 resources_.erase(it);
58 return make_scoped_ptr(resource);
59 }
60
61 // Create new resource.
62 Resource* resource = new Resource(
63 resource_provider_, size, format);
64
65 // Extend all read locks on all resources until the resource is
66 // finished being used, such that we know when resources are
67 // truly safe to recycle.
68 resource_provider_->EnableReadLockFences(resource->id(), true);
69
70 memory_usage_bytes_ += resource->bytes();
71 return make_scoped_ptr(resource);
72 }
73
74 void ResourcePool::ReleaseResource(
75 scoped_ptr<ResourcePool::Resource> resource) {
76 if (memory_usage_bytes_ > max_memory_usage_bytes_) {
77 memory_usage_bytes_ -= resource->bytes();
78 return;
79 }
80
81 resources_.push_back(resource.release());
82 }
83
84 void ResourcePool::SetMaxMemoryUsageBytes(size_t max_memory_usage_bytes) {
85 max_memory_usage_bytes_ = max_memory_usage_bytes;
86
87 while (!resources_.empty()) {
88 if (memory_usage_bytes_ <= max_memory_usage_bytes_)
89 break;
90 Resource* resource = resources_.front();
91 resources_.pop_front();
92 memory_usage_bytes_ -= resource->bytes();
93 delete resource;
94 }
95 }
96
97 } // namespace cc
OLDNEW
« no previous file with comments | « cc/resource_pool.h ('k') | cc/resource_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698