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 | |
12 | |
13 #include "GrAllocPool.h" | 11 #include "GrAllocPool.h" |
14 #include "GrAtlas.h" | 12 #include "GrAtlas.h" |
15 #include "GrTHashTable.h" | 13 #include "GrTHashTable.h" |
16 #include "GrPictureUtils.h" | 14 #include "GrPictureUtils.h" |
17 #include "GrRect.h" | 15 #include "GrRect.h" |
18 | 16 |
19 class GrGpu; | 17 class GrGpu; |
20 class SkPicture; | 18 class SkPicture; |
21 | 19 |
| 20 // GrAtlasLocation captures an atlased item's position in the atlas. This |
| 21 // means the plot in which it resides and its bounds inside the plot. |
| 22 // TODO: Make GrGlyph use one of these? |
| 23 class GrAtlasLocation { |
| 24 public: |
| 25 GrAtlasLocation() : fPlot(NULL) {} |
| 26 |
| 27 void set(GrPlot* plot, const GrIRect16& bounds) { |
| 28 fPlot = plot; |
| 29 fBounds = bounds; |
| 30 } |
| 31 |
| 32 const GrPlot* plot() const { |
| 33 return fPlot; |
| 34 } |
| 35 |
| 36 const GrIRect16& bounds() const { |
| 37 return fBounds; |
| 38 } |
| 39 |
| 40 private: |
| 41 GrPlot* fPlot; |
| 42 GrIRect16 fBounds; // only valid is fPlot != NULL |
| 43 }; |
| 44 |
22 // GrCachedLayer encapsulates the caching information for a single saveLayer. | 45 // GrCachedLayer encapsulates the caching information for a single saveLayer. |
23 // | 46 // |
24 // Atlased layers get a ref to their atlas GrTexture and 'fRect' contains | 47 // Atlased layers get a ref to their atlas GrTexture and their GrAtlasLocation |
25 // their absolute location in the backing texture. | 48 // is filled in. |
| 49 // In this case GrCachedLayer is roughly equivalent to a GrGlyph in the font |
| 50 // caching system. |
26 // | 51 // |
27 // Non-atlased layers get a ref to the GrTexture in which they reside. Their | 52 // Non-atlased layers get a ref to the GrTexture in which they reside. |
28 // 'fRect' will be empty. | |
29 // | |
30 // TODO: can we easily reuse the empty space in the non-atlased GrTexture's? | 53 // TODO: can we easily reuse the empty space in the non-atlased GrTexture's? |
31 struct GrCachedLayer { | 54 struct GrCachedLayer { |
32 public: | 55 public: |
33 GrCachedLayer(uint32_t pictureID, int layerID) { | |
34 fPictureID = pictureID; | |
35 fLayerID = layerID; | |
36 fTexture = NULL; | |
37 fRect = GrIRect16::MakeEmpty(); | |
38 } | |
39 | |
40 uint32_t pictureID() const { return fPictureID; } | 56 uint32_t pictureID() const { return fPictureID; } |
41 int layerID() const { return fLayerID; } | 57 int layerID() const { return fLayerID; } |
42 | 58 |
| 59 void init(uint32_t pictureID, int layerID) { |
| 60 fPictureID = pictureID; |
| 61 fLayerID = layerID; |
| 62 fTexture = NULL; |
| 63 fLocation.set(NULL, GrIRect16::MakeEmpty()); |
| 64 } |
| 65 |
43 // This call takes over the caller's ref | 66 // This call takes over the caller's ref |
44 void setTexture(GrTexture* texture, const GrIRect16& rect) { | 67 void setTexture(GrTexture* texture) { |
45 if (NULL != fTexture) { | 68 if (NULL != fTexture) { |
46 fTexture->unref(); | 69 fTexture->unref(); |
47 } | 70 } |
48 | 71 |
49 fTexture = texture; // just take over caller's ref | 72 fTexture = texture; // just take over caller's ref |
50 fRect = rect; | |
51 } | 73 } |
52 GrTexture* texture() { return fTexture; } | 74 GrTexture* getTexture() { return fTexture; } |
53 const GrIRect16& rect() const { return fRect; } | |
54 | 75 |
55 private: | 76 private: |
56 uint32_t fPictureID; | 77 uint32_t fPictureID; |
57 // fLayerID is only valid when fPicture != kInvalidGenID in which case it | 78 // fLayerID is only valid when fPicture != kInvalidGenID in which case it |
58 // is the index of this layer in the picture (one of 0 .. #layers). | 79 // is the index of this layer in the picture (one of 0 .. #layers). |
59 int fLayerID; | 80 int fLayerID; |
60 | 81 |
61 // fTexture is a ref on the atlasing texture for atlased layers and a | 82 // fTexture is a ref on the atlasing texture for atlased layers and a |
62 // ref on a GrTexture for non-atlased textures. In both cases, if this is | 83 // ref on a GrTexture for non-atlased textures. In both cases, if this is |
63 // non-NULL, that means that the texture is locked in the texture cache. | 84 // non-NULL, that means that the texture is locked in the texture cache. |
64 GrTexture* fTexture; | 85 GrTexture* fTexture; |
65 | 86 |
66 // For non-atlased layers 'fRect' is empty otherwise it is the bound of | 87 GrAtlasLocation fLocation; // only valid if the layer is atlased |
67 // the layer in the atlas. | |
68 GrIRect16 fRect; | |
69 }; | 88 }; |
70 | 89 |
71 // The GrLayerCache caches pre-computed saveLayers for later rendering. | 90 // The GrLayerCache caches pre-computed saveLayers for later rendering. |
72 // Non-atlased layers are stored in their own GrTexture while the atlased | 91 // Non-atlased layers are stored in their own GrTexture while the atlased |
73 // layers share a single GrTexture. | 92 // layers share a single GrTexture. |
74 // Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888) | 93 // Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888) |
75 // and one GrPlot (for the entire atlas). As such, the GrLayerCache | 94 // and one GrPlot (for the entire atlas). As such, the GrLayerCache |
76 // roughly combines the functionality of the GrFontCache and GrTextStrike | 95 // roughly combines the functionality of the GrFontCache and GrTextStrike |
77 // classes. | 96 // classes. |
78 class GrLayerCache { | 97 class GrLayerCache { |
(...skipping 10 matching lines...) Expand all Loading... |
89 | 108 |
90 // Inform the cache that layer's cached image is now required. Return true | 109 // Inform the cache that layer's cached image is now required. Return true |
91 // if it was found in the ResourceCache and doesn't need to be regenerated. | 110 // if it was found in the ResourceCache and doesn't need to be regenerated. |
92 // If false is returned the caller should (re)render the layer into the | 111 // If false is returned the caller should (re)render the layer into the |
93 // newly acquired texture. | 112 // newly acquired texture. |
94 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc); | 113 bool lock(GrCachedLayer* layer, const GrTextureDesc& desc); |
95 | 114 |
96 // Inform the cache that layer's cached image is not currently required | 115 // Inform the cache that layer's cached image is not currently required |
97 void unlock(GrCachedLayer* layer); | 116 void unlock(GrCachedLayer* layer); |
98 | 117 |
99 // Remove all the layers (and unlock any resources) associated with 'picture
' | |
100 void purge(const SkPicture* picture); | |
101 | |
102 private: | 118 private: |
103 GrContext* fContext; // pointer back to owning context | 119 GrContext* fContext; // pointer back to owning context |
104 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate | 120 SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate |
105 GrAtlas::ClientPlotUsage fPlotUsage; | 121 GrAtlas::ClientPlotUsage fPlotUsage; |
106 | 122 |
107 class PictureLayerKey; | 123 class PictureLayerKey; |
108 GrTHashTable<GrCachedLayer, PictureLayerKey, 7> fLayerHash; | 124 GrTHashTable<GrCachedLayer, PictureLayerKey, 7> fLayerHash; |
| 125 GrTAllocPool<GrCachedLayer> fLayerPool; |
109 | 126 |
110 void initAtlas(); | 127 void init(); |
111 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); | 128 GrCachedLayer* createLayer(const SkPicture* picture, int layerID); |
112 | |
113 // for testing | |
114 friend class GetNumLayers; | |
115 int numLayers() const { return fLayerHash.count(); } | |
116 }; | 129 }; |
117 | 130 |
118 #endif | 131 #endif |
OLD | NEW |