| 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 29 matching lines...) Expand all Loading... |
| 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 GrAtlas::GrAtlas(GrAtlasMgr* mgr, int plotX, int plotY, GrMaskFormat format) { | 47 GrAtlas::GrAtlas(GrAtlasMgr* mgr, int plotX, int plotY, GrMaskFormat format) { |
| 48 fAtlasMgr = mgr; // just a pointer, not an owner | 48 fAtlasMgr = mgr; // just a pointer, not an owner |
| 49 fNext = NULL; | 49 fNext = NULL; |
| 50 fUsed = false; |
| 51 |
| 50 fTexture = mgr->getTexture(format); // we're not an owner, just a pointer | 52 fTexture = mgr->getTexture(format); // we're not an owner, just a pointer |
| 51 fPlot.set(plotX, plotY); | 53 fPlot.set(plotX, plotY); |
| 52 | 54 |
| 53 fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER, | 55 fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER, |
| 54 GR_ATLAS_HEIGHT - BORDER); | 56 GR_ATLAS_HEIGHT - BORDER); |
| 55 | 57 |
| 56 fMaskFormat = format; | 58 fMaskFormat = format; |
| 57 | 59 |
| 58 #if GR_DEBUG | 60 #if GR_DEBUG |
| 59 // GrPrintf(" GrAtlas %p [%d %d] %d\n", this, plotX, plotY, gCounter); | 61 // GrPrintf(" GrAtlas %p [%d %d] %d\n", this, plotX, plotY, gCounter); |
| 60 gCounter += 1; | 62 gCounter += 1; |
| 61 #endif | 63 #endif |
| 62 } | 64 } |
| 63 | 65 |
| 64 GrAtlas::~GrAtlas() { | 66 GrAtlas::~GrAtlas() { |
| 65 fAtlasMgr->freePlot(fPlot.fX, fPlot.fY); | 67 fAtlasMgr->freePlot(fMaskFormat, fPlot.fX, fPlot.fY); |
| 66 | 68 |
| 67 delete fRects; | 69 delete fRects; |
| 68 | 70 |
| 69 #if GR_DEBUG | 71 #if GR_DEBUG |
| 70 --gCounter; | 72 --gCounter; |
| 71 // GrPrintf("~GrAtlas %p [%d %d] %d\n", this, fPlot.fX, fPlot.fY, gCounter); | 73 // GrPrintf("~GrAtlas %p [%d %d] %d\n", this, fPlot.fX, fPlot.fY, gCounter); |
| 72 #endif | 74 #endif |
| 73 } | 75 } |
| 74 | 76 |
| 75 static void adjustForPlot(GrIPoint16* loc, const GrIPoint16& plot) { | 77 static void adjustForPlot(GrIPoint16* loc, const GrIPoint16& plot) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 // smart and hasn't referenced the part of the texture we're about to update | 117 // smart and hasn't referenced the part of the texture we're about to update |
| 116 // since the last flush. | 118 // since the last flush. |
| 117 context->writeTexturePixels(fTexture, | 119 context->writeTexturePixels(fTexture, |
| 118 loc->fX, loc->fY, dstW, dstH, | 120 loc->fX, loc->fY, dstW, dstH, |
| 119 fTexture->config(), image, 0, | 121 fTexture->config(), image, 0, |
| 120 GrContext::kDontFlush_PixelOpsFlag); | 122 GrContext::kDontFlush_PixelOpsFlag); |
| 121 | 123 |
| 122 // now tell the caller to skip the top/left BORDER | 124 // now tell the caller to skip the top/left BORDER |
| 123 loc->fX += BORDER; | 125 loc->fX += BORDER; |
| 124 loc->fY += BORDER; | 126 loc->fY += BORDER; |
| 127 |
| 125 return true; | 128 return true; |
| 126 } | 129 } |
| 127 | 130 |
| 128 /////////////////////////////////////////////////////////////////////////////// | 131 /////////////////////////////////////////////////////////////////////////////// |
| 129 | 132 |
| 130 GrAtlasMgr::GrAtlasMgr(GrGpu* gpu) { | 133 GrAtlasMgr::GrAtlasMgr(GrGpu* gpu) { |
| 131 fGpu = gpu; | 134 fGpu = gpu; |
| 132 gpu->ref(); | 135 gpu->ref(); |
| 133 Gr_bzero(fTexture, sizeof(fTexture)); | 136 Gr_bzero(fTexture, sizeof(fTexture)); |
| 134 fPlotMgr = SkNEW_ARGS(GrPlotMgr, (GR_PLOT_WIDTH, GR_PLOT_HEIGHT)); | 137 for (int i = 0; i < kCount_GrMaskFormats; ++i) { |
| 138 fPlotMgr[i] = SkNEW_ARGS(GrPlotMgr, (GR_PLOT_WIDTH, GR_PLOT_HEIGHT)); |
| 139 } |
| 135 } | 140 } |
| 136 | 141 |
| 137 GrAtlasMgr::~GrAtlasMgr() { | 142 GrAtlasMgr::~GrAtlasMgr() { |
| 138 for (size_t i = 0; i < GR_ARRAY_COUNT(fTexture); i++) { | 143 for (size_t i = 0; i < GR_ARRAY_COUNT(fTexture); i++) { |
| 139 GrSafeUnref(fTexture[i]); | 144 GrSafeUnref(fTexture[i]); |
| 140 } | 145 } |
| 141 delete fPlotMgr; | 146 for (int i = 0; i < kCount_GrMaskFormats; ++i) { |
| 147 delete fPlotMgr[i]; |
| 148 } |
| 149 |
| 142 fGpu->unref(); | 150 fGpu->unref(); |
| 143 } | 151 } |
| 144 | 152 |
| 145 static GrPixelConfig maskformat2pixelconfig(GrMaskFormat format) { | 153 static GrPixelConfig maskformat2pixelconfig(GrMaskFormat format) { |
| 146 switch (format) { | 154 switch (format) { |
| 147 case kA8_GrMaskFormat: | 155 case kA8_GrMaskFormat: |
| 148 return kAlpha_8_GrPixelConfig; | 156 return kAlpha_8_GrPixelConfig; |
| 149 case kA565_GrMaskFormat: | 157 case kA565_GrMaskFormat: |
| 150 return kRGB_565_GrPixelConfig; | 158 return kRGB_565_GrPixelConfig; |
| 151 case kA888_GrMaskFormat: | 159 case kA888_GrMaskFormat: |
| 152 return kSkia8888_GrPixelConfig; | 160 return kSkia8888_GrPixelConfig; |
| 153 default: | 161 default: |
| 154 GrAssert(!"unknown maskformat"); | 162 GrAssert(!"unknown maskformat"); |
| 155 } | 163 } |
| 156 return kUnknown_GrPixelConfig; | 164 return kUnknown_GrPixelConfig; |
| 157 } | 165 } |
| 158 | 166 |
| 159 GrAtlas* GrAtlasMgr::addToAtlas(GrAtlas* atlas, | 167 GrAtlas* GrAtlasMgr::addToAtlas(GrAtlas** atlas, |
| 160 int width, int height, const void* image, | 168 int width, int height, const void* image, |
| 161 GrMaskFormat format, | 169 GrMaskFormat format, |
| 162 GrIPoint16* loc) { | 170 GrIPoint16* loc) { |
| 163 GrAssert(NULL == atlas || atlas->getMaskFormat() == format); | 171 GrAssert(NULL == *atlas || (*atlas)->getMaskFormat() == format); |
| 164 | 172 |
| 165 if (atlas && atlas->addSubImage(width, height, image, loc)) { | 173 // iterate through entire atlas list, see if we can find a hole |
| 166 return atlas; | 174 GrAtlas* atlasIter = *atlas; |
| 175 while (atlasIter) { |
| 176 if (atlasIter->addSubImage(width, height, image, loc)) { |
| 177 return atlasIter; |
| 178 } |
| 179 atlasIter = atlasIter->fNext; |
| 167 } | 180 } |
| 168 | 181 |
| 169 // If the above fails, then either we have no starting atlas, or the current | 182 // 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 | 183 // atlas list is full. Either way we need to allocate a new atlas |
| 171 | 184 |
| 172 GrIPoint16 plot; | 185 GrIPoint16 plot; |
| 173 if (!fPlotMgr->newPlot(&plot)) { | 186 if (!fPlotMgr[format]->newPlot(&plot)) { |
| 174 return NULL; | 187 return NULL; |
| 175 } | 188 } |
| 176 | 189 |
| 177 GrAssert(0 == kA8_GrMaskFormat); | 190 GrAssert(0 == kA8_GrMaskFormat); |
| 178 GrAssert(1 == kA565_GrMaskFormat); | 191 GrAssert(1 == kA565_GrMaskFormat); |
| 179 if (NULL == fTexture[format]) { | 192 if (NULL == fTexture[format]) { |
| 180 // TODO: Update this to use the cache rather than directly creating a te
xture. | 193 // TODO: Update this to use the cache rather than directly creating a te
xture. |
| 181 GrTextureDesc desc; | 194 GrTextureDesc desc; |
| 182 desc.fFlags = kDynamicUpdate_GrTextureFlagBit; | 195 desc.fFlags = kDynamicUpdate_GrTextureFlagBit; |
| 183 desc.fWidth = GR_ATLAS_TEXTURE_WIDTH; | 196 desc.fWidth = GR_ATLAS_TEXTURE_WIDTH; |
| 184 desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT; | 197 desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT; |
| 185 desc.fConfig = maskformat2pixelconfig(format); | 198 desc.fConfig = maskformat2pixelconfig(format); |
| 186 | 199 |
| 187 fTexture[format] = fGpu->createTexture(desc, NULL, 0); | 200 fTexture[format] = fGpu->createTexture(desc, NULL, 0); |
| 188 if (NULL == fTexture[format]) { | 201 if (NULL == fTexture[format]) { |
| 189 return NULL; | 202 return NULL; |
| 190 } | 203 } |
| 191 } | 204 } |
| 192 | 205 |
| 193 GrAtlas* newAtlas = SkNEW_ARGS(GrAtlas, (this, plot.fX, plot.fY, format)); | 206 GrAtlas* newAtlas = SkNEW_ARGS(GrAtlas, (this, plot.fX, plot.fY, format)); |
| 194 if (!newAtlas->addSubImage(width, height, image, loc)) { | 207 if (!newAtlas->addSubImage(width, height, image, loc)) { |
| 195 delete newAtlas; | 208 delete newAtlas; |
| 196 return NULL; | 209 return NULL; |
| 197 } | 210 } |
| 198 | 211 |
| 199 newAtlas->fNext = atlas; | 212 // new atlas, put at head |
| 213 newAtlas->fNext = *atlas; |
| 214 *atlas = newAtlas; |
| 215 |
| 200 return newAtlas; | 216 return newAtlas; |
| 201 } | 217 } |
| 202 | 218 |
| 203 void GrAtlasMgr::freePlot(int x, int y) { | 219 void GrAtlasMgr::freePlot(GrMaskFormat format, int x, int y) { |
| 204 GrAssert(fPlotMgr->isBusy(x, y)); | 220 GrAssert(fPlotMgr[format]->isBusy(x, y)); |
| 205 fPlotMgr->freePlot(x, y); | 221 fPlotMgr[format]->freePlot(x, y); |
| 206 } | 222 } |
| OLD | NEW |