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