2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrLayerCache_DEFINED
9 #define GrLayerCache_DEFINED
11 #include "GrAllocPool.h"
12 #include "GrTHashTable.h"
13 #include "GrPictureUtils.h"
21 // GrAtlasLocation captures an atlased item's position in the atlas. This
22 // means the plot in which it resides and its bounds inside the plot.
23 // TODO: Make GrGlyph use one of these?
24 class GrAtlasLocation {
26 GrAtlasLocation() : fPlot(NULL) {}
28 void set(GrPlot* plot, const GrIRect16& bounds) {
33 const GrPlot* plot() const {
37 const GrIRect16& bounds() const {
43 GrIRect16 fBounds; // only valid is fPlot != NULL
46 // GrCachedLayer encapsulates the caching information for a single saveLayer.
48 // Atlased layers get a ref to their atlas GrTexture and their GrAtlasLocation
50 // In this case GrCachedLayer is roughly equivalent to a GrGlyph in the font
53 // Non-atlased layers get a ref to the GrTexture in which they reside.
54 // TODO: can we easily reuse the empty space in the non-atlased GrTexture's?
55 struct GrCachedLayer {
57 uint32_t pictureID() const { return fPictureID; }
58 int layerID() const { return fLayerID; }
60 void init(uint32_t pictureID, int layerID) {
61 fPictureID = pictureID;
64 fLocation.set(NULL, GrIRect16::MakeEmpty());
67 // This call takes over the caller's ref
68 void setTexture(GrTexture* texture) {
69 if (NULL != fTexture) {
73 fTexture = texture; // just take over caller's ref
75 GrTexture* getTexture() { return fTexture; }
79 // fLayerID is only valid when fPicture != kInvalidGenID in which case it
80 // is the index of this layer in the picture (one of 0 .. #layers).
83 // fTexture is a ref on the atlasing texture for atlased layers and a
84 // ref on a GrTexture for non-atlased textures. In both cases, if this is
85 // non-NULL, that means that the texture is locked in the texture cache.
88 GrAtlasLocation fLocation; // only valid if the layer is atlased
91 // The GrLayerCache caches pre-computed saveLayers for later rendering.
92 // Non-atlased layers are stored in their own GrTexture while the atlased
93 // layers share a single GrTexture.
94 // Unlike the GrFontCache, the GrTexture atlas only has one GrAtlasMgr (for 8888)
95 // and one GrPlot (for the entire atlas). As such, the GrLayerCache
96 // roughly combines the functionality of the GrFontCache and GrTextStrike
100 GrLayerCache(GrGpu*);
105 GrCachedLayer* findLayerOrCreate(SkPicture* picture, int id);
108 SkAutoTUnref<GrGpu> fGpu;
109 SkAutoTDelete<GrAtlasMgr> fAtlasMgr; // TODO: could lazily allocate
111 class PictureLayerKey;
112 GrTHashTable<GrCachedLayer, PictureLayerKey, 7> fLayerHash;
113 GrTAllocPool<GrCachedLayer> fLayerPool;
116 GrCachedLayer* createLayer(SkPicture* picture, int id);