blob: d0936d6b28a37427b31d0b66cda1dc7138dfccdd [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////////////////////////////////////////////////////////////////////////////////
sugoi@google.com5f74cf82012-12-17 21:16:45 +000014bool GrSoftwarePathRenderer::canDrawPath(const SkPath&,
15 const SkStrokeRec&,
16 const GrDrawTarget*,
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000017 bool antiAlias) const {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000018 if (!antiAlias || NULL == fContext) {
19 // TODO: We could allow the SW path to also handle non-AA paths but
20 // this would mean that GrDefaultPathRenderer would never be called
21 // (since it appears after the SW renderer in the path renderer
rmistry@google.comd6176b02012-08-23 18:14:13 +000022 // chain). Some testing would need to be done r.e. performance
robertphillips@google.comed4155d2012-05-01 14:30:24 +000023 // and consistency of the resulting images before removing
24 // the "!antiAlias" clause from the above test
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000025 return false;
26 }
27
robertphillips@google.comed4155d2012-05-01 14:30:24 +000028 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000029}
30
bsalomon@google.com45a15f52012-12-10 19:10:17 +000031GrPathRenderer::StencilSupport GrSoftwarePathRenderer::onGetStencilSupport(
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000032 const SkPath&,
33 const SkStrokeRec&,
34 const GrDrawTarget*) const {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000035 return GrPathRenderer::kNoSupport_StencilSupport;
36}
37
robertphillips@google.comed4155d2012-05-01 14:30:24 +000038namespace {
39
40////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000041// gets device coord bounds of path (not considering the fill) and clip. The
rmistry@google.comd6176b02012-08-23 18:14:13 +000042// path bounds will be a subset of the clip bounds. returns false if
robertphillips@google.comed4155d2012-05-01 14:30:24 +000043// path bounds would be empty.
44bool get_path_and_clip_bounds(const GrDrawTarget* target,
45 const SkPath& path,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000046 const SkMatrix& matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000047 SkIRect* devPathBounds,
48 SkIRect* devClipBounds) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000049 // compute bounds as intersection of rt size, clip, and path
50 const GrRenderTarget* rt = target->getDrawState().getRenderTarget();
51 if (NULL == rt) {
52 return false;
53 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000054 *devPathBounds = SkIRect::MakeWH(rt->width(), rt->height());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000055
robertphillips@google.com7b112892012-07-31 15:18:21 +000056 target->getClip()->getConservativeBounds(rt, devClipBounds);
57
rmistry@google.comd6176b02012-08-23 18:14:13 +000058 // TODO: getConservativeBounds already intersects with the
robertphillips@google.com7b112892012-07-31 15:18:21 +000059 // render target's bounding box. Remove this next line
60 if (!devPathBounds->intersect(*devClipBounds)) {
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000061 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000062 }
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000063
robertphillips@google.com366f1c62012-06-29 21:38:47 +000064 if (!path.getBounds().isEmpty()) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000065 SkRect pathSBounds;
robertphillips@google.com366f1c62012-06-29 21:38:47 +000066 matrix.mapRect(&pathSBounds, path.getBounds());
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000067 SkIRect pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000068 pathSBounds.roundOut(&pathIBounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +000069 if (!devPathBounds->intersect(pathIBounds)) {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +000070 // set the correct path bounds, as this would be used later.
robertphillips@google.com7b112892012-07-31 15:18:21 +000071 *devPathBounds = pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000072 return false;
73 }
74 } else {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000075 *devPathBounds = SkIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000076 return false;
77 }
78 return true;
79}
80
81////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000082void draw_around_inv_path(GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000083 const SkIRect& devClipBounds,
84 const SkIRect& devPathBounds) {
bsalomon@google.com137f1342013-05-29 21:27:53 +000085 GrDrawState::AutoViewMatrixRestore avmr;
86 if (!avmr.setIdentity(target->drawState())) {
bsalomon@google.come3d32162012-07-20 13:37:06 +000087 return;
88 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000089 SkRect rect;
robertphillips@google.com7b112892012-07-31 15:18:21 +000090 if (devClipBounds.fTop < devPathBounds.fTop) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000091 rect.iset(devClipBounds.fLeft, devClipBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000092 devClipBounds.fRight, devPathBounds.fTop);
bsalomon@google.come3d32162012-07-20 13:37:06 +000093 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000094 }
robertphillips@google.com7b112892012-07-31 15:18:21 +000095 if (devClipBounds.fLeft < devPathBounds.fLeft) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000096 rect.iset(devClipBounds.fLeft, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +000097 devPathBounds.fLeft, devPathBounds.fBottom);
bsalomon@google.come3d32162012-07-20 13:37:06 +000098 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000099 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000100 if (devClipBounds.fRight > devPathBounds.fRight) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101 rect.iset(devPathBounds.fRight, devPathBounds.fTop,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000102 devClipBounds.fRight, devPathBounds.fBottom);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000103 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000104 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000105 if (devClipBounds.fBottom > devPathBounds.fBottom) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106 rect.iset(devClipBounds.fLeft, devPathBounds.fBottom,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000107 devClipBounds.fRight, devClipBounds.fBottom);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000108 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000109 }
110}
111
112}
113
114////////////////////////////////////////////////////////////////////////////////
115// return true on success; false on failure
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000116bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000117 const SkStrokeRec& stroke,
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000118 GrDrawTarget* target,
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000119 bool antiAlias) {
120
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000121 if (NULL == fContext) {
122 return false;
123 }
124
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000125 GrDrawState* drawState = target->drawState();
126
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000127 SkMatrix vm = drawState->getViewMatrix();
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000128
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000129 SkIRect devPathBounds, devClipBounds;
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000130 if (!get_path_and_clip_bounds(target, path, vm,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000131 &devPathBounds, &devClipBounds)) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000132 if (path.isInverseFillType()) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000133 draw_around_inv_path(target, devClipBounds, devPathBounds);
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000134 }
135 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000136 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000137
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000138 SkAutoTUnref<GrTexture> texture(
sugoi@google.com12b4e272012-12-06 20:13:11 +0000139 GrSWMaskHelper::DrawPathMaskToTexture(fContext, path, stroke,
140 devPathBounds,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000141 antiAlias, &vm));
142 if (NULL == texture) {
143 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000144 }
145
robertphillips@google.com7b112892012-07-31 15:18:21 +0000146 GrSWMaskHelper::DrawToTargetWithPathMask(texture, target, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000147
sugoi@google.com12b4e272012-12-06 20:13:11 +0000148 if (path.isInverseFillType()) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000149 draw_around_inv_path(target, devClipBounds, devPathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000150 }
151
152 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000153}