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_PRIORITIZED_RESOURCE_H_
6 #define CC_RESOURCES_PRIORITIZED_RESOURCE_H_
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "cc/base/cc_export.h"
12 #include "cc/resources/priority_calculator.h"
13 #include "cc/resources/resource.h"
14 #include "cc/resources/resource_provider.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/size.h"
17 #include "ui/gfx/vector2d.h"
21 class PrioritizedResourceManager
;
24 class CC_EXPORT PrioritizedResource
{
26 static scoped_ptr
<PrioritizedResource
> Create(
27 PrioritizedResourceManager
* manager
,
28 const gfx::Size
& size
,
29 ResourceFormat format
) {
30 return make_scoped_ptr(new PrioritizedResource(manager
, size
, format
));
32 static scoped_ptr
<PrioritizedResource
> Create(
33 PrioritizedResourceManager
* manager
) {
34 return make_scoped_ptr(
35 new PrioritizedResource(manager
, gfx::Size(), RGBA_8888
));
37 ~PrioritizedResource();
39 // Texture properties. Changing these causes the backing texture to be lost.
40 // Setting these to the same value is a no-op.
41 void SetTextureManager(PrioritizedResourceManager
* manager
);
42 PrioritizedResourceManager
* resource_manager() { return manager_
; }
43 void SetDimensions(const gfx::Size
& size
, ResourceFormat format
);
44 ResourceFormat
format() const { return format_
; }
45 gfx::Size
size() const { return size_
; }
46 size_t bytes() const { return bytes_
; }
47 bool contents_swizzled() const { return contents_swizzled_
; }
49 // Set priority for the requested texture.
50 void set_request_priority(int priority
) { priority_
= priority
; }
51 int request_priority() const { return priority_
; }
53 // After PrioritizedResource::PrioritizeTextures() is called, this returns
54 // if the the request succeeded and this texture can be acquired for use.
55 bool can_acquire_backing_texture() const { return is_above_priority_cutoff_
; }
57 // This returns whether we still have a backing texture. This can continue
58 // to be true even after CanAcquireBackingTexture() becomes false. In this
59 // case the texture can be used but shouldn't be updated since it will get
61 bool have_backing_texture() const { return !!backing(); }
63 bool BackingResourceWasEvicted() const;
65 // If CanAcquireBackingTexture() is true AcquireBackingTexture() will acquire
66 // a backing texture for use. Call this whenever the texture is actually
68 void AcquireBackingTexture(ResourceProvider
* resource_provider
);
70 // TODO(epenner): Request late is really a hack for when we are totally out of
71 // memory (all textures are visible) but we can still squeeze into the limit
72 // by not painting occluded textures. In this case the manager refuses all
73 // visible textures and RequestLate() will enable CanAcquireBackingTexture()
74 // on a call-order basis. We might want to just remove this in the future
75 // (carefully) and just make sure we don't regress OOMs situations.
78 // Update pixels of backing resource from image. This functions will aquire
79 // the backing if needed.
80 void SetPixels(ResourceProvider
* resource_provider
,
82 const gfx::Rect
& image_rect
,
83 const gfx::Rect
& source_rect
,
84 const gfx::Vector2d
& dest_offset
);
86 ResourceProvider::ResourceId
resource_id() const {
87 return backing_
? backing_
->id() : 0;
90 // Self-managed textures are accounted for when prioritizing other textures,
91 // but they are not allocated/recycled/deleted, so this needs to be done
92 // externally. CanAcquireBackingTexture() indicates if the texture would have
93 // been allowed given its priority.
94 void set_is_self_managed(bool is_self_managed
) {
95 is_self_managed_
= is_self_managed
;
97 bool is_self_managed() { return is_self_managed_
; }
98 void SetToSelfManagedMemoryPlaceholder(size_t bytes
);
100 void ReturnBackingTexture();
103 friend class PrioritizedResourceManager
;
104 friend class PrioritizedResourceTest
;
106 class Backing
: public Resource
{
109 ResourceProvider
* resource_provider
,
110 const gfx::Size
& size
,
111 ResourceFormat format
);
113 void UpdatePriority();
114 void UpdateState(ResourceProvider
* resource_provider
);
116 PrioritizedResource
* owner() { return owner_
; }
117 bool CanBeRecycledIfNotInExternalUse() const;
118 int request_priority_at_last_priority_update() const {
119 return priority_at_last_priority_update_
;
121 bool was_above_priority_cutoff_at_last_priority_update() const {
122 return was_above_priority_cutoff_at_last_priority_update_
;
124 bool in_drawing_impl_tree() const { return in_drawing_impl_tree_
; }
125 bool in_parent_compositor() const { return in_parent_compositor_
; }
127 void DeleteResource(ResourceProvider
* resource_provider
);
128 bool ResourceHasBeenDeleted() const;
131 const Proxy
* proxy() const;
133 friend class PrioritizedResource
;
134 friend class PrioritizedResourceManager
;
135 PrioritizedResource
* owner_
;
136 int priority_at_last_priority_update_
;
137 bool was_above_priority_cutoff_at_last_priority_update_
;
139 // Set if this is currently-drawing impl tree.
140 bool in_drawing_impl_tree_
;
141 // Set if this is in the parent compositor.
142 bool in_parent_compositor_
;
144 bool resource_has_been_deleted_
;
147 ResourceProvider
* resource_provider_
;
149 DISALLOW_COPY_AND_ASSIGN(Backing
);
152 PrioritizedResource(PrioritizedResourceManager
* resource_manager
,
153 const gfx::Size
& size
,
154 ResourceFormat format
);
156 bool is_above_priority_cutoff() { return is_above_priority_cutoff_
; }
157 void set_above_priority_cutoff(bool is_above_priority_cutoff
) {
158 is_above_priority_cutoff_
= is_above_priority_cutoff
;
160 void set_manager_internal(PrioritizedResourceManager
* manager
) {
164 Backing
* backing() const { return backing_
; }
165 void Link(Backing
* backing
);
169 ResourceFormat format_
;
171 bool contents_swizzled_
;
174 bool is_above_priority_cutoff_
;
175 bool is_self_managed_
;
178 PrioritizedResourceManager
* manager_
;
180 DISALLOW_COPY_AND_ASSIGN(PrioritizedResource
);
185 #endif // CC_RESOURCES_PRIORITIZED_RESOURCE_H_