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 SkIPoint& offset, con
st SkMatrix& ctm) | 54 // TODO: the key needs to include the clip |
| 55 Key(uint32_t pictureID, int start, const SkMatrix& ctm) |
55 : fPictureID(pictureID) | 56 : fPictureID(pictureID) |
56 , fStart(start) | 57 , fStart(start) { |
57 , fStop(stop) | 58 fCTM[0] = ctm.getScaleX(); |
58 , fOffset(offset) | 59 fCTM[1] = ctm.getSkewX(); |
59 , fCTM(ctm) { | 60 fCTM[2] = ctm.getSkewY(); |
60 fCTM.getType(); // force initialization of type so hashes match | 61 fCTM[3] = ctm.getScaleY(); |
61 | |
62 // Key needs to be tightly packed. | 62 // Key needs to be tightly packed. |
63 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + 2 * sizeof(int) +
| 63 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // picture I
D |
64 2 * sizeof(int32_t) + | 64 sizeof(int) + // start ind
ex |
65 9 * sizeof(SkScalar) + sizeof(uint32
_t)); | 65 4 * sizeof(SkScalar)); // 2x2 from
CTM |
66 } | 66 } |
67 | 67 |
68 bool operator==(const Key& other) const { | 68 bool operator==(const Key& other) const { |
69 return fPictureID == other.fPictureID && | 69 return fPictureID == other.fPictureID && |
70 fStart == other.fStart && | 70 fStart == other.fStart && |
71 fStop == other.fStop && | 71 0 == memcmp(fCTM, other.fCTM, sizeof(fCTM)); |
72 fOffset == other.fOffset && | |
73 fCTM.cheapEqualTo(other.fCTM); | |
74 } | 72 } |
75 | 73 |
76 uint32_t pictureID() const { return fPictureID; } | 74 uint32_t pictureID() const { return fPictureID; } |
77 int start() const { return fStart; } | 75 int start() const { return fStart; } |
78 int stop() const { return fStop; } | |
79 const SkIPoint& offset() const { return fOffset; } | |
80 const SkMatrix& ctm() const { return fCTM; } | |
81 | 76 |
82 private: | 77 private: |
83 // ID of the picture of which this layer is a part | 78 // ID of the picture of which this layer is a part |
84 const uint32_t fPictureID; | 79 const uint32_t fPictureID; |
85 // The range of commands in the picture this layer represents | 80 // The the index of the saveLayer command in the picture |
86 const int fStart; | 81 const int fStart; |
87 const int fStop; | 82 // The 2x2 portion of the CTM applied to this layer in the picture |
88 // The offset of the layer in device space | 83 SkScalar fCTM[4]; |
89 const SkIPoint fOffset; | |
90 // The CTM applied to this layer in the picture | |
91 SkMatrix fCTM; | |
92 }; | 84 }; |
93 | 85 |
94 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } | 86 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } |
95 static uint32_t Hash(const Key& key) { | 87 static uint32_t Hash(const Key& key) { |
96 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), size
of(Key)); | 88 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), size
of(Key)); |
97 } | 89 } |
98 | 90 |
99 // GrCachedLayer proper | 91 // GrCachedLayer proper |
100 GrCachedLayer(uint32_t pictureID, int start, int stop, | 92 GrCachedLayer(uint32_t pictureID, int start, int stop, |
101 const SkIPoint& offset, const SkMatrix& ctm, | 93 const SkMatrix& ctm, const SkPaint* paint) |
102 const SkPaint* paint) | 94 : fKey(pictureID, start, ctm) |
103 : fKey(pictureID, start, stop, offset, ctm) | 95 , fStop(stop) |
104 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL) | 96 , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL) |
105 , fTexture(NULL) | 97 , fTexture(NULL) |
106 , fRect(GrIRect16::MakeEmpty()) | 98 , fRect(GrIRect16::MakeEmpty()) |
107 , fPlot(NULL) | 99 , fPlot(NULL) |
108 , fLocked(false) { | 100 , fLocked(false) { |
109 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0); | 101 SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0); |
110 } | 102 } |
111 | 103 |
112 ~GrCachedLayer() { | 104 ~GrCachedLayer() { |
113 SkSafeUnref(fTexture); | 105 SkSafeUnref(fTexture); |
114 SkDELETE(fPaint); | 106 SkDELETE(fPaint); |
115 } | 107 } |
116 | 108 |
117 uint32_t pictureID() const { return fKey.pictureID(); } | 109 uint32_t pictureID() const { return fKey.pictureID(); } |
118 int start() const { return fKey.start(); } | 110 int start() const { return fKey.start(); } |
119 int stop() const { return fKey.stop(); } | |
120 const SkIPoint& offset() const { return fKey.offset(); } | |
121 const SkMatrix& ctm() const { return fKey.ctm(); } | |
122 | 111 |
| 112 int stop() const { return fStop; } |
123 void setTexture(GrTexture* texture, const GrIRect16& rect) { | 113 void setTexture(GrTexture* texture, const GrIRect16& rect) { |
124 SkRefCnt_SafeAssign(fTexture, texture); | 114 SkRefCnt_SafeAssign(fTexture, texture); |
125 fRect = rect; | 115 fRect = rect; |
126 } | 116 } |
127 GrTexture* texture() { return fTexture; } | 117 GrTexture* texture() { return fTexture; } |
128 const SkPaint* paint() const { return fPaint; } | 118 const SkPaint* paint() const { return fPaint; } |
129 const GrIRect16& rect() const { return fRect; } | 119 const GrIRect16& rect() const { return fRect; } |
130 | 120 |
131 void setPlot(GrPlot* plot) { | 121 void setPlot(GrPlot* plot) { |
132 SkASSERT(NULL == plot || NULL == fPlot); | 122 SkASSERT(NULL == plot || NULL == fPlot); |
133 fPlot = plot; | 123 fPlot = plot; |
134 } | 124 } |
135 GrPlot* plot() { return fPlot; } | 125 GrPlot* plot() { return fPlot; } |
136 | 126 |
137 bool isAtlased() const { return SkToBool(fPlot); } | 127 bool isAtlased() const { return SkToBool(fPlot); } |
138 | 128 |
139 void setLocked(bool locked) { fLocked = locked; } | 129 void setLocked(bool locked) { fLocked = locked; } |
140 bool locked() const { return fLocked; } | 130 bool locked() const { return fLocked; } |
141 | 131 |
142 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; }) | 132 SkDEBUGCODE(const GrPlot* plot() const { return fPlot; }) |
143 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) | 133 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) |
144 | 134 |
145 private: | 135 private: |
146 const Key fKey; | 136 const Key fKey; |
147 | 137 |
| 138 // The final "restore" operation index of the cached layer |
| 139 const int fStop; |
| 140 |
148 // The paint used when dropping the layer down into the owning canvas. | 141 // The paint used when dropping the layer down into the owning canvas. |
149 // Can be NULL. This class makes a copy for itself. | 142 // Can be NULL. This class makes a copy for itself. |
150 const SkPaint* fPaint; | 143 const SkPaint* fPaint; |
151 | 144 |
152 // fTexture is a ref on the atlasing texture for atlased layers and a | 145 // fTexture is a ref on the atlasing texture for atlased layers and a |
153 // ref on a GrTexture for non-atlased textures. | 146 // ref on a GrTexture for non-atlased textures. |
154 GrTexture* fTexture; | 147 GrTexture* fTexture; |
155 | 148 |
156 // For both atlased and non-atlased layers 'fRect' contains the bound of | 149 // For both atlased and non-atlased layers 'fRect' contains the bound of |
157 // the layer in whichever texture it resides. It is empty when 'fTexture' | 150 // the layer in whichever texture it resides. It is empty when 'fTexture' |
(...skipping 22 matching lines...) Expand all Loading... |
180 // classes. | 173 // classes. |
181 class GrLayerCache { | 174 class GrLayerCache { |
182 public: | 175 public: |
183 GrLayerCache(GrContext*); | 176 GrLayerCache(GrContext*); |
184 ~GrLayerCache(); | 177 ~GrLayerCache(); |
185 | 178 |
186 // As a cache, the GrLayerCache can be ordered to free up all its cached | 179 // As a cache, the GrLayerCache can be ordered to free up all its cached |
187 // elements by the GrContext | 180 // elements by the GrContext |
188 void freeAll(); | 181 void freeAll(); |
189 | 182 |
190 GrCachedLayer* findLayer(uint32_t pictureID, int start, int stop, | 183 GrCachedLayer* findLayer(uint32_t pictureID, int start, const SkMatrix& ctm)
; |
191 const SkIPoint& offset, const SkMatrix& ctm); | |
192 GrCachedLayer* findLayerOrCreate(uint32_t pictureID, | 184 GrCachedLayer* findLayerOrCreate(uint32_t pictureID, |
193 int start, int stop, | 185 int start, int stop, |
194 const SkIPoint& offset, | |
195 const SkMatrix& ctm, | 186 const SkMatrix& ctm, |
196 const SkPaint* paint); | 187 const SkPaint* paint); |
197 | 188 |
198 // Inform the cache that layer's cached image is now required. | 189 // Inform the cache that layer's cached image is now required. |
199 // 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 |
200 // layer was found in the cache and can be reused. | 191 // layer was found in the cache and can be reused. |
201 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas); | 192 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas); |
202 | 193 |
203 // 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 |
204 void unlock(GrCachedLayer* layer); | 195 void unlock(GrCachedLayer* layer); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 // This implements a plot-centric locking mechanism (since the atlas | 231 // This implements a plot-centric locking mechanism (since the atlas |
241 // 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., |
242 // 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 |
243 // 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 |
244 // 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. |
245 // Plots with a 0 lock count are open for recycling/purging. | 236 // Plots with a 0 lock count are open for recycling/purging. |
246 int fPlotLocks[kNumPlotsX * kNumPlotsY]; | 237 int fPlotLocks[kNumPlotsX * kNumPlotsY]; |
247 | 238 |
248 void initAtlas(); | 239 void initAtlas(); |
249 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, | 240 GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, |
250 const SkIPoint& offset, const SkMatrix& ctm, | 241 const SkMatrix& ctm, const SkPaint* paint); |
251 const SkPaint* paint); | |
252 | 242 |
253 void purgeAll(); | 243 void purgeAll(); |
254 | 244 |
255 // 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' |
256 void purge(uint32_t pictureID); | 246 void purge(uint32_t pictureID); |
257 | 247 |
258 static bool PlausiblyAtlasable(int width, int height) { | 248 static bool PlausiblyAtlasable(int width, int height) { |
259 return width <= kPlotWidth && height <= kPlotHeight; | 249 return width <= kPlotWidth && height <= kPlotHeight; |
260 } | 250 } |
261 | 251 |
262 void purgePlot(GrPlot* plot); | 252 void purgePlot(GrPlot* plot); |
263 | 253 |
264 // 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 |
265 // was purged; false otherwise. | 255 // was purged; false otherwise. |
266 bool purgePlot(); | 256 bool purgePlot(); |
267 | 257 |
268 // for testing | 258 // for testing |
269 friend class TestingAccess; | 259 friend class TestingAccess; |
270 int numLayers() const { return fLayerHash.count(); } | 260 int numLayers() const { return fLayerHash.count(); } |
271 }; | 261 }; |
272 | 262 |
273 #endif | 263 #endif |
OLD | NEW |