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 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 9 | #include "GrAtlas.h" |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 10 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrGpu.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 12 | #include "GrRectanizer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | |
| 14 | #if 0 |
| 15 | #define GR_PLOT_WIDTH 8 |
| 16 | #define GR_PLOT_HEIGHT 4 |
| 17 | #define GR_ATLAS_WIDTH 256 |
| 18 | #define GR_ATLAS_HEIGHT 256 |
| 19 | |
| 20 | #define GR_ATLAS_TEXTURE_WIDTH (GR_PLOT_WIDTH * GR_ATLAS_WIDTH) |
| 21 | #define GR_ATLAS_TEXTURE_HEIGHT (GR_PLOT_HEIGHT * GR_ATLAS_HEIGHT) |
| 22 | |
| 23 | #else |
| 24 | |
| 25 | #define GR_ATLAS_TEXTURE_WIDTH 1024 |
| 26 | #define GR_ATLAS_TEXTURE_HEIGHT 2048 |
| 27 | |
commit-bot@chromium.org | 09846a0 | 2013-10-02 17:37:59 +0000 | [diff] [blame] | 28 | #define GR_ATLAS_WIDTH 256 |
| 29 | #define GR_ATLAS_HEIGHT 256 |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 30 | |
| 31 | #define GR_PLOT_WIDTH (GR_ATLAS_TEXTURE_WIDTH / GR_ATLAS_WIDTH) |
| 32 | #define GR_PLOT_HEIGHT (GR_ATLAS_TEXTURE_HEIGHT / GR_ATLAS_HEIGHT) |
| 33 | |
| 34 | #endif |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | #define BORDER 1 |
| 39 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 40 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | static int gCounter; |
| 42 | #endif |
| 43 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 44 | // for testing |
| 45 | #define FONT_CACHE_STATS 0 |
| 46 | #if FONT_CACHE_STATS |
| 47 | static int g_UploadCount = 0; |
| 48 | #endif |
| 49 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 50 | GrPlot::GrPlot() : fDrawToken(NULL, 0) |
| 51 | , fNext(NULL) |
| 52 | , fTexture(NULL) |
| 53 | , fAtlasMgr(NULL) |
| 54 | , fBytesPerPixel(1) |
| 55 | { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER, |
| 57 | GR_ATLAS_HEIGHT - BORDER); |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 58 | fOffset.set(0, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 61 | GrPlot::~GrPlot() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | delete fRects; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | } |
| 64 | |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 65 | static inline void adjust_for_offset(GrIPoint16* loc, const GrIPoint16& offset) { |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 66 | loc->fX += offset.fX * GR_ATLAS_WIDTH; |
| 67 | loc->fY += offset.fY * GR_ATLAS_HEIGHT; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 68 | } |
| 69 | |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 70 | static inline uint8_t* zero_fill(uint8_t* ptr, size_t count) { |
| 71 | sk_bzero(ptr, count); |
| 72 | return ptr + count; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 73 | } |
| 74 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 75 | bool GrPlot::addSubImage(int width, int height, const void* image, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 76 | GrIPoint16* loc) { |
| 77 | if (!fRects->addRect(width + BORDER, height + BORDER, loc)) { |
| 78 | return false; |
| 79 | } |
| 80 | |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 81 | SkAutoSMalloc<1024> storage; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 82 | int dstW = width + 2*BORDER; |
| 83 | int dstH = height + 2*BORDER; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | if (BORDER) { |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 85 | const size_t dstRB = dstW * fBytesPerPixel; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 86 | uint8_t* dst = (uint8_t*)storage.reset(dstH * dstRB); |
reed@google.com | 939ca7c | 2013-09-26 19:56:51 +0000 | [diff] [blame] | 87 | sk_bzero(dst, dstRB); // zero top row |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 88 | dst += dstRB; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | for (int y = 0; y < height; y++) { |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 90 | dst = zero_fill(dst, fBytesPerPixel); // zero left edge |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 91 | memcpy(dst, image, width * fBytesPerPixel); |
| 92 | dst += width * fBytesPerPixel; |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 93 | dst = zero_fill(dst, fBytesPerPixel); // zero right edge |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 94 | image = (const void*)((const char*)image + width * fBytesPerPixel); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | } |
reed@google.com | 939ca7c | 2013-09-26 19:56:51 +0000 | [diff] [blame] | 96 | sk_bzero(dst, dstRB); // zero bottom row |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | image = storage.get(); |
| 98 | } |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 99 | adjust_for_offset(loc, fOffset); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 100 | GrContext* context = fTexture->getContext(); |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 101 | // We pass the flag that does not force a flush. We assume our caller is |
| 102 | // smart and hasn't referenced the part of the texture we're about to update |
| 103 | // since the last flush. |
| 104 | context->writeTexturePixels(fTexture, |
| 105 | loc->fX, loc->fY, dstW, dstH, |
| 106 | fTexture->config(), image, 0, |
| 107 | GrContext::kDontFlush_PixelOpsFlag); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | |
| 109 | // now tell the caller to skip the top/left BORDER |
| 110 | loc->fX += BORDER; |
| 111 | loc->fY += BORDER; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 112 | |
| 113 | #if FONT_CACHE_STATS |
| 114 | ++g_UploadCount; |
| 115 | #endif |
| 116 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 122 | GrAtlasMgr::GrAtlasMgr(GrGpu* gpu, GrPixelConfig config) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | fGpu = gpu; |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 124 | fPixelConfig = config; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | gpu->ref(); |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 126 | fTexture = NULL; |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 127 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 128 | // set up allocated plots |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 129 | size_t bpp = GrBytesPerPixel(fPixelConfig); |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 130 | fPlots = SkNEW_ARRAY(GrPlot, (GR_PLOT_WIDTH*GR_PLOT_HEIGHT)); |
| 131 | fFreePlots = NULL; |
| 132 | GrPlot* currPlot = fPlots; |
| 133 | for (int y = GR_PLOT_HEIGHT-1; y >= 0; --y) { |
| 134 | for (int x = GR_PLOT_WIDTH-1; x >= 0; --x) { |
| 135 | currPlot->fAtlasMgr = this; |
| 136 | currPlot->fOffset.set(x, y); |
| 137 | currPlot->fBytesPerPixel = bpp; |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 138 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 139 | // add to free list |
| 140 | currPlot->fNext = fFreePlots; |
| 141 | fFreePlots = currPlot; |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 142 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 143 | ++currPlot; |
| 144 | } |
| 145 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | GrAtlasMgr::~GrAtlasMgr() { |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 149 | SkSafeUnref(fTexture); |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 150 | SkDELETE_ARRAY(fPlots); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 151 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | fGpu->unref(); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 153 | #if FONT_CACHE_STATS |
| 154 | GrPrintf("Num uploads: %d\n", g_UploadCount); |
| 155 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 156 | } |
| 157 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 158 | GrPlot* GrAtlasMgr::addToAtlas(GrAtlas* atlas, |
| 159 | int width, int height, const void* image, |
| 160 | GrIPoint16* loc) { |
| 161 | // iterate through entire plot list, see if we can find a hole |
| 162 | GrPlot* plotIter = atlas->fPlots; |
| 163 | while (plotIter) { |
| 164 | if (plotIter->addSubImage(width, height, image, loc)) { |
| 165 | return plotIter; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 166 | } |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 167 | plotIter = plotIter->fNext; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 168 | } |
| 169 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 170 | // If the above fails, then either we have no starting plot, or the current |
| 171 | // plot list is full. Either way we need to allocate a new plot |
| 172 | GrPlot* newPlot = this->allocPlot(); |
| 173 | if (NULL == newPlot) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 174 | return NULL; |
| 175 | } |
| 176 | |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 177 | if (NULL == fTexture) { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 178 | // 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] | 179 | GrTextureDesc desc; |
| 180 | desc.fFlags = kDynamicUpdate_GrTextureFlagBit; |
| 181 | desc.fWidth = GR_ATLAS_TEXTURE_WIDTH; |
| 182 | desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT; |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 183 | desc.fConfig = fPixelConfig; |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 184 | |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 185 | fTexture = fGpu->createTexture(desc, NULL, 0); |
| 186 | if (NULL == fTexture) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
| 189 | } |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 190 | // be sure to set texture for fast lookup |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 191 | newPlot->fTexture = fTexture; |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 192 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 193 | if (!newPlot->addSubImage(width, height, image, loc)) { |
| 194 | this->freePlot(newPlot); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 195 | return NULL; |
| 196 | } |
| 197 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 198 | // new plot, put at head |
| 199 | newPlot->fNext = atlas->fPlots; |
| 200 | atlas->fPlots = newPlot; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 201 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 202 | return newPlot; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 203 | } |
| 204 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 205 | bool GrAtlasMgr::removeUnusedPlots(GrAtlas* atlas) { |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 206 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 207 | // GrPlot** is used so that the head element can be easily |
| 208 | // modified when the first element is deleted |
| 209 | GrPlot** plotRef = &atlas->fPlots; |
| 210 | GrPlot* plot = atlas->fPlots; |
| 211 | bool removed = false; |
| 212 | while (NULL != plot) { |
| 213 | if (plot->drawToken().isIssued()) { |
| 214 | *plotRef = plot->fNext; |
| 215 | this->freePlot(plot); |
| 216 | plot = *plotRef; |
| 217 | removed = true; |
| 218 | } else { |
| 219 | plotRef = &plot->fNext; |
| 220 | plot = plot->fNext; |
| 221 | } |
| 222 | } |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 223 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 224 | return removed; |
| 225 | } |
| 226 | |
| 227 | void GrAtlasMgr::deletePlotList(GrPlot* plot) { |
| 228 | while (NULL != plot) { |
| 229 | GrPlot* next = plot->fNext; |
| 230 | this->freePlot(plot); |
| 231 | plot = next; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | GrPlot* GrAtlasMgr::allocPlot() { |
| 236 | if (NULL == fFreePlots) { |
| 237 | return NULL; |
| 238 | } else { |
| 239 | GrPlot* alloc = fFreePlots; |
| 240 | fFreePlots = alloc->fNext; |
| 241 | #ifdef SK_DEBUG |
| 242 | // GrPrintf(" GrPlot %p [%d %d] %d\n", this, alloc->fOffset.fX, alloc->fOffset.fY, gCounter); |
| 243 | gCounter += 1; |
| 244 | #endif |
| 245 | return alloc; |
| 246 | } |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 247 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void GrAtlasMgr::freePlot(GrPlot* plot) { |
| 251 | SkASSERT(this == plot->fAtlasMgr); |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 252 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 253 | plot->fRects->reset(); |
| 254 | plot->fNext = fFreePlots; |
| 255 | fFreePlots = plot; |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 256 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 257 | #ifdef SK_DEBUG |
| 258 | --gCounter; |
| 259 | // GrPrintf("~GrPlot %p [%d %d] %d\n", this, plot->fOffset.fX, plot->fOffset.fY, gCounter); |
| 260 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 261 | } |