Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2570)

Side by Side Diff: src/gpu/GrLayerCache.cpp

Issue 354533004: Begin atlasing (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix unit test Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrAtlas.h" 8 #include "GrAtlas.h"
9 #include "GrGpu.h" 9 #include "GrGpu.h"
10 #include "GrLayerCache.h" 10 #include "GrLayerCache.h"
(...skipping 24 matching lines...) Expand all
35 static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) { 35 static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) {
36 return layer.pictureID() == key.pictureID() && layer.layerID() == key.la yerID(); 36 return layer.pictureID() == key.pictureID() && layer.layerID() == key.la yerID();
37 } 37 }
38 38
39 private: 39 private:
40 uint32_t fPictureID; 40 uint32_t fPictureID;
41 int fLayerID; 41 int fLayerID;
42 }; 42 };
43 43
44 GrLayerCache::GrLayerCache(GrContext* context) 44 GrLayerCache::GrLayerCache(GrContext* context)
45 : fContext(context) 45 : fContext(context) {
46 , fLayerPool(16) { // TODO: may need to increase this later 46 this->initAtlas();
47 } 47 }
48 48
49 GrLayerCache::~GrLayerCache() { 49 GrLayerCache::~GrLayerCache() {
50 } 50 }
51 51
52 void GrLayerCache::init() { 52 void GrLayerCache::initAtlas() {
53 static const int kAtlasTextureWidth = 1024; 53 static const int kAtlasTextureWidth = 1024;
54 static const int kAtlasTextureHeight = 1024; 54 static const int kAtlasTextureHeight = 1024;
55 55
56 SkASSERT(NULL == fAtlas.get()); 56 SkASSERT(NULL == fAtlas.get());
57 57
58 // The layer cache only gets 1 plot 58 // The layer cache only gets 1 plot
59 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight) ; 59 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight) ;
60 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfi g, 60 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfi g,
61 kRenderTarget_GrTextureFlagBit,
61 textureSize, 1, 1, false))); 62 textureSize, 1, 1, false)));
62 } 63 }
63 64
64 void GrLayerCache::freeAll() { 65 void GrLayerCache::freeAll() {
66 SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray();
67 for (int i = 0; i < fLayerHash.count(); ++i) {
68 this->unlock(layerArray[i]);
69 }
70
65 fLayerHash.deleteAll(); 71 fLayerHash.deleteAll();
72
73 // The atlas only lets go of its texture when the atlas is deleted.
66 fAtlas.free(); 74 fAtlas.free();
75 // GrLayerCache always assumes an atlas exists so recreate it. The atlas
76 // lazily allocates a replacement texture so reallocating a new
77 // atlas here won't disrupt a GrContext::contextDestroyed or freeGpuResource s.
78 // TODO: Make GrLayerCache lazily allocate the atlas manager?
79 this->initAtlas();
67 } 80 }
68 81
69 GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) { 82 GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) {
70 GrCachedLayer* layer = fLayerPool.alloc(); 83 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
71 84
72 SkASSERT(picture->uniqueID() != SK_InvalidGenID); 85 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layer ID));
73 layer->init(picture->uniqueID(), layerID);
74 fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); 86 fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer);
75 return layer; 87 return layer;
76 } 88 }
77 89
78 GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { 90 GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) {
79 SkASSERT(picture->uniqueID() != SK_InvalidGenID); 91 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
80 return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); 92 return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID));
81 } 93 }
82 94
83 GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int lay erID) { 95 GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int lay erID) {
84 SkASSERT(picture->uniqueID() != SK_InvalidGenID); 96 SkASSERT(picture->uniqueID() != SK_InvalidGenID);
85 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); 97 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID));
86 if (NULL == layer) { 98 if (NULL == layer) {
87 layer = this->createLayer(picture, layerID); 99 layer = this->createLayer(picture, layerID);
88 } 100 }
89 101
90 return layer; 102 return layer;
91 } 103 }
92 104
93 bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) { 105 bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) {
94 SkASSERT(NULL == layer->getTexture());
95 106
96 // This just uses scratch textures and doesn't cache the texture. 107 if (NULL != layer->texture()) {
108 // This layer is already locked
109 #ifdef SK_DEBUG
110 if (!layer->rect().isEmpty()) {
111 // It claims to be atlased
112 SkASSERT(layer->rect().width() == desc.fWidth);
113 SkASSERT(layer->rect().height() == desc.fHeight);
114 }
115 #endif
116 return true;
117 }
118
119 #if USE_ATLAS
120 SkIPoint16 loc;
121 GrPlot* plot = fAtlas->addToAtlas(&fPlotUsage, desc.fWidth, desc.fHeight, NU LL, &loc);
122 if (NULL != plot) {
123 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
124 SkToS16(desc.fWidth), SkToS16(des c.fHeight));
125 layer->setTexture(fAtlas->getTexture(), bounds);
126 return false;
127 }
128 #endif
129
130 // This path always uses a new scratch texture and (thus) doesn't cache anyt hing.
97 // This can yield a lot of re-rendering 131 // This can yield a lot of re-rendering
98 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kAppro x_ScratchTexMatch)); 132 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kAppro x_ScratchTexMatch),
133 GrIRect16::MakeEmpty());
99 return false; 134 return false;
100 } 135 }
101 136
102 void GrLayerCache::unlock(GrCachedLayer* layer) { 137 void GrLayerCache::unlock(GrCachedLayer* layer) {
103 if (NULL == layer || NULL == layer->getTexture()) { 138 if (NULL == layer || NULL == layer->texture()) {
104 return; 139 return;
105 } 140 }
106 141
107 fContext->unlockScratchTexture(layer->getTexture()); 142 // The atlas doesn't currently use a scratch texture (and we would have
108 layer->setTexture(NULL); 143 // to free up space differently anyways)
144 // TODO: unlock atlas space when a recycling rectanizer is available
145 if (layer->texture() != fAtlas->getTexture()) {
146 fContext->unlockScratchTexture(layer->texture());
147 layer->setTexture(NULL, GrIRect16::MakeEmpty());
148 }
109 } 149 }
150
151 void GrLayerCache::purge(const SkPicture* picture) {
152 // This is somewhat of an abuse of GrTHashTable. We need to find all the
153 // layers associated with 'picture' but the usual hash calls only look for
154 // exact key matches. This code peeks into the hash table's innards to
155 // find all the 'picture'-related layers.
156 // TODO: use a different data structure for the layer hash?
157 SkTDArray<GrCachedLayer*> toBeRemoved;
158
159 const SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray();
160 for (int i = 0; i < fLayerHash.count(); ++i) {
161 if (picture->uniqueID() == layerArray[i]->pictureID()) {
162 *toBeRemoved.append() = layerArray[i];
163 }
164 }
165
166 for (int i = 0; i < toBeRemoved.count(); ++i) {
167 this->unlock(toBeRemoved[i]);
168
169 PictureLayerKey key(picture->uniqueID(), toBeRemoved[i]->layerID());
170 fLayerHash.remove(key, toBeRemoved[i]);
171 }
172 }
OLDNEW
« include/core/SkSurface.h ('K') | « src/gpu/GrLayerCache.h ('k') | src/gpu/GrTextStrike.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698