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 #ifndef CC_RESOURCES_PRIORITIZED_RESOURCE_H_ 6 #define CC_RESOURCES_PRIORITIZED_RESOURCE_H_ 7 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" 18 19 namespace cc { 20 21 class PrioritizedResourceManager; 22 class Proxy; 23 24 class CC_EXPORT PrioritizedResource { 25 public: Create(PrioritizedResourceManager * manager,const gfx::Size & size,ResourceFormat format)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)); 31 } Create(PrioritizedResourceManager * manager)32 static scoped_ptr<PrioritizedResource> Create( 33 PrioritizedResourceManager* manager) { 34 return make_scoped_ptr( 35 new PrioritizedResource(manager, gfx::Size(), RGBA_8888)); 36 } 37 ~PrioritizedResource(); 38 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); resource_manager()42 PrioritizedResourceManager* resource_manager() { return manager_; } 43 void SetDimensions(const gfx::Size& size, ResourceFormat format); format()44 ResourceFormat format() const { return format_; } size()45 gfx::Size size() const { return size_; } bytes()46 size_t bytes() const { return bytes_; } contents_swizzled()47 bool contents_swizzled() const { return contents_swizzled_; } 48 49 // Set priority for the requested texture. set_request_priority(int priority)50 void set_request_priority(int priority) { priority_ = priority; } request_priority()51 int request_priority() const { return priority_; } 52 53 // After PrioritizedResource::PrioritizeTextures() is called, this returns 54 // if the the request succeeded and this texture can be acquired for use. can_acquire_backing_texture()55 bool can_acquire_backing_texture() const { return is_above_priority_cutoff_; } 56 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 60 // taken away "soon". have_backing_texture()61 bool have_backing_texture() const { return !!backing(); } 62 63 bool BackingResourceWasEvicted() const; 64 65 // If CanAcquireBackingTexture() is true AcquireBackingTexture() will acquire 66 // a backing texture for use. Call this whenever the texture is actually 67 // needed. 68 void AcquireBackingTexture(ResourceProvider* resource_provider); 69 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. 76 bool RequestLate(); 77 78 // Update pixels of backing resource from image. This functions will aquire 79 // the backing if needed. 80 void SetPixels(ResourceProvider* resource_provider, 81 const uint8_t* image, 82 const gfx::Rect& image_rect, 83 const gfx::Rect& source_rect, 84 const gfx::Vector2d& dest_offset); 85 resource_id()86 ResourceProvider::ResourceId resource_id() const { 87 return backing_ ? backing_->id() : 0; 88 } 89 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. set_is_self_managed(bool is_self_managed)94 void set_is_self_managed(bool is_self_managed) { 95 is_self_managed_ = is_self_managed; 96 } is_self_managed()97 bool is_self_managed() { return is_self_managed_; } 98 void SetToSelfManagedMemoryPlaceholder(size_t bytes); 99 100 void ReturnBackingTexture(); 101 102 private: 103 friend class PrioritizedResourceManager; 104 friend class PrioritizedResourceTest; 105 106 class Backing : public Resource { 107 public: 108 Backing(unsigned id, 109 ResourceProvider* resource_provider, 110 const gfx::Size& size, 111 ResourceFormat format); 112 ~Backing(); 113 void UpdatePriority(); 114 void UpdateState(ResourceProvider* resource_provider); 115 owner()116 PrioritizedResource* owner() { return owner_; } 117 bool CanBeRecycledIfNotInExternalUse() const; request_priority_at_last_priority_update()118 int request_priority_at_last_priority_update() const { 119 return priority_at_last_priority_update_; 120 } was_above_priority_cutoff_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_; 123 } in_drawing_impl_tree()124 bool in_drawing_impl_tree() const { return in_drawing_impl_tree_; } in_parent_compositor()125 bool in_parent_compositor() const { return in_parent_compositor_; } 126 127 void DeleteResource(ResourceProvider* resource_provider); 128 bool ResourceHasBeenDeleted() const; 129 130 private: 131 const Proxy* proxy() const; 132 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_; 138 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_; 143 144 bool resource_has_been_deleted_; 145 146 #if DCHECK_IS_ON 147 ResourceProvider* resource_provider_; 148 #endif 149 DISALLOW_COPY_AND_ASSIGN(Backing); 150 }; 151 152 PrioritizedResource(PrioritizedResourceManager* resource_manager, 153 const gfx::Size& size, 154 ResourceFormat format); 155 is_above_priority_cutoff()156 bool is_above_priority_cutoff() { return is_above_priority_cutoff_; } set_above_priority_cutoff(bool is_above_priority_cutoff)157 void set_above_priority_cutoff(bool is_above_priority_cutoff) { 158 is_above_priority_cutoff_ = is_above_priority_cutoff; 159 } set_manager_internal(PrioritizedResourceManager * manager)160 void set_manager_internal(PrioritizedResourceManager* manager) { 161 manager_ = manager; 162 } 163 backing()164 Backing* backing() const { return backing_; } 165 void Link(Backing* backing); 166 void Unlink(); 167 168 gfx::Size size_; 169 ResourceFormat format_; 170 size_t bytes_; 171 bool contents_swizzled_; 172 173 int priority_; 174 bool is_above_priority_cutoff_; 175 bool is_self_managed_; 176 177 Backing* backing_; 178 PrioritizedResourceManager* manager_; 179 180 DISALLOW_COPY_AND_ASSIGN(PrioritizedResource); 181 }; 182 183 } // namespace cc 184 185 #endif // CC_RESOURCES_PRIORITIZED_RESOURCE_H_ 186