OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 #define FONT_CACHE_STATS 0 | 48 #define FONT_CACHE_STATS 0 |
49 #if FONT_CACHE_STATS | 49 #if FONT_CACHE_STATS |
50 static int g_UploadCount = 0; | 50 static int g_UploadCount = 0; |
51 #endif | 51 #endif |
52 | 52 |
53 GrAtlas::GrAtlas(GrAtlasMgr* mgr, int plotX, int plotY, GrMaskFormat format) : | 53 GrAtlas::GrAtlas(GrAtlasMgr* mgr, int plotX, int plotY, GrMaskFormat format) : |
54 fDrawToken(NULL, 0) { | 54 fDrawToken(NULL, 0) { |
55 fAtlasMgr = mgr; // just a pointer, not an owner | 55 fAtlasMgr = mgr; // just a pointer, not an owner |
56 fNext = NULL; | 56 fNext = NULL; |
57 | 57 |
58 fTexture = mgr->getTexture(format); // we're not an owner, just a pointer | 58 fTexture = mgr->getTexture(); // we're not an owner, just a pointer |
59 fPlot.set(plotX, plotY); | 59 fPlot.set(plotX, plotY); |
60 | 60 |
61 fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER, | 61 fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER, |
62 GR_ATLAS_HEIGHT - BORDER); | 62 GR_ATLAS_HEIGHT - BORDER); |
63 | 63 |
64 fMaskFormat = format; | 64 fMaskFormat = format; |
65 | 65 |
66 #ifdef SK_DEBUG | 66 #ifdef SK_DEBUG |
67 // GrPrintf(" GrAtlas %p [%d %d] %d\n", this, plotX, plotY, gCounter); | 67 // GrPrintf(" GrAtlas %p [%d %d] %d\n", this, plotX, plotY, gCounter); |
68 gCounter += 1; | 68 gCounter += 1; |
69 #endif | 69 #endif |
70 } | 70 } |
71 | 71 |
72 GrAtlas::~GrAtlas() { | 72 GrAtlas::~GrAtlas() { |
73 fAtlasMgr->freePlot(fMaskFormat, fPlot.fX, fPlot.fY); | 73 fAtlasMgr->freePlot(fPlot.fX, fPlot.fY); |
74 | 74 |
75 delete fRects; | 75 delete fRects; |
76 | 76 |
77 #ifdef SK_DEBUG | 77 #ifdef SK_DEBUG |
78 --gCounter; | 78 --gCounter; |
79 // GrPrintf("~GrAtlas %p [%d %d] %d\n", this, fPlot.fX, fPlot.fY, gCounter); | 79 // GrPrintf("~GrAtlas %p [%d %d] %d\n", this, fPlot.fX, fPlot.fY, gCounter); |
80 #endif | 80 #endif |
81 } | 81 } |
82 | 82 |
83 bool GrAtlas::RemoveUnusedAtlases(GrAtlasMgr* atlasMgr, GrAtlas** startAtlas) { | 83 bool GrAtlas::RemoveUnusedAtlases(GrAtlasMgr* atlasMgr, GrAtlas** startAtlas) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 154 |
155 #if FONT_CACHE_STATS | 155 #if FONT_CACHE_STATS |
156 ++g_UploadCount; | 156 ++g_UploadCount; |
157 #endif | 157 #endif |
158 | 158 |
159 return true; | 159 return true; |
160 } | 160 } |
161 | 161 |
162 /////////////////////////////////////////////////////////////////////////////// | 162 /////////////////////////////////////////////////////////////////////////////// |
163 | 163 |
164 GrAtlasMgr::GrAtlasMgr(GrGpu* gpu) { | 164 GrAtlasMgr::GrAtlasMgr(GrGpu* gpu, GrMaskFormat format) { |
165 fGpu = gpu; | 165 fGpu = gpu; |
| 166 fMaskFormat = format; |
166 gpu->ref(); | 167 gpu->ref(); |
167 Gr_bzero(fTexture, sizeof(fTexture)); | 168 fTexture = NULL; |
168 for (int i = 0; i < kCount_GrMaskFormats; ++i) { | 169 fPlotMgr = SkNEW_ARGS(GrPlotMgr, (GR_PLOT_WIDTH, GR_PLOT_HEIGHT)); |
169 fPlotMgr[i] = SkNEW_ARGS(GrPlotMgr, (GR_PLOT_WIDTH, GR_PLOT_HEIGHT)); | |
170 } | |
171 } | 170 } |
172 | 171 |
173 GrAtlasMgr::~GrAtlasMgr() { | 172 GrAtlasMgr::~GrAtlasMgr() { |
174 for (size_t i = 0; i < GR_ARRAY_COUNT(fTexture); i++) { | 173 SkSafeUnref(fTexture); |
175 SkSafeUnref(fTexture[i]); | 174 delete fPlotMgr; |
176 } | |
177 for (int i = 0; i < kCount_GrMaskFormats; ++i) { | |
178 delete fPlotMgr[i]; | |
179 } | |
180 | 175 |
181 fGpu->unref(); | 176 fGpu->unref(); |
182 #if FONT_CACHE_STATS | 177 #if FONT_CACHE_STATS |
183 GrPrintf("Num uploads: %d\n", g_UploadCount); | 178 GrPrintf("Num uploads: %d\n", g_UploadCount); |
184 #endif | 179 #endif |
185 } | 180 } |
186 | 181 |
187 static GrPixelConfig maskformat2pixelconfig(GrMaskFormat format) { | 182 static GrPixelConfig maskformat2pixelconfig(GrMaskFormat format) { |
188 switch (format) { | 183 switch (format) { |
189 case kA8_GrMaskFormat: | 184 case kA8_GrMaskFormat: |
190 return kAlpha_8_GrPixelConfig; | 185 return kAlpha_8_GrPixelConfig; |
191 case kA565_GrMaskFormat: | 186 case kA565_GrMaskFormat: |
192 return kRGB_565_GrPixelConfig; | 187 return kRGB_565_GrPixelConfig; |
193 case kA888_GrMaskFormat: | 188 case kA888_GrMaskFormat: |
194 return kSkia8888_GrPixelConfig; | 189 return kSkia8888_GrPixelConfig; |
195 default: | 190 default: |
196 SkDEBUGFAIL("unknown maskformat"); | 191 SkDEBUGFAIL("unknown maskformat"); |
197 } | 192 } |
198 return kUnknown_GrPixelConfig; | 193 return kUnknown_GrPixelConfig; |
199 } | 194 } |
200 | 195 |
201 GrAtlas* GrAtlasMgr::addToAtlas(GrAtlas** atlas, | 196 GrAtlas* GrAtlasMgr::addToAtlas(GrAtlas** atlas, |
202 int width, int height, const void* image, | 197 int width, int height, const void* image, |
203 GrMaskFormat format, | |
204 GrIPoint16* loc) { | 198 GrIPoint16* loc) { |
205 SkASSERT(NULL == *atlas || (*atlas)->getMaskFormat() == format); | |
206 | |
207 // iterate through entire atlas list, see if we can find a hole | 199 // iterate through entire atlas list, see if we can find a hole |
208 GrAtlas* atlasIter = *atlas; | 200 GrAtlas* atlasIter = *atlas; |
209 while (atlasIter) { | 201 while (atlasIter) { |
210 if (atlasIter->addSubImage(width, height, image, loc)) { | 202 if (atlasIter->addSubImage(width, height, image, loc)) { |
211 return atlasIter; | 203 return atlasIter; |
212 } | 204 } |
213 atlasIter = atlasIter->fNext; | 205 atlasIter = atlasIter->fNext; |
214 } | 206 } |
215 | 207 |
216 // If the above fails, then either we have no starting atlas, or the current | 208 // If the above fails, then either we have no starting atlas, or the current |
217 // atlas list is full. Either way we need to allocate a new atlas | 209 // atlas list is full. Either way we need to allocate a new atlas |
218 | 210 |
219 GrIPoint16 plot; | 211 GrIPoint16 plot; |
220 if (!fPlotMgr[format]->newPlot(&plot)) { | 212 if (!fPlotMgr->newPlot(&plot)) { |
221 return NULL; | 213 return NULL; |
222 } | 214 } |
223 | 215 |
224 SkASSERT(0 == kA8_GrMaskFormat); | 216 if (NULL == fTexture) { |
225 SkASSERT(1 == kA565_GrMaskFormat); | |
226 if (NULL == fTexture[format]) { | |
227 // TODO: Update this to use the cache rather than directly creating a te
xture. | 217 // TODO: Update this to use the cache rather than directly creating a te
xture. |
228 GrTextureDesc desc; | 218 GrTextureDesc desc; |
229 desc.fFlags = kDynamicUpdate_GrTextureFlagBit; | 219 desc.fFlags = kDynamicUpdate_GrTextureFlagBit; |
230 desc.fWidth = GR_ATLAS_TEXTURE_WIDTH; | 220 desc.fWidth = GR_ATLAS_TEXTURE_WIDTH; |
231 desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT; | 221 desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT; |
232 desc.fConfig = maskformat2pixelconfig(format); | 222 desc.fConfig = maskformat2pixelconfig(fMaskFormat); |
233 | 223 |
234 fTexture[format] = fGpu->createTexture(desc, NULL, 0); | 224 fTexture = fGpu->createTexture(desc, NULL, 0); |
235 if (NULL == fTexture[format]) { | 225 if (NULL == fTexture) { |
236 return NULL; | 226 return NULL; |
237 } | 227 } |
238 } | 228 } |
239 | 229 |
240 GrAtlas* newAtlas = SkNEW_ARGS(GrAtlas, (this, plot.fX, plot.fY, format)); | 230 GrAtlas* newAtlas = SkNEW_ARGS(GrAtlas, (this, plot.fX, plot.fY, fMaskFormat
)); |
241 if (!newAtlas->addSubImage(width, height, image, loc)) { | 231 if (!newAtlas->addSubImage(width, height, image, loc)) { |
242 delete newAtlas; | 232 delete newAtlas; |
243 return NULL; | 233 return NULL; |
244 } | 234 } |
245 | 235 |
246 // new atlas, put at head | 236 // new atlas, put at head |
247 newAtlas->fNext = *atlas; | 237 newAtlas->fNext = *atlas; |
248 *atlas = newAtlas; | 238 *atlas = newAtlas; |
249 | 239 |
250 return newAtlas; | 240 return newAtlas; |
251 } | 241 } |
252 | 242 |
253 void GrAtlasMgr::freePlot(GrMaskFormat format, int x, int y) { | 243 void GrAtlasMgr::freePlot(int x, int y) { |
254 SkASSERT(fPlotMgr[format]->isBusy(x, y)); | 244 SkASSERT(fPlotMgr->isBusy(x, y)); |
255 fPlotMgr[format]->freePlot(x, y); | 245 fPlotMgr->freePlot(x, y); |
256 } | 246 } |
OLD | NEW |