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.
10 #include "GrLayerCache.h"
13 * PictureLayerKey just wraps a saveLayer's id in the picture for GrTHashTable.
15 class GrLayerCache::PictureLayerKey {
17 PictureLayerKey(uint32_t pictureID, int layerID)
18 : fPictureID(pictureID)
22 uint32_t pictureID() const { return fPictureID; }
23 int layerID() const { return fLayerID; }
25 uint32_t getHash() const { return (fPictureID << 16) | fLayerID; }
27 static bool LessThan(const GrCachedLayer& layer, const PictureLayerKey& key) {
28 if (layer.pictureID() == key.pictureID()) {
29 return layer.layerID() < key.layerID();
32 return layer.pictureID() < key.pictureID();
35 static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) {
36 return layer.pictureID() == key.pictureID() && layer.layerID() == key.layerID();
44 GrLayerCache::GrLayerCache(GrGpu* gpu)
46 , fLayerPool(16) { // TODO: may need to increase this later
49 GrLayerCache::~GrLayerCache() {
52 void GrLayerCache::init() {
53 static const int kAtlasTextureWidth = 1024;
54 static const int kAtlasTextureHeight = 1024;
56 SkASSERT(NULL == fAtlasMgr.get());
58 // The layer cache only gets 1 plot
59 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
60 fAtlasMgr.reset(SkNEW_ARGS(GrAtlasMgr, (fGpu, kSkia8888_GrPixelConfig,
64 void GrLayerCache::freeAll() {
65 fLayerHash.deleteAll();
69 GrCachedLayer* GrLayerCache::createLayer(SkPicture* picture, int layerID) {
70 GrCachedLayer* layer = fLayerPool.alloc();
72 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
73 layer->init(picture->uniqueID(), layerID);
74 fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer);
79 GrCachedLayer* GrLayerCache::findLayerOrCreate(SkPicture* picture, int layerID) {
80 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
81 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID));
83 layer = this->createLayer(picture, layerID);