blob: 0bf1cab46a4958e1cf014e8536df4a55d9e121d2 [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());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000048
robertphillips@google.combeb1af72012-07-26 18:52:16 +000049 const GrClipData* clipData = target->getClip();
50
51 SkRect conservativeBounds = clipData->fClipStack->getConservativeBounds();
52 conservativeBounds.roundOut(clipBounds);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000053 if (!pathBounds->intersect(*clipBounds)) {
54 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000055 }
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000056
robertphillips@google.com366f1c62012-06-29 21:38:47 +000057 if (!path.getBounds().isEmpty()) {
58 GrRect pathSBounds;
59 matrix.mapRect(&pathSBounds, path.getBounds());
robertphillips@google.comed4155d2012-05-01 14:30:24 +000060 GrIRect pathIBounds;
61 pathSBounds.roundOut(&pathIBounds);
62 if (!pathBounds->intersect(pathIBounds)) {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +000063 // set the correct path bounds, as this would be used later.
64 *pathBounds = pathIBounds;
robertphillips@google.comed4155d2012-05-01 14:30:24 +000065 return false;
66 }
67 } else {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +000068 *pathBounds = GrIRect::EmptyIRect();
robertphillips@google.comed4155d2012-05-01 14:30:24 +000069 return false;
70 }
71 return true;
72}
73
74////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comed4155d2012-05-01 14:30:24 +000075void draw_around_inv_path(GrDrawTarget* target,
robertphillips@google.comed4155d2012-05-01 14:30:24 +000076 const GrIRect& clipBounds,
77 const GrIRect& pathBounds) {
bsalomon@google.come3d32162012-07-20 13:37:06 +000078 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
79 if (!adcd.succeeded()) {
80 return;
81 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +000082 GrRect rect;
83 if (clipBounds.fTop < pathBounds.fTop) {
84 rect.iset(clipBounds.fLeft, clipBounds.fTop,
85 clipBounds.fRight, pathBounds.fTop);
bsalomon@google.come3d32162012-07-20 13:37:06 +000086 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000087 }
88 if (clipBounds.fLeft < pathBounds.fLeft) {
89 rect.iset(clipBounds.fLeft, pathBounds.fTop,
90 pathBounds.fLeft, pathBounds.fBottom);
bsalomon@google.come3d32162012-07-20 13:37:06 +000091 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000092 }
93 if (clipBounds.fRight > pathBounds.fRight) {
94 rect.iset(pathBounds.fRight, pathBounds.fTop,
95 clipBounds.fRight, pathBounds.fBottom);
bsalomon@google.come3d32162012-07-20 13:37:06 +000096 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +000097 }
98 if (clipBounds.fBottom > pathBounds.fBottom) {
99 rect.iset(clipBounds.fLeft, pathBounds.fBottom,
100 clipBounds.fRight, clipBounds.fBottom);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000101 target->drawSimpleRect(rect, NULL);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000102 }
103}
104
105}
106
107////////////////////////////////////////////////////////////////////////////////
108// return true on success; false on failure
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000109bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path,
110 GrPathFill fill,
111 const GrVec* translate,
112 GrDrawTarget* target,
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000113 bool antiAlias) {
114
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000115 if (NULL == fContext) {
116 return false;
117 }
118
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000119 GrDrawState* drawState = target->drawState();
120
121 GrMatrix vm = drawState->getViewMatrix();
122 if (NULL != translate) {
123 vm.postTranslate(translate->fX, translate->fY);
124 }
125
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000126 GrIRect pathBounds, clipBounds;
robertphillips@google.com366f1c62012-06-29 21:38:47 +0000127 if (!get_path_and_clip_bounds(target, path, vm,
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000128 &pathBounds, &clipBounds)) {
bsalomon@google.com276c1fa2012-06-19 13:22:45 +0000129 if (GrIsFillInverted(fill)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000130 draw_around_inv_path(target, clipBounds, pathBounds);
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(
136 GrSWMaskHelper::DrawPathMaskToTexture(fContext, path,
137 pathBounds, fill,
138 antiAlias, &vm));
139 if (NULL == texture) {
140 return false;
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000141 }
142
bsalomon@google.come3d32162012-07-20 13:37:06 +0000143 GrSWMaskHelper::DrawToTargetWithPathMask(texture, target, pathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000144
145 if (GrIsFillInverted(fill)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000146 draw_around_inv_path(target, clipBounds, pathBounds);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +0000147 }
148
149 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000150}