blob: a136886a6400f59b0fade456c9f4ac765dc70b78 [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"
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000013
robertphillips@google.comed4155d2012-05-01 14:30:24 +000014////////////////////////////////////////////////////////////////////////////////
bsalomon0aff2fa2015-07-31 06:48:27 -070015bool GrSoftwarePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
halcanary96fcdcc2015-08-27 07:41:13 -070016 if (nullptr == fContext) {
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000017 return false;
18 }
bsalomon0aff2fa2015-07-31 06:48:27 -070019 if (args.fStroke->isDashed()) {
kkinnunen18996512015-04-26 23:18:49 -070020 return false;
21 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +000022 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000023}
24
robertphillips@google.comed4155d2012-05-01 14:30:24 +000025namespace {
26
27////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000028// gets device coord bounds of path (not considering the fill) and clip. The
rmistry@google.comd6176b02012-08-23 18:14:13 +000029// path bounds will be a subset of the clip bounds. returns false if
robertphillips@google.comed4155d2012-05-01 14:30:24 +000030// path bounds would be empty.
robertphillipsb83bec52015-10-23 09:38:03 -070031bool get_path_and_clip_bounds(const GrPipelineBuilder* pipelineBuilder,
robertphillips@google.comed4155d2012-05-01 14:30:24 +000032 const SkPath& path,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000033 const SkMatrix& matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000034 SkIRect* devPathBounds,
35 SkIRect* devClipBounds) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000036 // compute bounds as intersection of rt size, clip, and path
egdaniel8dd688b2015-01-22 10:16:09 -080037 const GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
halcanary96fcdcc2015-08-27 07:41:13 -070038 if (nullptr == rt) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000039 return false;
40 }
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000041
robertphillips7bceedc2015-12-01 12:51:26 -080042 pipelineBuilder->clip().getConservativeBounds(rt->width(), rt->height(), devClipBounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +000043
robertphillipse85a32d2015-02-10 08:16:55 -080044 if (devClipBounds->isEmpty()) {
45 *devPathBounds = SkIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000046 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000047 }
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000048
robertphillips@google.com366f1c62012-06-29 21:38:47 +000049 if (!path.getBounds().isEmpty()) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000050 SkRect pathSBounds;
robertphillips@google.com366f1c62012-06-29 21:38:47 +000051 matrix.mapRect(&pathSBounds, path.getBounds());
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000052 SkIRect pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000053 pathSBounds.roundOut(&pathIBounds);
robertphillipse85a32d2015-02-10 08:16:55 -080054 *devPathBounds = *devClipBounds;
robertphillips@google.com7b112892012-07-31 15:18:21 +000055 if (!devPathBounds->intersect(pathIBounds)) {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +000056 // set the correct path bounds, as this would be used later.
robertphillips@google.com7b112892012-07-31 15:18:21 +000057 *devPathBounds = pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000058 return false;
59 }
60 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000061 *devPathBounds = SkIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000062 return false;
63 }
64 return true;
65}
66
67////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000068void draw_around_inv_path(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080069 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -080070 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -080071 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000072 const SkIRect& devClipBounds,
73 const SkIRect& devPathBounds) {
joshualittd27f73e2014-12-29 07:43:36 -080074 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -080075 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +000076 return;
77 }
joshualittd27f73e2014-12-29 07:43:36 -080078
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000079 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +000080 if (devClipBounds.fTop < devPathBounds.fTop) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000081 rect.iset(devClipBounds.fLeft, devClipBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000082 devClipBounds.fRight, devPathBounds.fTop);
joshualittd2b23e02015-08-21 10:53:34 -070083 target->drawNonAARect(*pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000084 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000085 if (devClipBounds.fLeft < devPathBounds.fLeft) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000086 rect.iset(devClipBounds.fLeft, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000087 devPathBounds.fLeft, devPathBounds.fBottom);
joshualittd2b23e02015-08-21 10:53:34 -070088 target->drawNonAARect(*pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000089 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000090 if (devClipBounds.fRight > devPathBounds.fRight) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000091 rect.iset(devPathBounds.fRight, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000092 devClipBounds.fRight, devPathBounds.fBottom);
joshualittd2b23e02015-08-21 10:53:34 -070093 target->drawNonAARect(*pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000094 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000095 if (devClipBounds.fBottom > devPathBounds.fBottom) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000096 rect.iset(devClipBounds.fLeft, devPathBounds.fBottom,
robertphillips@google.com7b112892012-07-31 15:18:21 +000097 devClipBounds.fRight, devClipBounds.fBottom);
joshualittd2b23e02015-08-21 10:53:34 -070098 target->drawNonAARect(*pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000099 }
100}
101
102}
103
104////////////////////////////////////////////////////////////////////////////////
105// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700106bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
halcanary96fcdcc2015-08-27 07:41:13 -0700107 if (nullptr == fContext) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000108 return false;
109 }
110
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000111 SkIRect devPathBounds, devClipBounds;
robertphillipsb83bec52015-10-23 09:38:03 -0700112 if (!get_path_and_clip_bounds(args.fPipelineBuilder, *args.fPath,
bsalomon0aff2fa2015-07-31 06:48:27 -0700113 *args.fViewMatrix, &devPathBounds, &devClipBounds)) {
114 if (args.fPath->isInverseFillType()) {
115 draw_around_inv_path(args.fTarget, args.fPipelineBuilder, args.fColor,
116 *args.fViewMatrix, devClipBounds, devPathBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000117 }
118 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000119 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000120
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000121 SkAutoTUnref<GrTexture> texture(
bsalomon0aff2fa2015-07-31 06:48:27 -0700122 GrSWMaskHelper::DrawPathMaskToTexture(fContext, *args.fPath, *args.fStroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000123 devPathBounds,
bsalomon0aff2fa2015-07-31 06:48:27 -0700124 args.fAntiAlias, args.fViewMatrix));
halcanary96fcdcc2015-08-27 07:41:13 -0700125 if (nullptr == texture) {
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000126 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000127 }
128
bsalomon0aff2fa2015-07-31 06:48:27 -0700129 GrSWMaskHelper::DrawToTargetWithPathMask(texture, args.fTarget, args.fPipelineBuilder,
130 args.fColor, *args.fViewMatrix, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000131
bsalomon0aff2fa2015-07-31 06:48:27 -0700132 if (args.fPath->isInverseFillType()) {
133 draw_around_inv_path(args.fTarget, args.fPipelineBuilder, args.fColor, *args.fViewMatrix,
134 devClipBounds, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000135 }
136
137 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000138}