robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 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: |
skia.committer@gmail.com | a915772 | 2014-04-03 03:04:26 +0000 | [diff] [blame] | 17 | PictureLayerKey(uint32_t pictureID, int layerID) |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 27 | static bool LessThan(const GrCachedLayer& layer, const PictureLayerKey& key) { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 28 | if (layer.pictureID() == key.pictureID()) { |
| 29 | return layer.layerID() < key.layerID(); |
| 30 | } |
| 31 | |
| 32 | return layer.pictureID() < key.pictureID(); |
| 33 | } |
| 34 | |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 35 | static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 36 | return layer.pictureID() == key.pictureID() && layer.layerID() == key.layerID(); |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | uint32_t fPictureID; |
| 41 | int fLayerID; |
| 42 | }; |
| 43 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 44 | GrLayerCache::GrLayerCache(GrContext* context) |
| 45 | : fContext(context) |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 46 | , fLayerPool(16) { // TODO: may need to increase this later |
| 47 | } |
| 48 | |
| 49 | GrLayerCache::~GrLayerCache() { |
| 50 | } |
| 51 | |
| 52 | void GrLayerCache::init() { |
| 53 | static const int kAtlasTextureWidth = 1024; |
| 54 | static const int kAtlasTextureHeight = 1024; |
| 55 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame^] | 56 | SkASSERT(NULL == fAtlas.get()); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 57 | |
| 58 | // The layer cache only gets 1 plot |
| 59 | SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight); |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame^] | 60 | fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig, |
| 61 | textureSize, 1, 1, false))); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void GrLayerCache::freeAll() { |
| 65 | fLayerHash.deleteAll(); |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame^] | 66 | fAtlas.free(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 67 | } |
| 68 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 69 | GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) { |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 70 | GrCachedLayer* layer = fLayerPool.alloc(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 72 | SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| 73 | layer->init(picture->uniqueID(), layerID); |
| 74 | fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 75 | return layer; |
| 76 | } |
| 77 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 78 | GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { |
| 79 | SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| 80 | return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
| 81 | } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 82 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 83 | GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) { |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 84 | SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 85 | GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 86 | if (NULL == layer) { |
| 87 | layer = this->createLayer(picture, layerID); |
| 88 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 89 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 90 | return layer; |
| 91 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 92 | |
| 93 | bool 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 | |
| 102 | void 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 | } |