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: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 165 | DEFINE_OP_CLASS_ID |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 166 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 167 | static GrDrawOp* Create(const GrColor& color, |
| 168 | const GrShape& shape, |
| 169 | const SkMatrix& viewMatrix, |
| 170 | SkIRect devClipBounds, |
| 171 | bool antiAlias) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 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 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 177 | SkString dumpInfo() const override { |
| 178 | SkString string; |
| 179 | string.appendf("Color 0x%08x, aa: %d\n", fColor, fAntiAlias); |
| 180 | string.append(DumpPipelineInfo(*this->pipeline())); |
| 181 | string.append(INHERITED::dumpInfo()); |
| 182 | return string; |
| 183 | } |
| 184 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 185 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 186 | GrInitInvariantOutput* coverage, |
| 187 | GrBatchToXPOverrides* overrides) const override { |
| 188 | color->setKnownFourComponents(fColor); |
| 189 | coverage->setUnknownSingleComponent(); |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 190 | } |
| 191 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 192 | private: |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 193 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 194 | // Handle any color overrides |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 195 | if (!overrides.readsColor()) { |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 196 | fColor = GrColor_ILLEGAL; |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 197 | } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 198 | overrides.getOverrideColorIfSet(&fColor); |
| 199 | fPipelineInfo = overrides; |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 200 | } |
| 201 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 202 | SkPath getPath() const { |
| 203 | SkASSERT(!fShape.style().applies()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 204 | SkPath path; |
| 205 | fShape.asPath(&path); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 206 | return path; |
| 207 | } |
| 208 | |
| 209 | void draw(Target* target, const GrGeometryProcessor* gp) const { |
| 210 | SkASSERT(!fAntiAlias); |
| 211 | GrResourceProvider* rp = target->resourceProvider(); |
| 212 | bool inverseFill = fShape.inverseFilled(); |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 213 | // construct a cache key from the path's genID and the view matrix |
| 214 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 215 | GrUniqueKey key; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 216 | static constexpr int kClipBoundsCnt = sizeof(fDevClipBounds) / sizeof(uint32_t); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 217 | int shapeKeyDataCnt = fShape.unstyledKeySize(); |
| 218 | SkASSERT(shapeKeyDataCnt >= 0); |
| 219 | GrUniqueKey::Builder builder(&key, kDomain, shapeKeyDataCnt + kClipBoundsCnt); |
| 220 | fShape.writeUnstyledKey(&builder[0]); |
| 221 | // For inverse fills, the tessellation is dependent on clip bounds. |
| 222 | if (inverseFill) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 223 | memcpy(&builder[shapeKeyDataCnt], &fDevClipBounds, sizeof(fDevClipBounds)); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 224 | } else { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 225 | memset(&builder[shapeKeyDataCnt], 0, sizeof(fDevClipBounds)); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 226 | } |
| 227 | builder.finish(); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 228 | sk_sp<GrBuffer> cachedVertexBuffer(rp->findAndRefTByUniqueKey<GrBuffer>(key)); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 229 | int actualCount; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 230 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 231 | tol = GrPathUtils::scaleToleranceToSrc(tol, fViewMatrix, fShape.bounds()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 232 | if (cache_match(cachedVertexBuffer.get(), tol, &actualCount)) { |
| 233 | this->drawVertices(target, gp, cachedVertexBuffer.get(), 0, actualCount); |
| 234 | return; |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 235 | } |
| 236 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 237 | SkRect clipBounds = SkRect::Make(fDevClipBounds); |
| 238 | |
| 239 | SkMatrix vmi; |
| 240 | if (!fViewMatrix.invert(&vmi)) { |
| 241 | return; |
| 242 | } |
| 243 | vmi.mapRect(&clipBounds); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 244 | bool isLinear; |
| 245 | bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags(); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 246 | StaticVertexAllocator allocator(gp->getVertexStride(), rp, canMapVB); |
| 247 | int count = GrTessellator::PathToTriangles(getPath(), tol, clipBounds, &allocator, |
| 248 | false, GrColor(), false, &isLinear); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 249 | if (count == 0) { |
| 250 | return; |
| 251 | } |
senorblanco | af1e21e | 2016-03-16 10:25:58 -0700 | [diff] [blame] | 252 | this->drawVertices(target, gp, allocator.vertexBuffer(), 0, count); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 253 | TessInfo info; |
| 254 | info.fTolerance = isLinear ? 0 : tol; |
| 255 | info.fCount = count; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 256 | key.setCustomData(SkData::MakeWithCopy(&info, sizeof(info))); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 257 | rp->assignUniqueKeyToResource(key, allocator.vertexBuffer()); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 258 | } |
| 259 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 260 | void drawAA(Target* target, const GrGeometryProcessor* gp) const { |
| 261 | SkASSERT(fAntiAlias); |
| 262 | SkPath path = getPath(); |
| 263 | if (path.isEmpty()) { |
| 264 | return; |
| 265 | } |
| 266 | SkRect clipBounds = SkRect::Make(fDevClipBounds); |
| 267 | path.transform(fViewMatrix); |
| 268 | SkScalar tol = GrPathUtils::kDefaultTolerance; |
| 269 | bool isLinear; |
| 270 | DynamicVertexAllocator allocator(gp->getVertexStride(), target); |
| 271 | bool canTweakAlphaForCoverage = fPipelineInfo.canTweakAlphaForCoverage(); |
| 272 | int count = GrTessellator::PathToTriangles(path, tol, clipBounds, &allocator, |
| 273 | true, fColor, canTweakAlphaForCoverage, |
| 274 | &isLinear); |
| 275 | if (count == 0) { |
| 276 | return; |
| 277 | } |
| 278 | drawVertices(target, gp, allocator.vertexBuffer(), allocator.firstVertex(), count); |
| 279 | } |
| 280 | |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 281 | void onPrepareDraws(Target* target) const override { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 282 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 283 | { |
| 284 | using namespace GrDefaultGeoProcFactory; |
| 285 | |
| 286 | Color color(fColor); |
| 287 | LocalCoords localCoords(fPipelineInfo.readsLocalCoords() ? |
| 288 | LocalCoords::kUsePosition_Type : |
| 289 | LocalCoords::kUnused_Type); |
| 290 | Coverage::Type coverageType; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 291 | if (fAntiAlias) { |
| 292 | color = Color(Color::kAttribute_Type); |
| 293 | if (fPipelineInfo.canTweakAlphaForCoverage()) { |
| 294 | coverageType = Coverage::kSolid_Type; |
| 295 | } else { |
| 296 | coverageType = Coverage::kAttribute_Type; |
| 297 | } |
| 298 | } else if (fPipelineInfo.readsCoverage()) { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 299 | coverageType = Coverage::kSolid_Type; |
| 300 | } else { |
| 301 | coverageType = Coverage::kNone_Type; |
| 302 | } |
| 303 | Coverage coverage(coverageType); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 304 | if (fAntiAlias) { |
| 305 | gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(color, coverage, localCoords, |
| 306 | fViewMatrix); |
| 307 | } else { |
| 308 | gp = GrDefaultGeoProcFactory::Make(color, coverage, localCoords, fViewMatrix); |
| 309 | } |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 310 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 311 | if (fAntiAlias) { |
| 312 | this->drawAA(target, gp.get()); |
| 313 | } else { |
| 314 | this->draw(target, gp.get()); |
| 315 | } |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 316 | } |
senorblanco | 84cd621 | 2015-08-04 10:01:58 -0700 | [diff] [blame] | 317 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 318 | void drawVertices(Target* target, const GrGeometryProcessor* gp, const GrBuffer* vb, |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 319 | int firstVertex, int count) const { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 320 | GrPrimitiveType primitiveType = TESSELLATOR_WIREFRAME ? kLines_GrPrimitiveType |
| 321 | : kTriangles_GrPrimitiveType; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 322 | GrMesh mesh; |
| 323 | mesh.init(primitiveType, vb, firstVertex, count); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 324 | target->draw(gp, mesh); |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 327 | bool onCombineIfPossible(GrOp*, const GrCaps&) override { return false; } |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 328 | |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 329 | TessellatingPathBatch(const GrColor& color, |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 330 | const GrShape& shape, |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 331 | const SkMatrix& viewMatrix, |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 332 | const SkIRect& devClipBounds, |
| 333 | bool antiAlias) |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 334 | : INHERITED(ClassID()) |
| 335 | , fColor(color) |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 336 | , fShape(shape) |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 337 | , fViewMatrix(viewMatrix) |
| 338 | , fDevClipBounds(devClipBounds) |
| 339 | , fAntiAlias(antiAlias) { |
| 340 | SkRect devBounds; |
| 341 | viewMatrix.mapRect(&devBounds, shape.bounds()); |
| 342 | if (shape.inverseFilled()) { |
| 343 | // Because the clip bounds are used to add a contour for inverse fills, they must also |
| 344 | // include the path bounds. |
| 345 | devBounds.join(SkRect::Make(fDevClipBounds)); |
| 346 | } |
| 347 | this->setBounds(devBounds, HasAABloat::kNo, IsZeroArea::kNo); |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 348 | } |
| 349 | |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 350 | GrColor fColor; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 351 | GrShape fShape; |
bsalomon | 91d844d | 2015-08-10 10:47:29 -0700 | [diff] [blame] | 352 | SkMatrix fViewMatrix; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 353 | SkIRect fDevClipBounds; |
| 354 | bool fAntiAlias; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 355 | GrXPOverridesForBatch fPipelineInfo; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 356 | |
| 357 | typedef GrVertexBatch INHERITED; |
senorblanco | 9ba3972 | 2015-03-05 07:13:42 -0800 | [diff] [blame] | 358 | }; |
| 359 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 360 | bool GrTessellatingPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 361 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 362 | "GrTessellatingPathRenderer::onDrawPath"); |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 363 | SkIRect clipBoundsI; |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 364 | args.fClip->getConservativeBounds(args.fRenderTargetContext->worstCaseWidth(), |
| 365 | args.fRenderTargetContext->worstCaseHeight(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 366 | &clipBoundsI); |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 367 | sk_sp<GrDrawOp> batch(TessellatingPathBatch::Create(args.fPaint->getColor(), |
| 368 | *args.fShape, |
| 369 | *args.fViewMatrix, |
| 370 | clipBoundsI, |
| 371 | args.fAntiAlias)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 372 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 373 | GrPipelineBuilder pipelineBuilder(*args.fPaint, |
| 374 | args.fRenderTargetContext->mustUseHWAA(*args.fPaint)); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 375 | pipelineBuilder.setUserStencil(args.fUserStencilSettings); |
| 376 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 377 | args.fRenderTargetContext->drawBatch(pipelineBuilder, *args.fClip, batch.get()); |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 378 | |
| 379 | return true; |
| 380 | } |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 381 | |
| 382 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 383 | |
| 384 | #ifdef GR_TEST_UTILS |
| 385 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 386 | DRAW_BATCH_TEST_DEFINE(TesselatingPathBatch) { |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 387 | GrColor color = GrRandomColor(random); |
| 388 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 389 | SkPath path = GrTest::TestPath(random); |
bsalomon | d3030ac | 2016-09-01 07:20:29 -0700 | [diff] [blame] | 390 | SkIRect devClipBounds = SkIRect::MakeLTRB( |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 391 | random->nextU(), random->nextU(), random->nextU(), random->nextU()); |
bsalomon | d3030ac | 2016-09-01 07:20:29 -0700 | [diff] [blame] | 392 | devClipBounds.sort(); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 393 | bool antiAlias = random->nextBool(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 394 | GrStyle style; |
| 395 | do { |
| 396 | GrTest::TestStyle(random, &style); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 397 | } while (!style.isSimpleFill()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 398 | GrShape shape(path, style); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 399 | return TessellatingPathBatch::Create(color, shape, viewMatrix, devClipBounds, antiAlias); |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | #endif |