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" |
| 12 | #include "src/core/SkAutoMalloc.h" |
| 13 | #include "src/core/SkAutoPixmapStorage.h" |
| 14 | #include "src/core/SkDistanceFieldGen.h" |
| 15 | #include "src/core/SkDraw.h" |
| 16 | #include "src/core/SkPointPriv.h" |
| 17 | #include "src/core/SkRasterClip.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 30 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 31 | |
jvanverth | fb1e2fc | 2015-09-15 13:11:11 -0700 | [diff] [blame] | 32 | #define ATLAS_TEXTURE_WIDTH 2048 |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 33 | #define ATLAS_TEXTURE_HEIGHT 2048 |
jvanverth | fb1e2fc | 2015-09-15 13:11:11 -0700 | [diff] [blame] | 34 | #define PLOT_WIDTH 512 |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 35 | #define PLOT_HEIGHT 256 |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 36 | |
| 37 | #define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH) |
| 38 | #define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT) |
| 39 | |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 40 | #ifdef DF_PATH_TRACKING |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 41 | static int g_NumCachedShapes = 0; |
| 42 | static int g_NumFreedShapes = 0; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 43 | #endif |
| 44 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 45 | // mip levels |
Jim Van Verth | 25b8ca1 | 2017-02-17 14:02:13 -0500 | [diff] [blame] | 46 | static const SkScalar kIdealMinMIP = 12; |
Jim Van Verth | 825d4d7 | 2018-01-30 20:37:03 +0000 | [diff] [blame] | 47 | static const SkScalar kMaxMIP = 162; |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 48 | |
| 49 | static const SkScalar kMaxDim = 73; |
Jim Van Verth | 25b8ca1 | 2017-02-17 14:02:13 -0500 | [diff] [blame] | 50 | static const SkScalar kMinSize = SK_ScalarHalf; |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 51 | static const SkScalar kMaxSize = 2*kMaxMIP; |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 52 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 53 | class ShapeDataKey { |
| 54 | public: |
| 55 | ShapeDataKey() {} |
| 56 | ShapeDataKey(const ShapeDataKey& that) { *this = that; } |
| 57 | ShapeDataKey(const GrShape& shape, uint32_t dim) { this->set(shape, dim); } |
| 58 | ShapeDataKey(const GrShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); } |
| 59 | |
| 60 | ShapeDataKey& operator=(const ShapeDataKey& that) { |
| 61 | fKey.reset(that.fKey.count()); |
| 62 | memcpy(fKey.get(), that.fKey.get(), fKey.count() * sizeof(uint32_t)); |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | // for SDF paths |
| 67 | void set(const GrShape& shape, uint32_t dim) { |
| 68 | // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any |
| 69 | // relevant styling information. |
| 70 | SkASSERT(shape.style().isSimpleFill()); |
| 71 | SkASSERT(shape.hasUnstyledKey()); |
| 72 | int shapeKeySize = shape.unstyledKeySize(); |
| 73 | fKey.reset(1 + shapeKeySize); |
| 74 | fKey[0] = dim; |
| 75 | shape.writeUnstyledKey(&fKey[1]); |
| 76 | } |
| 77 | |
| 78 | // for bitmap paths |
| 79 | void set(const GrShape& shape, const SkMatrix& ctm) { |
| 80 | // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any |
| 81 | // relevant styling information. |
| 82 | SkASSERT(shape.style().isSimpleFill()); |
| 83 | SkASSERT(shape.hasUnstyledKey()); |
| 84 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 85 | SkScalar sx = ctm.get(SkMatrix::kMScaleX); |
| 86 | SkScalar sy = ctm.get(SkMatrix::kMScaleY); |
| 87 | SkScalar kx = ctm.get(SkMatrix::kMSkewX); |
| 88 | SkScalar ky = ctm.get(SkMatrix::kMSkewY); |
| 89 | SkScalar tx = ctm.get(SkMatrix::kMTransX); |
| 90 | SkScalar ty = ctm.get(SkMatrix::kMTransY); |
| 91 | // Allow 8 bits each in x and y of subpixel positioning. |
Jim Van Verth | a64a585 | 2018-03-09 14:16:31 -0500 | [diff] [blame] | 92 | tx -= SkScalarFloorToScalar(tx); |
| 93 | ty -= SkScalarFloorToScalar(ty); |
| 94 | SkFixed fracX = SkScalarToFixed(tx) & 0x0000FF00; |
| 95 | SkFixed fracY = SkScalarToFixed(ty) & 0x0000FF00; |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 96 | int shapeKeySize = shape.unstyledKeySize(); |
| 97 | fKey.reset(5 + shapeKeySize); |
| 98 | fKey[0] = SkFloat2Bits(sx); |
| 99 | fKey[1] = SkFloat2Bits(sy); |
| 100 | fKey[2] = SkFloat2Bits(kx); |
| 101 | fKey[3] = SkFloat2Bits(ky); |
| 102 | fKey[4] = fracX | (fracY >> 8); |
| 103 | shape.writeUnstyledKey(&fKey[5]); |
| 104 | } |
| 105 | |
| 106 | bool operator==(const ShapeDataKey& that) const { |
| 107 | return fKey.count() == that.fKey.count() && |
| 108 | 0 == memcmp(fKey.get(), that.fKey.get(), sizeof(uint32_t) * fKey.count()); |
| 109 | } |
| 110 | |
| 111 | int count32() const { return fKey.count(); } |
| 112 | const uint32_t* data() const { return fKey.get(); } |
| 113 | |
| 114 | private: |
| 115 | // The key is composed of the GrShape's key, and either the dimensions of the DF |
| 116 | // generated for the path (32x32 max, 64x64 max, 128x128 max) if an SDF image or |
| 117 | // the matrix for the path with only fractional translation. |
| 118 | SkAutoSTArray<24, uint32_t> fKey; |
| 119 | }; |
| 120 | |
| 121 | class ShapeData { |
| 122 | public: |
| 123 | ShapeDataKey fKey; |
| 124 | GrDrawOpAtlas::AtlasID fID; |
| 125 | SkRect fBounds; |
| 126 | GrIRect16 fTextureCoords; |
| 127 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData); |
| 128 | |
| 129 | static inline const ShapeDataKey& GetKey(const ShapeData& data) { |
| 130 | return data.fKey; |
| 131 | } |
| 132 | |
Kevin Lubick | b5502b2 | 2018-03-12 10:17:06 -0400 | [diff] [blame] | 133 | static inline uint32_t Hash(const ShapeDataKey& key) { |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 134 | return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32()); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 140 | // Callback to clear out internal path cache when eviction occurs |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 141 | void GrSmallPathRenderer::HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) { |
| 142 | GrSmallPathRenderer* dfpr = (GrSmallPathRenderer*)pr; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 143 | // remove any paths that use this plot |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 144 | ShapeDataList::Iter iter; |
| 145 | iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 146 | ShapeData* shapeData; |
| 147 | while ((shapeData = iter.get())) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 148 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 149 | if (id == shapeData->fID) { |
| 150 | dfpr->fShapeCache.remove(shapeData->fKey); |
| 151 | dfpr->fShapeList.remove(shapeData); |
| 152 | delete shapeData; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 153 | #ifdef DF_PATH_TRACKING |
| 154 | ++g_NumFreedPaths; |
| 155 | #endif |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 160 | //////////////////////////////////////////////////////////////////////////////// |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 161 | GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {} |
jvanverth | 6d22eca | 2014-10-28 11:10:48 -0700 | [diff] [blame] | 162 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 163 | GrSmallPathRenderer::~GrSmallPathRenderer() { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 164 | ShapeDataList::Iter iter; |
| 165 | iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 166 | ShapeData* shapeData; |
| 167 | while ((shapeData = iter.get())) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 168 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 169 | delete shapeData; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 170 | } |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 171 | |
| 172 | #ifdef DF_PATH_TRACKING |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 173 | SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes); |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 174 | #endif |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | //////////////////////////////////////////////////////////////////////////////// |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 178 | GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 179 | if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 180 | return CanDrawPath::kNo; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 181 | } |
| 182 | // If the shape has no key then we won't get any reuse. |
| 183 | if (!args.fShape->hasUnstyledKey()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 184 | return CanDrawPath::kNo; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 185 | } |
| 186 | // This only supports filled paths, however, the caller may apply the style to make a filled |
| 187 | // path and try again. |
| 188 | if (!args.fShape->style().isSimpleFill()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 189 | return CanDrawPath::kNo; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 190 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 191 | // This does non-inverse coverage-based antialiased fills. |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 192 | if (GrAAType::kCoverage != args.fAAType) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 193 | return CanDrawPath::kNo; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 194 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 195 | // TODO: Support inverse fill |
bsalomon | db7979a | 2016-06-27 11:08:43 -0700 | [diff] [blame] | 196 | if (args.fShape->inverseFilled()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 197 | return CanDrawPath::kNo; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 198 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 199 | |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 200 | // Only support paths with bounds within kMaxDim by kMaxDim, |
| 201 | // scaled to have bounds within kMaxSize by kMaxSize. |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 202 | // 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] | 203 | SkScalar scaleFactors[2] = { 1, 1 }; |
| 204 | if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 205 | return CanDrawPath::kNo; |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 206 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 207 | SkRect bounds = args.fShape->styledBounds(); |
Jim Van Verth | f9e678d | 2017-02-15 15:46:52 -0500 | [diff] [blame] | 208 | SkScalar minDim = SkMinScalar(bounds.width(), bounds.height()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 209 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
Jim Van Verth | d25cc9b | 2017-02-16 10:01:46 -0500 | [diff] [blame] | 210 | SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]); |
| 211 | SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]); |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 212 | if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) { |
| 213 | return CanDrawPath::kNo; |
| 214 | } |
bsalomon | 6266dca | 2016-03-11 06:22:00 -0800 | [diff] [blame] | 215 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 216 | return CanDrawPath::kYes; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 217 | } |
| 218 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 219 | //////////////////////////////////////////////////////////////////////////////// |
| 220 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 221 | // padding around path bounds to allow for antialiased pixels |
| 222 | static const SkScalar kAntiAliasPad = 1.0f; |
| 223 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 224 | class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp { |
| 225 | private: |
| 226 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 227 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 228 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 229 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 230 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 231 | using ShapeCache = SkTDynamicHash<ShapeData, ShapeDataKey>; |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 232 | using ShapeDataList = GrSmallPathRenderer::ShapeDataList; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 233 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 234 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 235 | GrPaint&& paint, |
| 236 | const GrShape& shape, |
| 237 | const SkMatrix& viewMatrix, |
| 238 | GrDrawOpAtlas* atlas, |
| 239 | ShapeCache* shapeCache, |
| 240 | ShapeDataList* shapeList, |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 241 | bool gammaCorrect, |
| 242 | const GrUserStencilSettings* stencilSettings) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 243 | return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix, |
| 244 | atlas, shapeCache, shapeList, gammaCorrect, |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 245 | stencilSettings); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 246 | } |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 247 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 248 | SmallPathOp(Helper::MakeArgs helperArgs, const SkPMColor4f& color, const GrShape& shape, |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 249 | const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache, |
| 250 | ShapeDataList* shapeList, bool gammaCorrect, |
| 251 | const GrUserStencilSettings* stencilSettings) |
| 252 | : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 253 | SkASSERT(shape.hasUnstyledKey()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 254 | // Compute bounds |
| 255 | this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo); |
| 256 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 257 | #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] | 258 | fUsesDistanceField = true; |
| 259 | #else |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 260 | // 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] | 261 | fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP; |
| 262 | #endif |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 263 | // always use distance fields if in perspective |
| 264 | fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective(); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 265 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 266 | fShapes.emplace_back(Entry{color, shape, viewMatrix}); |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 267 | |
| 268 | fAtlas = atlas; |
| 269 | fShapeCache = shapeCache; |
| 270 | fShapeList = shapeList; |
| 271 | fGammaCorrect = gammaCorrect; |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 274 | const char* name() const override { return "SmallPathOp"; } |
| 275 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 276 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 277 | fHelper.visitProxies(func); |
| 278 | |
| 279 | const sk_sp<GrTextureProxy>* proxies = fAtlas->getProxies(); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 280 | for (uint32_t i = 0; i < fAtlas->numActivePages(); ++i) { |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 281 | SkASSERT(proxies[i]); |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 282 | func(proxies[i].get(), GrMipMapped::kNo); |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 286 | #ifdef SK_DEBUG |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 287 | SkString dumpInfo() const override { |
| 288 | SkString string; |
| 289 | for (const auto& geo : fShapes) { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 290 | string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA()); |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 291 | } |
| 292 | string += fHelper.dumpInfo(); |
| 293 | string += INHERITED::dumpInfo(); |
| 294 | return string; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 295 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 296 | #endif |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 297 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 298 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 299 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 300 | GrProcessorSet::Analysis finalize( |
| 301 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 302 | GrClampType clampType) override { |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 303 | return fHelper.finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 304 | caps, clip, hasMixedSampledCoverage, clampType, |
| 305 | GrProcessorAnalysisCoverage::kSingleChannel, &fShapes.front().fColor, &fWideColor); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 308 | private: |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 309 | struct FlushInfo { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 310 | sk_sp<const GrBuffer> fVertexBuffer; |
| 311 | sk_sp<const GrBuffer> fIndexBuffer; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 312 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 313 | GrPipeline::FixedDynamicState* fFixedDynamicState; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 314 | int fVertexOffset; |
| 315 | int fInstancesToFlush; |
| 316 | }; |
| 317 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 318 | void onPrepareDraws(Target* target) override { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 319 | int instanceCount = fShapes.count(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 320 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 321 | static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures; |
| 322 | GR_STATIC_ASSERT(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures); |
| 323 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 324 | FlushInfo flushInfo; |
| 325 | flushInfo.fFixedDynamicState = target->makeFixedDynamicState(kMaxTextures); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 326 | int numActiveProxies = fAtlas->numActivePages(); |
| 327 | const auto proxies = fAtlas->getProxies(); |
| 328 | for (int i = 0; i < numActiveProxies; ++i) { |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 329 | // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the |
| 330 | // proxies don't get added during the visitProxies call. Thus we add them here. |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 331 | flushInfo.fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get(); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 332 | target->sampledProxyArray()->push_back(proxies[i].get()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 333 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 334 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 335 | // Setup GrGeometryProcessor |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 336 | const SkMatrix& ctm = fShapes[0].fViewMatrix; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 337 | if (fUsesDistanceField) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 338 | uint32_t flags = 0; |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 339 | // 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] | 340 | flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 341 | flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 342 | flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0; |
| 343 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 344 | const SkMatrix* matrix; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 345 | SkMatrix invert; |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 346 | if (ctm.hasPerspective()) { |
| 347 | matrix = &ctm; |
| 348 | } else if (fHelper.usesLocalCoords()) { |
| 349 | if (!ctm.invert(&invert)) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 350 | return; |
| 351 | } |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 352 | matrix = &invert; |
| 353 | } else { |
| 354 | matrix = &SkMatrix::I(); |
| 355 | } |
| 356 | flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make( |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 357 | *target->caps().shaderCaps(), *matrix, fWideColor, fAtlas->getProxies(), |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 358 | fAtlas->numActivePages(), GrSamplerState::ClampBilerp(), flags); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 359 | } else { |
| 360 | SkMatrix invert; |
| 361 | if (fHelper.usesLocalCoords()) { |
| 362 | if (!ctm.invert(&invert)) { |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 363 | return; |
| 364 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 365 | } |
| 366 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 367 | flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make( |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 368 | *target->caps().shaderCaps(), this->color(), fWideColor, fAtlas->getProxies(), |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 369 | fAtlas->numActivePages(), GrSamplerState::ClampNearest(), kA8_GrMaskFormat, |
| 370 | invert, false); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 371 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 372 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 373 | // allocate vertices |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 374 | const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride(); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 375 | |
| 376 | // We need to make sure we don't overflow a 32 bit int when we request space in the |
| 377 | // makeVertexSpace call below. |
| 378 | if (instanceCount > SK_MaxS32 / kVerticesPerQuad) { |
| 379 | return; |
| 380 | } |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 381 | GrVertexWriter vertices{target->makeVertexSpace(kVertexStride, |
| 382 | kVerticesPerQuad * instanceCount, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 383 | &flushInfo.fVertexBuffer, |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 384 | &flushInfo.fVertexOffset)}; |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 385 | flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer(); |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 386 | if (!vertices.fPtr || !flushInfo.fIndexBuffer) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 387 | SkDebugf("Could not allocate vertices\n"); |
| 388 | return; |
| 389 | } |
| 390 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 391 | flushInfo.fInstancesToFlush = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 392 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 393 | const Entry& args = fShapes[i]; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 394 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 395 | ShapeData* shapeData; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 396 | if (fUsesDistanceField) { |
| 397 | // get mip level |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 398 | SkScalar maxScale; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 399 | const SkRect& bounds = args.fShape.bounds(); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 400 | if (args.fViewMatrix.hasPerspective()) { |
| 401 | // approximate the scale since we can't get it from the matrix |
| 402 | SkRect xformedBounds; |
| 403 | args.fViewMatrix.mapRect(&xformedBounds, bounds); |
Jim Van Verth | 5124593 | 2017-10-12 11:07:29 -0400 | [diff] [blame] | 404 | maxScale = SkScalarAbs(SkTMax(xformedBounds.width() / bounds.width(), |
| 405 | xformedBounds.height() / bounds.height())); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 406 | } else { |
| 407 | maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale()); |
| 408 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 409 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
| 410 | // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.) |
| 411 | // In the majority of cases this will yield a crisper rendering. |
| 412 | SkScalar mipScale = 1.0f; |
| 413 | // Our mipscale is the maxScale clamped to the next highest power of 2 |
| 414 | if (maxScale <= SK_ScalarHalf) { |
| 415 | SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale))); |
| 416 | mipScale = SkScalarPow(2, -log); |
| 417 | } else if (maxScale > SK_Scalar1) { |
| 418 | SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale)); |
| 419 | mipScale = SkScalarPow(2, log); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 420 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 421 | SkASSERT(maxScale <= mipScale); |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 422 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 423 | SkScalar mipSize = mipScale*SkScalarAbs(maxDim); |
| 424 | // For sizes less than kIdealMinMIP we want to use as large a distance field as we can |
| 425 | // so we can preserve as much detail as possible. However, we can't scale down more |
| 426 | // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize |
| 427 | // just bigger than the ideal, and then scale down until we are no more than 4x the |
| 428 | // original mipsize. |
| 429 | if (mipSize < kIdealMinMIP) { |
| 430 | SkScalar newMipSize = mipSize; |
| 431 | do { |
| 432 | newMipSize *= 2; |
| 433 | } while (newMipSize < kIdealMinMIP); |
| 434 | while (newMipSize > 4 * mipSize) { |
| 435 | newMipSize *= 0.25f; |
| 436 | } |
| 437 | mipSize = newMipSize; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 438 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 439 | SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP); |
Jim Van Verth | c0bc1bb | 2017-02-27 18:21:16 -0500 | [diff] [blame] | 440 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 441 | // check to see if df path is cached |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 442 | ShapeDataKey key(args.fShape, SkScalarCeilToInt(desiredDimension)); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 443 | shapeData = fShapeCache->find(key); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 444 | if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 445 | // Remove the stale cache entry |
| 446 | if (shapeData) { |
| 447 | fShapeCache->remove(shapeData->fKey); |
| 448 | fShapeList->remove(shapeData); |
| 449 | delete shapeData; |
| 450 | } |
| 451 | SkScalar scale = desiredDimension / maxDim; |
| 452 | |
| 453 | shapeData = new ShapeData; |
| 454 | if (!this->addDFPathToAtlas(target, |
| 455 | &flushInfo, |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 456 | fAtlas, |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 457 | shapeData, |
| 458 | args.fShape, |
| 459 | SkScalarCeilToInt(desiredDimension), |
| 460 | scale)) { |
| 461 | delete shapeData; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 462 | continue; |
| 463 | } |
Jim Van Verth | c0bc1bb | 2017-02-27 18:21:16 -0500 | [diff] [blame] | 464 | } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 465 | } else { |
| 466 | // check to see if bitmap path is cached |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 467 | ShapeDataKey key(args.fShape, args.fViewMatrix); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 468 | shapeData = fShapeCache->find(key); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 469 | if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 470 | // Remove the stale cache entry |
| 471 | if (shapeData) { |
| 472 | fShapeCache->remove(shapeData->fKey); |
| 473 | fShapeList->remove(shapeData); |
| 474 | delete shapeData; |
| 475 | } |
| 476 | |
| 477 | shapeData = new ShapeData; |
| 478 | if (!this->addBMPathToAtlas(target, |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 479 | &flushInfo, |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 480 | fAtlas, |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 481 | shapeData, |
| 482 | args.fShape, |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 483 | args.fViewMatrix)) { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 484 | delete shapeData; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 485 | continue; |
| 486 | } |
| 487 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Robert Phillips | 40a29d7 | 2018-01-18 12:59:22 -0500 | [diff] [blame] | 490 | auto uploadTarget = target->deferredUploadTarget(); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 491 | fAtlas->setLastUseToken(shapeData->fID, uploadTarget->tokenTracker()->nextDrawToken()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 492 | |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 493 | this->writePathVertices(fAtlas, vertices, GrVertexColor(args.fColor, fWideColor), |
| 494 | args.fViewMatrix, shapeData); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 495 | flushInfo.fInstancesToFlush++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 496 | } |
| 497 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 498 | this->flush(target, &flushInfo); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 501 | bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas, |
| 502 | int width, int height, const void* image, |
| 503 | GrDrawOpAtlas::AtlasID* id, SkIPoint16* atlasLocation) const { |
| 504 | auto resourceProvider = target->resourceProvider(); |
| 505 | auto uploadTarget = target->deferredUploadTarget(); |
| 506 | |
| 507 | GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, id, |
| 508 | uploadTarget, width, height, |
| 509 | image, atlasLocation); |
| 510 | if (GrDrawOpAtlas::ErrorCode::kError == code) { |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) { |
| 515 | this->flush(target, flushInfo); |
| 516 | |
| 517 | code = atlas->addToAtlas(resourceProvider, id, uploadTarget, width, height, |
| 518 | image, atlasLocation); |
| 519 | } |
| 520 | |
| 521 | return GrDrawOpAtlas::ErrorCode::kSucceeded == code; |
| 522 | } |
| 523 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 524 | bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 525 | GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape, |
| 526 | uint32_t dimension, SkScalar scale) const { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 527 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 528 | const SkRect& bounds = shape.bounds(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 529 | |
| 530 | // generate bounding rect for bitmap draw |
| 531 | SkRect scaledBounds = bounds; |
| 532 | // scale to mip level size |
| 533 | scaledBounds.fLeft *= scale; |
| 534 | scaledBounds.fTop *= scale; |
| 535 | scaledBounds.fRight *= scale; |
| 536 | scaledBounds.fBottom *= scale; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 537 | // subtract out integer portion of origin |
| 538 | // (SDF created will be placed with fractional offset burnt in) |
Jim Van Verth | 07b6ad0 | 2016-12-20 10:23:09 -0500 | [diff] [blame] | 539 | SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft); |
| 540 | SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 541 | scaledBounds.offset(-dx, -dy); |
| 542 | // get integer boundary |
| 543 | SkIRect devPathBounds; |
| 544 | scaledBounds.roundOut(&devPathBounds); |
| 545 | // pad to allow room for antialiasing |
jvanverth | ecbed9d | 2015-12-18 10:07:52 -0800 | [diff] [blame] | 546 | const int intPad = SkScalarCeilToInt(kAntiAliasPad); |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 547 | // place devBounds at origin |
| 548 | int width = devPathBounds.width() + 2*intPad; |
| 549 | int height = devPathBounds.height() + 2*intPad; |
| 550 | devPathBounds = SkIRect::MakeWH(width, height); |
Robert Phillips | 3cf781d | 2017-08-22 18:25:11 -0400 | [diff] [blame] | 551 | SkScalar translateX = intPad - dx; |
| 552 | SkScalar translateY = intPad - dy; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 553 | |
| 554 | // draw path to bitmap |
| 555 | SkMatrix drawMatrix; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 556 | drawMatrix.setScale(scale, scale); |
Robert Phillips | 3cf781d | 2017-08-22 18:25:11 -0400 | [diff] [blame] | 557 | drawMatrix.postTranslate(translateX, translateY); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 558 | |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 559 | SkASSERT(devPathBounds.fLeft == 0); |
| 560 | SkASSERT(devPathBounds.fTop == 0); |
Jim Van Verth | 25b8ca1 | 2017-02-17 14:02:13 -0500 | [diff] [blame] | 561 | SkASSERT(devPathBounds.width() > 0); |
| 562 | SkASSERT(devPathBounds.height() > 0); |
bsalomon | f93f515 | 2016-10-26 08:00:00 -0700 | [diff] [blame] | 563 | |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 564 | // setup signed distance field storage |
| 565 | SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 566 | width = dfBounds.width(); |
| 567 | height = dfBounds.height(); |
rmistry | 4784225 | 2016-12-21 04:25:18 -0800 | [diff] [blame] | 568 | // TODO We should really generate this directly into the plot somehow |
| 569 | SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char)); |
joel.liang | 6d2f73c | 2016-12-20 18:58:53 -0800 | [diff] [blame] | 570 | |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 571 | SkPath path; |
| 572 | shape.asPath(&path); |
| 573 | #ifndef SK_USE_LEGACY_DISTANCE_FIELDS |
| 574 | // Generate signed distance field directly from SkPath |
| 575 | bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(), |
| 576 | path, drawMatrix, |
| 577 | width, height, width * sizeof(unsigned char)); |
| 578 | if (!succeed) { |
| 579 | #endif |
| 580 | // setup bitmap backing |
| 581 | SkAutoPixmapStorage dst; |
| 582 | if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), |
| 583 | devPathBounds.height()))) { |
| 584 | return false; |
| 585 | } |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 586 | sk_bzero(dst.writable_addr(), dst.computeByteSize()); |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 587 | |
| 588 | // rasterize path |
| 589 | SkPaint paint; |
| 590 | paint.setStyle(SkPaint::kFill_Style); |
| 591 | paint.setAntiAlias(true); |
| 592 | |
| 593 | SkDraw draw; |
joel.liang | 8cbb424 | 2017-01-09 18:39:43 -0800 | [diff] [blame] | 594 | |
| 595 | SkRasterClip rasterClip; |
| 596 | rasterClip.setRect(devPathBounds); |
| 597 | draw.fRC = &rasterClip; |
| 598 | draw.fMatrix = &drawMatrix; |
| 599 | draw.fDst = dst; |
| 600 | |
| 601 | draw.drawPathCoverage(path, paint); |
| 602 | |
| 603 | // Generate signed distance field |
| 604 | SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(), |
| 605 | (const unsigned char*)dst.addr(), |
| 606 | dst.width(), dst.height(), dst.rowBytes()); |
| 607 | #ifndef SK_USE_LEGACY_DISTANCE_FIELDS |
| 608 | } |
| 609 | #endif |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 610 | |
| 611 | // add to atlas |
| 612 | SkIPoint16 atlasLocation; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 613 | GrDrawOpAtlas::AtlasID id; |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 614 | |
| 615 | if (!this->addToAtlas(target, flushInfo, atlas, |
| 616 | width, height, dfStorage.get(), &id, &atlasLocation)) { |
| 617 | return false; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // add to cache |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 621 | shapeData->fKey.set(shape, dimension); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 622 | shapeData->fID = id; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 623 | |
Robert Phillips | 3cf781d | 2017-08-22 18:25:11 -0400 | [diff] [blame] | 624 | shapeData->fBounds = SkRect::Make(devPathBounds); |
| 625 | shapeData->fBounds.offset(-translateX, -translateY); |
| 626 | shapeData->fBounds.fLeft /= scale; |
| 627 | shapeData->fBounds.fTop /= scale; |
| 628 | shapeData->fBounds.fRight /= scale; |
| 629 | shapeData->fBounds.fBottom /= scale; |
Jim Van Verth | ecdb686 | 2016-12-13 18:17:47 -0500 | [diff] [blame] | 630 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 631 | // We pack the 2bit page index in the low bit of the u and v texture coords |
| 632 | uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id); |
| 633 | SkASSERT(pageIndex < 4); |
| 634 | uint16_t uBit = (pageIndex >> 1) & 0x1; |
| 635 | uint16_t vBit = pageIndex & 0x1; |
| 636 | shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1 | uBit, |
| 637 | (atlasLocation.fY+SK_DistanceFieldPad) << 1 | vBit, |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 638 | (atlasLocation.fX+SK_DistanceFieldPad+ |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 639 | devPathBounds.width()) << 1 | uBit, |
Jim Van Verth | 6a7a704 | 2017-09-11 11:04:10 -0400 | [diff] [blame] | 640 | (atlasLocation.fY+SK_DistanceFieldPad+ |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 641 | devPathBounds.height()) << 1 | vBit); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 642 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 643 | fShapeCache->add(shapeData); |
| 644 | fShapeList->addToTail(shapeData); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 645 | #ifdef DF_PATH_TRACKING |
| 646 | ++g_NumCachedPaths; |
| 647 | #endif |
| 648 | return true; |
| 649 | } |
| 650 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 651 | bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 652 | GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape, |
| 653 | const SkMatrix& ctm) const { |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 654 | const SkRect& bounds = shape.bounds(); |
| 655 | if (bounds.isEmpty()) { |
| 656 | return false; |
| 657 | } |
| 658 | SkMatrix drawMatrix(ctm); |
Jim Van Verth | a64a585 | 2018-03-09 14:16:31 -0500 | [diff] [blame] | 659 | SkScalar tx = ctm.getTranslateX(); |
| 660 | SkScalar ty = ctm.getTranslateY(); |
| 661 | tx -= SkScalarFloorToScalar(tx); |
| 662 | ty -= SkScalarFloorToScalar(ty); |
| 663 | drawMatrix.set(SkMatrix::kMTransX, tx); |
| 664 | drawMatrix.set(SkMatrix::kMTransY, ty); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 665 | SkRect shapeDevBounds; |
| 666 | drawMatrix.mapRect(&shapeDevBounds, bounds); |
| 667 | SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft); |
| 668 | SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop); |
| 669 | |
| 670 | // get integer boundary |
| 671 | SkIRect devPathBounds; |
| 672 | shapeDevBounds.roundOut(&devPathBounds); |
| 673 | // pad to allow room for antialiasing |
| 674 | const int intPad = SkScalarCeilToInt(kAntiAliasPad); |
| 675 | // place devBounds at origin |
| 676 | int width = devPathBounds.width() + 2 * intPad; |
| 677 | int height = devPathBounds.height() + 2 * intPad; |
| 678 | devPathBounds = SkIRect::MakeWH(width, height); |
| 679 | SkScalar translateX = intPad - dx; |
| 680 | SkScalar translateY = intPad - dy; |
| 681 | |
| 682 | SkASSERT(devPathBounds.fLeft == 0); |
| 683 | SkASSERT(devPathBounds.fTop == 0); |
| 684 | SkASSERT(devPathBounds.width() > 0); |
| 685 | SkASSERT(devPathBounds.height() > 0); |
| 686 | |
| 687 | SkPath path; |
| 688 | shape.asPath(&path); |
| 689 | // setup bitmap backing |
| 690 | SkAutoPixmapStorage dst; |
| 691 | if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), |
| 692 | devPathBounds.height()))) { |
| 693 | return false; |
| 694 | } |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 695 | sk_bzero(dst.writable_addr(), dst.computeByteSize()); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 696 | |
| 697 | // rasterize path |
| 698 | SkPaint paint; |
| 699 | paint.setStyle(SkPaint::kFill_Style); |
| 700 | paint.setAntiAlias(true); |
| 701 | |
| 702 | SkDraw draw; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 703 | |
| 704 | SkRasterClip rasterClip; |
| 705 | rasterClip.setRect(devPathBounds); |
| 706 | draw.fRC = &rasterClip; |
| 707 | drawMatrix.postTranslate(translateX, translateY); |
| 708 | draw.fMatrix = &drawMatrix; |
| 709 | draw.fDst = dst; |
| 710 | |
| 711 | draw.drawPathCoverage(path, paint); |
| 712 | |
| 713 | // add to atlas |
| 714 | SkIPoint16 atlasLocation; |
| 715 | GrDrawOpAtlas::AtlasID id; |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 716 | |
| 717 | if (!this->addToAtlas(target, flushInfo, atlas, |
| 718 | dst.width(), dst.height(), dst.addr(), &id, &atlasLocation)) { |
| 719 | return false; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | // add to cache |
| 723 | shapeData->fKey.set(shape, ctm); |
| 724 | shapeData->fID = id; |
| 725 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 726 | shapeData->fBounds = SkRect::Make(devPathBounds); |
| 727 | shapeData->fBounds.offset(-translateX, -translateY); |
| 728 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 729 | // We pack the 2bit page index in the low bit of the u and v texture coords |
| 730 | uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id); |
| 731 | SkASSERT(pageIndex < 4); |
| 732 | uint16_t uBit = (pageIndex >> 1) & 0x1; |
| 733 | uint16_t vBit = pageIndex & 0x1; |
| 734 | shapeData->fTextureCoords.set(atlasLocation.fX << 1 | uBit, atlasLocation.fY << 1 | vBit, |
| 735 | (atlasLocation.fX+width) << 1 | uBit, |
| 736 | (atlasLocation.fY+height) << 1 | vBit); |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 737 | |
| 738 | fShapeCache->add(shapeData); |
| 739 | fShapeList->addToTail(shapeData); |
| 740 | #ifdef DF_PATH_TRACKING |
| 741 | ++g_NumCachedPaths; |
| 742 | #endif |
| 743 | return true; |
| 744 | } |
| 745 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 746 | void writePathVertices(GrDrawOpAtlas* atlas, |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 747 | GrVertexWriter& vertices, |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 748 | const GrVertexColor& color, |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 749 | const SkMatrix& ctm, |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 750 | const ShapeData* shapeData) const { |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 751 | SkRect translatedBounds(shapeData->fBounds); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 752 | if (!fUsesDistanceField) { |
Jim Van Verth | a64a585 | 2018-03-09 14:16:31 -0500 | [diff] [blame] | 753 | translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)), |
| 754 | SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY))); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 755 | } |
Jim Van Verth | 7704754 | 2017-01-11 14:17:00 -0500 | [diff] [blame] | 756 | |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 757 | // set up texture coordinates |
| 758 | GrVertexWriter::TriStrip<uint16_t> texCoords{ |
| 759 | (uint16_t)shapeData->fTextureCoords.fLeft, |
| 760 | (uint16_t)shapeData->fTextureCoords.fTop, |
| 761 | (uint16_t)shapeData->fTextureCoords.fRight, |
| 762 | (uint16_t)shapeData->fTextureCoords.fBottom |
| 763 | }; |
| 764 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 765 | if (fUsesDistanceField && !ctm.hasPerspective()) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 766 | vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm), |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 767 | color, |
| 768 | texCoords); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 769 | } else { |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 770 | vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds), |
| 771 | color, |
| 772 | texCoords); |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 773 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 776 | void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const { |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 777 | GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get(); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 778 | int numAtlasTextures = SkToInt(fAtlas->numActivePages()); |
| 779 | auto proxies = fAtlas->getProxies(); |
| 780 | if (gp->numTextureSamplers() != numAtlasTextures) { |
| 781 | for (int i = gp->numTextureSamplers(); i < numAtlasTextures; ++i) { |
| 782 | flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get(); |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 783 | // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the |
| 784 | // proxies don't get added during the visitProxies call. Thus we add them here. |
| 785 | target->sampledProxyArray()->push_back(proxies[i].get()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 786 | } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 787 | // During preparation the number of atlas pages has increased. |
| 788 | // Update the proxies used in the GP to match. |
| 789 | if (fUsesDistanceField) { |
| 790 | reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewProxies( |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 791 | fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampBilerp()); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 792 | } else { |
| 793 | reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies( |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 794 | fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampNearest()); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 798 | if (flushInfo->fInstancesToFlush) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 799 | GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 800 | int maxInstancesPerDraw = |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 801 | static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 802 | mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerQuad, kVerticesPerQuad, |
| 803 | flushInfo->fInstancesToFlush, maxInstancesPerDraw); |
| 804 | mesh->setVertexData(flushInfo->fVertexBuffer, flushInfo->fVertexOffset); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 805 | target->recordDraw( |
| 806 | flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fFixedDynamicState, nullptr); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 807 | flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush; |
| 808 | flushInfo->fInstancesToFlush = 0; |
| 809 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 812 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 813 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
| 814 | } |
| 815 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 816 | const SkPMColor4f& color() const { return fShapes[0].fColor; } |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 817 | bool usesDistanceField() const { return fUsesDistanceField; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 818 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 819 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 820 | SmallPathOp* that = t->cast<SmallPathOp>(); |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 821 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 822 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 825 | if (this->usesDistanceField() != that->usesDistanceField()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 826 | return CombineResult::kCannotCombine; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 827 | } |
| 828 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 829 | const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix; |
| 830 | const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix; |
| 831 | |
| 832 | if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 833 | return CombineResult::kCannotCombine; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 836 | // We can position on the cpu unless we're in perspective, |
| 837 | // but also need to make sure local matrices are identical |
| 838 | if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) && |
| 839 | !thisCtm.cheapEqualTo(thatCtm)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 840 | return CombineResult::kCannotCombine; |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 841 | } |
| 842 | |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 843 | // Depending on the ctm we may have a different shader for SDF paths |
| 844 | if (this->usesDistanceField()) { |
| 845 | if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() || |
| 846 | thisCtm.isSimilarity() != thatCtm.isSimilarity()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 847 | return CombineResult::kCannotCombine; |
Jim Van Verth | 5698c8a | 2017-10-12 10:18:44 -0400 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 851 | fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin()); |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 852 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 853 | return CombineResult::kMerged; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Jim Van Verth | 33632d8 | 2017-02-28 10:24:39 -0500 | [diff] [blame] | 856 | bool fUsesDistanceField; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 857 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 858 | struct Entry { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 859 | SkPMColor4f fColor; |
| 860 | GrShape fShape; |
| 861 | SkMatrix fViewMatrix; |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 862 | }; |
| 863 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 864 | SkSTArray<1, Entry> fShapes; |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 865 | Helper fHelper; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 866 | GrDrawOpAtlas* fAtlas; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 867 | ShapeCache* fShapeCache; |
| 868 | ShapeDataList* fShapeList; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 869 | bool fGammaCorrect; |
Brian Osman | c906d25 | 2018-12-04 11:17:46 -0500 | [diff] [blame] | 870 | bool fWideColor; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 871 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 872 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 873 | }; |
| 874 | |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 875 | bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 876 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 877 | "GrSmallPathRenderer::onDrawPath"); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 878 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 879 | // we've already bailed on inverse filled paths, so this is safe |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 880 | SkASSERT(!args.fShape->isEmpty()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 881 | SkASSERT(args.fShape->hasUnstyledKey()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 882 | if (!fAtlas) { |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 883 | const GrBackendFormat format = args.fContext->priv().caps()->getDefaultBackendFormat( |
| 884 | GrColorType::kAlpha_8, GrRenderable::kNo); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 885 | fAtlas = GrDrawOpAtlas::Make(args.fContext->priv().proxyProvider(), |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 886 | format, |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 887 | GrColorType::kAlpha_8, |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 888 | ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT, |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 889 | PLOT_WIDTH, PLOT_HEIGHT, |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 890 | GrDrawOpAtlas::AllowMultitexturing::kYes, |
Jim Van Verth | 8301046 | 2017-03-16 08:45:39 -0400 | [diff] [blame] | 891 | &GrSmallPathRenderer::HandleEviction, |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 892 | (void*)this); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 893 | if (!fAtlas) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 894 | return false; |
| 895 | } |
| 896 | } |
| 897 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 898 | std::unique_ptr<GrDrawOp> op = SmallPathOp::Make( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 899 | args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(), |
| 900 | &fShapeCache, &fShapeList, args.fGammaCorrect, args.fUserStencilSettings); |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 901 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
joshualitt | 9491f7f | 2015-02-11 11:33:38 -0800 | [diff] [blame] | 902 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 903 | return true; |
| 904 | } |
| 905 | |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 906 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 907 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 908 | #if GR_TEST_UTILS |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 909 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 910 | struct GrSmallPathRenderer::PathTestStruct { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 911 | PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {} |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 912 | ~PathTestStruct() { this->reset(); } |
| 913 | |
| 914 | void reset() { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 915 | ShapeDataList::Iter iter; |
| 916 | iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 917 | ShapeData* shapeData; |
| 918 | while ((shapeData = iter.get())) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 919 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 920 | fShapeList.remove(shapeData); |
| 921 | delete shapeData; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 922 | } |
Ben Wagner | 594f9ed | 2016-11-08 14:13:39 -0500 | [diff] [blame] | 923 | fAtlas = nullptr; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 924 | fShapeCache.reset(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 927 | static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 928 | PathTestStruct* dfpr = (PathTestStruct*)pr; |
| 929 | // remove any paths that use this plot |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 930 | ShapeDataList::Iter iter; |
| 931 | iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 932 | ShapeData* shapeData; |
| 933 | while ((shapeData = iter.get())) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 934 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 935 | if (id == shapeData->fID) { |
| 936 | dfpr->fShapeCache.remove(shapeData->fKey); |
| 937 | dfpr->fShapeList.remove(shapeData); |
| 938 | delete shapeData; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | uint32_t fContextID; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 944 | std::unique_ptr<GrDrawOpAtlas> fAtlas; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 945 | ShapeCache fShapeCache; |
| 946 | ShapeDataList fShapeList; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 947 | }; |
| 948 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 949 | std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly( |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 950 | GrRecordingContext* context, |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 951 | GrPaint&& paint, |
| 952 | const GrShape& shape, |
| 953 | const SkMatrix& viewMatrix, |
| 954 | GrDrawOpAtlas* atlas, |
| 955 | ShapeCache* shapeCache, |
| 956 | ShapeDataList* shapeList, |
| 957 | bool gammaCorrect, |
| 958 | const GrUserStencilSettings* stencil) { |
| 959 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 960 | return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix, |
| 961 | atlas, shapeCache, shapeList, gammaCorrect, |
| 962 | stencil); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 963 | |
| 964 | } |
| 965 | |
Brian Salomon | febbd23 | 2017-07-11 15:52:02 -0400 | [diff] [blame] | 966 | GR_DRAW_OP_TEST_DEFINE(SmallPathOp) { |
| 967 | using PathTestStruct = GrSmallPathRenderer::PathTestStruct; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 968 | static PathTestStruct gTestStruct; |
| 969 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 970 | if (context->priv().contextID() != gTestStruct.fContextID) { |
| 971 | gTestStruct.fContextID = context->priv().contextID(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 972 | gTestStruct.reset(); |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 973 | const GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat( |
| 974 | GrColorType::kAlpha_8, GrRenderable::kNo); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 975 | gTestStruct.fAtlas = GrDrawOpAtlas::Make(context->priv().proxyProvider(), |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 976 | format, GrColorType::kAlpha_8, |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 977 | ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT, |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 978 | PLOT_WIDTH, PLOT_HEIGHT, |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 979 | GrDrawOpAtlas::AllowMultitexturing::kYes, |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 980 | &PathTestStruct::HandleEviction, |
| 981 | (void*)&gTestStruct); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 985 | bool gammaCorrect = random->nextBool(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 986 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 987 | // This path renderer only allows fill styles. |
| 988 | GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill()); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 989 | return GrSmallPathRenderer::createOp_TestingOnly( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 990 | context, |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 991 | std::move(paint), shape, viewMatrix, |
| 992 | gTestStruct.fAtlas.get(), |
| 993 | &gTestStruct.fShapeCache, |
| 994 | &gTestStruct.fShapeList, |
| 995 | gammaCorrect, |
| 996 | GrGetRandomStencil(random, context)); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | #endif |