| 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 | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 38 | // for testing |
| 39 | #define FONT_CACHE_STATS 0 |
| 40 | #if FONT_CACHE_STATS |
| 41 | static int g_UploadCount = 0; |
| 42 | #endif |
| 43 | |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 44 | GrPlot::GrPlot() : fDrawToken(NULL, 0) |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 45 | , fTexture(NULL) |
| 46 | , fAtlasMgr(NULL) |
| 47 | , fBytesPerPixel(1) |
| 48 | { |
| commit-bot@chromium.org | f952924 | 2014-02-14 18:41:47 +0000 | [diff] [blame] | 49 | fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH, |
| 50 | GR_ATLAS_HEIGHT); |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 51 | fOffset.set(0, 0); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 54 | GrPlot::~GrPlot() { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | delete fRects; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 58 | 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] | 59 | loc->fX += offset.fX * GR_ATLAS_WIDTH; |
| 60 | loc->fY += offset.fY * GR_ATLAS_HEIGHT; |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 63 | static inline uint8_t* zero_fill(uint8_t* ptr, size_t count) { |
| 64 | sk_bzero(ptr, count); |
| 65 | return ptr + count; |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 68 | bool GrPlot::addSubImage(int width, int height, const void* image, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | GrIPoint16* loc) { |
| commit-bot@chromium.org | f952924 | 2014-02-14 18:41:47 +0000 | [diff] [blame] | 70 | if (!fRects->addRect(width, height, loc)) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
| bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 74 | SkAutoSMalloc<1024> storage; |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 75 | adjust_for_offset(loc, fOffset); |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 76 | GrContext* context = fTexture->getContext(); |
| bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 77 | // We pass the flag that does not force a flush. We assume our caller is |
| 78 | // smart and hasn't referenced the part of the texture we're about to update |
| 79 | // since the last flush. |
| 80 | context->writeTexturePixels(fTexture, |
| commit-bot@chromium.org | f952924 | 2014-02-14 18:41:47 +0000 | [diff] [blame] | 81 | loc->fX, loc->fY, width, height, |
| bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 82 | fTexture->config(), image, 0, |
| 83 | GrContext::kDontFlush_PixelOpsFlag); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 85 | #if FONT_CACHE_STATS |
| 86 | ++g_UploadCount; |
| 87 | #endif |
| 88 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 92 | void GrPlot::resetRects() { |
| 93 | SkASSERT(NULL != fRects); |
| 94 | fRects->reset(); |
| 95 | } |
| 96 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 107 | fPlotArray = SkNEW_ARRAY(GrPlot, (GR_PLOT_WIDTH*GR_PLOT_HEIGHT)); |
| 108 | |
| 109 | GrPlot* currPlot = fPlotArray; |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 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 | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 116 | // build LRU list |
| 117 | fPlotList.addToHead(currPlot); |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 118 | ++currPlot; |
| 119 | } |
| 120 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | GrAtlasMgr::~GrAtlasMgr() { |
| commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 124 | SkSafeUnref(fTexture); |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 125 | SkDELETE_ARRAY(fPlotArray); |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 126 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | fGpu->unref(); |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 128 | #if FONT_CACHE_STATS |
| 129 | GrPrintf("Num uploads: %d\n", g_UploadCount); |
| 130 | #endif |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 133 | void GrAtlasMgr::moveToHead(GrPlot* plot) { |
| 134 | if (fPlotList.head() == plot) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | fPlotList.remove(plot); |
| 139 | fPlotList.addToHead(plot); |
| 140 | }; |
| 141 | |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 142 | GrPlot* GrAtlasMgr::addToAtlas(GrAtlas* atlas, |
| 143 | int width, int height, const void* image, |
| 144 | GrIPoint16* loc) { |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 145 | // iterate through entire plot list for this atlas, see if we can find a hole |
| 146 | // last one was most recently added and probably most empty |
| 147 | for (int i = atlas->fPlots.count()-1; i >= 0; --i) { |
| 148 | GrPlot* plot = atlas->fPlots[i]; |
| 149 | if (plot->addSubImage(width, height, image, loc)) { |
| 150 | this->moveToHead(plot); |
| 151 | return plot; |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 152 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 155 | // before we get a new plot, make sure we have a backing texture |
| commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 156 | if (NULL == fTexture) { |
| bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 157 | // 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] | 158 | GrTextureDesc desc; |
| 159 | desc.fFlags = kDynamicUpdate_GrTextureFlagBit; |
| 160 | desc.fWidth = GR_ATLAS_TEXTURE_WIDTH; |
| 161 | desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT; |
| commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 162 | desc.fConfig = fPixelConfig; |
| robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 163 | |
| commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 164 | fTexture = fGpu->createTexture(desc, NULL, 0); |
| 165 | if (NULL == fTexture) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 166 | return NULL; |
| 167 | } |
| 168 | } |
| skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 169 | |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 170 | // now look through all allocated plots for one we can share, in MRU order |
| 171 | GrPlotList::Iter plotIter; |
| 172 | plotIter.init(fPlotList, GrPlotList::Iter::kHead_IterStart); |
| 173 | GrPlot* plot; |
| 174 | while (NULL != (plot = plotIter.get())) { |
| 175 | // make sure texture is set for quick lookup |
| 176 | plot->fTexture = fTexture; |
| 177 | if (plot->addSubImage(width, height, image, loc)) { |
| 178 | this->moveToHead(plot); |
| 179 | // new plot for atlas, put at end of array |
| 180 | *(atlas->fPlots.append()) = plot; |
| 181 | return plot; |
| 182 | } |
| 183 | plotIter.next(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 186 | // If the above fails, then the current plot list has no room |
| 187 | return NULL; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 190 | bool GrAtlasMgr::removePlot(GrAtlas* atlas, const GrPlot* plot) { |
| 191 | // iterate through plot list for this atlas |
| 192 | int count = atlas->fPlots.count(); |
| 193 | for (int i = 0; i < count; ++i) { |
| 194 | if (plot == atlas->fPlots[i]) { |
| 195 | atlas->fPlots.remove(i); |
| 196 | return true; |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 199 | |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 200 | return false; |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 203 | // get a plot that's not being used by the current draw |
| 204 | GrPlot* GrAtlasMgr::getUnusedPlot() { |
| 205 | GrPlotList::Iter plotIter; |
| 206 | plotIter.init(fPlotList, GrPlotList::Iter::kTail_IterStart); |
| 207 | GrPlot* plot; |
| 208 | while (NULL != (plot = plotIter.get())) { |
| 209 | if (plot->drawToken().isIssued()) { |
| 210 | return plot; |
| 211 | } |
| 212 | plotIter.prev(); |
| commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 213 | } |
| skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 214 | |
| commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame^] | 215 | return NULL; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 216 | } |
| commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 217 | |
| 218 | SkISize GrAtlas::getSize() const { |
| 219 | return SkISize::Make(GR_ATLAS_TEXTURE_WIDTH, GR_ATLAS_TEXTURE_HEIGHT); |
| 220 | } |