OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CCResourceProvider_h |
| 6 #define CCResourceProvider_h |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "CCGraphicsContext.h" |
| 10 #include "GraphicsContext3D.h" |
| 11 #include "IntSize.h" |
| 12 #include "SkBitmap.h" |
| 13 #include "SkCanvas.h" |
| 14 #include "TextureCopier.h" |
| 15 #include <wtf/Deque.h> |
| 16 #include <wtf/HashMap.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> |
| 22 |
| 23 namespace WebKit { |
| 24 class WebGraphicsContext3D; |
| 25 } |
| 26 |
| 27 namespace cc { |
| 28 |
| 29 class IntRect; |
| 30 class LayerTextureSubImage; |
| 31 class TextureCopier; |
| 32 class TextureUploader; |
| 33 |
| 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 HashMap<ResourceId, ResourceId> ResourceIdMap; |
| 41 enum TextureUsageHint { TextureUsageAny, TextureUsageFramebuffer }; |
| 42 enum ResourceType { |
| 43 GLTexture = 1, |
| 44 Bitmap, |
| 45 }; |
| 46 struct Mailbox { |
| 47 GC3Dbyte name[64]; |
| 48 }; |
| 49 struct TransferableResource { |
| 50 unsigned id; |
| 51 GC3Denum format; |
| 52 IntSize size; |
| 53 Mailbox mailbox; |
| 54 }; |
| 55 typedef Vector<TransferableResource> TransferableResourceArray; |
| 56 struct TransferableResourceList { |
| 57 TransferableResourceList(); |
| 58 ~TransferableResourceList(); |
| 59 |
| 60 TransferableResourceArray resources; |
| 61 unsigned syncPoint; |
| 62 }; |
| 63 |
| 64 static PassOwnPtr<CCResourceProvider> create(CCGraphicsContext*); |
| 65 |
| 66 virtual ~CCResourceProvider(); |
| 67 |
| 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(); } |
| 73 |
| 74 // Checks whether a resource is in use by a consumer. |
| 75 bool inUseByConsumer(ResourceId); |
| 76 |
| 77 |
| 78 // Producer interface. |
| 79 |
| 80 void setDefaultResourceType(ResourceType type) { m_defaultResourceType = typ
e; } |
| 81 ResourceType defaultResourceType() const { return m_defaultResourceType; } |
| 82 ResourceType resourceType(ResourceId); |
| 83 |
| 84 // Creates a resource of the default resource type. |
| 85 ResourceId createResource(int pool, const IntSize&, GC3Denum format, Texture
UsageHint); |
| 86 |
| 87 // You can also explicitly create a specific resource type. |
| 88 ResourceId createGLTexture(int pool, const IntSize&, GC3Denum format, Textur
eUsageHint); |
| 89 ResourceId createBitmap(int pool, const IntSize&); |
| 90 // Wraps an external texture into a GL resource. |
| 91 ResourceId createResourceFromExternalTexture(unsigned textureId); |
| 92 |
| 93 void deleteResource(ResourceId); |
| 94 |
| 95 // Deletes all resources owned by a given pool. |
| 96 void deleteOwnedResources(int pool); |
| 97 |
| 98 // Upload data from image, copying sourceRect (in image) into destRect (in t
he resource). |
| 99 void upload(ResourceId, const uint8_t* image, const IntRect& imageRect, cons
t IntRect& sourceRect, const IntSize& destOffset); |
| 100 |
| 101 // Flush all context operations, kicking uploads and ensuring ordering with |
| 102 // respect to other contexts. |
| 103 void flush(); |
| 104 |
| 105 // Only flush the command buffer if supported. |
| 106 // Returns true if the shallow flush occurred, false otherwise. |
| 107 bool shallowFlushIfSupported(); |
| 108 |
| 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); |
| 112 |
| 113 // Destroys accounting for the child, deleting all resources from that pool. |
| 114 void destroyChild(int child); |
| 115 |
| 116 // Gets the child->parent resource ID map. |
| 117 const ResourceIdMap& getChildToParentMap(int child) const; |
| 118 |
| 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&); |
| 124 |
| 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
&); |
| 131 |
| 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&); |
| 138 |
| 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&); |
| 144 |
| 145 // Only for testing |
| 146 size_t mailboxCount() const { return m_mailboxes.size(); } |
| 147 |
| 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(); |
| 156 |
| 157 unsigned textureId() const { return m_textureId; } |
| 158 |
| 159 private: |
| 160 CCResourceProvider* m_resourceProvider; |
| 161 CCResourceProvider::ResourceId m_resourceId; |
| 162 unsigned m_textureId; |
| 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL); |
| 165 }; |
| 166 |
| 167 class ScopedWriteLockGL { |
| 168 public: |
| 169 ScopedWriteLockGL(CCResourceProvider*, CCResourceProvider::ResourceId); |
| 170 ~ScopedWriteLockGL(); |
| 171 |
| 172 unsigned textureId() const { return m_textureId; } |
| 173 |
| 174 private: |
| 175 CCResourceProvider* m_resourceProvider; |
| 176 CCResourceProvider::ResourceId m_resourceId; |
| 177 unsigned m_textureId; |
| 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL); |
| 180 }; |
| 181 |
| 182 class ScopedReadLockSoftware { |
| 183 public: |
| 184 ScopedReadLockSoftware(CCResourceProvider*, CCResourceProvider::Resource
Id); |
| 185 ~ScopedReadLockSoftware(); |
| 186 |
| 187 const SkBitmap* skBitmap() const { return &m_skBitmap; } |
| 188 |
| 189 private: |
| 190 CCResourceProvider* m_resourceProvider; |
| 191 CCResourceProvider::ResourceId m_resourceId; |
| 192 SkBitmap m_skBitmap; |
| 193 |
| 194 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware); |
| 195 }; |
| 196 |
| 197 class ScopedWriteLockSoftware { |
| 198 public: |
| 199 ScopedWriteLockSoftware(CCResourceProvider*, CCResourceProvider::Resourc
eId); |
| 200 ~ScopedWriteLockSoftware(); |
| 201 |
| 202 SkCanvas* skCanvas() { return m_skCanvas.get(); } |
| 203 |
| 204 private: |
| 205 CCResourceProvider* m_resourceProvider; |
| 206 CCResourceProvider::ResourceId m_resourceId; |
| 207 SkBitmap m_skBitmap; |
| 208 OwnPtr<SkCanvas> m_skCanvas; |
| 209 |
| 210 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware); |
| 211 }; |
| 212 |
| 213 private: |
| 214 struct Resource { |
| 215 Resource(); |
| 216 Resource(unsigned textureId, int pool, const IntSize& size, GC3Denum for
mat); |
| 217 Resource(uint8_t* pixels, int pool, const IntSize& size, GC3Denum format
); |
| 218 |
| 219 unsigned glId; |
| 220 uint8_t* pixels; |
| 221 int pool; |
| 222 int lockForReadCount; |
| 223 bool lockedForWrite; |
| 224 bool external; |
| 225 bool exported; |
| 226 IntSize size; |
| 227 GC3Denum format; |
| 228 ResourceType type; |
| 229 }; |
| 230 typedef HashMap<ResourceId, Resource> ResourceMap; |
| 231 struct Child { |
| 232 Child(); |
| 233 ~Child(); |
| 234 |
| 235 int pool; |
| 236 ResourceIdMap childToParentMap; |
| 237 ResourceIdMap parentToChildMap; |
| 238 }; |
| 239 typedef HashMap<int, Child> ChildMap; |
| 240 |
| 241 explicit CCResourceProvider(CCGraphicsContext*); |
| 242 bool initialize(); |
| 243 |
| 244 const Resource* lockForRead(ResourceId); |
| 245 void unlockForRead(ResourceId); |
| 246 const Resource* lockForWrite(ResourceId); |
| 247 void unlockForWrite(ResourceId); |
| 248 static void populateSkBitmapWithResource(SkBitmap*, const Resource*); |
| 249 |
| 250 bool transferResource(WebKit::WebGraphicsContext3D*, ResourceId, Transferabl
eResource*); |
| 251 void trimMailboxDeque(); |
| 252 |
| 253 CCGraphicsContext* m_context; |
| 254 ResourceId m_nextId; |
| 255 ResourceMap m_resources; |
| 256 int m_nextChild; |
| 257 ChildMap m_children; |
| 258 |
| 259 Deque<Mailbox> m_mailboxes; |
| 260 |
| 261 ResourceType m_defaultResourceType; |
| 262 bool m_useTextureStorageExt; |
| 263 bool m_useTextureUsageHint; |
| 264 bool m_useShallowFlush; |
| 265 OwnPtr<LayerTextureSubImage> m_texSubImage; |
| 266 OwnPtr<TextureUploader> m_textureUploader; |
| 267 OwnPtr<AcceleratedTextureCopier> m_textureCopier; |
| 268 int m_maxTextureSize; |
| 269 |
| 270 DISALLOW_COPY_AND_ASSIGN(CCResourceProvider); |
| 271 }; |
| 272 |
| 273 } |
| 274 |
| 275 #endif |
OLD | NEW |