| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrLayerCache_DEFINED | 8 #ifndef GrLayerCache_DEFINED |
| 9 #define GrLayerCache_DEFINED | 9 #define GrLayerCache_DEFINED |
| 10 | 10 |
| 11 #define USE_ATLAS 0 | 11 #define USE_ATLAS 0 |
| 12 | 12 |
| 13 #include "GrAllocPool.h" | 13 #include "GrAllocPool.h" |
| 14 #include "GrAtlas.h" | 14 #include "GrAtlas.h" |
| 15 #include "GrPictureUtils.h" | 15 #include "GrPictureUtils.h" |
| 16 #include "GrRect.h" | 16 #include "GrRect.h" |
| 17 #include "SkChecksum.h" | 17 #include "SkChecksum.h" |
| 18 #include "SkGpuDevice.h" |
| 18 #include "SkTDynamicHash.h" | 19 #include "SkTDynamicHash.h" |
| 20 #include "SkMessageBus.h" |
| 19 | 21 |
| 20 class SkPicture; | 22 class SkPicture; |
| 21 | 23 |
| 24 // The layer cache listens for these messages to purge picture-related resources
. |
| 25 struct GrPictureDeletedMessage { |
| 26 uint32_t pictureID; |
| 27 }; |
| 28 |
| 29 class GrPictureDeletionListener : public SkPicture::DeletionListener { |
| 30 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{ |
| 31 const GrPictureDeletedMessage message = { pictureID }; |
| 32 SkMessageBus<GrPictureDeletedMessage>::Post(message); |
| 33 } |
| 34 }; |
| 35 |
| 22 // GrPictureInfo stores the atlas plots used by a single picture. A single | 36 // GrPictureInfo stores the atlas plots used by a single picture. A single |
| 23 // plot may be used to store layers from multiple pictures. | 37 // plot may be used to store layers from multiple pictures. |
| 24 struct GrPictureInfo { | 38 struct GrPictureInfo { |
| 25 public: | 39 public: |
| 26 // for SkTDynamicHash - just use the pictureID as the hash key | 40 // for SkTDynamicHash - just use the pictureID as the hash key |
| 27 static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictIn
fo.fPictureID; } | 41 static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictIn
fo.fPictureID; } |
| 28 static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); } | 42 static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); } |
| 29 | 43 |
| 30 // GrPictureInfo proper | 44 // GrPictureInfo proper |
| 31 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { } | 45 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 153 |
| 140 // Inform the cache that layer's cached image is now required. Return true | 154 // Inform the cache that layer's cached image is now required. Return true |
| 141 // if it was found in the ResourceCache and doesn't need to be regenerated. | 155 // if it was found in the ResourceCache and doesn't need to be regenerated. |
| 142 // If false is returned the caller should (re)render the layer into the | 156 // If false is returned the caller should (re)render the layer into the |
| 143 // newly acquired texture. | 157 // newly acquired texture. |
| 144 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc); | 158 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc); |
| 145 | 159 |
| 146 // Inform the cache that layer's cached image is not currently required | 160 // Inform the cache that layer's cached image is not currently required |
| 147 void unlock(GrCachedLayer* layer); | 161 void unlock(GrCachedLayer* layer); |
| 148 | 162 |
| 149 // Remove all the layers (and unlock any resources) associated with 'picture
' | 163 // Cleanup after any SkPicture deletions |
| 150 void purge(const SkPicture* picture); | 164 void processDeletedPictures(); |
| 151 | 165 |
| 152 SkDEBUGCODE(void validate() const;) | 166 SkDEBUGCODE(void validate() const;) |
| 153 | 167 |
| 154 private: | 168 private: |
| 155 static const int kNumPlotsX = 2; | 169 static const int kNumPlotsX = 2; |
| 156 static const int kNumPlotsY = 2; | 170 static const int kNumPlotsY = 2; |
| 157 | 171 |
| 158 GrContext* fContext; // pointer back to owning context | 172 GrContext* fContext; // pointer back to owning context |
| 159 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate | 173 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate |
| 160 | 174 |
| 161 // We cache this information here (rather then, say, on the owning picture) | 175 // We cache this information here (rather then, say, on the owning picture) |
| 162 // because we want to be able to clean it up as needed (e.g., if a picture | 176 // because we want to be able to clean it up as needed (e.g., if a picture |
| 163 // is leaked and never cleans itself up we still want to be able to | 177 // is leaked and never cleans itself up we still want to be able to |
| 164 // remove the GrPictureInfo once its layers are purged from all the atlas | 178 // remove the GrPictureInfo once its layers are purged from all the atlas |
| 165 // plots). | 179 // plots). |
| 166 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash; | 180 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash; |
| 167 | 181 |
| 168 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash; | 182 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash; |
| 169 | 183 |
| 184 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox; |
| 185 |
| 170 void initAtlas(); | 186 void initAtlas(); |
| 171 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); | 187 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); |
| 172 | 188 |
| 189 // Remove all the layers (and unlock any resources) associated with 'picture
ID' |
| 190 void purge(uint32_t pictureID); |
| 191 |
| 173 // for testing | 192 // for testing |
| 174 friend class GetNumLayers; | 193 friend class TestingAccess; |
| 175 int numLayers() const { return fLayerHash.count(); } | 194 int numLayers() const { return fLayerHash.count(); } |
| 176 }; | 195 }; |
| 177 | 196 |
| 178 #endif | 197 #endif |
| OLD | NEW |