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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/gpu/ops/GrSmallPathRenderer.h" |
Robert Phillips | be9aff2 | 2019-02-15 11:33:22 -0500 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/core/SkAutoPixmapStorage.h" |
| 13 | #include "src/core/SkDistanceFieldGen.h" |
| 14 | #include "src/core/SkDraw.h" |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 15 | #include "src/core/SkMatrixPriv.h" |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 16 | #include "src/core/SkMatrixProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkPointPriv.h" |
| 18 | #include "src/core/SkRasterClip.h" |
| 19 | #include "src/gpu/GrBuffer.h" |
| 20 | #include "src/gpu/GrCaps.h" |
| 21 | #include "src/gpu/GrDistanceFieldGenFromVector.h" |
| 22 | #include "src/gpu/GrDrawOpTest.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrRenderTargetContext.h" |
| 24 | #include "src/gpu/GrResourceProvider.h" |
| 25 | #include "src/gpu/GrVertexWriter.h" |
| 26 | #include "src/gpu/effects/GrBitmapTextGeoProc.h" |
| 27 | #include "src/gpu/effects/GrDistanceFieldGeoProc.h" |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 28 | #include "src/gpu/geometry/GrQuad.h" |
Robert Phillips | 5b68ed4 | 2020-08-10 14:46:42 -0400 | [diff] [blame] | 29 | #include "src/gpu/geometry/GrStyledShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Robert Phillips | 55f681f | 2020-02-28 08:58:15 -0500 | [diff] [blame] | 31 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h" |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 32 | #include "src/gpu/ops/GrSmallPathAtlasMgr.h" |
Robert Phillips | 34949e3 | 2020-08-05 15:54:31 -0400 | [diff] [blame] | 33 | #include "src/gpu/ops/GrSmallPathShapeData.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 34 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 35 | // mip levels |
Sergey Ulanov | f1b2b42 | 2020-01-24 15:39:32 -0800 | [diff] [blame] | 36 | static constexpr SkScalar kIdealMinMIP = 12; |
| 37 | static constexpr SkScalar kMaxMIP = 162; |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 38 | |
Sergey Ulanov | f1b2b42 | 2020-01-24 15:39:32 -0800 | [diff] [blame] | 39 | static constexpr SkScalar kMaxDim = 73; |
| 40 | static constexpr SkScalar kMinSize = SK_ScalarHalf; |
| 41 | static constexpr SkScalar kMaxSize = 2*kMaxMIP; |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 42 | |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 43 | GrSmallPathRenderer::GrSmallPathRenderer() : fAtlasMgr(new GrSmallPathAtlasMgr) {} |
| 44 | |
Robert Phillips | a4bb064 | 2020-08-11 09:55:17 -0400 | [diff] [blame] | 45 | GrSmallPathRenderer::~GrSmallPathRenderer() {} |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 46 | |
Robert Phillips | 5dd3d88 | 2020-08-10 09:29:39 -0400 | [diff] [blame] | 47 | void GrSmallPathRenderer::addToOnFlushCallbacks(GrRecordingContext* rContext) { |
| 48 | rContext->priv().addOnFlushCallbackObject(fAtlasMgr.get()); |
| 49 | } |
| 50 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 51 | GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 52 | if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 53 | return CanDrawPath::kNo; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 54 | } |
| 55 | // If the shape has no key then we won't get any reuse. |
| 56 | if (!args.fShape->hasUnstyledKey()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 57 | return CanDrawPath::kNo; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 58 | } |
| 59 | // This only supports filled paths, however, the caller may apply the style to make a filled |
| 60 | // path and try again. |
| 61 | if (!args.fShape->style().isSimpleFill()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 62 | return CanDrawPath::kNo; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 63 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 64 | // This does non-inverse coverage-based antialiased fills. |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 65 | if (GrAAType::kCoverage != args.fAAType) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 66 | return CanDrawPath::kNo; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 67 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 68 | // TODO: Support inverse fill |
bsalomon | db7979a | 2016-06-27 11:08:43 -0700 | [diff] [blame] | 69 | if (args.fShape->inverseFilled()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 70 | return CanDrawPath::kNo; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 71 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 72 | |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 73 | // Only support paths with bounds within kMaxDim by kMaxDim, |
| 74 | // scaled to have bounds within kMaxSize by kMaxSize. |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 75 | // The goal is to accelerate rendering of lots of small paths that may be scaling. |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 76 | SkScalar scaleFactors[2] = { 1, 1 }; |
| 77 | if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 78 | return CanDrawPath::kNo; |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 79 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 80 | SkRect bounds = args.fShape->styledBounds(); |
Brian Osman | 116b33e | 2020-02-05 13:34:09 -0500 | [diff] [blame] | 81 | SkScalar minDim = std::min(bounds.width(), bounds.height()); |
| 82 | SkScalar maxDim = std::max(bounds.width(), bounds.height()); |
Jim Van Verth | d25cc9b | 2017-02-16 10:01:46 -0500 | [diff] [blame] | 83 | SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]); |
| 84 | SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]); |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 85 | if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) { |
| 86 | return CanDrawPath::kNo; |
| 87 | } |
bsalomon | 6266dca | 2016-03-11 06:22:00 -0800 | [diff] [blame] | 88 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 89 | return CanDrawPath::kYes; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 90 | } |
| 91 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 92 | //////////////////////////////////////////////////////////////////////////////// |
| 93 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 94 | // padding around path bounds to allow for antialiased pixels |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 95 | static const int kAntiAliasPad = 1; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 96 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 97 | class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp { |
| 98 | private: |
| 99 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 100 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 101 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 102 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 103 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 104 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 105 | GrPaint&& paint, |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 106 | const GrStyledShape& shape, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 107 | const SkMatrix& viewMatrix, |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 108 | GrSmallPathAtlasMgr* atlasMgr, |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 109 | bool gammaCorrect, |
| 110 | const GrUserStencilSettings* stencilSettings) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 111 | return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix, |
Robert Phillips | a4bb064 | 2020-08-11 09:55:17 -0400 | [diff] [blame] | 112 | atlasMgr, gammaCorrect, stencilSettings); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 113 | } |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 114 | |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 115 | SmallPathOp(Helper::MakeArgs helperArgs, const SkPMColor4f& color, const GrStyledShape& shape, |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 116 | const SkMatrix& viewMatrix, GrSmallPathAtlasMgr* atlasMgr, bool gammaCorrect, |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 117 | const GrUserStencilSettings* stencilSettings) |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 118 | : INHERITED(ClassID()) |
| 119 | , fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 120 | SkASSERT(shape.hasUnstyledKey()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 121 | // Compute bounds |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 122 | this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsHairline::kNo); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 123 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 124 | #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] | 125 | fUsesDistanceField = true; |
| 126 | #else |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 127 | // 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] | 128 | fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP; |
| 129 | #endif |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 130 | // always use distance fields if in perspective |
| 131 | fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective(); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 132 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 133 | fShapes.emplace_back(Entry{color, shape, viewMatrix}); |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 134 | |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 135 | fAtlasMgr = atlasMgr; |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 136 | fGammaCorrect = gammaCorrect; |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 139 | const char* name() const override { return "SmallPathOp"; } |
| 140 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 141 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 142 | fHelper.visitProxies(func); |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 145 | #ifdef SK_DEBUG |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 146 | SkString dumpInfo() const override { |
| 147 | SkString string; |
| 148 | for (const auto& geo : fShapes) { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 149 | string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA()); |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 150 | } |
| 151 | string += fHelper.dumpInfo(); |
| 152 | string += INHERITED::dumpInfo(); |
| 153 | return string; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 154 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 155 | #endif |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 156 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 157 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 158 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 159 | GrProcessorSet::Analysis finalize( |
| 160 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 161 | GrClampType clampType) override { |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 162 | return fHelper.finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 163 | caps, clip, hasMixedSampledCoverage, clampType, |
| 164 | GrProcessorAnalysisCoverage::kSingleChannel, &fShapes.front().fColor, &fWideColor); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 167 | private: |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 168 | struct FlushInfo { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 169 | sk_sp<const GrBuffer> fVertexBuffer; |
| 170 | sk_sp<const GrBuffer> fIndexBuffer; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 171 | GrGeometryProcessor* fGeometryProcessor; |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 172 | const GrSurfaceProxy** fPrimProcProxies; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 173 | int fVertexOffset; |
| 174 | int fInstancesToFlush; |
| 175 | }; |
| 176 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 177 | GrProgramInfo* programInfo() override { |
| 178 | // TODO [PI]: implement |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 182 | void onCreateProgramInfo(const GrCaps*, |
| 183 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 184 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 185 | GrAppliedClip&&, |
| 186 | const GrXferProcessor::DstProxyView&) override { |
| 187 | // TODO [PI]: implement |
| 188 | } |
| 189 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 190 | void onPrePrepareDraws(GrRecordingContext*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 191 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 192 | GrAppliedClip*, |
| 193 | const GrXferProcessor::DstProxyView&) override { |
| 194 | // TODO [PI]: implement |
| 195 | } |
| 196 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 197 | void onPrepareDraws(Target* target) override { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 198 | int instanceCount = fShapes.count(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 199 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 200 | static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 201 | static_assert(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 202 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 203 | FlushInfo flushInfo; |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 204 | flushInfo.fPrimProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures); |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 205 | |
| 206 | int numActiveProxies; |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 207 | const GrSurfaceProxyView* views = fAtlasMgr->getViews(&numActiveProxies); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 208 | for (int i = 0; i < numActiveProxies; ++i) { |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 209 | // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the |
| 210 | // proxies don't get added during the visitProxies call. Thus we add them here. |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 211 | flushInfo.fPrimProcProxies[i] = views[i].proxy(); |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 212 | target->sampledProxyArray()->push_back(views[i].proxy()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 213 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 214 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 215 | // Setup GrGeometryProcessor |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 216 | const SkMatrix& ctm = fShapes[0].fViewMatrix; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 217 | if (fUsesDistanceField) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 218 | uint32_t flags = 0; |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 219 | // Still need to key off of ctm to pick the right shader for the transformed quad |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 220 | flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 221 | flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 222 | flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0; |
| 223 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 224 | const SkMatrix* matrix; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 225 | SkMatrix invert; |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 226 | if (ctm.hasPerspective()) { |
| 227 | matrix = &ctm; |
| 228 | } else if (fHelper.usesLocalCoords()) { |
| 229 | if (!ctm.invert(&invert)) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 230 | return; |
| 231 | } |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 232 | matrix = &invert; |
| 233 | } else { |
| 234 | matrix = &SkMatrix::I(); |
| 235 | } |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 236 | flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make( |
| 237 | target->allocator(), *target->caps().shaderCaps(), *matrix, fWideColor, |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 238 | views, numActiveProxies, GrSamplerState::Filter::kLinear, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 239 | flags); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 240 | } else { |
| 241 | SkMatrix invert; |
| 242 | if (fHelper.usesLocalCoords()) { |
| 243 | if (!ctm.invert(&invert)) { |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 244 | return; |
| 245 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 246 | } |
| 247 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 248 | flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make( |
| 249 | target->allocator(), *target->caps().shaderCaps(), this->color(), fWideColor, |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 250 | views, numActiveProxies, GrSamplerState::Filter::kNearest, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 251 | kA8_GrMaskFormat, invert, false); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 252 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 253 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 254 | // allocate vertices |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 255 | const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride(); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 256 | |
| 257 | // We need to make sure we don't overflow a 32 bit int when we request space in the |
| 258 | // makeVertexSpace call below. |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 259 | if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) { |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 260 | return; |
| 261 | } |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 262 | GrVertexWriter vertices{ target->makeVertexSpace( |
| 263 | kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount, |
| 264 | &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)}; |
| 265 | |
| 266 | flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer(); |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 267 | if (!vertices.fPtr || !flushInfo.fIndexBuffer) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 268 | SkDebugf("Could not allocate vertices\n"); |
| 269 | return; |
| 270 | } |
| 271 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 272 | flushInfo.fInstancesToFlush = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 273 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 274 | const Entry& args = fShapes[i]; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 275 | |
Robert Phillips | 34949e3 | 2020-08-05 15:54:31 -0400 | [diff] [blame] | 276 | GrSmallPathShapeData* shapeData; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 277 | if (fUsesDistanceField) { |
| 278 | // get mip level |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 279 | SkScalar maxScale; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 280 | const SkRect& bounds = args.fShape.bounds(); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 281 | if (args.fViewMatrix.hasPerspective()) { |
| 282 | // approximate the scale since we can't get it from the matrix |
| 283 | SkRect xformedBounds; |
| 284 | args.fViewMatrix.mapRect(&xformedBounds, bounds); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 285 | maxScale = SkScalarAbs(std::max(xformedBounds.width() / bounds.width(), |
Jim Van Verth | 5124593 | 2017-10-12 11:07:29 -0400 | [diff] [blame] | 286 | xformedBounds.height() / bounds.height())); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 287 | } else { |
| 288 | maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale()); |
| 289 | } |
Brian Osman | 116b33e | 2020-02-05 13:34:09 -0500 | [diff] [blame] | 290 | SkScalar maxDim = std::max(bounds.width(), bounds.height()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 291 | // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.) |
| 292 | // In the majority of cases this will yield a crisper rendering. |
| 293 | SkScalar mipScale = 1.0f; |
| 294 | // Our mipscale is the maxScale clamped to the next highest power of 2 |
| 295 | if (maxScale <= SK_ScalarHalf) { |
| 296 | SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale))); |
| 297 | mipScale = SkScalarPow(2, -log); |
| 298 | } else if (maxScale > SK_Scalar1) { |
| 299 | SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale)); |
| 300 | mipScale = SkScalarPow(2, log); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 301 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 302 | SkASSERT(maxScale <= mipScale); |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 303 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 304 | SkScalar mipSize = mipScale*SkScalarAbs(maxDim); |
| 305 | // For sizes less than kIdealMinMIP we want to use as large a distance field as we can |
| 306 | // so we can preserve as much detail as possible. However, we can't scale down more |
| 307 | // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize |
| 308 | // just bigger than the ideal, and then scale down until we are no more than 4x the |
| 309 | // original mipsize. |
| 310 | if (mipSize < kIdealMinMIP) { |
| 311 | SkScalar newMipSize = mipSize; |
| 312 | do { |
| 313 | newMipSize *= 2; |
| 314 | } while (newMipSize < kIdealMinMIP); |
| 315 | while (newMipSize > 4 * mipSize) { |
| 316 | newMipSize *= 0.25f; |
| 317 | } |
| 318 | mipSize = newMipSize; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 319 | } |
Robert Phillips | 109ff20 | 2020-08-10 16:39:31 -0400 | [diff] [blame] | 320 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 321 | SkScalar desiredDimension = std::min(mipSize, kMaxMIP); |
Robert Phillips | 109ff20 | 2020-08-10 16:39:31 -0400 | [diff] [blame] | 322 | int ceilDesiredDimension = SkScalarCeilToInt(desiredDimension); |
Jim Van Verth | c0bc1bb | 2017-02-27 18:21:16 -0500 | [diff] [blame] | 323 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 324 | // check to see if df path is cached |
Robert Phillips | 109ff20 | 2020-08-10 16:39:31 -0400 | [diff] [blame] | 325 | shapeData = fAtlasMgr->findOrCreate(args.fShape, ceilDesiredDimension); |
Robert Phillips | 6507e63 | 2020-08-07 14:35:56 -0400 | [diff] [blame] | 326 | if (!shapeData->fAtlasLocator.plotLocator().isValid()) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 327 | SkScalar scale = desiredDimension / maxDim; |
| 328 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 329 | if (!this->addDFPathToAtlas(target, |
| 330 | &flushInfo, |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 331 | fAtlasMgr->atlas(), |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 332 | shapeData, |
| 333 | args.fShape, |
Robert Phillips | 109ff20 | 2020-08-10 16:39:31 -0400 | [diff] [blame] | 334 | ceilDesiredDimension, |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 335 | scale)) { |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 336 | fAtlasMgr->deleteCacheEntry(shapeData); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 337 | continue; |
| 338 | } |
Jim Van Verth | c0bc1bb | 2017-02-27 18:21:16 -0500 | [diff] [blame] | 339 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 340 | } else { |
| 341 | // check to see if bitmap path is cached |
Robert Phillips | 109ff20 | 2020-08-10 16:39:31 -0400 | [diff] [blame] | 342 | shapeData = fAtlasMgr->findOrCreate(args.fShape, args.fViewMatrix); |
Robert Phillips | 6507e63 | 2020-08-07 14:35:56 -0400 | [diff] [blame] | 343 | if (!shapeData->fAtlasLocator.plotLocator().isValid()) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 344 | if (!this->addBMPathToAtlas(target, |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 345 | &flushInfo, |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 346 | fAtlasMgr->atlas(), |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 347 | shapeData, |
| 348 | args.fShape, |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 349 | args.fViewMatrix)) { |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 350 | fAtlasMgr->deleteCacheEntry(shapeData); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 351 | continue; |
| 352 | } |
| 353 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 356 | auto uploadTarget = target->deferredUploadTarget(); |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 357 | fAtlasMgr->setUseToken(shapeData, uploadTarget->tokenTracker()->nextDrawToken()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 358 | |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 359 | this->writePathVertices(vertices, GrVertexColor(args.fColor, fWideColor), |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 360 | args.fViewMatrix, shapeData); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 361 | flushInfo.fInstancesToFlush++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 362 | } |
| 363 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 364 | this->flush(target, &flushInfo); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 367 | bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas, |
| 368 | int width, int height, const void* image, |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 369 | GrDrawOpAtlas::AtlasLocator* atlasLocator) const { |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 370 | auto resourceProvider = target->resourceProvider(); |
| 371 | auto uploadTarget = target->deferredUploadTarget(); |
| 372 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 373 | GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, uploadTarget, |
| 374 | width, height, image, atlasLocator); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 375 | if (GrDrawOpAtlas::ErrorCode::kError == code) { |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) { |
| 380 | this->flush(target, flushInfo); |
| 381 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 382 | code = atlas->addToAtlas(resourceProvider, uploadTarget, width, height, |
| 383 | image, atlasLocator); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return GrDrawOpAtlas::ErrorCode::kSucceeded == code; |
| 387 | } |
| 388 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 389 | bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, |
Robert Phillips | 34949e3 | 2020-08-05 15:54:31 -0400 | [diff] [blame] | 390 | GrDrawOpAtlas* atlas, GrSmallPathShapeData* shapeData, |
| 391 | const GrStyledShape& shape, uint32_t dimension, SkScalar scale) const { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 392 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 393 | const SkRect& bounds = shape.bounds(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 394 | |
| 395 | // generate bounding rect for bitmap draw |
| 396 | SkRect scaledBounds = bounds; |
| 397 | // scale to mip level size |
| 398 | scaledBounds.fLeft *= scale; |
| 399 | scaledBounds.fTop *= scale; |
| 400 | scaledBounds.fRight *= scale; |
| 401 | scaledBounds.fBottom *= scale; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 402 | // subtract out integer portion of origin |
| 403 | // (SDF created will be placed with fractional offset burnt in) |
Jim Van Verth | 07b6ad0 | 2016-12-20 10:23:09 -0500 | [diff] [blame] | 404 | SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft); |
| 405 | SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 406 | scaledBounds.offset(-dx, -dy); |
| 407 | // get integer boundary |
| 408 | SkIRect devPathBounds; |
| 409 | scaledBounds.roundOut(&devPathBounds); |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 410 | // place devBounds at origin with padding to allow room for antialiasing |
| 411 | int width = devPathBounds.width() + 2 * kAntiAliasPad; |
| 412 | int height = devPathBounds.height() + 2 * kAntiAliasPad; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 413 | devPathBounds = SkIRect::MakeWH(width, height); |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 414 | SkScalar translateX = kAntiAliasPad - dx; |
| 415 | SkScalar translateY = kAntiAliasPad - dy; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 416 | |
| 417 | // draw path to bitmap |
| 418 | SkMatrix drawMatrix; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 419 | drawMatrix.setScale(scale, scale); |
Robert Phillips | 3cf781d | 2017-08-22 18:25:11 -0400 | [diff] [blame] | 420 | drawMatrix.postTranslate(translateX, translateY); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 421 | |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 422 | SkASSERT(devPathBounds.fLeft == 0); |
| 423 | SkASSERT(devPathBounds.fTop == 0); |
Jim Van Verth | 25b8ca1 | 2017-02-17 14:02:13 -0500 | [diff] [blame] | 424 | SkASSERT(devPathBounds.width() > 0); |
| 425 | SkASSERT(devPathBounds.height() > 0); |
bsalomon | f93f515 | 2016-10-26 08:00:00 -0700 | [diff] [blame] | 426 | |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 427 | // setup signed distance field storage |
| 428 | SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 429 | width = dfBounds.width(); |
| 430 | height = dfBounds.height(); |
rmistry | 4784225 | 2016-12-21 04:25:18 -0800 | [diff] [blame] | 431 | // TODO We should really generate this directly into the plot somehow |
| 432 | SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char)); |
joel.liang | 6d2f73c | 2016-12-20 18:58:53 -0800 | [diff] [blame] | 433 | |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 434 | SkPath path; |
| 435 | shape.asPath(&path); |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 436 | // Generate signed distance field directly from SkPath |
| 437 | bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(), |
Robert Phillips | a4bb064 | 2020-08-11 09:55:17 -0400 | [diff] [blame] | 438 | path, drawMatrix, width, height, |
| 439 | width * sizeof(unsigned char)); |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 440 | if (!succeed) { |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 441 | // setup bitmap backing |
| 442 | SkAutoPixmapStorage dst; |
Robert Phillips | a4bb064 | 2020-08-11 09:55:17 -0400 | [diff] [blame] | 443 | if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) { |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 444 | return false; |
| 445 | } |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 446 | sk_bzero(dst.writable_addr(), dst.computeByteSize()); |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 447 | |
| 448 | // rasterize path |
| 449 | SkPaint paint; |
| 450 | paint.setStyle(SkPaint::kFill_Style); |
| 451 | paint.setAntiAlias(true); |
| 452 | |
| 453 | SkDraw draw; |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 454 | |
| 455 | SkRasterClip rasterClip; |
| 456 | rasterClip.setRect(devPathBounds); |
| 457 | draw.fRC = &rasterClip; |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 458 | SkSimpleMatrixProvider matrixProvider(drawMatrix); |
| 459 | draw.fMatrixProvider = &matrixProvider; |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 460 | draw.fDst = dst; |
| 461 | |
| 462 | draw.drawPathCoverage(path, paint); |
| 463 | |
| 464 | // Generate signed distance field |
| 465 | SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(), |
| 466 | (const unsigned char*)dst.addr(), |
| 467 | dst.width(), dst.height(), dst.rowBytes()); |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 468 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 469 | |
| 470 | // add to atlas |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 471 | if (!this->addToAtlas(target, flushInfo, atlas, width, height, dfStorage.get(), |
| 472 | &shapeData->fAtlasLocator)) { |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 473 | return false; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Herb Derby | 1318e45 | 2020-08-03 14:38:10 -0400 | [diff] [blame] | 476 | shapeData->fAtlasLocator.insetSrc(SK_DistanceFieldPad); |
| 477 | |
Robert Phillips | 3cf781d | 2017-08-22 18:25:11 -0400 | [diff] [blame] | 478 | shapeData->fBounds = SkRect::Make(devPathBounds); |
| 479 | shapeData->fBounds.offset(-translateX, -translateY); |
| 480 | shapeData->fBounds.fLeft /= scale; |
| 481 | shapeData->fBounds.fTop /= scale; |
| 482 | shapeData->fBounds.fRight /= scale; |
| 483 | shapeData->fBounds.fBottom /= scale; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 487 | bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, |
Robert Phillips | 34949e3 | 2020-08-05 15:54:31 -0400 | [diff] [blame] | 488 | GrDrawOpAtlas* atlas, GrSmallPathShapeData* shapeData, |
| 489 | const GrStyledShape& shape, const SkMatrix& ctm) const { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 490 | const SkRect& bounds = shape.bounds(); |
| 491 | if (bounds.isEmpty()) { |
| 492 | return false; |
| 493 | } |
| 494 | SkMatrix drawMatrix(ctm); |
Jim Van Verth | a64a585 | 2018-03-09 14:16:31 -0500 | [diff] [blame] | 495 | SkScalar tx = ctm.getTranslateX(); |
| 496 | SkScalar ty = ctm.getTranslateY(); |
| 497 | tx -= SkScalarFloorToScalar(tx); |
| 498 | ty -= SkScalarFloorToScalar(ty); |
| 499 | drawMatrix.set(SkMatrix::kMTransX, tx); |
| 500 | drawMatrix.set(SkMatrix::kMTransY, ty); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 501 | SkRect shapeDevBounds; |
| 502 | drawMatrix.mapRect(&shapeDevBounds, bounds); |
| 503 | SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft); |
| 504 | SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop); |
| 505 | |
| 506 | // get integer boundary |
| 507 | SkIRect devPathBounds; |
| 508 | shapeDevBounds.roundOut(&devPathBounds); |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 509 | // place devBounds at origin with padding to allow room for antialiasing |
| 510 | int width = devPathBounds.width() + 2 * kAntiAliasPad; |
| 511 | int height = devPathBounds.height() + 2 * kAntiAliasPad; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 512 | devPathBounds = SkIRect::MakeWH(width, height); |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 513 | SkScalar translateX = kAntiAliasPad - dx; |
| 514 | SkScalar translateY = kAntiAliasPad - dy; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 515 | |
| 516 | SkASSERT(devPathBounds.fLeft == 0); |
| 517 | SkASSERT(devPathBounds.fTop == 0); |
| 518 | SkASSERT(devPathBounds.width() > 0); |
| 519 | SkASSERT(devPathBounds.height() > 0); |
| 520 | |
| 521 | SkPath path; |
| 522 | shape.asPath(&path); |
| 523 | // setup bitmap backing |
| 524 | SkAutoPixmapStorage dst; |
Robert Phillips | a4bb064 | 2020-08-11 09:55:17 -0400 | [diff] [blame] | 525 | if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 526 | return false; |
| 527 | } |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 528 | sk_bzero(dst.writable_addr(), dst.computeByteSize()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 529 | |
| 530 | // rasterize path |
| 531 | SkPaint paint; |
| 532 | paint.setStyle(SkPaint::kFill_Style); |
| 533 | paint.setAntiAlias(true); |
| 534 | |
| 535 | SkDraw draw; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 536 | |
| 537 | SkRasterClip rasterClip; |
| 538 | rasterClip.setRect(devPathBounds); |
| 539 | draw.fRC = &rasterClip; |
| 540 | drawMatrix.postTranslate(translateX, translateY); |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 541 | SkSimpleMatrixProvider matrixProvider(drawMatrix); |
| 542 | draw.fMatrixProvider = &matrixProvider; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 543 | draw.fDst = dst; |
| 544 | |
| 545 | draw.drawPathCoverage(path, paint); |
| 546 | |
| 547 | // add to atlas |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 548 | if (!this->addToAtlas(target, flushInfo, atlas, dst.width(), dst.height(), dst.addr(), |
| 549 | &shapeData->fAtlasLocator)) { |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 550 | return false; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 551 | } |
| 552 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 553 | shapeData->fBounds = SkRect::Make(devPathBounds); |
| 554 | shapeData->fBounds.offset(-translateX, -translateY); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 555 | return true; |
| 556 | } |
| 557 | |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 558 | void writePathVertices(GrVertexWriter& vertices, |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 559 | const GrVertexColor& color, |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 560 | const SkMatrix& ctm, |
Robert Phillips | 34949e3 | 2020-08-05 15:54:31 -0400 | [diff] [blame] | 561 | const GrSmallPathShapeData* shapeData) const { |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 562 | SkRect translatedBounds(shapeData->fBounds); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 563 | if (!fUsesDistanceField) { |
Jim Van Verth | a64a585 | 2018-03-09 14:16:31 -0500 | [diff] [blame] | 564 | translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)), |
| 565 | SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY))); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 566 | } |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 567 | |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 568 | // set up texture coordinates |
Herb Derby | 1318e45 | 2020-08-03 14:38:10 -0400 | [diff] [blame] | 569 | auto texCoords = GrVertexWriter::TriStripFromUVs(shapeData->fAtlasLocator.getUVs()); |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 570 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 571 | if (fUsesDistanceField && !ctm.hasPerspective()) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 572 | vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm), |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 573 | color, |
| 574 | texCoords); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 575 | } else { |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 576 | vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds), |
| 577 | color, |
| 578 | texCoords); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 579 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 582 | void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const { |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 583 | |
| 584 | int numActiveProxies; |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 585 | const GrSurfaceProxyView* views = fAtlasMgr->getViews(&numActiveProxies); |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 586 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 587 | GrGeometryProcessor* gp = flushInfo->fGeometryProcessor; |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 588 | |
| 589 | if (gp->numTextureSamplers() != numActiveProxies) { |
| 590 | for (int i = gp->numTextureSamplers(); i < numActiveProxies; ++i) { |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 591 | flushInfo->fPrimProcProxies[i] = views[i].proxy(); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 592 | // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the |
| 593 | // proxies don't get added during the visitProxies call. Thus we add them here. |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 594 | target->sampledProxyArray()->push_back(views[i].proxy()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 595 | } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 596 | // During preparation the number of atlas pages has increased. |
| 597 | // Update the proxies used in the GP to match. |
| 598 | if (fUsesDistanceField) { |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 599 | reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewViews( |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 600 | views, numActiveProxies, GrSamplerState::Filter::kLinear); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 601 | } else { |
Greg Daniel | 9715b6c | 2019-12-10 15:03:10 -0500 | [diff] [blame] | 602 | reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews( |
Robert Phillips | 76e7a7f | 2020-08-06 08:24:34 -0400 | [diff] [blame] | 603 | views, numActiveProxies, GrSamplerState::Filter::kNearest); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 607 | if (flushInfo->fInstancesToFlush) { |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 608 | GrSimpleMesh* mesh = target->allocMesh(); |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 609 | mesh->setIndexedPatterned(flushInfo->fIndexBuffer, |
| 610 | GrResourceProvider::NumIndicesPerNonAAQuad(), |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 611 | flushInfo->fInstancesToFlush, |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 612 | GrResourceProvider::MaxNumNonAAQuads(), |
| 613 | flushInfo->fVertexBuffer, |
| 614 | GrResourceProvider::NumVertsPerNonAAQuad(), |
| 615 | flushInfo->fVertexOffset); |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 616 | target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies, |
| 617 | GrPrimitiveType::kTriangles); |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 618 | flushInfo->fVertexOffset += GrResourceProvider::NumVertsPerNonAAQuad() * |
| 619 | flushInfo->fInstancesToFlush; |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 620 | flushInfo->fInstancesToFlush = 0; |
| 621 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 624 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 625 | auto pipeline = fHelper.createPipelineWithStencil(flushState); |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 626 | |
| 627 | flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 630 | const SkPMColor4f& color() const { return fShapes[0].fColor; } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 631 | bool usesDistanceField() const { return fUsesDistanceField; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 632 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 633 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, |
| 634 | const GrCaps& caps) override { |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 635 | SmallPathOp* that = t->cast<SmallPathOp>(); |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 636 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 637 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 640 | if (this->usesDistanceField() != that->usesDistanceField()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 641 | return CombineResult::kCannotCombine; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 642 | } |
| 643 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 644 | const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix; |
| 645 | const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix; |
| 646 | |
| 647 | if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 648 | return CombineResult::kCannotCombine; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 651 | // We can position on the cpu unless we're in perspective, |
| 652 | // but also need to make sure local matrices are identical |
| 653 | if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) && |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 654 | !SkMatrixPriv::CheapEqual(thisCtm, thatCtm)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 655 | return CombineResult::kCannotCombine; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 656 | } |
| 657 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 658 | // Depending on the ctm we may have a different shader for SDF paths |
| 659 | if (this->usesDistanceField()) { |
| 660 | if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() || |
| 661 | thisCtm.isSimilarity() != thatCtm.isSimilarity()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 662 | return CombineResult::kCannotCombine; |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 666 | fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin()); |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 667 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 668 | return CombineResult::kMerged; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 671 | bool fUsesDistanceField; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 672 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 673 | struct Entry { |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 674 | SkPMColor4f fColor; |
| 675 | GrStyledShape fShape; |
| 676 | SkMatrix fViewMatrix; |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 677 | }; |
| 678 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 679 | SkSTArray<1, Entry> fShapes; |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 680 | Helper fHelper; |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 681 | GrSmallPathAtlasMgr* fAtlasMgr; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 682 | bool fGammaCorrect; |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 683 | bool fWideColor; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 684 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 685 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 686 | }; |
| 687 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 688 | bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 689 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 690 | "GrSmallPathRenderer::onDrawPath"); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 691 | |
Robert Phillips | 5dd3d88 | 2020-08-10 09:29:39 -0400 | [diff] [blame] | 692 | const GrCaps* caps = args.fContext->priv().caps(); |
| 693 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 694 | // we've already bailed on inverse filled paths, so this is safe |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 695 | SkASSERT(!args.fShape->isEmpty()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 696 | SkASSERT(args.fShape->hasUnstyledKey()); |
Robert Phillips | 5dd3d88 | 2020-08-10 09:29:39 -0400 | [diff] [blame] | 697 | if (!fAtlasMgr->initAtlas(args.fContext->priv().proxyProvider(), caps)) { |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 698 | return false; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 701 | std::unique_ptr<GrDrawOp> op = SmallPathOp::Make( |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 702 | args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, |
| 703 | fAtlasMgr.get(), args.fGammaCorrect, args.fUserStencilSettings); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 704 | args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op)); |
joshualitt | 9491f7f | 2015-02-11 11:33:38 -0800 | [diff] [blame] | 705 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 706 | return true; |
| 707 | } |
| 708 | |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 709 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 710 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 711 | #if GR_TEST_UTILS |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 712 | |
Robert Phillips | 5dd3d88 | 2020-08-10 09:29:39 -0400 | [diff] [blame] | 713 | struct GrSmallPathRenderer::PathTestStruct { |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 714 | PathTestStruct() : fContextID(SK_InvalidGenID) {} |
Robert Phillips | 5dd3d88 | 2020-08-10 09:29:39 -0400 | [diff] [blame] | 715 | ~PathTestStruct() { } |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 716 | |
| 717 | uint32_t fContextID; |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 718 | std::unique_ptr<GrSmallPathAtlasMgr> fAtlasMgr; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 719 | }; |
| 720 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 721 | std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly( |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 722 | GrRecordingContext* context, |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 723 | GrPaint&& paint, |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 724 | const GrStyledShape& shape, |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 725 | const SkMatrix& viewMatrix, |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 726 | GrSmallPathAtlasMgr* atlasMgr, |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 727 | bool gammaCorrect, |
| 728 | const GrUserStencilSettings* stencil) { |
| 729 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 730 | return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix, |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 731 | atlasMgr, gammaCorrect, stencil); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 732 | } |
| 733 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 734 | GR_DRAW_OP_TEST_DEFINE(SmallPathOp) { |
Robert Phillips | 5dd3d88 | 2020-08-10 09:29:39 -0400 | [diff] [blame] | 735 | static GrSmallPathRenderer::PathTestStruct gTestStruct; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 736 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 737 | if (context->priv().contextID() != gTestStruct.fContextID) { |
| 738 | gTestStruct.fContextID = context->priv().contextID(); |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 739 | gTestStruct.fAtlasMgr = std::make_unique<GrSmallPathAtlasMgr>(); |
| 740 | gTestStruct.fAtlasMgr->initAtlas(context->priv().proxyProvider(), |
Robert Phillips | 5dd3d88 | 2020-08-10 09:29:39 -0400 | [diff] [blame] | 741 | context->priv().caps()); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 745 | bool gammaCorrect = random->nextBool(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 746 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 747 | // This path renderer only allows fill styles. |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 748 | GrStyledShape shape(GrTest::TestPath(random), GrStyle::SimpleFill()); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 749 | return GrSmallPathRenderer::createOp_TestingOnly( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 750 | context, |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 751 | std::move(paint), shape, viewMatrix, |
Robert Phillips | cac1764 | 2020-08-07 16:17:10 -0400 | [diff] [blame] | 752 | gTestStruct.fAtlasMgr.get(), |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 753 | gammaCorrect, |
| 754 | GrGetRandomStencil(random, context)); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | #endif |