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
) {
137 PrioritizedResource::Backing::~Backing() {
139 DCHECK(resource_has_been_deleted_
);
142 void PrioritizedResource::Backing::DeleteResource(
143 ResourceProvider
* resource_provider
) {
144 DCHECK(!proxy() || proxy()->IsImplThread());
145 DCHECK(!resource_has_been_deleted_
);
147 DCHECK(resource_provider
== resource_provider_
);
150 resource_provider
->DeleteResource(id());
152 resource_has_been_deleted_
= true;
155 bool PrioritizedResource::Backing::ResourceHasBeenDeleted() const {
156 DCHECK(!proxy() || proxy()->IsImplThread());
157 return resource_has_been_deleted_
;
160 bool PrioritizedResource::Backing::CanBeRecycledIfNotInExternalUse() const {
161 DCHECK(!proxy() || proxy()->IsImplThread());
162 return !was_above_priority_cutoff_at_last_priority_update_
&&
163 !in_drawing_impl_tree_
;
166 void PrioritizedResource::Backing::UpdatePriority() {
168 (proxy()->IsImplThread() && proxy()->IsMainThreadBlocked()));
170 priority_at_last_priority_update_
= owner_
->request_priority();
171 was_above_priority_cutoff_at_last_priority_update_
=
172 owner_
->is_above_priority_cutoff();
174 priority_at_last_priority_update_
= PriorityCalculator::LowestPriority();
175 was_above_priority_cutoff_at_last_priority_update_
= false;
179 void PrioritizedResource::Backing::UpdateState(
180 ResourceProvider
* resource_provider
) {
182 (proxy()->IsImplThread() && proxy()->IsMainThreadBlocked()));
183 in_drawing_impl_tree_
= !!owner();
184 in_parent_compositor_
= resource_provider
->InUseByConsumer(id());
185 if (!in_drawing_impl_tree_
) {
186 DCHECK_EQ(priority_at_last_priority_update_
,
187 PriorityCalculator::LowestPriority());
191 void PrioritizedResource::ReturnBackingTexture() {
192 DCHECK(manager_
|| !backing_
);
194 manager_
->ReturnBackingTexture(this);
197 const Proxy
* PrioritizedResource::Backing::proxy() const {
198 if (!owner_
|| !owner_
->resource_manager())
200 return owner_
->resource_manager()->ProxyForDebug();