blob: eb5495ca08791375c3413405100482003b28287f [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 Salomon6a639042016-12-14 11:08:17 -050012#include "GrPipelineBuilder.h"
Brian Osman32342f02017-03-04 08:12:46 -050013#include "GrResourceProvider.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000014#include "GrSWMaskHelper.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050015#include "GrSurfaceContextPriv.h"
Brian Salomon89527432016-12-16 09:52:16 -050016#include "ops/GrRectOpFactory.h"
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000017
robertphillips@google.comed4155d2012-05-01 14:30:24 +000018////////////////////////////////////////////////////////////////////////////////
bsalomon0aff2fa2015-07-31 06:48:27 -070019bool GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomon8acedde2016-06-24 10:42:16 -070020 // Pass on any style that applies. The caller will apply the style if a suitable renderer is
21 // not found and try again with the new GrShape.
Brian Osman32342f02017-03-04 08:12:46 -050022 return !args.fShape->style().applies() && SkToBool(fResourceProvider) &&
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050023 (args.fAAType == GrAAType::kCoverage || args.fAAType == GrAAType::kNone);
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000024}
25
robertphillips@google.comed4155d2012-05-01 14:30:24 +000026////////////////////////////////////////////////////////////////////////////////
bsalomon39ef7fb2016-09-21 11:16:05 -070027static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix,
28 SkIRect* devBounds) {
29 SkRect shapeBounds = shape.styledBounds();
30 if (shapeBounds.isEmpty()) {
31 return false;
32 }
33 SkRect shapeDevBounds;
34 matrix.mapRect(&shapeDevBounds, shapeBounds);
Brian Salomonc1c607e2016-12-20 11:41:43 -050035 // Even though these are "unclipped" bounds we still clip to the int32_t range.
36 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
37 // would round down to this value when cast to a float, but who really cares.
38 // INT32_MIN is exactly representable.
39 static constexpr int32_t kMaxInt = 2147483520;
40 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
41 return false;
42 }
bsalomon39ef7fb2016-09-21 11:16:05 -070043 shapeDevBounds.roundOut(devBounds);
44 return true;
45}
46
47// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
48// is no intersection.
49static bool get_shape_and_clip_bounds(int width, int height,
50 const GrClip& clip,
51 const GrShape& shape,
52 const SkMatrix& matrix,
53 SkIRect* unclippedDevShapeBounds,
54 SkIRect* clippedDevShapeBounds,
55 SkIRect* devClipBounds) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000056 // compute bounds as intersection of rt size, clip, and path
robertphillips0152d732016-05-20 06:38:43 -070057 clip.getConservativeBounds(width, height, devClipBounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +000058
bsalomon39ef7fb2016-09-21 11:16:05 -070059 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
60 *unclippedDevShapeBounds = SkIRect::EmptyIRect();
61 *clippedDevShapeBounds = SkIRect::EmptyIRect();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000062 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000063 }
bsalomon39ef7fb2016-09-21 11:16:05 -070064 if (!clippedDevShapeBounds->intersect(*devClipBounds, *unclippedDevShapeBounds)) {
65 *clippedDevShapeBounds = SkIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000066 return false;
67 }
68 return true;
69}
70
71////////////////////////////////////////////////////////////////////////////////
robertphillips976f5f02016-06-03 10:59:20 -070072
Brian Osman11052242016-10-27 14:47:55 -040073void GrSoftwarePathRenderer::DrawNonAARect(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -050074 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070075 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070076 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -070077 const SkMatrix& viewMatrix,
78 const SkRect& rect,
79 const SkMatrix& localMatrix) {
Brian Salomonf8334782017-01-03 09:42:58 -050080 std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(paint.getColor(), viewMatrix, rect,
81 nullptr, &localMatrix));
robertphillips976f5f02016-06-03 10:59:20 -070082
Brian Salomon82f44312017-01-11 13:42:54 -050083 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
bsalomonbb243832016-07-22 07:10:19 -070084 pipelineBuilder.setUserStencil(&userStencilSettings);
Brian Salomon24f19782016-12-13 15:10:11 -050085 renderTargetContext->addDrawOp(pipelineBuilder, clip, std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -070086}
87
Brian Osman11052242016-10-27 14:47:55 -040088void GrSoftwarePathRenderer::DrawAroundInvPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -050089 GrPaint&& paint,
robertphillipsd2b6d642016-07-21 08:55:08 -070090 const GrUserStencilSettings& userStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070091 const GrClip& clip,
robertphillips976f5f02016-06-03 10:59:20 -070092 const SkMatrix& viewMatrix,
93 const SkIRect& devClipBounds,
94 const SkIRect& devPathBounds) {
joshualittd27f73e2014-12-29 07:43:36 -080095 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -080096 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +000097 return;
98 }
joshualittd27f73e2014-12-29 07:43:36 -080099
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000100 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000101 if (devClipBounds.fTop < devPathBounds.fTop) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000102 rect.iset(devClipBounds.fLeft, devClipBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000103 devClipBounds.fRight, devPathBounds.fTop);
Brian Salomon82f44312017-01-11 13:42:54 -0500104 DrawNonAARect(renderTargetContext, GrPaint(paint), userStencilSettings, clip, SkMatrix::I(),
105 rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000106 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000107 if (devClipBounds.fLeft < devPathBounds.fLeft) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108 rect.iset(devClipBounds.fLeft, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000109 devPathBounds.fLeft, devPathBounds.fBottom);
Brian Salomon82f44312017-01-11 13:42:54 -0500110 DrawNonAARect(renderTargetContext, GrPaint(paint), userStencilSettings, clip, SkMatrix::I(),
111 rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000112 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000113 if (devClipBounds.fRight > devPathBounds.fRight) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114 rect.iset(devPathBounds.fRight, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000115 devClipBounds.fRight, devPathBounds.fBottom);
Brian Salomon82f44312017-01-11 13:42:54 -0500116 DrawNonAARect(renderTargetContext, GrPaint(paint), userStencilSettings, clip, SkMatrix::I(),
117 rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000118 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000119 if (devClipBounds.fBottom > devPathBounds.fBottom) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120 rect.iset(devClipBounds.fLeft, devPathBounds.fBottom,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000121 devClipBounds.fRight, devClipBounds.fBottom);
Brian Salomon82f44312017-01-11 13:42:54 -0500122 DrawNonAARect(renderTargetContext, std::move(paint), userStencilSettings, clip,
robertphillips976f5f02016-06-03 10:59:20 -0700123 SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000124 }
125}
126
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000127////////////////////////////////////////////////////////////////////////////////
128// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700129bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400130 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700131 "GrSoftwarePathRenderer::onDrawPath");
Brian Osman32342f02017-03-04 08:12:46 -0500132 if (!fResourceProvider) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000133 return false;
134 }
135
bsalomon8acedde2016-06-24 10:42:16 -0700136 // We really need to know if the shape will be inverse filled or not
137 bool inverseFilled = false;
138 SkTLazy<GrShape> tmpShape;
caryclarkd6562002016-07-27 12:02:07 -0700139 SkASSERT(!args.fShape->style().applies());
bsalomon8acedde2016-06-24 10:42:16 -0700140 inverseFilled = args.fShape->inverseFilled();
141
bsalomon39ef7fb2016-09-21 11:16:05 -0700142 SkIRect unclippedDevShapeBounds, clippedDevShapeBounds, devClipBounds;
143 // To prevent overloading the cache with entries during animations we limit the cache of masks
144 // to cases where the matrix preserves axis alignment.
145 bool useCache = fAllowCaching && !inverseFilled && args.fViewMatrix->preservesAxisAlignment() &&
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500146 args.fShape->hasUnstyledKey() && GrAAType::kCoverage == args.fAAType;
bsalomon39ef7fb2016-09-21 11:16:05 -0700147
Brian Osman11052242016-10-27 14:47:55 -0400148 if (!get_shape_and_clip_bounds(args.fRenderTargetContext->width(),
149 args.fRenderTargetContext->height(),
bsalomon8acedde2016-06-24 10:42:16 -0700150 *args.fClip, *args.fShape,
bsalomon39ef7fb2016-09-21 11:16:05 -0700151 *args.fViewMatrix, &unclippedDevShapeBounds,
152 &clippedDevShapeBounds,
153 &devClipBounds)) {
bsalomon8acedde2016-06-24 10:42:16 -0700154 if (inverseFilled) {
Brian Salomon82f44312017-01-11 13:42:54 -0500155 DrawAroundInvPath(args.fRenderTargetContext, std::move(args.fPaint),
156 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
157 devClipBounds, unclippedDevShapeBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000158 }
159 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000160 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000161
bsalomon39ef7fb2016-09-21 11:16:05 -0700162 const SkIRect* boundsForMask = &clippedDevShapeBounds;
163 if (useCache) {
164 // Use the cache only if >50% of the path is visible.
165 int unclippedWidth = unclippedDevShapeBounds.width();
166 int unclippedHeight = unclippedDevShapeBounds.height();
167 int unclippedArea = unclippedWidth * unclippedHeight;
168 int clippedArea = clippedDevShapeBounds.width() * clippedDevShapeBounds.height();
Brian Osman11052242016-10-27 14:47:55 -0400169 int maxTextureSize = args.fRenderTargetContext->caps()->maxTextureSize();
bsalomon39ef7fb2016-09-21 11:16:05 -0700170 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
171 unclippedHeight > maxTextureSize) {
172 useCache = false;
173 } else {
174 boundsForMask = &unclippedDevShapeBounds;
175 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000176 }
177
bsalomon39ef7fb2016-09-21 11:16:05 -0700178 GrUniqueKey maskKey;
179 struct KeyData {
180 SkScalar fFractionalTranslateX;
181 SkScalar fFractionalTranslateY;
182 };
183
184 if (useCache) {
185 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
186 SkScalar sx = args.fViewMatrix->get(SkMatrix::kMScaleX);
187 SkScalar sy = args.fViewMatrix->get(SkMatrix::kMScaleY);
188 SkScalar kx = args.fViewMatrix->get(SkMatrix::kMSkewX);
189 SkScalar ky = args.fViewMatrix->get(SkMatrix::kMSkewY);
190 SkScalar tx = args.fViewMatrix->get(SkMatrix::kMTransX);
191 SkScalar ty = args.fViewMatrix->get(SkMatrix::kMTransY);
192 // Allow 8 bits each in x and y of subpixel positioning.
193 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
194 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
195 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
196 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + args.fShape->unstyledKeySize());
197 builder[0] = SkFloat2Bits(sx);
198 builder[1] = SkFloat2Bits(sy);
199 builder[2] = SkFloat2Bits(kx);
200 builder[3] = SkFloat2Bits(ky);
201 builder[4] = fracX | (fracY >> 8);
202 args.fShape->writeUnstyledKey(&builder[5]);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500203 // FIXME: Doesn't the key need to consider whether we're using AA or not? In practice that
204 // should always be true, though.
bsalomon39ef7fb2016-09-21 11:16:05 -0700205 }
206
207 sk_sp<GrTexture> texture;
208 if (useCache) {
Brian Osman32342f02017-03-04 08:12:46 -0500209 texture.reset(fResourceProvider->findAndRefTextureByUniqueKey(maskKey));
bsalomon39ef7fb2016-09-21 11:16:05 -0700210 }
211 if (!texture) {
Robert Phillips417b7f42016-12-14 09:12:13 -0500212 SkBackingFit fit = useCache ? SkBackingFit::kExact : SkBackingFit::kApprox;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500213 GrAA aa = GrAAType::kCoverage == args.fAAType ? GrAA::kYes : GrAA::kNo;
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500214 GrContext* context = args.fRenderTargetContext->surfPriv().getContext();
215 texture = GrSWMaskHelper::DrawShapeMaskToTexture(context, *args.fShape,
216 *boundsForMask, aa,
217 fit, args.fViewMatrix);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500218 if (!texture) {
219 return false;
220 }
221 if (useCache) {
Brian Osman32342f02017-03-04 08:12:46 -0500222 fResourceProvider->assignUniqueKeyToTexture(maskKey, texture.get());
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500223 }
bsalomon39ef7fb2016-09-21 11:16:05 -0700224 }
bsalomon8acedde2016-06-24 10:42:16 -0700225 if (inverseFilled) {
Brian Salomon82f44312017-01-11 13:42:54 -0500226 DrawAroundInvPath(args.fRenderTargetContext, GrPaint(args.fPaint),
227 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix, devClipBounds,
228 unclippedDevShapeBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000229 }
Brian Salomon82f44312017-01-11 13:42:54 -0500230 GrSWMaskHelper::DrawToTargetWithShapeMask(
231 texture.get(), args.fRenderTargetContext, std::move(args.fPaint),
232 *args.fUserStencilSettings, *args.fClip, *args.fViewMatrix,
233 SkIPoint{boundsForMask->fLeft, boundsForMask->fTop}, *boundsForMask);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000234
235 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000236}