blob: 7a7c1e4379ae2307ec15b761914428de2e4d3cdc [file] [log] [blame]
jvanverthfa38a302014-10-06 05:59:05 -07001
2/*
3 * Copyright 2014 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.
7 */
8
9#include "GrAADistanceFieldPathRenderer.h"
10
11#include "GrAtlas.h"
12#include "GrContext.h"
13#include "GrDrawState.h"
14#include "GrSurfacePriv.h"
15#include "GrSWMaskHelper.h"
16#include "GrTexturePriv.h"
17#include "effects/GrDistanceFieldTextureEffect.h"
18
19#include "SkDistanceFieldGen.h"
20#include "SkRTConf.h"
21
22#define ATLAS_TEXTURE_WIDTH 1024
jvanverthb61283f2014-10-30 05:57:21 -070023#define ATLAS_TEXTURE_HEIGHT 2048
jvanverthfa38a302014-10-06 05:59:05 -070024#define PLOT_WIDTH 256
25#define PLOT_HEIGHT 256
26
27#define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH)
28#define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT)
29
30SK_CONF_DECLARE(bool, c_DumpPathCache, "gpu.dumpPathCache", false,
31 "Dump the contents of the path cache before every purge.");
32
jvanverthb3eb6872014-10-24 07:12:51 -070033#ifdef DF_PATH_TRACKING
34static int g_NumCachedPaths = 0;
35static int g_NumFreedPaths = 0;
36#endif
37
jvanverthb61283f2014-10-30 05:57:21 -070038// mip levels
39static const int kSmallMIP = 32;
jvanverthada68ef2014-11-03 14:00:24 -080040static const int kMediumMIP = 78;
41static const int kLargeMIP = 192;
jvanverthb61283f2014-10-30 05:57:21 -070042
jvanverthfa38a302014-10-06 05:59:05 -070043////////////////////////////////////////////////////////////////////////////////
jvanverth6d22eca2014-10-28 11:10:48 -070044GrAADistanceFieldPathRenderer::GrAADistanceFieldPathRenderer(GrContext* context)
45 : fContext(context)
46 , fAtlas(NULL)
47 , fEffectFlags(kInvalid_DistanceFieldEffectFlag) {
48}
49
jvanverthfa38a302014-10-06 05:59:05 -070050GrAADistanceFieldPathRenderer::~GrAADistanceFieldPathRenderer() {
51 PathDataList::Iter iter;
52 iter.init(fPathList, PathDataList::Iter::kHead_IterStart);
53 PathData* pathData;
54 while ((pathData = iter.get())) {
55 iter.next();
56 fPathList.remove(pathData);
57 SkDELETE(pathData);
58 }
59
60 SkDELETE(fAtlas);
jvanverthb3eb6872014-10-24 07:12:51 -070061
62#ifdef DF_PATH_TRACKING
63 SkDebugf("Cached paths: %d, freed paths: %d\n", g_NumCachedPaths, g_NumFreedPaths);
64#endif
jvanverthfa38a302014-10-06 05:59:05 -070065}
66
67////////////////////////////////////////////////////////////////////////////////
joshualitt9853cce2014-11-17 14:22:48 -080068bool GrAADistanceFieldPathRenderer::canDrawPath(const GrDrawTarget* target,
69 const GrDrawState* drawState,
70 const SkPath& path,
jvanverthfa38a302014-10-06 05:59:05 -070071 const SkStrokeRec& stroke,
jvanverthfa38a302014-10-06 05:59:05 -070072 bool antiAlias) const {
jvanverthb3eb6872014-10-24 07:12:51 -070073
jvanverthfa38a302014-10-06 05:59:05 -070074 // TODO: Support inverse fill
75 // TODO: Support strokes
76 if (!target->caps()->shaderDerivativeSupport() || !antiAlias || path.isInverseFillType()
jvanverthb3eb6872014-10-24 07:12:51 -070077 || path.isVolatile() || SkStrokeRec::kFill_Style != stroke.getStyle()) {
jvanverthfa38a302014-10-06 05:59:05 -070078 return false;
79 }
80
jvanverthb61283f2014-10-30 05:57:21 -070081 // currently don't support perspective
joshualitt9853cce2014-11-17 14:22:48 -080082 const SkMatrix& vm = drawState->getViewMatrix();
jvanverthb61283f2014-10-30 05:57:21 -070083 if (vm.hasPerspective()) {
jvanverthfa38a302014-10-06 05:59:05 -070084 return false;
85 }
86
jvanverthb61283f2014-10-30 05:57:21 -070087 // only support paths smaller than 64x64, scaled to less than 256x256
88 // the goal is to accelerate rendering of lots of small paths that may be scaling
89 SkScalar maxScale = vm.getMaxScale();
jvanverthfa38a302014-10-06 05:59:05 -070090 const SkRect& bounds = path.getBounds();
jvanverthb61283f2014-10-30 05:57:21 -070091 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
92 return maxDim < 64.f && maxDim*maxScale < 256.f;
jvanverthfa38a302014-10-06 05:59:05 -070093}
94
95
joshualitt9853cce2014-11-17 14:22:48 -080096GrPathRenderer::StencilSupport
97GrAADistanceFieldPathRenderer::onGetStencilSupport(const GrDrawTarget*,
98 const GrDrawState*,
99 const SkPath&,
100 const SkStrokeRec&) const {
jvanverthfa38a302014-10-06 05:59:05 -0700101 return GrPathRenderer::kNoSupport_StencilSupport;
102}
103
104////////////////////////////////////////////////////////////////////////////////
105
joshualitt9853cce2014-11-17 14:22:48 -0800106bool GrAADistanceFieldPathRenderer::onDrawPath(GrDrawTarget* target,
107 GrDrawState* drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800108 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800109 const SkPath& path,
jvanverthfa38a302014-10-06 05:59:05 -0700110 const SkStrokeRec& stroke,
jvanverthfa38a302014-10-06 05:59:05 -0700111 bool antiAlias) {
112 // we've already bailed on inverse filled paths, so this is safe
113 if (path.isEmpty()) {
114 return true;
115 }
116
117 SkASSERT(fContext);
118
jvanverthb61283f2014-10-30 05:57:21 -0700119 // get mip level
joshualitt9853cce2014-11-17 14:22:48 -0800120 const SkMatrix& vm = drawState->getViewMatrix();
jvanverthb61283f2014-10-30 05:57:21 -0700121 SkScalar maxScale = vm.getMaxScale();
122 const SkRect& bounds = path.getBounds();
123 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
124 SkScalar size = maxScale*maxDim;
125 uint32_t desiredDimension;
126 if (size <= kSmallMIP) {
127 desiredDimension = kSmallMIP;
128 } else if (size <= kMediumMIP) {
129 desiredDimension = kMediumMIP;
130 } else {
131 desiredDimension = kLargeMIP;
132 }
133
jvanverthfa38a302014-10-06 05:59:05 -0700134 // check to see if path is cached
135 // TODO: handle stroked vs. filled version of same path
jvanverthb61283f2014-10-30 05:57:21 -0700136 PathData::Key key = { path.getGenerationID(), desiredDimension };
137 PathData* pathData = fPathCache.find(key);
jvanverthfa38a302014-10-06 05:59:05 -0700138 if (NULL == pathData) {
jvanverthb61283f2014-10-30 05:57:21 -0700139 SkScalar scale = desiredDimension/maxDim;
140 pathData = this->addPathToAtlas(path, stroke, antiAlias, desiredDimension, scale);
jvanverthfa38a302014-10-06 05:59:05 -0700141 if (NULL == pathData) {
142 return false;
143 }
144 }
145
146 // use signed distance field to render
joshualitt2e3b3e32014-12-09 13:31:14 -0800147 return this->internalDrawPath(target, drawState, color, path, pathData);
jvanverthfa38a302014-10-06 05:59:05 -0700148}
149
jvanverthfa38a302014-10-06 05:59:05 -0700150// padding around path bounds to allow for antialiased pixels
151const SkScalar kAntiAliasPad = 1.0f;
152
153GrAADistanceFieldPathRenderer::PathData* GrAADistanceFieldPathRenderer::addPathToAtlas(
154 const SkPath& path,
155 const SkStrokeRec& stroke,
jvanverthb61283f2014-10-30 05:57:21 -0700156 bool antiAlias,
157 uint32_t dimension,
158 SkScalar scale) {
jvanverthfa38a302014-10-06 05:59:05 -0700159
160 // generate distance field and add to atlas
161 if (NULL == fAtlas) {
162 SkISize textureSize = SkISize::Make(ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT);
163 fAtlas = SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kAlpha_8_GrPixelConfig,
bsalomonf2703d82014-10-28 14:33:06 -0700164 kNone_GrSurfaceFlags, textureSize,
jvanverthfa38a302014-10-06 05:59:05 -0700165 NUM_PLOTS_X, NUM_PLOTS_Y, false));
166 if (NULL == fAtlas) {
167 return NULL;
168 }
169 }
170
171 const SkRect& bounds = path.getBounds();
172
173 // generate bounding rect for bitmap draw
174 SkRect scaledBounds = bounds;
jvanverthb61283f2014-10-30 05:57:21 -0700175 // scale to mip level size
176 scaledBounds.fLeft *= scale;
177 scaledBounds.fTop *= scale;
178 scaledBounds.fRight *= scale;
179 scaledBounds.fBottom *= scale;
jvanverthfa38a302014-10-06 05:59:05 -0700180 // move the origin to an integer boundary (gives better results)
181 SkScalar dx = SkScalarFraction(scaledBounds.fLeft);
182 SkScalar dy = SkScalarFraction(scaledBounds.fTop);
183 scaledBounds.offset(-dx, -dy);
184 // get integer boundary
185 SkIRect devPathBounds;
186 scaledBounds.roundOut(&devPathBounds);
187 // pad to allow room for antialiasing
188 devPathBounds.outset(SkScalarCeilToInt(kAntiAliasPad), SkScalarCeilToInt(kAntiAliasPad));
189 // move origin to upper left corner
190 devPathBounds.offsetTo(0,0);
191
192 // draw path to bitmap
193 SkMatrix drawMatrix;
194 drawMatrix.setTranslate(-bounds.left(), -bounds.top());
jvanverthb61283f2014-10-30 05:57:21 -0700195 drawMatrix.postScale(scale, scale);
jvanverthfa38a302014-10-06 05:59:05 -0700196 drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad);
197 GrSWMaskHelper helper(fContext);
198
199 if (!helper.init(devPathBounds, &drawMatrix)) {
200 return NULL;
201 }
202 helper.draw(path, stroke, SkRegion::kReplace_Op, antiAlias, 0xFF);
203
204 // generate signed distance field
205 devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad);
206 int width = devPathBounds.width();
207 int height = devPathBounds.height();
208 SkAutoSMalloc<1024> dfStorage(width*height*sizeof(unsigned char));
209 helper.toSDF((unsigned char*) dfStorage.get());
210
211 // add to atlas
212 SkIPoint16 atlasLocation;
213 GrPlot* plot = fAtlas->addToAtlas(&fPlotUsage, width, height, dfStorage.get(),
214 &atlasLocation);
215
216 // if atlas full
217 if (NULL == plot) {
218 if (this->freeUnusedPlot()) {
219 plot = fAtlas->addToAtlas(&fPlotUsage, width, height, dfStorage.get(),
220 &atlasLocation);
221 if (plot) {
222 goto HAS_ATLAS;
223 }
224 }
225
226 if (c_DumpPathCache) {
227#ifdef SK_DEVELOPER
228 GrTexture* texture = fAtlas->getTexture();
229 texture->surfacePriv().savePixels("pathcache.png");
230#endif
231 }
232
233 // before we purge the cache, we must flush any accumulated draws
234 fContext->flush();
235
236 if (this->freeUnusedPlot()) {
237 plot = fAtlas->addToAtlas(&fPlotUsage, width, height, dfStorage.get(),
238 &atlasLocation);
239 if (plot) {
240 goto HAS_ATLAS;
241 }
242 }
243
244 return NULL;
245 }
246
247HAS_ATLAS:
248 // add to cache
249 PathData* pathData = SkNEW(PathData);
jvanverthb61283f2014-10-30 05:57:21 -0700250 pathData->fKey.fGenID = path.getGenerationID();
251 pathData->fKey.fDimension = dimension;
252 pathData->fScale = scale;
jvanverthfa38a302014-10-06 05:59:05 -0700253 pathData->fPlot = plot;
254 // change the scaled rect to match the size of the inset distance field
255 scaledBounds.fRight = scaledBounds.fLeft +
256 SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset);
257 scaledBounds.fBottom = scaledBounds.fTop +
258 SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset);
259 // shift the origin to the correct place relative to the distance field
260 // need to also restore the fractional translation
261 scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dx,
262 -SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dy);
263 pathData->fBounds = scaledBounds;
264 // origin we render from is inset from distance field edge
265 atlasLocation.fX += SK_DistanceFieldInset;
266 atlasLocation.fY += SK_DistanceFieldInset;
267 pathData->fAtlasLocation = atlasLocation;
268
269 fPathCache.add(pathData);
270 fPathList.addToTail(pathData);
jvanverthb3eb6872014-10-24 07:12:51 -0700271#ifdef DF_PATH_TRACKING
272 ++g_NumCachedPaths;
273#endif
274
jvanverthfa38a302014-10-06 05:59:05 -0700275 return pathData;
276}
277
278bool GrAADistanceFieldPathRenderer::freeUnusedPlot() {
279 // find an unused plot
280 GrPlot* plot = fAtlas->getUnusedPlot();
281 if (NULL == plot) {
282 return false;
283 }
284 plot->resetRects();
285
286 // remove any paths that use this plot
287 PathDataList::Iter iter;
288 iter.init(fPathList, PathDataList::Iter::kHead_IterStart);
289 PathData* pathData;
290 while ((pathData = iter.get())) {
291 iter.next();
292 if (plot == pathData->fPlot) {
jvanverthb61283f2014-10-30 05:57:21 -0700293 fPathCache.remove(pathData->fKey);
jvanverthfa38a302014-10-06 05:59:05 -0700294 fPathList.remove(pathData);
295 SkDELETE(pathData);
jvanverthb3eb6872014-10-24 07:12:51 -0700296#ifdef DF_PATH_TRACKING
297 ++g_NumFreedPaths;
298#endif
jvanverthfa38a302014-10-06 05:59:05 -0700299 }
300 }
301
302 // tell the atlas to free the plot
303 GrAtlas::RemovePlot(&fPlotUsage, plot);
304
305 return true;
306}
307
joshualitt9853cce2014-11-17 14:22:48 -0800308bool GrAADistanceFieldPathRenderer::internalDrawPath(GrDrawTarget* target,
309 GrDrawState* drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800310 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800311 const SkPath& path,
312 const PathData* pathData) {
jvanverthfa38a302014-10-06 05:59:05 -0700313 GrTexture* texture = fAtlas->getTexture();
jvanverthf19657f2014-10-07 08:04:58 -0700314 GrDrawState::AutoRestoreEffects are(drawState);
jvanverthfa38a302014-10-06 05:59:05 -0700315
316 SkASSERT(pathData->fPlot);
317 GrDrawTarget::DrawToken drawToken = target->getCurrentDrawToken();
318 pathData->fPlot->setDrawToken(drawToken);
319
joshualitt2dd1ae02014-12-03 06:24:10 -0800320 // set up any flags
321 uint32_t flags = 0;
322 const SkMatrix& vm = drawState->getViewMatrix();
323 flags |= vm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
324
325 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
joshualitt8c0f6152014-12-10 14:12:22 -0800326 if (flags != fEffectFlags || fCachedGeometryProcessor->getColor() != color) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800327 fCachedGeometryProcessor.reset(GrDistanceFieldNoGammaTextureEffect::Create(color,
328 texture,
joshualitt2dd1ae02014-12-03 06:24:10 -0800329 params,
330 flags));
331 fEffectFlags = flags;
332 }
joshualitt8c0f6152014-12-10 14:12:22 -0800333 drawState->setGeometryProcessor(fCachedGeometryProcessor.get());
joshualitt2dd1ae02014-12-03 06:24:10 -0800334
jvanverthfa38a302014-10-06 05:59:05 -0700335 void* vertices = NULL;
joshualitt2dd1ae02014-12-03 06:24:10 -0800336 bool success = target->reserveVertexAndIndexSpace(4,
337 fCachedGeometryProcessor->getVertexStride(),
338 0, &vertices, NULL);
339 SkASSERT(fCachedGeometryProcessor->getVertexStride() == 2 * sizeof(SkPoint));
jvanverthfa38a302014-10-06 05:59:05 -0700340 GrAlwaysAssert(success);
341
342 SkScalar dx = pathData->fBounds.fLeft;
343 SkScalar dy = pathData->fBounds.fTop;
344 SkScalar width = pathData->fBounds.width();
345 SkScalar height = pathData->fBounds.height();
346
jvanverthb61283f2014-10-30 05:57:21 -0700347 SkScalar invScale = 1.0f/pathData->fScale;
jvanverthfa38a302014-10-06 05:59:05 -0700348 dx *= invScale;
349 dy *= invScale;
350 width *= invScale;
351 height *= invScale;
352
353 SkFixed tx = SkIntToFixed(pathData->fAtlasLocation.fX);
354 SkFixed ty = SkIntToFixed(pathData->fAtlasLocation.fY);
355 SkFixed tw = SkScalarToFixed(pathData->fBounds.width());
356 SkFixed th = SkScalarToFixed(pathData->fBounds.height());
357
358 // vertex positions
359 SkRect r = SkRect::MakeXYWH(dx, dy, width, height);
360 size_t vertSize = 2 * sizeof(SkPoint);
361 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
362 positions->setRectFan(r.left(), r.top(), r.right(), r.bottom(), vertSize);
363
364 // vertex texture coords
365 intptr_t intPtr = reinterpret_cast<intptr_t>(positions);
366 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(intPtr + vertSize - sizeof(SkPoint));
367 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
368 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
369 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
370 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
371 vertSize);
372
jvanverthfa38a302014-10-06 05:59:05 -0700373 vm.mapRect(&r);
jvanverth6d22eca2014-10-28 11:10:48 -0700374 target->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
joshualitt8c0f6152014-12-10 14:12:22 -0800375 target->drawIndexedInstances(drawState, kTriangles_GrPrimitiveType, 1, 4, 6, &r);
jvanverthfa38a302014-10-06 05:59:05 -0700376 target->resetVertexSource();
jvanverthfa38a302014-10-06 05:59:05 -0700377
378 return true;
379}
380