jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 1 | |
| 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 |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 23 | #define ATLAS_TEXTURE_HEIGHT 2048 |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 24 | #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 | |
| 30 | SK_CONF_DECLARE(bool, c_DumpPathCache, "gpu.dumpPathCache", false, |
| 31 | "Dump the contents of the path cache before every purge."); |
| 32 | |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 33 | #ifdef DF_PATH_TRACKING |
| 34 | static int g_NumCachedPaths = 0; |
| 35 | static int g_NumFreedPaths = 0; |
| 36 | #endif |
| 37 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 38 | // mip levels |
| 39 | static const int kSmallMIP = 32; |
jvanverth | ada68ef | 2014-11-03 14:00:24 -0800 | [diff] [blame] | 40 | static const int kMediumMIP = 78; |
| 41 | static const int kLargeMIP = 192; |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 42 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 43 | //////////////////////////////////////////////////////////////////////////////// |
jvanverth | 6d22eca | 2014-10-28 11:10:48 -0700 | [diff] [blame] | 44 | GrAADistanceFieldPathRenderer::GrAADistanceFieldPathRenderer(GrContext* context) |
| 45 | : fContext(context) |
| 46 | , fAtlas(NULL) |
| 47 | , fEffectFlags(kInvalid_DistanceFieldEffectFlag) { |
| 48 | } |
| 49 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 50 | GrAADistanceFieldPathRenderer::~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); |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 61 | |
| 62 | #ifdef DF_PATH_TRACKING |
| 63 | SkDebugf("Cached paths: %d, freed paths: %d\n", g_NumCachedPaths, g_NumFreedPaths); |
| 64 | #endif |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | //////////////////////////////////////////////////////////////////////////////// |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 68 | bool GrAADistanceFieldPathRenderer::canDrawPath(const GrDrawTarget* target, |
| 69 | const GrDrawState* drawState, |
| 70 | const SkPath& path, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 71 | const SkStrokeRec& stroke, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 72 | bool antiAlias) const { |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 73 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 74 | // TODO: Support inverse fill |
| 75 | // TODO: Support strokes |
| 76 | if (!target->caps()->shaderDerivativeSupport() || !antiAlias || path.isInverseFillType() |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 77 | || path.isVolatile() || SkStrokeRec::kFill_Style != stroke.getStyle()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 81 | // currently don't support perspective |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 82 | const SkMatrix& vm = drawState->getViewMatrix(); |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 83 | if (vm.hasPerspective()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 87 | // 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(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 90 | const SkRect& bounds = path.getBounds(); |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 91 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
| 92 | return maxDim < 64.f && maxDim*maxScale < 256.f; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 96 | GrPathRenderer::StencilSupport |
| 97 | GrAADistanceFieldPathRenderer::onGetStencilSupport(const GrDrawTarget*, |
| 98 | const GrDrawState*, |
| 99 | const SkPath&, |
| 100 | const SkStrokeRec&) const { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 101 | return GrPathRenderer::kNoSupport_StencilSupport; |
| 102 | } |
| 103 | |
| 104 | //////////////////////////////////////////////////////////////////////////////// |
| 105 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 106 | bool GrAADistanceFieldPathRenderer::onDrawPath(GrDrawTarget* target, |
| 107 | GrDrawState* drawState, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 108 | GrColor color, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 109 | const SkPath& path, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 110 | const SkStrokeRec& stroke, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 111 | 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 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 119 | // get mip level |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 120 | const SkMatrix& vm = drawState->getViewMatrix(); |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 121 | 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 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 134 | // check to see if path is cached |
| 135 | // TODO: handle stroked vs. filled version of same path |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 136 | PathData::Key key = { path.getGenerationID(), desiredDimension }; |
| 137 | PathData* pathData = fPathCache.find(key); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 138 | if (NULL == pathData) { |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 139 | SkScalar scale = desiredDimension/maxDim; |
| 140 | pathData = this->addPathToAtlas(path, stroke, antiAlias, desiredDimension, scale); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 141 | if (NULL == pathData) { |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // use signed distance field to render |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 147 | return this->internalDrawPath(target, drawState, color, path, pathData); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 148 | } |
| 149 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 150 | // padding around path bounds to allow for antialiased pixels |
| 151 | const SkScalar kAntiAliasPad = 1.0f; |
| 152 | |
| 153 | GrAADistanceFieldPathRenderer::PathData* GrAADistanceFieldPathRenderer::addPathToAtlas( |
| 154 | const SkPath& path, |
| 155 | const SkStrokeRec& stroke, |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 156 | bool antiAlias, |
| 157 | uint32_t dimension, |
| 158 | SkScalar scale) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 159 | |
| 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, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 164 | kNone_GrSurfaceFlags, textureSize, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 165 | 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; |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 175 | // scale to mip level size |
| 176 | scaledBounds.fLeft *= scale; |
| 177 | scaledBounds.fTop *= scale; |
| 178 | scaledBounds.fRight *= scale; |
| 179 | scaledBounds.fBottom *= scale; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 180 | // 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()); |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 195 | drawMatrix.postScale(scale, scale); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 196 | 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 | |
| 247 | HAS_ATLAS: |
| 248 | // add to cache |
| 249 | PathData* pathData = SkNEW(PathData); |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 250 | pathData->fKey.fGenID = path.getGenerationID(); |
| 251 | pathData->fKey.fDimension = dimension; |
| 252 | pathData->fScale = scale; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 253 | 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); |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 271 | #ifdef DF_PATH_TRACKING |
| 272 | ++g_NumCachedPaths; |
| 273 | #endif |
| 274 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 275 | return pathData; |
| 276 | } |
| 277 | |
| 278 | bool 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) { |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 293 | fPathCache.remove(pathData->fKey); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 294 | fPathList.remove(pathData); |
| 295 | SkDELETE(pathData); |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 296 | #ifdef DF_PATH_TRACKING |
| 297 | ++g_NumFreedPaths; |
| 298 | #endif |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | // tell the atlas to free the plot |
| 303 | GrAtlas::RemovePlot(&fPlotUsage, plot); |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 308 | bool GrAADistanceFieldPathRenderer::internalDrawPath(GrDrawTarget* target, |
| 309 | GrDrawState* drawState, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 310 | GrColor color, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 311 | const SkPath& path, |
| 312 | const PathData* pathData) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 313 | GrTexture* texture = fAtlas->getTexture(); |
jvanverth | f19657f | 2014-10-07 08:04:58 -0700 | [diff] [blame] | 314 | GrDrawState::AutoRestoreEffects are(drawState); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 315 | |
| 316 | SkASSERT(pathData->fPlot); |
| 317 | GrDrawTarget::DrawToken drawToken = target->getCurrentDrawToken(); |
| 318 | pathData->fPlot->setDrawToken(drawToken); |
| 319 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 320 | // 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); |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 326 | if (flags != fEffectFlags || fCachedGeometryProcessor->getColor() != color) { |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 327 | fCachedGeometryProcessor.reset(GrDistanceFieldNoGammaTextureEffect::Create(color, |
| 328 | texture, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 329 | params, |
| 330 | flags)); |
| 331 | fEffectFlags = flags; |
| 332 | } |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 333 | drawState->setGeometryProcessor(fCachedGeometryProcessor.get()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 334 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 335 | void* vertices = NULL; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 336 | bool success = target->reserveVertexAndIndexSpace(4, |
| 337 | fCachedGeometryProcessor->getVertexStride(), |
| 338 | 0, &vertices, NULL); |
| 339 | SkASSERT(fCachedGeometryProcessor->getVertexStride() == 2 * sizeof(SkPoint)); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 340 | 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 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 347 | SkScalar invScale = 1.0f/pathData->fScale; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 348 | 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 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 373 | vm.mapRect(&r); |
jvanverth | 6d22eca | 2014-10-28 11:10:48 -0700 | [diff] [blame] | 374 | target->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); |
joshualitt | 8c0f615 | 2014-12-10 14:12:22 -0800 | [diff] [blame] | 375 | target->drawIndexedInstances(drawState, kTriangles_GrPrimitiveType, 1, 4, 6, &r); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 376 | target->resetVertexSource(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 377 | |
| 378 | return true; |
| 379 | } |
| 380 | |