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 CCPrioritizedTexture_h
6 #define CCPrioritizedTexture_h
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "CCPriorityCalculator.h"
11 #include "CCResourceProvider.h"
12 #include "CCTexture.h"
13 #include "GraphicsContext3D.h"
19 class CCPrioritizedTextureManager
;
21 class CCPrioritizedTexture
{
23 static scoped_ptr
<CCPrioritizedTexture
> create(CCPrioritizedTextureManager
* manager
, IntSize size
, GC3Denum format
)
25 return make_scoped_ptr(new CCPrioritizedTexture(manager
, size
, format
));
27 static scoped_ptr
<CCPrioritizedTexture
> create(CCPrioritizedTextureManager
* manager
)
29 return make_scoped_ptr(new CCPrioritizedTexture(manager
, IntSize(), 0));
31 ~CCPrioritizedTexture();
33 // Texture properties. Changing these causes the backing texture to be lost.
34 // Setting these to the same value is a no-op.
35 void setTextureManager(CCPrioritizedTextureManager
*);
36 CCPrioritizedTextureManager
* textureManager() { return m_manager
; }
37 void setDimensions(IntSize
, GC3Denum format
);
38 GC3Denum
format() const { return m_format
; }
39 IntSize
size() const { return m_size
; }
40 size_t bytes() const { return m_bytes
; }
42 // Set priority for the requested texture.
43 void setRequestPriority(int priority
) { m_priority
= priority
; }
44 int requestPriority() const { return m_priority
; }
46 // After CCPrioritizedTexture::prioritizeTextures() is called, this returns
47 // if the the request succeeded and this texture can be acquired for use.
48 bool canAcquireBackingTexture() const { return m_isAbovePriorityCutoff
; }
50 // This returns whether we still have a backing texture. This can continue
51 // to be true even after canAcquireBackingTexture() becomes false. In this
52 // case the texture can be used but shouldn't be updated since it will get
54 bool haveBackingTexture() const { return !!backing(); }
56 bool backingResourceWasEvicted() const;
58 // If canAcquireBackingTexture() is true acquireBackingTexture() will acquire
59 // a backing texture for use. Call this whenever the texture is actually needed.
60 void acquireBackingTexture(CCResourceProvider
*);
62 // FIXME: Request late is really a hack for when we are totally out of memory
63 // (all textures are visible) but we can still squeeze into the limit
64 // by not painting occluded textures. In this case the manager
65 // refuses all visible textures and requestLate() will enable
66 // canAcquireBackingTexture() on a call-order basis. We might want to
67 // just remove this in the future (carefully) and just make sure we don't
68 // regress OOMs situations.
71 // Uploads pixels into the backing resource. This functions will aquire the backing if needed.
72 void upload(CCResourceProvider
*, const uint8_t* image
, const IntRect
& imageRect
, const IntRect
& sourceRect
, const IntSize
& destOffset
);
74 CCResourceProvider::ResourceId
resourceId() const;
76 // Self-managed textures are accounted for when prioritizing other textures,
77 // but they are not allocated/recycled/deleted, so this needs to be done
78 // externally. canAcquireBackingTexture() indicates if the texture would have
79 // been allowed given its priority.
80 void setIsSelfManaged(bool isSelfManaged
) { m_isSelfManaged
= isSelfManaged
; }
81 bool isSelfManaged() { return m_isSelfManaged
; }
82 void setToSelfManagedMemoryPlaceholder(size_t bytes
);
85 friend class CCPrioritizedTextureManager
;
86 friend class CCPrioritizedTextureTest
;
88 class Backing
: public CCTexture
{
90 Backing(unsigned id
, CCResourceProvider
*, IntSize
, GC3Denum format
);
92 void updatePriority();
93 void updateInDrawingImplTree();
95 CCPrioritizedTexture
* owner() { return m_owner
; }
96 bool canBeRecycled() const;
97 int requestPriorityAtLastPriorityUpdate() const { return m_priorityAtLastPriorityUpdate
; }
98 bool wasAbovePriorityCutoffAtLastPriorityUpdate() const { return m_wasAbovePriorityCutoffAtLastPriorityUpdate
; }
99 bool inDrawingImplTree() const { return m_inDrawingImplTree
; }
101 void deleteResource(CCResourceProvider
*);
102 bool resourceHasBeenDeleted() const;
105 friend class CCPrioritizedTexture
;
106 CCPrioritizedTexture
* m_owner
;
107 int m_priorityAtLastPriorityUpdate
;
108 bool m_wasAbovePriorityCutoffAtLastPriorityUpdate
;
110 // Set if this is currently-drawing impl tree.
111 bool m_inDrawingImplTree
;
113 bool m_resourceHasBeenDeleted
;
115 CCResourceProvider
* m_resourceProvider
;
118 DISALLOW_COPY_AND_ASSIGN(Backing
);
121 CCPrioritizedTexture(CCPrioritizedTextureManager
*, IntSize
, GC3Denum format
);
123 bool isAbovePriorityCutoff() { return m_isAbovePriorityCutoff
; }
124 void setAbovePriorityCutoff(bool isAbovePriorityCutoff
) { m_isAbovePriorityCutoff
= isAbovePriorityCutoff
; }
125 void setManagerInternal(CCPrioritizedTextureManager
* manager
) { m_manager
= manager
; }
127 Backing
* backing() const { return m_backing
; }
136 bool m_isAbovePriorityCutoff
;
137 bool m_isSelfManaged
;
140 CCPrioritizedTextureManager
* m_manager
;
142 DISALLOW_COPY_AND_ASSIGN(CCPrioritizedTexture
);