robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrLayerCache_DEFINED |
| 9 | #define GrLayerCache_DEFINED |
| 10 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 11 | #include "GrAtlas.h" |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 12 | #include "GrPictureUtils.h" |
| 13 | #include "GrRect.h" |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 14 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 15 | #include "SkChecksum.h" |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 16 | #include "SkMessageBus.h" |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 17 | #include "SkTDynamicHash.h" |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 18 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 19 | class SkPicture; |
| 20 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 21 | // The layer cache listens for these messages to purge picture-related resources. |
| 22 | struct GrPictureDeletedMessage { |
| 23 | uint32_t pictureID; |
| 24 | }; |
| 25 | |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 26 | // GrPictureInfo stores the atlas plots used by a single picture. A single |
| 27 | // plot may be used to store layers from multiple pictures. |
| 28 | struct GrPictureInfo { |
| 29 | public: |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 30 | // for SkTDynamicHash - just use the pictureID as the hash key |
| 31 | static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictInfo.fPictureID; } |
| 32 | static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); } |
| 33 | |
| 34 | // GrPictureInfo proper |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 35 | GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { } |
| 36 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 37 | const uint32_t fPictureID; |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 38 | |
| 39 | GrAtlas::ClientPlotUsage fPlotUsage; |
| 40 | }; |
| 41 | |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 42 | // GrCachedLayer encapsulates the caching information for a single saveLayer. |
| 43 | // |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 44 | // Atlased layers get a ref to the backing GrTexture while non-atlased layers |
| 45 | // get a ref to the GrTexture in which they reside. In both cases 'fRect' |
| 46 | // contains the layer's extent in its texture. |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 47 | // Atlased layers also get a pointer to the plot in which they reside. |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 48 | // For non-atlased layers, the lock field just corresponds to locking in |
| 49 | // the resource cache. For atlased layers, it implements an additional level |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 50 | // of locking to allow atlased layers to be reused multiple times. |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 51 | struct GrCachedLayer { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 52 | public: |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 53 | // For SkTDynamicHash |
| 54 | struct Key { |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 55 | Key(uint32_t pictureID, int start, const SkIRect& bounds, const SkMatrix& ctm) |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 56 | : fPictureID(pictureID) |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 57 | , fStart(start) |
| 58 | , fBounds(bounds) |
| 59 | , fCTM(ctm) { |
| 60 | fCTM.getType(); // force initialization of type so hashes match |
| 61 | |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 62 | // Key needs to be tightly packed. |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 63 | GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // picture ID |
| 64 | sizeof(int) + // start index |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 65 | 4 * sizeof(uint32_t) + // bounds |
| 66 | 9 * sizeof(SkScalar) + sizeof(uint32_t)); // matrix |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 67 | } |
| 68 | |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 69 | bool operator==(const Key& other) const { |
| 70 | return fPictureID == other.fPictureID && |
| 71 | fStart == other.fStart && |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 72 | fBounds == other.fBounds && |
| 73 | fCTM.cheapEqualTo(other.fCTM); |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | uint32_t pictureID() const { return fPictureID; } |
| 77 | int start() const { return fStart; } |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 78 | const SkIRect& bound() const { return fBounds; } |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
| 81 | // ID of the picture of which this layer is a part |
| 82 | const uint32_t fPictureID; |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 83 | // The the index of the saveLayer command in the picture |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 84 | const int fStart; |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 85 | // The bounds of the layer. The TL corner is its offset. |
| 86 | const SkIRect fBounds; |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 87 | // The 2x2 portion of the CTM applied to this layer in the picture |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 88 | SkMatrix fCTM; |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } |
| 92 | static uint32_t Hash(const Key& key) { |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 93 | return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 94 | } |
| 95 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 96 | // GrCachedLayer proper |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 97 | GrCachedLayer(uint32_t pictureID, int start, int stop, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 98 | const SkIRect& bounds, const SkMatrix& ctm, |
| 99 | const SkPaint* paint) |
| 100 | : fKey(pictureID, start, bounds, ctm) |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 101 | , fStop(stop) |
robertphillips | a0537de | 2014-09-18 08:01:23 -0700 | [diff] [blame] | 102 | , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL) |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 103 | , fTexture(NULL) |
| 104 | , fRect(GrIRect16::MakeEmpty()) |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 105 | , fPlot(NULL) |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 106 | , fUses(0) |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 107 | , fLocked(false) { |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 108 | SkASSERT(SK_InvalidGenID != pictureID && start >= 0 && stop >= 0); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 109 | } |
| 110 | |
robertphillips | ed6f03e | 2014-07-30 07:31:35 -0700 | [diff] [blame] | 111 | ~GrCachedLayer() { |
| 112 | SkSafeUnref(fTexture); |
robertphillips | a0537de | 2014-09-18 08:01:23 -0700 | [diff] [blame] | 113 | SkDELETE(fPaint); |
robertphillips | ed6f03e | 2014-07-30 07:31:35 -0700 | [diff] [blame] | 114 | } |
| 115 | |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 116 | uint32_t pictureID() const { return fKey.pictureID(); } |
| 117 | int start() const { return fKey.start(); } |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 118 | const SkIRect& bound() const { return fKey.bound(); } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 119 | |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 120 | int stop() const { return fStop; } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 121 | void setTexture(GrTexture* texture, const GrIRect16& rect) { |
robertphillips | ed6f03e | 2014-07-30 07:31:35 -0700 | [diff] [blame] | 122 | SkRefCnt_SafeAssign(fTexture, texture); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 123 | fRect = rect; |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 124 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 125 | GrTexture* texture() { return fTexture; } |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 126 | const SkPaint* paint() const { return fPaint; } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 127 | const GrIRect16& rect() const { return fRect; } |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 128 | |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 129 | void setPlot(GrPlot* plot) { |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 130 | SkASSERT(NULL == plot || NULL == fPlot); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 131 | fPlot = plot; |
| 132 | } |
| 133 | GrPlot* plot() { return fPlot; } |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 134 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 135 | bool isAtlased() const { return SkToBool(fPlot); } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 136 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 137 | void setLocked(bool locked) { fLocked = locked; } |
| 138 | bool locked() const { return fLocked; } |
| 139 | |
| 140 | SkDEBUGCODE(const GrPlot* plot() const { return fPlot; }) |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 141 | SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 142 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 143 | private: |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 144 | const Key fKey; |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 145 | |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 146 | // The final "restore" operation index of the cached layer |
| 147 | const int fStop; |
| 148 | |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 149 | // The paint used when dropping the layer down into the owning canvas. |
robertphillips | a0537de | 2014-09-18 08:01:23 -0700 | [diff] [blame] | 150 | // Can be NULL. This class makes a copy for itself. |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 151 | const SkPaint* fPaint; |
| 152 | |
skia.committer@gmail.com | d942731 | 2014-04-12 03:05:59 +0000 | [diff] [blame] | 153 | // fTexture is a ref on the atlasing texture for atlased layers and a |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 154 | // ref on a GrTexture for non-atlased textures. |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 155 | GrTexture* fTexture; |
| 156 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 157 | // For both atlased and non-atlased layers 'fRect' contains the bound of |
| 158 | // the layer in whichever texture it resides. It is empty when 'fTexture' |
| 159 | // is NULL. |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 160 | GrIRect16 fRect; |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 161 | |
| 162 | // For atlased layers, fPlot stores the atlas plot in which the layer rests. |
| 163 | // It is always NULL for non-atlased layers. |
| 164 | GrPlot* fPlot; |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 165 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 166 | // The number of actively hoisted layers using this cached image (e.g., |
| 167 | // extant GrHoistedLayers pointing at this object). This object will |
| 168 | // be unlocked when the use count reaches 0. |
| 169 | int fUses; |
| 170 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 171 | // For non-atlased layers 'fLocked' should always match "fTexture". |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 172 | // (i.e., if there is a texture it is locked). |
| 173 | // For atlased layers, 'fLocked' is true if the layer is in a plot and |
| 174 | // actively required for rendering. If the layer is in a plot but not |
| 175 | // actively required for rendering, then 'fLocked' is false. If the |
| 176 | // layer isn't in a plot then is can never be locked. |
| 177 | bool fLocked; |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 178 | |
| 179 | void addUse() { ++fUses; } |
| 180 | void removeUse() { SkASSERT(fUses > 0); --fUses; } |
| 181 | int uses() const { return fUses; } |
| 182 | |
| 183 | friend class GrLayerCache; // for access to usage methods |
| 184 | friend class TestingAccess; // for testing |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | // The GrLayerCache caches pre-computed saveLayers for later rendering. |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 188 | // Non-atlased layers are stored in their own GrTexture while the atlased |
| 189 | // layers share a single GrTexture. |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 190 | // Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888) |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 191 | // and one GrPlot (for the entire atlas). As such, the GrLayerCache |
| 192 | // roughly combines the functionality of the GrFontCache and GrTextStrike |
| 193 | // classes. |
| 194 | class GrLayerCache { |
| 195 | public: |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 196 | GrLayerCache(GrContext*); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 197 | ~GrLayerCache(); |
| 198 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 199 | // As a cache, the GrLayerCache can be ordered to free up all its cached |
| 200 | // elements by the GrContext |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 201 | void freeAll(); |
| 202 | |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 203 | GrCachedLayer* findLayer(uint32_t pictureID, int start, |
| 204 | const SkIRect& bounds, const SkMatrix& ctm); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 205 | GrCachedLayer* findLayerOrCreate(uint32_t pictureID, |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 206 | int start, int stop, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 207 | const SkIRect& bounds, |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 208 | const SkMatrix& ctm, |
| 209 | const SkPaint* paint); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 210 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 211 | // Attempt to place 'layer' in the atlas. Return true on success; false on failure. |
| 212 | // When true is returned, 'needsRendering' will indicate if the layer must be (re)drawn. |
| 213 | // Additionally, the GPU resources will be locked. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame^] | 214 | bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 215 | |
| 216 | // Attempt to lock the GPU resources required for a layer. Return true on success; |
| 217 | // false on failure. When true is returned 'needsRendering' will indicate if the |
| 218 | // layer must be (re)drawn. |
| 219 | // Note that atlased layers should already have been locked and rendered so only |
| 220 | // free floating layers will have 'needsRendering' set. |
| 221 | // Currently, this path always uses a new scratch texture for non-Atlased layers |
| 222 | // and (thus) doesn't cache anything. This can yield a lot of re-rendering. |
| 223 | // TODO: allow rediscovery of free-floating layers that are still in the resource cache. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame^] | 224 | bool lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering); |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 225 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 226 | // addUse is just here to keep the API symmetric |
| 227 | void addUse(GrCachedLayer* layer) { layer->addUse(); } |
| 228 | void removeUse(GrCachedLayer* layer) { |
| 229 | layer->removeUse(); |
| 230 | if (layer->uses() == 0) { |
| 231 | // If no one cares about the layer allow it to be recycled. |
| 232 | this->unlock(layer); |
| 233 | } |
| 234 | } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 235 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 236 | // Setup to be notified when 'picture' is deleted |
| 237 | void trackPicture(const SkPicture* picture); |
| 238 | |
| 239 | // Cleanup after any SkPicture deletions |
| 240 | void processDeletedPictures(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 241 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 242 | SkDEBUGCODE(void validate() const;) |
| 243 | |
robertphillips | 84ac082 | 2014-10-14 07:07:59 -0700 | [diff] [blame] | 244 | #ifdef SK_DEVELOPER |
| 245 | void writeLayersToDisk(const SkString& dirName); |
| 246 | #endif |
| 247 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 248 | static bool PlausiblyAtlasable(int width, int height) { |
| 249 | return width <= kPlotWidth && height <= kPlotHeight; |
| 250 | } |
| 251 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 252 | private: |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 253 | static const int kAtlasTextureWidth = 1024; |
| 254 | static const int kAtlasTextureHeight = 1024; |
| 255 | |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 256 | static const int kNumPlotsX = 2; |
| 257 | static const int kNumPlotsY = 2; |
| 258 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 259 | static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX; |
| 260 | static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY; |
| 261 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 262 | GrContext* fContext; // pointer back to owning context |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 263 | SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 264 | |
| 265 | // We cache this information here (rather then, say, on the owning picture) |
| 266 | // because we want to be able to clean it up as needed (e.g., if a picture |
| 267 | // is leaked and never cleans itself up we still want to be able to |
| 268 | // remove the GrPictureInfo once its layers are purged from all the atlas |
| 269 | // plots). |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 270 | SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash; |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 271 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 272 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash; |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 273 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 274 | SkMessageBus<GrPictureDeletedMessage>::Inbox fPictDeletionInbox; |
| 275 | |
| 276 | SkAutoTUnref<SkPicture::DeletionListener> fDeletionListener; |
| 277 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 278 | // This implements a plot-centric locking mechanism (since the atlas |
| 279 | // backing texture is always locked). Each layer that is locked (i.e., |
| 280 | // needed for the current rendering) in a plot increments the plot lock |
robertphillips | a32c6bc | 2014-10-09 12:47:01 -0700 | [diff] [blame] | 281 | // count for that plot. Similarly, once a rendering is complete all the |
| 282 | // layers used in it decrement the lock count for the used plots. |
| 283 | // Plots with a 0 lock count are open for recycling/purging. |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 284 | int fPlotLocks[kNumPlotsX * kNumPlotsY]; |
| 285 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 286 | // Inform the cache that layer's cached image is not currently required |
| 287 | void unlock(GrCachedLayer* layer); |
| 288 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 289 | void initAtlas(); |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 290 | GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 291 | const SkIRect& bounds, const SkMatrix& ctm, |
| 292 | const SkPaint* paint); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 293 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 294 | void purgeAll(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 295 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 296 | // Remove all the layers (and unlock any resources) associated with 'pictureID' |
| 297 | void purge(uint32_t pictureID); |
| 298 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 299 | void purgePlot(GrPlot* plot); |
| 300 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 301 | // Try to find a purgeable plot and clear it out. Return true if a plot |
| 302 | // was purged; false otherwise. |
| 303 | bool purgePlot(); |
| 304 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 305 | void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; } |
| 306 | void decPlotLock(int plotIdx) { |
| 307 | SkASSERT(fPlotLocks[plotIdx] > 0); |
| 308 | --fPlotLocks[plotIdx]; |
| 309 | } |
| 310 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 311 | // for testing |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 312 | friend class TestingAccess; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 313 | int numLayers() const { return fLayerHash.count(); } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | #endif |