Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: src/gpu/GrLayerCache.h

Issue 402693003: Replace GrTHash with SkTDynamicHash (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix initializer order Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "SkTDynamicHash.h"
18 18
19 class SkPicture; 19 class SkPicture;
20 20
21 // GrPictureInfo stores the atlas plots used by a single picture. A single 21 // GrPictureInfo stores the atlas plots used by a single picture. A single
22 // plot may be used to store layers from multiple pictures. 22 // plot may be used to store layers from multiple pictures.
23 struct GrPictureInfo { 23 struct GrPictureInfo {
24 public: 24 public:
25 // for SkTDynamicHash - just use the pictureID as the hash key
26 static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictIn fo.fPictureID; }
27 static uint32_t Hash(const uint32_t& key) { return key; }
mtklein 2014/07/17 19:36:20 Might consider trying SkChecksum::Mix(key) in the
robertphillips 2014/07/17 20:02:46 Done.
28
29 // GrPictureInfo proper
25 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { } 30 GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { }
26 31
27 uint32_t fPictureID; 32 const uint32_t fPictureID;
28 33
29 GrAtlas::ClientPlotUsage fPlotUsage; 34 GrAtlas::ClientPlotUsage fPlotUsage;
30 }; 35 };
31 36
32 // GrCachedLayer encapsulates the caching information for a single saveLayer. 37 // GrCachedLayer encapsulates the caching information for a single saveLayer.
33 // 38 //
34 // Atlased layers get a ref to the backing GrTexture while non-atlased layers 39 // 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' 40 // get a ref to the GrTexture in which they reside. In both cases 'fRect'
36 // contains the layer's extent in its texture. 41 // contains the layer's extent in its texture.
37 // Atlased layers also get a pointer to the plot in which they reside. 42 // Atlased layers also get a pointer to the plot in which they reside.
38 struct GrCachedLayer { 43 struct GrCachedLayer {
39 public: 44 public:
45 // For SkTDynamicHash
46 struct Key {
47 Key(uint32_t pictureID, int layerID) : fPictureID(pictureID) , fLayerID( layerID) {}
48
49 bool operator<(const Key& other) const {
mtklein 2014/07/17 19:36:20 SkTDynamicHash won't need this. Is this dead code
robertphillips 2014/07/17 20:02:47 Done.
50 if (fPictureID == other.fPictureID) {
51 return fLayerID < other.fLayerID;
52 }
53 return fPictureID < other.fPictureID;
54 }
55
56 bool operator==(const Key& other) const {
57 return fPictureID == other.fPictureID && fLayerID == other.fLayerID;
58 }
59
60 uint32_t getHash() const { return (fPictureID << 16) | fLayerID; }
mtklein 2014/07/17 19:36:20 Again, SkChecksum::Mix() is a good idea. Using th
robertphillips 2014/07/17 20:02:46 Done.
61
62 uint32_t getPictureID() const { return fPictureID; }
63 int getLayerID() const { return fLayerID; }
64
65 private:
66 // ID of the picture of which this layer is a part
67 const uint32_t fPictureID;
68 // fLayerID is the index of this layer in the picture (one of 0 .. #laye rs).
69 const int fLayerID;
70 };
71
72 static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; }
73 static uint32_t Hash(const Key& key) { return key.getHash(); }
mtklein 2014/07/17 19:36:20 Consider inlining Key::getHash into this?
robertphillips 2014/07/17 20:02:47 Done.
74
75 // GrCachedLayer proper
40 GrCachedLayer(uint32_t pictureID, int layerID) 76 GrCachedLayer(uint32_t pictureID, int layerID)
41 : fPlot(NULL) { 77 : fKey(pictureID, layerID)
42 fPictureID = pictureID; 78 , fTexture(NULL)
43 fLayerID = layerID; 79 , fRect(GrIRect16::MakeEmpty()),
44 fTexture = NULL; 80 » , fPlot(NULL) {
mtklein 2014/07/17 19:36:20 Stray tab?
robertphillips 2014/07/17 20:02:46 Done.
45 fRect = GrIRect16::MakeEmpty(); 81 SkASSERT(SK_InvalidGenID != pictureID && layerID >= 0);
46 } 82 }
47 83
48 uint32_t pictureID() const { return fPictureID; } 84 uint32_t pictureID() const { return fKey.getPictureID(); }
49 int layerID() const { return fLayerID; } 85 int layerID() const { return fKey.getLayerID(); }
50 86
51 // This call takes over the caller's ref 87 // This call takes over the caller's ref
52 void setTexture(GrTexture* texture, const GrIRect16& rect) { 88 void setTexture(GrTexture* texture, const GrIRect16& rect) {
53 if (NULL != fTexture) { 89 if (NULL != fTexture) {
54 fTexture->unref(); 90 fTexture->unref();
55 } 91 }
56 92
57 fTexture = texture; // just take over caller's ref 93 fTexture = texture; // just take over caller's ref
58 fRect = rect; 94 fRect = rect;
59 } 95 }
60 GrTexture* texture() { return fTexture; } 96 GrTexture* texture() { return fTexture; }
61 const GrIRect16& rect() const { return fRect; } 97 const GrIRect16& rect() const { return fRect; }
62 98
63 void setPlot(GrPlot* plot) { 99 void setPlot(GrPlot* plot) {
64 SkASSERT(NULL == fPlot); 100 SkASSERT(NULL == fPlot);
65 fPlot = plot; 101 fPlot = plot;
66 } 102 }
67 GrPlot* plot() { return fPlot; } 103 GrPlot* plot() { return fPlot; }
68 104
69 bool isAtlased() const { return NULL != fPlot; } 105 bool isAtlased() const { return NULL != fPlot; }
70 106
71 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) 107 SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;)
72 108
73 private: 109 private:
74 // ID of the picture of which this layer is a part 110 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 111
81 // fTexture is a ref on the atlasing texture for atlased layers and a 112 // 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 113 // 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. 114 // non-NULL, that means that the texture is locked in the texture cache.
84 GrTexture* fTexture; 115 GrTexture* fTexture;
85 116
86 // For both atlased and non-atlased layers 'fRect' contains the bound of 117 // 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' 118 // the layer in whichever texture it resides. It is empty when 'fTexture'
88 // is NULL. 119 // is NULL.
89 GrIRect16 fRect; 120 GrIRect16 fRect;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 static const int kNumPlotsY = 2; 162 static const int kNumPlotsY = 2;
132 163
133 GrContext* fContext; // pointer back to owning context 164 GrContext* fContext; // pointer back to owning context
134 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate 165 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate
135 166
136 // We cache this information here (rather then, say, on the owning picture) 167 // 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 168 // 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 169 // 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 170 // remove the GrPictureInfo once its layers are purged from all the atlas
140 // plots). 171 // plots).
141 class PictureKey; 172 SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash;
142 GrTHashTable<GrPictureInfo, PictureKey, 7> fPictureHash;
143 173
144 class PictureLayerKey; 174 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash;
145 GrTHashTable<GrCachedLayer, PictureLayerKey, 7> fLayerHash;
146 175
147 void initAtlas(); 176 void initAtlas();
148 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); 177 GrCachedLayer* createLayer(const SkPicture* picture, int layerID);
149 178
150 // for testing 179 // for testing
151 friend class GetNumLayers; 180 friend class GetNumLayers;
152 int numLayers() const { return fLayerHash.count(); } 181 int numLayers() const { return fLayerHash.count(); }
153 }; 182 };
154 183
155 #endif 184 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698