blob: dd362908d3fedc65a3459dc52bc0b88dcd250563 [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
robertphillips@google.com8b169312013-10-15 17:47:36 +000065static inline void adjust_for_offset(GrIPoint16* loc, const GrIPoint16& offset) {
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000066 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
robertphillips@google.com8b169312013-10-15 17:47:36 +000070static inline uint8_t* zero_fill(uint8_t* ptr, size_t count) {
71 sk_bzero(ptr, count);
72 return ptr + count;
reed@google.com98539c62011-03-15 15:40:16 +000073}
74
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000075bool GrPlot::addSubImage(int width, int height, const void* image,
reed@google.comac10a2d2010-12-22 21:39:39 +000076 GrIPoint16* loc) {
77 if (!fRects->addRect(width + BORDER, height + BORDER, loc)) {
78 return false;
79 }
80
bsalomon@google.com3582bf92011-06-30 21:32:31 +000081 SkAutoSMalloc<1024> storage;
reed@google.com98539c62011-03-15 15:40:16 +000082 int dstW = width + 2*BORDER;
83 int dstH = height + 2*BORDER;
reed@google.comac10a2d2010-12-22 21:39:39 +000084 if (BORDER) {
commit-bot@chromium.org95294412013-09-26 15:28:40 +000085 const size_t dstRB = dstW * fBytesPerPixel;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +000086 uint8_t* dst = (uint8_t*)storage.reset(dstH * dstRB);
reed@google.com939ca7c2013-09-26 19:56:51 +000087 sk_bzero(dst, dstRB); // zero top row
reed@google.com98539c62011-03-15 15:40:16 +000088 dst += dstRB;
reed@google.comac10a2d2010-12-22 21:39:39 +000089 for (int y = 0; y < height; y++) {
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000090 dst = zero_fill(dst, fBytesPerPixel); // zero left edge
commit-bot@chromium.org95294412013-09-26 15:28:40 +000091 memcpy(dst, image, width * fBytesPerPixel);
92 dst += width * fBytesPerPixel;
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000093 dst = zero_fill(dst, fBytesPerPixel); // zero right edge
commit-bot@chromium.org95294412013-09-26 15:28:40 +000094 image = (const void*)((const char*)image + width * fBytesPerPixel);
reed@google.comac10a2d2010-12-22 21:39:39 +000095 }
reed@google.com939ca7c2013-09-26 19:56:51 +000096 sk_bzero(dst, dstRB); // zero bottom row
reed@google.comac10a2d2010-12-22 21:39:39 +000097 image = storage.get();
98 }
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000099 adjust_for_offset(loc, fOffset);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000100 GrContext* context = fTexture->getContext();
bsalomon@google.com0342a852012-08-20 19:22:38 +0000101 // We pass the flag that does not force a flush. We assume our caller is
102 // smart and hasn't referenced the part of the texture we're about to update
103 // since the last flush.
104 context->writeTexturePixels(fTexture,
105 loc->fX, loc->fY, dstW, dstH,
106 fTexture->config(), image, 0,
107 GrContext::kDontFlush_PixelOpsFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
109 // now tell the caller to skip the top/left BORDER
110 loc->fX += BORDER;
111 loc->fY += BORDER;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000112
113#if FONT_CACHE_STATS
114 ++g_UploadCount;
115#endif
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 return true;
118}
119
120///////////////////////////////////////////////////////////////////////////////
121
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000122GrAtlasMgr::GrAtlasMgr(GrGpu* gpu, GrPixelConfig config) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 fGpu = gpu;
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000124 fPixelConfig = config;
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 gpu->ref();
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000126 fTexture = NULL;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000127
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000128 // set up allocated plots
robertphillips@google.com8b169312013-10-15 17:47:36 +0000129 size_t bpp = GrBytesPerPixel(fPixelConfig);
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000130 fPlots = SkNEW_ARRAY(GrPlot, (GR_PLOT_WIDTH*GR_PLOT_HEIGHT));
131 fFreePlots = NULL;
132 GrPlot* currPlot = fPlots;
133 for (int y = GR_PLOT_HEIGHT-1; y >= 0; --y) {
134 for (int x = GR_PLOT_WIDTH-1; x >= 0; --x) {
135 currPlot->fAtlasMgr = this;
136 currPlot->fOffset.set(x, y);
137 currPlot->fBytesPerPixel = bpp;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000138
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000139 // add to free list
140 currPlot->fNext = fFreePlots;
141 fFreePlots = currPlot;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000142
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000143 ++currPlot;
144 }
145 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000146}
147
148GrAtlasMgr::~GrAtlasMgr() {
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000149 SkSafeUnref(fTexture);
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000150 SkDELETE_ARRAY(fPlots);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000151
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 fGpu->unref();
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000153#if FONT_CACHE_STATS
154 GrPrintf("Num uploads: %d\n", g_UploadCount);
155#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000156}
157
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000158GrPlot* GrAtlasMgr::addToAtlas(GrAtlas* atlas,
159 int width, int height, const void* image,
160 GrIPoint16* loc) {
161 // iterate through entire plot list, see if we can find a hole
162 GrPlot* plotIter = atlas->fPlots;
163 while (plotIter) {
164 if (plotIter->addSubImage(width, height, image, loc)) {
165 return plotIter;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000166 }
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000167 plotIter = plotIter->fNext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 }
169
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000170 // If the above fails, then either we have no starting plot, or the current
171 // plot list is full. Either way we need to allocate a new plot
172 GrPlot* newPlot = this->allocPlot();
173 if (NULL == newPlot) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 return NULL;
175 }
176
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000177 if (NULL == fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000178 // TODO: Update this to use the cache rather than directly creating a texture.
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000179 GrTextureDesc desc;
180 desc.fFlags = kDynamicUpdate_GrTextureFlagBit;
181 desc.fWidth = GR_ATLAS_TEXTURE_WIDTH;
182 desc.fHeight = GR_ATLAS_TEXTURE_HEIGHT;
commit-bot@chromium.org95294412013-09-26 15:28:40 +0000183 desc.fConfig = fPixelConfig;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000184
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000185 fTexture = fGpu->createTexture(desc, NULL, 0);
186 if (NULL == fTexture) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000187 return NULL;
188 }
189 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000190 // be sure to set texture for fast lookup
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000191 newPlot->fTexture = fTexture;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000192
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000193 if (!newPlot->addSubImage(width, height, image, loc)) {
194 this->freePlot(newPlot);
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 return NULL;
196 }
197
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000198 // new plot, put at head
199 newPlot->fNext = atlas->fPlots;
200 atlas->fPlots = newPlot;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000201
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000202 return newPlot;
reed@google.comac10a2d2010-12-22 21:39:39 +0000203}
204
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000205bool GrAtlasMgr::removeUnusedPlots(GrAtlas* atlas) {
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000206
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000207 // GrPlot** is used so that the head element can be easily
208 // modified when the first element is deleted
209 GrPlot** plotRef = &atlas->fPlots;
210 GrPlot* plot = atlas->fPlots;
211 bool removed = false;
212 while (NULL != plot) {
213 if (plot->drawToken().isIssued()) {
214 *plotRef = plot->fNext;
215 this->freePlot(plot);
216 plot = *plotRef;
217 removed = true;
218 } else {
219 plotRef = &plot->fNext;
220 plot = plot->fNext;
221 }
222 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000223
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000224 return removed;
225}
226
227void GrAtlasMgr::deletePlotList(GrPlot* plot) {
228 while (NULL != plot) {
229 GrPlot* next = plot->fNext;
230 this->freePlot(plot);
231 plot = next;
232 }
233}
234
235GrPlot* GrAtlasMgr::allocPlot() {
236 if (NULL == fFreePlots) {
237 return NULL;
238 } else {
239 GrPlot* alloc = fFreePlots;
240 fFreePlots = alloc->fNext;
241#ifdef SK_DEBUG
242// GrPrintf(" GrPlot %p [%d %d] %d\n", this, alloc->fOffset.fX, alloc->fOffset.fY, gCounter);
243 gCounter += 1;
244#endif
245 return alloc;
246 }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000247
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000248}
249
250void GrAtlasMgr::freePlot(GrPlot* plot) {
251 SkASSERT(this == plot->fAtlasMgr);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000252
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000253 plot->fRects->reset();
254 plot->fNext = fFreePlots;
255 fFreePlots = plot;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000256
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000257#ifdef SK_DEBUG
258 --gCounter;
259// GrPrintf("~GrPlot %p [%d %d] %d\n", this, plot->fOffset.fX, plot->fOffset.fY, gCounter);
260#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000261}