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

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

Issue 21594005: GPU Font Cache improvements: (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fixed some nits Created 7 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « src/gpu/GrAtlas.h ('k') | src/gpu/GrTextContext.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 26 matching lines...) Expand all
37 #endif 37 #endif
38 38
39 /////////////////////////////////////////////////////////////////////////////// 39 ///////////////////////////////////////////////////////////////////////////////
40 40
41 #define BORDER 1 41 #define BORDER 1
42 42
43 #if GR_DEBUG 43 #if GR_DEBUG
44 static int gCounter; 44 static int gCounter;
45 #endif 45 #endif
46 46
47 // for testing
48 #define FONT_CACHE_STATS 0
49 #if FONT_CACHE_STATS
50 static int g_UploadCount = 0;
51 #endif
52
47 GrAtlas::GrAtlas(GrAtlasMgr* mgr, int plotX, int plotY, GrMaskFormat format) { 53 GrAtlas::GrAtlas(GrAtlasMgr* mgr, int plotX, int plotY, GrMaskFormat format) {
48 fAtlasMgr = mgr; // just a pointer, not an owner 54 fAtlasMgr = mgr; // just a pointer, not an owner
49 fNext = NULL; 55 fNext = NULL;
56 fUsed = false;
57
50 fTexture = mgr->getTexture(format); // we're not an owner, just a pointer 58 fTexture = mgr->getTexture(format); // we're not an owner, just a pointer
51 fPlot.set(plotX, plotY); 59 fPlot.set(plotX, plotY);
52 60
53 fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER, 61 fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER,
54 GR_ATLAS_HEIGHT - BORDER); 62 GR_ATLAS_HEIGHT - BORDER);
55 63
56 fMaskFormat = format; 64 fMaskFormat = format;
57 65
58 #if GR_DEBUG 66 #if GR_DEBUG
59 // GrPrintf(" GrAtlas %p [%d %d] %d\n", this, plotX, plotY, gCounter); 67 // GrPrintf(" GrAtlas %p [%d %d] %d\n", this, plotX, plotY, gCounter);
60 gCounter += 1; 68 gCounter += 1;
61 #endif 69 #endif
62 } 70 }
63 71
64 GrAtlas::~GrAtlas() { 72 GrAtlas::~GrAtlas() {
65 fAtlasMgr->freePlot(fPlot.fX, fPlot.fY); 73 fAtlasMgr->freePlot(fMaskFormat, fPlot.fX, fPlot.fY);
66 74
67 delete fRects; 75 delete fRects;
68 76
69 #if GR_DEBUG 77 #if GR_DEBUG
70 --gCounter; 78 --gCounter;
71 // 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);
72 #endif 80 #endif
73 } 81 }
74 82
83 bool GrAtlas::RemoveUnusedAtlases(GrAtlasMgr* atlasMgr, GrAtlas** startAtlas) {
84 // GrAtlas** is used so that a pointer to the head element can be passed in and
85 // modified when the first element is deleted
86 GrAtlas** atlasRef = startAtlas;
87 GrAtlas* atlas = *startAtlas;
88 bool removed = false;
89 while (NULL != atlas) {
90 if (!atlas->used()) {
91 *atlasRef = atlas->fNext;
92 atlasMgr->deleteAtlas(atlas);
93 atlas = *atlasRef;
94 removed = true;
95 } else {
96 atlasRef = &atlas->fNext;
97 atlas = atlas->fNext;
98 }
99 }
100
101 return removed;
102 }
103
75 static void adjustForPlot(GrIPoint16* loc, const GrIPoint16& plot) { 104 static void adjustForPlot(GrIPoint16* loc, const GrIPoint16& plot) {
76 loc->fX += plot.fX * GR_ATLAS_WIDTH; 105 loc->fX += plot.fX * GR_ATLAS_WIDTH;
77 loc->fY += plot.fY * GR_ATLAS_HEIGHT; 106 loc->fY += plot.fY * GR_ATLAS_HEIGHT;
78 } 107 }
79 108
80 static uint8_t* zerofill(uint8_t* ptr, int count) { 109 static uint8_t* zerofill(uint8_t* ptr, int count) {
81 while (--count >= 0) { 110 while (--count >= 0) {
82 *ptr++ = 0; 111 *ptr++ = 0;
83 } 112 }
84 return ptr; 113 return ptr;
(...skipping 30 matching lines...) Expand all
115 // smart and hasn't referenced the part of the texture we're about to update 144 // smart and hasn't referenced the part of the texture we're about to update
116 // since the last flush. 145 // since the last flush.
117 context->writeTexturePixels(fTexture, 146 context->writeTexturePixels(fTexture,
118 loc->fX, loc->fY, dstW, dstH, 147 loc->fX, loc->fY, dstW, dstH,
119 fTexture->config(), image, 0, 148 fTexture->config(), image, 0,
120 GrContext::kDontFlush_PixelOpsFlag); 149 GrContext::kDontFlush_PixelOpsFlag);
121 150
122 // now tell the caller to skip the top/left BORDER 151 // now tell the caller to skip the top/left BORDER
123 loc->fX += BORDER; 152 loc->fX += BORDER;
124 loc->fY += BORDER; 153 loc->fY += BORDER;
154
155 #if FONT_CACHE_STATS
156 ++g_UploadCount;
157 #endif
158
125 return true; 159 return true;
126 } 160 }
127 161
128 /////////////////////////////////////////////////////////////////////////////// 162 ///////////////////////////////////////////////////////////////////////////////
129 163
130 GrAtlasMgr::GrAtlasMgr(GrGpu* gpu) { 164 GrAtlasMgr::GrAtlasMgr(GrGpu* gpu) {
131 fGpu = gpu; 165 fGpu = gpu;
132 gpu->ref(); 166 gpu->ref();
133 Gr_bzero(fTexture, sizeof(fTexture)); 167 Gr_bzero(fTexture, sizeof(fTexture));
134 fPlotMgr = SkNEW_ARGS(GrPlotMgr, (GR_PLOT_WIDTH, GR_PLOT_HEIGHT)); 168 fPlotMgr = SkNEW_ARGS(GrPlotMgr, (GR_PLOT_WIDTH, GR_PLOT_HEIGHT));
135 } 169 }
136 170
137 GrAtlasMgr::~GrAtlasMgr() { 171 GrAtlasMgr::~GrAtlasMgr() {
138 for (size_t i = 0; i < GR_ARRAY_COUNT(fTexture); i++) { 172 for (size_t i = 0; i < GR_ARRAY_COUNT(fTexture); i++) {
139 GrSafeUnref(fTexture[i]); 173 GrSafeUnref(fTexture[i]);
140 } 174 }
141 delete fPlotMgr; 175 delete fPlotMgr;
176
142 fGpu->unref(); 177 fGpu->unref();
178 #if FONT_CACHE_STATS
179 GrPrintf("Num uploads: %d\n", g_UploadCount);
180 #endif
143 } 181 }
144 182
145 static GrPixelConfig maskformat2pixelconfig(GrMaskFormat format) { 183 static GrPixelConfig maskformat2pixelconfig(GrMaskFormat format) {
146 switch (format) { 184 switch (format) {
147 case kA8_GrMaskFormat: 185 case kA8_GrMaskFormat:
148 return kAlpha_8_GrPixelConfig; 186 return kAlpha_8_GrPixelConfig;
149 case kA565_GrMaskFormat: 187 case kA565_GrMaskFormat:
150 return kRGB_565_GrPixelConfig; 188 return kRGB_565_GrPixelConfig;
151 case kA888_GrMaskFormat: 189 case kA888_GrMaskFormat:
152 return kSkia8888_GrPixelConfig; 190 return kSkia8888_GrPixelConfig;
153 default: 191 default:
154 GrAssert(!"unknown maskformat"); 192 GrAssert(!"unknown maskformat");
155 } 193 }
156 return kUnknown_GrPixelConfig; 194 return kUnknown_GrPixelConfig;
157 } 195 }
158 196
159 GrAtlas* GrAtlasMgr::addToAtlas(GrAtlas* atlas, 197 GrAtlas* GrAtlasMgr::addToAtlas(GrAtlas** atlas,
160 int width, int height, const void* image, 198 int width, int height, const void* image,
161 GrMaskFormat format, 199 GrMaskFormat format,
162 GrIPoint16* loc) { 200 GrIPoint16* loc) {
163 GrAssert(NULL == atlas || atlas->getMaskFormat() == format); 201 GrAssert(NULL == *atlas || (*atlas)->getMaskFormat() == format);
164 202
165 if (atlas && atlas->addSubImage(width, height, image, loc)) { 203 // iterate through entire atlas list, see if we can find a hole
166 return atlas; 204 GrAtlas* atlasIter = *atlas;
205 while (atlasIter) {
206 if (atlasIter->addSubImage(width, height, image, loc)) {
207 return atlasIter;
208 }
209 atlasIter = atlasIter->fNext;
167 } 210 }
168 211
169 // If the above fails, then either we have no starting atlas, or the current 212 // If the above fails, then either we have no starting atlas, or the current
170 // one is full. Either way we need to allocate a new atlas 213 // atlas list is full. Either way we need to allocate a new atlas
171 214
172 GrIPoint16 plot; 215 GrIPoint16 plot;
173 if (!fPlotMgr->newPlot(&plot)) { 216 if (!fPlotMgr->newPlot(&plot)) {
174 return NULL; 217 return NULL;
175 } 218 }
176 219
177 GrAssert(0 == kA8_GrMaskFormat); 220 GrAssert(0 == kA8_GrMaskFormat);
178 GrAssert(1 == kA565_GrMaskFormat); 221 GrAssert(1 == kA565_GrMaskFormat);
179 if (NULL == fTexture[format]) { 222 if (NULL == fTexture[format]) {
180 // TODO: Update this to use the cache rather than directly creating a te xture. 223 // TODO: Update this to use the cache rather than directly creating a te xture.
181 GrTextureDesc desc; 224 GrTextureDesc desc;
182 desc.fFlags = kDynamicUpdate_GrTextureFlagBit; 225 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
183 desc.fWidth = GR_ATLAS_TEXTURE_WIDTH; 226 desc.fWidth = GR_ATLAS_TEXTURE_WIDTH;
184 desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT; 227 desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT;
185 desc.fConfig = maskformat2pixelconfig(format); 228 desc.fConfig = maskformat2pixelconfig(format);
186 229
187 fTexture[format] = fGpu->createTexture(desc, NULL, 0); 230 fTexture[format] = fGpu->createTexture(desc, NULL, 0);
188 if (NULL == fTexture[format]) { 231 if (NULL == fTexture[format]) {
189 return NULL; 232 return NULL;
190 } 233 }
191 } 234 }
192 235
193 GrAtlas* newAtlas = SkNEW_ARGS(GrAtlas, (this, plot.fX, plot.fY, format)); 236 GrAtlas* newAtlas = SkNEW_ARGS(GrAtlas, (this, plot.fX, plot.fY, format));
194 if (!newAtlas->addSubImage(width, height, image, loc)) { 237 if (!newAtlas->addSubImage(width, height, image, loc)) {
195 delete newAtlas; 238 delete newAtlas;
196 return NULL; 239 return NULL;
197 } 240 }
198 241
199 newAtlas->fNext = atlas; 242 // new atlas, put at head
243 newAtlas->fNext = *atlas;
244 *atlas = newAtlas;
245
200 return newAtlas; 246 return newAtlas;
201 } 247 }
202 248
203 void GrAtlasMgr::freePlot(int x, int y) { 249 void GrAtlasMgr::freePlot(GrMaskFormat format, int x, int y) {
204 GrAssert(fPlotMgr->isBusy(x, y)); 250 GrAssert(fPlotMgr->isBusy(x, y));
205 fPlotMgr->freePlot(x, y); 251 fPlotMgr->freePlot(x, y);
206 } 252 }
OLDNEW
« no previous file with comments | « src/gpu/GrAtlas.h ('k') | src/gpu/GrTextContext.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698