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