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 #include "GrAtlas.h"
9 #include "GrGpu.h"
10 #include "GrLayerCache.h"
11
12 /**
13 * PictureLayerKey just wraps a saveLayer's id in the picture for GrTHashTable.
14 */
15 class GrLayerCache::PictureLayerKey {
16 public:
PictureLayerKey(uint32_t pictureID,int layerID)17 PictureLayerKey(uint32_t pictureID, int layerID)
18 : fPictureID(pictureID)
19 , fLayerID(layerID) {
20 }
21
pictureID() const22 uint32_t pictureID() const { return fPictureID; }
layerID() const23 int layerID() const { return fLayerID; }
24
getHash() const25 uint32_t getHash() const { return (fPictureID << 16) | fLayerID; }
26
LessThan(const GrCachedLayer & layer,const PictureLayerKey & key)27 static bool LessThan(const GrCachedLayer& layer, const PictureLayerKey& key) {
28 if (layer.pictureID() == key.pictureID()) {
29 return layer.layerID() < key.layerID();
30 }
31
32 return layer.pictureID() < key.pictureID();
33 }
34
Equals(const GrCachedLayer & layer,const PictureLayerKey & key)35 static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) {
36 return layer.pictureID() == key.pictureID() && layer.layerID() == key.layerID();
37 }
38
39 private:
40 uint32_t fPictureID;
41 int fLayerID;
42 };
43
GrLayerCache(GrGpu * gpu)44 GrLayerCache::GrLayerCache(GrGpu* gpu)
45 : fGpu(SkRef(gpu))
46 , fLayerPool(16) { // TODO: may need to increase this later
47 }
48
~GrLayerCache()49 GrLayerCache::~GrLayerCache() {
50 }
51
init()52 void GrLayerCache::init() {
53 static const int kAtlasTextureWidth = 1024;
54 static const int kAtlasTextureHeight = 1024;
55
56 SkASSERT(NULL == fAtlasMgr.get());
57
58 // The layer cache only gets 1 plot
59 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
60 fAtlasMgr.reset(SkNEW_ARGS(GrAtlasMgr, (fGpu, kSkia8888_GrPixelConfig,
61 textureSize, 1, 1, false)));
62 }
63
freeAll()64 void GrLayerCache::freeAll() {
65 fLayerHash.deleteAll();
66 fAtlasMgr.free();
67 }
68
createLayer(const SkPicture * picture,int layerID)69 GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) {
70 GrCachedLayer* layer = fLayerPool.alloc();
71
72 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
73 layer->init(picture->uniqueID(), layerID);
74 fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer);
75 return layer;
76 }
77
78
findLayerOrCreate(const SkPicture * picture,int layerID)79 GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) {
80 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
81 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID));
82 if (NULL == layer) {
83 layer = this->createLayer(picture, layerID);
84 }
85 return layer;
86 }
87