blob: 85fa43ee190eea90df6e63e85e2e8414aa10ea00 [file] [log] [blame]
Chris Daltonb832ce62020-01-06 19:49:37 -07001/*
2 * Copyright 2019 Google LLC.
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
Chris Dalton0a22b1e2020-03-26 11:52:15 -06008#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Chris Daltonb832ce62020-01-06 19:49:37 -07009
Chris Daltond2dc8dd2020-05-19 16:32:02 -060010#include "src/core/SkIPoint16.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070011#include "src/core/SkPathPriv.h"
12#include "src/gpu/GrClip.h"
13#include "src/gpu/GrMemoryPool.h"
14#include "src/gpu/GrRecordingContextPriv.h"
15#include "src/gpu/GrRenderTargetContext.h"
Chris Daltonc3b67eb2020-02-10 21:09:58 -070016#include "src/gpu/GrSurfaceContextPriv.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000017#include "src/gpu/geometry/GrStyledShape.h"
Chris Daltonc3b67eb2020-02-10 21:09:58 -070018#include "src/gpu/ops/GrFillRectOp.h"
Chris Dalton4e998532020-02-10 11:06:42 -070019#include "src/gpu/tessellate/GrDrawAtlasPathOp.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070020#include "src/gpu/tessellate/GrTessellatePathOp.h"
21
Chris Dalton4e998532020-02-10 11:06:42 -070022constexpr static SkISize kAtlasInitialSize{512, 512};
23constexpr static int kMaxAtlasSize = 2048;
24
Chris Daltond2dc8dd2020-05-19 16:32:02 -060025// The atlas is only used for small-area paths, which means at least one dimension of every path is
26// guaranteed to be quite small. So if we transpose tall paths, then every path will have a small
27// height, which lends very well to efficient pow2 atlas packing.
28constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2;
29
30// Ensure every path in the atlas falls in or below the 128px high rectanizer band.
31constexpr static int kMaxAtlasPathHeight = 128;
32
Chris Dalton0a22b1e2020-03-26 11:52:15 -060033GrTessellationPathRenderer::GrTessellationPathRenderer(const GrCaps& caps) : fAtlas(
Chris Dalton4e998532020-02-10 11:06:42 -070034 GrColorType::kAlpha_8, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize,
Chris Daltond2dc8dd2020-05-19 16:32:02 -060035 std::min(kMaxAtlasSize, caps.maxPreferredRenderTargetSize()), caps, kAtlasAlgorithm) {
Chris Dalton4e998532020-02-10 11:06:42 -070036}
37
Chris Dalton0a22b1e2020-03-26 11:52:15 -060038GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -070039 const CanDrawPathArgs& args) const {
40 // This class should not have been added to the chain without tessellation support.
41 SkASSERT(args.fCaps->shaderCaps()->tessellationSupport());
Chris Dalton0f6bb8a2020-01-15 09:40:54 -070042 if (!args.fShape->style().isSimpleFill() || args.fShape->inverseFilled() ||
43 args.fViewMatrix->hasPerspective()) {
Chris Daltonb832ce62020-01-06 19:49:37 -070044 return CanDrawPath::kNo;
45 }
46 if (GrAAType::kCoverage == args.fAAType) {
47 SkASSERT(1 == args.fProxy->numSamples());
48 if (!args.fProxy->canUseMixedSamples(*args.fCaps)) {
49 return CanDrawPath::kNo;
50 }
51 }
52 SkPath path;
53 args.fShape->asPath(&path);
54 if (SkPathPriv::ConicWeightCnt(path)) {
55 return CanDrawPath::kNo;
56 }
57 return CanDrawPath::kYes;
58}
59
Chris Dalton0a22b1e2020-03-26 11:52:15 -060060bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
Chris Dalton4e998532020-02-10 11:06:42 -070061 GrRenderTargetContext* renderTargetContext = args.fRenderTargetContext;
62 GrOpMemoryPool* pool = args.fContext->priv().opMemoryPool();
Chris Daltonb832ce62020-01-06 19:49:37 -070063 SkPath path;
64 args.fShape->asPath(&path);
65
Chris Dalton4e998532020-02-10 11:06:42 -070066 // See if the path is small and simple enough to atlas instead of drawing directly.
67 //
68 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
69 // render the sample mask to an integer texture, but such a scheme would probably require
70 // GL_EXT_post_depth_coverage, which appears to have low adoption.
71 SkIRect devIBounds;
Chris Daltond2dc8dd2020-05-19 16:32:02 -060072 SkIPoint16 locationInAtlas;
73 bool transposedInAtlas;
Chris Dalton4e998532020-02-10 11:06:42 -070074 if (this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, path,
Chris Daltond2dc8dd2020-05-19 16:32:02 -060075 args.fAAType, &devIBounds, &locationInAtlas, &transposedInAtlas)) {
Chris Dalton4e998532020-02-10 11:06:42 -070076 auto op = pool->allocate<GrDrawAtlasPathOp>(
77 renderTargetContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()),
Chris Daltond2dc8dd2020-05-19 16:32:02 -060078 devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix,
79 std::move(args.fPaint)); renderTargetContext->addDrawOp(*args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -070080 return true;
81 }
Chris Daltonb832ce62020-01-06 19:49:37 -070082
Chris Dalton4e998532020-02-10 11:06:42 -070083 auto op = pool->allocate<GrTessellatePathOp>(
84 *args.fViewMatrix, path, std::move(args.fPaint), args.fAAType);
85 renderTargetContext->addDrawOp(*args.fClip, std::move(op));
86 return true;
87}
88
Chris Dalton0a22b1e2020-03-26 11:52:15 -060089bool GrTessellationPathRenderer::tryAddPathToAtlas(
Chris Dalton4e998532020-02-10 11:06:42 -070090 const GrCaps& caps, const SkMatrix& viewMatrix, const SkPath& path, GrAAType aaType,
Chris Daltond2dc8dd2020-05-19 16:32:02 -060091 SkIRect* devIBounds, SkIPoint16* locationInAtlas, bool* transposedInAtlas) {
Chris Dalton4e998532020-02-10 11:06:42 -070092 if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) {
93 return false;
94 }
95
Chris Daltond2dc8dd2020-05-19 16:32:02 -060096 // Atlas paths require their points to be transformed on the CPU and copied into an "uber path".
97 // Check if this path has too many points to justify this extra work.
98 if (path.countPoints() > 200) {
Chris Dalton4e998532020-02-10 11:06:42 -070099 return false;
100 }
101
Chris Dalton4e998532020-02-10 11:06:42 -0700102 SkRect devBounds;
103 viewMatrix.mapRect(&devBounds, path.getBounds());
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600104 devBounds.roundOut(devIBounds);
105
106 // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this
107 // guarantees that every atlas entry has a small height, which lends very well to efficient pow2
108 // atlas packing.
109 int maxDimenstion = devIBounds->width();
110 int minDimension = devIBounds->height();
111 *transposedInAtlas = minDimension > maxDimenstion;
112 if (*transposedInAtlas) {
113 std::swap(minDimension, maxDimenstion);
114 }
115
116 // Check if the path is too large for an atlas. Since we use "minDimension" for height in the
117 // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight.
118 if (maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight ||
119 maxDimenstion > kMaxAtlasSize / 2) {
Chris Dalton4e998532020-02-10 11:06:42 -0700120 return false;
121 }
122
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600123 if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) {
Chris Dalton4e998532020-02-10 11:06:42 -0700124 return false;
125 }
126
127 SkMatrix atlasMatrix = viewMatrix;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600128 if (*transposedInAtlas) {
129 std::swap(atlasMatrix[0], atlasMatrix[3]);
130 std::swap(atlasMatrix[1], atlasMatrix[4]);
131 float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY();
132 atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x());
133 atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y());
134 } else {
135 atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(),
136 locationInAtlas->y() - devIBounds->y());
137 }
Chris Dalton4e998532020-02-10 11:06:42 -0700138
139 // Concatenate this path onto our uber path that matches its fill and AA types.
140 SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600141 uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0).
Chris Dalton4e998532020-02-10 11:06:42 -0700142 uberPath->addPath(path, atlasMatrix);
Chris Daltonb832ce62020-01-06 19:49:37 -0700143 return true;
144}
145
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600146void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700147 SkPath path;
148 args.fShape->asPath(&path);
149
150 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
151
Chris Daltonf9aea7f2020-01-21 11:19:26 -0700152 auto op = args.fContext->priv().opMemoryPool()->allocate<GrTessellatePathOp>(
153 *args.fViewMatrix, path, GrPaint(), aaType, GrTessellatePathOp::Flags::kStencilOnly);
154 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
Chris Daltonb832ce62020-01-06 19:49:37 -0700155}
Chris Dalton4e998532020-02-10 11:06:42 -0700156
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600157void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
158 const uint32_t* opsTaskIDs, int numOpsTaskIDs) {
Chris Dalton4e998532020-02-10 11:06:42 -0700159 if (!fAtlas.drawBounds().isEmpty()) {
160 this->renderAtlas(onFlushRP);
161 fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps());
162 }
163 for (SkPath& path : fAtlasUberPaths) {
164 path.reset();
165 }
166}
167
168constexpr static GrUserStencilSettings kTestStencil(
169 GrUserStencilSettings::StaticInit<
170 0x0000,
171 GrUserStencilTest::kNotEqual,
172 0xffff,
173 GrUserStencilOp::kKeep,
174 GrUserStencilOp::kKeep,
175 0xffff>());
176
177constexpr static GrUserStencilSettings kTestAndResetStencil(
178 GrUserStencilSettings::StaticInit<
179 0x0000,
180 GrUserStencilTest::kNotEqual,
181 0xffff,
182 GrUserStencilOp::kZero,
183 GrUserStencilOp::kKeep,
184 0xffff>());
185
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600186void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton4e998532020-02-10 11:06:42 -0700187 auto rtc = fAtlas.instantiate(onFlushRP);
188 if (!rtc) {
189 return;
190 }
191
192 // Add ops to stencil the atlas paths.
193 for (auto antialias : {false, true}) {
194 for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) {
195 SkPath* uberPath = this->getAtlasUberPath(fillType, antialias);
196 if (uberPath->isEmpty()) {
197 continue;
198 }
199 uberPath->setFillType(fillType);
200 GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone;
201 auto op = onFlushRP->opMemoryPool()->allocate<GrTessellatePathOp>(
202 SkMatrix::I(), *uberPath, GrPaint(), aaType,
203 GrTessellatePathOp::Flags::kStencilOnly);
204 rtc->addDrawOp(GrNoClip(), std::move(op));
205 }
206 }
207
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700208 // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks.
209 auto fillRectFlags = GrFillRectOp::InputFlags::kNone;
Chris Dalton4e998532020-02-10 11:06:42 -0700210
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700211 // This will be the final op in the renderTargetContext. So if Ganesh is planning to discard the
212 // stencil values anyway, then we might not actually need to reset the stencil values back to 0.
213 bool mustResetStencil = !onFlushRP->caps()->discardStencilValuesAfterRenderPass();
214
215 if (rtc->numSamples() <= 1) {
216 // We are mixed sampled. We need to enable conservative raster and ensure stencil values get
217 // reset in order to avoid artifacts along the diagonal of the atlas.
218 fillRectFlags |= GrFillRectOp::InputFlags::kConservativeRaster;
219 mustResetStencil = true;
220 }
221
222 SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height());
223 const GrUserStencilSettings* stencil;
224 if (mustResetStencil) {
225 // Outset the cover rect in case there are T-junctions in the path bounds.
226 coverRect.outset(1, 1);
227 stencil = &kTestAndResetStencil;
228 } else {
229 stencil = &kTestStencil;
230 }
231
232 GrQuad coverQuad(coverRect);
233 DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll};
234
Chris Dalton4e998532020-02-10 11:06:42 -0700235 GrPaint paint;
236 paint.setColor4f(SK_PMColor4fWHITE);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700237
238 auto coverOp = GrFillRectOp::Make(rtc->surfPriv().getContext(), std::move(paint),
239 GrAAType::kMSAA, &drawQuad, stencil, fillRectFlags);
240 rtc->addDrawOp(GrNoClip(), std::move(coverOp));
Chris Dalton4e998532020-02-10 11:06:42 -0700241
242 if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) {
243 onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()),
244 GrSurfaceProxy::ResolveFlags::kMSAA);
245 }
246}