blob: b9c39fa8ceed6c0f44d7983bdbd34c149cc62eaa [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkDraw.h"
9#include "SkBlitter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
11#include "SkColorPriv.h"
12#include "SkDevice.h"
reed@google.com1c028bd2013-08-28 15:23:19 +000013#include "SkDeviceLooper.h"
bungeman@google.com2211b622012-01-13 15:02:58 +000014#include "SkFixed.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkMaskFilter.h"
16#include "SkPaint.h"
17#include "SkPathEffect.h"
reed@google.com045e62d2011-10-24 12:19:46 +000018#include "SkRasterClip.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkRasterizer.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000020#include "SkRRect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkScan.h"
22#include "SkShader.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000023#include "SkSmallAllocator.h"
robertphillips@google.com76f9e932013-01-15 20:17:47 +000024#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkStroke.h"
kkinnunencb9a2c82014-06-12 23:06:28 -070026#include "SkTextMapStateProc.h"
reed@google.com32e5d972011-07-27 18:25:57 +000027#include "SkTLazy.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000028#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000029#include "SkVertState.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000030
31#include "SkAutoKern.h"
32#include "SkBitmapProcShader.h"
33#include "SkDrawProcs.h"
reed@google.comae573582013-01-03 15:22:40 +000034#include "SkMatrixUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037//#define TRACE_BITMAP_DRAWS
38
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
reed@google.comfd4236e2011-07-25 21:16:22 +000040/** Helper for allocating small blitters on the stack.
41 */
reed@google.com40c2ba22011-07-25 19:41:22 +000042class SkAutoBlitterChoose : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043public:
reed@google.com1d6ee0b2011-07-05 17:44:56 +000044 SkAutoBlitterChoose() {
45 fBlitter = NULL;
46 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 SkAutoBlitterChoose(const SkBitmap& device, const SkMatrix& matrix,
reed@google.com126f7f52013-11-07 16:06:53 +000048 const SkPaint& paint, bool drawCoverage = false) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000049 fBlitter = SkBlitter::Choose(device, matrix, paint, &fAllocator,
50 drawCoverage);
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +000051 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000052
53 SkBlitter* operator->() { return fBlitter; }
54 SkBlitter* get() const { return fBlitter; }
55
reed@google.com1d6ee0b2011-07-05 17:44:56 +000056 void choose(const SkBitmap& device, const SkMatrix& matrix,
krajcevski53f09592014-08-06 11:12:14 -070057 const SkPaint& paint, bool drawCoverage = false) {
reed@google.com1d6ee0b2011-07-05 17:44:56 +000058 SkASSERT(!fBlitter);
krajcevski53f09592014-08-06 11:12:14 -070059 fBlitter = SkBlitter::Choose(device, matrix, paint, &fAllocator,
60 drawCoverage);
reed@google.com1d6ee0b2011-07-05 17:44:56 +000061 }
62
reed@android.com8a1c16f2008-12-17 15:59:43 +000063private:
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000064 // Owned by fAllocator, which will handle the delete.
65 SkBlitter* fBlitter;
66 SkTBlitterAllocator fAllocator;
reed@android.com8a1c16f2008-12-17 15:59:43 +000067};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +000068#define SkAutoBlitterChoose(...) SK_REQUIRE_LOCAL_VAR(SkAutoBlitterChoose)
reed@android.com8a1c16f2008-12-17 15:59:43 +000069
reed@google.com40c2ba22011-07-25 19:41:22 +000070/**
71 * Since we are providing the storage for the shader (to avoid the perf cost
72 * of calling new) we insist that in our destructor we can account for all
73 * owners of the shader.
74 */
75class SkAutoBitmapShaderInstall : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000076public:
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000077 SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint,
78 const SkMatrix* localMatrix = NULL)
reed@google.com40c2ba22011-07-25 19:41:22 +000079 : fPaint(paint) /* makes a copy of the paint */ {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000080 fPaint.setShader(CreateBitmapShader(src, SkShader::kClamp_TileMode,
81 SkShader::kClamp_TileMode,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000082 localMatrix, &fAllocator));
reed@google.com40c2ba22011-07-25 19:41:22 +000083 // we deliberately left the shader with an owner-count of 2
84 SkASSERT(2 == fPaint.getShader()->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 }
reed@google.com82065d62011-02-07 15:30:46 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 ~SkAutoBitmapShaderInstall() {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000088 // since fAllocator will destroy shader, we insist that owners == 2
89 SkASSERT(2 == fPaint.getShader()->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
reed@google.com40c2ba22011-07-25 19:41:22 +000091 fPaint.setShader(NULL); // unref the shader by 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 }
reed@google.com82065d62011-02-07 15:30:46 +000094
reed@google.com40c2ba22011-07-25 19:41:22 +000095 // return the new paint that has the shader applied
96 const SkPaint& paintWithShader() const { return fPaint; }
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098private:
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000099 // copy of caller's paint (which we then modify)
100 SkPaint fPaint;
101 // Stores the shader.
102 SkTBlitterAllocator fAllocator;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000104#define SkAutoBitmapShaderInstall(...) SK_REQUIRE_LOCAL_VAR(SkAutoBitmapShaderInstall)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106///////////////////////////////////////////////////////////////////////////////
107
reed@android.comf2b98d62010-12-20 18:26:13 +0000108SkDraw::SkDraw() {
109 sk_bzero(this, sizeof(*this));
110}
111
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112SkDraw::SkDraw(const SkDraw& src) {
113 memcpy(this, &src, sizeof(*this));
114}
115
reed@google.com4bbdeac2013-01-24 21:03:11 +0000116bool SkDraw::computeConservativeLocalClipBounds(SkRect* localBounds) const {
117 if (fRC->isEmpty()) {
118 return false;
119 }
120
121 SkMatrix inverse;
122 if (!fMatrix->invert(&inverse)) {
123 return false;
124 }
125
126 SkIRect devBounds = fRC->getBounds();
127 // outset to have slop for antialasing and hairlines
128 devBounds.outset(1, 1);
129 inverse.mapRect(localBounds, SkRect::Make(devBounds));
130 return true;
131}
132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133///////////////////////////////////////////////////////////////////////////////
134
135typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
136
137static void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) {
reed@android.com4516f472009-06-29 16:25:36 +0000138 sk_bzero(pixels, bytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139}
140
141static void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {}
142
143static void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000144 sk_memset32((uint32_t*)pixels, data, SkToInt(bytes >> 2));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145}
146
147static void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000148 sk_memset16((uint16_t*)pixels, data, SkToInt(bytes >> 1));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149}
150
151static void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
152 memset(pixels, data, bytes);
153}
154
155static BitmapXferProc ChooseBitmapXferProc(const SkBitmap& bitmap,
156 const SkPaint& paint,
157 uint32_t* data) {
158 // todo: we can apply colorfilter up front if no shader, so we wouldn't
159 // need to abort this fastpath
160 if (paint.getShader() || paint.getColorFilter()) {
161 return NULL;
162 }
163
reed@android.com845fdac2009-06-23 03:01:32 +0000164 SkXfermode::Mode mode;
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +0000165 if (!SkXfermode::AsMode(paint.getXfermode(), &mode)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 return NULL;
167 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000168
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 SkColor color = paint.getColor();
reed@google.coma76de3d2011-01-13 18:30:42 +0000170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 // collaps modes based on color...
reed@android.com845fdac2009-06-23 03:01:32 +0000172 if (SkXfermode::kSrcOver_Mode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 unsigned alpha = SkColorGetA(color);
174 if (0 == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000175 mode = SkXfermode::kDst_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 } else if (0xFF == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000177 mode = SkXfermode::kSrc_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 }
179 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000180
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 switch (mode) {
reed@android.com845fdac2009-06-23 03:01:32 +0000182 case SkXfermode::kClear_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183// SkDebugf("--- D_Clear_BitmapXferProc\n");
184 return D_Clear_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000185 case SkXfermode::kDst_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186// SkDebugf("--- D_Dst_BitmapXferProc\n");
187 return D_Dst_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000188 case SkXfermode::kSrc_Mode: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 /*
reed@google.coma76de3d2011-01-13 18:30:42 +0000190 should I worry about dithering for the lower depths?
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 */
192 SkPMColor pmc = SkPreMultiplyColor(color);
reed@google.com900ecf22014-02-20 20:55:37 +0000193 switch (bitmap.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000194 case kN32_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 if (data) {
196 *data = pmc;
197 }
198// SkDebugf("--- D32_Src_BitmapXferProc\n");
199 return D32_Src_BitmapXferProc;
reed@google.com900ecf22014-02-20 20:55:37 +0000200 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 if (data) {
202 *data = SkPixel32ToPixel16(pmc);
203 }
204// SkDebugf("--- D16_Src_BitmapXferProc\n");
205 return D16_Src_BitmapXferProc;
reed@google.com900ecf22014-02-20 20:55:37 +0000206 case kAlpha_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 if (data) {
208 *data = SkGetPackedA32(pmc);
209 }
210// SkDebugf("--- DA8_Src_BitmapXferProc\n");
211 return DA8_Src_BitmapXferProc;
212 default:
213 break;
214 }
215 break;
216 }
217 default:
218 break;
219 }
220 return NULL;
221}
222
223static void CallBitmapXferProc(const SkBitmap& bitmap, const SkIRect& rect,
224 BitmapXferProc proc, uint32_t procData) {
225 int shiftPerPixel;
reed@google.com900ecf22014-02-20 20:55:37 +0000226 switch (bitmap.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000227 case kN32_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 shiftPerPixel = 2;
229 break;
reed@google.com900ecf22014-02-20 20:55:37 +0000230 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 shiftPerPixel = 1;
232 break;
reed@google.com900ecf22014-02-20 20:55:37 +0000233 case kAlpha_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 shiftPerPixel = 0;
235 break;
236 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000237 SkDEBUGFAIL("Can't use xferproc on this config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 return;
239 }
240
241 uint8_t* pixels = (uint8_t*)bitmap.getPixels();
242 SkASSERT(pixels);
243 const size_t rowBytes = bitmap.rowBytes();
244 const int widthBytes = rect.width() << shiftPerPixel;
245
246 // skip down to the first scanline and X position
247 pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel);
248 for (int scans = rect.height() - 1; scans >= 0; --scans) {
249 proc(pixels, widthBytes, procData);
250 pixels += rowBytes;
251 }
252}
253
254void SkDraw::drawPaint(const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000255 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256
reed@google.com045e62d2011-10-24 12:19:46 +0000257 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 return;
259 }
260
261 SkIRect devRect;
262 devRect.set(0, 0, fBitmap->width(), fBitmap->height());
reed@google.coma76de3d2011-01-13 18:30:42 +0000263
reed@google.com045e62d2011-10-24 12:19:46 +0000264 if (fRC->isBW()) {
265 /* If we don't have a shader (i.e. we're just a solid color) we may
266 be faster to operate directly on the device bitmap, rather than invoking
267 a blitter. Esp. true for xfermodes, which require a colorshader to be
268 present, which is just redundant work. Since we're drawing everywhere
269 in the clip, we don't have to worry about antialiasing.
270 */
271 uint32_t procData = 0; // to avoid the warning
272 BitmapXferProc proc = ChooseBitmapXferProc(*fBitmap, paint, &procData);
273 if (proc) {
274 if (D_Dst_BitmapXferProc == proc) { // nothing to do
275 return;
276 }
277
278 SkRegion::Iterator iter(fRC->bwRgn());
279 while (!iter.done()) {
280 CallBitmapXferProc(*fBitmap, iter.rect(), proc, procData);
281 iter.next();
282 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 return;
284 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 }
reed@google.com045e62d2011-10-24 12:19:46 +0000286
287 // normal case: use a blitter
288 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
289 SkScan::FillIRect(devRect, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290}
291
292///////////////////////////////////////////////////////////////////////////////
293
294struct PtProcRec {
295 SkCanvas::PointMode fMode;
296 const SkPaint* fPaint;
297 const SkRegion* fClip;
reed@google.com045e62d2011-10-24 12:19:46 +0000298 const SkRasterClip* fRC;
reed@google.coma76de3d2011-01-13 18:30:42 +0000299
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 // computed values
301 SkFixed fRadius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000302
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count,
304 SkBlitter*);
305
306 bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
reed@google.com045e62d2011-10-24 12:19:46 +0000307 const SkRasterClip*);
308 Proc chooseProc(SkBlitter** blitter);
309
310private:
311 SkAAClipBlitterWrapper fWrapper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312};
313
314static void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
315 int count, SkBlitter* blitter) {
316 SkASSERT(rec.fClip->isRect());
317 const SkIRect& r = rec.fClip->getBounds();
reed@google.coma76de3d2011-01-13 18:30:42 +0000318
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 for (int i = 0; i < count; i++) {
reed@google.com2d47a212012-11-29 21:01:00 +0000320 int x = SkScalarFloorToInt(devPts[i].fX);
321 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 if (r.contains(x, y)) {
323 blitter->blitH(x, y, 1);
324 }
325 }
326}
327
328static void bw_pt_rect_16_hair_proc(const PtProcRec& rec,
329 const SkPoint devPts[], int count,
330 SkBlitter* blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +0000331 SkASSERT(rec.fRC->isRect());
332 const SkIRect& r = rec.fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 uint32_t value;
334 const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
335 SkASSERT(bitmap);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000336
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337 uint16_t* addr = bitmap->getAddr16(0, 0);
scroggo@google.come5f48242013-02-25 21:47:41 +0000338 size_t rb = bitmap->rowBytes();
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000339
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 for (int i = 0; i < count; i++) {
reed@google.com2d47a212012-11-29 21:01:00 +0000341 int x = SkScalarFloorToInt(devPts[i].fX);
342 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 if (r.contains(x, y)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
345 }
346 }
347}
348
reed@google.com2d47a212012-11-29 21:01:00 +0000349static void bw_pt_rect_32_hair_proc(const PtProcRec& rec,
350 const SkPoint devPts[], int count,
351 SkBlitter* blitter) {
352 SkASSERT(rec.fRC->isRect());
353 const SkIRect& r = rec.fRC->getBounds();
354 uint32_t value;
355 const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
356 SkASSERT(bitmap);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000357
reed@google.com2d47a212012-11-29 21:01:00 +0000358 SkPMColor* addr = bitmap->getAddr32(0, 0);
scroggo@google.come5f48242013-02-25 21:47:41 +0000359 size_t rb = bitmap->rowBytes();
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000360
reed@google.com2d47a212012-11-29 21:01:00 +0000361 for (int i = 0; i < count; i++) {
362 int x = SkScalarFloorToInt(devPts[i].fX);
363 int y = SkScalarFloorToInt(devPts[i].fY);
364 if (r.contains(x, y)) {
365 ((SkPMColor*)((char*)addr + y * rb))[x] = value;
366 }
367 }
368}
369
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370static void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
371 int count, SkBlitter* blitter) {
372 for (int i = 0; i < count; i++) {
reed@google.come1ca7052013-12-17 19:22:07 +0000373 int x = SkScalarFloorToInt(devPts[i].fX);
374 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375 if (rec.fClip->contains(x, y)) {
376 blitter->blitH(x, y, 1);
377 }
378 }
379}
380
381static void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
382 int count, SkBlitter* blitter) {
383 for (int i = 0; i < count; i += 2) {
reed@google.com045e62d2011-10-24 12:19:46 +0000384 SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 }
386}
387
388static void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
389 int count, SkBlitter* blitter) {
390 for (int i = 0; i < count - 1; i++) {
reed@google.com045e62d2011-10-24 12:19:46 +0000391 SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392 }
393}
394
395// aa versions
396
397static void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
398 int count, SkBlitter* blitter) {
399 for (int i = 0; i < count; i += 2) {
reed@google.com045e62d2011-10-24 12:19:46 +0000400 SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 }
402}
403
404static void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
405 int count, SkBlitter* blitter) {
406 for (int i = 0; i < count - 1; i++) {
reed@google.com045e62d2011-10-24 12:19:46 +0000407 SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000408 }
409}
410
411// square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
412
413static void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
414 int count, SkBlitter* blitter) {
415 const SkFixed radius = rec.fRadius;
416 for (int i = 0; i < count; i++) {
417 SkFixed x = SkScalarToFixed(devPts[i].fX);
418 SkFixed y = SkScalarToFixed(devPts[i].fY);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000419
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 SkXRect r;
421 r.fLeft = x - radius;
422 r.fTop = y - radius;
423 r.fRight = x + radius;
424 r.fBottom = y + radius;
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000425
reed@google.com045e62d2011-10-24 12:19:46 +0000426 SkScan::FillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427 }
428}
429
430static void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
431 int count, SkBlitter* blitter) {
432 const SkFixed radius = rec.fRadius;
433 for (int i = 0; i < count; i++) {
434 SkFixed x = SkScalarToFixed(devPts[i].fX);
435 SkFixed y = SkScalarToFixed(devPts[i].fY);
reed@google.coma76de3d2011-01-13 18:30:42 +0000436
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437 SkXRect r;
438 r.fLeft = x - radius;
439 r.fTop = y - radius;
440 r.fRight = x + radius;
441 r.fBottom = y + radius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000442
reed@google.com045e62d2011-10-24 12:19:46 +0000443 SkScan::AntiFillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 }
445}
446
reed@android.comb4f404a2009-07-10 17:02:17 +0000447// If this guy returns true, then chooseProc() must return a valid proc
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
reed@google.com045e62d2011-10-24 12:19:46 +0000449 const SkMatrix* matrix, const SkRasterClip* rc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450 if (paint.getPathEffect()) {
451 return false;
452 }
453 SkScalar width = paint.getStrokeWidth();
454 if (0 == width) {
455 fMode = mode;
456 fPaint = &paint;
reed@google.com045e62d2011-10-24 12:19:46 +0000457 fClip = NULL;
458 fRC = rc;
reed@google.com2d47a212012-11-29 21:01:00 +0000459 fRadius = SK_FixedHalf;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 return true;
461 }
reed@android.comb4f404a2009-07-10 17:02:17 +0000462 if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
robertphillips9f2251c2014-11-04 13:33:50 -0800463 matrix->isScaleTranslate() && SkCanvas::kPoints_PointMode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464 SkScalar sx = matrix->get(SkMatrix::kMScaleX);
465 SkScalar sy = matrix->get(SkMatrix::kMScaleY);
466 if (SkScalarNearlyZero(sx - sy)) {
467 if (sx < 0) {
468 sx = -sx;
469 }
470
471 fMode = mode;
472 fPaint = &paint;
reed@google.com045e62d2011-10-24 12:19:46 +0000473 fClip = NULL;
474 fRC = rc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475 fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
476 return true;
477 }
478 }
479 return false;
480}
481
reed@google.com045e62d2011-10-24 12:19:46 +0000482PtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
reed@android.comb4f404a2009-07-10 17:02:17 +0000483 Proc proc = NULL;
reed@google.coma76de3d2011-01-13 18:30:42 +0000484
reed@google.com045e62d2011-10-24 12:19:46 +0000485 SkBlitter* blitter = *blitterPtr;
486 if (fRC->isBW()) {
487 fClip = &fRC->bwRgn();
488 } else {
489 fWrapper.init(*fRC, blitter);
490 fClip = &fWrapper.getRgn();
491 blitter = fWrapper.getBlitter();
492 *blitterPtr = blitter;
493 }
494
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495 // for our arrays
496 SkASSERT(0 == SkCanvas::kPoints_PointMode);
497 SkASSERT(1 == SkCanvas::kLines_PointMode);
498 SkASSERT(2 == SkCanvas::kPolygon_PointMode);
499 SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
500
reed@google.com2d47a212012-11-29 21:01:00 +0000501 if (fPaint->isAntiAlias()) {
502 if (0 == fPaint->getStrokeWidth()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503 static const Proc gAAProcs[] = {
504 aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
505 };
506 proc = gAAProcs[fMode];
reed@google.com2d47a212012-11-29 21:01:00 +0000507 } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
508 SkASSERT(SkCanvas::kPoints_PointMode == fMode);
509 proc = aa_square_proc;
510 }
511 } else { // BW
512 if (fRadius <= SK_FixedHalf) { // small radii and hairline
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
514 uint32_t value;
515 const SkBitmap* bm = blitter->justAnOpaqueColor(&value);
reed@google.com900ecf22014-02-20 20:55:37 +0000516 if (bm && kRGB_565_SkColorType == bm->colorType()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517 proc = bw_pt_rect_16_hair_proc;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000518 } else if (bm && kN32_SkColorType == bm->colorType()) {
reed@google.com2d47a212012-11-29 21:01:00 +0000519 proc = bw_pt_rect_32_hair_proc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 } else {
521 proc = bw_pt_rect_hair_proc;
522 }
523 } else {
524 static Proc gBWProcs[] = {
525 bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
526 };
527 proc = gBWProcs[fMode];
528 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529 } else {
530 proc = bw_square_proc;
531 }
532 }
533 return proc;
534}
535
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536// each of these costs 8-bytes of stack space, so don't make it too large
537// must be even for lines/polygon to work
538#define MAX_DEV_PTS 32
539
540void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
reed@android.comf2b98d62010-12-20 18:26:13 +0000541 const SkPoint pts[], const SkPaint& paint,
542 bool forceUseDevice) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000543 // if we're in lines mode, force count to be even
544 if (SkCanvas::kLines_PointMode == mode) {
545 count &= ~(size_t)1;
546 }
547
548 if ((long)count <= 0) {
549 return;
550 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000551
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552 SkASSERT(pts != NULL);
reed@android.comf2b98d62010-12-20 18:26:13 +0000553 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +0000554
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000556 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557 return;
558 }
559
560 PtProcRec rec;
reed@google.com045e62d2011-10-24 12:19:46 +0000561 if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000562 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
563
564 SkPoint devPts[MAX_DEV_PTS];
565 const SkMatrix* matrix = fMatrix;
566 SkBlitter* bltr = blitter.get();
reed@google.com045e62d2011-10-24 12:19:46 +0000567 PtProcRec::Proc proc = rec.chooseProc(&bltr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568 // we have to back up subsequent passes if we're in polygon mode
569 const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
reed@google.coma76de3d2011-01-13 18:30:42 +0000570
reed@android.com8a1c16f2008-12-17 15:59:43 +0000571 do {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000572 int n = SkToInt(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573 if (n > MAX_DEV_PTS) {
574 n = MAX_DEV_PTS;
575 }
576 matrix->mapPoints(devPts, pts, n);
577 proc(rec, devPts, n, bltr);
578 pts += n - backup;
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000579 SkASSERT(SkToInt(count) >= n);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000580 count -= n;
581 if (count > 0) {
582 count += backup;
583 }
584 } while (count != 0);
585 } else {
586 switch (mode) {
587 case SkCanvas::kPoints_PointMode: {
588 // temporarily mark the paint as filling.
reed@google.com40c2ba22011-07-25 19:41:22 +0000589 SkPaint newPaint(paint);
590 newPaint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591
reed@google.com40c2ba22011-07-25 19:41:22 +0000592 SkScalar width = newPaint.getStrokeWidth();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000593 SkScalar radius = SkScalarHalf(width);
reed@google.coma76de3d2011-01-13 18:30:42 +0000594
reed@google.com40c2ba22011-07-25 19:41:22 +0000595 if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000596 SkPath path;
597 SkMatrix preMatrix;
jvanverthb3eb6872014-10-24 07:12:51 -0700598
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599 path.addCircle(0, 0, radius);
600 for (size_t i = 0; i < count; i++) {
601 preMatrix.setTranslate(pts[i].fX, pts[i].fY);
602 // pass true for the last point, since we can modify
603 // then path then
jvanverthb3eb6872014-10-24 07:12:51 -0700604 path.setIsVolatile((count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000605 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000606 fDevice->drawPath(*this, path, newPaint, &preMatrix,
reed@android.comf2b98d62010-12-20 18:26:13 +0000607 (count-1) == i);
608 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000609 this->drawPath(path, newPaint, &preMatrix,
610 (count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000611 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000612 }
613 } else {
614 SkRect r;
reed@google.coma76de3d2011-01-13 18:30:42 +0000615
reed@android.com8a1c16f2008-12-17 15:59:43 +0000616 for (size_t i = 0; i < count; i++) {
617 r.fLeft = pts[i].fX - radius;
618 r.fTop = pts[i].fY - radius;
619 r.fRight = r.fLeft + width;
620 r.fBottom = r.fTop + width;
reed@android.comf2b98d62010-12-20 18:26:13 +0000621 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000622 fDevice->drawRect(*this, r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000623 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000624 this->drawRect(r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000625 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000626 }
627 }
628 break;
629 }
630 case SkCanvas::kLines_PointMode:
bsalomon49f085d2014-09-05 13:34:00 -0700631 if (2 == count && paint.getPathEffect()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000632 // most likely a dashed line - see if it is one of the ones
633 // we can accelerate
634 SkStrokeRec rec(paint);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000635 SkPathEffect::PointData pointData;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000636
637 SkPath path;
638 path.moveTo(pts[0]);
639 path.lineTo(pts[1]);
640
reed@google.com4bbdeac2013-01-24 21:03:11 +0000641 SkRect cullRect = SkRect::Make(fRC->getBounds());
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000642
reed@google.com4bbdeac2013-01-24 21:03:11 +0000643 if (paint.getPathEffect()->asPoints(&pointData, path, rec,
644 *fMatrix, &cullRect)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000645 // 'asPoints' managed to find some fast path
646
robertphillips@google.com629ab542012-11-28 17:18:11 +0000647 SkPaint newP(paint);
648 newP.setPathEffect(NULL);
robertphillips@google.com935ad022012-12-05 19:07:21 +0000649 newP.setStyle(SkPaint::kFill_Style);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000650
robertphillips@google.com6d875572012-12-17 18:56:29 +0000651 if (!pointData.fFirst.isEmpty()) {
652 if (fDevice) {
653 fDevice->drawPath(*this, pointData.fFirst, newP);
654 } else {
655 this->drawPath(pointData.fFirst, newP);
656 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000657 }
robertphillips@google.com6d875572012-12-17 18:56:29 +0000658
659 if (!pointData.fLast.isEmpty()) {
660 if (fDevice) {
661 fDevice->drawPath(*this, pointData.fLast, newP);
662 } else {
663 this->drawPath(pointData.fLast, newP);
664 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000665 }
robertphillips@google.com6d875572012-12-17 18:56:29 +0000666
667 if (pointData.fSize.fX == pointData.fSize.fY) {
668 // The rest of the dashed line can just be drawn as points
669 SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth()));
670
671 if (SkPathEffect::PointData::kCircles_PointFlag & pointData.fFlags) {
672 newP.setStrokeCap(SkPaint::kRound_Cap);
673 } else {
674 newP.setStrokeCap(SkPaint::kButt_Cap);
675 }
676
677 if (fDevice) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000678 fDevice->drawPoints(*this,
robertphillips@google.com6d875572012-12-17 18:56:29 +0000679 SkCanvas::kPoints_PointMode,
680 pointData.fNumPoints,
681 pointData.fPoints,
682 newP);
683 } else {
684 this->drawPoints(SkCanvas::kPoints_PointMode,
685 pointData.fNumPoints,
686 pointData.fPoints,
687 newP,
688 forceUseDevice);
689 }
690 break;
691 } else {
692 // The rest of the dashed line must be drawn as rects
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000693 SkASSERT(!(SkPathEffect::PointData::kCircles_PointFlag &
robertphillips@google.com6d875572012-12-17 18:56:29 +0000694 pointData.fFlags));
695
696 SkRect r;
697
698 for (int i = 0; i < pointData.fNumPoints; ++i) {
699 r.set(pointData.fPoints[i].fX - pointData.fSize.fX,
700 pointData.fPoints[i].fY - pointData.fSize.fY,
701 pointData.fPoints[i].fX + pointData.fSize.fX,
702 pointData.fPoints[i].fY + pointData.fSize.fY);
703 if (fDevice) {
704 fDevice->drawRect(*this, r, newP);
705 } else {
706 this->drawRect(r, newP);
707 }
708 }
709 }
710
robertphillips@google.com629ab542012-11-28 17:18:11 +0000711 break;
712 }
713 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000714 // couldn't take fast path so fall through!
reed@android.com8a1c16f2008-12-17 15:59:43 +0000715 case SkCanvas::kPolygon_PointMode: {
716 count -= 1;
717 SkPath path;
718 SkPaint p(paint);
719 p.setStyle(SkPaint::kStroke_Style);
720 size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
jvanverthb3eb6872014-10-24 07:12:51 -0700721 path.setIsVolatile(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000722 for (size_t i = 0; i < count; i += inc) {
723 path.moveTo(pts[i]);
724 path.lineTo(pts[i+1]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000725 if (fDevice) {
726 fDevice->drawPath(*this, path, p, NULL, true);
727 } else {
728 this->drawPath(path, p, NULL, true);
729 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000730 path.rewind();
731 }
732 break;
733 }
734 }
735 }
736}
737
reed@google.com761fb622011-04-04 18:58:05 +0000738static bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
739 SkPoint* strokeSize) {
740 if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
741 paint.getStrokeMiter() < SK_ScalarSqrt2) {
742 return false;
743 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000744
reed@google.com761fb622011-04-04 18:58:05 +0000745 SkASSERT(matrix.rectStaysRect());
746 SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
747 matrix.mapVectors(strokeSize, &pt, 1);
reed@google.com61153382011-04-05 13:05:18 +0000748 strokeSize->fX = SkScalarAbs(strokeSize->fX);
749 strokeSize->fY = SkScalarAbs(strokeSize->fY);
reed@google.com761fb622011-04-04 18:58:05 +0000750 return true;
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000751}
752
reed@google.com62ab7ad2011-04-05 14:08:25 +0000753SkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
754 const SkMatrix& matrix,
755 SkPoint* strokeSize) {
756 RectType rtype;
757 const SkScalar width = paint.getStrokeWidth();
758 const bool zeroWidth = (0 == width);
759 SkPaint::Style style = paint.getStyle();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000760
reed@google.com62ab7ad2011-04-05 14:08:25 +0000761 if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
762 style = SkPaint::kFill_Style;
763 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000764
reed@google.com62ab7ad2011-04-05 14:08:25 +0000765 if (paint.getPathEffect() || paint.getMaskFilter() ||
766 paint.getRasterizer() || !matrix.rectStaysRect() ||
767 SkPaint::kStrokeAndFill_Style == style) {
768 rtype = kPath_RectType;
769 } else if (SkPaint::kFill_Style == style) {
770 rtype = kFill_RectType;
771 } else if (zeroWidth) {
772 rtype = kHair_RectType;
773 } else if (easy_rect_join(paint, matrix, strokeSize)) {
774 rtype = kStroke_RectType;
775 } else {
776 rtype = kPath_RectType;
777 }
778 return rtype;
779}
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000780
reed@google.com73244152012-05-11 14:35:23 +0000781static const SkPoint* rect_points(const SkRect& r) {
reed@google.comfc2f0d02013-03-29 14:23:56 +0000782 return SkTCast<const SkPoint*>(&r);
reed@google.com73244152012-05-11 14:35:23 +0000783}
784
785static SkPoint* rect_points(SkRect& r) {
reed@google.comfc2f0d02013-03-29 14:23:56 +0000786 return SkTCast<SkPoint*>(&r);
reed@google.com40c2ba22011-07-25 19:41:22 +0000787}
788
reed@android.com8a1c16f2008-12-17 15:59:43 +0000789void SkDraw::drawRect(const SkRect& rect, const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000790 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000791
792 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000793 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000794 return;
795 }
796
reed@google.com761fb622011-04-04 18:58:05 +0000797 SkPoint strokeSize;
reed@google.com62ab7ad2011-04-05 14:08:25 +0000798 RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000799
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000800 if (kPath_RectType == rtype) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801 SkPath tmp;
802 tmp.addRect(rect);
803 tmp.setFillType(SkPath::kWinding_FillType);
reed@android.com187d5592009-07-08 14:03:56 +0000804 this->drawPath(tmp, paint, NULL, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805 return;
806 }
807
808 const SkMatrix& matrix = *fMatrix;
809 SkRect devRect;
810
811 // transform rect into devRect
reed@google.com73244152012-05-11 14:35:23 +0000812 matrix.mapPoints(rect_points(devRect), rect_points(rect), 2);
813 devRect.sort();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000814
reed@android.com8a1c16f2008-12-17 15:59:43 +0000815 // look for the quick exit, before we build a blitter
reed@google.com1c028bd2013-08-28 15:23:19 +0000816 SkIRect ir;
817 devRect.roundOut(&ir);
818 if (paint.getStyle() != SkPaint::kFill_Style) {
819 // extra space for hairlines
georgeb3eba472014-09-09 11:33:57 -0700820 if (paint.getStrokeWidth() == 0) {
821 ir.outset(1, 1);
822 } else {
823 SkScalar radius = SkScalarHalf(paint.getStrokeWidth());
824 ir.outset(radius, radius);
825 }
reed@google.com1c028bd2013-08-28 15:23:19 +0000826 }
827 if (fRC->quickReject(ir)) {
828 return;
rmistry@google.come09d6f42013-08-27 18:53:41 +0000829 }
830
reed@google.com1c028bd2013-08-28 15:23:19 +0000831 SkDeviceLooper looper(*fBitmap, *fRC, ir, paint.isAntiAlias());
832 while (looper.next()) {
833 SkRect localDevRect;
834 looper.mapRect(&localDevRect, devRect);
835 SkMatrix localMatrix;
836 looper.mapMatrix(&localMatrix, matrix);
rmistry@google.come09d6f42013-08-27 18:53:41 +0000837
reed@google.com1c028bd2013-08-28 15:23:19 +0000838 SkAutoBlitterChoose blitterStorage(looper.getBitmap(), localMatrix,
839 paint);
840 const SkRasterClip& clip = looper.getRC();
841 SkBlitter* blitter = blitterStorage.get();
842
843 // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
844 // case we are also hairline (if we've gotten to here), which devolves to
845 // effectively just kFill
846 switch (rtype) {
847 case kFill_RectType:
848 if (paint.isAntiAlias()) {
849 SkScan::AntiFillRect(localDevRect, clip, blitter);
850 } else {
851 SkScan::FillRect(localDevRect, clip, blitter);
852 }
853 break;
854 case kStroke_RectType:
855 if (paint.isAntiAlias()) {
856 SkScan::AntiFrameRect(localDevRect, strokeSize, clip, blitter);
857 } else {
858 SkScan::FrameRect(localDevRect, strokeSize, clip, blitter);
859 }
860 break;
861 case kHair_RectType:
862 if (paint.isAntiAlias()) {
863 SkScan::AntiHairRect(localDevRect, clip, blitter);
864 } else {
865 SkScan::HairRect(localDevRect, clip, blitter);
866 }
867 break;
868 default:
869 SkDEBUGFAIL("bad rtype");
870 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000871 }
872}
873
874void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
875 if (srcM.fBounds.isEmpty()) {
876 return;
877 }
878
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000879 const SkMask* mask = &srcM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000880
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000881 SkMask dstM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000882 if (paint.getMaskFilter() &&
883 paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) {
884 mask = &dstM;
bungeman@google.combf2ac7e2011-09-26 19:51:33 +0000885 } else {
886 dstM.fImage = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000887 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000888 SkAutoMaskFreeImage ami(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000889
reed@google.com045e62d2011-10-24 12:19:46 +0000890 SkAutoBlitterChoose blitterChooser(*fBitmap, *fMatrix, paint);
891 SkBlitter* blitter = blitterChooser.get();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000892
reed@google.com045e62d2011-10-24 12:19:46 +0000893 SkAAClipBlitterWrapper wrapper;
894 const SkRegion* clipRgn;
895
896 if (fRC->isBW()) {
897 clipRgn = &fRC->bwRgn();
898 } else {
899 wrapper.init(*fRC, blitter);
900 clipRgn = &wrapper.getRgn();
901 blitter = wrapper.getBlitter();
902 }
903 blitter->blitMaskRegion(*mask, *clipRgn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000904}
905
reed@android.comebdeeb82009-09-03 21:45:49 +0000906static SkScalar fast_len(const SkVector& vec) {
907 SkScalar x = SkScalarAbs(vec.fX);
908 SkScalar y = SkScalarAbs(vec.fY);
909 if (x < y) {
910 SkTSwap(x, y);
911 }
912 return x + SkScalarHalf(y);
913}
914
reed@google.comecadf992011-09-19 19:18:18 +0000915static bool xfermodeSupportsCoverageAsAlpha(SkXfermode* xfer) {
916 SkXfermode::Coeff dc;
917 if (!SkXfermode::AsCoeff(xfer, NULL, &dc)) {
918 return false;
919 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000920
reed@google.comecadf992011-09-19 19:18:18 +0000921 switch (dc) {
922 case SkXfermode::kOne_Coeff:
923 case SkXfermode::kISA_Coeff:
924 case SkXfermode::kISC_Coeff:
925 return true;
926 default:
927 return false;
928 }
929}
930
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000931bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix& matrix,
932 SkScalar* coverage) {
933 SkASSERT(strokeWidth > 0);
934 // We need to try to fake a thick-stroke with a modulated hairline.
reed@google.comecadf992011-09-19 19:18:18 +0000935
tomhudson@google.com8d430182011-06-06 19:11:19 +0000936 if (matrix.hasPerspective()) {
reed@android.comebdeeb82009-09-03 21:45:49 +0000937 return false;
938 }
reed@google.comecadf992011-09-19 19:18:18 +0000939
reed@android.comebdeeb82009-09-03 21:45:49 +0000940 SkVector src[2], dst[2];
reed@google.comecadf992011-09-19 19:18:18 +0000941 src[0].set(strokeWidth, 0);
942 src[1].set(0, strokeWidth);
reed@android.comebdeeb82009-09-03 21:45:49 +0000943 matrix.mapVectors(dst, src, 2);
944 SkScalar len0 = fast_len(dst[0]);
945 SkScalar len1 = fast_len(dst[1]);
agl@chromium.org652807b2010-04-27 15:47:34 +0000946 if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
bsalomon49f085d2014-09-05 13:34:00 -0700947 if (coverage) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000948 *coverage = SkScalarAve(len0, len1);
949 }
reed@android.comebdeeb82009-09-03 21:45:49 +0000950 return true;
951 }
952 return false;
953}
954
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000955void SkDraw::drawRRect(const SkRRect& rrect, const SkPaint& paint) const {
956 SkDEBUGCODE(this->validate());
957
958 if (fRC->isEmpty()) {
959 return;
960 }
961
962 {
963 // TODO: Investigate optimizing these options. They are in the same
964 // order as SkDraw::drawPath, which handles each case. It may be
965 // that there is no way to optimize for these using the SkRRect path.
966 SkScalar coverage;
967 if (SkDrawTreatAsHairline(paint, *fMatrix, &coverage)) {
968 goto DRAW_PATH;
969 }
970
971 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
972 goto DRAW_PATH;
973 }
974
975 if (paint.getRasterizer()) {
976 goto DRAW_PATH;
977 }
978 }
979
980 if (paint.getMaskFilter()) {
981 // Transform the rrect into device space.
982 SkRRect devRRect;
983 if (rrect.transform(*fMatrix, &devRRect)) {
984 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
reed868074b2014-06-03 10:53:59 -0700985 if (paint.getMaskFilter()->filterRRect(devRRect, *fMatrix, *fRC, blitter.get(),
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000986 SkPaint::kFill_Style)) {
987 return; // filterRRect() called the blitter, so we're done
988 }
989 }
990 }
991
992DRAW_PATH:
993 // Now fall back to the default case of using a path.
994 SkPath path;
995 path.addRRect(rrect);
996 this->drawPath(path, paint, NULL, true);
997}
998
reed@google.com32e5d972011-07-27 18:25:57 +0000999void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
reed@google.com126f7f52013-11-07 16:06:53 +00001000 const SkMatrix* prePathMatrix, bool pathIsMutable,
krajcevski53f09592014-08-06 11:12:14 -07001001 bool drawCoverage, SkBlitter* customBlitter) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001002 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001003
1004 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001005 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001006 return;
1007 }
1008
1009 SkPath* pathPtr = (SkPath*)&origSrcPath;
1010 bool doFill = true;
1011 SkPath tmpPath;
1012 SkMatrix tmpMatrix;
1013 const SkMatrix* matrix = fMatrix;
jvanverthb3eb6872014-10-24 07:12:51 -07001014 tmpPath.setIsVolatile(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001015
1016 if (prePathMatrix) {
reed@google.com32e5d972011-07-27 18:25:57 +00001017 if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
1018 origPaint.getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001019 SkPath* result = pathPtr;
reed@google.coma76de3d2011-01-13 18:30:42 +00001020
reed@android.com8a1c16f2008-12-17 15:59:43 +00001021 if (!pathIsMutable) {
1022 result = &tmpPath;
1023 pathIsMutable = true;
1024 }
1025 pathPtr->transform(*prePathMatrix, result);
1026 pathPtr = result;
1027 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001028 tmpMatrix.setConcat(*matrix, *prePathMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001029 matrix = &tmpMatrix;
1030 }
1031 }
1032 // at this point we're done with prePathMatrix
1033 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.coma76de3d2011-01-13 18:30:42 +00001034
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001035 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
reed@google.coma76de3d2011-01-13 18:30:42 +00001036
reed@google.comecadf992011-09-19 19:18:18 +00001037 {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001038 SkScalar coverage;
1039 if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) {
1040 if (SK_Scalar1 == coverage) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001041 paint.writable()->setStrokeWidth(0);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001042 } else if (xfermodeSupportsCoverageAsAlpha(origPaint.getXfermode())) {
1043 U8CPU newAlpha;
1044#if 0
1045 newAlpha = SkToU8(SkScalarRoundToInt(coverage *
1046 origPaint.getAlpha()));
1047#else
1048 // this is the old technique, which we preserve for now so
1049 // we don't change previous results (testing)
1050 // the new way seems fine, its just (a tiny bit) different
1051 int scale = (int)SkScalarMul(coverage, 256);
1052 newAlpha = origPaint.getAlpha() * scale >> 8;
1053#endif
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001054 SkPaint* writablePaint = paint.writable();
1055 writablePaint->setStrokeWidth(0);
1056 writablePaint->setAlpha(newAlpha);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001057 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001058 }
1059 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001060
reed@google.com32e5d972011-07-27 18:25:57 +00001061 if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
reed@google.com4bbdeac2013-01-24 21:03:11 +00001062 SkRect cullRect;
1063 const SkRect* cullRectPtr = NULL;
1064 if (this->computeConservativeLocalClipBounds(&cullRect)) {
1065 cullRectPtr = &cullRect;
1066 }
1067 doFill = paint->getFillPath(*pathPtr, &tmpPath, cullRectPtr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001068 pathPtr = &tmpPath;
1069 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001070
reed@google.com32e5d972011-07-27 18:25:57 +00001071 if (paint->getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001072 SkMask mask;
reed@google.com32e5d972011-07-27 18:25:57 +00001073 if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
reed@google.com045e62d2011-10-24 12:19:46 +00001074 &fRC->getBounds(), paint->getMaskFilter(), &mask,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001075 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
reed@google.com32e5d972011-07-27 18:25:57 +00001076 this->drawDevMask(mask, *paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001077 SkMask::FreeImage(mask.fImage);
1078 }
1079 return;
1080 }
1081
1082 // avoid possibly allocating a new path in transform if we can
1083 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1084
1085 // transform the path into device space
1086 pathPtr->transform(*matrix, devPathPtr);
1087
krajcevski53f09592014-08-06 11:12:14 -07001088 SkBlitter* blitter = NULL;
1089 SkAutoBlitterChoose blitterStorage;
1090 if (NULL == customBlitter) {
1091 blitterStorage.choose(*fBitmap, *fMatrix, *paint, drawCoverage);
1092 blitter = blitterStorage.get();
1093 } else {
1094 blitter = customBlitter;
1095 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001096
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001097 if (paint->getMaskFilter()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001098 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001099 SkPaint::kStroke_Style;
krajcevski53f09592014-08-06 11:12:14 -07001100 if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC, blitter, style)) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001101 return; // filterPath() called the blitter, so we're done
1102 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001103 }
1104
reed@google.com045e62d2011-10-24 12:19:46 +00001105 void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001106 if (doFill) {
reed@google.com32e5d972011-07-27 18:25:57 +00001107 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001108 proc = SkScan::AntiFillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001109 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001110 proc = SkScan::FillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001111 }
1112 } else { // hairline
reed@google.com32e5d972011-07-27 18:25:57 +00001113 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001114 proc = SkScan::AntiHairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001115 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001116 proc = SkScan::HairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001117 }
1118 }
krajcevski53f09592014-08-06 11:12:14 -07001119 proc(*devPathPtr, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001120}
1121
reed@android.com0baf1932009-06-24 12:41:42 +00001122/** For the purposes of drawing bitmaps, if a matrix is "almost" translate
1123 go ahead and treat it as if it were, so that subsequent code can go fast.
1124 */
1125static bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
reed@google.com070dcd82013-01-07 20:27:52 +00001126 unsigned bits = 0; // TODO: find a way to allow the caller to tell us to
1127 // respect filtering.
1128 return SkTreatAsSprite(matrix, bitmap.width(), bitmap.height(), bits);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001129}
1130
1131void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
1132 const SkPaint& paint) const {
reed@google.com900ecf22014-02-20 20:55:37 +00001133 SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001134
reed@google.coma76de3d2011-01-13 18:30:42 +00001135 if (just_translate(*fMatrix, bitmap)) {
reed@google.come1ca7052013-12-17 19:22:07 +00001136 int ix = SkScalarRoundToInt(fMatrix->getTranslateX());
1137 int iy = SkScalarRoundToInt(fMatrix->getTranslateY());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001138
reed@google.coma641f3f2012-12-13 22:16:30 +00001139 SkAutoLockPixels alp(bitmap);
1140 if (!bitmap.readyToDraw()) {
1141 return;
1142 }
1143
reed@android.com8a1c16f2008-12-17 15:59:43 +00001144 SkMask mask;
1145 mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1146 mask.fFormat = SkMask::kA8_Format;
scroggo@google.come5f48242013-02-25 21:47:41 +00001147 mask.fRowBytes = SkToU32(bitmap.rowBytes());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001148 mask.fImage = bitmap.getAddr8(0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001149
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150 this->drawDevMask(mask, paint);
1151 } else { // need to xform the bitmap first
1152 SkRect r;
1153 SkMask mask;
reed@google.coma76de3d2011-01-13 18:30:42 +00001154
reed@android.com8a1c16f2008-12-17 15:59:43 +00001155 r.set(0, 0,
1156 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
1157 fMatrix->mapRect(&r);
1158 r.round(&mask.fBounds);
reed@google.coma76de3d2011-01-13 18:30:42 +00001159
reed@android.com8a1c16f2008-12-17 15:59:43 +00001160 // set the mask's bounds to the transformed bitmap-bounds,
1161 // clipped to the actual device
1162 {
1163 SkIRect devBounds;
1164 devBounds.set(0, 0, fBitmap->width(), fBitmap->height());
1165 // need intersect(l, t, r, b) on irect
1166 if (!mask.fBounds.intersect(devBounds)) {
1167 return;
1168 }
1169 }
reed@android.com543ed932009-04-24 12:43:40 +00001170
reed@android.com8a1c16f2008-12-17 15:59:43 +00001171 mask.fFormat = SkMask::kA8_Format;
1172 mask.fRowBytes = SkAlign4(mask.fBounds.width());
reed@android.com543ed932009-04-24 12:43:40 +00001173 size_t size = mask.computeImageSize();
1174 if (0 == size) {
1175 // the mask is too big to allocated, draw nothing
1176 return;
1177 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001178
1179 // allocate (and clear) our temp buffer to hold the transformed bitmap
reed@android.com8a1c16f2008-12-17 15:59:43 +00001180 SkAutoMalloc storage(size);
1181 mask.fImage = (uint8_t*)storage.get();
1182 memset(mask.fImage, 0, size);
reed@google.coma76de3d2011-01-13 18:30:42 +00001183
reed@android.com8a1c16f2008-12-17 15:59:43 +00001184 // now draw our bitmap(src) into mask(dst), transformed by the matrix
1185 {
1186 SkBitmap device;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001187 device.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
1188 mask.fImage, mask.fRowBytes);
reed@google.coma76de3d2011-01-13 18:30:42 +00001189
reed@android.com8a1c16f2008-12-17 15:59:43 +00001190 SkCanvas c(device);
1191 // need the unclipped top/left for the translate
1192 c.translate(-SkIntToScalar(mask.fBounds.fLeft),
1193 -SkIntToScalar(mask.fBounds.fTop));
1194 c.concat(*fMatrix);
reed@android.com3469c762009-02-24 19:03:20 +00001195
1196 // We can't call drawBitmap, or we'll infinitely recurse. Instead
reed@android.comfb12c3e2009-03-05 20:43:42 +00001197 // we manually build a shader and draw that into our new mask
reed@android.com3469c762009-02-24 19:03:20 +00001198 SkPaint tmpPaint;
1199 tmpPaint.setFlags(paint.getFlags());
reed@google.com40c2ba22011-07-25 19:41:22 +00001200 SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
reed@android.com3469c762009-02-24 19:03:20 +00001201 SkRect rr;
1202 rr.set(0, 0, SkIntToScalar(bitmap.width()),
1203 SkIntToScalar(bitmap.height()));
reed@google.com40c2ba22011-07-25 19:41:22 +00001204 c.drawRect(rr, install.paintWithShader());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001205 }
1206 this->drawDevMask(mask, paint);
1207 }
1208}
1209
reed@google.com045e62d2011-10-24 12:19:46 +00001210static bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001211 const SkRect& srcR) {
1212 SkRect dstR;
1213 SkIRect devIR;
reed@google.coma76de3d2011-01-13 18:30:42 +00001214
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 m.mapRect(&dstR, srcR);
reed@google.coma76de3d2011-01-13 18:30:42 +00001216 dstR.roundOut(&devIR);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001217 return c.quickReject(devIR);
1218}
1219
reed@google.com045e62d2011-10-24 12:19:46 +00001220static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001221 int width, int height) {
1222 SkRect r;
1223 r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
1224 return clipped_out(matrix, clip, r);
1225}
1226
reed@google.com045e62d2011-10-24 12:19:46 +00001227static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y,
1228 const SkBitmap& bitmap) {
1229 return clip.isBW() ||
1230 clip.quickContains(x, y, x + bitmap.width(), y + bitmap.height());
1231}
1232
reed@android.com8a1c16f2008-12-17 15:59:43 +00001233void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
reed@google.com40c2ba22011-07-25 19:41:22 +00001234 const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001235 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001236
1237 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001238 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001240 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001241 return;
1242 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001243
reed@google.com40c2ba22011-07-25 19:41:22 +00001244 SkPaint paint(origPaint);
1245 paint.setStyle(SkPaint::kFill_Style);
reed@google.coma76de3d2011-01-13 18:30:42 +00001246
reed@android.com8a1c16f2008-12-17 15:59:43 +00001247 SkMatrix matrix;
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001248 matrix.setConcat(*fMatrix, prematrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001249
reed@google.com045e62d2011-10-24 12:19:46 +00001250 if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251 return;
1252 }
1253
reed868074b2014-06-03 10:53:59 -07001254 if (bitmap.colorType() != kAlpha_8_SkColorType && just_translate(matrix, bitmap)) {
reed@google.comf7ef56d2012-12-14 13:46:53 +00001255 //
1256 // It is safe to call lock pixels now, since we know the matrix is
1257 // (more or less) identity.
1258 //
1259 SkAutoLockPixels alp(bitmap);
1260 if (!bitmap.readyToDraw()) {
1261 return;
1262 }
reed@google.come1ca7052013-12-17 19:22:07 +00001263 int ix = SkScalarRoundToInt(matrix.getTranslateX());
1264 int iy = SkScalarRoundToInt(matrix.getTranslateY());
reed@google.com045e62d2011-10-24 12:19:46 +00001265 if (clipHandlesSprite(*fRC, ix, iy, bitmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001266 SkTBlitterAllocator allocator;
1267 // blitter will be owned by the allocator.
1268 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1269 ix, iy, &allocator);
reed@google.com045e62d2011-10-24 12:19:46 +00001270 if (blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +00001271 SkIRect ir;
1272 ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273
reed@google.com045e62d2011-10-24 12:19:46 +00001274 SkScan::FillIRect(ir, *fRC, blitter);
1275 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001276 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001277 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001278 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001279
reed@android.com8a1c16f2008-12-17 15:59:43 +00001280 // now make a temp draw on the stack, and use it
1281 //
1282 SkDraw draw(*this);
1283 draw.fMatrix = &matrix;
reed@google.coma76de3d2011-01-13 18:30:42 +00001284
reed@google.com900ecf22014-02-20 20:55:37 +00001285 if (bitmap.colorType() == kAlpha_8_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001286 draw.drawBitmapAsMask(bitmap, paint);
1287 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +00001288 SkAutoBitmapShaderInstall install(bitmap, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001289
1290 SkRect r;
1291 r.set(0, 0, SkIntToScalar(bitmap.width()),
1292 SkIntToScalar(bitmap.height()));
reed@google.coma76de3d2011-01-13 18:30:42 +00001293 // is this ok if paint has a rasterizer?
reed@google.com40c2ba22011-07-25 19:41:22 +00001294 draw.drawRect(r, install.paintWithShader());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001295 }
1296}
1297
1298void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
reed@google.com40c2ba22011-07-25 19:41:22 +00001299 const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001300 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +00001301
reed@android.com8a1c16f2008-12-17 15:59:43 +00001302 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001303 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001304 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001305 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001306 return;
1307 }
1308
1309 SkIRect bounds;
1310 bounds.set(x, y, x + bitmap.width(), y + bitmap.height());
1311
reed@google.com045e62d2011-10-24 12:19:46 +00001312 if (fRC->quickReject(bounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001313 return; // nothing to draw
1314 }
1315
reed@google.com40c2ba22011-07-25 19:41:22 +00001316 SkPaint paint(origPaint);
1317 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001318
reed@google.com045e62d2011-10-24 12:19:46 +00001319 if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001320 SkTBlitterAllocator allocator;
1321 // blitter will be owned by the allocator.
1322 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1323 x, y, &allocator);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001324
1325 if (blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +00001326 SkScan::FillIRect(bounds, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001327 return;
1328 }
1329 }
1330
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331 SkMatrix matrix;
1332 SkRect r;
1333
1334 // get a scalar version of our rect
1335 r.set(bounds);
1336
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001337 // create shader with offset
reed@android.com8a1c16f2008-12-17 15:59:43 +00001338 matrix.setTranslate(r.fLeft, r.fTop);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001339 SkAutoBitmapShaderInstall install(bitmap, paint, &matrix);
1340 const SkPaint& shaderPaint = install.paintWithShader();
reed@google.coma76de3d2011-01-13 18:30:42 +00001341
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342 SkDraw draw(*this);
1343 matrix.reset();
1344 draw.fMatrix = &matrix;
1345 // call ourself with a rect
reed@google.coma76de3d2011-01-13 18:30:42 +00001346 // is this OK if paint has a rasterizer?
reed@google.com40c2ba22011-07-25 19:41:22 +00001347 draw.drawRect(r, shaderPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001348}
1349
1350///////////////////////////////////////////////////////////////////////////////
1351
1352#include "SkScalerContext.h"
1353#include "SkGlyphCache.h"
reed@google.come6913762012-08-07 15:19:47 +00001354#include "SkTextToPathIter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001355#include "SkUtils.h"
1356
1357static void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
1358 const char text[], size_t byteLength, SkVector* stopVector) {
1359 SkFixed x = 0, y = 0;
1360 const char* stop = text + byteLength;
1361
1362 SkAutoKern autokern;
reed@google.coma76de3d2011-01-13 18:30:42 +00001363
reed@android.com8a1c16f2008-12-17 15:59:43 +00001364 while (text < stop) {
1365 // don't need x, y here, since all subpixel variants will have the
1366 // same advance
1367 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1368
1369 x += autokern.adjust(glyph) + glyph.fAdvanceX;
1370 y += glyph.fAdvanceY;
1371 }
1372 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
1373
1374 SkASSERT(text == stop);
1375}
1376
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001377bool SkDraw::ShouldDrawTextAsPaths(const SkPaint& paint, const SkMatrix& ctm) {
1378 // hairline glyphs are fast enough so we don't need to cache them
1379 if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) {
1380 return true;
1381 }
1382
1383 // we don't cache perspective
1384 if (ctm.hasPerspective()) {
1385 return true;
1386 }
1387
1388 SkMatrix textM;
1389 return SkPaint::TooBigToUseCache(ctm, *paint.setTextMatrix(&textM));
1390}
1391
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392void SkDraw::drawText_asPaths(const char text[], size_t byteLength,
1393 SkScalar x, SkScalar y,
1394 const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001395 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001396
djsollen@google.com166e6532012-03-20 14:24:38 +00001397 SkTextToPathIter iter(text, byteLength, paint, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001398
1399 SkMatrix matrix;
1400 matrix.setScale(iter.getPathScale(), iter.getPathScale());
1401 matrix.postTranslate(x, y);
1402
1403 const SkPath* iterPath;
1404 SkScalar xpos, prevXPos = 0;
1405
reed@google.com7b4531f2012-08-07 15:53:00 +00001406 while (iter.next(&iterPath, &xpos)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001407 matrix.postTranslate(xpos - prevXPos, 0);
reed@google.com7b4531f2012-08-07 15:53:00 +00001408 if (iterPath) {
1409 const SkPaint& pnt = iter.getPaint();
1410 if (fDevice) {
1411 fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
1412 } else {
1413 this->drawPath(*iterPath, pnt, &matrix, false);
1414 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001415 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001416 prevXPos = xpos;
1417 }
1418}
1419
reed@android.com8a1c16f2008-12-17 15:59:43 +00001420// disable warning : local variable used without having been initialized
reed@google.coma76de3d2011-01-13 18:30:42 +00001421#if defined _WIN32 && _MSC_VER >= 1300
reed@android.com8a1c16f2008-12-17 15:59:43 +00001422#pragma warning ( push )
1423#pragma warning ( disable : 4701 )
1424#endif
1425
1426//////////////////////////////////////////////////////////////////////////////
1427
reed868074b2014-06-03 10:53:59 -07001428static void D1G_RectClip(const SkDraw1Glyph& state, SkFixed fx, SkFixed fy, const SkGlyph& glyph) {
mike@reedtribe.org9fb00412014-01-06 03:02:37 +00001429 int left = SkFixedFloorToInt(fx);
1430 int top = SkFixedFloorToInt(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001431 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001432 SkASSERT((NULL == state.fClip && state.fAAClip) ||
1433 (state.fClip && NULL == state.fAAClip && state.fClip->isRect()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001434
1435 left += glyph.fLeft;
1436 top += glyph.fTop;
1437
1438 int right = left + glyph.fWidth;
1439 int bottom = top + glyph.fHeight;
1440
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001441 SkMask mask;
1442 SkIRect storage;
1443 SkIRect* bounds = &mask.fBounds;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001444
tomhudson@google.com83a44462011-10-27 15:27:51 +00001445 mask.fBounds.set(left, top, right, bottom);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001446
tomhudson@google.com83a44462011-10-27 15:27:51 +00001447 // this extra test is worth it, assuming that most of the time it succeeds
1448 // since we can avoid writing to storage
1449 if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
1450 if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
1451 return;
1452 bounds = &storage;
1453 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001454
tomhudson@google.com83a44462011-10-27 15:27:51 +00001455 uint8_t* aa = (uint8_t*)glyph.fImage;
1456 if (NULL == aa) {
1457 aa = (uint8_t*)state.fCache->findImage(glyph);
1458 if (NULL == aa) {
1459 return; // can't rasterize glyph
reed@android.com8a1c16f2008-12-17 15:59:43 +00001460 }
tomhudson@google.com83a44462011-10-27 15:27:51 +00001461 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001462
tomhudson@google.com83a44462011-10-27 15:27:51 +00001463 mask.fRowBytes = glyph.rowBytes();
1464 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1465 mask.fImage = aa;
reed@google.com5bdfb332013-05-02 18:55:44 +00001466 state.blitMask(mask, *bounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001467}
1468
reed868074b2014-06-03 10:53:59 -07001469static void D1G_RgnClip(const SkDraw1Glyph& state, SkFixed fx, SkFixed fy, const SkGlyph& glyph) {
mike@reedtribe.org9fb00412014-01-06 03:02:37 +00001470 int left = SkFixedFloorToInt(fx);
1471 int top = SkFixedFloorToInt(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001472 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001473 SkASSERT(!state.fClip->isRect());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001474
1475 SkMask mask;
1476
1477 left += glyph.fLeft;
1478 top += glyph.fTop;
1479
1480 mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001481 SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001482
tomhudson@google.com83a44462011-10-27 15:27:51 +00001483 if (!clipper.done()) {
1484 const SkIRect& cr = clipper.rect();
1485 const uint8_t* aa = (const uint8_t*)glyph.fImage;
1486 if (NULL == aa) {
1487 aa = (uint8_t*)state.fCache->findImage(glyph);
1488 if (NULL == aa) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001489 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001490 }
tomhudson@google.com83a44462011-10-27 15:27:51 +00001491 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001492
tomhudson@google.com83a44462011-10-27 15:27:51 +00001493 mask.fRowBytes = glyph.rowBytes();
1494 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1495 mask.fImage = (uint8_t*)aa;
1496 do {
reed@google.com5bdfb332013-05-02 18:55:44 +00001497 state.blitMask(mask, cr);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001498 clipper.next();
1499 } while (!clipper.done());
1500 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001501}
1502
reed@google.comfd4236e2011-07-25 21:16:22 +00001503static bool hasCustomD1GProc(const SkDraw& draw) {
1504 return draw.fProcs && draw.fProcs->fD1GProc;
1505}
1506
1507static bool needsRasterTextBlit(const SkDraw& draw) {
1508 return !hasCustomD1GProc(draw);
1509}
1510
reed868074b2014-06-03 10:53:59 -07001511SkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter, SkGlyphCache* cache,
1512 const SkPaint& pnt) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001513 fDraw = draw;
bungeman@google.com2211b622012-01-13 15:02:58 +00001514 fBlitter = blitter;
1515 fCache = cache;
reed@google.com5bdfb332013-05-02 18:55:44 +00001516 fPaint = &pnt;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001517
bungeman@google.com94471032013-02-25 15:55:13 +00001518 if (cache->isSubpixel()) {
1519 fHalfSampleX = fHalfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits);
1520 } else {
1521 fHalfSampleX = fHalfSampleY = SK_FixedHalf;
1522 }
1523
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001524 if (hasCustomD1GProc(*draw)) {
reed@google.com045e62d2011-10-24 12:19:46 +00001525 // todo: fix this assumption about clips w/ custom
1526 fClip = draw->fClip;
1527 fClipBounds = fClip->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001528 return draw->fProcs->fD1GProc;
1529 }
1530
reed@google.com045e62d2011-10-24 12:19:46 +00001531 if (draw->fRC->isBW()) {
1532 fAAClip = NULL;
1533 fClip = &draw->fRC->bwRgn();
1534 fClipBounds = fClip->getBounds();
reed868074b2014-06-03 10:53:59 -07001535 if (fClip->isRect()) {
1536 return D1G_RectClip;
reed@google.com045e62d2011-10-24 12:19:46 +00001537 } else {
reed868074b2014-06-03 10:53:59 -07001538 return D1G_RgnClip;
reed@google.com045e62d2011-10-24 12:19:46 +00001539 }
1540 } else { // aaclip
1541 fAAClip = &draw->fRC->aaRgn();
1542 fClip = NULL;
1543 fClipBounds = fAAClip->getBounds();
reed868074b2014-06-03 10:53:59 -07001544 return D1G_RectClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001545 }
1546}
1547
reed@google.com5bdfb332013-05-02 18:55:44 +00001548void SkDraw1Glyph::blitMaskAsSprite(const SkMask& mask) const {
1549 SkASSERT(SkMask::kARGB32_Format == mask.fFormat);
1550
1551 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001552 bm.installPixels(SkImageInfo::MakeN32Premul(mask.fBounds.width(), mask.fBounds.height()),
1553 (SkPMColor*)mask.fImage, mask.fRowBytes);
reed@google.com5bdfb332013-05-02 18:55:44 +00001554
1555 fDraw->drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), *fPaint);
1556}
1557
reed@android.com8a1c16f2008-12-17 15:59:43 +00001558///////////////////////////////////////////////////////////////////////////////
1559
1560void SkDraw::drawText(const char text[], size_t byteLength,
1561 SkScalar x, SkScalar y, const SkPaint& paint) const {
1562 SkASSERT(byteLength == 0 || text != NULL);
1563
reed@android.comf2b98d62010-12-20 18:26:13 +00001564 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001565
1566 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001567 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001568 return;
1569 }
1570
bsalomon@google.com36d6eda2012-10-10 16:12:14 +00001571 // SkScalarRec doesn't currently have a way of representing hairline stroke and
1572 // will fill if its frame-width is 0.
reed@google.comed43dff2013-06-04 16:56:27 +00001573 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001574 this->drawText_asPaths(text, byteLength, x, y, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001575 return;
1576 }
1577
1578 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1579
reede010f1c2014-09-17 10:49:38 -07001580 SkAutoGlyphCache autoCache(paint, &fDevice->getLeakyProperties(), fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001581 SkGlyphCache* cache = autoCache.getCache();
reed@google.coma76de3d2011-01-13 18:30:42 +00001582
reed@android.com8a1c16f2008-12-17 15:59:43 +00001583 // transform our starting point
1584 {
1585 SkPoint loc;
bungeman@google.com94471032013-02-25 15:55:13 +00001586 fMatrix->mapXY(x, y, &loc);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001587 x = loc.fX;
1588 y = loc.fY;
1589 }
1590
1591 // need to measure first
1592 if (paint.getTextAlign() != SkPaint::kLeft_Align) {
1593 SkVector stop;
1594
1595 measure_text(cache, glyphCacheProc, text, byteLength, &stop);
1596
1597 SkScalar stopX = stop.fX;
1598 SkScalar stopY = stop.fY;
1599
1600 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
1601 stopX = SkScalarHalf(stopX);
1602 stopY = SkScalarHalf(stopY);
1603 }
1604 x -= stopX;
1605 y -= stopY;
1606 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001607
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608 const char* stop = text + byteLength;
1609
reed@google.com045e62d2011-10-24 12:19:46 +00001610 SkAAClipBlitter aaBlitter;
1611 SkAutoBlitterChoose blitterChooser;
1612 SkBlitter* blitter = NULL;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001613 if (needsRasterTextBlit(*this)) {
bungeman@google.com94471032013-02-25 15:55:13 +00001614 blitterChooser.choose(*fBitmap, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +00001615 blitter = blitterChooser.get();
1616 if (fRC->isAA()) {
1617 aaBlitter.init(blitter, &fRC->aaRgn());
1618 blitter = &aaBlitter;
1619 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001620 }
1621
reed@android.com8a1c16f2008-12-17 15:59:43 +00001622 SkAutoKern autokern;
bungeman@google.com52c748b2011-08-22 21:30:43 +00001623 SkDraw1Glyph d1g;
reed@google.com5bdfb332013-05-02 18:55:44 +00001624 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001625
bungeman@google.com94471032013-02-25 15:55:13 +00001626 SkFixed fxMask = ~0;
1627 SkFixed fyMask = ~0;
1628 if (cache->isSubpixel()) {
1629 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix);
1630 if (kX_SkAxisAlignment == baseline) {
1631 fyMask = 0;
1632 d1g.fHalfSampleY = SK_FixedHalf;
1633 } else if (kY_SkAxisAlignment == baseline) {
1634 fxMask = 0;
1635 d1g.fHalfSampleX = SK_FixedHalf;
1636 }
1637 }
1638
1639 SkFixed fx = SkScalarToFixed(x) + d1g.fHalfSampleX;
1640 SkFixed fy = SkScalarToFixed(y) + d1g.fHalfSampleY;
1641
reed@android.com8a1c16f2008-12-17 15:59:43 +00001642 while (text < stop) {
bungeman@google.com2211b622012-01-13 15:02:58 +00001643 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001644
1645 fx += autokern.adjust(glyph);
1646
1647 if (glyph.fWidth) {
bungeman@google.com52c748b2011-08-22 21:30:43 +00001648 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001649 }
jvanverth@google.comd830d132013-11-11 20:54:09 +00001650
reed@android.com8a1c16f2008-12-17 15:59:43 +00001651 fx += glyph.fAdvanceX;
1652 fy += glyph.fAdvanceY;
1653 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001654}
1655
reed@android.com8a1c16f2008-12-17 15:59:43 +00001656//////////////////////////////////////////////////////////////////////////////
1657
reed@google.comed43dff2013-06-04 16:56:27 +00001658void SkDraw::drawPosText_asPaths(const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001659 const SkScalar pos[], int scalarsPerPosition,
1660 const SkPoint& offset, const SkPaint& origPaint) const {
reed@google.comed43dff2013-06-04 16:56:27 +00001661 // setup our std paint, in hopes of getting hits in the cache
1662 SkPaint paint(origPaint);
1663 SkScalar matrixScale = paint.setupForAsPaths();
1664
reed@google.com5a649022013-06-05 18:00:30 +00001665 SkMatrix matrix;
1666 matrix.setScale(matrixScale, matrixScale);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001667
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001668 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
1669 paint.setStyle(SkPaint::kFill_Style);
1670 paint.setPathEffect(NULL);
1671
reed@google.comed43dff2013-06-04 16:56:27 +00001672 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1673 SkAutoGlyphCache autoCache(paint, NULL, NULL);
1674 SkGlyphCache* cache = autoCache.getCache();
1675
1676 const char* stop = text + byteLength;
kkinnunencb9a2c82014-06-12 23:06:28 -07001677 SkTextAlignProcScalar alignProc(paint.getTextAlign());
fmalita05c4a432014-09-29 06:29:53 -07001678 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001679
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001680 // Now restore the original settings, so we "draw" with whatever style/stroking.
1681 paint.setStyle(origPaint.getStyle());
1682 paint.setPathEffect(origPaint.getPathEffect());
1683
reed@google.comed43dff2013-06-04 16:56:27 +00001684 while (text < stop) {
1685 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1686 if (glyph.fWidth) {
1687 const SkPath* path = cache->findPath(glyph);
1688 if (path) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001689 SkPoint tmsLoc;
1690 tmsProc(pos, &tmsLoc);
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001691 SkPoint loc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001692 alignProc(tmsLoc, glyph, &loc);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001693
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001694 matrix[SkMatrix::kMTransX] = loc.fX;
1695 matrix[SkMatrix::kMTransY] = loc.fY;
reed@google.com5a649022013-06-05 18:00:30 +00001696 if (fDevice) {
1697 fDevice->drawPath(*this, *path, paint, &matrix, false);
1698 } else {
1699 this->drawPath(*path, paint, &matrix, false);
1700 }
reed@google.comed43dff2013-06-04 16:56:27 +00001701 }
1702 }
1703 pos += scalarsPerPosition;
1704 }
1705}
1706
reed@android.com8a1c16f2008-12-17 15:59:43 +00001707void SkDraw::drawPosText(const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001708 const SkScalar pos[], int scalarsPerPosition,
1709 const SkPoint& offset, const SkPaint& paint) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001710 SkASSERT(byteLength == 0 || text != NULL);
1711 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1712
reed@android.comf2b98d62010-12-20 18:26:13 +00001713 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001714
1715 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001716 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001717 return;
1718 }
1719
reed@google.comed43dff2013-06-04 16:56:27 +00001720 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
fmalita05c4a432014-09-29 06:29:53 -07001721 this->drawPosText_asPaths(text, byteLength, pos, scalarsPerPosition, offset, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001722 return;
1723 }
1724
1725 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
reede010f1c2014-09-17 10:49:38 -07001726 SkAutoGlyphCache autoCache(paint, &fDevice->getLeakyProperties(), fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001727 SkGlyphCache* cache = autoCache.getCache();
reed@google.coma76de3d2011-01-13 18:30:42 +00001728
reed@google.com045e62d2011-10-24 12:19:46 +00001729 SkAAClipBlitterWrapper wrapper;
1730 SkAutoBlitterChoose blitterChooser;
1731 SkBlitter* blitter = NULL;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001732 if (needsRasterTextBlit(*this)) {
bungeman@google.com94471032013-02-25 15:55:13 +00001733 blitterChooser.choose(*fBitmap, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +00001734 blitter = blitterChooser.get();
1735 if (fRC->isAA()) {
1736 wrapper.init(*fRC, blitter);
1737 blitter = wrapper.getBlitter();
1738 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001739 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001740
reed@android.com8a1c16f2008-12-17 15:59:43 +00001741 const char* stop = text + byteLength;
kkinnunencb9a2c82014-06-12 23:06:28 -07001742 SkTextAlignProc alignProc(paint.getTextAlign());
bungeman@google.com2211b622012-01-13 15:02:58 +00001743 SkDraw1Glyph d1g;
reed@google.com5bdfb332013-05-02 18:55:44 +00001744 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
fmalita05c4a432014-09-29 06:29:53 -07001745 SkTextMapStateProc tmsProc(*fMatrix, offset, scalarsPerPosition);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001746
bungeman@google.com2211b622012-01-13 15:02:58 +00001747 if (cache->isSubpixel()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001748 // maybe we should skip the rounding if linearText is set
bungeman@google.com94471032013-02-25 15:55:13 +00001749 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix);
1750
1751 SkFixed fxMask = ~0;
1752 SkFixed fyMask = ~0;
1753 if (kX_SkAxisAlignment == baseline) {
1754 fyMask = 0;
1755#ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX
1756 d1g.fHalfSampleY = SK_FixedHalf;
1757#endif
1758 } else if (kY_SkAxisAlignment == baseline) {
1759 fxMask = 0;
1760#ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX
1761 d1g.fHalfSampleX = SK_FixedHalf;
1762#endif
1763 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001764
1765 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1766 while (text < stop) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001767 SkPoint tmsLoc;
1768 tmsProc(pos, &tmsLoc);
1769 SkFixed fx = SkScalarToFixed(tmsLoc.fX) + d1g.fHalfSampleX;
1770 SkFixed fy = SkScalarToFixed(tmsLoc.fY) + d1g.fHalfSampleY;
reed@google.coma76de3d2011-01-13 18:30:42 +00001771
reed@android.comf2b98d62010-12-20 18:26:13 +00001772 const SkGlyph& glyph = glyphCacheProc(cache, &text,
1773 fx & fxMask, fy & fyMask);
reed@google.coma76de3d2011-01-13 18:30:42 +00001774
reed@android.com8a1c16f2008-12-17 15:59:43 +00001775 if (glyph.fWidth) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001776 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001777 }
1778 pos += scalarsPerPosition;
1779 }
1780 } else {
1781 while (text < stop) {
bungeman@google.com9330cfe2012-01-04 14:17:00 +00001782 const char* currentText = text;
bungeman@google.com94471032013-02-25 15:55:13 +00001783 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001784
bungeman@google.com94471032013-02-25 15:55:13 +00001785 if (metricGlyph.fWidth) {
1786 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
1787 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
kkinnunencb9a2c82014-06-12 23:06:28 -07001788 SkPoint tmsLoc;
1789 tmsProc(pos, &tmsLoc);
bungeman@google.com94471032013-02-25 15:55:13 +00001790 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001791 alignProc(tmsLoc, metricGlyph, &fixedLoc);
reed@google.coma76de3d2011-01-13 18:30:42 +00001792
bungeman@google.com94471032013-02-25 15:55:13 +00001793 SkFixed fx = fixedLoc.fX + d1g.fHalfSampleX;
1794 SkFixed fy = fixedLoc.fY + d1g.fHalfSampleY;
reed@google.coma76de3d2011-01-13 18:30:42 +00001795
reed@android.com8a1c16f2008-12-17 15:59:43 +00001796 // have to call again, now that we've been "aligned"
bungeman@google.com94471032013-02-25 15:55:13 +00001797 const SkGlyph& glyph = glyphCacheProc(cache, &currentText,
1798 fx & fxMask, fy & fyMask);
1799 // the assumption is that the metrics haven't changed
1800 SkASSERT(prevAdvX == glyph.fAdvanceX);
1801 SkASSERT(prevAdvY == glyph.fAdvanceY);
1802 SkASSERT(glyph.fWidth);
reed@google.coma76de3d2011-01-13 18:30:42 +00001803
bungeman@google.com94471032013-02-25 15:55:13 +00001804 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001805 }
1806 pos += scalarsPerPosition;
1807 }
1808 }
1809 } else { // not subpixel
reed@google.comaeb07862012-04-18 18:32:04 +00001810 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1811 while (text < stop) {
1812 // the last 2 parameters are ignored
1813 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001814
reed@google.comaeb07862012-04-18 18:32:04 +00001815 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001816 SkPoint tmsLoc;
1817 tmsProc(pos, &tmsLoc);
reed@google.coma76de3d2011-01-13 18:30:42 +00001818
reed@google.comaeb07862012-04-18 18:32:04 +00001819 proc(d1g,
kkinnunencb9a2c82014-06-12 23:06:28 -07001820 SkScalarToFixed(tmsLoc.fX) + SK_FixedHalf, //d1g.fHalfSampleX,
1821 SkScalarToFixed(tmsLoc.fY) + SK_FixedHalf, //d1g.fHalfSampleY,
reed@google.comaeb07862012-04-18 18:32:04 +00001822 glyph);
1823 }
1824 pos += scalarsPerPosition;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001825 }
reed@google.comaeb07862012-04-18 18:32:04 +00001826 } else {
1827 while (text < stop) {
1828 // the last 2 parameters are ignored
1829 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1830
1831 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001832 SkPoint tmsLoc;
1833 tmsProc(pos, &tmsLoc);
reed@google.comaeb07862012-04-18 18:32:04 +00001834
1835 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001836 alignProc(tmsLoc, glyph, &fixedLoc);
reed@google.comaeb07862012-04-18 18:32:04 +00001837
1838 proc(d1g,
bungeman@google.com94471032013-02-25 15:55:13 +00001839 fixedLoc.fX + SK_FixedHalf, //d1g.fHalfSampleX,
1840 fixedLoc.fY + SK_FixedHalf, //d1g.fHalfSampleY,
reed@google.comaeb07862012-04-18 18:32:04 +00001841 glyph);
1842 }
1843 pos += scalarsPerPosition;
1844 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001845 }
1846 }
1847}
1848
1849#if defined _WIN32 && _MSC_VER >= 1300
1850#pragma warning ( pop )
1851#endif
1852
1853///////////////////////////////////////////////////////////////////////////////
1854
1855#include "SkPathMeasure.h"
1856
1857static void morphpoints(SkPoint dst[], const SkPoint src[], int count,
1858 SkPathMeasure& meas, const SkMatrix& matrix) {
1859 SkMatrix::MapXYProc proc = matrix.getMapXYProc();
1860
1861 for (int i = 0; i < count; i++) {
1862 SkPoint pos;
1863 SkVector tangent;
1864
1865 proc(matrix, src[i].fX, src[i].fY, &pos);
1866 SkScalar sx = pos.fX;
1867 SkScalar sy = pos.fY;
1868
reed@google.com8f17b0d2012-04-12 13:24:30 +00001869 if (!meas.getPosTan(sx, &pos, &tangent)) {
1870 // set to 0 if the measure failed, so that we just set dst == pos
1871 tangent.set(0, 0);
1872 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001873
1874 /* This is the old way (that explains our approach but is way too slow
1875 SkMatrix matrix;
1876 SkPoint pt;
1877
1878 pt.set(sx, sy);
1879 matrix.setSinCos(tangent.fY, tangent.fX);
1880 matrix.preTranslate(-sx, 0);
1881 matrix.postTranslate(pos.fX, pos.fY);
1882 matrix.mapPoints(&dst[i], &pt, 1);
1883 */
1884 dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy),
1885 pos.fY + SkScalarMul(tangent.fX, sy));
1886 }
1887}
1888
1889/* TODO
1890
1891 Need differentially more subdivisions when the follow-path is curvy. Not sure how to
1892 determine that, but we need it. I guess a cheap answer is let the caller tell us,
1893 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
1894*/
1895static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
1896 const SkMatrix& matrix) {
1897 SkPath::Iter iter(src, false);
1898 SkPoint srcP[4], dstP[3];
1899 SkPath::Verb verb;
1900
1901 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
1902 switch (verb) {
1903 case SkPath::kMove_Verb:
1904 morphpoints(dstP, srcP, 1, meas, matrix);
1905 dst->moveTo(dstP[0]);
1906 break;
1907 case SkPath::kLine_Verb:
1908 // turn lines into quads to look bendy
1909 srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX);
1910 srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY);
1911 morphpoints(dstP, srcP, 2, meas, matrix);
1912 dst->quadTo(dstP[0], dstP[1]);
1913 break;
1914 case SkPath::kQuad_Verb:
1915 morphpoints(dstP, &srcP[1], 2, meas, matrix);
1916 dst->quadTo(dstP[0], dstP[1]);
1917 break;
1918 case SkPath::kCubic_Verb:
1919 morphpoints(dstP, &srcP[1], 3, meas, matrix);
1920 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
1921 break;
1922 case SkPath::kClose_Verb:
1923 dst->close();
1924 break;
1925 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001926 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001927 break;
1928 }
1929 }
1930}
1931
1932void SkDraw::drawTextOnPath(const char text[], size_t byteLength,
1933 const SkPath& follow, const SkMatrix* matrix,
1934 const SkPaint& paint) const {
1935 SkASSERT(byteLength == 0 || text != NULL);
1936
1937 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001938 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001939 return;
1940 }
1941
djsollen@google.com166e6532012-03-20 14:24:38 +00001942 SkTextToPathIter iter(text, byteLength, paint, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001943 SkPathMeasure meas(follow, false);
1944 SkScalar hOffset = 0;
1945
1946 // need to measure first
1947 if (paint.getTextAlign() != SkPaint::kLeft_Align) {
1948 SkScalar pathLen = meas.getLength();
1949 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
1950 pathLen = SkScalarHalf(pathLen);
1951 }
1952 hOffset += pathLen;
1953 }
1954
1955 const SkPath* iterPath;
1956 SkScalar xpos;
1957 SkMatrix scaledMatrix;
1958 SkScalar scale = iter.getPathScale();
1959
1960 scaledMatrix.setScale(scale, scale);
reed@google.coma76de3d2011-01-13 18:30:42 +00001961
reed@google.com7b4531f2012-08-07 15:53:00 +00001962 while (iter.next(&iterPath, &xpos)) {
1963 if (iterPath) {
1964 SkPath tmp;
1965 SkMatrix m(scaledMatrix);
jvanverthb3eb6872014-10-24 07:12:51 -07001966
1967 tmp.setIsVolatile(true);
reed@google.com7b4531f2012-08-07 15:53:00 +00001968 m.postTranslate(xpos + hOffset, 0);
1969 if (matrix) {
1970 m.postConcat(*matrix);
1971 }
1972 morphpath(&tmp, *iterPath, meas, m);
1973 if (fDevice) {
1974 fDevice->drawPath(*this, tmp, iter.getPaint(), NULL, true);
1975 } else {
1976 this->drawPath(tmp, iter.getPaint(), NULL, true);
1977 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001978 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001979 }
1980}
1981
1982///////////////////////////////////////////////////////////////////////////////
1983
reed@google.com045e62d2011-10-24 12:19:46 +00001984typedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRasterClip&,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001985 SkBlitter*);
1986
1987static HairProc ChooseHairProc(bool doAntiAlias) {
1988 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
1989}
1990
1991static bool texture_to_matrix(const VertState& state, const SkPoint verts[],
1992 const SkPoint texs[], SkMatrix* matrix) {
1993 SkPoint src[3], dst[3];
reed@google.coma76de3d2011-01-13 18:30:42 +00001994
reed@android.com8a1c16f2008-12-17 15:59:43 +00001995 src[0] = texs[state.f0];
1996 src[1] = texs[state.f1];
1997 src[2] = texs[state.f2];
1998 dst[0] = verts[state.f0];
1999 dst[1] = verts[state.f1];
2000 dst[2] = verts[state.f2];
2001 return matrix->setPolyToPoly(src, dst, 3);
2002}
2003
2004class SkTriColorShader : public SkShader {
2005public:
2006 SkTriColorShader() {}
2007
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002008 virtual size_t contextSize() const SK_OVERRIDE;
reed@google.coma76de3d2011-01-13 18:30:42 +00002009
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002010 class TriColorShaderContext : public SkShader::Context {
2011 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00002012 TriColorShaderContext(const SkTriColorShader& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002013 virtual ~TriColorShaderContext();
2014
2015 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
2016
2017 virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
2018
2019 private:
2020 SkMatrix fDstToUnit;
2021 SkPMColor fColors[3];
2022
2023 typedef SkShader::Context INHERITED;
2024 };
reed@google.coma76de3d2011-01-13 18:30:42 +00002025
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00002026 SK_TO_STRING_OVERRIDE()
reed9fa60da2014-08-21 07:59:51 -07002027 SK_DECLARE_NOT_FLATTENABLE_PROCS(SkTriColorShader)
djsollen@google.comba28d032012-03-26 17:57:35 +00002028
reed@android.com8a1c16f2008-12-17 15:59:43 +00002029protected:
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +00002030 virtual Context* onCreateContext(const ContextRec& rec, void* storage) const SK_OVERRIDE {
2031 return SkNEW_PLACEMENT_ARGS(storage, TriColorShaderContext, (*this, rec));
2032 }
2033
reed@android.com8a1c16f2008-12-17 15:59:43 +00002034private:
reed@android.com8a1c16f2008-12-17 15:59:43 +00002035 typedef SkShader INHERITED;
2036};
2037
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002038bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const SkColor colors[],
2039 int index0, int index1, int index2) {
reed@google.coma76de3d2011-01-13 18:30:42 +00002040
reed@android.com8a1c16f2008-12-17 15:59:43 +00002041 fColors[0] = SkPreMultiplyColor(colors[index0]);
2042 fColors[1] = SkPreMultiplyColor(colors[index1]);
2043 fColors[2] = SkPreMultiplyColor(colors[index2]);
reed@google.coma76de3d2011-01-13 18:30:42 +00002044
reed@android.com8a1c16f2008-12-17 15:59:43 +00002045 SkMatrix m, im;
2046 m.reset();
2047 m.set(0, pts[index1].fX - pts[index0].fX);
2048 m.set(1, pts[index2].fX - pts[index0].fX);
2049 m.set(2, pts[index0].fX);
2050 m.set(3, pts[index1].fY - pts[index0].fY);
2051 m.set(4, pts[index2].fY - pts[index0].fY);
2052 m.set(5, pts[index0].fY);
2053 if (!m.invert(&im)) {
2054 return false;
2055 }
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00002056 // We can't call getTotalInverse(), because we explicitly don't want to look at the localmatrix
2057 // as our interators are intrinsically tied to the vertices, and nothing else.
2058 SkMatrix ctmInv;
2059 if (!this->getCTM().invert(&ctmInv)) {
2060 return false;
2061 }
2062 fDstToUnit.setConcat(im, ctmInv);
commit-bot@chromium.org92362382014-03-18 12:51:48 +00002063 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002064}
2065
2066#include "SkColorPriv.h"
reed@android.com689411a2008-12-18 02:52:32 +00002067#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00002068
2069static int ScalarTo256(SkScalar v) {
2070 int scale = SkScalarToFixed(v) >> 8;
2071 if (scale < 0) {
2072 scale = 0;
2073 }
2074 if (scale > 255) {
2075 scale = 255;
2076 }
2077 return SkAlpha255To256(scale);
2078}
2079
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002080
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00002081SkTriColorShader::TriColorShaderContext::TriColorShaderContext(const SkTriColorShader& shader,
2082 const ContextRec& rec)
2083 : INHERITED(shader, rec) {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002084
2085SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {}
2086
2087size_t SkTriColorShader::contextSize() const {
2088 return sizeof(TriColorShaderContext);
2089}
2090void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00002091 const int alphaScale = Sk255To256(this->getPaintAlpha());
2092
reed@android.com8a1c16f2008-12-17 15:59:43 +00002093 SkPoint src;
reed@google.coma76de3d2011-01-13 18:30:42 +00002094
reed@android.com8a1c16f2008-12-17 15:59:43 +00002095 for (int i = 0; i < count; i++) {
2096 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
2097 x += 1;
reed@google.coma76de3d2011-01-13 18:30:42 +00002098
reed@android.com8a1c16f2008-12-17 15:59:43 +00002099 int scale1 = ScalarTo256(src.fX);
2100 int scale2 = ScalarTo256(src.fY);
2101 int scale0 = 256 - scale1 - scale2;
2102 if (scale0 < 0) {
2103 if (scale1 > scale2) {
2104 scale2 = 256 - scale1;
2105 } else {
2106 scale1 = 256 - scale2;
2107 }
2108 scale0 = 0;
2109 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002110
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00002111 if (256 != alphaScale) {
2112 scale0 = SkAlphaMul(scale0, alphaScale);
2113 scale1 = SkAlphaMul(scale1, alphaScale);
2114 scale2 = SkAlphaMul(scale2, alphaScale);
2115 }
2116
reed@android.com8a1c16f2008-12-17 15:59:43 +00002117 dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00002118 SkAlphaMulQ(fColors[1], scale1) +
2119 SkAlphaMulQ(fColors[2], scale2);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002120 }
2121}
2122
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00002123#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00002124void SkTriColorShader::toString(SkString* str) const {
2125 str->append("SkTriColorShader: (");
2126
2127 this->INHERITED::toString(str);
2128
2129 str->append(")");
2130}
2131#endif
2132
reed@android.com8a1c16f2008-12-17 15:59:43 +00002133void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
2134 const SkPoint vertices[], const SkPoint textures[],
2135 const SkColor colors[], SkXfermode* xmode,
2136 const uint16_t indices[], int indexCount,
2137 const SkPaint& paint) const {
bsalomon49f085d2014-09-05 13:34:00 -07002138 SkASSERT(0 == count || vertices);
reed@google.coma76de3d2011-01-13 18:30:42 +00002139
reed@android.com8a1c16f2008-12-17 15:59:43 +00002140 // abort early if there is nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00002141 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002142 return;
2143 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002144
reed@android.com8a1c16f2008-12-17 15:59:43 +00002145 // transform out vertices into device coordinates
2146 SkAutoSTMalloc<16, SkPoint> storage(count);
2147 SkPoint* devVerts = storage.get();
2148 fMatrix->mapPoints(devVerts, vertices, count);
reed@google.coma76de3d2011-01-13 18:30:42 +00002149
reed@android.com8a1c16f2008-12-17 15:59:43 +00002150 /*
2151 We can draw the vertices in 1 of 4 ways:
2152
2153 - solid color (no shader/texture[], no colors[])
2154 - just colors (no shader/texture[], has colors[])
2155 - just texture (has shader/texture[], no colors[])
2156 - colors * texture (has shader/texture[], has colors[])
reed@google.coma76de3d2011-01-13 18:30:42 +00002157
reed@android.com8a1c16f2008-12-17 15:59:43 +00002158 Thus for texture drawing, we need both texture[] and a shader.
2159 */
2160
2161 SkTriColorShader triShader; // must be above declaration of p
2162 SkPaint p(paint);
2163
2164 SkShader* shader = p.getShader();
2165 if (NULL == shader) {
2166 // if we have no shader, we ignore the texture coordinates
2167 textures = NULL;
2168 } else if (NULL == textures) {
2169 // if we don't have texture coordinates, ignore the shader
2170 p.setShader(NULL);
2171 shader = NULL;
2172 }
2173
2174 // setup the custom shader (if needed)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002175 SkAutoTUnref<SkComposeShader> composeShader;
bsalomon49f085d2014-09-05 13:34:00 -07002176 if (colors) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002177 if (NULL == textures) {
2178 // just colors (no texture)
reed@google.coma641f3f2012-12-13 22:16:30 +00002179 shader = p.setShader(&triShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002180 } else {
2181 // colors * texture
2182 SkASSERT(shader);
2183 bool releaseMode = false;
2184 if (NULL == xmode) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00002185 xmode = SkXfermode::Create(SkXfermode::kModulate_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002186 releaseMode = true;
2187 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002188 composeShader.reset(SkNEW_ARGS(SkComposeShader, (&triShader, shader, xmode)));
2189 p.setShader(composeShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002190 if (releaseMode) {
2191 xmode->unref();
2192 }
2193 }
2194 }
2195
2196 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002197 // Abort early if we failed to create a shader context.
reed@google.comea033602012-12-14 13:13:55 +00002198 if (blitter->isNullBlitter()) {
2199 return;
2200 }
2201
reed@android.com8a1c16f2008-12-17 15:59:43 +00002202 // setup our state and function pointer for iterating triangles
2203 VertState state(count, indices, indexCount);
2204 VertState::Proc vertProc = state.chooseProc(vmode);
reed@google.coma76de3d2011-01-13 18:30:42 +00002205
bsalomon49f085d2014-09-05 13:34:00 -07002206 if (textures || colors) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002207 while (vertProc(&state)) {
bsalomon49f085d2014-09-05 13:34:00 -07002208 if (textures) {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00002209 SkMatrix tempM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002210 if (texture_to_matrix(state, vertices, textures, &tempM)) {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00002211 SkShader::ContextRec rec(*fBitmap, p, *fMatrix);
2212 rec.fLocalMatrix = &tempM;
2213 if (!blitter->resetShaderContext(rec)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002214 continue;
2215 }
2216 }
2217 }
bsalomon49f085d2014-09-05 13:34:00 -07002218 if (colors) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002219 // Find the context for triShader.
2220 SkTriColorShader::TriColorShaderContext* triColorShaderContext;
2221
2222 SkShader::Context* shaderContext = blitter->getShaderContext();
2223 SkASSERT(shaderContext);
2224 if (p.getShader() == &triShader) {
2225 triColorShaderContext =
2226 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContext);
2227 } else {
2228 // The shader is a compose shader and triShader is its first shader.
2229 SkASSERT(p.getShader() == composeShader);
2230 SkASSERT(composeShader->getShaderA() == &triShader);
2231 SkComposeShader::ComposeShaderContext* composeShaderContext =
2232 static_cast<SkComposeShader::ComposeShaderContext*>(shaderContext);
2233 SkShader::Context* shaderContextA = composeShaderContext->getShaderContextA();
2234 triColorShaderContext =
2235 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContextA);
2236 }
2237
2238 if (!triColorShaderContext->setup(vertices, colors,
2239 state.f0, state.f1, state.f2)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002240 continue;
2241 }
2242 }
reed@google.com045e62d2011-10-24 12:19:46 +00002243
2244 SkPoint tmp[] = {
2245 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2246 };
2247 SkScan::FillTriangle(tmp, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002248 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002249 } else {
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00002250 // no colors[] and no texture, stroke hairlines with paint's color.
reed@android.com8a1c16f2008-12-17 15:59:43 +00002251 HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
reed@google.com045e62d2011-10-24 12:19:46 +00002252 const SkRasterClip& clip = *fRC;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002253 while (vertProc(&state)) {
reed@google.com045e62d2011-10-24 12:19:46 +00002254 hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get());
2255 hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get());
2256 hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002257 }
2258 }
2259}
2260
reed@google.com0a0a2362011-03-23 13:51:55 +00002261///////////////////////////////////////////////////////////////////////////////
2262///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00002263
2264#ifdef SK_DEBUG
2265
reed@android.comf2b98d62010-12-20 18:26:13 +00002266void SkDraw::validate() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002267 SkASSERT(fBitmap != NULL);
2268 SkASSERT(fMatrix != NULL);
2269 SkASSERT(fClip != NULL);
reed@google.com045e62d2011-10-24 12:19:46 +00002270 SkASSERT(fRC != NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002271
reed@google.com045e62d2011-10-24 12:19:46 +00002272 const SkIRect& cr = fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002273 SkIRect br;
2274
reed@android.comf2b98d62010-12-20 18:26:13 +00002275 br.set(0, 0, fBitmap->width(), fBitmap->height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002276 SkASSERT(cr.isEmpty() || br.contains(cr));
2277}
2278
2279#endif
2280
reed@android.com8a1c16f2008-12-17 15:59:43 +00002281////////////////////////////////////////////////////////////////////////////////////////////////
2282
2283#include "SkPath.h"
2284#include "SkDraw.h"
2285#include "SkRegion.h"
2286#include "SkBlitter.h"
2287
2288static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
reed@google.com30711b72012-12-18 19:18:39 +00002289 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
reed@android.com8a1c16f2008-12-17 15:59:43 +00002290 SkIRect* bounds) {
2291 if (devPath.isEmpty()) {
2292 return false;
2293 }
2294
reed@android.com8a1c16f2008-12-17 15:59:43 +00002295 // init our bounds from the path
2296 {
reed@android.comd252db02009-04-01 18:31:44 +00002297 SkRect pathBounds = devPath.getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002298 pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf);
2299 pathBounds.roundOut(bounds);
2300 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002301
tomhudson@google.com6db75fc2012-03-23 14:46:48 +00002302 SkIPoint margin = SkIPoint::Make(0, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002303 if (filter) {
2304 SkASSERT(filterMatrix);
reed@google.coma76de3d2011-01-13 18:30:42 +00002305
bungeman@google.com5af16f82011-09-02 15:06:44 +00002306 SkMask srcM, dstM;
reed@google.coma76de3d2011-01-13 18:30:42 +00002307
reed@android.com8a1c16f2008-12-17 15:59:43 +00002308 srcM.fBounds = *bounds;
2309 srcM.fFormat = SkMask::kA8_Format;
2310 srcM.fImage = NULL;
2311 if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
2312 return false;
2313 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002314 }
2315
bungeman@google.com5af16f82011-09-02 15:06:44 +00002316 // (possibly) trim the bounds to reflect the clip
reed@android.com8a1c16f2008-12-17 15:59:43 +00002317 // (plus whatever slop the filter needs)
bungeman@google.com5af16f82011-09-02 15:06:44 +00002318 if (clipBounds) {
2319 SkIRect tmp = *clipBounds;
reed@android.com35555912009-03-16 18:46:55 +00002320 // Ugh. Guard against gigantic margins from wacky filters. Without this
2321 // check we can request arbitrary amounts of slop beyond our visible
2322 // clip, and bring down the renderer (at least on finite RAM machines
2323 // like handsets, etc.). Need to balance this invented value between
2324 // quality of large filters like blurs, and the corresponding memory
2325 // requests.
2326 static const int MAX_MARGIN = 128;
2327 tmp.inset(-SkMin32(margin.fX, MAX_MARGIN),
2328 -SkMin32(margin.fY, MAX_MARGIN));
bungeman@google.com5af16f82011-09-02 15:06:44 +00002329 if (!bounds->intersect(tmp)) {
2330 return false;
2331 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002332 }
2333
2334 return true;
2335}
2336
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002337static void draw_into_mask(const SkMask& mask, const SkPath& devPath,
2338 SkPaint::Style style) {
reed@google.com045e62d2011-10-24 12:19:46 +00002339 SkBitmap bm;
2340 SkDraw draw;
2341 SkRasterClip clip;
2342 SkMatrix matrix;
2343 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002344
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00002345 bm.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
2346 mask.fImage, mask.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002347
reed@google.com045e62d2011-10-24 12:19:46 +00002348 clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00002349 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
2350 -SkIntToScalar(mask.fBounds.fTop));
2351
2352 draw.fBitmap = &bm;
reed@google.com045e62d2011-10-24 12:19:46 +00002353 draw.fRC = &clip;
2354 draw.fClip = &clip.bwRgn();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002355 draw.fMatrix = &matrix;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002356 paint.setAntiAlias(true);
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002357 paint.setStyle(style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002358 draw.drawPath(devPath, paint);
2359}
2360
2361bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
reed@google.com30711b72012-12-18 19:18:39 +00002362 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002363 SkMask* mask, SkMask::CreateMode mode,
2364 SkPaint::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002365 if (SkMask::kJustRenderImage_CreateMode != mode) {
2366 if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
2367 return false;
2368 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002369
reed@android.com8a1c16f2008-12-17 15:59:43 +00002370 if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
2371 mask->fFormat = SkMask::kA8_Format;
2372 mask->fRowBytes = mask->fBounds.width();
reed@android.com543ed932009-04-24 12:43:40 +00002373 size_t size = mask->computeImageSize();
2374 if (0 == size) {
2375 // we're too big to allocate the mask, abort
2376 return false;
2377 }
2378 mask->fImage = SkMask::AllocImage(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002379 memset(mask->fImage, 0, mask->computeImageSize());
2380 }
2381
2382 if (SkMask::kJustComputeBounds_CreateMode != mode) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002383 draw_into_mask(*mask, devPath, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002384 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002385
reed@android.com8a1c16f2008-12-17 15:59:43 +00002386 return true;
2387}