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 CCResourceProvider_h
6 #define CCResourceProvider_h
8 #include "base/basictypes.h"
9 #include "base/hash_tables.h"
10 #include "CCGraphicsContext.h"
11 #include "GraphicsContext3D.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "third_party/skia/include/core/SkCanvas.h"
15 #include "TextureCopier.h"
16 #include <wtf/Deque.h>
17 #include <wtf/OwnPtr.h>
18 #include <wtf/PassOwnPtr.h>
19 #include <wtf/PassRefPtr.h>
20 #include <wtf/RefPtr.h>
21 #include <wtf/Vector.h>
24 class WebGraphicsContext3D
;
30 class LayerTextureSubImage
;
32 class TextureUploader
;
34 // Thread-safety notes: this class is not thread-safe and can only be called
35 // from the thread it was created on (in practice, the compositor thread).
36 class CCResourceProvider
{
38 typedef unsigned ResourceId
;
39 typedef Vector
<ResourceId
> ResourceIdArray
;
40 typedef base::hash_map
<ResourceId
, ResourceId
> ResourceIdMap
;
41 enum TextureUsageHint
{ TextureUsageAny
, TextureUsageFramebuffer
};
49 struct TransferableResource
{
55 typedef Vector
<TransferableResource
> TransferableResourceArray
;
56 struct TransferableResourceList
{
57 TransferableResourceList();
58 ~TransferableResourceList();
60 TransferableResourceArray resources
;
64 static PassOwnPtr
<CCResourceProvider
> create(CCGraphicsContext
*);
66 virtual ~CCResourceProvider();
68 WebKit::WebGraphicsContext3D
* graphicsContext3D();
69 TextureUploader
* textureUploader() const { return m_textureUploader
.get(); }
70 TextureCopier
* textureCopier() const { return m_textureCopier
.get(); }
71 int maxTextureSize() const { return m_maxTextureSize
; }
72 unsigned numResources() const { return m_resources
.size(); }
74 // Checks whether a resource is in use by a consumer.
75 bool inUseByConsumer(ResourceId
);
78 // Producer interface.
80 void setDefaultResourceType(ResourceType type
) { m_defaultResourceType
= type
; }
81 ResourceType
defaultResourceType() const { return m_defaultResourceType
; }
82 ResourceType
resourceType(ResourceId
);
84 // Creates a resource of the default resource type.
85 ResourceId
createResource(int pool
, const IntSize
&, GC3Denum format
, TextureUsageHint
);
87 // You can also explicitly create a specific resource type.
88 ResourceId
createGLTexture(int pool
, const IntSize
&, GC3Denum format
, TextureUsageHint
);
89 ResourceId
createBitmap(int pool
, const IntSize
&);
90 // Wraps an external texture into a GL resource.
91 ResourceId
createResourceFromExternalTexture(unsigned textureId
);
93 void deleteResource(ResourceId
);
95 // Deletes all resources owned by a given pool.
96 void deleteOwnedResources(int pool
);
98 // Upload data from image, copying sourceRect (in image) into destRect (in the resource).
99 void upload(ResourceId
, const uint8_t* image
, const IntRect
& imageRect
, const IntRect
& sourceRect
, const IntSize
& destOffset
);
101 // Flush all context operations, kicking uploads and ensuring ordering with
102 // respect to other contexts.
105 // Only flush the command buffer if supported.
106 // Returns true if the shallow flush occurred, false otherwise.
107 bool shallowFlushIfSupported();
109 // Creates accounting for a child, and associate it with a pool. Resources
110 // transfered from that child will go to that pool. Returns a child ID.
111 int createChild(int pool
);
113 // Destroys accounting for the child, deleting all resources from that pool.
114 void destroyChild(int child
);
116 // Gets the child->parent resource ID map.
117 const ResourceIdMap
& getChildToParentMap(int child
) const;
119 // Prepares resources to be transfered to the parent, moving them to
120 // mailboxes and serializing meta-data into TransferableResources.
121 // Resources are not removed from the CCResourceProvider, but are markes as
123 TransferableResourceList
prepareSendToParent(const ResourceIdArray
&);
125 // Prepares resources to be transfered back to the child, moving them to
126 // mailboxes and serializing meta-data into TransferableResources.
127 // Resources are removed from the CCResourceProvider. Note: the resource IDs
128 // passed are in the parent namespace and will be translated to the child
129 // namespace when returned.
130 TransferableResourceList
prepareSendToChild(int child
, const ResourceIdArray
&);
132 // Receives resources from a child, moving them from mailboxes. Resource IDs
133 // passed are in the child namespace, and will be translated to the parent
134 // namespace, added to the child->parent map.
135 // NOTE: if the syncPoint filed in TransferableResourceList is set, this
137 void receiveFromChild(int child
, const TransferableResourceList
&);
139 // Receives resources from the parent, moving them from mailboxes. Resource IDs
140 // passed are in the child namespace.
141 // NOTE: if the syncPoint filed in TransferableResourceList is set, this
143 void receiveFromParent(const TransferableResourceList
&);
146 size_t mailboxCount() const { return m_mailboxes
.size(); }
148 // The following lock classes are part of the CCResourceProvider API and are
149 // needed to read and write the resource contents. The user must ensure
150 // that they only use GL locks on GL resources, etc, and this is enforced
152 class ScopedReadLockGL
{
154 ScopedReadLockGL(CCResourceProvider
*, CCResourceProvider::ResourceId
);
157 unsigned textureId() const { return m_textureId
; }
160 CCResourceProvider
* m_resourceProvider
;
161 CCResourceProvider::ResourceId m_resourceId
;
162 unsigned m_textureId
;
164 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL
);
167 class ScopedWriteLockGL
{
169 ScopedWriteLockGL(CCResourceProvider
*, CCResourceProvider::ResourceId
);
170 ~ScopedWriteLockGL();
172 unsigned textureId() const { return m_textureId
; }
175 CCResourceProvider
* m_resourceProvider
;
176 CCResourceProvider::ResourceId m_resourceId
;
177 unsigned m_textureId
;
179 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL
);
182 class ScopedReadLockSoftware
{
184 ScopedReadLockSoftware(CCResourceProvider
*, CCResourceProvider::ResourceId
);
185 ~ScopedReadLockSoftware();
187 const SkBitmap
* skBitmap() const { return &m_skBitmap
; }
190 CCResourceProvider
* m_resourceProvider
;
191 CCResourceProvider::ResourceId m_resourceId
;
194 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware
);
197 class ScopedWriteLockSoftware
{
199 ScopedWriteLockSoftware(CCResourceProvider
*, CCResourceProvider::ResourceId
);
200 ~ScopedWriteLockSoftware();
202 SkCanvas
* skCanvas() { return m_skCanvas
.get(); }
205 CCResourceProvider
* m_resourceProvider
;
206 CCResourceProvider::ResourceId m_resourceId
;
208 OwnPtr
<SkCanvas
> m_skCanvas
;
210 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware
);
216 Resource(unsigned textureId
, int pool
, const IntSize
& size
, GC3Denum format
);
217 Resource(uint8_t* pixels
, int pool
, const IntSize
& size
, GC3Denum format
);
222 int lockForReadCount
;
226 bool markedForDeletion
;
231 typedef base::hash_map
<ResourceId
, Resource
> ResourceMap
;
237 ResourceIdMap childToParentMap
;
238 ResourceIdMap parentToChildMap
;
240 typedef base::hash_map
<int, Child
> ChildMap
;
242 explicit CCResourceProvider(CCGraphicsContext
*);
245 const Resource
* lockForRead(ResourceId
);
246 void unlockForRead(ResourceId
);
247 const Resource
* lockForWrite(ResourceId
);
248 void unlockForWrite(ResourceId
);
249 static void populateSkBitmapWithResource(SkBitmap
*, const Resource
*);
251 bool transferResource(WebKit::WebGraphicsContext3D
*, ResourceId
, TransferableResource
*);
252 void trimMailboxDeque();
253 void deleteResourceInternal(ResourceMap::iterator it
);
255 CCGraphicsContext
* m_context
;
257 ResourceMap m_resources
;
261 Deque
<Mailbox
> m_mailboxes
;
263 ResourceType m_defaultResourceType
;
264 bool m_useTextureStorageExt
;
265 bool m_useTextureUsageHint
;
266 bool m_useShallowFlush
;
267 OwnPtr
<LayerTextureSubImage
> m_texSubImage
;
268 OwnPtr
<TextureUploader
> m_textureUploader
;
269 OwnPtr
<AcceleratedTextureCopier
> m_textureCopier
;
270 int m_maxTextureSize
;
272 DISALLOW_COPY_AND_ASSIGN(CCResourceProvider
);