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_PRIORITIZED_RESOURCE_H_
6 #define CC_PRIORITIZED_RESOURCE_H_
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "cc/cc_export.h"
12 #include "cc/priority_calculator.h"
13 #include "cc/resource.h"
14 #include "cc/resource_provider.h"
15 #include "third_party/khronos/GLES2/gl2.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/size.h"
18 #include "ui/gfx/vector2d.h"
22 class PrioritizedResourceManager
;
25 class CC_EXPORT PrioritizedResource
{
27 static scoped_ptr
<PrioritizedResource
> create(PrioritizedResourceManager
* manager
, gfx::Size size
, GLenum format
)
29 return make_scoped_ptr(new PrioritizedResource(manager
, size
, format
));
31 static scoped_ptr
<PrioritizedResource
> create(PrioritizedResourceManager
* manager
)
33 return make_scoped_ptr(new PrioritizedResource(manager
, gfx::Size(), 0));
35 ~PrioritizedResource();
37 // Texture properties. Changing these causes the backing texture to be lost.
38 // Setting these to the same value is a no-op.
39 void setTextureManager(PrioritizedResourceManager
*);
40 PrioritizedResourceManager
* resourceManager() { return m_manager
; }
41 void setDimensions(gfx::Size
, GLenum format
);
42 GLenum
format() const { return m_format
; }
43 gfx::Size
size() const { return m_size
; }
44 size_t bytes() const { return m_bytes
; }
45 bool contentsSwizzled() const { return m_contentsSwizzled
; }
47 // Set priority for the requested texture.
48 void setRequestPriority(int priority
) { m_priority
= priority
; }
49 int requestPriority() const { return m_priority
; }
51 // After PrioritizedResource::prioritizeTextures() is called, this returns
52 // if the the request succeeded and this texture can be acquired for use.
53 bool canAcquireBackingTexture() const { return m_isAbovePriorityCutoff
; }
55 // This returns whether we still have a backing texture. This can continue
56 // to be true even after canAcquireBackingTexture() becomes false. In this
57 // case the texture can be used but shouldn't be updated since it will get
59 bool haveBackingTexture() const { return !!backing(); }
61 bool backingResourceWasEvicted() const;
63 // If canAcquireBackingTexture() is true acquireBackingTexture() will acquire
64 // a backing texture for use. Call this whenever the texture is actually needed.
65 void acquireBackingTexture(ResourceProvider
*);
67 // FIXME: Request late is really a hack for when we are totally out of memory
68 // (all textures are visible) but we can still squeeze into the limit
69 // by not painting occluded textures. In this case the manager
70 // refuses all visible textures and requestLate() will enable
71 // canAcquireBackingTexture() on a call-order basis. We might want to
72 // just remove this in the future (carefully) and just make sure we don't
73 // regress OOMs situations.
76 // Update pixels of backing resource from image. This functions will aquire the backing if needed.
77 void setPixels(ResourceProvider
*, const uint8_t* image
, const gfx::Rect
& imageRect
, const gfx::Rect
& sourceRect
, const gfx::Vector2d
& destOffset
);
79 ResourceProvider::ResourceId
resourceId() const;
81 // Self-managed textures are accounted for when prioritizing other textures,
82 // but they are not allocated/recycled/deleted, so this needs to be done
83 // externally. canAcquireBackingTexture() indicates if the texture would have
84 // been allowed given its priority.
85 void setIsSelfManaged(bool isSelfManaged
) { m_isSelfManaged
= isSelfManaged
; }
86 bool isSelfManaged() { return m_isSelfManaged
; }
87 void setToSelfManagedMemoryPlaceholder(size_t bytes
);
89 void returnBackingTexture();
92 friend class PrioritizedResourceManager
;
93 friend class PrioritizedResourceTest
;
95 class Backing
: public Resource
{
97 Backing(unsigned id
, ResourceProvider
*, gfx::Size
, GLenum format
);
99 void updatePriority();
100 void updateInDrawingImplTree();
102 PrioritizedResource
* owner() { return m_owner
; }
103 bool canBeRecycled() const;
104 int requestPriorityAtLastPriorityUpdate() const { return m_priorityAtLastPriorityUpdate
; }
105 bool wasAbovePriorityCutoffAtLastPriorityUpdate() const { return m_wasAbovePriorityCutoffAtLastPriorityUpdate
; }
106 bool inDrawingImplTree() const { return m_inDrawingImplTree
; }
108 void deleteResource(ResourceProvider
*);
109 bool resourceHasBeenDeleted() const;
112 const Proxy
* proxy() const;
114 friend class PrioritizedResource
;
115 friend class PrioritizedResourceManager
;
116 PrioritizedResource
* m_owner
;
117 int m_priorityAtLastPriorityUpdate
;
118 bool m_wasAbovePriorityCutoffAtLastPriorityUpdate
;
120 // Set if this is currently-drawing impl tree.
121 bool m_inDrawingImplTree
;
123 bool m_resourceHasBeenDeleted
;
126 ResourceProvider
* m_resourceProvider
;
128 DISALLOW_COPY_AND_ASSIGN(Backing
);
131 PrioritizedResource(PrioritizedResourceManager
*, gfx::Size
, GLenum format
);
133 bool isAbovePriorityCutoff() { return m_isAbovePriorityCutoff
; }
134 void setAbovePriorityCutoff(bool isAbovePriorityCutoff
) { m_isAbovePriorityCutoff
= isAbovePriorityCutoff
; }
135 void setManagerInternal(PrioritizedResourceManager
* manager
) { m_manager
= manager
; }
137 Backing
* backing() const { return m_backing
; }
144 bool m_contentsSwizzled
;
147 bool m_isAbovePriorityCutoff
;
148 bool m_isSelfManaged
;
151 PrioritizedResourceManager
* m_manager
;
153 DISALLOW_COPY_AND_ASSIGN(PrioritizedResource
);
158 #endif // CC_PRIORITIZED_RESOURCE_H_