Add Sad Tab resources to the iOS build.
[chromium-blink-merge.git] / cc / resource_provider.h
blobc0ccc5c328987a993175e472076467268ace5787
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_RESOURCE_PROVIDER_H_
6 #define CC_RESOURCE_PROVIDER_H_
8 #include "base/basictypes.h"
9 #include "base/hash_tables.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/threading/thread_checker.h"
12 #include "cc/cc_export.h"
13 #include "cc/graphics_context.h"
14 #include "cc/texture_copier.h"
15 #include "cc/transferable_resource.h"
16 #include "third_party/khronos/GLES2/gl2.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "third_party/skia/include/core/SkCanvas.h"
19 #include "ui/gfx/size.h"
20 #include <deque>
21 #include <vector>
23 namespace WebKit {
24 class WebGraphicsContext3D;
27 namespace gfx {
28 class Rect;
29 class Vector2d;
32 namespace cc {
34 class TextureUploader;
36 // This class is not thread-safe and can only be called from the thread it was
37 // created on (in practice, the impl thread).
38 class CC_EXPORT ResourceProvider {
39 public:
40 typedef unsigned ResourceId;
41 typedef std::vector<ResourceId> ResourceIdArray;
42 typedef base::hash_map<ResourceId, ResourceId> ResourceIdMap;
43 enum TextureUsageHint { TextureUsageAny, TextureUsageFramebuffer };
44 enum ResourceType {
45 GLTexture = 1,
46 Bitmap,
49 static scoped_ptr<ResourceProvider> create(GraphicsContext*);
51 virtual ~ResourceProvider();
53 WebKit::WebGraphicsContext3D* graphicsContext3D();
54 TextureCopier* textureCopier() const { return m_textureCopier.get(); }
55 int maxTextureSize() const { return m_maxTextureSize; }
56 unsigned numResources() const { return m_resources.size(); }
58 // Checks whether a resource is in use by a consumer.
59 bool inUseByConsumer(ResourceId);
62 // Producer interface.
64 void setDefaultResourceType(ResourceType type) { m_defaultResourceType = type; }
65 ResourceType defaultResourceType() const { return m_defaultResourceType; }
66 ResourceType resourceType(ResourceId);
68 // Creates a resource of the default resource type.
69 ResourceId createResource(int pool, const gfx::Size&, GLenum format, TextureUsageHint);
71 // You can also explicitly create a specific resource type.
72 ResourceId createGLTexture(int pool, const gfx::Size&, GLenum format, TextureUsageHint);
73 ResourceId createBitmap(int pool, const gfx::Size&);
74 // Wraps an external texture into a GL resource.
75 ResourceId createResourceFromExternalTexture(unsigned textureId);
77 void deleteResource(ResourceId);
79 // Deletes all resources owned by a given pool.
80 void deleteOwnedResources(int pool);
82 // Update pixels from image, copying sourceRect (in image) into destRect (in the resource).
83 void setPixels(ResourceId, const uint8_t* image, const gfx::Rect& imageRect, const gfx::Rect& sourceRect, const gfx::Vector2d& destOffset);
85 // Check upload status.
86 size_t numBlockingUploads();
87 void markPendingUploadsAsNonBlocking();
88 double estimatedUploadsPerSecond();
89 void flushUploads();
91 // Flush all context operations, kicking uploads and ensuring ordering with
92 // respect to other contexts.
93 void flush();
95 // Only flush the command buffer if supported.
96 // Returns true if the shallow flush occurred, false otherwise.
97 bool shallowFlushIfSupported();
99 // Creates accounting for a child, and associate it with a pool. Resources
100 // transfered from that child will go to that pool. Returns a child ID.
101 int createChild(int pool);
103 // Destroys accounting for the child, deleting all resources from that pool.
104 void destroyChild(int child);
106 // Gets the child->parent resource ID map.
107 const ResourceIdMap& getChildToParentMap(int child) const;
109 // Prepares resources to be transfered to the parent, moving them to
110 // mailboxes and serializing meta-data into TransferableResources.
111 // Resources are not removed from the ResourceProvider, but are marked as
112 // "in use".
113 void prepareSendToParent(const ResourceIdArray&, TransferableResourceList*);
115 // Prepares resources to be transfered back to the child, moving them to
116 // mailboxes and serializing meta-data into TransferableResources.
117 // Resources are removed from the ResourceProvider. Note: the resource IDs
118 // passed are in the parent namespace and will be translated to the child
119 // namespace when returned.
120 void prepareSendToChild(int child, const ResourceIdArray&, TransferableResourceList*);
122 // Receives resources from a child, moving them from mailboxes. Resource IDs
123 // passed are in the child namespace, and will be translated to the parent
124 // namespace, added to the child->parent map.
125 // NOTE: if the syncPoint filed in TransferableResourceList is set, this
126 // will wait on it.
127 void receiveFromChild(int child, const TransferableResourceList&);
129 // Receives resources from the parent, moving them from mailboxes. Resource IDs
130 // passed are in the child namespace.
131 // NOTE: if the syncPoint filed in TransferableResourceList is set, this
132 // will wait on it.
133 void receiveFromParent(const TransferableResourceList&);
135 // Only for testing
136 size_t mailboxCount() const { return m_mailboxes.size(); }
138 // The following lock classes are part of the ResourceProvider API and are
139 // needed to read and write the resource contents. The user must ensure
140 // that they only use GL locks on GL resources, etc, and this is enforced
141 // by assertions.
142 class CC_EXPORT ScopedReadLockGL {
143 public:
144 ScopedReadLockGL(ResourceProvider*, ResourceProvider::ResourceId);
145 ~ScopedReadLockGL();
147 unsigned textureId() const { return m_textureId; }
149 private:
150 ResourceProvider* m_resourceProvider;
151 ResourceProvider::ResourceId m_resourceId;
152 unsigned m_textureId;
154 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL);
157 class CC_EXPORT ScopedWriteLockGL {
158 public:
159 ScopedWriteLockGL(ResourceProvider*, ResourceProvider::ResourceId);
160 ~ScopedWriteLockGL();
162 unsigned textureId() const { return m_textureId; }
164 private:
165 ResourceProvider* m_resourceProvider;
166 ResourceProvider::ResourceId m_resourceId;
167 unsigned m_textureId;
169 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL);
172 class CC_EXPORT ScopedReadLockSoftware {
173 public:
174 ScopedReadLockSoftware(ResourceProvider*, ResourceProvider::ResourceId);
175 ~ScopedReadLockSoftware();
177 const SkBitmap* skBitmap() const { return &m_skBitmap; }
179 private:
180 ResourceProvider* m_resourceProvider;
181 ResourceProvider::ResourceId m_resourceId;
182 SkBitmap m_skBitmap;
184 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware);
187 class CC_EXPORT ScopedWriteLockSoftware {
188 public:
189 ScopedWriteLockSoftware(ResourceProvider*, ResourceProvider::ResourceId);
190 ~ScopedWriteLockSoftware();
192 SkCanvas* skCanvas() { return m_skCanvas.get(); }
194 private:
195 ResourceProvider* m_resourceProvider;
196 ResourceProvider::ResourceId m_resourceId;
197 SkBitmap m_skBitmap;
198 scoped_ptr<SkCanvas> m_skCanvas;
200 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware);
203 // Temporary functions for debugging crashes in issue 151428 in canary.
204 // Do not use these!
205 static void debugNotifyEnterZone(unsigned int index);
206 static void debugNotifyLeaveZone();
208 private:
209 struct Resource {
210 Resource();
211 Resource(unsigned textureId, int pool, const gfx::Size& size, GLenum format);
212 Resource(uint8_t* pixels, int pool, const gfx::Size& size, GLenum format);
214 unsigned glId;
215 uint8_t* pixels;
216 int pool;
217 int lockForReadCount;
218 bool lockedForWrite;
219 bool external;
220 bool exported;
221 bool markedForDeletion;
222 gfx::Size size;
223 GLenum format;
224 ResourceType type;
226 typedef base::hash_map<ResourceId, Resource> ResourceMap;
227 struct Child {
228 Child();
229 ~Child();
231 int pool;
232 ResourceIdMap childToParentMap;
233 ResourceIdMap parentToChildMap;
235 typedef base::hash_map<int, Child> ChildMap;
237 explicit ResourceProvider(GraphicsContext*);
238 bool initialize();
240 const Resource* lockForRead(ResourceId);
241 void unlockForRead(ResourceId);
242 const Resource* lockForWrite(ResourceId);
243 void unlockForWrite(ResourceId);
244 static void populateSkBitmapWithResource(SkBitmap*, const Resource*);
246 bool transferResource(WebKit::WebGraphicsContext3D*, ResourceId, TransferableResource*);
247 void trimMailboxDeque();
248 void deleteResourceInternal(ResourceMap::iterator it);
250 GraphicsContext* m_context;
251 ResourceId m_nextId;
252 ResourceMap m_resources;
253 int m_nextChild;
254 ChildMap m_children;
256 std::deque<Mailbox> m_mailboxes;
258 ResourceType m_defaultResourceType;
259 bool m_useTextureStorageExt;
260 bool m_useTextureUsageHint;
261 bool m_useShallowFlush;
262 scoped_ptr<TextureUploader> m_textureUploader;
263 scoped_ptr<AcceleratedTextureCopier> m_textureCopier;
264 int m_maxTextureSize;
266 base::ThreadChecker m_threadChecker;
268 DISALLOW_COPY_AND_ASSIGN(ResourceProvider);
273 #endif // CC_RESOURCE_PROVIDER_H_