GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / cc / resources / prioritized_resource.h
blob3b6ecc028e84ff66416776d1e078c936de6bb701
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"
18 namespace cc {
20 class PrioritizedResourceManager;
21 class Proxy;
23 class CC_EXPORT PrioritizedResource {
24 public:
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
59 // taken away "soon".
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
66 // needed.
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.
75 bool RequestLate();
77 // Update pixels of backing resource from image. This functions will aquire
78 // the backing if needed.
79 void SetPixels(ResourceProvider* resource_provider,
80 const uint8_t* image,
81 const gfx::Rect& image_rect,
82 const gfx::Rect& source_rect,
83 const gfx::Vector2d& dest_offset);
85 ResourceProvider::ResourceId resource_id() const {
86 return backing_ ? backing_->id() : 0;
89 // Self-managed textures are accounted for when prioritizing other textures,
90 // but they are not allocated/recycled/deleted, so this needs to be done
91 // externally. CanAcquireBackingTexture() indicates if the texture would have
92 // been allowed given its priority.
93 void set_is_self_managed(bool is_self_managed) {
94 is_self_managed_ = is_self_managed;
96 bool is_self_managed() { return is_self_managed_; }
97 void SetToSelfManagedMemoryPlaceholder(size_t bytes);
99 void ReturnBackingTexture();
101 private:
102 friend class PrioritizedResourceManager;
103 friend class PrioritizedResourceTest;
105 class Backing : public Resource {
106 public:
107 Backing(unsigned id,
108 ResourceProvider* resource_provider,
109 const gfx::Size& size,
110 ResourceFormat format);
111 ~Backing();
112 void UpdatePriority();
113 void UpdateState(ResourceProvider* resource_provider);
115 PrioritizedResource* owner() { return owner_; }
116 bool CanBeRecycledIfNotInExternalUse() const;
117 int request_priority_at_last_priority_update() const {
118 return priority_at_last_priority_update_;
120 bool was_above_priority_cutoff_at_last_priority_update() const {
121 return was_above_priority_cutoff_at_last_priority_update_;
123 bool in_drawing_impl_tree() const { return in_drawing_impl_tree_; }
124 bool in_parent_compositor() const { return in_parent_compositor_; }
126 void DeleteResource(ResourceProvider* resource_provider);
127 bool ResourceHasBeenDeleted() const;
129 private:
130 const Proxy* proxy() const;
132 friend class PrioritizedResource;
133 friend class PrioritizedResourceManager;
134 PrioritizedResource* owner_;
135 int priority_at_last_priority_update_;
136 bool was_above_priority_cutoff_at_last_priority_update_;
138 // Set if this is currently-drawing impl tree.
139 bool in_drawing_impl_tree_;
140 // Set if this is in the parent compositor.
141 bool in_parent_compositor_;
143 bool resource_has_been_deleted_;
145 #if DCHECK_IS_ON()
146 ResourceProvider* resource_provider_;
147 #endif
148 DISALLOW_COPY_AND_ASSIGN(Backing);
151 PrioritizedResource(PrioritizedResourceManager* resource_manager,
152 const gfx::Size& size,
153 ResourceFormat format);
155 bool is_above_priority_cutoff() { return is_above_priority_cutoff_; }
156 void set_above_priority_cutoff(bool is_above_priority_cutoff) {
157 is_above_priority_cutoff_ = is_above_priority_cutoff;
159 void set_manager_internal(PrioritizedResourceManager* manager) {
160 manager_ = manager;
163 Backing* backing() const { return backing_; }
164 void Link(Backing* backing);
165 void Unlink();
167 gfx::Size size_;
168 ResourceFormat format_;
169 size_t bytes_;
170 bool contents_swizzled_;
172 int priority_;
173 bool is_above_priority_cutoff_;
174 bool is_self_managed_;
176 Backing* backing_;
177 PrioritizedResourceManager* manager_;
179 DISALLOW_COPY_AND_ASSIGN(PrioritizedResource);
182 } // namespace cc
184 #endif // CC_RESOURCES_PRIORITIZED_RESOURCE_H_