blob: af77c205b528b28c8808b22d0a6eec2bfc07091e [file] [log] [blame]
robertphillips@google.comf4c2c522012-04-27 12:08:47 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrSoftwarePathRenderer.h"
robertphillips@google.comed4155d2012-05-01 14:30:24 +000010#include "GrContext.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000011#include "GrSWMaskHelper.h"
bsalomon72e3ae42015-04-28 08:08:46 -070012#include "GrVertexBuffer.h"
joshualitt04194f32016-01-13 10:08:27 -080013#include "batches/GrRectBatchFactory.h"
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000014
robertphillips@google.comed4155d2012-05-01 14:30:24 +000015////////////////////////////////////////////////////////////////////////////////
bsalomon0aff2fa2015-07-31 06:48:27 -070016bool GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
halcanary96fcdcc2015-08-27 07:41:13 -070017 if (nullptr == fContext) {
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000018 return false;
19 }
bsalomon0aff2fa2015-07-31 06:48:27 -070020 if (args.fStroke->isDashed()) {
kkinnunen18996512015-04-26 23:18:49 -070021 return false;
22 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +000023 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000024}
25
robertphillips@google.comed4155d2012-05-01 14:30:24 +000026namespace {
27
28////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000029// gets device coord bounds of path (not considering the fill) and clip. The
rmistry@google.comd6176b02012-08-23 18:14:13 +000030// path bounds will be a subset of the clip bounds. returns false if
robertphillips@google.comed4155d2012-05-01 14:30:24 +000031// path bounds would be empty.
robertphillipsb83bec52015-10-23 09:38:03 -070032bool get_path_and_clip_bounds(const GrPipelineBuilder* pipelineBuilder,
robertphillips@google.comed4155d2012-05-01 14:30:24 +000033 const SkPath& path,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000034 const SkMatrix& matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000035 SkIRect* devPathBounds,
36 SkIRect* devClipBounds) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000037 // compute bounds as intersection of rt size, clip, and path
egdaniel8dd688b2015-01-22 10:16:09 -080038 const GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
halcanary96fcdcc2015-08-27 07:41:13 -070039 if (nullptr == rt) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000040 return false;
41 }
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000042
robertphillips7bceedc2015-12-01 12:51:26 -080043 pipelineBuilder->clip().getConservativeBounds(rt->width(), rt->height(), devClipBounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +000044
robertphillipse85a32d2015-02-10 08:16:55 -080045 if (devClipBounds->isEmpty()) {
46 *devPathBounds = SkIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000047 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000048 }
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000049
robertphillips@google.com366f1c62012-06-29 21:38:47 +000050 if (!path.getBounds().isEmpty()) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000051 SkRect pathSBounds;
robertphillips@google.com366f1c62012-06-29 21:38:47 +000052 matrix.mapRect(&pathSBounds, path.getBounds());
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000053 SkIRect pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000054 pathSBounds.roundOut(&pathIBounds);
robertphillipse85a32d2015-02-10 08:16:55 -080055 *devPathBounds = *devClipBounds;
robertphillips@google.com7b112892012-07-31 15:18:21 +000056 if (!devPathBounds->intersect(pathIBounds)) {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +000057 // set the correct path bounds, as this would be used later.
robertphillips@google.com7b112892012-07-31 15:18:21 +000058 *devPathBounds = pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000059 return false;
60 }
61 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000062 *devPathBounds = SkIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000063 return false;
64 }
65 return true;
66}
67
68////////////////////////////////////////////////////////////////////////////////
joshualitt04194f32016-01-13 10:08:27 -080069static void draw_non_aa_rect(GrDrawTarget* drawTarget,
70 const GrPipelineBuilder& pipelineBuilder,
71 GrColor color,
72 const SkMatrix& viewMatrix,
73 const SkRect& rect,
74 const SkMatrix& localMatrix) {
75 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
76 nullptr, &localMatrix));
77 drawTarget->drawBatch(pipelineBuilder, batch);
78}
79
robertphillips@google.comed4155d2012-05-01 14:30:24 +000080void draw_around_inv_path(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080081 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -080082 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -080083 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000084 const SkIRect& devClipBounds,
85 const SkIRect& devPathBounds) {
joshualittd27f73e2014-12-29 07:43:36 -080086 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -080087 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +000088 return;
89 }
joshualittd27f73e2014-12-29 07:43:36 -080090
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000091 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +000092 if (devClipBounds.fTop < devPathBounds.fTop) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000093 rect.iset(devClipBounds.fLeft, devClipBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000094 devClipBounds.fRight, devPathBounds.fTop);
joshualitt04194f32016-01-13 10:08:27 -080095 draw_non_aa_rect(target, *pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000096 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000097 if (devClipBounds.fLeft < devPathBounds.fLeft) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000098 rect.iset(devClipBounds.fLeft, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000099 devPathBounds.fLeft, devPathBounds.fBottom);
joshualitt04194f32016-01-13 10:08:27 -0800100 draw_non_aa_rect(target, *pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000101 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000102 if (devClipBounds.fRight > devPathBounds.fRight) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103 rect.iset(devPathBounds.fRight, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000104 devClipBounds.fRight, devPathBounds.fBottom);
joshualitt04194f32016-01-13 10:08:27 -0800105 draw_non_aa_rect(target, *pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000106 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000107 if (devClipBounds.fBottom > devPathBounds.fBottom) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108 rect.iset(devClipBounds.fLeft, devPathBounds.fBottom,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000109 devClipBounds.fRight, devClipBounds.fBottom);
joshualitt04194f32016-01-13 10:08:27 -0800110 draw_non_aa_rect(target, *pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000111 }
112}
113
114}
115
116////////////////////////////////////////////////////////////////////////////////
117// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700118bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
joshualittde83b412016-01-14 09:58:36 -0800119 GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(), "GrSoftwarePathRenderer::onDrawPath");
halcanary96fcdcc2015-08-27 07:41:13 -0700120 if (nullptr == fContext) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000121 return false;
122 }
123
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000124 SkIRect devPathBounds, devClipBounds;
robertphillipsb83bec52015-10-23 09:38:03 -0700125 if (!get_path_and_clip_bounds(args.fPipelineBuilder, *args.fPath,
bsalomon0aff2fa2015-07-31 06:48:27 -0700126 *args.fViewMatrix, &devPathBounds, &devClipBounds)) {
127 if (args.fPath->isInverseFillType()) {
128 draw_around_inv_path(args.fTarget, args.fPipelineBuilder, args.fColor,
129 *args.fViewMatrix, devClipBounds, devPathBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000130 }
131 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000132 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000133
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000134 SkAutoTUnref<GrTexture> texture(
bsalomon0aff2fa2015-07-31 06:48:27 -0700135 GrSWMaskHelper::DrawPathMaskToTexture(fContext, *args.fPath, *args.fStroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000136 devPathBounds,
bsalomon0aff2fa2015-07-31 06:48:27 -0700137 args.fAntiAlias, args.fViewMatrix));
halcanary96fcdcc2015-08-27 07:41:13 -0700138 if (nullptr == texture) {
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000139 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000140 }
141
bsalomon0aff2fa2015-07-31 06:48:27 -0700142 GrSWMaskHelper::DrawToTargetWithPathMask(texture, args.fTarget, args.fPipelineBuilder,
143 args.fColor, *args.fViewMatrix, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000144
bsalomon0aff2fa2015-07-31 06:48:27 -0700145 if (args.fPath->isInverseFillType()) {
146 draw_around_inv_path(args.fTarget, args.fPipelineBuilder, args.fColor, *args.fViewMatrix,
147 devClipBounds, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000148 }
149
150 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000151}