blob: 5ab5fb48224a18f4013d724816ee218858947bdc [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.comf4c2c522012-04-27 12:08:47 +000014bool GrSoftwarePathRenderer::canDrawPath(const SkPath& path,
15 GrPathFill fill,
16 const GrDrawTarget* target,
17 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
22 // chain). Some testing would need to be done r.e. performance
23 // 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
robertphillips@google.comed4155d2012-05-01 14:30:24 +000031namespace {
32
33////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000034// gets device coord bounds of path (not considering the fill) and clip. The
35// path bounds will be a subset of the clip bounds. returns false if
36// path bounds would be empty.
37bool get_path_and_clip_bounds(const GrDrawTarget* target,
38 const SkPath& path,
robertphillips@google.com366f1c62012-06-29 21:38:47 +000039 const GrMatrix& matrix,
robertphillips@google.comed4155d2012-05-01 14:30:24 +000040 GrIRect* pathBounds,
41 GrIRect* clipBounds) {
42 // compute bounds as intersection of rt size, clip, and path
43 const GrRenderTarget* rt = target->getDrawState().getRenderTarget();
44 if (NULL == rt) {
45 return false;
46 }
47 *pathBounds = GrIRect::MakeWH(rt->width(), rt->height());
48 const GrClip& clip = target->getClip();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000049
50 clip.getConservativeBounds().roundOut(clipBounds);
51 if (!pathBounds->intersect(*clipBounds)) {
52 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000053 }
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000054
robertphillips@google.com366f1c62012-06-29 21:38:47 +000055 if (!path.getBounds().isEmpty()) {
56 GrRect pathSBounds;
57 matrix.mapRect(&pathSBounds, path.getBounds());
robertphillips@google.comed4155d2012-05-01 14:30:24 +000058 GrIRect pathIBounds;
59 pathSBounds.roundOut(&pathIBounds);
60 if (!pathBounds->intersect(pathIBounds)) {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +000061 // set the correct path bounds, as this would be used later.
62 *pathBounds = pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000063 return false;
64 }
65 } else {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +000066 *pathBounds = GrIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000067 return false;
68 }
69 return true;
70}
71
72////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000073void draw_around_inv_path(GrDrawTarget* target,
74 GrDrawState::StageMask stageMask,
75 const GrIRect& clipBounds,
76 const GrIRect& pathBounds) {
77 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
78 GrRect rect;
79 if (clipBounds.fTop < pathBounds.fTop) {
80 rect.iset(clipBounds.fLeft, clipBounds.fTop,
81 clipBounds.fRight, pathBounds.fTop);
82 target->drawSimpleRect(rect, NULL, stageMask);
83 }
84 if (clipBounds.fLeft < pathBounds.fLeft) {
85 rect.iset(clipBounds.fLeft, pathBounds.fTop,
86 pathBounds.fLeft, pathBounds.fBottom);
87 target->drawSimpleRect(rect, NULL, stageMask);
88 }
89 if (clipBounds.fRight > pathBounds.fRight) {
90 rect.iset(pathBounds.fRight, pathBounds.fTop,
91 clipBounds.fRight, pathBounds.fBottom);
92 target->drawSimpleRect(rect, NULL, stageMask);
93 }
94 if (clipBounds.fBottom > pathBounds.fBottom) {
95 rect.iset(clipBounds.fLeft, pathBounds.fBottom,
96 clipBounds.fRight, clipBounds.fBottom);
97 target->drawSimpleRect(rect, NULL, stageMask);
98 }
99}
100
101}
102
103////////////////////////////////////////////////////////////////////////////////
104// return true on success; false on failure
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000105bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path,
106 GrPathFill fill,
107 const GrVec* translate,
108 GrDrawTarget* target,
109 GrDrawState::StageMask stageMask,
110 bool antiAlias) {
111
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000112 if (NULL == fContext) {
113 return false;
114 }
115
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000116 GrDrawState* drawState = target->drawState();
117
118 GrMatrix vm = drawState->getViewMatrix();
119 if (NULL != translate) {
120 vm.postTranslate(translate->fX, translate->fY);
121 }
122
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000123 GrIRect pathBounds, clipBounds;
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000124 if (!get_path_and_clip_bounds(target, path, vm,
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000125 &pathBounds, &clipBounds)) {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000126 if (GrIsFillInverted(fill)) {
127 draw_around_inv_path(target, stageMask,
128 clipBounds, pathBounds);
129 }
130 return true;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000131 }
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000132
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000133 SkAutoTUnref<GrTexture> texture(
134 GrSWMaskHelper::DrawPathMaskToTexture(fContext, path,
135 pathBounds, fill,
136 antiAlias, &vm));
137 if (NULL == texture) {
138 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000139 }
140
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000141 GrSWMaskHelper::DrawToTargetWithPathMask(texture, target,
142 stageMask, pathBounds);
143
144 if (GrIsFillInverted(fill)) {
145 draw_around_inv_path(target, stageMask,
146 clipBounds, pathBounds);
147 }
148
149 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000150}