blob: a9b8ae95e6f0f67ffd6ed1230e7514d42e9364d0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrAtlas.h"
bsalomon@google.com6f379512011-11-16 20:36:03 +000010#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrGpu.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrRectanizer.h"
jvanverth8e80d172014-06-19 12:01:10 -070013#include "GrTracing.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
reed@google.comac10a2d2010-12-22 21:39:39 +000015///////////////////////////////////////////////////////////////////////////////
16
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000017// for testing
18#define FONT_CACHE_STATS 0
19#if FONT_CACHE_STATS
20static int g_UploadCount = 0;
21#endif
22
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000023GrPlot::GrPlot() : fDrawToken(NULL, 0)
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000024 , fTexture(NULL)
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000025 , fRects(NULL)
robertphillips1d86ee82014-06-24 15:08:49 -070026 , fAtlas(NULL)
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000027 , fBytesPerPixel(1)
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000028 , fDirty(false)
29 , fBatchUploads(false)
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000030{
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000031 fOffset.set(0, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +000032}
33
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000034GrPlot::~GrPlot() {
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000035 SkDELETE_ARRAY(fPlotData);
36 fPlotData = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000037 delete fRects;
reed@google.comac10a2d2010-12-22 21:39:39 +000038}
39
robertphillips1d86ee82014-06-24 15:08:49 -070040void GrPlot::init(GrAtlas* atlas, int offX, int offY, int width, int height, size_t bpp,
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000041 bool batchUploads) {
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000042 fRects = GrRectanizer::Factory(width, height);
robertphillips1d86ee82014-06-24 15:08:49 -070043 fAtlas = atlas;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000044 fOffset.set(offX * width, offY * height);
45 fBytesPerPixel = bpp;
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000046 fPlotData = NULL;
47 fDirtyRect.setEmpty();
48 fDirty = false;
49 fBatchUploads = batchUploads;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000050}
51
robertphillipsd5373412014-06-02 10:20:14 -070052static inline void adjust_for_offset(SkIPoint16* loc, const SkIPoint16& offset) {
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000053 loc->fX += offset.fX;
54 loc->fY += offset.fY;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000055}
56
robertphillips952841b2014-06-30 08:26:50 -070057bool GrPlot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000058 float percentFull = fRects->percentFull();
commit-bot@chromium.orgf9529242014-02-14 18:41:47 +000059 if (!fRects->addRect(width, height, loc)) {
reed@google.comac10a2d2010-12-22 21:39:39 +000060 return false;
61 }
62
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000063 // if batching uploads, create backing memory on first use
64 // once the plot is nearly full we will revert to uploading each subimage individually
65 int plotWidth = fRects->width();
66 int plotHeight = fRects->height();
67 if (fBatchUploads && NULL == fPlotData && 0.0f == percentFull) {
68 fPlotData = SkNEW_ARRAY(unsigned char, fBytesPerPixel*plotWidth*plotHeight);
69 memset(fPlotData, 0, fBytesPerPixel*plotWidth*plotHeight);
70 }
71
72 // if we have backing memory, copy to the memory and set for future upload
73 if (NULL != fPlotData) {
74 const unsigned char* imagePtr = (const unsigned char*) image;
75 // point ourselves at the right starting spot
76 unsigned char* dataPtr = fPlotData;
77 dataPtr += fBytesPerPixel*plotWidth*loc->fY;
78 dataPtr += fBytesPerPixel*loc->fX;
79 // copy into the data buffer
80 for (int i = 0; i < height; ++i) {
81 memcpy(dataPtr, imagePtr, fBytesPerPixel*width);
82 dataPtr += fBytesPerPixel*plotWidth;
83 imagePtr += fBytesPerPixel*width;
84 }
85
86 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
87 adjust_for_offset(loc, fOffset);
88 fDirty = true;
89 // otherwise, just upload the image directly
robertphillips952841b2014-06-30 08:26:50 -070090 } else if (NULL != image) {
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000091 adjust_for_offset(loc, fOffset);
92 GrContext* context = fTexture->getContext();
jvanverth8e80d172014-06-19 12:01:10 -070093 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrPlot::uploadToTexture");
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000094 context->writeTexturePixels(fTexture,
95 loc->fX, loc->fY, width, height,
96 fTexture->config(), image, 0,
97 GrContext::kDontFlush_PixelOpsFlag);
robertphillips952841b2014-06-30 08:26:50 -070098 } else {
99 adjust_for_offset(loc, fOffset);
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000100 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000101
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000102#if FONT_CACHE_STATS
103 ++g_UploadCount;
104#endif
105
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 return true;
107}
108
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000109void GrPlot::uploadToTexture() {
110 static const float kNearlyFullTolerance = 0.85f;
111
112 // should only do this if batching is enabled
113 SkASSERT(fBatchUploads);
114
115 if (fDirty) {
jvanverth8e80d172014-06-19 12:01:10 -0700116 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrPlot::uploadToTexture");
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000117 SkASSERT(NULL != fTexture);
118 GrContext* context = fTexture->getContext();
119 // We pass the flag that does not force a flush. We assume our caller is
120 // smart and hasn't referenced the part of the texture we're about to update
121 // since the last flush.
jvanverth8e80d172014-06-19 12:01:10 -0700122 size_t rowBytes = fBytesPerPixel*fRects->width();
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000123 const unsigned char* dataPtr = fPlotData;
124 dataPtr += rowBytes*fDirtyRect.fTop;
125 dataPtr += fBytesPerPixel*fDirtyRect.fLeft;
126 context->writeTexturePixels(fTexture,
skia.committer@gmail.coma1633da2014-05-15 03:03:58 +0000127 fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000128 fDirtyRect.width(), fDirtyRect.height(),
skia.committer@gmail.coma1633da2014-05-15 03:03:58 +0000129 fTexture->config(), dataPtr,
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000130 rowBytes,
131 GrContext::kDontFlush_PixelOpsFlag);
132 fDirtyRect.setEmpty();
133 fDirty = false;
134 // If the Plot is nearly full, anything else we add will probably be small and one
135 // at a time, so free up the memory and after this upload any new images directly.
136 if (fRects->percentFull() > kNearlyFullTolerance) {
137 SkDELETE_ARRAY(fPlotData);
138 fPlotData = NULL;
139 }
140 }
141}
142
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000143void GrPlot::resetRects() {
144 SkASSERT(NULL != fRects);
145 fRects->reset();
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000146}
147
reed@google.comac10a2d2010-12-22 21:39:39 +0000148///////////////////////////////////////////////////////////////////////////////
149
robertphillips952841b2014-06-30 08:26:50 -0700150GrAtlas::GrAtlas(GrGpu* gpu, GrPixelConfig config, GrTextureFlags flags,
robertphillips1d86ee82014-06-24 15:08:49 -0700151 const SkISize& backingTextureSize,
152 int numPlotsX, int numPlotsY, bool batchUploads) {
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000153 fGpu = SkRef(gpu);
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000154 fPixelConfig = config;
robertphillips952841b2014-06-30 08:26:50 -0700155 fFlags = flags;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000156 fBackingTextureSize = backingTextureSize;
157 fNumPlotsX = numPlotsX;
158 fNumPlotsY = numPlotsY;
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000159 fBatchUploads = batchUploads;
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000160 fTexture = NULL;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000161
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000162 int textureWidth = fBackingTextureSize.width();
163 int textureHeight = fBackingTextureSize.height();
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000164
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000165 int plotWidth = textureWidth / fNumPlotsX;
166 int plotHeight = textureHeight / fNumPlotsY;
167
168 SkASSERT(plotWidth * fNumPlotsX == textureWidth);
169 SkASSERT(plotHeight * fNumPlotsY == textureHeight);
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000170
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000171 // We currently do not support compressed atlases...
172 SkASSERT(!GrPixelConfigIsCompressed(config));
173
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000174 // set up allocated plots
robertphillips@google.com8b169312013-10-15 17:47:36 +0000175 size_t bpp = GrBytesPerPixel(fPixelConfig);
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000176 fPlotArray = SkNEW_ARRAY(GrPlot, (fNumPlotsX*fNumPlotsY));
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000177
178 GrPlot* currPlot = fPlotArray;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000179 for (int y = numPlotsY-1; y >= 0; --y) {
180 for (int x = numPlotsX-1; x >= 0; --x) {
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000181 currPlot->init(this, x, y, plotWidth, plotHeight, bpp, batchUploads);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000182
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000183 // build LRU list
184 fPlotList.addToHead(currPlot);
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000185 ++currPlot;
186 }
187 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000188}
189
robertphillips1d86ee82014-06-24 15:08:49 -0700190GrAtlas::~GrAtlas() {
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000191 SkSafeUnref(fTexture);
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000192 SkDELETE_ARRAY(fPlotArray);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000193
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 fGpu->unref();
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000195#if FONT_CACHE_STATS
196 GrPrintf("Num uploads: %d\n", g_UploadCount);
197#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000198}
199
robertphillips1d86ee82014-06-24 15:08:49 -0700200void GrAtlas::makeMRU(GrPlot* plot) {
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000201 if (fPlotList.head() == plot) {
202 return;
203 }
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000204
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000205 fPlotList.remove(plot);
206 fPlotList.addToHead(plot);
207};
208
robertphillips1d86ee82014-06-24 15:08:49 -0700209GrPlot* GrAtlas::addToAtlas(ClientPlotUsage* usage,
210 int width, int height, const void* image,
211 SkIPoint16* loc) {
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000212 // iterate through entire plot list for this atlas, see if we can find a hole
213 // last one was most recently added and probably most empty
robertphillips1d86ee82014-06-24 15:08:49 -0700214 for (int i = usage->fPlots.count()-1; i >= 0; --i) {
215 GrPlot* plot = usage->fPlots[i];
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000216 if (plot->addSubImage(width, height, image, loc)) {
robertphillips1d86ee82014-06-24 15:08:49 -0700217 this->makeMRU(plot);
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000218 return plot;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000219 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 }
221
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000222 // before we get a new plot, make sure we have a backing texture
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000223 if (NULL == fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000224 // TODO: Update this to use the cache rather than directly creating a texture.
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000225 GrTextureDesc desc;
robertphillips952841b2014-06-30 08:26:50 -0700226 desc.fFlags = fFlags | kDynamicUpdate_GrTextureFlagBit;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000227 desc.fWidth = fBackingTextureSize.width();
228 desc.fHeight = fBackingTextureSize.height();
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000229 desc.fConfig = fPixelConfig;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000230
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000231 fTexture = fGpu->createTexture(desc, NULL, 0);
232 if (NULL == fTexture) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 return NULL;
234 }
235 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000236
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000237 // now look through all allocated plots for one we can share, in MRU order
238 GrPlotList::Iter plotIter;
239 plotIter.init(fPlotList, GrPlotList::Iter::kHead_IterStart);
240 GrPlot* plot;
241 while (NULL != (plot = plotIter.get())) {
242 // make sure texture is set for quick lookup
243 plot->fTexture = fTexture;
244 if (plot->addSubImage(width, height, image, loc)) {
robertphillips1d86ee82014-06-24 15:08:49 -0700245 this->makeMRU(plot);
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000246 // new plot for atlas, put at end of array
robertphillips1d86ee82014-06-24 15:08:49 -0700247 SkASSERT(!usage->fPlots.contains(plot));
248 *(usage->fPlots.append()) = plot;
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000249 return plot;
250 }
251 plotIter.next();
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 }
253
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000254 // If the above fails, then the current plot list has no room
255 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000256}
257
robertphillipsc4f30b12014-07-13 10:09:42 -0700258void GrAtlas::RemovePlot(ClientPlotUsage* usage, const GrPlot* plot) {
robertphillips1d86ee82014-06-24 15:08:49 -0700259 int index = usage->fPlots.find(const_cast<GrPlot*>(plot));
260 if (index >= 0) {
261 usage->fPlots.remove(index);
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000262 }
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000263}
264
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000265// get a plot that's not being used by the current draw
robertphillips1d86ee82014-06-24 15:08:49 -0700266GrPlot* GrAtlas::getUnusedPlot() {
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000267 GrPlotList::Iter plotIter;
268 plotIter.init(fPlotList, GrPlotList::Iter::kTail_IterStart);
269 GrPlot* plot;
270 while (NULL != (plot = plotIter.get())) {
271 if (plot->drawToken().isIssued()) {
272 return plot;
273 }
274 plotIter.prev();
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000275 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000276
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000277 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000278}
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000279
robertphillips1d86ee82014-06-24 15:08:49 -0700280void GrAtlas::uploadPlotsToTexture() {
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000281 if (fBatchUploads) {
282 GrPlotList::Iter plotIter;
283 plotIter.init(fPlotList, GrPlotList::Iter::kHead_IterStart);
284 GrPlot* plot;
285 while (NULL != (plot = plotIter.get())) {
286 plot->uploadToTexture();
287 plotIter.next();
288 }
289 }
290}