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" | |
14 #include "GrAtlas.h" | 13 #include "GrAtlas.h" |
15 #include "GrPictureUtils.h" | 14 #include "GrPictureUtils.h" |
16 #include "GrRect.h" | 15 #include "GrRect.h" |
17 #include "SkChecksum.h" | 16 #include "SkChecksum.h" |
18 #include "SkTDynamicHash.h" | 17 #include "SkTDynamicHash.h" |
19 #include "SkMessageBus.h" | 18 #include "SkMessageBus.h" |
20 | 19 |
21 class SkPicture; | 20 class SkPicture; |
22 | 21 |
23 // The layer cache listens for these messages to purge picture-related resources
. | 22 // The layer cache listens for these messages to purge picture-related resources
. |
(...skipping 16 matching lines...) Expand all Loading... |
40 | 39 |
41 GrAtlas::ClientPlotUsage fPlotUsage; | 40 GrAtlas::ClientPlotUsage fPlotUsage; |
42 }; | 41 }; |
43 | 42 |
44 // GrCachedLayer encapsulates the caching information for a single saveLayer. | 43 // GrCachedLayer encapsulates the caching information for a single saveLayer. |
45 // | 44 // |
46 // Atlased layers get a ref to the backing GrTexture while non-atlased layers | 45 // Atlased layers get a ref to the backing GrTexture while non-atlased layers |
47 // get a ref to the GrTexture in which they reside. In both cases 'fRect' | 46 // get a ref to the GrTexture in which they reside. In both cases 'fRect' |
48 // contains the layer's extent in its texture. | 47 // contains the layer's extent in its texture. |
49 // Atlased layers also get a pointer to the plot in which they reside. | 48 // Atlased layers also get a pointer to the plot in which they reside. |
| 49 // For non-atlased layers the lock field just corresponds to locking in |
| 50 // the resource cache. For atlased layers it implements an additional level |
| 51 // of locking to allow atlased layers to be reused multiple times. |
50 struct GrCachedLayer { | 52 struct GrCachedLayer { |
51 public: | 53 public: |
52 // For SkTDynamicHash | 54 // For SkTDynamicHash |
53 struct Key { | 55 struct Key { |
54 Key(uint32_t pictureID, int layerID) : fPictureID(pictureID) , fLayerID(
layerID) {} | 56 Key(uint32_t pictureID, int layerID) : fPictureID(pictureID) , fLayerID(
layerID) {} |
55 | 57 |
56 bool operator==(const Key& other) const { | 58 bool operator==(const Key& other) const { |
57 return fPictureID == other.fPictureID && fLayerID == other.fLayerID; | 59 return fPictureID == other.fPictureID && fLayerID == other.fLayerID; |
58 } | 60 } |
59 | 61 |
(...skipping 10 matching lines...) Expand all Loading... |
70 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } | 72 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } |
71 static uint32_t Hash(const Key& key) { | 73 static uint32_t Hash(const Key& key) { |
72 return SkChecksum::Mix((key.getPictureID() << 16) | key.getLayerID()); | 74 return SkChecksum::Mix((key.getPictureID() << 16) | key.getLayerID()); |
73 } | 75 } |
74 | 76 |
75 // GrCachedLayer proper | 77 // GrCachedLayer proper |
76 GrCachedLayer(uint32_t pictureID, int layerID) | 78 GrCachedLayer(uint32_t pictureID, int layerID) |
77 : fKey(pictureID, layerID) | 79 : fKey(pictureID, layerID) |
78 , fTexture(NULL) | 80 , fTexture(NULL) |
79 , fRect(GrIRect16::MakeEmpty()) | 81 , fRect(GrIRect16::MakeEmpty()) |
80 , fPlot(NULL) { | 82 , fPlot(NULL) |
| 83 , fLocked(false) { |
81 SkASSERT(SK_InvalidGenID != pictureID && layerID >= 0); | 84 SkASSERT(SK_InvalidGenID != pictureID && layerID >= 0); |
82 } | 85 } |
83 | 86 |
84 uint32_t pictureID() const { return fKey.getPictureID(); } | 87 uint32_t pictureID() const { return fKey.getPictureID(); } |
85 int layerID() const { return fKey.getLayerID(); } | 88 int layerID() const { return fKey.getLayerID(); } |
86 | 89 |
87 // This call takes over the caller's ref | 90 // This call takes over the caller's ref |
88 void setTexture(GrTexture* texture, const GrIRect16& rect) { | 91 void setTexture(GrTexture* texture, const GrIRect16& rect) { |
89 if (NULL != fTexture) { | 92 if (NULL != fTexture) { |
90 fTexture->unref(); | 93 fTexture->unref(); |
91 } | 94 } |
92 | 95 |
93 fTexture = texture; // just take over caller's ref | 96 fTexture = texture; // just take over caller's ref |
94 fRect = rect; | 97 fRect = rect; |
95 } | 98 } |
96 GrTexture* texture() { return fTexture; } | 99 GrTexture* texture() { return fTexture; } |
97 const GrIRect16& rect() const { return fRect; } | 100 const GrIRect16& rect() const { return fRect; } |
98 | 101 |
99 void setPlot(GrPlot* plot) { | 102 void setPlot(GrPlot* plot) { |
100 SkASSERT(NULL == fPlot); | 103 SkASSERT(NULL == fPlot); |
101 fPlot = plot; | 104 fPlot = plot; |
102 } | 105 } |
103 GrPlot* plot() { return fPlot; } | 106 GrPlot* plot() { return fPlot; } |
104 | 107 |
105 bool isAtlased() const { return NULL != fPlot; } | 108 bool isAtlased() const { return NULL != fPlot; } |
106 | 109 |
| 110 void setLocked(bool locked) { fLocked = locked; } |
| 111 bool locked() const { return fLocked; } |
| 112 |
| 113 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; }) |
107 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) | 114 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) |
108 | 115 |
109 private: | 116 private: |
110 const Key fKey; | 117 const Key fKey; |
111 | 118 |
112 // fTexture is a ref on the atlasing texture for atlased layers and a | 119 // fTexture is a ref on the atlasing texture for atlased layers and a |
113 // ref on a GrTexture for non-atlased textures. In both cases, if this is | 120 // ref on a GrTexture for non-atlased textures. |
114 // non-NULL, that means that the texture is locked in the texture cache. | |
115 GrTexture* fTexture; | 121 GrTexture* fTexture; |
116 | 122 |
117 // For both atlased and non-atlased layers 'fRect' contains the bound of | 123 // For both atlased and non-atlased layers 'fRect' contains the bound of |
118 // the layer in whichever texture it resides. It is empty when 'fTexture' | 124 // the layer in whichever texture it resides. It is empty when 'fTexture' |
119 // is NULL. | 125 // is NULL. |
120 GrIRect16 fRect; | 126 GrIRect16 fRect; |
121 | 127 |
122 // For atlased layers, fPlot stores the atlas plot in which the layer rests. | 128 // For atlased layers, fPlot stores the atlas plot in which the layer rests. |
123 // It is always NULL for non-atlased layers. | 129 // It is always NULL for non-atlased layers. |
124 GrPlot* fPlot; | 130 GrPlot* fPlot; |
| 131 |
| 132 // For non-atlased layers 'fLocked' should always match "NULL != fTexture". |
| 133 // (i.e., if there is a texture it is locked). |
| 134 // For atlased layers, 'fLocked' is true if the layer is in a plot and |
| 135 // actively required for rendering. If the layer is in a plot but not |
| 136 // actively required for rendering, then 'fLocked' is false. If the |
| 137 // layer isn't in a plot then is can never be locked. |
| 138 bool fLocked; |
125 }; | 139 }; |
126 | 140 |
127 // The GrLayerCache caches pre-computed saveLayers for later rendering. | 141 // The GrLayerCache caches pre-computed saveLayers for later rendering. |
128 // Non-atlased layers are stored in their own GrTexture while the atlased | 142 // Non-atlased layers are stored in their own GrTexture while the atlased |
129 // layers share a single GrTexture. | 143 // layers share a single GrTexture. |
130 // Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888) | 144 // Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888) |
131 // and one GrPlot (for the entire atlas). As such, the GrLayerCache | 145 // and one GrPlot (for the entire atlas). As such, the GrLayerCache |
132 // roughly combines the functionality of the GrFontCache and GrTextStrike | 146 // roughly combines the functionality of the GrFontCache and GrTextStrike |
133 // classes. | 147 // classes. |
134 class GrLayerCache { | 148 class GrLayerCache { |
(...skipping 19 matching lines...) Expand all Loading... |
154 | 168 |
155 // Setup to be notified when 'picture' is deleted | 169 // Setup to be notified when 'picture' is deleted |
156 void trackPicture(const SkPicture* picture); | 170 void trackPicture(const SkPicture* picture); |
157 | 171 |
158 // Cleanup after any SkPicture deletions | 172 // Cleanup after any SkPicture deletions |
159 void processDeletedPictures(); | 173 void processDeletedPictures(); |
160 | 174 |
161 SkDEBUGCODE(void validate() const;) | 175 SkDEBUGCODE(void validate() const;) |
162 | 176 |
163 private: | 177 private: |
| 178 static const int kAtlasTextureWidth = 1024; |
| 179 static const int kAtlasTextureHeight = 1024; |
| 180 |
164 static const int kNumPlotsX = 2; | 181 static const int kNumPlotsX = 2; |
165 static const int kNumPlotsY = 2; | 182 static const int kNumPlotsY = 2; |
166 | 183 |
| 184 static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX; |
| 185 static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY; |
| 186 |
167 GrContext* fContext; // pointer back to owning context | 187 GrContext* fContext; // pointer back to owning context |
168 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate | 188 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate |
169 | 189 |
170 // We cache this information here (rather then, say, on the owning picture) | 190 // We cache this information here (rather then, say, on the owning picture) |
171 // because we want to be able to clean it up as needed (e.g., if a picture | 191 // because we want to be able to clean it up as needed (e.g., if a picture |
172 // is leaked and never cleans itself up we still want to be able to | 192 // is leaked and never cleans itself up we still want to be able to |
173 // remove the GrPictureInfo once its layers are purged from all the atlas | 193 // remove the GrPictureInfo once its layers are purged from all the atlas |
174 // plots). | 194 // plots). |
175 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash; | 195 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash; |
176 | 196 |
177 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash; | 197 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash; |
178 | 198 |
179 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox; | 199 SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox; |
180 | 200 |
181 SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener; | 201 SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener; |
182 | 202 |
| 203 // This implements a plot-centric locking mechanism (since the atlas |
| 204 // backing texture is always locked). Each layer that is locked (i.e., |
| 205 // needed for the current rendering) in a plot increments the plot lock |
| 206 // count for that plot. Similarly, once a rendering is complete all the |
| 207 // layers used in it decrement the lock count for the used plots. |
| 208 // Plots with a 0 lock count are open for recycling/purging. |
| 209 int fPlotLocks[kNumPlotsX * kNumPlotsY]; |
| 210 |
183 void initAtlas(); | 211 void initAtlas(); |
184 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); | 212 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); |
185 | 213 |
186 // Remove all the layers (and unlock any resources) associated with 'picture
ID' | 214 // Remove all the layers (and unlock any resources) associated with 'picture
ID' |
187 void purge(uint32_t pictureID); | 215 void purge(uint32_t pictureID); |
188 | 216 |
| 217 static bool PlausiblyAtlasable(int width, int height) { |
| 218 return width <= kPlotWidth && height <= kPlotHeight; |
| 219 } |
| 220 |
| 221 // Try to find a purgeable plot and clear it out. Return true if a plot |
| 222 // was purged; false otherwise. |
| 223 bool purgePlot(); |
| 224 |
189 // for testing | 225 // for testing |
190 friend class TestingAccess; | 226 friend class TestingAccess; |
191 int numLayers() const { return fLayerHash.count(); } | 227 int numLayers() const { return fLayerHash.count(); } |
192 }; | 228 }; |
193 | 229 |
194 #endif | 230 #endif |
OLD | NEW |