blob: f54d3b09e6087702738acf74272683929536f42f [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.
31bool get_path_and_clip_bounds(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080032 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
joshualitt44701df2015-02-23 14:44:57 -080043 pipelineBuilder->clip().getConservativeBounds(rt, 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////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000069void draw_around_inv_path(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080070 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -080071 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -080072 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000073 const SkIRect& devClipBounds,
74 const SkIRect& devPathBounds) {
joshualittd27f73e2014-12-29 07:43:36 -080075 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -080076 if (!viewMatrix.invert(&invert)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +000077 return;
78 }
joshualittd27f73e2014-12-29 07:43:36 -080079
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000080 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +000081 if (devClipBounds.fTop < devPathBounds.fTop) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000082 rect.iset(devClipBounds.fLeft, devClipBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000083 devClipBounds.fRight, devPathBounds.fTop);
joshualittd2b23e02015-08-21 10:53:34 -070084 target->drawNonAARect(*pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000085 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000086 if (devClipBounds.fLeft < devPathBounds.fLeft) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000087 rect.iset(devClipBounds.fLeft, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000088 devPathBounds.fLeft, devPathBounds.fBottom);
joshualittd2b23e02015-08-21 10:53:34 -070089 target->drawNonAARect(*pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000090 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000091 if (devClipBounds.fRight > devPathBounds.fRight) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000092 rect.iset(devPathBounds.fRight, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000093 devClipBounds.fRight, devPathBounds.fBottom);
joshualittd2b23e02015-08-21 10:53:34 -070094 target->drawNonAARect(*pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000095 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000096 if (devClipBounds.fBottom > devPathBounds.fBottom) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000097 rect.iset(devClipBounds.fLeft, devPathBounds.fBottom,
robertphillips@google.com7b112892012-07-31 15:18:21 +000098 devClipBounds.fRight, devClipBounds.fBottom);
joshualittd2b23e02015-08-21 10:53:34 -070099 target->drawNonAARect(*pipelineBuilder, color, SkMatrix::I(), rect, invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000100 }
101}
102
103}
104
105////////////////////////////////////////////////////////////////////////////////
106// return true on success; false on failure
bsalomon0aff2fa2015-07-31 06:48:27 -0700107bool GrSoftwarePathRenderer::onDrawPath(const DrawPathArgs& args) {
halcanary96fcdcc2015-08-27 07:41:13 -0700108 if (nullptr == fContext) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000109 return false;
110 }
111
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000112 SkIRect devPathBounds, devClipBounds;
bsalomon0aff2fa2015-07-31 06:48:27 -0700113 if (!get_path_and_clip_bounds(args.fTarget, args.fPipelineBuilder, *args.fPath,
114 *args.fViewMatrix, &devPathBounds, &devClipBounds)) {
115 if (args.fPath->isInverseFillType()) {
116 draw_around_inv_path(args.fTarget, args.fPipelineBuilder, args.fColor,
117 *args.fViewMatrix, devClipBounds, devPathBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000118 }
119 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000120 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000121
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000122 SkAutoTUnref<GrTexture> texture(
bsalomon0aff2fa2015-07-31 06:48:27 -0700123 GrSWMaskHelper::DrawPathMaskToTexture(fContext, *args.fPath, *args.fStroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000124 devPathBounds,
bsalomon0aff2fa2015-07-31 06:48:27 -0700125 args.fAntiAlias, args.fViewMatrix));
halcanary96fcdcc2015-08-27 07:41:13 -0700126 if (nullptr == texture) {
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000127 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000128 }
129
bsalomon0aff2fa2015-07-31 06:48:27 -0700130 GrSWMaskHelper::DrawToTargetWithPathMask(texture, args.fTarget, args.fPipelineBuilder,
131 args.fColor, *args.fViewMatrix, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000132
bsalomon0aff2fa2015-07-31 06:48:27 -0700133 if (args.fPath->isInverseFillType()) {
134 draw_around_inv_path(args.fTarget, args.fPipelineBuilder, args.fColor, *args.fViewMatrix,
135 devClipBounds, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000136 }
137
138 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000139}