GrLayerCache.h revision 2b4e370a2fe00168838e43f5a78ccc3b371609f5
1/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrLayerCache_DEFINED
9#define GrLayerCache_DEFINED
10
11#include "GrAllocPool.h"
12#include "GrTHashTable.h"
13#include "GrPictureUtils.h"
14#include "GrRect.h"
15
16class GrAtlasMgr;
17class GrGpu;
18class GrPlot;
19class SkPicture;
20
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?
24class GrAtlasLocation {
25public:
26    GrAtlasLocation() : fPlot(NULL) {}
27
28    void set(GrPlot* plot, const GrIRect16& bounds) {
29        fPlot = plot;
30        fBounds = bounds;
31    }
32
33    const GrPlot* plot() const {
34        return fPlot;
35    }
36
37    const GrIRect16& bounds() const {
38        return fBounds;
39    }
40
41private:
42    GrPlot*   fPlot;
43    GrIRect16 fBounds;  // only valid is fPlot != NULL
44};
45
46// A GrAtlasedLayer encapsulates the atlasing information for a single saveLayer.
47// It is roughly equivalent to a GrGlyph in the font caching system
48class GrAtlasedLayer {
49public:
50    GrAtlasedLayer() : fPictureID(SK_InvalidGenID) { }
51
52    uint32_t pictureID() const { return fPictureID; }
53    int layerID() const { return fLayerID; }
54
55    void init(uint32_t pictureID, int layerID) {
56        fPictureID = pictureID;
57        fLayerID   = layerID;
58    }
59
60private:
61    uint32_t        fPictureID;
62    int             fLayerID;        // only valid if fPicture != kInvalidGenID
63    GrAtlasLocation fLocation;
64};
65
66// The GrLayerCache caches pre-computed saveLayers for later rendering.
67// Unlike the GrFontCache, this cache only has one GrAtlasMgr (for 8888)
68// and one GrPlot (for the entire atlas). As such, the GrLayerCache
69// roughly combines the functionality of the GrFontCache and GrTextStrike
70// classes.
71class GrLayerCache {
72public:
73    GrLayerCache(GrGpu*);
74    ~GrLayerCache();
75
76    void freeAll();
77
78    const GrAtlasedLayer* findLayerOrCreate(SkPicture* picture, int id);
79
80private:
81    SkAutoTUnref<GrGpu>       fGpu;
82    SkAutoTDelete<GrAtlasMgr> fAtlasMgr; // TODO: could lazily allocate
83
84    class PictureLayerKey;
85    GrTHashTable<GrAtlasedLayer, PictureLayerKey, 7> fLayerHash;
86    GrTAllocPool<GrAtlasedLayer> fLayerPool;
87
88    void init();
89    GrAtlasedLayer* createLayer(SkPicture* picture, int id);
90
91};
92
93#endif
94