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