blob: c20d8090621cfc3509a4dbd817a5279930a2416e [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
robertphillips4ec84da2014-06-24 13:10:43 -070044GrLayerCache::GrLayerCache(GrContext* context)
45 : fContext(context)
robertphillips@google.come930a072014-04-03 00:34:27 +000046 , 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
robertphillips1d86ee82014-06-24 15:08:49 -070056 SkASSERT(NULL == fAtlas.get());
robertphillips@google.come930a072014-04-03 00:34:27 +000057
58 // The layer cache only gets 1 plot
59 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -070060 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
61 textureSize, 1, 1, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +000062}
63
64void GrLayerCache::freeAll() {
65 fLayerHash.deleteAll();
robertphillips1d86ee82014-06-24 15:08:49 -070066 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +000067}
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
robertphillips4ec84da2014-06-24 13:10:43 -070078GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) {
79 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
80 return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID));
81}
robertphillips@google.come930a072014-04-03 00:34:27 +000082
robertphillips9b14f262014-06-04 05:40:44 -070083GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) {
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +000084 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
commit-bot@chromium.org365cd312014-04-11 15:53:47 +000085 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID));
robertphillips@google.come930a072014-04-03 00:34:27 +000086 if (NULL == layer) {
87 layer = this->createLayer(picture, layerID);
88 }
robertphillips4ec84da2014-06-24 13:10:43 -070089
robertphillips@google.come930a072014-04-03 00:34:27 +000090 return layer;
91}
robertphillips4ec84da2014-06-24 13:10:43 -070092
93bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) {
94 SkASSERT(NULL == layer->getTexture());
95
96 // This just uses scratch textures and doesn't cache the texture.
97 // This can yield a lot of re-rendering
98 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
99 return false;
100}
101
102void GrLayerCache::unlock(GrCachedLayer* layer) {
103 if (NULL == layer || NULL == layer->getTexture()) {
104 return;
105 }
106
107 fContext->unlockScratchTexture(layer->getTexture());
108 layer->setTexture(NULL);
109}