blob: 4120c54d8fd8b940e460149055e3c706f458ac9a [file] [log] [blame]
robertphillips@google.come930a072014-04-03 00:34:27 +00001/*
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 */
15class GrLayerCache::PictureLayerKey {
16public:
17 PictureLayerKey(uint32_t pictureID, int layerID)
18 : fPictureID(pictureID)
19 , fLayerID(layerID) {
20 }
21
22 uint32_t pictureID() const { return fPictureID; }
23 int layerID() const { return fLayerID; }
24
25 uint32_t getHash() const { return (fPictureID << 16) | fLayerID; }
26
27 static bool LessThan(const GrAtlasedLayer& 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
35 static bool Equals(const GrAtlasedLayer& layer, const PictureLayerKey& key) {
36 return layer.pictureID() == key.pictureID() && layer.layerID() == key.layerID();
37 }
38
39private:
40 uint32_t fPictureID;
41 int fLayerID;
42};
43
44GrLayerCache::GrLayerCache(GrGpu* gpu)
45 : fGpu(SkRef(gpu))
46 , fLayerPool(16) { // TODO: may need to increase this later
47}
48
49GrLayerCache::~GrLayerCache() {
50}
51
52void 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)));
62}
63
64void GrLayerCache::freeAll() {
65 fLayerHash.deleteAll();
66 fAtlasMgr.free();
67}
68
69GrAtlasedLayer* GrLayerCache::createLayer(SkPicture* picture, int layerID) {
70 GrAtlasedLayer* layer = fLayerPool.alloc();
71
72 SkASSERT(picture->getGenerationID() != SkPicture::kInvalidGenID);
73 layer->init(picture->getGenerationID(), layerID);
74 fLayerHash.insert(PictureLayerKey(picture->getGenerationID(), layerID), layer);
75 return layer;
76}
77
78
79const GrAtlasedLayer* GrLayerCache::findLayerOrCreate(SkPicture* picture, int layerID) {
80 SkASSERT(picture->getGenerationID() != SkPicture::kInvalidGenID);
81 GrAtlasedLayer* layer = fLayerHash.find(PictureLayerKey(picture->getGenerationID(), layerID));
82 if (NULL == layer) {
83 layer = this->createLayer(picture, layerID);
84 }
85 return layer;
86}