| 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 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 // get a ref to the GrTexture in which they reside. In both cases 'fRect' | 44 // get a ref to the GrTexture in which they reside. In both cases 'fRect' |
| 45 // contains the layer's extent in its texture. | 45 // contains the layer's extent in its texture. |
| 46 // Atlased layers also get a pointer to the plot in which they reside. | 46 // Atlased layers also get a pointer to the plot in which they reside. |
| 47 // For non-atlased layers, the lock field just corresponds to locking in | 47 // For non-atlased layers, the lock field just corresponds to locking in |
| 48 // the resource cache. For atlased layers, it implements an additional level | 48 // the resource cache. For atlased layers, it implements an additional level |
| 49 // of locking to allow atlased layers to be reused multiple times. | 49 // of locking to allow atlased layers to be reused multiple times. |
| 50 struct GrCachedLayer { | 50 struct GrCachedLayer { |
| 51 public: | 51 public: |
| 52 // For SkTDynamicHash | 52 // For SkTDynamicHash |
| 53 struct Key { | 53 struct Key { |
| 54 Key(uint32_t pictureID, int start, int stop, const SkMatrix& ctm) | 54 Key(uint32_t pictureID, int start, int stop, const SkIPoint& offset, con
st SkMatrix& ctm) |
| 55 : fPictureID(pictureID) | 55 : fPictureID(pictureID) |
| 56 , fStart(start) | 56 , fStart(start) |
| 57 , fStop(stop) | 57 , fStop(stop) |
| 58 , fOffset(offset) |
| 58 , fCTM(ctm) { | 59 , fCTM(ctm) { |
| 59 fCTM.getType(); // force initialization of type so hashes match | 60 fCTM.getType(); // force initialization of type so hashes match |
| 60 | 61 |
| 61 // Key needs to be tightly packed. | 62 // Key needs to be tightly packed. |
| 62 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + 2 * sizeof(int) +
| 63 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + 2 * sizeof(int) +
|
| 64 2 * sizeof(int32_t) + |
| 63 9 * sizeof(SkScalar) + sizeof(uint32
_t)); | 65 9 * sizeof(SkScalar) + sizeof(uint32
_t)); |
| 64 } | 66 } |
| 65 | 67 |
| 66 bool operator==(const Key& other) const { | 68 bool operator==(const Key& other) const { |
| 67 return fPictureID == other.fPictureID && | 69 return fPictureID == other.fPictureID && |
| 68 fStart == other.fStart && | 70 fStart == other.fStart && |
| 69 fStop == other.fStop && | 71 fStop == other.fStop && |
| 72 fOffset == other.fOffset && |
| 70 fCTM.cheapEqualTo(other.fCTM); | 73 fCTM.cheapEqualTo(other.fCTM); |
| 71 } | 74 } |
| 72 | 75 |
| 73 uint32_t pictureID() const { return fPictureID; } | 76 uint32_t pictureID() const { return fPictureID; } |
| 74 int start() const { return fStart; } | 77 int start() const { return fStart; } |
| 75 int stop() const { return fStop; } | 78 int stop() const { return fStop; } |
| 79 const SkIPoint& offset() const { return fOffset; } |
| 76 const SkMatrix& ctm() const { return fCTM; } | 80 const SkMatrix& ctm() const { return fCTM; } |
| 77 | 81 |
| 78 private: | 82 private: |
| 79 // ID of the picture of which this layer is a part | 83 // ID of the picture of which this layer is a part |
| 80 const uint32_t fPictureID; | 84 const uint32_t fPictureID; |
| 81 // The range of commands in the picture this layer represents | 85 // The range of commands in the picture this layer represents |
| 82 const int fStart; | 86 const int fStart; |
| 83 const int fStop; | 87 const int fStop; |
| 88 // The offset of the layer in device space |
| 89 const SkIPoint fOffset; |
| 84 // The CTM applied to this layer in the picture | 90 // The CTM applied to this layer in the picture |
| 85 SkMatrix fCTM; | 91 SkMatrix fCTM; |
| 86 }; | 92 }; |
| 87 | 93 |
| 88 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } | 94 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } |
| 89 static uint32_t Hash(const Key& key) { | 95 static uint32_t Hash(const Key& key) { |
| 90 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), size
of(Key)); | 96 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), size
of(Key)); |
| 91 } | 97 } |
| 92 | 98 |
| 93 // GrCachedLayer proper | 99 // GrCachedLayer proper |
| 94 GrCachedLayer(uint32_t pictureID, int start, int stop, const SkMatrix& ctm) | 100 GrCachedLayer(uint32_t pictureID, int start, int stop, |
| 95 : fKey(pictureID, start, stop, ctm) | 101 const SkIPoint& offset, const SkMatrix& ctm) |
| 102 : fKey(pictureID, start, stop, offset, ctm) |
| 96 , fTexture(NULL) | 103 , fTexture(NULL) |
| 97 , fRect(GrIRect16::MakeEmpty()) | 104 , fRect(GrIRect16::MakeEmpty()) |
| 98 , fPlot(NULL) | 105 , fPlot(NULL) |
| 99 , fLocked(false) { | 106 , fLocked(false) { |
| 100 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0); | 107 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0); |
| 101 } | 108 } |
| 102 | 109 |
| 103 ~GrCachedLayer() { | 110 ~GrCachedLayer() { |
| 104 SkSafeUnref(fTexture); | 111 SkSafeUnref(fTexture); |
| 105 } | 112 } |
| 106 | 113 |
| 107 uint32_t pictureID() const { return fKey.pictureID(); } | 114 uint32_t pictureID() const { return fKey.pictureID(); } |
| 108 int start() const { return fKey.start(); } | 115 int start() const { return fKey.start(); } |
| 109 int stop() const { return fKey.stop(); } | 116 int stop() const { return fKey.stop(); } |
| 117 const SkIPoint& offset() const { return fKey.offset(); } |
| 110 const SkMatrix& ctm() const { return fKey.ctm(); } | 118 const SkMatrix& ctm() const { return fKey.ctm(); } |
| 111 | 119 |
| 112 void setTexture(GrTexture* texture, const GrIRect16& rect) { | 120 void setTexture(GrTexture* texture, const GrIRect16& rect) { |
| 113 SkRefCnt_SafeAssign(fTexture, texture); | 121 SkRefCnt_SafeAssign(fTexture, texture); |
| 114 fRect = rect; | 122 fRect = rect; |
| 115 } | 123 } |
| 116 GrTexture* texture() { return fTexture; } | 124 GrTexture* texture() { return fTexture; } |
| 117 const GrIRect16& rect() const { return fRect; } | 125 const GrIRect16& rect() const { return fRect; } |
| 118 | 126 |
| 119 void setPlot(GrPlot* plot) { | 127 void setPlot(GrPlot* plot) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 // classes. | 172 // classes. |
| 165 class GrLayerCache { | 173 class GrLayerCache { |
| 166 public: | 174 public: |
| 167 GrLayerCache(GrContext*); | 175 GrLayerCache(GrContext*); |
| 168 ~GrLayerCache(); | 176 ~GrLayerCache(); |
| 169 | 177 |
| 170 // As a cache, the GrLayerCache can be ordered to free up all its cached | 178 // As a cache, the GrLayerCache can be ordered to free up all its cached |
| 171 // elements by the GrContext | 179 // elements by the GrContext |
| 172 void freeAll(); | 180 void freeAll(); |
| 173 | 181 |
| 174 GrCachedLayer* findLayer(uint32_t pictureID, int start, int stop, const SkMa
trix& ctm); | 182 GrCachedLayer* findLayer(uint32_t pictureID, int start, int stop, |
| 183 const SkIPoint& offset, const SkMatrix& ctm); |
| 175 GrCachedLayer* findLayerOrCreate(uint32_t pictureID, | 184 GrCachedLayer* findLayerOrCreate(uint32_t pictureID, |
| 176 int start, int stop, | 185 int start, int stop, |
| 186 const SkIPoint& offset, |
| 177 const SkMatrix& ctm); | 187 const SkMatrix& ctm); |
| 178 | 188 |
| 179 // Inform the cache that layer's cached image is now required. | 189 // Inform the cache that layer's cached image is now required. |
| 180 // Return true if the layer must be re-rendered. Return false if the | 190 // Return true if the layer must be re-rendered. Return false if the |
| 181 // layer was found in the cache and can be reused. | 191 // layer was found in the cache and can be reused. |
| 182 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas); | 192 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas); |
| 183 | 193 |
| 184 // Inform the cache that layer's cached image is not currently required | 194 // Inform the cache that layer's cached image is not currently required |
| 185 void unlock(GrCachedLayer* layer); | 195 void unlock(GrCachedLayer* layer); |
| 186 | 196 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 230 |
| 221 // This implements a plot-centric locking mechanism (since the atlas | 231 // This implements a plot-centric locking mechanism (since the atlas |
| 222 // backing texture is always locked). Each layer that is locked (i.e., | 232 // backing texture is always locked). Each layer that is locked (i.e., |
| 223 // needed for the current rendering) in a plot increments the plot lock | 233 // needed for the current rendering) in a plot increments the plot lock |
| 224 // count for that plot. Similarly, once a rendering is complete all the | 234 // count for that plot. Similarly, once a rendering is complete all the |
| 225 // layers used in it decrement the lock count for the used plots. | 235 // layers used in it decrement the lock count for the used plots. |
| 226 // Plots with a 0 lock count are open for recycling/purging. | 236 // Plots with a 0 lock count are open for recycling/purging. |
| 227 int fPlotLocks[kNumPlotsX * kNumPlotsY]; | 237 int fPlotLocks[kNumPlotsX * kNumPlotsY]; |
| 228 | 238 |
| 229 void initAtlas(); | 239 void initAtlas(); |
| 230 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, const Sk
Matrix& ctm); | 240 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, |
| 241 const SkIPoint& offset, const SkMatrix& ctm); |
| 231 | 242 |
| 232 public: | |
| 233 void purgeAll(); | 243 void purgeAll(); |
| 234 | 244 |
| 235 // Remove all the layers (and unlock any resources) associated with 'picture
ID' | 245 // Remove all the layers (and unlock any resources) associated with 'picture
ID' |
| 236 void purge(uint32_t pictureID); | 246 void purge(uint32_t pictureID); |
| 237 | 247 |
| 238 static bool PlausiblyAtlasable(int width, int height) { | 248 static bool PlausiblyAtlasable(int width, int height) { |
| 239 return width <= kPlotWidth && height <= kPlotHeight; | 249 return width <= kPlotWidth && height <= kPlotHeight; |
| 240 } | 250 } |
| 241 | 251 |
| 242 void purgePlot(GrPlot* plot); | 252 void purgePlot(GrPlot* plot); |
| 243 | 253 |
| 244 // Try to find a purgeable plot and clear it out. Return true if a plot | 254 // Try to find a purgeable plot and clear it out. Return true if a plot |
| 245 // was purged; false otherwise. | 255 // was purged; false otherwise. |
| 246 bool purgePlot(); | 256 bool purgePlot(); |
| 247 | 257 |
| 248 // for testing | 258 // for testing |
| 249 friend class TestingAccess; | 259 friend class TestingAccess; |
| 250 int numLayers() const { return fLayerHash.count(); } | 260 int numLayers() const { return fLayerHash.count(); } |
| 251 }; | 261 }; |
| 252 | 262 |
| 253 #endif | 263 #endif |
| OLD | NEW |