jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 3 | * Copyright 2017 ARM Ltd. |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 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 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 9 | #include "GrSmallPathRenderer.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 10 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 11 | #include "GrBuffer.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 12 | #include "GrContext.h" |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 13 | #include "GrDistanceFieldGenFromVector.h" |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 14 | #include "GrDrawOpTest.h" |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 15 | #include "GrResourceProvider.h" |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 16 | #include "GrSimpleMeshDrawOpHelper.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 17 | #include "SkAutoMalloc.h" |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 18 | #include "SkAutoPixmapStorage.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 19 | #include "SkDistanceFieldGen.h" |
Brian Osman | c7da146 | 2017-08-17 16:14:25 -0400 | [diff] [blame] | 20 | #include "SkRasterClip.h" |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 21 | #include "effects/GrBitmapTextGeoProc.h" |
| 22 | #include "effects/GrDistanceFieldGeoProc.h" |
| 23 | #include "ops/GrMeshDrawOp.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 24 | |
jvanverth | fb1e2fc | 2015-09-15 13:11:11 -0700 | [diff] [blame] | 25 | #define ATLAS_TEXTURE_WIDTH 2048 |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 26 | #define ATLAS_TEXTURE_HEIGHT 2048 |
jvanverth | fb1e2fc | 2015-09-15 13:11:11 -0700 | [diff] [blame] | 27 | #define PLOT_WIDTH 512 |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 28 | #define PLOT_HEIGHT 256 |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 29 | |
| 30 | #define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH) |
| 31 | #define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT) |
| 32 | |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 33 | #ifdef DF_PATH_TRACKING |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 34 | static int g_NumCachedShapes = 0; |
| 35 | static int g_NumFreedShapes = 0; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 38 | // mip levels |
Jim Van Verth | 25b8ca1 | 2017-02-17 14:02:13 -0500 | [diff] [blame] | 39 | static const SkScalar kIdealMinMIP = 12; |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 40 | static const SkScalar kMaxMIP = 162; |
| 41 | |
| 42 | static const SkScalar kMaxDim = 73; |
Jim Van Verth | 25b8ca1 | 2017-02-17 14:02:13 -0500 | [diff] [blame] | 43 | static const SkScalar kMinSize = SK_ScalarHalf; |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 44 | static const SkScalar kMaxSize = 2*kMaxMIP; |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 45 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 46 | // Callback to clear out internal path cache when eviction occurs |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 47 | void GrSmallPathRenderer::HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) { |
| 48 | GrSmallPathRenderer* dfpr = (GrSmallPathRenderer*)pr; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 49 | // remove any paths that use this plot |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 50 | ShapeDataList::Iter iter; |
| 51 | iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 52 | ShapeData* shapeData; |
| 53 | while ((shapeData = iter.get())) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 54 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 55 | if (id == shapeData->fID) { |
| 56 | dfpr->fShapeCache.remove(shapeData->fKey); |
| 57 | dfpr->fShapeList.remove(shapeData); |
| 58 | delete shapeData; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 59 | #ifdef DF_PATH_TRACKING |
| 60 | ++g_NumFreedPaths; |
| 61 | #endif |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 66 | //////////////////////////////////////////////////////////////////////////////// |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 67 | GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {} |
jvanverth | 6d22eca | 2014-10-28 11:10:48 -0700 | [diff] [blame] | 68 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 69 | GrSmallPathRenderer::~GrSmallPathRenderer() { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 70 | ShapeDataList::Iter iter; |
| 71 | iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 72 | ShapeData* shapeData; |
| 73 | while ((shapeData = iter.get())) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 74 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 75 | delete shapeData; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 76 | } |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 77 | |
| 78 | #ifdef DF_PATH_TRACKING |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 79 | SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes); |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 80 | #endif |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | //////////////////////////////////////////////////////////////////////////////// |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 84 | GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 85 | if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 86 | return CanDrawPath::kNo; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 87 | } |
| 88 | // If the shape has no key then we won't get any reuse. |
| 89 | if (!args.fShape->hasUnstyledKey()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 90 | return CanDrawPath::kNo; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 91 | } |
| 92 | // This only supports filled paths, however, the caller may apply the style to make a filled |
| 93 | // path and try again. |
| 94 | if (!args.fShape->style().isSimpleFill()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 95 | return CanDrawPath::kNo; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 96 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 97 | // This does non-inverse coverage-based antialiased fills. |
| 98 | if (GrAAType::kCoverage != args.fAAType) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 99 | return CanDrawPath::kNo; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 100 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 101 | // TODO: Support inverse fill |
bsalomon | db7979a | 2016-06-27 11:08:43 -0700 | [diff] [blame] | 102 | if (args.fShape->inverseFilled()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 103 | return CanDrawPath::kNo; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 104 | } |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 105 | // currently don't support perspective |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 106 | if (args.fViewMatrix->hasPerspective()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 107 | return CanDrawPath::kNo; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 108 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 109 | |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 110 | // Only support paths with bounds within kMaxDim by kMaxDim, |
| 111 | // scaled to have bounds within kMaxSize by kMaxSize. |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 112 | // The goal is to accelerate rendering of lots of small paths that may be scaling. |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 113 | SkScalar scaleFactors[2]; |
| 114 | if (!args.fViewMatrix->getMinMaxScales(scaleFactors)) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 115 | return CanDrawPath::kNo; |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 116 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 117 | SkRect bounds = args.fShape->styledBounds(); |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 118 | SkScalar minDim = SkMinScalar(bounds.width(), bounds.height()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 119 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
Jim Van Verth | d25cc9b | 2017-02-16 10:01:46 -0500 | [diff] [blame] | 120 | SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]); |
| 121 | SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]); |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 122 | if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) { |
| 123 | return CanDrawPath::kNo; |
| 124 | } |
bsalomon | 6266dca | 2016-03-11 06:22:00 -0800 | [diff] [blame] | 125 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 126 | return CanDrawPath::kYes; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 127 | } |
| 128 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 129 | //////////////////////////////////////////////////////////////////////////////// |
| 130 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 131 | // padding around path bounds to allow for antialiased pixels |
| 132 | static const SkScalar kAntiAliasPad = 1.0f; |
| 133 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 134 | class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp { |
| 135 | private: |
| 136 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 137 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 138 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 139 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 140 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 141 | using ShapeData = GrSmallPathRenderer::ShapeData; |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 142 | using ShapeCache = SkTDynamicHash<ShapeData, ShapeData::Key>; |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 143 | using ShapeDataList = GrSmallPathRenderer::ShapeDataList; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 144 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 145 | static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const GrShape& shape, |
| 146 | const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, |
| 147 | ShapeCache* shapeCache, ShapeDataList* shapeList, |
| 148 | bool gammaCorrect, |
| 149 | const GrUserStencilSettings* stencilSettings) { |
| 150 | return Helper::FactoryHelper<SmallPathOp>(std::move(paint), shape, viewMatrix, atlas, |
| 151 | shapeCache, shapeList, gammaCorrect, |
| 152 | stencilSettings); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 153 | } |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 154 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 155 | SmallPathOp(Helper::MakeArgs helperArgs, GrColor color, const GrShape& shape, |
| 156 | const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache, |
| 157 | ShapeDataList* shapeList, bool gammaCorrect, |
| 158 | const GrUserStencilSettings* stencilSettings) |
| 159 | : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 160 | SkASSERT(shape.hasUnstyledKey()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 161 | // Compute bounds |
| 162 | this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo); |
| 163 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 164 | #if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 165 | fUsesDistanceField = true; |
| 166 | #else |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 167 | // only use distance fields on desktop and Android framework to save space in the atlas |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 168 | fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP; |
| 169 | #endif |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 170 | fViewMatrix = viewMatrix; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 171 | SkVector translate = SkVector::Make(0, 0); |
| 172 | if (!fUsesDistanceField) { |
| 173 | // In this case we don't apply a view matrix, so we need to remove the non-subpixel |
| 174 | // translation and add it back when we generate the quad for the path |
| 175 | SkScalar translateX = viewMatrix.getTranslateX(); |
| 176 | SkScalar translateY = viewMatrix.getTranslateY(); |
| 177 | translate = SkVector::Make(SkScalarFloorToScalar(translateX), |
| 178 | SkScalarFloorToScalar(translateY)); |
| 179 | // Only store the fractional part of the translation in the view matrix |
| 180 | fViewMatrix.setTranslateX(translateX - translate.fX); |
| 181 | fViewMatrix.setTranslateY(translateY - translate.fY); |
| 182 | } |
| 183 | |
| 184 | fShapes.emplace_back(Entry{color, shape, translate}); |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 185 | |
| 186 | fAtlas = atlas; |
| 187 | fShapeCache = shapeCache; |
| 188 | fShapeList = shapeList; |
| 189 | fGammaCorrect = gammaCorrect; |
| 190 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 193 | const char* name() const override { return "SmallPathOp"; } |
| 194 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 195 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 196 | fHelper.visitProxies(func); |
| 197 | |
| 198 | const sk_sp<GrTextureProxy>* proxies = fAtlas->getProxies(); |
| 199 | for (int i = 0; i < GrDrawOpAtlas::kMaxPages; ++i) { |
| 200 | if (proxies[i].get()) { |
| 201 | func(proxies[i].get()); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 206 | SkString dumpInfo() const override { |
| 207 | SkString string; |
| 208 | for (const auto& geo : fShapes) { |
| 209 | string.appendf("Color: 0x%08x\n", geo.fColor); |
| 210 | } |
| 211 | string += fHelper.dumpInfo(); |
| 212 | string += INHERITED::dumpInfo(); |
| 213 | return string; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 216 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 217 | |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 218 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 219 | GrPixelConfigIsClamped dstIsClamped) override { |
| 220 | return fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped, |
| 221 | GrProcessorAnalysisCoverage::kSingleChannel, |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 222 | &fShapes.front().fColor); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 225 | private: |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 226 | struct FlushInfo { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 227 | sk_sp<const GrBuffer> fVertexBuffer; |
| 228 | sk_sp<const GrBuffer> fIndexBuffer; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 229 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 230 | const GrPipeline* fPipeline; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 231 | int fVertexOffset; |
| 232 | int fInstancesToFlush; |
| 233 | }; |
| 234 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 235 | void onPrepareDraws(Target* target) override { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 236 | int instanceCount = fShapes.count(); |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 237 | const SkMatrix& ctm = this->viewMatrix(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 238 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 239 | FlushInfo flushInfo; |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 240 | flushInfo.fPipeline = fHelper.makePipeline(target); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 241 | // Setup GrGeometryProcessor |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 242 | GrDrawOpAtlas* atlas = fAtlas; |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 243 | uint32_t atlasPageCount = atlas->pageCount(); |
| 244 | if (!atlasPageCount) { |
| 245 | return; |
| 246 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 247 | if (fUsesDistanceField) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 248 | uint32_t flags = 0; |
| 249 | flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 250 | flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 251 | flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0; |
| 252 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 253 | flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make( |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 254 | this->color(), this->viewMatrix(), atlas->getProxies(), |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 255 | GrSamplerState::ClampBilerp(), flags, fHelper.usesLocalCoords()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 256 | } else { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 257 | SkMatrix invert; |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 258 | if (fHelper.usesLocalCoords()) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 259 | if (!this->viewMatrix().invert(&invert)) { |
| 260 | SkDebugf("Could not invert viewmatrix\n"); |
| 261 | return; |
| 262 | } |
| 263 | // for local coords, we need to add the translation back in that we removed |
| 264 | // from the stored view matrix |
| 265 | invert.preTranslate(-fShapes[0].fTranslate.fX, -fShapes[0].fTranslate.fY); |
| 266 | } |
| 267 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 268 | flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make( |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 269 | this->color(), atlas->getProxies(), GrSamplerState::ClampNearest(), |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 270 | kA8_GrMaskFormat, invert, fHelper.usesLocalCoords()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 271 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 272 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 273 | // allocate vertices |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 274 | size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride(); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 275 | SkASSERT(vertexStride == sizeof(SkPoint) + sizeof(GrColor) + 2*sizeof(uint16_t)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 276 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 277 | const GrBuffer* vertexBuffer; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 278 | void* vertices = target->makeVertexSpace(vertexStride, |
| 279 | kVerticesPerQuad * instanceCount, |
| 280 | &vertexBuffer, |
| 281 | &flushInfo.fVertexOffset); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 282 | flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 283 | flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer()); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 284 | if (!vertices || !flushInfo.fIndexBuffer) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 285 | SkDebugf("Could not allocate vertices\n"); |
| 286 | return; |
| 287 | } |
| 288 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 289 | flushInfo.fInstancesToFlush = 0; |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 290 | // Pointer to the next set of vertices to write. |
| 291 | intptr_t offset = reinterpret_cast<intptr_t>(vertices); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 292 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 293 | const Entry& args = fShapes[i]; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 294 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 295 | ShapeData* shapeData; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 296 | if (fUsesDistanceField) { |
| 297 | // get mip level |
Robert Phillips | 3cf781d | 2017-08-22 18:25:11 -0400 | [diff] [blame] | 298 | SkScalar maxScale = SkScalarAbs(this->viewMatrix().getMaxScale()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 299 | const SkRect& bounds = args.fShape.bounds(); |
| 300 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
| 301 | // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.) |
| 302 | // In the majority of cases this will yield a crisper rendering. |
| 303 | SkScalar mipScale = 1.0f; |
| 304 | // Our mipscale is the maxScale clamped to the next highest power of 2 |
| 305 | if (maxScale <= SK_ScalarHalf) { |
| 306 | SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale))); |
| 307 | mipScale = SkScalarPow(2, -log); |
| 308 | } else if (maxScale > SK_Scalar1) { |
| 309 | SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale)); |
| 310 | mipScale = SkScalarPow(2, log); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 311 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 312 | SkASSERT(maxScale <= mipScale); |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 313 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 314 | SkScalar mipSize = mipScale*SkScalarAbs(maxDim); |
| 315 | // For sizes less than kIdealMinMIP we want to use as large a distance field as we can |
| 316 | // so we can preserve as much detail as possible. However, we can't scale down more |
| 317 | // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize |
| 318 | // just bigger than the ideal, and then scale down until we are no more than 4x the |
| 319 | // original mipsize. |
| 320 | if (mipSize < kIdealMinMIP) { |
| 321 | SkScalar newMipSize = mipSize; |
| 322 | do { |
| 323 | newMipSize *= 2; |
| 324 | } while (newMipSize < kIdealMinMIP); |
| 325 | while (newMipSize > 4 * mipSize) { |
| 326 | newMipSize *= 0.25f; |
| 327 | } |
| 328 | mipSize = newMipSize; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 329 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 330 | SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP); |
Jim Van Verth | c0bc1bb | 2017-02-27 18:21:16 -0500 | [diff] [blame] | 331 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 332 | // check to see if df path is cached |
| 333 | ShapeData::Key key(args.fShape, SkScalarCeilToInt(desiredDimension)); |
| 334 | shapeData = fShapeCache->find(key); |
| 335 | if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) { |
| 336 | // Remove the stale cache entry |
| 337 | if (shapeData) { |
| 338 | fShapeCache->remove(shapeData->fKey); |
| 339 | fShapeList->remove(shapeData); |
| 340 | delete shapeData; |
| 341 | } |
| 342 | SkScalar scale = desiredDimension / maxDim; |
| 343 | |
| 344 | shapeData = new ShapeData; |
| 345 | if (!this->addDFPathToAtlas(target, |
| 346 | &flushInfo, |
| 347 | atlas, |
| 348 | shapeData, |
| 349 | args.fShape, |
| 350 | SkScalarCeilToInt(desiredDimension), |
| 351 | scale)) { |
| 352 | delete shapeData; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 353 | continue; |
| 354 | } |
Jim Van Verth | c0bc1bb | 2017-02-27 18:21:16 -0500 | [diff] [blame] | 355 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 356 | } else { |
| 357 | // check to see if bitmap path is cached |
| 358 | ShapeData::Key key(args.fShape, this->viewMatrix()); |
| 359 | shapeData = fShapeCache->find(key); |
| 360 | if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) { |
| 361 | // Remove the stale cache entry |
| 362 | if (shapeData) { |
| 363 | fShapeCache->remove(shapeData->fKey); |
| 364 | fShapeList->remove(shapeData); |
| 365 | delete shapeData; |
| 366 | } |
| 367 | |
| 368 | shapeData = new ShapeData; |
| 369 | if (!this->addBMPathToAtlas(target, |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 370 | &flushInfo, |
| 371 | atlas, |
| 372 | shapeData, |
| 373 | args.fShape, |
| 374 | this->viewMatrix())) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 375 | delete shapeData; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 376 | continue; |
| 377 | } |
| 378 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 379 | } |
| 380 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 381 | atlas->setLastUseToken(shapeData->fID, target->nextDrawToken()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 382 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 383 | this->writePathVertices(target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 384 | atlas, |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 385 | offset, |
| 386 | args.fColor, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 387 | vertexStride, |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 388 | args.fTranslate, |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 389 | shapeData); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 390 | offset += kVerticesPerQuad * vertexStride; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 391 | flushInfo.fInstancesToFlush++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 392 | } |
| 393 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 394 | this->flush(target, &flushInfo); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 397 | bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 398 | GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape, |
| 399 | uint32_t dimension, SkScalar scale) const { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 400 | const SkRect& bounds = shape.bounds(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 401 | |
| 402 | // generate bounding rect for bitmap draw |
| 403 | SkRect scaledBounds = bounds; |
| 404 | // scale to mip level size |
| 405 | scaledBounds.fLeft *= scale; |
| 406 | scaledBounds.fTop *= scale; |
| 407 | scaledBounds.fRight *= scale; |
| 408 | scaledBounds.fBottom *= scale; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 409 | // subtract out integer portion of origin |
| 410 | // (SDF created will be placed with fractional offset burnt in) |
Jim Van Verth | 07b6ad0 | 2016-12-20 10:23:09 -0500 | [diff] [blame] | 411 | SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft); |
| 412 | SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 413 | scaledBounds.offset(-dx, -dy); |
| 414 | // get integer boundary |
| 415 | SkIRect devPathBounds; |
| 416 | scaledBounds.roundOut(&devPathBounds); |
| 417 | // pad to allow room for antialiasing |
jvanverth | ecbed9d | 2015-12-18 10:07:52 -0800 | [diff] [blame] | 418 | const int intPad = SkScalarCeilToInt(kAntiAliasPad); |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 419 | // place devBounds at origin |
| 420 | int width = devPathBounds.width() + 2*intPad; |
| 421 | int height = devPathBounds.height() + 2*intPad; |
| 422 | devPathBounds = SkIRect::MakeWH(width, height); |
Robert Phillips | 3cf781d | 2017-08-22 18:25:11 -0400 | [diff] [blame] | 423 | SkScalar translateX = intPad - dx; |
| 424 | SkScalar translateY = intPad - dy; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 425 | |
| 426 | // draw path to bitmap |
| 427 | SkMatrix drawMatrix; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 428 | drawMatrix.setScale(scale, scale); |
Robert Phillips | 3cf781d | 2017-08-22 18:25:11 -0400 | [diff] [blame] | 429 | drawMatrix.postTranslate(translateX, translateY); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 430 | |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 431 | SkASSERT(devPathBounds.fLeft == 0); |
| 432 | SkASSERT(devPathBounds.fTop == 0); |
Jim Van Verth | 25b8ca1 | 2017-02-17 14:02:13 -0500 | [diff] [blame] | 433 | SkASSERT(devPathBounds.width() > 0); |
| 434 | SkASSERT(devPathBounds.height() > 0); |
bsalomon | f93f515 | 2016-10-26 08:00:00 -0700 | [diff] [blame] | 435 | |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 436 | // setup signed distance field storage |
| 437 | SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 438 | width = dfBounds.width(); |
| 439 | height = dfBounds.height(); |
rmistry | 4784225 | 2016-12-21 04:25:18 -0800 | [diff] [blame] | 440 | // TODO We should really generate this directly into the plot somehow |
| 441 | SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char)); |
joel.liang | 6d2f73c | 2016-12-20 18:58:53 -0800 | [diff] [blame] | 442 | |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 443 | SkPath path; |
| 444 | shape.asPath(&path); |
| 445 | #ifndef SK_USE_LEGACY_DISTANCE_FIELDS |
| 446 | // Generate signed distance field directly from SkPath |
| 447 | bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(), |
| 448 | path, drawMatrix, |
| 449 | width, height, width * sizeof(unsigned char)); |
| 450 | if (!succeed) { |
| 451 | #endif |
| 452 | // setup bitmap backing |
| 453 | SkAutoPixmapStorage dst; |
| 454 | if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), |
| 455 | devPathBounds.height()))) { |
| 456 | return false; |
| 457 | } |
Mike Reed | 98a6216 | 2017-09-26 12:47:08 -0400 | [diff] [blame^] | 458 | sk_bzero(dst.writable_addr(), dst.computeByteSize()); |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 459 | |
| 460 | // rasterize path |
| 461 | SkPaint paint; |
| 462 | paint.setStyle(SkPaint::kFill_Style); |
| 463 | paint.setAntiAlias(true); |
| 464 | |
| 465 | SkDraw draw; |
| 466 | sk_bzero(&draw, sizeof(draw)); |
| 467 | |
| 468 | SkRasterClip rasterClip; |
| 469 | rasterClip.setRect(devPathBounds); |
| 470 | draw.fRC = &rasterClip; |
| 471 | draw.fMatrix = &drawMatrix; |
| 472 | draw.fDst = dst; |
| 473 | |
| 474 | draw.drawPathCoverage(path, paint); |
| 475 | |
| 476 | // Generate signed distance field |
| 477 | SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(), |
| 478 | (const unsigned char*)dst.addr(), |
| 479 | dst.width(), dst.height(), dst.rowBytes()); |
| 480 | #ifndef SK_USE_LEGACY_DISTANCE_FIELDS |
| 481 | } |
| 482 | #endif |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 483 | |
| 484 | // add to atlas |
| 485 | SkIPoint16 atlasLocation; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 486 | GrDrawOpAtlas::AtlasID id; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 487 | if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 488 | this->flush(target, flushInfo); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 489 | if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) { |
| 490 | return false; |
| 491 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // add to cache |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 495 | shapeData->fKey.set(shape, dimension); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 496 | shapeData->fID = id; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 497 | |
Robert Phillips | 3cf781d | 2017-08-22 18:25:11 -0400 | [diff] [blame] | 498 | shapeData->fBounds = SkRect::Make(devPathBounds); |
| 499 | shapeData->fBounds.offset(-translateX, -translateY); |
| 500 | shapeData->fBounds.fLeft /= scale; |
| 501 | shapeData->fBounds.fTop /= scale; |
| 502 | shapeData->fBounds.fRight /= scale; |
| 503 | shapeData->fBounds.fBottom /= scale; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 504 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 505 | // We pack the 2bit page index in the low bit of the u and v texture coords |
| 506 | uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id); |
| 507 | SkASSERT(pageIndex < 4); |
| 508 | uint16_t uBit = (pageIndex >> 1) & 0x1; |
| 509 | uint16_t vBit = pageIndex & 0x1; |
| 510 | shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1 | uBit, |
| 511 | (atlasLocation.fY+SK_DistanceFieldPad) << 1 | vBit, |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 512 | (atlasLocation.fX+SK_DistanceFieldPad+ |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 513 | devPathBounds.width()) << 1 | uBit, |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 514 | (atlasLocation.fY+SK_DistanceFieldPad+ |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 515 | devPathBounds.height()) << 1 | vBit); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 516 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 517 | fShapeCache->add(shapeData); |
| 518 | fShapeList->addToTail(shapeData); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 519 | #ifdef DF_PATH_TRACKING |
| 520 | ++g_NumCachedPaths; |
| 521 | #endif |
| 522 | return true; |
| 523 | } |
| 524 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 525 | bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 526 | GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape, |
| 527 | const SkMatrix& ctm) const { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 528 | const SkRect& bounds = shape.bounds(); |
| 529 | if (bounds.isEmpty()) { |
| 530 | return false; |
| 531 | } |
| 532 | SkMatrix drawMatrix(ctm); |
| 533 | drawMatrix.set(SkMatrix::kMTransX, SkScalarFraction(ctm.get(SkMatrix::kMTransX))); |
| 534 | drawMatrix.set(SkMatrix::kMTransY, SkScalarFraction(ctm.get(SkMatrix::kMTransY))); |
| 535 | SkRect shapeDevBounds; |
| 536 | drawMatrix.mapRect(&shapeDevBounds, bounds); |
| 537 | SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft); |
| 538 | SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop); |
| 539 | |
| 540 | // get integer boundary |
| 541 | SkIRect devPathBounds; |
| 542 | shapeDevBounds.roundOut(&devPathBounds); |
| 543 | // pad to allow room for antialiasing |
| 544 | const int intPad = SkScalarCeilToInt(kAntiAliasPad); |
| 545 | // place devBounds at origin |
| 546 | int width = devPathBounds.width() + 2 * intPad; |
| 547 | int height = devPathBounds.height() + 2 * intPad; |
| 548 | devPathBounds = SkIRect::MakeWH(width, height); |
| 549 | SkScalar translateX = intPad - dx; |
| 550 | SkScalar translateY = intPad - dy; |
| 551 | |
| 552 | SkASSERT(devPathBounds.fLeft == 0); |
| 553 | SkASSERT(devPathBounds.fTop == 0); |
| 554 | SkASSERT(devPathBounds.width() > 0); |
| 555 | SkASSERT(devPathBounds.height() > 0); |
| 556 | |
| 557 | SkPath path; |
| 558 | shape.asPath(&path); |
| 559 | // setup bitmap backing |
| 560 | SkAutoPixmapStorage dst; |
| 561 | if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), |
| 562 | devPathBounds.height()))) { |
| 563 | return false; |
| 564 | } |
Mike Reed | 98a6216 | 2017-09-26 12:47:08 -0400 | [diff] [blame^] | 565 | sk_bzero(dst.writable_addr(), dst.computeByteSize()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 566 | |
| 567 | // rasterize path |
| 568 | SkPaint paint; |
| 569 | paint.setStyle(SkPaint::kFill_Style); |
| 570 | paint.setAntiAlias(true); |
| 571 | |
| 572 | SkDraw draw; |
| 573 | sk_bzero(&draw, sizeof(draw)); |
| 574 | |
| 575 | SkRasterClip rasterClip; |
| 576 | rasterClip.setRect(devPathBounds); |
| 577 | draw.fRC = &rasterClip; |
| 578 | drawMatrix.postTranslate(translateX, translateY); |
| 579 | draw.fMatrix = &drawMatrix; |
| 580 | draw.fDst = dst; |
| 581 | |
| 582 | draw.drawPathCoverage(path, paint); |
| 583 | |
| 584 | // add to atlas |
| 585 | SkIPoint16 atlasLocation; |
| 586 | GrDrawOpAtlas::AtlasID id; |
| 587 | if (!atlas->addToAtlas(&id, target, dst.width(), dst.height(), dst.addr(), |
| 588 | &atlasLocation)) { |
| 589 | this->flush(target, flushInfo); |
| 590 | if (!atlas->addToAtlas(&id, target, dst.width(), dst.height(), dst.addr(), |
| 591 | &atlasLocation)) { |
| 592 | return false; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // add to cache |
| 597 | shapeData->fKey.set(shape, ctm); |
| 598 | shapeData->fID = id; |
| 599 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 600 | shapeData->fBounds = SkRect::Make(devPathBounds); |
| 601 | shapeData->fBounds.offset(-translateX, -translateY); |
| 602 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 603 | // We pack the 2bit page index in the low bit of the u and v texture coords |
| 604 | uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id); |
| 605 | SkASSERT(pageIndex < 4); |
| 606 | uint16_t uBit = (pageIndex >> 1) & 0x1; |
| 607 | uint16_t vBit = pageIndex & 0x1; |
| 608 | shapeData->fTextureCoords.set(atlasLocation.fX << 1 | uBit, atlasLocation.fY << 1 | vBit, |
| 609 | (atlasLocation.fX+width) << 1 | uBit, |
| 610 | (atlasLocation.fY+height) << 1 | vBit); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 611 | |
| 612 | fShapeCache->add(shapeData); |
| 613 | fShapeList->addToTail(shapeData); |
| 614 | #ifdef DF_PATH_TRACKING |
| 615 | ++g_NumCachedPaths; |
| 616 | #endif |
| 617 | return true; |
| 618 | } |
| 619 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 620 | void writePathVertices(GrDrawOp::Target* target, |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 621 | GrDrawOpAtlas* atlas, |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 622 | intptr_t offset, |
| 623 | GrColor color, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 624 | size_t vertexStride, |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 625 | const SkVector& preTranslate, |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 626 | const ShapeData* shapeData) const { |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 627 | SkPoint* positions = reinterpret_cast<SkPoint*>(offset); |
| 628 | |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 629 | SkRect bounds = shapeData->fBounds; |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 630 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 631 | // vertex positions |
| 632 | // TODO make the vertex attributes a struct |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 633 | positions->setRectFan(bounds.left() + preTranslate.fX, |
| 634 | bounds.top() + preTranslate.fY, |
| 635 | bounds.right() + preTranslate.fX, |
| 636 | bounds.bottom() + preTranslate.fY, |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 637 | vertexStride); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 638 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 639 | // colors |
| 640 | for (int i = 0; i < kVerticesPerQuad; i++) { |
| 641 | GrColor* colorPtr = (GrColor*)(offset + sizeof(SkPoint) + i * vertexStride); |
| 642 | *colorPtr = color; |
| 643 | } |
| 644 | |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 645 | // set up texture coordinates |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 646 | uint16_t l = shapeData->fTextureCoords.fLeft; |
| 647 | uint16_t t = shapeData->fTextureCoords.fTop; |
| 648 | uint16_t r = shapeData->fTextureCoords.fRight; |
| 649 | uint16_t b = shapeData->fTextureCoords.fBottom; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 650 | |
| 651 | // set vertex texture coords |
| 652 | intptr_t textureCoordOffset = offset + sizeof(SkPoint) + sizeof(GrColor); |
| 653 | uint16_t* textureCoords = (uint16_t*) textureCoordOffset; |
| 654 | textureCoords[0] = l; |
| 655 | textureCoords[1] = t; |
| 656 | textureCoordOffset += vertexStride; |
| 657 | textureCoords = (uint16_t*)textureCoordOffset; |
| 658 | textureCoords[0] = l; |
| 659 | textureCoords[1] = b; |
| 660 | textureCoordOffset += vertexStride; |
| 661 | textureCoords = (uint16_t*)textureCoordOffset; |
| 662 | textureCoords[0] = r; |
| 663 | textureCoords[1] = b; |
| 664 | textureCoordOffset += vertexStride; |
| 665 | textureCoords = (uint16_t*)textureCoordOffset; |
| 666 | textureCoords[0] = r; |
| 667 | textureCoords[1] = t; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 670 | void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const { |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 671 | GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get(); |
| 672 | if (gp->numTextureSamplers() != (int)fAtlas->pageCount()) { |
| 673 | // During preparation the number of atlas pages has increased. |
| 674 | // Update the proxies used in the GP to match. |
| 675 | if (fUsesDistanceField) { |
| 676 | reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewProxies( |
| 677 | fAtlas->getProxies(), GrSamplerState::ClampBilerp()); |
| 678 | } else { |
| 679 | reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies( |
| 680 | fAtlas->getProxies(), GrSamplerState::ClampNearest()); |
| 681 | } |
| 682 | } |
| 683 | |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 684 | if (flushInfo->fInstancesToFlush) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 685 | GrMesh mesh(GrPrimitiveType::kTriangles); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 686 | int maxInstancesPerDraw = |
| 687 | static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 688 | mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerQuad, |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 689 | kVerticesPerQuad, flushInfo->fInstancesToFlush, |
| 690 | maxInstancesPerDraw); |
| 691 | mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset); |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 692 | target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 693 | flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush; |
| 694 | flushInfo->fInstancesToFlush = 0; |
| 695 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 698 | GrColor color() const { return fShapes[0].fColor; } |
| 699 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 700 | bool usesDistanceField() const { return fUsesDistanceField; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 701 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 702 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 703 | SmallPathOp* that = t->cast<SmallPathOp>(); |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 704 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 705 | return false; |
| 706 | } |
| 707 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 708 | if (this->usesDistanceField() != that->usesDistanceField()) { |
| 709 | return false; |
| 710 | } |
| 711 | |
| 712 | // TODO We can position on the cpu for distance field paths |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 713 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 714 | return false; |
| 715 | } |
| 716 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 717 | if (!this->usesDistanceField() && fHelper.usesLocalCoords() && |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 718 | !this->fShapes[0].fTranslate.equalsWithinTolerance(that->fShapes[0].fTranslate)) { |
| 719 | return false; |
| 720 | } |
| 721 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 722 | fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 723 | this->joinBounds(*that); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 724 | return true; |
| 725 | } |
| 726 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 727 | SkMatrix fViewMatrix; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 728 | bool fUsesDistanceField; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 729 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 730 | struct Entry { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 731 | GrColor fColor; |
| 732 | GrShape fShape; |
| 733 | SkVector fTranslate; |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 734 | }; |
| 735 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 736 | SkSTArray<1, Entry> fShapes; |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 737 | Helper fHelper; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 738 | GrDrawOpAtlas* fAtlas; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 739 | ShapeCache* fShapeCache; |
| 740 | ShapeDataList* fShapeList; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 741 | bool fGammaCorrect; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 742 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 743 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 744 | }; |
| 745 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 746 | bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 747 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 748 | "GrSmallPathRenderer::onDrawPath"); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 749 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 750 | // we've already bailed on inverse filled paths, so this is safe |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 751 | SkASSERT(!args.fShape->isEmpty()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 752 | SkASSERT(args.fShape->hasUnstyledKey()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 753 | if (!fAtlas) { |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 754 | fAtlas = GrDrawOpAtlas::Make(args.fContext, |
| 755 | kAlpha_8_GrPixelConfig, |
| 756 | ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT, |
| 757 | NUM_PLOTS_X, NUM_PLOTS_Y, |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 758 | &GrSmallPathRenderer::HandleEviction, |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 759 | (void*)this); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 760 | if (!fAtlas) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 761 | return false; |
| 762 | } |
| 763 | } |
| 764 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 765 | std::unique_ptr<GrDrawOp> op = SmallPathOp::Make( |
| 766 | std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(), &fShapeCache, |
| 767 | &fShapeList, args.fGammaCorrect, args.fUserStencilSettings); |
| 768 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
joshualitt | 9491f7f | 2015-02-11 11:33:38 -0800 | [diff] [blame] | 769 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 770 | return true; |
| 771 | } |
| 772 | |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 773 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 774 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 775 | #if GR_TEST_UTILS |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 776 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 777 | struct GrSmallPathRenderer::PathTestStruct { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 778 | PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {} |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 779 | ~PathTestStruct() { this->reset(); } |
| 780 | |
| 781 | void reset() { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 782 | ShapeDataList::Iter iter; |
| 783 | iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 784 | ShapeData* shapeData; |
| 785 | while ((shapeData = iter.get())) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 786 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 787 | fShapeList.remove(shapeData); |
| 788 | delete shapeData; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 789 | } |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 790 | fAtlas = nullptr; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 791 | fShapeCache.reset(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 794 | static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 795 | PathTestStruct* dfpr = (PathTestStruct*)pr; |
| 796 | // remove any paths that use this plot |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 797 | ShapeDataList::Iter iter; |
| 798 | iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 799 | ShapeData* shapeData; |
| 800 | while ((shapeData = iter.get())) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 801 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 802 | if (id == shapeData->fID) { |
| 803 | dfpr->fShapeCache.remove(shapeData->fKey); |
| 804 | dfpr->fShapeList.remove(shapeData); |
| 805 | delete shapeData; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | uint32_t fContextID; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 811 | std::unique_ptr<GrDrawOpAtlas> fAtlas; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 812 | ShapeCache fShapeCache; |
| 813 | ShapeDataList fShapeList; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 814 | }; |
| 815 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 816 | GR_DRAW_OP_TEST_DEFINE(SmallPathOp) { |
| 817 | using PathTestStruct = GrSmallPathRenderer::PathTestStruct; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 818 | static PathTestStruct gTestStruct; |
| 819 | |
| 820 | if (context->uniqueID() != gTestStruct.fContextID) { |
| 821 | gTestStruct.fContextID = context->uniqueID(); |
| 822 | gTestStruct.reset(); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 823 | gTestStruct.fAtlas = GrDrawOpAtlas::Make(context, kAlpha_8_GrPixelConfig, |
| 824 | ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT, |
| 825 | NUM_PLOTS_X, NUM_PLOTS_Y, |
| 826 | &PathTestStruct::HandleEviction, |
| 827 | (void*)&gTestStruct); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 831 | bool gammaCorrect = random->nextBool(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 832 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 833 | // This path renderer only allows fill styles. |
| 834 | GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill()); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 835 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 836 | return GrSmallPathRenderer::SmallPathOp::Make( |
| 837 | std::move(paint), shape, viewMatrix, gTestStruct.fAtlas.get(), &gTestStruct.fShapeCache, |
| 838 | &gTestStruct.fShapeList, gammaCorrect, GrGetRandomStencil(random, context)); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | #endif |