blob: 90b35f7431a38e2e0f382c67757e796ff38d5238 [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"
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000012
robertphillips@google.comed4155d2012-05-01 14:30:24 +000013////////////////////////////////////////////////////////////////////////////////
joshualitt9853cce2014-11-17 14:22:48 -080014bool GrSoftwarePathRenderer::canDrawPath(const GrDrawTarget*,
15 const GrDrawState*,
joshualitt8059eb92014-12-29 15:10:07 -080016 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -080017 const SkPath&,
robertphillips@google.come79f3202014-02-11 16:30:21 +000018 const SkStrokeRec&,
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000019 bool antiAlias) const {
robertphillips730c0442014-07-25 05:52:38 -070020 if (NULL == fContext) {
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000021 return false;
22 }
23
robertphillips@google.comed4155d2012-05-01 14:30:24 +000024 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000025}
26
joshualitt9853cce2014-11-17 14:22:48 -080027GrPathRenderer::StencilSupport
28GrSoftwarePathRenderer::onGetStencilSupport(const GrDrawTarget*,
29 const GrDrawState*,
30 const SkPath&,
31 const SkStrokeRec&) const {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000032 return GrPathRenderer::kNoSupport_StencilSupport;
33}
34
robertphillips@google.comed4155d2012-05-01 14:30:24 +000035namespace {
36
37////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000038// gets device coord bounds of path (not considering the fill) and clip. The
rmistry@google.comd6176b02012-08-23 18:14:13 +000039// path bounds will be a subset of the clip bounds. returns false if
robertphillips@google.comed4155d2012-05-01 14:30:24 +000040// path bounds would be empty.
41bool get_path_and_clip_bounds(const GrDrawTarget* target,
joshualitt9853cce2014-11-17 14:22:48 -080042 const GrDrawState* drawState,
robertphillips@google.comed4155d2012-05-01 14:30:24 +000043 const SkPath& path,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000044 const SkMatrix& matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000045 SkIRect* devPathBounds,
46 SkIRect* devClipBounds) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000047 // compute bounds as intersection of rt size, clip, and path
joshualitt9853cce2014-11-17 14:22:48 -080048 const GrRenderTarget* rt = drawState->getRenderTarget();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000049 if (NULL == rt) {
50 return false;
51 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000052 *devPathBounds = SkIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000053
robertphillips@google.com7b112892012-07-31 15:18:21 +000054 target->getClip()->getConservativeBounds(rt, devClipBounds);
55
rmistry@google.comd6176b02012-08-23 18:14:13 +000056 // TODO: getConservativeBounds already intersects with the
robertphillips@google.com7b112892012-07-31 15:18:21 +000057 // render target's bounding box. Remove this next line
58 if (!devPathBounds->intersect(*devClipBounds)) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000059 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000060 }
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000061
robertphillips@google.com366f1c62012-06-29 21:38:47 +000062 if (!path.getBounds().isEmpty()) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000063 SkRect pathSBounds;
robertphillips@google.com366f1c62012-06-29 21:38:47 +000064 matrix.mapRect(&pathSBounds, path.getBounds());
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000065 SkIRect pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000066 pathSBounds.roundOut(&pathIBounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +000067 if (!devPathBounds->intersect(pathIBounds)) {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +000068 // set the correct path bounds, as this would be used later.
robertphillips@google.com7b112892012-07-31 15:18:21 +000069 *devPathBounds = pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000070 return false;
71 }
72 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000073 *devPathBounds = SkIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000074 return false;
75 }
76 return true;
77}
78
79////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000080void draw_around_inv_path(GrDrawTarget* target,
joshualitt9853cce2014-11-17 14:22:48 -080081 GrDrawState* drawState,
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);
joshualitt8059eb92014-12-29 15:10:07 -080095 target->drawRect(drawState, color, SkMatrix::I(), rect, NULL, &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);
joshualitt8059eb92014-12-29 15:10:07 -0800100 target->drawRect(drawState, color, SkMatrix::I(), rect, NULL, &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);
joshualitt8059eb92014-12-29 15:10:07 -0800105 target->drawRect(drawState, color, SkMatrix::I(), rect, NULL, &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);
joshualitt8059eb92014-12-29 15:10:07 -0800110 target->drawRect(drawState, color, SkMatrix::I(), rect, NULL, &invert);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000111 }
112}
113
114}
115
116////////////////////////////////////////////////////////////////////////////////
117// return true on success; false on failure
joshualitt9853cce2014-11-17 14:22:48 -0800118bool GrSoftwarePathRenderer::onDrawPath(GrDrawTarget* target,
119 GrDrawState* drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800120 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800121 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800122 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000123 const SkStrokeRec& stroke,
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000124 bool antiAlias) {
125
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000126 if (NULL == fContext) {
127 return false;
128 }
129
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000130 SkIRect devPathBounds, devClipBounds;
joshualitt8059eb92014-12-29 15:10:07 -0800131 if (!get_path_and_clip_bounds(target, drawState, path, viewMatrix,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000132 &devPathBounds, &devClipBounds)) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000133 if (path.isInverseFillType()) {
joshualitt8059eb92014-12-29 15:10:07 -0800134 draw_around_inv_path(target, drawState, color, viewMatrix, devClipBounds,devPathBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000135 }
136 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000137 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000138
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000139 SkAutoTUnref<GrTexture> texture(
robertphillips@google.come79f3202014-02-11 16:30:21 +0000140 GrSWMaskHelper::DrawPathMaskToTexture(fContext, path, stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000141 devPathBounds,
joshualitt8059eb92014-12-29 15:10:07 -0800142 antiAlias, &viewMatrix));
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000143 if (NULL == texture) {
144 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000145 }
146
joshualitt9853cce2014-11-17 14:22:48 -0800147 GrDrawState copy = *drawState;
joshualitt8059eb92014-12-29 15:10:07 -0800148 GrSWMaskHelper::DrawToTargetWithPathMask(texture, target, &copy, color, viewMatrix,
149 devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000150
robertphillips@google.come79f3202014-02-11 16:30:21 +0000151 if (path.isInverseFillType()) {
joshualitt8059eb92014-12-29 15:10:07 -0800152 draw_around_inv_path(target, drawState, color, viewMatrix, devClipBounds, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000153 }
154
155 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000156}