blob: 1213c41a7f36481ad9a13c53b9c7db5b656f9795 [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 "GrPaint.h"
11#include "SkPaint.h"
12#include "GrRenderTarget.h"
13#include "GrContext.h"
14#include "SkDraw.h"
15#include "SkRasterClip.h"
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000016
robertphillips@google.comed4155d2012-05-01 14:30:24 +000017////////////////////////////////////////////////////////////////////////////////
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000018bool GrSoftwarePathRenderer::canDrawPath(const SkPath& path,
19 GrPathFill fill,
20 const GrDrawTarget* target,
21 bool antiAlias) const {
robertphillips@google.comed4155d2012-05-01 14:30:24 +000022 if (!antiAlias || NULL == fContext) {
23 // TODO: We could allow the SW path to also handle non-AA paths but
24 // this would mean that GrDefaultPathRenderer would never be called
25 // (since it appears after the SW renderer in the path renderer
26 // chain). Some testing would need to be done r.e. performance
27 // and consistency of the resulting images before removing
28 // the "!antiAlias" clause from the above test
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000029 return false;
30 }
31
robertphillips@google.comed4155d2012-05-01 14:30:24 +000032 return true;
robertphillips@google.comf4c2c522012-04-27 12:08:47 +000033}
34
robertphillips@google.comed4155d2012-05-01 14:30:24 +000035namespace {
36
37////////////////////////////////////////////////////////////////////////////////
38SkPath::FillType gr_fill_to_sk_fill(GrPathFill fill) {
39 switch (fill) {
bsalomon@google.com47059542012-06-06 20:51:20 +000040 case kWinding_GrPathFill:
robertphillips@google.comed4155d2012-05-01 14:30:24 +000041 return SkPath::kWinding_FillType;
bsalomon@google.com47059542012-06-06 20:51:20 +000042 case kEvenOdd_GrPathFill:
robertphillips@google.comed4155d2012-05-01 14:30:24 +000043 return SkPath::kEvenOdd_FillType;
bsalomon@google.com47059542012-06-06 20:51:20 +000044 case kInverseWinding_GrPathFill:
robertphillips@google.comed4155d2012-05-01 14:30:24 +000045 return SkPath::kInverseWinding_FillType;
bsalomon@google.com47059542012-06-06 20:51:20 +000046 case kInverseEvenOdd_GrPathFill:
robertphillips@google.comed4155d2012-05-01 14:30:24 +000047 return SkPath::kInverseEvenOdd_FillType;
48 default:
49 GrCrash("Unexpected fill.");
50 return SkPath::kWinding_FillType;
51 }
52}
53
54////////////////////////////////////////////////////////////////////////////////
55// gets device coord bounds of path (not considering the fill) and clip. The
56// path bounds will be a subset of the clip bounds. returns false if
57// path bounds would be empty.
58bool get_path_and_clip_bounds(const GrDrawTarget* target,
59 const SkPath& path,
60 const GrVec* translate,
61 GrIRect* pathBounds,
62 GrIRect* clipBounds) {
63 // compute bounds as intersection of rt size, clip, and path
64 const GrRenderTarget* rt = target->getDrawState().getRenderTarget();
65 if (NULL == rt) {
66 return false;
67 }
68 *pathBounds = GrIRect::MakeWH(rt->width(), rt->height());
69 const GrClip& clip = target->getClip();
70 if (clip.hasConservativeBounds()) {
71 clip.getConservativeBounds().roundOut(clipBounds);
72 if (!pathBounds->intersect(*clipBounds)) {
73 return false;
74 }
75 } else {
76 // pathBounds is currently the rt extent, set clip bounds to that rect.
77 *clipBounds = *pathBounds;
78 }
79 GrRect pathSBounds = path.getBounds();
80 if (!pathSBounds.isEmpty()) {
81 if (NULL != translate) {
82 pathSBounds.offset(*translate);
83 }
84 target->getDrawState().getViewMatrix().mapRect(&pathSBounds,
85 pathSBounds);
86 GrIRect pathIBounds;
87 pathSBounds.roundOut(&pathIBounds);
88 if (!pathBounds->intersect(pathIBounds)) {
89 return false;
90 }
91 } else {
92 return false;
93 }
94 return true;
95}
96
robertphillips@google.comb4f06d72012-05-15 12:10:05 +000097
98/*
99 * Convert a boolean operation into a transfer mode code
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000100 */
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000101SkXfermode::Mode op_to_mode(SkRegion::Op op) {
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000102
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000103 static const SkXfermode::Mode modeMap[] = {
robertphillips@google.comfa662942012-05-17 12:20:22 +0000104 SkXfermode::kDstOut_Mode, // kDifference_Op
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000105 SkXfermode::kMultiply_Mode, // kIntersect_Op
106 SkXfermode::kSrcOver_Mode, // kUnion_Op
107 SkXfermode::kXor_Mode, // kXOR_Op
108 SkXfermode::kClear_Mode, // kReverseDifference_Op
109 SkXfermode::kSrc_Mode, // kReplace_Op
110 };
111
112 return modeMap[op];
113}
114
115}
116
117/**
118 * Draw a single rect element of the clip stack into the accumulation bitmap
119 */
120void GrSWMaskHelper::draw(const GrRect& clientRect, SkRegion::Op op,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000121 bool antiAlias, GrColor color) {
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000122 SkPaint paint;
123
124 SkXfermode* mode = SkXfermode::Create(op_to_mode(op));
125
126 paint.setXfermode(mode);
127 paint.setAntiAlias(antiAlias);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000128 paint.setColor(color);
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000129
130 fDraw.drawRect(clientRect, paint);
131
132 SkSafeUnref(mode);
133}
134
135/**
136 * Draw a single path element of the clip stack into the accumulation bitmap
137 */
138void GrSWMaskHelper::draw(const SkPath& clientPath, SkRegion::Op op,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000139 GrPathFill fill, bool antiAlias, GrColor color) {
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000140
141 SkPaint paint;
142 SkPath tmpPath;
143 const SkPath* pathToDraw = &clientPath;
bsalomon@google.com47059542012-06-06 20:51:20 +0000144 if (kHairLine_GrPathFill == fill) {
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000145 paint.setStyle(SkPaint::kStroke_Style);
146 paint.setStrokeWidth(SK_Scalar1);
147 } else {
148 paint.setStyle(SkPaint::kFill_Style);
149 SkPath::FillType skfill = gr_fill_to_sk_fill(fill);
150 if (skfill != pathToDraw->getFillType()) {
151 tmpPath = *pathToDraw;
152 tmpPath.setFillType(skfill);
153 pathToDraw = &tmpPath;
154 }
155 }
156 SkXfermode* mode = SkXfermode::Create(op_to_mode(op));
157
158 paint.setXfermode(mode);
159 paint.setAntiAlias(antiAlias);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000160 paint.setColor(color);
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000161
162 fDraw.drawPath(*pathToDraw, paint);
163
164 SkSafeUnref(mode);
165}
166
robertphillips@google.comfa662942012-05-17 12:20:22 +0000167bool GrSWMaskHelper::init(const GrIRect& pathDevBounds,
168 const GrPoint* translate,
169 bool useMatrix) {
170 if (useMatrix) {
171 fMatrix = fContext->getMatrix();
172 } else {
173 fMatrix.setIdentity();
174 }
175
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000176 if (NULL != translate) {
177 fMatrix.postTranslate(translate->fX, translate->fY);
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000178 }
179
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000180 fMatrix.postTranslate(-pathDevBounds.fLeft * SK_Scalar1,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000181 -pathDevBounds.fTop * SK_Scalar1);
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000182 GrIRect bounds = GrIRect::MakeWH(pathDevBounds.width(),
robertphillips@google.comfa662942012-05-17 12:20:22 +0000183 pathDevBounds.height());
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000184
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000185 fBM.setConfig(SkBitmap::kA8_Config, bounds.fRight, bounds.fBottom);
186 if (!fBM.allocPixels()) {
187 return false;
188 }
189 sk_bzero(fBM.getPixels(), fBM.getSafeSize());
190
191 sk_bzero(&fDraw, sizeof(fDraw));
192 fRasterClip.setRect(bounds);
193 fDraw.fRC = &fRasterClip;
194 fDraw.fClip = &fRasterClip.bwRgn();
195 fDraw.fMatrix = &fMatrix;
196 fDraw.fBitmap = &fBM;
197 return true;
198}
199
200/**
201 * Get a texture (from the texture cache) of the correct size & format
202 */
203bool GrSWMaskHelper::getTexture(GrAutoScratchTexture* tex) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000204 GrTextureDesc desc;
205 desc.fWidth = fBM.width();
206 desc.fHeight = fBM.height();
207 desc.fConfig = kAlpha_8_GrPixelConfig;
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000208
209 tex->set(fContext, desc);
210 GrTexture* texture = tex->texture();
211
212 if (NULL == texture) {
213 return false;
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000214 }
215
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000216 return true;
217}
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000218
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000219/**
220 * Move the result of the software mask generation back to the gpu
221 */
222void GrSWMaskHelper::toTexture(GrTexture *texture) {
223 SkAutoLockPixels alp(fBM);
224 texture->writePixels(0, 0, fBM.width(), fBM.height(),
225 kAlpha_8_GrPixelConfig,
226 fBM.getPixels(), fBM.rowBytes());
227}
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000228
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000229namespace {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000230////////////////////////////////////////////////////////////////////////////////
231/**
232 * sw rasterizes path to A8 mask using the context's matrix and uploads to a
233 * scratch texture.
234 */
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000235bool sw_draw_path_to_mask_texture(const SkPath& clientPath,
236 const GrIRect& pathDevBounds,
237 GrPathFill fill,
238 GrContext* context,
239 const GrPoint* translate,
240 GrAutoScratchTexture* tex,
241 bool antiAlias) {
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000242 GrSWMaskHelper helper(context);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000243
robertphillips@google.comfa662942012-05-17 12:20:22 +0000244 if (!helper.init(pathDevBounds, translate, true)) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000245 return false;
246 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000247
robertphillips@google.comfa662942012-05-17 12:20:22 +0000248 helper.draw(clientPath, SkRegion::kReplace_Op,
249 fill, antiAlias, SK_ColorWHITE);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000250
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000251 if (!helper.getTexture(tex)) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000252 return false;
253 }
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000254
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000255 helper.toTexture(tex->texture());
256
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000257 return true;
258}
259
260////////////////////////////////////////////////////////////////////////////////
261void draw_around_inv_path(GrDrawTarget* target,
262 GrDrawState::StageMask stageMask,
263 const GrIRect& clipBounds,
264 const GrIRect& pathBounds) {
265 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
266 GrRect rect;
267 if (clipBounds.fTop < pathBounds.fTop) {
268 rect.iset(clipBounds.fLeft, clipBounds.fTop,
269 clipBounds.fRight, pathBounds.fTop);
270 target->drawSimpleRect(rect, NULL, stageMask);
271 }
272 if (clipBounds.fLeft < pathBounds.fLeft) {
273 rect.iset(clipBounds.fLeft, pathBounds.fTop,
274 pathBounds.fLeft, pathBounds.fBottom);
275 target->drawSimpleRect(rect, NULL, stageMask);
276 }
277 if (clipBounds.fRight > pathBounds.fRight) {
278 rect.iset(pathBounds.fRight, pathBounds.fTop,
279 clipBounds.fRight, pathBounds.fBottom);
280 target->drawSimpleRect(rect, NULL, stageMask);
281 }
282 if (clipBounds.fBottom > pathBounds.fBottom) {
283 rect.iset(clipBounds.fLeft, pathBounds.fBottom,
284 clipBounds.fRight, clipBounds.fBottom);
285 target->drawSimpleRect(rect, NULL, stageMask);
286 }
287}
288
289}
290
291////////////////////////////////////////////////////////////////////////////////
292// return true on success; false on failure
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000293bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path,
294 GrPathFill fill,
295 const GrVec* translate,
296 GrDrawTarget* target,
297 GrDrawState::StageMask stageMask,
298 bool antiAlias) {
299
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000300 if (NULL == fContext) {
301 return false;
302 }
303
304 GrAutoScratchTexture ast;
305 GrIRect pathBounds, clipBounds;
306 if (!get_path_and_clip_bounds(target, path, translate,
307 &pathBounds, &clipBounds)) {
308 return true; // path is empty so there is nothing to do
309 }
310 if (sw_draw_path_to_mask_texture(path, pathBounds,
311 fill, fContext,
312 translate, &ast, antiAlias)) {
313 GrTexture* texture = ast.texture();
314 GrAssert(NULL != texture);
315 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
316 enum {
robertphillips@google.com7ff2e372012-05-02 19:27:44 +0000317 // the SW path renderer shares this stage with glyph
318 // rendering (kGlyphMaskStage in GrBatchedTextContext)
319 kPathMaskStage = GrPaint::kTotalStages,
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000320 };
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000321 GrAssert(NULL == target->drawState()->getTexture(kPathMaskStage));
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000322 target->drawState()->setTexture(kPathMaskStage, texture);
323 target->drawState()->sampler(kPathMaskStage)->reset();
324 GrScalar w = GrIntToScalar(pathBounds.width());
325 GrScalar h = GrIntToScalar(pathBounds.height());
326 GrRect maskRect = GrRect::MakeWH(w / texture->width(),
327 h / texture->height());
328 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
329 srcRects[kPathMaskStage] = &maskRect;
330 stageMask |= 1 << kPathMaskStage;
331 GrRect dstRect = GrRect::MakeLTRB(
332 SK_Scalar1* pathBounds.fLeft,
333 SK_Scalar1* pathBounds.fTop,
334 SK_Scalar1* pathBounds.fRight,
335 SK_Scalar1* pathBounds.fBottom);
336 target->drawRect(dstRect, NULL, stageMask, srcRects, NULL);
337 target->drawState()->setTexture(kPathMaskStage, NULL);
338 if (GrIsFillInverted(fill)) {
339 draw_around_inv_path(target, stageMask,
340 clipBounds, pathBounds);
341 }
342 return true;
343 }
344
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000345 return false;
346}