blob: e171b78b4a37f9bb5bce24338d5c875622d07cbf [file] [log] [blame]
robertphillips@google.comf4c2c522012-04-27 12:08:47 +00001/*
2 * Copyright 2012 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 "GrSoftwarePathRenderer.h"
robertphillips976f5f02016-06-03 10:59:20 -07009#include "GrAuditTrail.h"
10#include "GrClip.h"
bsalomon39ef7fb2016-09-21 11:16:05 -070011#include "GrGpuResourcePriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050012#include "GrResourceProvider.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000013#include "GrSWMaskHelper.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040014#include "ops/GrDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040015#include "ops/GrRectOpFactory.h"
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000016
robertphillips@google.comed4155d2012-05-01 14:30:24 +000017////////////////////////////////////////////////////////////////////////////////
bsalomon0aff2fa2015-07-31 06:48:27 -070018bool GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomon8acedde2016-06-24 10:42:16 -070019 // Pass on any style that applies. The caller will apply the style if a suitable renderer is
20 // not found and try again with the new GrShape.
Brian Osman32342f02017-03-04 08:12:46 -050021 return !args.fShape->style().applies() && SkToBool(fResourceProvider) &&
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050022 (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::kNone);
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000023}
24
robertphillips@google.comed4155d2012-05-01 14:30:24 +000025////////////////////////////////////////////////////////////////////////////////
bsalomon39ef7fb2016-09-21 11:16:05 -070026static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix,
27 SkIRect* devBounds) {
28 SkRect shapeBounds = shape.styledBounds();
29 if (shapeBounds.isEmpty()) {
30 return false;
31 }
32 SkRect shapeDevBounds;
33 matrix.mapRect(&shapeDevBounds, shapeBounds);
Brian Salomonc1c607e2016-12-20 11:41:43 -050034 // Even though these are "unclipped" bounds we still clip to the int32_t range.
35 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
36 // would round down to this value when cast to a float, but who really cares.
37 // INT32_MIN is exactly representable.
38 static constexpr int32_t kMaxInt = 2147483520;
39 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
40 return false;
41 }
bsalomon39ef7fb2016-09-21 11:16:05 -070042 shapeDevBounds.roundOut(devBounds);
43 return true;
44}
45
46// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
47// is no intersection.
48static bool get_shape_and_clip_bounds(int width, int height,
49 const GrClip& clip,
50 const GrShape& shape,
51 const SkMatrix& matrix,
52 SkIRect* unclippedDevShapeBounds,
53 SkIRect* clippedDevShapeBounds,
54 SkIRect* devClipBounds) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000055 // compute bounds as intersection of rt size, clip, and path
robertphillips0152d732016-05-20 06:38:43 -070056 clip.getConservativeBounds(width, height, devClipBounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +000057
bsalomon39ef7fb2016-09-21 11:16:05 -070058 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
59 *unclippedDevShapeBounds = SkIRect::EmptyIRect();
60 *clippedDevShapeBounds = SkIRect::EmptyIRect();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000061 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000062 }
bsalomon39ef7fb2016-09-21 11:16:05 -070063 if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) {
64 *clippedDevShapeBounds = SkIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000065 return false;
66 }
67 return true;
68}
69
70////////////////////////////////////////////////////////////////////////////////
robertphillips976f5f02016-06-03 10:59:20 -070071
Brian Osman11052242016-10-27 14:47:55 -040072void GrSoftwarePathRenderer::DrawNonAARect(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -050073 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070074 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070075 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -070076 const SkMatrix& viewMatrix,
77 const SkRect& rect,
78 const SkMatrix& localMatrix) {
Brian Salomonbaaf4392017-06-15 09:59:23 -040079 renderTargetContext->addDrawOp(clip,
80 GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
81 std::move(paint), viewMatrix, localMatrix, rect,
82 GrAAType::kNone, &userStencilSettings));
robertphillips976f5f02016-06-03 10:59:20 -070083}
84
Brian Osman11052242016-10-27 14:47:55 -040085void GrSoftwarePathRenderer::DrawAroundInvPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -050086 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070087 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070088 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -070089 const SkMatrix& viewMatrix,
90 const SkIRect& devClipBounds,
91 const SkIRect& devPathBounds) {
joshualittd27f73e2014-12-29 07:43:36 -080092 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -080093 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +000094 return;
95 }
joshualittd27f73e2014-12-29 07:43:36 -080096
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000097 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +000098 if (devClipBounds.fTop < devPathBounds.fTop) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000099 rect.iset(devClipBounds.fLeft, devClipBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000100 devClipBounds.fRight, devPathBounds.fTop);
Brian Salomon82f44312017-01-11 13:42:54 -0500101 DrawNonAARect(renderTargetContext, GrPaint(paint), userStencilSettings, clip, SkMatrix::I(),
102 rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000103 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000104 if (devClipBounds.fLeft < devPathBounds.fLeft) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105 rect.iset(devClipBounds.fLeft, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000106 devPathBounds.fLeft, devPathBounds.fBottom);
Brian Salomon82f44312017-01-11 13:42:54 -0500107 DrawNonAARect(renderTargetContext, GrPaint(paint), userStencilSettings, clip, SkMatrix::I(),
108 rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000109 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000110 if (devClipBounds.fRight > devPathBounds.fRight) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111 rect.iset(devPathBounds.fRight, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000112 devClipBounds.fRight, devPathBounds.fBottom);
Brian Salomon82f44312017-01-11 13:42:54 -0500113 DrawNonAARect(renderTargetContext, GrPaint(paint), userStencilSettings, clip, SkMatrix::I(),
114 rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000115 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000116 if (devClipBounds.fBottom > devPathBounds.fBottom) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117 rect.iset(devClipBounds.fLeft, devPathBounds.fBottom,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000118 devClipBounds.fRight, devClipBounds.fBottom);
Brian Salomon82f44312017-01-11 13:42:54 -0500119 DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip,
robertphillips976f5f02016-06-03 10:59:20 -0700120 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000121 }
122}
123
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000124////////////////////////////////////////////////////////////////////////////////
125// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700126bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400127 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700128 "GrSoftwarePathRenderer::onDrawPath");
Brian Osman32342f02017-03-04 08:12:46 -0500129 if (!fResourceProvider) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000130 return false;
131 }
132
bsalomon8acedde2016-06-24 10:42:16 -0700133 // We really need to know if the shape will be inverse filled or not
134 bool inverseFilled = false;
135 SkTLazy<GrShape> tmpShape;
caryclarkd6562002016-07-27 12:02:07 -0700136 SkASSERT(!args.fShape->style().applies());
Eric Karl5c779752017-05-08 12:02:07 -0700137 // If the path is hairline, ignore inverse fill.
138 inverseFilled = args.fShape->inverseFilled() &&
139 !IsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr);
bsalomon8acedde2016-06-24 10:42:16 -0700140
bsalomon39ef7fb2016-09-21 11:16:05 -0700141 SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds;
142 // To prevent overloading the cache with entries during animations we limit the cache of masks
143 // to cases where the matrix preserves axis alignment.
144 bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() &&
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500145 args.fShape->hasUnstyledKey() && GrAAType::kCoverage == args.fAAType;
bsalomon39ef7fb2016-09-21 11:16:05 -0700146
Brian Osman11052242016-10-27 14:47:55 -0400147 if (!get_shape_and_clip_bounds(args.fRenderTargetContext->width(),
148 args.fRenderTargetContext->height(),
bsalomon8acedde2016-06-24 10:42:16 -0700149 *args.fClip, *args.fShape,
bsalomon39ef7fb2016-09-21 11:16:05 -0700150 *args.fViewMatrix, &unclippedDevShapeBounds,
151 &clippedDevShapeBounds,
152 &devClipBounds)) {
bsalomon8acedde2016-06-24 10:42:16 -0700153 if (inverseFilled) {
Brian Salomon82f44312017-01-11 13:42:54 -0500154 DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint),
155 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
156 devClipBounds, unclippedDevShapeBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000157 }
158 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000159 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000160
bsalomon39ef7fb2016-09-21 11:16:05 -0700161 const SkIRect* boundsForMask = &clippedDevShapeBounds;
162 if (useCache) {
163 // Use the cache only if >50% of the path is visible.
164 int unclippedWidth = unclippedDevShapeBounds.width();
165 int unclippedHeight = unclippedDevShapeBounds.height();
166 int unclippedArea = unclippedWidth * unclippedHeight;
167 int clippedArea = clippedDevShapeBounds.width() * clippedDevShapeBounds.height();
Brian Osman11052242016-10-27 14:47:55 -0400168 int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize();
bsalomon39ef7fb2016-09-21 11:16:05 -0700169 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
170 unclippedHeight > maxTextureSize) {
171 useCache = false;
172 } else {
173 boundsForMask = &unclippedDevShapeBounds;
174 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000175 }
176
bsalomon39ef7fb2016-09-21 11:16:05 -0700177 GrUniqueKey maskKey;
178 struct KeyData {
179 SkScalar fFractionalTranslateX;
180 SkScalar fFractionalTranslateY;
181 };
182
183 if (useCache) {
184 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
185 SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX);
186 SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY);
187 SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX);
188 SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY);
189 SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX);
190 SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY);
191 // Allow 8 bits each in x and y of subpixel positioning.
192 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
193 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
194 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
195 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + args.fShape->unstyledKeySize());
196 builder[0] = SkFloat2Bits(sx);
197 builder[1] = SkFloat2Bits(sy);
198 builder[2] = SkFloat2Bits(kx);
199 builder[3] = SkFloat2Bits(ky);
200 builder[4] = fracX | (fracY >> 8);
201 args.fShape->writeUnstyledKey(&builder[5]);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500202 // FIXME: Doesn't the key need to consider whether we're using AA or not? In practice that
203 // should always be true, though.
bsalomon39ef7fb2016-09-21 11:16:05 -0700204 }
205
Robert Phillipsd3749482017-03-14 09:17:43 -0400206 sk_sp<GrTextureProxy> proxy;
bsalomon39ef7fb2016-09-21 11:16:05 -0700207 if (useCache) {
Robert Phillips96be9df2017-07-21 14:17:45 +0000208 proxy = fResourceProvider->findProxyByUniqueKey(maskKey);
bsalomon39ef7fb2016-09-21 11:16:05 -0700209 }
Robert Phillipsd3749482017-03-14 09:17:43 -0400210 if (!proxy) {
Robert Phillips417b7f42016-12-14 09:12:13 -0500211 SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500212 GrAA aa = GrAAType::kCoverage == args.fAAType ? GrAA::kYes : GrAA::kNo;
Robert Phillips26c90e02017-03-14 14:39:29 -0400213 proxy = GrSWMaskHelper::DrawShapeMaskToTexture(args.fContext, *args.fShape,
Robert Phillipsd3749482017-03-14 09:17:43 -0400214 *boundsForMask, aa,
215 fit, args.fViewMatrix);
216 if (!proxy) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500217 return false;
218 }
219 if (useCache) {
Robert Phillipsd3749482017-03-14 09:17:43 -0400220 fResourceProvider->assignUniqueKeyToProxy(maskKey, proxy.get());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500221 }
bsalomon39ef7fb2016-09-21 11:16:05 -0700222 }
bsalomon8acedde2016-06-24 10:42:16 -0700223 if (inverseFilled) {
Brian Salomon82f44312017-01-11 13:42:54 -0500224 DrawAroundInvPath(args.fRenderTargetContext, GrPaint(args.fPaint),
225 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, devClipBounds,
226 unclippedDevShapeBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000227 }
Brian Salomon82f44312017-01-11 13:42:54 -0500228 GrSWMaskHelper::DrawToTargetWithShapeMask(
Robert Phillips296b1cc2017-03-15 10:42:12 -0400229 std::move(proxy), args.fRenderTargetContext, std::move(args.fPaint),
Brian Salomon82f44312017-01-11 13:42:54 -0500230 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
231 SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000232
233 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000234}