senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrTessellatingPathRenderer.h" |
| 9 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 10 | #include "GrAuditTrail.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 11 | #include "GrBatchFlushState.h" |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 12 | #include "GrBatchTest.h" |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 13 | #include "GrClip.h" |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 14 | #include "GrDefaultGeoProcFactory.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 15 | #include "GrMesh.h" |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 16 | #include "GrPathUtils.h" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 17 | #include "GrPipelineBuilder.h" |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 18 | #include "GrResourceCache.h" |
| 19 | #include "GrResourceProvider.h" |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 20 | #include "GrTessellator.h" |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 21 | #include "SkGeometry.h" |
| 22 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 23 | #include "batches/GrVertexBatch.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 24 | |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | |
| 27 | /* |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 28 | * This path renderer tessellates the path into triangles using GrTessellator, uploads the |
| 29 | * triangles to a vertex buffer, and renders them with a single draw call. It can do screenspace |
| 30 | * antialiasing with a one-pixel coverage ramp. |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 31 | */ |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 32 | namespace { |
| 33 | |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 34 | struct TessInfo { |
| 35 | SkScalar fTolerance; |
senorblanco | 06f989a | 2015-09-02 09:05:17 -0700 | [diff] [blame] | 36 | int fCount; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 39 | // When the SkPathRef genID changes, invalidate a corresponding GrResource described by key. |
| 40 | class PathInvalidator : public SkPathRef::GenIDChangeListener { |
| 41 | public: |
| 42 | explicit PathInvalidator(const GrUniqueKey& key) : fMsg(key) {} |
| 43 | private: |
| 44 | GrUniqueKeyInvalidatedMessage fMsg; |
| 45 | |
| 46 | void onChange() override { |
| 47 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); |
| 48 | } |
| 49 | }; |
| 50 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 51 | bool cache_match(GrBuffer* vertexBuffer, SkScalar tol, int* actualCount) { |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 52 | if (!vertexBuffer) { |
| 53 | return false; |
| 54 | } |
| 55 | const SkData* data = vertexBuffer->getUniqueKey().getCustomData(); |
| 56 | SkASSERT(data); |
| 57 | const TessInfo* info = static_cast<const TessInfo*>(data->data()); |
| 58 | if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) { |
senorblanco | 06f989a | 2015-09-02 09:05:17 -0700 | [diff] [blame] | 59 | *actualCount = info->fCount; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 65 | class StaticVertexAllocator : public GrTessellator::VertexAllocator { |
| 66 | public: |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 67 | StaticVertexAllocator(size_t stride, GrResourceProvider* resourceProvider, bool canMapVB) |
| 68 | : VertexAllocator(stride) |
| 69 | , fResourceProvider(resourceProvider) |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 70 | , fCanMapVB(canMapVB) |
| 71 | , fVertices(nullptr) { |
| 72 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 73 | void* lock(int vertexCount) override { |
| 74 | size_t size = vertexCount * stride(); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 75 | fVertexBuffer.reset(fResourceProvider->createBuffer( |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 76 | size, kVertex_GrBufferType, kStatic_GrAccessPattern, 0)); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 77 | if (!fVertexBuffer.get()) { |
| 78 | return nullptr; |
| 79 | } |
| 80 | if (fCanMapVB) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 81 | fVertices = fVertexBuffer->map(); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 82 | } else { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 83 | fVertices = sk_malloc_throw(vertexCount * stride()); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 84 | } |
| 85 | return fVertices; |
| 86 | } |
| 87 | void unlock(int actualCount) override { |
| 88 | if (fCanMapVB) { |
| 89 | fVertexBuffer->unmap(); |
| 90 | } else { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 91 | fVertexBuffer->updateData(fVertices, actualCount * stride()); |
| 92 | sk_free(fVertices); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 93 | } |
| 94 | fVertices = nullptr; |
| 95 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 96 | GrBuffer* vertexBuffer() { return fVertexBuffer.get(); } |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 97 | private: |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 98 | sk_sp<GrBuffer> fVertexBuffer; |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 99 | GrResourceProvider* fResourceProvider; |
| 100 | bool fCanMapVB; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 101 | void* fVertices; |
| 102 | }; |
| 103 | |
| 104 | class DynamicVertexAllocator : public GrTessellator::VertexAllocator { |
| 105 | public: |
| 106 | DynamicVertexAllocator(size_t stride, GrVertexBatch::Target* target) |
| 107 | : VertexAllocator(stride) |
| 108 | , fTarget(target) |
| 109 | , fVertexBuffer(nullptr) |
| 110 | , fVertices(nullptr) { |
| 111 | } |
| 112 | void* lock(int vertexCount) override { |
| 113 | fVertexCount = vertexCount; |
| 114 | fVertices = fTarget->makeVertexSpace(stride(), vertexCount, &fVertexBuffer, &fFirstVertex); |
| 115 | return fVertices; |
| 116 | } |
| 117 | void unlock(int actualCount) override { |
| 118 | fTarget->putBackVertices(fVertexCount - actualCount, stride()); |
| 119 | fVertices = nullptr; |
| 120 | } |
| 121 | const GrBuffer* vertexBuffer() const { return fVertexBuffer; } |
| 122 | int firstVertex() const { return fFirstVertex; } |
| 123 | private: |
| 124 | GrVertexBatch::Target* fTarget; |
| 125 | const GrBuffer* fVertexBuffer; |
| 126 | int fVertexCount; |
| 127 | int fFirstVertex; |
| 128 | void* fVertices; |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 131 | } // namespace |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 132 | |
| 133 | GrTessellatingPathRenderer::GrTessellatingPathRenderer() { |
| 134 | } |
| 135 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 136 | bool GrTessellatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 137 | // This path renderer can draw fill styles, and can do screenspace antialiasing via a |
| 138 | // one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex |
| 139 | // ones to simpler algorithms. We pass on paths that have styles, though they may come back |
| 140 | // around after applying the styling information to the geometry to create a filled path. In |
| 141 | // the non-AA case, We skip paths thta don't have a key since the real advantage of this path |
| 142 | // renderer comes from caching the tessellated geometry. In the AA case, we do not cache, so we |
| 143 | // accept paths without keys. |
| 144 | if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) { |
| 145 | return false; |
| 146 | } |
| 147 | if (args.fAntiAlias) { |
| 148 | #ifdef SK_DISABLE_SCREENSPACE_TESS_AA_PATH_RENDERER |
| 149 | return false; |
| 150 | #else |
| 151 | SkPath path; |
| 152 | args.fShape->asPath(&path); |
| 153 | if (path.countVerbs() > 10) { |
| 154 | return false; |
| 155 | } |
| 156 | #endif |
| 157 | } else if (!args.fShape->hasUnstyledKey()) { |
| 158 | return false; |
| 159 | } |
| 160 | return true; |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 161 | } |
| 162 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 163 | class TessellatingPathBatch : public GrVertexBatch { |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 164 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 165 | DEFINE_BATCH_CLASS_ID |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 166 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 167 | static GrDrawBatch* Create(const GrColor& color, |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 168 | const GrShape& shape, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 169 | const SkMatrix& viewMatrix, |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 170 | SkIRect devClipBounds, |
| 171 | bool antiAlias) { |
| 172 | return new TessellatingPathBatch(color, shape, viewMatrix, devClipBounds, antiAlias); |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 173 | } |
| 174 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 175 | const char* name() const override { return "TessellatingPathBatch"; } |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 176 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 177 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 178 | GrInitInvariantOutput* coverage, |
| 179 | GrBatchToXPOverrides* overrides) const override { |
| 180 | color->setKnownFourComponents(fColor); |
| 181 | coverage->setUnknownSingleComponent(); |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 182 | } |
| 183 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 184 | private: |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 185 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 186 | // Handle any color overrides |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 187 | if (!overrides.readsColor()) { |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 188 | fColor = GrColor_ILLEGAL; |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 189 | } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 190 | overrides.getOverrideColorIfSet(&fColor); |
| 191 | fPipelineInfo = overrides; |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 192 | } |
| 193 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 194 | SkPath getPath() const { |
| 195 | SkASSERT(!fShape.style().applies()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 196 | SkPath path; |
| 197 | fShape.asPath(&path); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 198 | return path; |
| 199 | } |
| 200 | |
| 201 | void draw(Target* target, const GrGeometryProcessor* gp) const { |
| 202 | SkASSERT(!fAntiAlias); |
| 203 | GrResourceProvider* rp = target->resourceProvider(); |
| 204 | bool inverseFill = fShape.inverseFilled(); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 205 | // construct a cache key from the path's genID and the view matrix |
| 206 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 207 | GrUniqueKey key; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 208 | static constexpr int kClipBoundsCnt = sizeof(fDevClipBounds) / sizeof(uint32_t); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 209 | int shapeKeyDataCnt = fShape.unstyledKeySize(); |
| 210 | SkASSERT(shapeKeyDataCnt >= 0); |
| 211 | GrUniqueKey::Builder builder(&key, kDomain, shapeKeyDataCnt + kClipBoundsCnt); |
| 212 | fShape.writeUnstyledKey(&builder[0]); |
| 213 | // For inverse fills, the tessellation is dependent on clip bounds. |
| 214 | if (inverseFill) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 215 | memcpy(&builder[shapeKeyDataCnt], &fDevClipBounds, sizeof(fDevClipBounds)); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 216 | } else { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 217 | memset(&builder[shapeKeyDataCnt], 0, sizeof(fDevClipBounds)); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 218 | } |
| 219 | builder.finish(); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 220 | sk_sp<GrBuffer> cachedVertexBuffer(rp->findAndRefTByUniqueKey<GrBuffer>(key)); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 221 | int actualCount; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 222 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 223 | tol = GrPathUtils::scaleToleranceToSrc(tol, fViewMatrix, fShape.bounds()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 224 | if (cache_match(cachedVertexBuffer.get(), tol, &actualCount)) { |
| 225 | this->drawVertices(target, gp, cachedVertexBuffer.get(), 0, actualCount); |
| 226 | return; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 227 | } |
| 228 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 229 | SkRect clipBounds = SkRect::Make(fDevClipBounds); |
| 230 | |
| 231 | SkMatrix vmi; |
| 232 | if (!fViewMatrix.invert(&vmi)) { |
| 233 | return; |
| 234 | } |
| 235 | vmi.mapRect(&clipBounds); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 236 | bool isLinear; |
| 237 | bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags(); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 238 | StaticVertexAllocator allocator(gp->getVertexStride(), rp, canMapVB); |
| 239 | int count = GrTessellator::PathToTriangles(getPath(), tol, clipBounds, &allocator, |
| 240 | false, GrColor(), false, &isLinear); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 241 | if (count == 0) { |
| 242 | return; |
| 243 | } |
senorblanco | af1e21e | 2016-03-16 10:25:58 -0700 | [diff] [blame] | 244 | this->drawVertices(target, gp, allocator.vertexBuffer(), 0, count); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 245 | TessInfo info; |
| 246 | info.fTolerance = isLinear ? 0 : tol; |
| 247 | info.fCount = count; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 248 | key.setCustomData(SkData::MakeWithCopy(&info, sizeof(info))); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 249 | rp->assignUniqueKeyToResource(key, allocator.vertexBuffer()); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 250 | } |
| 251 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 252 | void drawAA(Target* target, const GrGeometryProcessor* gp) const { |
| 253 | SkASSERT(fAntiAlias); |
| 254 | SkPath path = getPath(); |
| 255 | if (path.isEmpty()) { |
| 256 | return; |
| 257 | } |
| 258 | SkRect clipBounds = SkRect::Make(fDevClipBounds); |
| 259 | path.transform(fViewMatrix); |
| 260 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 261 | bool isLinear; |
| 262 | DynamicVertexAllocator allocator(gp->getVertexStride(), target); |
| 263 | bool canTweakAlphaForCoverage = fPipelineInfo.canTweakAlphaForCoverage(); |
| 264 | int count = GrTessellator::PathToTriangles(path, tol, clipBounds, &allocator, |
| 265 | true, fColor, canTweakAlphaForCoverage, |
| 266 | &isLinear); |
| 267 | if (count == 0) { |
| 268 | return; |
| 269 | } |
| 270 | drawVertices(target, gp, allocator.vertexBuffer(), allocator.firstVertex(), count); |
| 271 | } |
| 272 | |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 273 | void onPrepareDraws(Target* target) const override { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 274 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 275 | { |
| 276 | using namespace GrDefaultGeoProcFactory; |
| 277 | |
| 278 | Color color(fColor); |
| 279 | LocalCoords localCoords(fPipelineInfo.readsLocalCoords() ? |
| 280 | LocalCoords::kUsePosition_Type : |
| 281 | LocalCoords::kUnused_Type); |
| 282 | Coverage::Type coverageType; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 283 | if (fAntiAlias) { |
| 284 | color = Color(Color::kAttribute_Type); |
| 285 | if (fPipelineInfo.canTweakAlphaForCoverage()) { |
| 286 | coverageType = Coverage::kSolid_Type; |
| 287 | } else { |
| 288 | coverageType = Coverage::kAttribute_Type; |
| 289 | } |
| 290 | } else if (fPipelineInfo.readsCoverage()) { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 291 | coverageType = Coverage::kSolid_Type; |
| 292 | } else { |
| 293 | coverageType = Coverage::kNone_Type; |
| 294 | } |
| 295 | Coverage coverage(coverageType); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 296 | if (fAntiAlias) { |
| 297 | gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(color, coverage, localCoords, |
| 298 | fViewMatrix); |
| 299 | } else { |
| 300 | gp = GrDefaultGeoProcFactory::Make(color, coverage, localCoords, fViewMatrix); |
| 301 | } |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 302 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 303 | if (fAntiAlias) { |
| 304 | this->drawAA(target, gp.get()); |
| 305 | } else { |
| 306 | this->draw(target, gp.get()); |
| 307 | } |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 308 | } |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 309 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 310 | void drawVertices(Target* target, const GrGeometryProcessor* gp, const GrBuffer* vb, |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 311 | int firstVertex, int count) const { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 312 | GrPrimitiveType primitiveType = TESSELLATOR_WIREFRAME ? kLines_GrPrimitiveType |
| 313 | : kTriangles_GrPrimitiveType; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 314 | GrMesh mesh; |
| 315 | mesh.init(primitiveType, vb, firstVertex, count); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 316 | target->draw(gp, mesh); |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 317 | } |
| 318 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 319 | bool onCombineIfPossible(GrBatch*, const GrCaps&) override { return false; } |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 320 | |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 321 | TessellatingPathBatch(const GrColor& color, |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 322 | const GrShape& shape, |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 323 | const SkMatrix& viewMatrix, |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 324 | const SkIRect& devClipBounds, |
| 325 | bool antiAlias) |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 326 | : INHERITED(ClassID()) |
| 327 | , fColor(color) |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 328 | , fShape(shape) |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 329 | , fViewMatrix(viewMatrix) |
| 330 | , fDevClipBounds(devClipBounds) |
| 331 | , fAntiAlias(antiAlias) { |
| 332 | SkRect devBounds; |
| 333 | viewMatrix.mapRect(&devBounds, shape.bounds()); |
| 334 | if (shape.inverseFilled()) { |
| 335 | // Because the clip bounds are used to add a contour for inverse fills, they must also |
| 336 | // include the path bounds. |
| 337 | devBounds.join(SkRect::Make(fDevClipBounds)); |
| 338 | } |
| 339 | this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo); |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 340 | } |
| 341 | |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 342 | GrColor fColor; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 343 | GrShape fShape; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 344 | SkMatrix fViewMatrix; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 345 | SkIRect fDevClipBounds; |
| 346 | bool fAntiAlias; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 347 | GrXPOverridesForBatch fPipelineInfo; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 348 | |
| 349 | typedef GrVertexBatch INHERITED; |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 350 | }; |
| 351 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 352 | bool GrTessellatingPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 353 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 354 | "GrTessellatingPathRenderer::onDrawPath"); |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 355 | SkIRect clipBoundsI; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 356 | args.fClip->getConservativeBounds(args.fRenderTargetContext->width(), |
| 357 | args.fRenderTargetContext->height(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 358 | &clipBoundsI); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 359 | sk_sp<GrDrawBatch> batch(TessellatingPathBatch::Create(args.fPaint->getColor(), |
| 360 | *args.fShape, |
| 361 | *args.fViewMatrix, |
| 362 | clipBoundsI, |
| 363 | args.fAntiAlias)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 364 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 365 | GrPipelineBuilder pipelineBuilder(*args.fPaint, |
| 366 | args.fRenderTargetContext->mustUseHWAA(*args.fPaint)); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 367 | pipelineBuilder.setUserStencil(args.fUserStencilSettings); |
| 368 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 369 | args.fRenderTargetContext->drawBatch(pipelineBuilder, *args.fClip, batch.get()); |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 370 | |
| 371 | return true; |
| 372 | } |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 373 | |
| 374 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 375 | |
| 376 | #ifdef GR_TEST_UTILS |
| 377 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 378 | DRAW_BATCH_TEST_DEFINE(TesselatingPathBatch) { |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 379 | GrColor color = GrRandomColor(random); |
| 380 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 381 | SkPath path = GrTest::TestPath(random); |
bsalomon | d3030ac | 2016-09-01 07:20:29 -0700 | [diff] [blame] | 382 | SkIRect devClipBounds = SkIRect::MakeLTRB( |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 383 | random->nextU(), random->nextU(), random->nextU(), random->nextU()); |
bsalomon | d3030ac | 2016-09-01 07:20:29 -0700 | [diff] [blame] | 384 | devClipBounds.sort(); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 385 | bool antiAlias = random->nextBool(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 386 | GrStyle style; |
| 387 | do { |
| 388 | GrTest::TestStyle(random, &style); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 389 | } while (!style.isSimpleFill()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 390 | GrShape shape(path, style); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 391 | return TessellatingPathBatch::Create(color, shape, viewMatrix, devClipBounds, antiAlias); |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | #endif |