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 #include "cc/resources/prioritized_resource.h"
9 #include "cc/resources/platform_color.h"
10 #include "cc/resources/prioritized_resource_manager.h"
11 #include "cc/resources/priority_calculator.h"
12 #include "cc/trees/proxy.h"
16 PrioritizedResource::PrioritizedResource(PrioritizedResourceManager* manager,
17 const gfx::Size& size,
18 ResourceFormat format)
22 contents_swizzled_(false),
23 priority_(PriorityCalculator::LowestPriority()),
24 is_above_priority_cutoff_(false),
25 is_self_managed_(false),
28 bytes_ = Resource::MemorySizeBytes(size, format);
30 manager->RegisterTexture(this);
33 PrioritizedResource::~PrioritizedResource() {
35 manager_->UnregisterTexture(this);
38 void PrioritizedResource::SetTextureManager(
39 PrioritizedResourceManager* manager) {
40 if (manager_ == manager)
43 manager_->UnregisterTexture(this);
45 manager->RegisterTexture(this);
48 void PrioritizedResource::SetDimensions(const gfx::Size& size,
49 ResourceFormat format) {
50 if (format_ != format || size_ != size) {
51 is_above_priority_cutoff_ = false;
54 bytes_ = Resource::MemorySizeBytes(size, format);
55 DCHECK(manager_ || !backing_);
57 manager_->ReturnBackingTexture(this);
61 bool PrioritizedResource::RequestLate() {
64 return manager_->RequestLate(this);
67 bool PrioritizedResource::BackingResourceWasEvicted() const {
68 return backing_ ? backing_->ResourceHasBeenDeleted() : false;
71 void PrioritizedResource::AcquireBackingTexture(
72 ResourceProvider* resource_provider) {
73 DCHECK(is_above_priority_cutoff_);
74 if (is_above_priority_cutoff_)
75 manager_->AcquireBackingTextureIfNeeded(this, resource_provider);
78 void PrioritizedResource::SetPixels(ResourceProvider* resource_provider,
80 const gfx::Rect& image_rect,
81 const gfx::Rect& source_rect,
82 const gfx::Vector2d& dest_offset) {
83 DCHECK(is_above_priority_cutoff_);
84 if (is_above_priority_cutoff_)
85 AcquireBackingTexture(resource_provider);
87 resource_provider->SetPixels(
88 resource_id(), image, image_rect, source_rect, dest_offset);
90 // The component order may be bgra if we uploaded bgra pixels to rgba
91 // texture. Mark contents as swizzled if image component order is
92 // different than texture format.
93 contents_swizzled_ = !PlatformColor::SameComponentOrder(format_);
96 void PrioritizedResource::Link(Backing* backing) {
98 DCHECK(!backing->owner_);
102 backing_->owner_ = this;
105 void PrioritizedResource::Unlink() {
107 DCHECK(backing_->owner_ == this);
109 backing_->owner_ = NULL;
113 void PrioritizedResource::SetToSelfManagedMemoryPlaceholder(size_t bytes) {
114 SetDimensions(gfx::Size(), RGBA_8888);
115 set_is_self_managed(true);
119 PrioritizedResource::Backing::Backing(unsigned id,
120 ResourceProvider* resource_provider,
121 const gfx::Size& size,
122 ResourceFormat format)
123 : Resource(id, size, format),
125 priority_at_last_priority_update_(PriorityCalculator::LowestPriority()),
126 was_above_priority_cutoff_at_last_priority_update_(false),
127 in_drawing_impl_tree_(false),
128 in_parent_compositor_(false),
130 resource_has_been_deleted_(false) {}
132 resource_has_been_deleted_(false),
133 resource_provider_(resource_provider) {}
136 PrioritizedResource::Backing::~Backing() {
138 DCHECK(resource_has_been_deleted_);
141 void PrioritizedResource::Backing::DeleteResource(
142 ResourceProvider* resource_provider) {
143 DCHECK(!proxy() || proxy()->IsImplThread());
144 DCHECK(!resource_has_been_deleted_);
146 DCHECK(resource_provider == resource_provider_);
149 resource_provider->DeleteResource(id());
151 resource_has_been_deleted_ = true;
154 bool PrioritizedResource::Backing::ResourceHasBeenDeleted() const {
155 DCHECK(!proxy() || proxy()->IsImplThread());
156 return resource_has_been_deleted_;
159 bool PrioritizedResource::Backing::CanBeRecycledIfNotInExternalUse() const {
160 DCHECK(!proxy() || proxy()->IsImplThread());
161 return !was_above_priority_cutoff_at_last_priority_update_ &&
162 !in_drawing_impl_tree_;
165 void PrioritizedResource::Backing::UpdatePriority() {
167 (proxy()->IsImplThread() && proxy()->IsMainThreadBlocked()));
169 priority_at_last_priority_update_ = owner_->request_priority();
170 was_above_priority_cutoff_at_last_priority_update_ =
171 owner_->is_above_priority_cutoff();
173 priority_at_last_priority_update_ = PriorityCalculator::LowestPriority();
174 was_above_priority_cutoff_at_last_priority_update_ = false;
178 void PrioritizedResource::Backing::UpdateState(
179 ResourceProvider* resource_provider) {
181 (proxy()->IsImplThread() && proxy()->IsMainThreadBlocked()));
182 in_drawing_impl_tree_ = !!owner();
183 in_parent_compositor_ = resource_provider->InUseByConsumer(id());
184 if (!in_drawing_impl_tree_) {
185 DCHECK_EQ(priority_at_last_priority_update_,
186 PriorityCalculator::LowestPriority());
190 void PrioritizedResource::ReturnBackingTexture() {
191 DCHECK(manager_ || !backing_);
193 manager_->ReturnBackingTexture(this);
196 const Proxy* PrioritizedResource::Backing::proxy() const {
197 if (!owner_ || !owner_->resource_manager())
199 return owner_->resource_manager()->ProxyForDebug();