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/resource.h"
13 #include "cc/resources/resource_provider.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/geometry/size.h"
16 #include "ui/gfx/geometry/vector2d.h"
20 class PrioritizedResourceManager
;
23 class CC_EXPORT PrioritizedResource
{
25 static scoped_ptr
<PrioritizedResource
> Create(
26 PrioritizedResourceManager
* manager
,
27 const gfx::Size
& size
,
28 ResourceFormat format
) {
29 return make_scoped_ptr(new PrioritizedResource(manager
, size
, format
));
31 static scoped_ptr
<PrioritizedResource
> Create(
32 PrioritizedResourceManager
* manager
) {
33 return make_scoped_ptr(
34 new PrioritizedResource(manager
, gfx::Size(), RGBA_8888
));
36 ~PrioritizedResource();
38 // Texture properties. Changing these causes the backing texture to be lost.
39 // Setting these to the same value is a no-op.
40 void SetTextureManager(PrioritizedResourceManager
* manager
);
41 PrioritizedResourceManager
* resource_manager() { return manager_
; }
42 void SetDimensions(const gfx::Size
& size
, ResourceFormat format
);
43 ResourceFormat
format() const { return format_
; }
44 gfx::Size
size() const { return size_
; }
45 size_t bytes() const { return bytes_
; }
46 bool contents_swizzled() const { return contents_swizzled_
; }
48 // Set priority for the requested texture.
49 void set_request_priority(int priority
) { priority_
= priority
; }
50 int request_priority() const { return priority_
; }
52 // After PrioritizedResource::PrioritizeTextures() is called, this returns
53 // if the the request succeeded and this texture can be acquired for use.
54 bool can_acquire_backing_texture() const { return is_above_priority_cutoff_
; }
56 // This returns whether we still have a backing texture. This can continue
57 // to be true even after CanAcquireBackingTexture() becomes false. In this
58 // case the texture can be used but shouldn't be updated since it will get
60 bool have_backing_texture() const { return !!backing(); }
62 bool BackingResourceWasEvicted() const;
64 // If CanAcquireBackingTexture() is true AcquireBackingTexture() will acquire
65 // a backing texture for use. Call this whenever the texture is actually
67 void AcquireBackingTexture(ResourceProvider
* resource_provider
);
69 // TODO(epenner): Request late is really a hack for when we are totally out of
70 // memory (all textures are visible) but we can still squeeze into the limit
71 // by not painting occluded textures. In this case the manager refuses all
72 // visible textures and RequestLate() will enable CanAcquireBackingTexture()
73 // on a call-order basis. We might want to just remove this in the future
74 // (carefully) and just make sure we don't regress OOMs situations.
77 // Update pixels of backing resource from image. This functions will aquire
78 // the backing if needed.
79 void SetPixels(ResourceProvider
* resource_provider
,
81 const gfx::Rect
& image_rect
,
82 const gfx::Rect
& source_rect
,
83 const gfx::Vector2d
& dest_offset
);
85 ResourceId
resource_id() const { return backing_
? backing_
->id() : 0; }
87 // Self-managed textures are accounted for when prioritizing other textures,
88 // but they are not allocated/recycled/deleted, so this needs to be done
89 // externally. CanAcquireBackingTexture() indicates if the texture would have
90 // been allowed given its priority.
91 void set_is_self_managed(bool is_self_managed
) {
92 is_self_managed_
= is_self_managed
;
94 bool is_self_managed() { return is_self_managed_
; }
95 void SetToSelfManagedMemoryPlaceholder(size_t bytes
);
97 void ReturnBackingTexture();
100 friend class PrioritizedResourceManager
;
101 friend class PrioritizedResourceTest
;
103 class Backing
: public Resource
{
106 ResourceProvider
* resource_provider
,
107 const gfx::Size
& size
,
108 ResourceFormat format
);
110 void UpdatePriority();
111 void UpdateState(ResourceProvider
* resource_provider
);
113 PrioritizedResource
* owner() { return owner_
; }
114 bool CanBeRecycledIfNotInExternalUse() const;
115 int request_priority_at_last_priority_update() const {
116 return priority_at_last_priority_update_
;
118 bool was_above_priority_cutoff_at_last_priority_update() const {
119 return was_above_priority_cutoff_at_last_priority_update_
;
121 bool in_drawing_impl_tree() const { return in_drawing_impl_tree_
; }
122 bool in_parent_compositor() const { return in_parent_compositor_
; }
124 void DeleteResource(ResourceProvider
* resource_provider
);
125 bool ResourceHasBeenDeleted() const;
128 const Proxy
* proxy() const;
130 friend class PrioritizedResource
;
131 friend class PrioritizedResourceManager
;
132 PrioritizedResource
* owner_
;
133 int priority_at_last_priority_update_
;
134 bool was_above_priority_cutoff_at_last_priority_update_
;
136 // Set if this is currently-drawing impl tree.
137 bool in_drawing_impl_tree_
;
138 // Set if this is in the parent compositor.
139 bool in_parent_compositor_
;
141 bool resource_has_been_deleted_
;
144 ResourceProvider
* resource_provider_
;
146 DISALLOW_COPY_AND_ASSIGN(Backing
);
149 PrioritizedResource(PrioritizedResourceManager
* resource_manager
,
150 const gfx::Size
& size
,
151 ResourceFormat format
);
153 bool is_above_priority_cutoff() { return is_above_priority_cutoff_
; }
154 void set_above_priority_cutoff(bool is_above_priority_cutoff
) {
155 is_above_priority_cutoff_
= is_above_priority_cutoff
;
157 void set_manager_internal(PrioritizedResourceManager
* manager
) {
161 Backing
* backing() const { return backing_
; }
162 void Link(Backing
* backing
);
166 ResourceFormat format_
;
168 bool contents_swizzled_
;
171 bool is_above_priority_cutoff_
;
172 bool is_self_managed_
;
175 PrioritizedResourceManager
* manager_
;
177 DISALLOW_COPY_AND_ASSIGN(PrioritizedResource
);
182 #endif // CC_RESOURCES_PRIORITIZED_RESOURCE_H_