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" |
jvanverth | 8e80d17 | 2014-06-19 12:01:10 -0700 | [diff] [blame] | 13 | #include "GrTracing.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 17 | // for testing |
| 18 | #define FONT_CACHE_STATS 0 |
| 19 | #if FONT_CACHE_STATS |
| 20 | static int g_UploadCount = 0; |
| 21 | #endif |
| 22 | |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 23 | GrPlot::GrPlot() |
joshualitt | 8debd89 | 2015-05-19 15:05:24 -0700 | [diff] [blame] | 24 | : fID(-1) |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 25 | , fTexture(NULL) |
| 26 | , fRects(NULL) |
| 27 | , fAtlas(NULL) |
| 28 | , fBytesPerPixel(1) |
| 29 | , fDirty(false) |
| 30 | , fBatchUploads(false) |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 31 | { |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 32 | fOffset.set(0, 0); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | } |
| 34 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 35 | GrPlot::~GrPlot() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 36 | delete[] fPlotData; |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 37 | fPlotData = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 38 | delete fRects; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | } |
| 40 | |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 41 | void GrPlot::init(GrAtlas* atlas, int id, int offX, int offY, int width, int height, size_t bpp, |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 42 | bool batchUploads) { |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 43 | fID = id; |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 44 | fRects = GrRectanizer::Factory(width, height); |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 45 | fAtlas = atlas; |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 46 | fOffset.set(offX * width, offY * height); |
| 47 | fBytesPerPixel = bpp; |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 48 | fPlotData = NULL; |
| 49 | fDirtyRect.setEmpty(); |
| 50 | fDirty = false; |
| 51 | fBatchUploads = batchUploads; |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 52 | } |
| 53 | |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 54 | static inline void adjust_for_offset(SkIPoint16* loc, const SkIPoint16& offset) { |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 55 | loc->fX += offset.fX; |
| 56 | loc->fY += offset.fY; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 57 | } |
| 58 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 59 | bool GrPlot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) { |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 60 | float percentFull = fRects->percentFull(); |
commit-bot@chromium.org | f952924 | 2014-02-14 18:41:47 +0000 | [diff] [blame] | 61 | if (!fRects->addRect(width, height, loc)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 65 | // if batching uploads, create backing memory on first use |
| 66 | // once the plot is nearly full we will revert to uploading each subimage individually |
| 67 | int plotWidth = fRects->width(); |
| 68 | int plotHeight = fRects->height(); |
| 69 | if (fBatchUploads && NULL == fPlotData && 0.0f == percentFull) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 70 | fPlotData = new unsigned char[fBytesPerPixel * plotWidth * plotHeight]; |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 71 | memset(fPlotData, 0, fBytesPerPixel*plotWidth*plotHeight); |
| 72 | } |
| 73 | |
| 74 | // if we have backing memory, copy to the memory and set for future upload |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 75 | if (fPlotData) { |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 76 | const unsigned char* imagePtr = (const unsigned char*) image; |
| 77 | // point ourselves at the right starting spot |
| 78 | unsigned char* dataPtr = fPlotData; |
| 79 | dataPtr += fBytesPerPixel*plotWidth*loc->fY; |
| 80 | dataPtr += fBytesPerPixel*loc->fX; |
| 81 | // copy into the data buffer |
| 82 | for (int i = 0; i < height; ++i) { |
| 83 | memcpy(dataPtr, imagePtr, fBytesPerPixel*width); |
| 84 | dataPtr += fBytesPerPixel*plotWidth; |
| 85 | imagePtr += fBytesPerPixel*width; |
| 86 | } |
| 87 | |
| 88 | fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height); |
| 89 | adjust_for_offset(loc, fOffset); |
| 90 | fDirty = true; |
| 91 | // otherwise, just upload the image directly |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 92 | } else if (image) { |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 93 | adjust_for_offset(loc, fOffset); |
jvanverth | 8e80d17 | 2014-06-19 12:01:10 -0700 | [diff] [blame] | 94 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrPlot::uploadToTexture"); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 95 | fTexture->writePixels(loc->fX, loc->fY, width, height, fTexture->config(), image, 0, |
| 96 | GrContext::kDontFlush_PixelOpsFlag); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 97 | } else { |
| 98 | adjust_for_offset(loc, fOffset); |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 99 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 101 | #if FONT_CACHE_STATS |
| 102 | ++g_UploadCount; |
| 103 | #endif |
| 104 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 108 | void GrPlot::resetRects() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 109 | SkASSERT(fRects); |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 110 | fRects->reset(); |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 111 | } |
| 112 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | /////////////////////////////////////////////////////////////////////////////// |
| 114 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 115 | GrAtlas::GrAtlas(GrGpu* gpu, GrPixelConfig config, GrSurfaceFlags flags, |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 116 | const SkISize& backingTextureSize, |
| 117 | int numPlotsX, int numPlotsY, bool batchUploads) { |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 118 | fGpu = SkRef(gpu); |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 119 | fPixelConfig = config; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 120 | fFlags = flags; |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 121 | fBackingTextureSize = backingTextureSize; |
| 122 | fNumPlotsX = numPlotsX; |
| 123 | fNumPlotsY = numPlotsY; |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 124 | fBatchUploads = batchUploads; |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 125 | fTexture = NULL; |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 126 | |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 127 | int textureWidth = fBackingTextureSize.width(); |
| 128 | int textureHeight = fBackingTextureSize.height(); |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 129 | |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 130 | int plotWidth = textureWidth / fNumPlotsX; |
| 131 | int plotHeight = textureHeight / fNumPlotsY; |
| 132 | |
| 133 | SkASSERT(plotWidth * fNumPlotsX == textureWidth); |
| 134 | SkASSERT(plotHeight * fNumPlotsY == textureHeight); |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 135 | |
commit-bot@chromium.org | 6e7ddaa | 2014-05-30 13:55:58 +0000 | [diff] [blame] | 136 | // We currently do not support compressed atlases... |
| 137 | SkASSERT(!GrPixelConfigIsCompressed(config)); |
| 138 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 139 | // set up allocated plots |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 140 | size_t bpp = GrBytesPerPixel(fPixelConfig); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 141 | fPlotArray = new GrPlot[(fNumPlotsX * fNumPlotsY)]; |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 142 | |
| 143 | GrPlot* currPlot = fPlotArray; |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 144 | for (int y = numPlotsY-1; y >= 0; --y) { |
| 145 | for (int x = numPlotsX-1; x >= 0; --x) { |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 146 | currPlot->init(this, y*numPlotsX+x, x, y, plotWidth, plotHeight, bpp, batchUploads); |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 147 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 148 | // build LRU list |
| 149 | fPlotList.addToHead(currPlot); |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 150 | ++currPlot; |
| 151 | } |
| 152 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | } |
| 154 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 155 | GrAtlas::~GrAtlas() { |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 156 | SkSafeUnref(fTexture); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 157 | delete[] fPlotArray; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 158 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | fGpu->unref(); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 160 | #if FONT_CACHE_STATS |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 161 | SkDebugf("Num uploads: %d\n", g_UploadCount); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 162 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 163 | } |
| 164 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 165 | void GrAtlas::makeMRU(GrPlot* plot) { |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 166 | if (fPlotList.head() == plot) { |
| 167 | return; |
| 168 | } |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 169 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 170 | fPlotList.remove(plot); |
| 171 | fPlotList.addToHead(plot); |
| 172 | }; |
| 173 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 174 | GrPlot* GrAtlas::addToAtlas(ClientPlotUsage* usage, |
| 175 | int width, int height, const void* image, |
| 176 | SkIPoint16* loc) { |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 177 | // iterate through entire plot list for this atlas, see if we can find a hole |
| 178 | // last one was most recently added and probably most empty |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 179 | for (int i = usage->fPlots.count()-1; i >= 0; --i) { |
| 180 | GrPlot* plot = usage->fPlots[i]; |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 181 | // client may have plots from more than one atlas, must check for ours before adding |
| 182 | if (this == plot->fAtlas && plot->addSubImage(width, height, image, loc)) { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 183 | this->makeMRU(plot); |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 184 | return plot; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 185 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 | } |
| 187 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 188 | // 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] | 189 | if (NULL == fTexture) { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 190 | // TODO: Update this to use the cache rather than directly creating a texture. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 191 | GrSurfaceDesc desc; |
| 192 | desc.fFlags = fFlags; |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 193 | desc.fWidth = fBackingTextureSize.width(); |
| 194 | desc.fHeight = fBackingTextureSize.height(); |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 195 | desc.fConfig = fPixelConfig; |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 196 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 197 | fTexture = fGpu->createTexture(desc, true, NULL, 0); |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 198 | if (NULL == fTexture) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 199 | return NULL; |
| 200 | } |
| 201 | } |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 202 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 203 | // now look through all allocated plots for one we can share, in MRU order |
| 204 | GrPlotList::Iter plotIter; |
| 205 | plotIter.init(fPlotList, GrPlotList::Iter::kHead_IterStart); |
| 206 | GrPlot* plot; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 207 | while ((plot = plotIter.get())) { |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 208 | // make sure texture is set for quick lookup |
| 209 | plot->fTexture = fTexture; |
| 210 | if (plot->addSubImage(width, height, image, loc)) { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 211 | this->makeMRU(plot); |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 212 | // new plot for atlas, put at end of array |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 213 | SkASSERT(!usage->fPlots.contains(plot)); |
| 214 | *(usage->fPlots.append()) = plot; |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 215 | return plot; |
| 216 | } |
| 217 | plotIter.next(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | } |
| 219 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 220 | // If the above fails, then the current plot list has no room |
| 221 | return NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 222 | } |
| 223 | |
robertphillips | c4f30b1 | 2014-07-13 10:09:42 -0700 | [diff] [blame] | 224 | void GrAtlas::RemovePlot(ClientPlotUsage* usage, const GrPlot* plot) { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 225 | int index = usage->fPlots.find(const_cast<GrPlot*>(plot)); |
| 226 | if (index >= 0) { |
| 227 | usage->fPlots.remove(index); |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 228 | } |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 229 | } |