Implement android_webview url intercepting.
[chromium-blink-merge.git] / cc / resource_provider.h
blobb3595281c7f8b94a5448b252b8d4b4857a1be510
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"
12 #include "IntSize.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>
23 namespace WebKit {
24 class WebGraphicsContext3D;
27 namespace cc {
29 class IntRect;
30 class LayerTextureSubImage;
31 class TextureCopier;
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 {
37 public:
38 typedef unsigned ResourceId;
39 typedef Vector<ResourceId> ResourceIdArray;
40 typedef base::hash_map<ResourceId, ResourceId> ResourceIdMap;
41 enum TextureUsageHint { TextureUsageAny, TextureUsageFramebuffer };
42 enum ResourceType {
43 GLTexture = 1,
44 Bitmap,
46 struct Mailbox {
47 GC3Dbyte name[64];
49 struct TransferableResource {
50 unsigned id;
51 GC3Denum format;
52 IntSize size;
53 Mailbox mailbox;
55 typedef Vector<TransferableResource> TransferableResourceArray;
56 struct TransferableResourceList {
57 TransferableResourceList();
58 ~TransferableResourceList();
60 TransferableResourceArray resources;
61 unsigned syncPoint;
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.
103 void flush();
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
122 // "in use".
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
136 // will wait on it.
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
142 // will wait on it.
143 void receiveFromParent(const TransferableResourceList&);
145 // Only for testing
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
151 // by assertions.
152 class ScopedReadLockGL {
153 public:
154 ScopedReadLockGL(CCResourceProvider*, CCResourceProvider::ResourceId);
155 ~ScopedReadLockGL();
157 unsigned textureId() const { return m_textureId; }
159 private:
160 CCResourceProvider* m_resourceProvider;
161 CCResourceProvider::ResourceId m_resourceId;
162 unsigned m_textureId;
164 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL);
167 class ScopedWriteLockGL {
168 public:
169 ScopedWriteLockGL(CCResourceProvider*, CCResourceProvider::ResourceId);
170 ~ScopedWriteLockGL();
172 unsigned textureId() const { return m_textureId; }
174 private:
175 CCResourceProvider* m_resourceProvider;
176 CCResourceProvider::ResourceId m_resourceId;
177 unsigned m_textureId;
179 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL);
182 class ScopedReadLockSoftware {
183 public:
184 ScopedReadLockSoftware(CCResourceProvider*, CCResourceProvider::ResourceId);
185 ~ScopedReadLockSoftware();
187 const SkBitmap* skBitmap() const { return &m_skBitmap; }
189 private:
190 CCResourceProvider* m_resourceProvider;
191 CCResourceProvider::ResourceId m_resourceId;
192 SkBitmap m_skBitmap;
194 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware);
197 class ScopedWriteLockSoftware {
198 public:
199 ScopedWriteLockSoftware(CCResourceProvider*, CCResourceProvider::ResourceId);
200 ~ScopedWriteLockSoftware();
202 SkCanvas* skCanvas() { return m_skCanvas.get(); }
204 private:
205 CCResourceProvider* m_resourceProvider;
206 CCResourceProvider::ResourceId m_resourceId;
207 SkBitmap m_skBitmap;
208 OwnPtr<SkCanvas> m_skCanvas;
210 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware);
213 private:
214 struct Resource {
215 Resource();
216 Resource(unsigned textureId, int pool, const IntSize& size, GC3Denum format);
217 Resource(uint8_t* pixels, int pool, const IntSize& size, GC3Denum format);
219 unsigned glId;
220 uint8_t* pixels;
221 int pool;
222 int lockForReadCount;
223 bool lockedForWrite;
224 bool external;
225 bool exported;
226 bool markedForDeletion;
227 IntSize size;
228 GC3Denum format;
229 ResourceType type;
231 typedef base::hash_map<ResourceId, Resource> ResourceMap;
232 struct Child {
233 Child();
234 ~Child();
236 int pool;
237 ResourceIdMap childToParentMap;
238 ResourceIdMap parentToChildMap;
240 typedef base::hash_map<int, Child> ChildMap;
242 explicit CCResourceProvider(CCGraphicsContext*);
243 bool initialize();
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;
256 ResourceId m_nextId;
257 ResourceMap m_resources;
258 int m_nextChild;
259 ChildMap m_children;
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);
277 #endif