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.
6 #ifndef CCResourceProvider_h
7 #define CCResourceProvider_h
9 #include "CCGraphicsContext.h"
10 #include "GraphicsContext3D.h"
14 #include <wtf/Deque.h>
15 #include <wtf/HashMap.h>
16 #include <wtf/OwnPtr.h>
17 #include <wtf/PassOwnPtr.h>
18 #include <wtf/PassRefPtr.h>
19 #include <wtf/RefPtr.h>
20 #include <wtf/Vector.h>
23 class WebGraphicsContext3D
;
29 class LayerTextureSubImage
;
31 // Thread-safety notes: this class is not thread-safe and can only be called
32 // from the thread it was created on (in practice, the compositor thread).
33 class CCResourceProvider
{
34 WTF_MAKE_NONCOPYABLE(CCResourceProvider
);
36 typedef unsigned ResourceId
;
37 typedef Vector
<ResourceId
> ResourceIdArray
;
38 typedef HashMap
<ResourceId
, ResourceId
> ResourceIdMap
;
39 enum TextureUsageHint
{ TextureUsageAny
, TextureUsageFramebuffer
};
47 struct TransferableResource
{
53 typedef Vector
<TransferableResource
> TransferableResourceArray
;
54 struct TransferableResourceList
{
55 TransferableResourceArray resources
;
59 static PassOwnPtr
<CCResourceProvider
> create(CCGraphicsContext
*);
61 virtual ~CCResourceProvider();
63 WebKit::WebGraphicsContext3D
* graphicsContext3D();
64 int maxTextureSize() const { return m_maxTextureSize
; }
65 unsigned numResources() const { return m_resources
.size(); }
67 // Checks whether a resource is in use by a consumer.
68 bool inUseByConsumer(ResourceId
);
71 // Producer interface.
73 void setDefaultResourceType(ResourceType type
) { m_defaultResourceType
= type
; }
74 ResourceType
defaultResourceType() const { return m_defaultResourceType
; }
75 ResourceType
resourceType(ResourceId
);
77 // Creates a resource of the default resource type.
78 ResourceId
createResource(int pool
, const IntSize
&, GC3Denum format
, TextureUsageHint
);
80 // You can also explicitly create a specific resource type.
81 ResourceId
createGLTexture(int pool
, const IntSize
&, GC3Denum format
, TextureUsageHint
);
82 ResourceId
createBitmap(int pool
, const IntSize
&);
83 // Wraps an external texture into a GL resource.
84 ResourceId
createResourceFromExternalTexture(unsigned textureId
);
86 void deleteResource(ResourceId
);
88 // Deletes all resources owned by a given pool.
89 void deleteOwnedResources(int pool
);
91 // Upload data from image, copying sourceRect (in image) into destRect (in the resource).
92 void upload(ResourceId
, const uint8_t* image
, const IntRect
& imageRect
, const IntRect
& sourceRect
, const IntSize
& destOffset
);
94 // Flush all context operations, kicking uploads and ensuring ordering with
95 // respect to other contexts.
98 // Only flush the command buffer if supported.
99 // Returns true if the shallow flush occurred, false otherwise.
100 bool shallowFlushIfSupported();
102 // Creates accounting for a child, and associate it with a pool. Resources
103 // transfered from that child will go to that pool. Returns a child ID.
104 int createChild(int pool
);
106 // Destroys accounting for the child, deleting all resources from that pool.
107 void destroyChild(int child
);
109 // Gets the child->parent resource ID map.
110 const ResourceIdMap
& getChildToParentMap(int child
) const;
112 // Prepares resources to be transfered to the parent, moving them to
113 // mailboxes and serializing meta-data into TransferableResources.
114 // Resources are not removed from the CCResourceProvider, but are markes as
116 TransferableResourceList
prepareSendToParent(const ResourceIdArray
&);
118 // Prepares resources to be transfered back to the child, moving them to
119 // mailboxes and serializing meta-data into TransferableResources.
120 // Resources are removed from the CCResourceProvider. Note: the resource IDs
121 // passed are in the parent namespace and will be translated to the child
122 // namespace when returned.
123 TransferableResourceList
prepareSendToChild(int child
, const ResourceIdArray
&);
125 // Receives resources from a child, moving them from mailboxes. Resource IDs
126 // passed are in the child namespace, and will be translated to the parent
127 // namespace, added to the child->parent map.
128 // NOTE: if the syncPoint filed in TransferableResourceList is set, this
130 void receiveFromChild(int child
, const TransferableResourceList
&);
132 // Receives resources from the parent, moving them from mailboxes. Resource IDs
133 // passed are in the child namespace.
134 // NOTE: if the syncPoint filed in TransferableResourceList is set, this
136 void receiveFromParent(const TransferableResourceList
&);
139 size_t mailboxCount() const { return m_mailboxes
.size(); }
141 // The following lock classes are part of the CCResourceProvider API and are
142 // needed to read and write the resource contents. The user must ensure
143 // that they only use GL locks on GL resources, etc, and this is enforced
145 class ScopedReadLockGL
{
146 WTF_MAKE_NONCOPYABLE(ScopedReadLockGL
);
148 ScopedReadLockGL(CCResourceProvider
*, CCResourceProvider::ResourceId
);
151 unsigned textureId() const { return m_textureId
; }
154 CCResourceProvider
* m_resourceProvider
;
155 CCResourceProvider::ResourceId m_resourceId
;
156 unsigned m_textureId
;
159 class ScopedWriteLockGL
{
160 WTF_MAKE_NONCOPYABLE(ScopedWriteLockGL
);
162 ScopedWriteLockGL(CCResourceProvider
*, CCResourceProvider::ResourceId
);
163 ~ScopedWriteLockGL();
165 unsigned textureId() const { return m_textureId
; }
168 CCResourceProvider
* m_resourceProvider
;
169 CCResourceProvider::ResourceId m_resourceId
;
170 unsigned m_textureId
;
173 class ScopedReadLockSoftware
{
174 WTF_MAKE_NONCOPYABLE(ScopedReadLockSoftware
);
176 ScopedReadLockSoftware(CCResourceProvider
*, CCResourceProvider::ResourceId
);
177 ~ScopedReadLockSoftware();
179 const SkBitmap
* skBitmap() const { return &m_skBitmap
; }
182 CCResourceProvider
* m_resourceProvider
;
183 CCResourceProvider::ResourceId m_resourceId
;
187 class ScopedWriteLockSoftware
{
188 WTF_MAKE_NONCOPYABLE(ScopedWriteLockSoftware
);
190 ScopedWriteLockSoftware(CCResourceProvider
*, CCResourceProvider::ResourceId
);
191 ~ScopedWriteLockSoftware();
193 SkCanvas
* skCanvas() { return &m_skCanvas
; }
196 CCResourceProvider
* m_resourceProvider
;
197 CCResourceProvider::ResourceId m_resourceId
;
208 , lockForReadCount(0)
209 , lockedForWrite(false)
214 , type(static_cast<ResourceType
>(0))
216 Resource(unsigned textureId
, int pool
, const IntSize
& size
, GC3Denum format
)
220 , lockForReadCount(0)
221 , lockedForWrite(false)
228 Resource(uint8_t* pixels
, int pool
, const IntSize
& size
, GC3Denum format
)
232 , lockForReadCount(0)
233 , lockedForWrite(false)
243 int lockForReadCount
;
251 typedef HashMap
<ResourceId
, Resource
> ResourceMap
;
254 ResourceIdMap childToParentMap
;
255 ResourceIdMap parentToChildMap
;
257 typedef HashMap
<int, Child
> ChildMap
;
259 explicit CCResourceProvider(CCGraphicsContext
*);
262 const Resource
* lockForRead(ResourceId
);
263 void unlockForRead(ResourceId
);
264 const Resource
* lockForWrite(ResourceId
);
265 void unlockForWrite(ResourceId
);
266 static void populateSkBitmapWithResource(SkBitmap
*, const Resource
*);
268 bool transferResource(WebKit::WebGraphicsContext3D
*, ResourceId
, TransferableResource
*);
269 void trimMailboxDeque();
271 CCGraphicsContext
* m_context
;
273 ResourceMap m_resources
;
277 Deque
<Mailbox
> m_mailboxes
;
279 ResourceType m_defaultResourceType
;
280 bool m_useTextureStorageExt
;
281 bool m_useTextureUsageHint
;
282 bool m_useShallowFlush
;
283 OwnPtr
<LayerTextureSubImage
> m_texSubImage
;
284 int m_maxTextureSize
;