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 "GrTHashTable.h" | |
16 #include "GrPictureUtils.h" | 15 #include "GrPictureUtils.h" |
17 #include "GrRect.h" | 16 #include "GrRect.h" |
| 17 #include "SkChecksum.h" |
| 18 #include "SkTDynamicHash.h" |
18 | 19 |
19 class SkPicture; | 20 class SkPicture; |
20 | 21 |
21 // GrPictureInfo stores the atlas plots used by a single picture. A single | 22 // GrPictureInfo stores the atlas plots used by a single picture. A single |
22 // plot may be used to store layers from multiple pictures. | 23 // plot may be used to store layers from multiple pictures. |
23 struct GrPictureInfo { | 24 struct GrPictureInfo { |
24 public: | 25 public: |
| 26 // for SkTDynamicHash - just use the pictureID as the hash key |
| 27 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); } |
| 29 |
| 30 // GrPictureInfo proper |
25 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { } | 31 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { } |
26 | 32 |
27 uint32_t fPictureID; | 33 const uint32_t fPictureID; |
28 | 34 |
29 GrAtlas::ClientPlotUsage fPlotUsage; | 35 GrAtlas::ClientPlotUsage fPlotUsage; |
30 }; | 36 }; |
31 | 37 |
32 // GrCachedLayer encapsulates the caching information for a single saveLayer. | 38 // GrCachedLayer encapsulates the caching information for a single saveLayer. |
33 // | 39 // |
34 // Atlased layers get a ref to the backing GrTexture while non-atlased layers | 40 // Atlased layers get a ref to the backing GrTexture while non-atlased layers |
35 // get a ref to the GrTexture in which they reside. In both cases 'fRect' | 41 // get a ref to the GrTexture in which they reside. In both cases 'fRect' |
36 // contains the layer's extent in its texture. | 42 // contains the layer's extent in its texture. |
37 // Atlased layers also get a pointer to the plot in which they reside. | 43 // Atlased layers also get a pointer to the plot in which they reside. |
38 struct GrCachedLayer { | 44 struct GrCachedLayer { |
39 public: | 45 public: |
40 GrCachedLayer(uint32_t pictureID, int layerID) | 46 // For SkTDynamicHash |
41 : fPlot(NULL) { | 47 struct Key { |
42 fPictureID = pictureID; | 48 Key(uint32_t pictureID, int layerID) : fPictureID(pictureID) , fLayerID(
layerID) {} |
43 fLayerID = layerID; | 49 |
44 fTexture = NULL; | 50 bool operator==(const Key& other) const { |
45 fRect = GrIRect16::MakeEmpty(); | 51 return fPictureID == other.fPictureID && fLayerID == other.fLayerID; |
| 52 } |
| 53 |
| 54 uint32_t getPictureID() const { return fPictureID; } |
| 55 int getLayerID() const { return fLayerID; } |
| 56 |
| 57 private: |
| 58 // ID of the picture of which this layer is a part |
| 59 const uint32_t fPictureID; |
| 60 // fLayerID is the index of this layer in the picture (one of 0 .. #laye
rs). |
| 61 const int fLayerID; |
| 62 }; |
| 63 |
| 64 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } |
| 65 static uint32_t Hash(const Key& key) { |
| 66 return SkChecksum::Mix((key.getPictureID() << 16) | key.getLayerID()); |
46 } | 67 } |
47 | 68 |
48 uint32_t pictureID() const { return fPictureID; } | 69 // GrCachedLayer proper |
49 int layerID() const { return fLayerID; } | 70 GrCachedLayer(uint32_t pictureID, int layerID) |
| 71 : fKey(pictureID, layerID) |
| 72 , fTexture(NULL) |
| 73 , fRect(GrIRect16::MakeEmpty()) |
| 74 , fPlot(NULL) { |
| 75 SkASSERT(SK_InvalidGenID != pictureID && layerID >= 0); |
| 76 } |
| 77 |
| 78 uint32_t pictureID() const { return fKey.getPictureID(); } |
| 79 int layerID() const { return fKey.getLayerID(); } |
50 | 80 |
51 // This call takes over the caller's ref | 81 // This call takes over the caller's ref |
52 void setTexture(GrTexture* texture, const GrIRect16& rect) { | 82 void setTexture(GrTexture* texture, const GrIRect16& rect) { |
53 if (NULL != fTexture) { | 83 if (NULL != fTexture) { |
54 fTexture->unref(); | 84 fTexture->unref(); |
55 } | 85 } |
56 | 86 |
57 fTexture = texture; // just take over caller's ref | 87 fTexture = texture; // just take over caller's ref |
58 fRect = rect; | 88 fRect = rect; |
59 } | 89 } |
60 GrTexture* texture() { return fTexture; } | 90 GrTexture* texture() { return fTexture; } |
61 const GrIRect16& rect() const { return fRect; } | 91 const GrIRect16& rect() const { return fRect; } |
62 | 92 |
63 void setPlot(GrPlot* plot) { | 93 void setPlot(GrPlot* plot) { |
64 SkASSERT(NULL == fPlot); | 94 SkASSERT(NULL == fPlot); |
65 fPlot = plot; | 95 fPlot = plot; |
66 } | 96 } |
67 GrPlot* plot() { return fPlot; } | 97 GrPlot* plot() { return fPlot; } |
68 | 98 |
69 bool isAtlased() const { return NULL != fPlot; } | 99 bool isAtlased() const { return NULL != fPlot; } |
70 | 100 |
71 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) | 101 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) |
72 | 102 |
73 private: | 103 private: |
74 // ID of the picture of which this layer is a part | 104 const Key fKey; |
75 uint32_t fPictureID; | |
76 | |
77 // fLayerID is only valid when fPicture != kInvalidGenID in which case it | |
78 // is the index of this layer in the picture (one of 0 .. #layers). | |
79 int fLayerID; | |
80 | 105 |
81 // fTexture is a ref on the atlasing texture for atlased layers and a | 106 // fTexture is a ref on the atlasing texture for atlased layers and a |
82 // ref on a GrTexture for non-atlased textures. In both cases, if this is | 107 // ref on a GrTexture for non-atlased textures. In both cases, if this is |
83 // non-NULL, that means that the texture is locked in the texture cache. | 108 // non-NULL, that means that the texture is locked in the texture cache. |
84 GrTexture* fTexture; | 109 GrTexture* fTexture; |
85 | 110 |
86 // For both atlased and non-atlased layers 'fRect' contains the bound of | 111 // For both atlased and non-atlased layers 'fRect' contains the bound of |
87 // the layer in whichever texture it resides. It is empty when 'fTexture' | 112 // the layer in whichever texture it resides. It is empty when 'fTexture' |
88 // is NULL. | 113 // is NULL. |
89 GrIRect16 fRect; | 114 GrIRect16 fRect; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 static const int kNumPlotsY = 2; | 156 static const int kNumPlotsY = 2; |
132 | 157 |
133 GrContext* fContext; // pointer back to owning context | 158 GrContext* fContext; // pointer back to owning context |
134 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate | 159 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate |
135 | 160 |
136 // We cache this information here (rather then, say, on the owning picture) | 161 // We cache this information here (rather then, say, on the owning picture) |
137 // because we want to be able to clean it up as needed (e.g., if a picture | 162 // because we want to be able to clean it up as needed (e.g., if a picture |
138 // is leaked and never cleans itself up we still want to be able to | 163 // is leaked and never cleans itself up we still want to be able to |
139 // remove the GrPictureInfo once its layers are purged from all the atlas | 164 // remove the GrPictureInfo once its layers are purged from all the atlas |
140 // plots). | 165 // plots). |
141 class PictureKey; | 166 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash; |
142 GrTHashTable<GrPictureInfo, PictureKey, 7> fPictureHash; | |
143 | 167 |
144 class PictureLayerKey; | 168 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash; |
145 GrTHashTable<GrCachedLayer, PictureLayerKey, 7> fLayerHash; | |
146 | 169 |
147 void initAtlas(); | 170 void initAtlas(); |
148 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); | 171 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); |
149 | 172 |
150 // for testing | 173 // for testing |
151 friend class GetNumLayers; | 174 friend class GetNumLayers; |
152 int numLayers() const { return fLayerHash.count(); } | 175 int numLayers() const { return fLayerHash.count(); } |
153 }; | 176 }; |
154 | 177 |
155 #endif | 178 #endif |
OLD | NEW |