blob: 70fe3e017fe72873b2e65b5413afdbead058d8cb [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
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.org09846a02013-10-02 17:37:59 +000028#define GR_ATLAS_WIDTH 256
29#define GR_ATLAS_HEIGHT 256
reed@google.comac10a2d2010-12-22 21:39:39 +000030
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
38#define BORDER 1
39
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000040#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +000041 static int gCounter;
42#endif
43
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000044// for testing
45#define FONT_CACHE_STATS 0
46#if FONT_CACHE_STATS
47static int g_UploadCount = 0;
48#endif
49
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000050GrPlot::GrPlot() : fDrawToken(NULL, 0)
51 , fNext(NULL)
52 , fTexture(NULL)
53 , fAtlasMgr(NULL)
54 , fBytesPerPixel(1)
55{
reed@google.comac10a2d2010-12-22 21:39:39 +000056 fRects = GrRectanizer::Factory(GR_ATLAS_WIDTH - BORDER,
57 GR_ATLAS_HEIGHT - BORDER);
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000058 fOffset.set(0, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +000059}
60
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000061GrPlot::~GrPlot() {
reed@google.comac10a2d2010-12-22 21:39:39 +000062 delete fRects;
reed@google.comac10a2d2010-12-22 21:39:39 +000063}
64
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000065static void adjust_for_offset(GrIPoint16* loc, const GrIPoint16& offset) {
66 loc->fX += offset.fX * GR_ATLAS_WIDTH;
67 loc->fY += offset.fY * GR_ATLAS_HEIGHT;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000068}
69
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000070static uint8_t* zero_fill(uint8_t* ptr, int count) {
reed@google.com98539c62011-03-15 15:40:16 +000071 while (--count >= 0) {
72 *ptr++ = 0;
73 }
74 return ptr;
75}
76
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000077bool GrPlot::addSubImage(int width, int height, const void* image,
reed@google.comac10a2d2010-12-22 21:39:39 +000078 GrIPoint16* loc) {
79 if (!fRects->addRect(width + BORDER, height + BORDER, loc)) {
80 return false;
81 }
82
bsalomon@google.com3582bf92011-06-30 21:32:31 +000083 SkAutoSMalloc<1024> storage;
reed@google.com98539c62011-03-15 15:40:16 +000084 int dstW = width + 2*BORDER;
85 int dstH = height + 2*BORDER;
reed@google.comac10a2d2010-12-22 21:39:39 +000086 if (BORDER) {
commit-bot@chromium.org95294412013-09-26 15:28:40 +000087 const size_t dstRB = dstW * fBytesPerPixel;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +000088 uint8_t* dst = (uint8_t*)storage.reset(dstH * dstRB);
reed@google.com939ca7c2013-09-26 19:56:51 +000089 sk_bzero(dst, dstRB); // zero top row
reed@google.com98539c62011-03-15 15:40:16 +000090 dst += dstRB;
reed@google.comac10a2d2010-12-22 21:39:39 +000091 for (int y = 0; y < height; y++) {
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000092 dst = zero_fill(dst, fBytesPerPixel); // zero left edge
commit-bot@chromium.org95294412013-09-26 15:28:40 +000093 memcpy(dst, image, width * fBytesPerPixel);
94 dst += width * fBytesPerPixel;
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000095 dst = zero_fill(dst, fBytesPerPixel); // zero right edge
commit-bot@chromium.org95294412013-09-26 15:28:40 +000096 image = (const void*)((const char*)image + width * fBytesPerPixel);
reed@google.comac10a2d2010-12-22 21:39:39 +000097 }
reed@google.com939ca7c2013-09-26 19:56:51 +000098 sk_bzero(dst, dstRB); // zero bottom row
reed@google.comac10a2d2010-12-22 21:39:39 +000099 image = storage.get();
100 }
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000101 adjust_for_offset(loc, fOffset);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000102 GrContext* context = fTexture->getContext();
bsalomon@google.com0342a852012-08-20 19:22:38 +0000103 // We pass the flag that does not force a flush. We assume our caller is
104 // smart and hasn't referenced the part of the texture we're about to update
105 // since the last flush.
106 context->writeTexturePixels(fTexture,
107 loc->fX, loc->fY, dstW, dstH,
108 fTexture->config(), image, 0,
109 GrContext::kDontFlush_PixelOpsFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
111 // now tell the caller to skip the top/left BORDER
112 loc->fX += BORDER;
113 loc->fY += BORDER;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000114
115#if FONT_CACHE_STATS
116 ++g_UploadCount;
117#endif
118
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 return true;
120}
121
122///////////////////////////////////////////////////////////////////////////////
123
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000124GrAtlasMgr::GrAtlasMgr(GrGpu* gpu, GrPixelConfig config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 fGpu = gpu;
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000126 fPixelConfig = config;
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 gpu->ref();
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000128 fTexture = NULL;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000129
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000130 // set up allocated plots
131 int bpp = GrBytesPerPixel(fPixelConfig);
132 fPlots = SkNEW_ARRAY(GrPlot, (GR_PLOT_WIDTH*GR_PLOT_HEIGHT));
133 fFreePlots = NULL;
134 GrPlot* currPlot = fPlots;
135 for (int y = GR_PLOT_HEIGHT-1; y >= 0; --y) {
136 for (int x = GR_PLOT_WIDTH-1; x >= 0; --x) {
137 currPlot->fAtlasMgr = this;
138 currPlot->fOffset.set(x, y);
139 currPlot->fBytesPerPixel = bpp;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000140
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000141 // add to free list
142 currPlot->fNext = fFreePlots;
143 fFreePlots = currPlot;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000144
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000145 ++currPlot;
146 }
147 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000148}
149
150GrAtlasMgr::~GrAtlasMgr() {
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000151 SkSafeUnref(fTexture);
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000152 SkDELETE_ARRAY(fPlots);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000153
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 fGpu->unref();
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000155#if FONT_CACHE_STATS
156 GrPrintf("Num uploads: %d\n", g_UploadCount);
157#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000158}
159
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000160GrPlot* GrAtlasMgr::addToAtlas(GrAtlas* atlas,
161 int width, int height, const void* image,
162 GrIPoint16* loc) {
163 // iterate through entire plot list, see if we can find a hole
164 GrPlot* plotIter = atlas->fPlots;
165 while (plotIter) {
166 if (plotIter->addSubImage(width, height, image, loc)) {
167 return plotIter;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000168 }
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000169 plotIter = plotIter->fNext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 }
171
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000172 // If the above fails, then either we have no starting plot, or the current
173 // plot list is full. Either way we need to allocate a new plot
174 GrPlot* newPlot = this->allocPlot();
175 if (NULL == newPlot) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 return NULL;
177 }
178
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000179 if (NULL == fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000180 // TODO: Update this to use the cache rather than directly creating a texture.
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000181 GrTextureDesc desc;
182 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
183 desc.fWidth = GR_ATLAS_TEXTURE_WIDTH;
184 desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT;
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000185 desc.fConfig = fPixelConfig;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000186
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000187 fTexture = fGpu->createTexture(desc, NULL, 0);
188 if (NULL == fTexture) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 return NULL;
190 }
191 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000192 // be sure to set texture for fast lookup
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000193 newPlot->fTexture = fTexture;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000194
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000195 if (!newPlot->addSubImage(width, height, image, loc)) {
196 this->freePlot(newPlot);
reed@google.comac10a2d2010-12-22 21:39:39 +0000197 return NULL;
198 }
199
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000200 // new plot, put at head
201 newPlot->fNext = atlas->fPlots;
202 atlas->fPlots = newPlot;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000203
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000204 return newPlot;
reed@google.comac10a2d2010-12-22 21:39:39 +0000205}
206
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000207bool GrAtlasMgr::removeUnusedPlots(GrAtlas* atlas) {
208 // GrPlot** is used so that the head element can be easily
209 // modified when the first element is deleted
210 GrPlot** plotRef = &atlas->fPlots;
211 GrPlot* plot = atlas->fPlots;
212 bool removed = false;
213 while (NULL != plot) {
214 if (plot->drawToken().isIssued()) {
215 *plotRef = plot->fNext;
216 this->freePlot(plot);
217 plot = *plotRef;
218 removed = true;
219 } else {
220 plotRef = &plot->fNext;
221 plot = plot->fNext;
222 }
223 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000224
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000225 return removed;
226}
227
228void GrAtlasMgr::deletePlotList(GrPlot* plot) {
229 while (NULL != plot) {
230 GrPlot* next = plot->fNext;
231 this->freePlot(plot);
232 plot = next;
233 }
234}
235
236GrPlot* GrAtlasMgr::allocPlot() {
237 if (NULL == fFreePlots) {
238 return NULL;
239 } else {
240 GrPlot* alloc = fFreePlots;
241 fFreePlots = alloc->fNext;
242#ifdef SK_DEBUG
243// GrPrintf(" GrPlot %p [%d %d] %d\n", this, alloc->fOffset.fX, alloc->fOffset.fY, gCounter);
244 gCounter += 1;
245#endif
246 return alloc;
247 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000248
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000249}
250
251void GrAtlasMgr::freePlot(GrPlot* plot) {
252 SkASSERT(this == plot->fAtlasMgr);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000253
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000254 plot->fRects->reset();
255 plot->fNext = fFreePlots;
256 fFreePlots = plot;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000257
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000258#ifdef SK_DEBUG
259 --gCounter;
260// GrPrintf("~GrPlot %p [%d %d] %d\n", this, plot->fOffset.fX, plot->fOffset.fY, gCounter);
261#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000262}