epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrAtlas.h" |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | #include "GrGpu.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "GrRectanizer.h" |
| 15 | #include "GrPlotMgr.h" |
| 16 | |
| 17 | #if 0 |
| 18 | #define GR_PLOT_WIDTH 8 |
| 19 | #define GR_PLOT_HEIGHT 4 |
| 20 | #define GR_ATLAS_WIDTH 256 |
| 21 | #define GR_ATLAS_HEIGHT 256 |
| 22 | |
| 23 | #define GR_ATLAS_TEXTURE_WIDTH (GR_PLOT_WIDTH * GR_ATLAS_WIDTH) |
| 24 | #define GR_ATLAS_TEXTURE_HEIGHT (GR_PLOT_HEIGHT * GR_ATLAS_HEIGHT) |
| 25 | |
| 26 | #else |
| 27 | |
| 28 | #define GR_ATLAS_TEXTURE_WIDTH 1024 |
| 29 | #define GR_ATLAS_TEXTURE_HEIGHT 2048 |
| 30 | |
| 31 | #define GR_ATLAS_WIDTH 341 |
| 32 | #define GR_ATLAS_HEIGHT 341 |
| 33 | |
| 34 | #define GR_PLOT_WIDTH (GR_ATLAS_TEXTURE_WIDTH / GR_ATLAS_WIDTH) |
| 35 | #define GR_PLOT_HEIGHT (GR_ATLAS_TEXTURE_HEIGHT / GR_ATLAS_HEIGHT) |
| 36 | |
| 37 | #endif |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
| 41 | #define BORDER 1 |
| 42 | |
| 43 | #if GR_DEBUG |
| 44 | static int gCounter; |
| 45 | #endif |
| 46 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 47 | // for testing |
| 48 | #define FONT_CACHE_STATS 0 |
| 49 | #if FONT_CACHE_STATS |
| 50 | static int g_UploadCount = 0; |
| 51 | #endif |
| 52 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 53 | GrAtlas::GrAtlas(GrAtlasMgr* mgr, int plotX, int plotY, GrMaskFormat format) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | fAtlasMgr = mgr; // just a pointer, not an owner |
| 55 | fNext = NULL; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 56 | fUsed = false; |
| 57 | |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 58 | fTexture = mgr->getTexture(format); // we're not an owner, just a pointer |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | fPlot.set(plotX, plotY); |
| 60 | |
| 61 | fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER, |
| 62 | GR_ATLAS_HEIGHT - BORDER); |
| 63 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 64 | fMaskFormat = format; |
| 65 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 66 | #if GR_DEBUG |
reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 67 | // GrPrintf(" GrAtlas %p [%d %d] %d\n", this, plotX, plotY, gCounter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 68 | gCounter += 1; |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | GrAtlas::~GrAtlas() { |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 73 | fAtlasMgr->freePlot(fMaskFormat, fPlot.fX, fPlot.fY); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 74 | |
| 75 | delete fRects; |
| 76 | |
| 77 | #if GR_DEBUG |
| 78 | --gCounter; |
reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 79 | // GrPrintf("~GrAtlas %p [%d %d] %d\n", this, fPlot.fX, fPlot.fY, gCounter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | #endif |
| 81 | } |
| 82 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 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 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | static void adjustForPlot(GrIPoint16* loc, const GrIPoint16& plot) { |
| 105 | loc->fX += plot.fX * GR_ATLAS_WIDTH; |
| 106 | loc->fY += plot.fY * GR_ATLAS_HEIGHT; |
| 107 | } |
| 108 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 109 | static uint8_t* zerofill(uint8_t* ptr, int count) { |
| 110 | while (--count >= 0) { |
| 111 | *ptr++ = 0; |
| 112 | } |
| 113 | return ptr; |
| 114 | } |
| 115 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | bool GrAtlas::addSubImage(int width, int height, const void* image, |
| 117 | GrIPoint16* loc) { |
| 118 | if (!fRects->addRect(width + BORDER, height + BORDER, loc)) { |
| 119 | return false; |
| 120 | } |
| 121 | |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 122 | SkAutoSMalloc<1024> storage; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 123 | int dstW = width + 2*BORDER; |
| 124 | int dstH = height + 2*BORDER; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | if (BORDER) { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 126 | const int bpp = GrMaskFormatBytesPerPixel(fMaskFormat); |
| 127 | const size_t dstRB = dstW * bpp; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 128 | uint8_t* dst = (uint8_t*)storage.reset(dstH * dstRB); |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 129 | Gr_bzero(dst, dstRB); // zero top row |
| 130 | dst += dstRB; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | for (int y = 0; y < height; y++) { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 132 | dst = zerofill(dst, bpp); // zero left edge |
| 133 | memcpy(dst, image, width * bpp); |
| 134 | dst += width * bpp; |
| 135 | dst = zerofill(dst, bpp); // zero right edge |
| 136 | image = (const void*)((const char*)image + width * bpp); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | } |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 138 | Gr_bzero(dst, dstRB); // zero bottom row |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | image = storage.get(); |
| 140 | } |
| 141 | adjustForPlot(loc, fPlot); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 142 | GrContext* context = fTexture->getContext(); |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 143 | // We pass the flag that does not force a flush. We assume our caller is |
| 144 | // smart and hasn't referenced the part of the texture we're about to update |
| 145 | // since the last flush. |
| 146 | context->writeTexturePixels(fTexture, |
| 147 | loc->fX, loc->fY, dstW, dstH, |
| 148 | fTexture->config(), image, 0, |
| 149 | GrContext::kDontFlush_PixelOpsFlag); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 150 | |
| 151 | // now tell the caller to skip the top/left BORDER |
| 152 | loc->fX += BORDER; |
| 153 | loc->fY += BORDER; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 154 | |
| 155 | #if FONT_CACHE_STATS |
| 156 | ++g_UploadCount; |
| 157 | #endif |
| 158 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
| 162 | /////////////////////////////////////////////////////////////////////////////// |
| 163 | |
| 164 | GrAtlasMgr::GrAtlasMgr(GrGpu* gpu) { |
| 165 | fGpu = gpu; |
| 166 | gpu->ref(); |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 167 | Gr_bzero(fTexture, sizeof(fTexture)); |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 168 | fPlotMgr = SkNEW_ARGS(GrPlotMgr, (GR_PLOT_WIDTH, GR_PLOT_HEIGHT)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | GrAtlasMgr::~GrAtlasMgr() { |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 172 | for (size_t i = 0; i < GR_ARRAY_COUNT(fTexture); i++) { |
| 173 | GrSafeUnref(fTexture[i]); |
| 174 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 175 | delete fPlotMgr; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 176 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 177 | fGpu->unref(); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 178 | #if FONT_CACHE_STATS |
| 179 | GrPrintf("Num uploads: %d\n", g_UploadCount); |
| 180 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | } |
| 182 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 183 | static GrPixelConfig maskformat2pixelconfig(GrMaskFormat format) { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 184 | switch (format) { |
| 185 | case kA8_GrMaskFormat: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 186 | return kAlpha_8_GrPixelConfig; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 187 | case kA565_GrMaskFormat: |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 188 | return kRGB_565_GrPixelConfig; |
caryclark@google.com | 1eeaf0b | 2011-06-22 13:19:43 +0000 | [diff] [blame] | 189 | case kA888_GrMaskFormat: |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 190 | return kSkia8888_GrPixelConfig; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 191 | default: |
| 192 | GrAssert(!"unknown maskformat"); |
| 193 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 194 | return kUnknown_GrPixelConfig; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 195 | } |
| 196 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 197 | GrAtlas* GrAtlasMgr::addToAtlas(GrAtlas** atlas, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 198 | int width, int height, const void* image, |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 199 | GrMaskFormat format, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | GrIPoint16* loc) { |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 201 | GrAssert(NULL == *atlas || (*atlas)->getMaskFormat() == format); |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 202 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 203 | // iterate through entire atlas list, see if we can find a hole |
| 204 | GrAtlas* atlasIter = *atlas; |
| 205 | while (atlasIter) { |
| 206 | if (atlasIter->addSubImage(width, height, image, loc)) { |
| 207 | return atlasIter; |
| 208 | } |
| 209 | atlasIter = atlasIter->fNext; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // If the above fails, then either we have no starting atlas, or the current |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 213 | // atlas list is full. Either way we need to allocate a new atlas |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 214 | |
| 215 | GrIPoint16 plot; |
| 216 | if (!fPlotMgr->newPlot(&plot)) { |
| 217 | return NULL; |
| 218 | } |
| 219 | |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 220 | GrAssert(0 == kA8_GrMaskFormat); |
| 221 | GrAssert(1 == kA565_GrMaskFormat); |
| 222 | if (NULL == fTexture[format]) { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 223 | // TODO: Update this to use the cache rather than directly creating a texture. |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 224 | GrTextureDesc desc; |
| 225 | desc.fFlags = kDynamicUpdate_GrTextureFlagBit; |
| 226 | desc.fWidth = GR_ATLAS_TEXTURE_WIDTH; |
| 227 | desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT; |
| 228 | desc.fConfig = maskformat2pixelconfig(format); |
| 229 | |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 230 | fTexture[format] = fGpu->createTexture(desc, NULL, 0); |
| 231 | if (NULL == fTexture[format]) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | } |
| 235 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 236 | GrAtlas* newAtlas = SkNEW_ARGS(GrAtlas, (this, plot.fX, plot.fY, format)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 237 | if (!newAtlas->addSubImage(width, height, image, loc)) { |
| 238 | delete newAtlas; |
| 239 | return NULL; |
| 240 | } |
| 241 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 242 | // new atlas, put at head |
| 243 | newAtlas->fNext = *atlas; |
| 244 | *atlas = newAtlas; |
| 245 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 246 | return newAtlas; |
| 247 | } |
| 248 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame^] | 249 | void GrAtlasMgr::freePlot(GrMaskFormat format, int x, int y) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 250 | GrAssert(fPlotMgr->isBusy(x, y)); |
| 251 | fPlotMgr->freePlot(x, y); |
| 252 | } |