OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2010 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 "GrGpuResourcePriv.h" | |
9 #include "GrLayerAtlas.h" | |
10 #include "GrRectanizer.h" | |
11 #include "GrTextureProvider.h" | |
12 | |
13 /////////////////////////////////////////////////////////////////////////////// | |
14 GrLayerAtlas::Plot::Plot() | |
15 : fID(-1) | |
16 , fRects(nullptr) { | |
17 fOffset.set(0, 0); | |
18 } | |
19 | |
20 GrLayerAtlas::Plot::~Plot() { | |
21 delete fRects; | |
22 } | |
23 | |
24 void GrLayerAtlas::Plot::init(int id, int offX, int offY, int width, int height)
{ | |
25 fID = id; | |
26 fRects = GrRectanizer::Factory(width, height); | |
27 fOffset.set(offX * width, offY * height); | |
28 } | |
29 | |
30 bool GrLayerAtlas::Plot::allocateRect(int width, int height, SkIPoint16* loc) { | |
31 if (!fRects->addRect(width, height, loc)) { | |
32 return false; | |
33 } | |
34 | |
35 loc->fX += fOffset.fX; | |
36 loc->fY += fOffset.fY; | |
37 return true; | |
38 } | |
39 | |
40 void GrLayerAtlas::Plot::reset() { | |
41 SkASSERT(fRects); | |
42 fRects->reset(); | |
43 } | |
44 | |
45 /////////////////////////////////////////////////////////////////////////////// | |
46 GR_DECLARE_STATIC_UNIQUE_KEY(gLayerAtlasKey); | |
47 static const GrUniqueKey& get_layer_atlas_key() { | |
48 GR_DEFINE_STATIC_UNIQUE_KEY(gLayerAtlasKey); | |
49 return gLayerAtlasKey; | |
50 } | |
51 | |
52 bool GrLayerAtlas::reattachBackingTexture() { | |
53 SkASSERT(!fTexture); | |
54 | |
55 fTexture.reset(fTexProvider->findAndRefTextureByUniqueKey(get_layer_atlas_ke
y())); | |
56 return fTexture != nullptr; | |
57 } | |
58 | |
59 void GrLayerAtlas::createBackingTexture() { | |
60 SkASSERT(!fTexture); | |
61 | |
62 GrSurfaceDesc desc; | |
63 desc.fFlags = fFlags; | |
64 desc.fWidth = fBackingTextureSize.width(); | |
65 desc.fHeight = fBackingTextureSize.height(); | |
66 desc.fConfig = fPixelConfig; | |
67 | |
68 fTexture.reset(fTexProvider->createTexture(desc, SkBudgeted::kYes, nullptr,
0)); | |
69 | |
70 fTexture->resourcePriv().setUniqueKey(get_layer_atlas_key()); | |
71 } | |
72 | |
73 GrLayerAtlas::GrLayerAtlas(GrTextureProvider* texProvider, GrPixelConfig config, | |
74 GrSurfaceFlags flags, | |
75 const SkISize& backingTextureSize, | |
76 int numPlotsX, int numPlotsY) { | |
77 fTexProvider = texProvider; | |
78 fPixelConfig = config; | |
79 fFlags = flags; | |
80 fBackingTextureSize = backingTextureSize; | |
81 | |
82 int textureWidth = fBackingTextureSize.width(); | |
83 int textureHeight = fBackingTextureSize.height(); | |
84 | |
85 int plotWidth = textureWidth / numPlotsX; | |
86 int plotHeight = textureHeight / numPlotsY; | |
87 | |
88 SkASSERT(plotWidth * numPlotsX == textureWidth); | |
89 SkASSERT(plotHeight * numPlotsY == textureHeight); | |
90 | |
91 // We currently do not support compressed atlases... | |
92 SkASSERT(!GrPixelConfigIsCompressed(config)); | |
93 | |
94 // set up allocated plots | |
95 fPlotArray = new Plot[numPlotsX * numPlotsY]; | |
96 | |
97 Plot* currPlot = fPlotArray; | |
98 for (int y = numPlotsY-1; y >= 0; --y) { | |
99 for (int x = numPlotsX-1; x >= 0; --x) { | |
100 currPlot->init(y*numPlotsX+x, x, y, plotWidth, plotHeight); | |
101 | |
102 // build LRU list | |
103 fPlotList.addToHead(currPlot); | |
104 ++currPlot; | |
105 } | |
106 } | |
107 } | |
108 | |
109 void GrLayerAtlas::resetPlots() { | |
110 PlotIter iter; | |
111 for (Plot* plot = iter.init(fPlotList, PlotIter::kHead_IterStart); plot; plo
t = iter.next()) { | |
112 plot->reset(); | |
113 } | |
114 } | |
115 | |
116 GrLayerAtlas::~GrLayerAtlas() { | |
117 delete[] fPlotArray; | |
118 } | |
119 | |
120 void GrLayerAtlas::makeMRU(Plot* plot) { | |
121 if (fPlotList.head() == plot) { | |
122 return; | |
123 } | |
124 | |
125 fPlotList.remove(plot); | |
126 fPlotList.addToHead(plot); | |
127 }; | |
128 | |
129 GrLayerAtlas::Plot* GrLayerAtlas::addToAtlas(ClientPlotUsage* usage, | |
130 int width, int height, SkIPoint16*
loc) { | |
131 // Iterate through the plots currently being used by this client and see if
we can find a hole. | |
132 // The last one was most recently added and probably most empty. | |
133 // We want to consolidate the uses from individual clients to the same plot(
s) so that | |
134 // when a specific client goes away they are more likely to completely empty
a plot. | |
135 for (int i = usage->numPlots()-1; i >= 0; --i) { | |
136 Plot* plot = usage->plot(i); | |
137 if (plot->allocateRect(width, height, loc)) { | |
138 this->makeMRU(plot); | |
139 return plot; | |
140 } | |
141 } | |
142 | |
143 // before we get a new plot, make sure we have a backing texture | |
144 if (nullptr == fTexture) { | |
145 this->createBackingTexture(); | |
146 if (nullptr == fTexture) { | |
147 return nullptr; | |
148 } | |
149 } | |
150 | |
151 // Now look through all allocated plots for one we can share, in MRU order | |
152 // TODO: its seems like traversing from emptiest to fullest would make more
sense | |
153 PlotList::Iter plotIter; | |
154 plotIter.init(fPlotList, PlotList::Iter::kHead_IterStart); | |
155 Plot* plot; | |
156 while ((plot = plotIter.get())) { | |
157 if (plot->allocateRect(width, height, loc)) { | |
158 this->makeMRU(plot); | |
159 // new plot for atlas, put at end of array | |
160 usage->appendPlot(plot); | |
161 return plot; | |
162 } | |
163 plotIter.next(); | |
164 } | |
165 | |
166 // If the above fails, then the current plot list has no room | |
167 return nullptr; | |
168 } | |
OLD | NEW |