blob: 61711c07ced747dfdd9a525b1e68d75721f6bd68 [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////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +000014bool GrSoftwarePathRenderer::canDrawPath(const SkStrokeRec&,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000015 const GrDrawTarget*,
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000016 bool antiAlias) const {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000017 if (!antiAlias || NULL == fContext) {
18 // TODO: We could allow the SW path to also handle non-AA paths but
19 // this would mean that GrDefaultPathRenderer would never be called
20 // (since it appears after the SW renderer in the path renderer
rmistry@google.comd6176b02012-08-23 18:14:13 +000021 // chain). Some testing would need to be done r.e. performance
robertphillips@google.comed4155d2012-05-01 14:30:24 +000022 // and consistency of the resulting images before removing
23 // the "!antiAlias" clause from the above test
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000024 return false;
25 }
26
robertphillips@google.comed4155d2012-05-01 14:30:24 +000027 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000028}
29
bsalomon@google.com45a15f52012-12-10 19:10:17 +000030GrPathRenderer::StencilSupport GrSoftwarePathRenderer::onGetStencilSupport(
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000031 const SkStrokeRec&,
32 const GrDrawTarget*) const {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000033 return GrPathRenderer::kNoSupport_StencilSupport;
34}
35
robertphillips@google.comed4155d2012-05-01 14:30:24 +000036namespace {
37
38////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000039// gets device coord bounds of path (not considering the fill) and clip. The
rmistry@google.comd6176b02012-08-23 18:14:13 +000040// path bounds will be a subset of the clip bounds. returns false if
robertphillips@google.comed4155d2012-05-01 14:30:24 +000041// path bounds would be empty.
42bool get_path_and_clip_bounds(const GrDrawTarget* target,
43 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
48 const GrRenderTarget* rt = target->getDrawState().getRenderTarget();
49 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,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000081 const SkIRect& devClipBounds,
82 const SkIRect& devPathBounds) {
bsalomon@google.com137f1342013-05-29 21:27:53 +000083 GrDrawState::AutoViewMatrixRestore avmr;
84 if (!avmr.setIdentity(target->drawState())) {
bsalomon@google.come3d32162012-07-20 13:37:06 +000085 return;
86 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000087 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +000088 if (devClipBounds.fTop < devPathBounds.fTop) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000089 rect.iset(devClipBounds.fLeft, devClipBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000090 devClipBounds.fRight, devPathBounds.fTop);
bsalomon@google.come3d32162012-07-20 13:37:06 +000091 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000092 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000093 if (devClipBounds.fLeft < devPathBounds.fLeft) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000094 rect.iset(devClipBounds.fLeft, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000095 devPathBounds.fLeft, devPathBounds.fBottom);
bsalomon@google.come3d32162012-07-20 13:37:06 +000096 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000097 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000098 if (devClipBounds.fRight > devPathBounds.fRight) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000099 rect.iset(devPathBounds.fRight, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000100 devClipBounds.fRight, devPathBounds.fBottom);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000101 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000102 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000103 if (devClipBounds.fBottom > devPathBounds.fBottom) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104 rect.iset(devClipBounds.fLeft, devPathBounds.fBottom,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000105 devClipBounds.fRight, devClipBounds.fBottom);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000106 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000107 }
108}
109
110}
111
112////////////////////////////////////////////////////////////////////////////////
113// return true on success; false on failure
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000114bool GrSoftwarePathRenderer::onDrawPath(const SkStrokeRec& stroke,
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000115 GrDrawTarget* target,
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000116 bool antiAlias) {
117
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000118 if (NULL == fContext) {
119 return false;
120 }
121
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000122 GrDrawState* drawState = target->drawState();
123
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000124 SkMatrix vm = drawState->getViewMatrix();
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000125
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000126 SkIRect devPathBounds, devClipBounds;
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000127 if (!get_path_and_clip_bounds(target, this->path(), vm,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000128 &devPathBounds, &devClipBounds)) {
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000129 if (this->path().isInverseFillType()) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000130 draw_around_inv_path(target, devClipBounds, devPathBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000131 }
132 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000133 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000134
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000135 SkAutoTUnref<GrTexture> texture(
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000136 GrSWMaskHelper::DrawPathMaskToTexture(fContext, this->path(), stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000137 devPathBounds,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000138 antiAlias, &vm));
139 if (NULL == texture) {
140 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000141 }
142
robertphillips@google.com7b112892012-07-31 15:18:21 +0000143 GrSWMaskHelper::DrawToTargetWithPathMask(texture, target, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000144
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000145 if (this->path().isInverseFillType()) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000146 draw_around_inv_path(target, devClipBounds, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000147 }
148
149 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000150}