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