blob: 817f55a86df7e038ca3d7216d72c2db7dfa6b7e4 [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) {
40 case kWinding_PathFill:
41 return SkPath::kWinding_FillType;
42 case kEvenOdd_PathFill:
43 return SkPath::kEvenOdd_FillType;
44 case kInverseWinding_PathFill:
45 return SkPath::kInverseWinding_FillType;
46 case kInverseEvenOdd_PathFill:
47 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;
144 if (kHairLine_PathFill == fill) {
145 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) {
204 const GrTextureDesc desc = {
205 kNone_GrTextureFlags,
206 fBM.width(),
207 fBM.height(),
208 kAlpha_8_GrPixelConfig,
209 0 // samples
210 };
211
212 tex->set(fContext, desc);
213 GrTexture* texture = tex->texture();
214
215 if (NULL == texture) {
216 return false;
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000217 }
218
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000219 return true;
220}
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000221
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000222/**
223 * Move the result of the software mask generation back to the gpu
224 */
225void GrSWMaskHelper::toTexture(GrTexture *texture) {
226 SkAutoLockPixels alp(fBM);
227 texture->writePixels(0, 0, fBM.width(), fBM.height(),
228 kAlpha_8_GrPixelConfig,
229 fBM.getPixels(), fBM.rowBytes());
230}
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000231
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000232namespace {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000233////////////////////////////////////////////////////////////////////////////////
234/**
235 * sw rasterizes path to A8 mask using the context's matrix and uploads to a
236 * scratch texture.
237 */
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000238bool sw_draw_path_to_mask_texture(const SkPath& clientPath,
239 const GrIRect& pathDevBounds,
240 GrPathFill fill,
241 GrContext* context,
242 const GrPoint* translate,
243 GrAutoScratchTexture* tex,
244 bool antiAlias) {
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000245 GrSWMaskHelper helper(context);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000246
robertphillips@google.comfa662942012-05-17 12:20:22 +0000247 if (!helper.init(pathDevBounds, translate, true)) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000248 return false;
249 }
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000250
robertphillips@google.comfa662942012-05-17 12:20:22 +0000251 helper.draw(clientPath, SkRegion::kReplace_Op,
252 fill, antiAlias, SK_ColorWHITE);
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000253
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000254 if (!helper.getTexture(tex)) {
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000255 return false;
256 }
robertphillips@google.com6f31a3b2012-05-14 17:37:05 +0000257
robertphillips@google.comb4f06d72012-05-15 12:10:05 +0000258 helper.toTexture(tex->texture());
259
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000260 return true;
261}
262
263////////////////////////////////////////////////////////////////////////////////
264void draw_around_inv_path(GrDrawTarget* target,
265 GrDrawState::StageMask stageMask,
266 const GrIRect& clipBounds,
267 const GrIRect& pathBounds) {
268 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
269 GrRect rect;
270 if (clipBounds.fTop < pathBounds.fTop) {
271 rect.iset(clipBounds.fLeft, clipBounds.fTop,
272 clipBounds.fRight, pathBounds.fTop);
273 target->drawSimpleRect(rect, NULL, stageMask);
274 }
275 if (clipBounds.fLeft < pathBounds.fLeft) {
276 rect.iset(clipBounds.fLeft, pathBounds.fTop,
277 pathBounds.fLeft, pathBounds.fBottom);
278 target->drawSimpleRect(rect, NULL, stageMask);
279 }
280 if (clipBounds.fRight > pathBounds.fRight) {
281 rect.iset(pathBounds.fRight, pathBounds.fTop,
282 clipBounds.fRight, pathBounds.fBottom);
283 target->drawSimpleRect(rect, NULL, stageMask);
284 }
285 if (clipBounds.fBottom > pathBounds.fBottom) {
286 rect.iset(clipBounds.fLeft, pathBounds.fBottom,
287 clipBounds.fRight, clipBounds.fBottom);
288 target->drawSimpleRect(rect, NULL, stageMask);
289 }
290}
291
292}
293
294////////////////////////////////////////////////////////////////////////////////
295// return true on success; false on failure
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000296bool GrSoftwarePathRenderer::onDrawPath(const SkPath& path,
297 GrPathFill fill,
298 const GrVec* translate,
299 GrDrawTarget* target,
300 GrDrawState::StageMask stageMask,
301 bool antiAlias) {
302
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000303 if (NULL == fContext) {
304 return false;
305 }
306
307 GrAutoScratchTexture ast;
308 GrIRect pathBounds, clipBounds;
309 if (!get_path_and_clip_bounds(target, path, translate,
310 &pathBounds, &clipBounds)) {
311 return true; // path is empty so there is nothing to do
312 }
313 if (sw_draw_path_to_mask_texture(path, pathBounds,
314 fill, fContext,
315 translate, &ast, antiAlias)) {
316 GrTexture* texture = ast.texture();
317 GrAssert(NULL != texture);
318 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
319 enum {
robertphillips@google.com7ff2e372012-05-02 19:27:44 +0000320 // the SW path renderer shares this stage with glyph
321 // rendering (kGlyphMaskStage in GrBatchedTextContext)
322 kPathMaskStage = GrPaint::kTotalStages,
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000323 };
robertphillips@google.combeeb97c2012-05-09 21:15:28 +0000324 GrAssert(NULL == target->drawState()->getTexture(kPathMaskStage));
robertphillips@google.comed4155d2012-05-01 14:30:24 +0000325 target->drawState()->setTexture(kPathMaskStage, texture);
326 target->drawState()->sampler(kPathMaskStage)->reset();
327 GrScalar w = GrIntToScalar(pathBounds.width());
328 GrScalar h = GrIntToScalar(pathBounds.height());
329 GrRect maskRect = GrRect::MakeWH(w / texture->width(),
330 h / texture->height());
331 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
332 srcRects[kPathMaskStage] = &maskRect;
333 stageMask |= 1 << kPathMaskStage;
334 GrRect dstRect = GrRect::MakeLTRB(
335 SK_Scalar1* pathBounds.fLeft,
336 SK_Scalar1* pathBounds.fTop,
337 SK_Scalar1* pathBounds.fRight,
338 SK_Scalar1* pathBounds.fBottom);
339 target->drawRect(dstRect, NULL, stageMask, srcRects, NULL);
340 target->drawState()->setTexture(kPathMaskStage, NULL);
341 if (GrIsFillInverted(fill)) {
342 draw_around_inv_path(target, stageMask,
343 clipBounds, pathBounds);
344 }
345 return true;
346 }
347
robertphillips@google.comf4c2c522012-04-27 12:08:47 +0000348 return false;
349}