Don't consider a Bluetooth adapter present until it has an address.
[chromium-blink-merge.git] / cc / CCResourceProvider.h
blob836868364e7235780d0cb7aab2ce8324be6f84b7
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"
11 #include "IntSize.h"
12 #include "SkBitmap.h"
13 #include "SkCanvas.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>
22 namespace WebKit {
23 class WebGraphicsContext3D;
26 namespace WebCore {
28 class IntRect;
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);
35 public:
36 typedef unsigned ResourceId;
37 typedef Vector<ResourceId> ResourceIdArray;
38 typedef HashMap<ResourceId, ResourceId> ResourceIdMap;
39 enum TextureUsageHint { TextureUsageAny, TextureUsageFramebuffer };
40 enum ResourceType {
41 GLTexture = 1,
42 Bitmap,
44 struct Mailbox {
45 GC3Dbyte name[64];
47 struct TransferableResource {
48 unsigned id;
49 GC3Denum format;
50 IntSize size;
51 Mailbox mailbox;
53 typedef Vector<TransferableResource> TransferableResourceArray;
54 struct TransferableResourceList {
55 TransferableResourceArray resources;
56 unsigned syncPoint;
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.
96 void flush();
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
115 // "in use".
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
129 // will wait on it.
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
135 // will wait on it.
136 void receiveFromParent(const TransferableResourceList&);
138 // Only for testing
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
144 // by assertions.
145 class ScopedReadLockGL {
146 WTF_MAKE_NONCOPYABLE(ScopedReadLockGL);
147 public:
148 ScopedReadLockGL(CCResourceProvider*, CCResourceProvider::ResourceId);
149 ~ScopedReadLockGL();
151 unsigned textureId() const { return m_textureId; }
153 private:
154 CCResourceProvider* m_resourceProvider;
155 CCResourceProvider::ResourceId m_resourceId;
156 unsigned m_textureId;
159 class ScopedWriteLockGL {
160 WTF_MAKE_NONCOPYABLE(ScopedWriteLockGL);
161 public:
162 ScopedWriteLockGL(CCResourceProvider*, CCResourceProvider::ResourceId);
163 ~ScopedWriteLockGL();
165 unsigned textureId() const { return m_textureId; }
167 private:
168 CCResourceProvider* m_resourceProvider;
169 CCResourceProvider::ResourceId m_resourceId;
170 unsigned m_textureId;
173 class ScopedReadLockSoftware {
174 WTF_MAKE_NONCOPYABLE(ScopedReadLockSoftware);
175 public:
176 ScopedReadLockSoftware(CCResourceProvider*, CCResourceProvider::ResourceId);
177 ~ScopedReadLockSoftware();
179 const SkBitmap* skBitmap() const { return &m_skBitmap; }
181 private:
182 CCResourceProvider* m_resourceProvider;
183 CCResourceProvider::ResourceId m_resourceId;
184 SkBitmap m_skBitmap;
187 class ScopedWriteLockSoftware {
188 WTF_MAKE_NONCOPYABLE(ScopedWriteLockSoftware);
189 public:
190 ScopedWriteLockSoftware(CCResourceProvider*, CCResourceProvider::ResourceId);
191 ~ScopedWriteLockSoftware();
193 SkCanvas* skCanvas() { return &m_skCanvas; }
195 private:
196 CCResourceProvider* m_resourceProvider;
197 CCResourceProvider::ResourceId m_resourceId;
198 SkBitmap m_skBitmap;
199 SkCanvas m_skCanvas;
202 private:
203 struct Resource {
204 Resource()
205 : glId(0)
206 , pixels(0)
207 , pool(0)
208 , lockForReadCount(0)
209 , lockedForWrite(false)
210 , external(false)
211 , exported(false)
212 , size()
213 , format(0)
214 , type(static_cast<ResourceType>(0))
216 Resource(unsigned textureId, int pool, const IntSize& size, GC3Denum format)
217 : glId(textureId)
218 , pixels(0)
219 , pool(pool)
220 , lockForReadCount(0)
221 , lockedForWrite(false)
222 , external(false)
223 , exported(false)
224 , size(size)
225 , format(format)
226 , type(GLTexture)
228 Resource(uint8_t* pixels, int pool, const IntSize& size, GC3Denum format)
229 : glId(0)
230 , pixels(pixels)
231 , pool(pool)
232 , lockForReadCount(0)
233 , lockedForWrite(false)
234 , external(false)
235 , exported(false)
236 , size(size)
237 , format(format)
238 , type(Bitmap)
240 unsigned glId;
241 uint8_t* pixels;
242 int pool;
243 int lockForReadCount;
244 bool lockedForWrite;
245 bool external;
246 bool exported;
247 IntSize size;
248 GC3Denum format;
249 ResourceType type;
251 typedef HashMap<ResourceId, Resource> ResourceMap;
252 struct Child {
253 int pool;
254 ResourceIdMap childToParentMap;
255 ResourceIdMap parentToChildMap;
257 typedef HashMap<int, Child> ChildMap;
259 explicit CCResourceProvider(CCGraphicsContext*);
260 bool initialize();
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;
272 ResourceId m_nextId;
273 ResourceMap m_resources;
274 int m_nextChild;
275 ChildMap m_children;
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;
289 #endif