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