blob: f6377bf7e92e5713196296114a545dd4dfb5e177 [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:
skia.committer@gmail.coma9157722014-04-03 03:04:26 +000017 PictureLayerKey(uint32_t pictureID, int layerID)
robertphillips@google.come930a072014-04-03 00:34:27 +000018 : 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
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000027 static bool LessThan(const GrCachedLayer& layer, const PictureLayerKey& key) {
robertphillips@google.come930a072014-04-03 00:34:27 +000028 if (layer.pictureID() == key.pictureID()) {
29 return layer.layerID() < key.layerID();
30 }
31
32 return layer.pictureID() < key.pictureID();
33 }
34
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000035 static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) {
robertphillips@google.come930a072014-04-03 00:34:27 +000036 return layer.pictureID() == key.pictureID() && layer.layerID() == key.layerID();
37 }
38
39private:
40 uint32_t fPictureID;
41 int fLayerID;
42};
43
skia.committer@gmail.coma9157722014-04-03 03:04:26 +000044GrLayerCache::GrLayerCache(GrGpu* gpu)
robertphillips@google.come930a072014-04-03 00:34:27 +000045 : 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,
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000061 textureSize, 1, 1, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +000062}
63
64void GrLayerCache::freeAll() {
65 fLayerHash.deleteAll();
66 fAtlasMgr.free();
67}
68
robertphillips9b14f262014-06-04 05:40:44 -070069GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) {
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000070 GrCachedLayer* layer = fLayerPool.alloc();
robertphillips@google.come930a072014-04-03 00:34:27 +000071
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +000072 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
73 layer->init(picture->uniqueID(), layerID);
74 fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer);
robertphillips@google.come930a072014-04-03 00:34:27 +000075 return layer;
76}
77
78
robertphillips9b14f262014-06-04 05:40:44 -070079GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) {
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +000080 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000081 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID));
robertphillips@google.come930a072014-04-03 00:34:27 +000082 if (NULL == layer) {
83 layer = this->createLayer(picture, layerID);
84 }
85 return layer;
86}