blob: 6ac504a9489b2c52fa182397277bb51b88545a9d [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,
108 const SkPath& path,
jvanverthfa38a302014-10-06 05:59:05 -0700109 const SkStrokeRec& stroke,
jvanverthfa38a302014-10-06 05:59:05 -0700110 bool antiAlias) {
111 // we've already bailed on inverse filled paths, so this is safe
112 if (path.isEmpty()) {
113 return true;
114 }
115
116 SkASSERT(fContext);
117
jvanverthb61283f2014-10-30 05:57:21 -0700118 // get mip level
joshualitt9853cce2014-11-17 14:22:48 -0800119 const SkMatrix& vm = drawState->getViewMatrix();
jvanverthb61283f2014-10-30 05:57:21 -0700120 SkScalar maxScale = vm.getMaxScale();
121 const SkRect& bounds = path.getBounds();
122 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
123 SkScalar size = maxScale*maxDim;
124 uint32_t desiredDimension;
125 if (size <= kSmallMIP) {
126 desiredDimension = kSmallMIP;
127 } else if (size <= kMediumMIP) {
128 desiredDimension = kMediumMIP;
129 } else {
130 desiredDimension = kLargeMIP;
131 }
132
jvanverthfa38a302014-10-06 05:59:05 -0700133 // check to see if path is cached
134 // TODO: handle stroked vs. filled version of same path
jvanverthb61283f2014-10-30 05:57:21 -0700135 PathData::Key key = { path.getGenerationID(), desiredDimension };
136 PathData* pathData = fPathCache.find(key);
jvanverthfa38a302014-10-06 05:59:05 -0700137 if (NULL == pathData) {
jvanverthb61283f2014-10-30 05:57:21 -0700138 SkScalar scale = desiredDimension/maxDim;
139 pathData = this->addPathToAtlas(path, stroke, antiAlias, desiredDimension, scale);
jvanverthfa38a302014-10-06 05:59:05 -0700140 if (NULL == pathData) {
141 return false;
142 }
143 }
144
145 // use signed distance field to render
joshualitt9853cce2014-11-17 14:22:48 -0800146 return this->internalDrawPath(target, drawState, path, pathData);
jvanverthfa38a302014-10-06 05:59:05 -0700147}
148
jvanverthfa38a302014-10-06 05:59:05 -0700149// padding around path bounds to allow for antialiased pixels
150const SkScalar kAntiAliasPad = 1.0f;
151
152GrAADistanceFieldPathRenderer::PathData* GrAADistanceFieldPathRenderer::addPathToAtlas(
153 const SkPath& path,
154 const SkStrokeRec& stroke,
jvanverthb61283f2014-10-30 05:57:21 -0700155 bool antiAlias,
156 uint32_t dimension,
157 SkScalar scale) {
jvanverthfa38a302014-10-06 05:59:05 -0700158
159 // generate distance field and add to atlas
160 if (NULL == fAtlas) {
161 SkISize textureSize = SkISize::Make(ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT);
162 fAtlas = SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kAlpha_8_GrPixelConfig,
bsalomonf2703d82014-10-28 14:33:06 -0700163 kNone_GrSurfaceFlags, textureSize,
jvanverthfa38a302014-10-06 05:59:05 -0700164 NUM_PLOTS_X, NUM_PLOTS_Y, false));
165 if (NULL == fAtlas) {
166 return NULL;
167 }
168 }
169
170 const SkRect& bounds = path.getBounds();
171
172 // generate bounding rect for bitmap draw
173 SkRect scaledBounds = bounds;
jvanverthb61283f2014-10-30 05:57:21 -0700174 // scale to mip level size
175 scaledBounds.fLeft *= scale;
176 scaledBounds.fTop *= scale;
177 scaledBounds.fRight *= scale;
178 scaledBounds.fBottom *= scale;
jvanverthfa38a302014-10-06 05:59:05 -0700179 // move the origin to an integer boundary (gives better results)
180 SkScalar dx = SkScalarFraction(scaledBounds.fLeft);
181 SkScalar dy = SkScalarFraction(scaledBounds.fTop);
182 scaledBounds.offset(-dx, -dy);
183 // get integer boundary
184 SkIRect devPathBounds;
185 scaledBounds.roundOut(&devPathBounds);
186 // pad to allow room for antialiasing
187 devPathBounds.outset(SkScalarCeilToInt(kAntiAliasPad), SkScalarCeilToInt(kAntiAliasPad));
188 // move origin to upper left corner
189 devPathBounds.offsetTo(0,0);
190
191 // draw path to bitmap
192 SkMatrix drawMatrix;
193 drawMatrix.setTranslate(-bounds.left(), -bounds.top());
jvanverthb61283f2014-10-30 05:57:21 -0700194 drawMatrix.postScale(scale, scale);
jvanverthfa38a302014-10-06 05:59:05 -0700195 drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad);
196 GrSWMaskHelper helper(fContext);
197
198 if (!helper.init(devPathBounds, &drawMatrix)) {
199 return NULL;
200 }
201 helper.draw(path, stroke, SkRegion::kReplace_Op, antiAlias, 0xFF);
202
203 // generate signed distance field
204 devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad);
205 int width = devPathBounds.width();
206 int height = devPathBounds.height();
207 SkAutoSMalloc<1024> dfStorage(width*height*sizeof(unsigned char));
208 helper.toSDF((unsigned char*) dfStorage.get());
209
210 // add to atlas
211 SkIPoint16 atlasLocation;
212 GrPlot* plot = fAtlas->addToAtlas(&fPlotUsage, width, height, dfStorage.get(),
213 &atlasLocation);
214
215 // if atlas full
216 if (NULL == plot) {
217 if (this->freeUnusedPlot()) {
218 plot = fAtlas->addToAtlas(&fPlotUsage, width, height, dfStorage.get(),
219 &atlasLocation);
220 if (plot) {
221 goto HAS_ATLAS;
222 }
223 }
224
225 if (c_DumpPathCache) {
226#ifdef SK_DEVELOPER
227 GrTexture* texture = fAtlas->getTexture();
228 texture->surfacePriv().savePixels("pathcache.png");
229#endif
230 }
231
232 // before we purge the cache, we must flush any accumulated draws
233 fContext->flush();
234
235 if (this->freeUnusedPlot()) {
236 plot = fAtlas->addToAtlas(&fPlotUsage, width, height, dfStorage.get(),
237 &atlasLocation);
238 if (plot) {
239 goto HAS_ATLAS;
240 }
241 }
242
243 return NULL;
244 }
245
246HAS_ATLAS:
247 // add to cache
248 PathData* pathData = SkNEW(PathData);
jvanverthb61283f2014-10-30 05:57:21 -0700249 pathData->fKey.fGenID = path.getGenerationID();
250 pathData->fKey.fDimension = dimension;
251 pathData->fScale = scale;
jvanverthfa38a302014-10-06 05:59:05 -0700252 pathData->fPlot = plot;
253 // change the scaled rect to match the size of the inset distance field
254 scaledBounds.fRight = scaledBounds.fLeft +
255 SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset);
256 scaledBounds.fBottom = scaledBounds.fTop +
257 SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset);
258 // shift the origin to the correct place relative to the distance field
259 // need to also restore the fractional translation
260 scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dx,
261 -SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dy);
262 pathData->fBounds = scaledBounds;
263 // origin we render from is inset from distance field edge
264 atlasLocation.fX += SK_DistanceFieldInset;
265 atlasLocation.fY += SK_DistanceFieldInset;
266 pathData->fAtlasLocation = atlasLocation;
267
268 fPathCache.add(pathData);
269 fPathList.addToTail(pathData);
jvanverthb3eb6872014-10-24 07:12:51 -0700270#ifdef DF_PATH_TRACKING
271 ++g_NumCachedPaths;
272#endif
273
jvanverthfa38a302014-10-06 05:59:05 -0700274 return pathData;
275}
276
277bool GrAADistanceFieldPathRenderer::freeUnusedPlot() {
278 // find an unused plot
279 GrPlot* plot = fAtlas->getUnusedPlot();
280 if (NULL == plot) {
281 return false;
282 }
283 plot->resetRects();
284
285 // remove any paths that use this plot
286 PathDataList::Iter iter;
287 iter.init(fPathList, PathDataList::Iter::kHead_IterStart);
288 PathData* pathData;
289 while ((pathData = iter.get())) {
290 iter.next();
291 if (plot == pathData->fPlot) {
jvanverthb61283f2014-10-30 05:57:21 -0700292 fPathCache.remove(pathData->fKey);
jvanverthfa38a302014-10-06 05:59:05 -0700293 fPathList.remove(pathData);
294 SkDELETE(pathData);
jvanverthb3eb6872014-10-24 07:12:51 -0700295#ifdef DF_PATH_TRACKING
296 ++g_NumFreedPaths;
297#endif
jvanverthfa38a302014-10-06 05:59:05 -0700298 }
299 }
300
301 // tell the atlas to free the plot
302 GrAtlas::RemovePlot(&fPlotUsage, plot);
303
304 return true;
305}
306
joshualitt9853cce2014-11-17 14:22:48 -0800307bool GrAADistanceFieldPathRenderer::internalDrawPath(GrDrawTarget* target,
308 GrDrawState* drawState,
309 const SkPath& path,
310 const PathData* pathData) {
jvanverthfa38a302014-10-06 05:59:05 -0700311 GrTexture* texture = fAtlas->getTexture();
jvanverthf19657f2014-10-07 08:04:58 -0700312 GrDrawState::AutoRestoreEffects are(drawState);
jvanverthfa38a302014-10-06 05:59:05 -0700313
314 SkASSERT(pathData->fPlot);
315 GrDrawTarget::DrawToken drawToken = target->getCurrentDrawToken();
316 pathData->fPlot->setDrawToken(drawToken);
317
joshualitt2dd1ae02014-12-03 06:24:10 -0800318 // set up any flags
319 uint32_t flags = 0;
320 const SkMatrix& vm = drawState->getViewMatrix();
321 flags |= vm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
322
323 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
324 if (flags != fEffectFlags) {
325 fCachedGeometryProcessor.reset(GrDistanceFieldNoGammaTextureEffect::Create(texture,
326 params,
327 flags));
328 fEffectFlags = flags;
329 }
330 drawState->setGeometryProcessor(fCachedGeometryProcessor.get());
331
jvanverthfa38a302014-10-06 05:59:05 -0700332 void* vertices = NULL;
joshualitt2dd1ae02014-12-03 06:24:10 -0800333 bool success = target->reserveVertexAndIndexSpace(4,
334 fCachedGeometryProcessor->getVertexStride(),
335 0, &vertices, NULL);
336 SkASSERT(fCachedGeometryProcessor->getVertexStride() == 2 * sizeof(SkPoint));
jvanverthfa38a302014-10-06 05:59:05 -0700337 GrAlwaysAssert(success);
338
339 SkScalar dx = pathData->fBounds.fLeft;
340 SkScalar dy = pathData->fBounds.fTop;
341 SkScalar width = pathData->fBounds.width();
342 SkScalar height = pathData->fBounds.height();
343
jvanverthb61283f2014-10-30 05:57:21 -0700344 SkScalar invScale = 1.0f/pathData->fScale;
jvanverthfa38a302014-10-06 05:59:05 -0700345 dx *= invScale;
346 dy *= invScale;
347 width *= invScale;
348 height *= invScale;
349
350 SkFixed tx = SkIntToFixed(pathData->fAtlasLocation.fX);
351 SkFixed ty = SkIntToFixed(pathData->fAtlasLocation.fY);
352 SkFixed tw = SkScalarToFixed(pathData->fBounds.width());
353 SkFixed th = SkScalarToFixed(pathData->fBounds.height());
354
355 // vertex positions
356 SkRect r = SkRect::MakeXYWH(dx, dy, width, height);
357 size_t vertSize = 2 * sizeof(SkPoint);
358 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
359 positions->setRectFan(r.left(), r.top(), r.right(), r.bottom(), vertSize);
360
361 // vertex texture coords
362 intptr_t intPtr = reinterpret_cast<intptr_t>(positions);
363 SkPoint* textureCoords = reinterpret_cast<SkPoint*>(intPtr + vertSize - sizeof(SkPoint));
364 textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)),
365 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)),
366 SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)),
367 SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)),
368 vertSize);
369
jvanverthfa38a302014-10-06 05:59:05 -0700370 vm.mapRect(&r);
jvanverth6d22eca2014-10-28 11:10:48 -0700371 target->setIndexSourceToBuffer(fContext->getQuadIndexBuffer());
joshualitt9853cce2014-11-17 14:22:48 -0800372 target->drawIndexedInstances(drawState, kTriangles_GrPrimitiveType, 1, 4, 6, &r);
jvanverthfa38a302014-10-06 05:59:05 -0700373 target->resetVertexSource();
jvanverthfa38a302014-10-06 05:59:05 -0700374
375 return true;
376}
377