blob: cc98b925c257b4dcc2af54797e4f60ef70c01b5d [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"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
reed@google.comac10a2d2010-12-22 21:39:39 +000014///////////////////////////////////////////////////////////////////////////////
15
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000016// for testing
17#define FONT_CACHE_STATS 0
18#if FONT_CACHE_STATS
19static int g_UploadCount = 0;
20#endif
21
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000022GrPlot::GrPlot() : fDrawToken(NULL, 0)
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000023 , fTexture(NULL)
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000024 , fRects(NULL)
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000025 , fAtlasMgr(NULL)
26 , fBytesPerPixel(1)
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000027 , fDirty(false)
28 , fBatchUploads(false)
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000029{
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000030 fOffset.set(0, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +000031}
32
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000033GrPlot::~GrPlot() {
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000034 SkDELETE_ARRAY(fPlotData);
35 fPlotData = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000036 delete fRects;
reed@google.comac10a2d2010-12-22 21:39:39 +000037}
38
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000039void GrPlot::init(GrAtlasMgr* mgr, int offX, int offY, int width, int height, size_t bpp,
40 bool batchUploads) {
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000041 fRects = GrRectanizer::Factory(width, height);
42 fAtlasMgr = mgr;
43 fOffset.set(offX * width, offY * height);
44 fBytesPerPixel = bpp;
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000045 fPlotData = NULL;
46 fDirtyRect.setEmpty();
47 fDirty = false;
48 fBatchUploads = batchUploads;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000049}
50
robertphillips@google.com8b169312013-10-15 17:47:36 +000051static inline void adjust_for_offset(GrIPoint16* loc, const GrIPoint16& offset) {
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000052 loc->fX += offset.fX;
53 loc->fY += offset.fY;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000054}
55
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000056bool GrPlot::addSubImage(int width, int height, const void* image,
reed@google.comac10a2d2010-12-22 21:39:39 +000057 GrIPoint16* 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
90 } else {
91 adjust_for_offset(loc, fOffset);
92 GrContext* context = fTexture->getContext();
93 context->writeTexturePixels(fTexture,
94 loc->fX, loc->fY, width, height,
95 fTexture->config(), image, 0,
96 GrContext::kDontFlush_PixelOpsFlag);
97 }
reed@google.comac10a2d2010-12-22 21:39:39 +000098
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000099#if FONT_CACHE_STATS
100 ++g_UploadCount;
101#endif
102
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 return true;
104}
105
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000106void GrPlot::uploadToTexture() {
107 static const float kNearlyFullTolerance = 0.85f;
108
109 // should only do this if batching is enabled
110 SkASSERT(fBatchUploads);
111
112 if (fDirty) {
113 SkASSERT(NULL != fTexture);
114 GrContext* context = fTexture->getContext();
115 // We pass the flag that does not force a flush. We assume our caller is
116 // smart and hasn't referenced the part of the texture we're about to update
117 // since the last flush.
118 int rowBytes = fBytesPerPixel*fRects->width();
119 const unsigned char* dataPtr = fPlotData;
120 dataPtr += rowBytes*fDirtyRect.fTop;
121 dataPtr += fBytesPerPixel*fDirtyRect.fLeft;
122 context->writeTexturePixels(fTexture,
skia.committer@gmail.coma1633da2014-05-15 03:03:58 +0000123 fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000124 fDirtyRect.width(), fDirtyRect.height(),
skia.committer@gmail.coma1633da2014-05-15 03:03:58 +0000125 fTexture->config(), dataPtr,
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000126 rowBytes,
127 GrContext::kDontFlush_PixelOpsFlag);
128 fDirtyRect.setEmpty();
129 fDirty = false;
130 // If the Plot is nearly full, anything else we add will probably be small and one
131 // at a time, so free up the memory and after this upload any new images directly.
132 if (fRects->percentFull() > kNearlyFullTolerance) {
133 SkDELETE_ARRAY(fPlotData);
134 fPlotData = NULL;
135 }
136 }
137}
138
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000139void GrPlot::resetRects() {
140 SkASSERT(NULL != fRects);
141 fRects->reset();
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000142}
143
reed@google.comac10a2d2010-12-22 21:39:39 +0000144///////////////////////////////////////////////////////////////////////////////
145
skia.committer@gmail.comc282ba82014-04-02 03:05:59 +0000146GrAtlasMgr::GrAtlasMgr(GrGpu* gpu, GrPixelConfig config,
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000147 const SkISize& backingTextureSize,
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000148 int numPlotsX, int numPlotsY, bool batchUploads) {
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000149 fGpu = SkRef(gpu);
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000150 fPixelConfig = config;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000151 fBackingTextureSize = backingTextureSize;
152 fNumPlotsX = numPlotsX;
153 fNumPlotsY = numPlotsY;
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000154 fBatchUploads = batchUploads;
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000155 fTexture = NULL;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000156
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000157 int textureWidth = fBackingTextureSize.width();
158 int textureHeight = fBackingTextureSize.height();
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000159
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000160 int plotWidth = textureWidth / fNumPlotsX;
161 int plotHeight = textureHeight / fNumPlotsY;
162
163 SkASSERT(plotWidth * fNumPlotsX == textureWidth);
164 SkASSERT(plotHeight * fNumPlotsY == textureHeight);
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000165
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000166 // We currently do not support compressed atlases...
167 SkASSERT(!GrPixelConfigIsCompressed(config));
168
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000169 // set up allocated plots
robertphillips@google.com8b169312013-10-15 17:47:36 +0000170 size_t bpp = GrBytesPerPixel(fPixelConfig);
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000171 fPlotArray = SkNEW_ARRAY(GrPlot, (fNumPlotsX*fNumPlotsY));
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000172
173 GrPlot* currPlot = fPlotArray;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000174 for (int y = numPlotsY-1; y >= 0; --y) {
175 for (int x = numPlotsX-1; x >= 0; --x) {
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000176 currPlot->init(this, x, y, plotWidth, plotHeight, bpp, batchUploads);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000177
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000178 // build LRU list
179 fPlotList.addToHead(currPlot);
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000180 ++currPlot;
181 }
182 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000183}
184
185GrAtlasMgr::~GrAtlasMgr() {
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000186 SkSafeUnref(fTexture);
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000187 SkDELETE_ARRAY(fPlotArray);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000188
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 fGpu->unref();
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000190#if FONT_CACHE_STATS
191 GrPrintf("Num uploads: %d\n", g_UploadCount);
192#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000193}
194
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000195void GrAtlasMgr::moveToHead(GrPlot* plot) {
196 if (fPlotList.head() == plot) {
197 return;
198 }
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000199
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000200 fPlotList.remove(plot);
201 fPlotList.addToHead(plot);
202};
203
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000204GrPlot* GrAtlasMgr::addToAtlas(GrAtlas* atlas,
205 int width, int height, const void* image,
206 GrIPoint16* loc) {
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000207 // iterate through entire plot list for this atlas, see if we can find a hole
208 // last one was most recently added and probably most empty
209 for (int i = atlas->fPlots.count()-1; i >= 0; --i) {
210 GrPlot* plot = atlas->fPlots[i];
211 if (plot->addSubImage(width, height, image, loc)) {
212 this->moveToHead(plot);
213 return plot;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000214 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 }
216
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000217 // before we get a new plot, make sure we have a backing texture
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000218 if (NULL == fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000219 // TODO: Update this to use the cache rather than directly creating a texture.
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000220 GrTextureDesc desc;
221 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000222 desc.fWidth = fBackingTextureSize.width();
223 desc.fHeight = fBackingTextureSize.height();
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000224 desc.fConfig = fPixelConfig;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000225
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000226 fTexture = fGpu->createTexture(desc, NULL, 0);
227 if (NULL == fTexture) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000228 return NULL;
229 }
230 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000231
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000232 // now look through all allocated plots for one we can share, in MRU order
233 GrPlotList::Iter plotIter;
234 plotIter.init(fPlotList, GrPlotList::Iter::kHead_IterStart);
235 GrPlot* plot;
236 while (NULL != (plot = plotIter.get())) {
237 // make sure texture is set for quick lookup
238 plot->fTexture = fTexture;
239 if (plot->addSubImage(width, height, image, loc)) {
240 this->moveToHead(plot);
241 // new plot for atlas, put at end of array
242 *(atlas->fPlots.append()) = plot;
243 return plot;
244 }
245 plotIter.next();
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 }
247
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000248 // If the above fails, then the current plot list has no room
249 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000250}
251
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000252bool GrAtlasMgr::removePlot(GrAtlas* atlas, const GrPlot* plot) {
253 // iterate through plot list for this atlas
254 int count = atlas->fPlots.count();
255 for (int i = 0; i < count; ++i) {
256 if (plot == atlas->fPlots[i]) {
257 atlas->fPlots.remove(i);
258 return true;
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000259 }
260 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000261
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000262 return false;
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
266GrPlot* GrAtlasMgr::getUnusedPlot() {
267 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
280void GrAtlasMgr::uploadPlotsToTexture() {
281 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}