blob: d0d4f0718a73cdac443c52c5838993b8f77b94e7 [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 &&
463 matrix->rectStaysRect() && 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;
reed@google.coma76de3d2011-01-13 18:30:42 +0000598
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
reed@android.comf2b98d62010-12-20 18:26:13 +0000604 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000605 fDevice->drawPath(*this, path, newPaint, &preMatrix,
reed@android.comf2b98d62010-12-20 18:26:13 +0000606 (count-1) == i);
607 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000608 this->drawPath(path, newPaint, &preMatrix,
609 (count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000610 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000611 }
612 } else {
613 SkRect r;
reed@google.coma76de3d2011-01-13 18:30:42 +0000614
reed@android.com8a1c16f2008-12-17 15:59:43 +0000615 for (size_t i = 0; i < count; i++) {
616 r.fLeft = pts[i].fX - radius;
617 r.fTop = pts[i].fY - radius;
618 r.fRight = r.fLeft + width;
619 r.fBottom = r.fTop + width;
reed@android.comf2b98d62010-12-20 18:26:13 +0000620 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000621 fDevice->drawRect(*this, r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000622 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000623 this->drawRect(r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000624 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 }
626 }
627 break;
628 }
629 case SkCanvas::kLines_PointMode:
robertphillips@google.com629ab542012-11-28 17:18:11 +0000630#ifndef SK_DISABLE_DASHING_OPTIMIZATION
631 if (2 == count && NULL != paint.getPathEffect()) {
632 // 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 }
714#endif // DISABLE_DASHING_OPTIMIZATION
715 // couldn't take fast path so fall through!
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716 case SkCanvas::kPolygon_PointMode: {
717 count -= 1;
718 SkPath path;
719 SkPaint p(paint);
720 p.setStyle(SkPaint::kStroke_Style);
721 size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
722 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
820 ir.inset(-1, -1);
821 }
822 if (fRC->quickReject(ir)) {
823 return;
rmistry@google.come09d6f42013-08-27 18:53:41 +0000824 }
825
reed@google.com1c028bd2013-08-28 15:23:19 +0000826 SkDeviceLooper looper(*fBitmap, *fRC, ir, paint.isAntiAlias());
827 while (looper.next()) {
828 SkRect localDevRect;
829 looper.mapRect(&localDevRect, devRect);
830 SkMatrix localMatrix;
831 looper.mapMatrix(&localMatrix, matrix);
rmistry@google.come09d6f42013-08-27 18:53:41 +0000832
reed@google.com1c028bd2013-08-28 15:23:19 +0000833 SkAutoBlitterChoose blitterStorage(looper.getBitmap(), localMatrix,
834 paint);
835 const SkRasterClip& clip = looper.getRC();
836 SkBlitter* blitter = blitterStorage.get();
837
838 // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
839 // case we are also hairline (if we've gotten to here), which devolves to
840 // effectively just kFill
841 switch (rtype) {
842 case kFill_RectType:
843 if (paint.isAntiAlias()) {
844 SkScan::AntiFillRect(localDevRect, clip, blitter);
845 } else {
846 SkScan::FillRect(localDevRect, clip, blitter);
847 }
848 break;
849 case kStroke_RectType:
850 if (paint.isAntiAlias()) {
851 SkScan::AntiFrameRect(localDevRect, strokeSize, clip, blitter);
852 } else {
853 SkScan::FrameRect(localDevRect, strokeSize, clip, blitter);
854 }
855 break;
856 case kHair_RectType:
857 if (paint.isAntiAlias()) {
858 SkScan::AntiHairRect(localDevRect, clip, blitter);
859 } else {
860 SkScan::HairRect(localDevRect, clip, blitter);
861 }
862 break;
863 default:
864 SkDEBUGFAIL("bad rtype");
865 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000866 }
867}
868
869void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
870 if (srcM.fBounds.isEmpty()) {
871 return;
872 }
873
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000874 const SkMask* mask = &srcM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000875
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000876 SkMask dstM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000877 if (paint.getMaskFilter() &&
878 paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) {
879 mask = &dstM;
bungeman@google.combf2ac7e2011-09-26 19:51:33 +0000880 } else {
881 dstM.fImage = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000882 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000883 SkAutoMaskFreeImage ami(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000884
reed@google.com045e62d2011-10-24 12:19:46 +0000885 SkAutoBlitterChoose blitterChooser(*fBitmap, *fMatrix, paint);
886 SkBlitter* blitter = blitterChooser.get();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000887
reed@google.com045e62d2011-10-24 12:19:46 +0000888 SkAAClipBlitterWrapper wrapper;
889 const SkRegion* clipRgn;
890
891 if (fRC->isBW()) {
892 clipRgn = &fRC->bwRgn();
893 } else {
894 wrapper.init(*fRC, blitter);
895 clipRgn = &wrapper.getRgn();
896 blitter = wrapper.getBlitter();
897 }
898 blitter->blitMaskRegion(*mask, *clipRgn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000899}
900
reed@android.comebdeeb82009-09-03 21:45:49 +0000901static SkScalar fast_len(const SkVector& vec) {
902 SkScalar x = SkScalarAbs(vec.fX);
903 SkScalar y = SkScalarAbs(vec.fY);
904 if (x < y) {
905 SkTSwap(x, y);
906 }
907 return x + SkScalarHalf(y);
908}
909
reed@google.comecadf992011-09-19 19:18:18 +0000910static bool xfermodeSupportsCoverageAsAlpha(SkXfermode* xfer) {
911 SkXfermode::Coeff dc;
912 if (!SkXfermode::AsCoeff(xfer, NULL, &dc)) {
913 return false;
914 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000915
reed@google.comecadf992011-09-19 19:18:18 +0000916 switch (dc) {
917 case SkXfermode::kOne_Coeff:
918 case SkXfermode::kISA_Coeff:
919 case SkXfermode::kISC_Coeff:
920 return true;
921 default:
922 return false;
923 }
924}
925
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000926bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix& matrix,
927 SkScalar* coverage) {
928 SkASSERT(strokeWidth > 0);
929 // We need to try to fake a thick-stroke with a modulated hairline.
reed@google.comecadf992011-09-19 19:18:18 +0000930
tomhudson@google.com8d430182011-06-06 19:11:19 +0000931 if (matrix.hasPerspective()) {
reed@android.comebdeeb82009-09-03 21:45:49 +0000932 return false;
933 }
reed@google.comecadf992011-09-19 19:18:18 +0000934
reed@android.comebdeeb82009-09-03 21:45:49 +0000935 SkVector src[2], dst[2];
reed@google.comecadf992011-09-19 19:18:18 +0000936 src[0].set(strokeWidth, 0);
937 src[1].set(0, strokeWidth);
reed@android.comebdeeb82009-09-03 21:45:49 +0000938 matrix.mapVectors(dst, src, 2);
939 SkScalar len0 = fast_len(dst[0]);
940 SkScalar len1 = fast_len(dst[1]);
agl@chromium.org652807b2010-04-27 15:47:34 +0000941 if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000942 if (NULL != coverage) {
943 *coverage = SkScalarAve(len0, len1);
944 }
reed@android.comebdeeb82009-09-03 21:45:49 +0000945 return true;
946 }
947 return false;
948}
949
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000950void SkDraw::drawRRect(const SkRRect& rrect, const SkPaint& paint) const {
951 SkDEBUGCODE(this->validate());
952
953 if (fRC->isEmpty()) {
954 return;
955 }
956
957 {
958 // TODO: Investigate optimizing these options. They are in the same
959 // order as SkDraw::drawPath, which handles each case. It may be
960 // that there is no way to optimize for these using the SkRRect path.
961 SkScalar coverage;
962 if (SkDrawTreatAsHairline(paint, *fMatrix, &coverage)) {
963 goto DRAW_PATH;
964 }
965
966 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
967 goto DRAW_PATH;
968 }
969
970 if (paint.getRasterizer()) {
971 goto DRAW_PATH;
972 }
973 }
974
975 if (paint.getMaskFilter()) {
976 // Transform the rrect into device space.
977 SkRRect devRRect;
978 if (rrect.transform(*fMatrix, &devRRect)) {
979 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
reed868074b2014-06-03 10:53:59 -0700980 if (paint.getMaskFilter()->filterRRect(devRRect, *fMatrix, *fRC, blitter.get(),
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000981 SkPaint::kFill_Style)) {
982 return; // filterRRect() called the blitter, so we're done
983 }
984 }
985 }
986
987DRAW_PATH:
988 // Now fall back to the default case of using a path.
989 SkPath path;
990 path.addRRect(rrect);
991 this->drawPath(path, paint, NULL, true);
992}
993
reed@google.com32e5d972011-07-27 18:25:57 +0000994void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
reed@google.com126f7f52013-11-07 16:06:53 +0000995 const SkMatrix* prePathMatrix, bool pathIsMutable,
krajcevski53f09592014-08-06 11:12:14 -0700996 bool drawCoverage, SkBlitter* customBlitter) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000997 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000998
999 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001000 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001001 return;
1002 }
1003
1004 SkPath* pathPtr = (SkPath*)&origSrcPath;
1005 bool doFill = true;
1006 SkPath tmpPath;
1007 SkMatrix tmpMatrix;
1008 const SkMatrix* matrix = fMatrix;
1009
1010 if (prePathMatrix) {
reed@google.com32e5d972011-07-27 18:25:57 +00001011 if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
1012 origPaint.getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001013 SkPath* result = pathPtr;
reed@google.coma76de3d2011-01-13 18:30:42 +00001014
reed@android.com8a1c16f2008-12-17 15:59:43 +00001015 if (!pathIsMutable) {
1016 result = &tmpPath;
1017 pathIsMutable = true;
1018 }
1019 pathPtr->transform(*prePathMatrix, result);
1020 pathPtr = result;
1021 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001022 tmpMatrix.setConcat(*matrix, *prePathMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001023 matrix = &tmpMatrix;
1024 }
1025 }
1026 // at this point we're done with prePathMatrix
1027 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.coma76de3d2011-01-13 18:30:42 +00001028
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001029 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
reed@google.coma76de3d2011-01-13 18:30:42 +00001030
reed@google.comecadf992011-09-19 19:18:18 +00001031 {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001032 SkScalar coverage;
1033 if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) {
1034 if (SK_Scalar1 == coverage) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001035 paint.writable()->setStrokeWidth(0);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001036 } else if (xfermodeSupportsCoverageAsAlpha(origPaint.getXfermode())) {
1037 U8CPU newAlpha;
1038#if 0
1039 newAlpha = SkToU8(SkScalarRoundToInt(coverage *
1040 origPaint.getAlpha()));
1041#else
1042 // this is the old technique, which we preserve for now so
1043 // we don't change previous results (testing)
1044 // the new way seems fine, its just (a tiny bit) different
1045 int scale = (int)SkScalarMul(coverage, 256);
1046 newAlpha = origPaint.getAlpha() * scale >> 8;
1047#endif
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001048 SkPaint* writablePaint = paint.writable();
1049 writablePaint->setStrokeWidth(0);
1050 writablePaint->setAlpha(newAlpha);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001051 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001052 }
1053 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001054
reed@google.com32e5d972011-07-27 18:25:57 +00001055 if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
reed@google.com4bbdeac2013-01-24 21:03:11 +00001056 SkRect cullRect;
1057 const SkRect* cullRectPtr = NULL;
1058 if (this->computeConservativeLocalClipBounds(&cullRect)) {
1059 cullRectPtr = &cullRect;
1060 }
1061 doFill = paint->getFillPath(*pathPtr, &tmpPath, cullRectPtr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001062 pathPtr = &tmpPath;
1063 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001064
reed@google.com32e5d972011-07-27 18:25:57 +00001065 if (paint->getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001066 SkMask mask;
reed@google.com32e5d972011-07-27 18:25:57 +00001067 if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
reed@google.com045e62d2011-10-24 12:19:46 +00001068 &fRC->getBounds(), paint->getMaskFilter(), &mask,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001069 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
reed@google.com32e5d972011-07-27 18:25:57 +00001070 this->drawDevMask(mask, *paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001071 SkMask::FreeImage(mask.fImage);
1072 }
1073 return;
1074 }
1075
1076 // avoid possibly allocating a new path in transform if we can
1077 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1078
1079 // transform the path into device space
1080 pathPtr->transform(*matrix, devPathPtr);
1081
krajcevski53f09592014-08-06 11:12:14 -07001082 SkBlitter* blitter = NULL;
1083 SkAutoBlitterChoose blitterStorage;
1084 if (NULL == customBlitter) {
1085 blitterStorage.choose(*fBitmap, *fMatrix, *paint, drawCoverage);
1086 blitter = blitterStorage.get();
1087 } else {
1088 blitter = customBlitter;
1089 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001090
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001091 if (paint->getMaskFilter()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001092 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001093 SkPaint::kStroke_Style;
krajcevski53f09592014-08-06 11:12:14 -07001094 if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC, blitter, style)) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001095 return; // filterPath() called the blitter, so we're done
1096 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001097 }
1098
reed@google.com045e62d2011-10-24 12:19:46 +00001099 void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001100 if (doFill) {
reed@google.com32e5d972011-07-27 18:25:57 +00001101 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001102 proc = SkScan::AntiFillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001103 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001104 proc = SkScan::FillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001105 }
1106 } else { // hairline
reed@google.com32e5d972011-07-27 18:25:57 +00001107 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001108 proc = SkScan::AntiHairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001109 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001110 proc = SkScan::HairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001111 }
1112 }
krajcevski53f09592014-08-06 11:12:14 -07001113 proc(*devPathPtr, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001114}
1115
reed@android.com0baf1932009-06-24 12:41:42 +00001116/** For the purposes of drawing bitmaps, if a matrix is "almost" translate
1117 go ahead and treat it as if it were, so that subsequent code can go fast.
1118 */
1119static bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
reed@google.com070dcd82013-01-07 20:27:52 +00001120 unsigned bits = 0; // TODO: find a way to allow the caller to tell us to
1121 // respect filtering.
1122 return SkTreatAsSprite(matrix, bitmap.width(), bitmap.height(), bits);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123}
1124
1125void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
1126 const SkPaint& paint) const {
reed@google.com900ecf22014-02-20 20:55:37 +00001127 SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001128
reed@google.coma76de3d2011-01-13 18:30:42 +00001129 if (just_translate(*fMatrix, bitmap)) {
reed@google.come1ca7052013-12-17 19:22:07 +00001130 int ix = SkScalarRoundToInt(fMatrix->getTranslateX());
1131 int iy = SkScalarRoundToInt(fMatrix->getTranslateY());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001132
reed@google.coma641f3f2012-12-13 22:16:30 +00001133 SkAutoLockPixels alp(bitmap);
1134 if (!bitmap.readyToDraw()) {
1135 return;
1136 }
1137
reed@android.com8a1c16f2008-12-17 15:59:43 +00001138 SkMask mask;
1139 mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1140 mask.fFormat = SkMask::kA8_Format;
scroggo@google.come5f48242013-02-25 21:47:41 +00001141 mask.fRowBytes = SkToU32(bitmap.rowBytes());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001142 mask.fImage = bitmap.getAddr8(0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001143
reed@android.com8a1c16f2008-12-17 15:59:43 +00001144 this->drawDevMask(mask, paint);
1145 } else { // need to xform the bitmap first
1146 SkRect r;
1147 SkMask mask;
reed@google.coma76de3d2011-01-13 18:30:42 +00001148
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149 r.set(0, 0,
1150 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
1151 fMatrix->mapRect(&r);
1152 r.round(&mask.fBounds);
reed@google.coma76de3d2011-01-13 18:30:42 +00001153
reed@android.com8a1c16f2008-12-17 15:59:43 +00001154 // set the mask's bounds to the transformed bitmap-bounds,
1155 // clipped to the actual device
1156 {
1157 SkIRect devBounds;
1158 devBounds.set(0, 0, fBitmap->width(), fBitmap->height());
1159 // need intersect(l, t, r, b) on irect
1160 if (!mask.fBounds.intersect(devBounds)) {
1161 return;
1162 }
1163 }
reed@android.com543ed932009-04-24 12:43:40 +00001164
reed@android.com8a1c16f2008-12-17 15:59:43 +00001165 mask.fFormat = SkMask::kA8_Format;
1166 mask.fRowBytes = SkAlign4(mask.fBounds.width());
reed@android.com543ed932009-04-24 12:43:40 +00001167 size_t size = mask.computeImageSize();
1168 if (0 == size) {
1169 // the mask is too big to allocated, draw nothing
1170 return;
1171 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001172
1173 // allocate (and clear) our temp buffer to hold the transformed bitmap
reed@android.com8a1c16f2008-12-17 15:59:43 +00001174 SkAutoMalloc storage(size);
1175 mask.fImage = (uint8_t*)storage.get();
1176 memset(mask.fImage, 0, size);
reed@google.coma76de3d2011-01-13 18:30:42 +00001177
reed@android.com8a1c16f2008-12-17 15:59:43 +00001178 // now draw our bitmap(src) into mask(dst), transformed by the matrix
1179 {
1180 SkBitmap device;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001181 device.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
1182 mask.fImage, mask.fRowBytes);
reed@google.coma76de3d2011-01-13 18:30:42 +00001183
reed@android.com8a1c16f2008-12-17 15:59:43 +00001184 SkCanvas c(device);
1185 // need the unclipped top/left for the translate
1186 c.translate(-SkIntToScalar(mask.fBounds.fLeft),
1187 -SkIntToScalar(mask.fBounds.fTop));
1188 c.concat(*fMatrix);
reed@android.com3469c762009-02-24 19:03:20 +00001189
1190 // We can't call drawBitmap, or we'll infinitely recurse. Instead
reed@android.comfb12c3e2009-03-05 20:43:42 +00001191 // we manually build a shader and draw that into our new mask
reed@android.com3469c762009-02-24 19:03:20 +00001192 SkPaint tmpPaint;
1193 tmpPaint.setFlags(paint.getFlags());
reed@google.com40c2ba22011-07-25 19:41:22 +00001194 SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
reed@android.com3469c762009-02-24 19:03:20 +00001195 SkRect rr;
1196 rr.set(0, 0, SkIntToScalar(bitmap.width()),
1197 SkIntToScalar(bitmap.height()));
reed@google.com40c2ba22011-07-25 19:41:22 +00001198 c.drawRect(rr, install.paintWithShader());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199 }
1200 this->drawDevMask(mask, paint);
1201 }
1202}
1203
reed@google.com045e62d2011-10-24 12:19:46 +00001204static bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001205 const SkRect& srcR) {
1206 SkRect dstR;
1207 SkIRect devIR;
reed@google.coma76de3d2011-01-13 18:30:42 +00001208
reed@android.com8a1c16f2008-12-17 15:59:43 +00001209 m.mapRect(&dstR, srcR);
reed@google.coma76de3d2011-01-13 18:30:42 +00001210 dstR.roundOut(&devIR);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001211 return c.quickReject(devIR);
1212}
1213
reed@google.com045e62d2011-10-24 12:19:46 +00001214static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 int width, int height) {
1216 SkRect r;
1217 r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
1218 return clipped_out(matrix, clip, r);
1219}
1220
reed@google.com045e62d2011-10-24 12:19:46 +00001221static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y,
1222 const SkBitmap& bitmap) {
1223 return clip.isBW() ||
1224 clip.quickContains(x, y, x + bitmap.width(), y + bitmap.height());
1225}
1226
reed@android.com8a1c16f2008-12-17 15:59:43 +00001227void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
reed@google.com40c2ba22011-07-25 19:41:22 +00001228 const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001229 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001230
1231 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001232 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001233 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001234 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001235 return;
1236 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001237
reed@google.com40c2ba22011-07-25 19:41:22 +00001238 SkPaint paint(origPaint);
1239 paint.setStyle(SkPaint::kFill_Style);
reed@google.coma76de3d2011-01-13 18:30:42 +00001240
reed@android.com8a1c16f2008-12-17 15:59:43 +00001241 SkMatrix matrix;
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001242 matrix.setConcat(*fMatrix, prematrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001243
reed@google.com045e62d2011-10-24 12:19:46 +00001244 if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001245 return;
1246 }
1247
reed868074b2014-06-03 10:53:59 -07001248 if (bitmap.colorType() != kAlpha_8_SkColorType && just_translate(matrix, bitmap)) {
reed@google.comf7ef56d2012-12-14 13:46:53 +00001249 //
1250 // It is safe to call lock pixels now, since we know the matrix is
1251 // (more or less) identity.
1252 //
1253 SkAutoLockPixels alp(bitmap);
1254 if (!bitmap.readyToDraw()) {
1255 return;
1256 }
reed@google.come1ca7052013-12-17 19:22:07 +00001257 int ix = SkScalarRoundToInt(matrix.getTranslateX());
1258 int iy = SkScalarRoundToInt(matrix.getTranslateY());
reed@google.com045e62d2011-10-24 12:19:46 +00001259 if (clipHandlesSprite(*fRC, ix, iy, bitmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001260 SkTBlitterAllocator allocator;
1261 // blitter will be owned by the allocator.
1262 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1263 ix, iy, &allocator);
reed@google.com045e62d2011-10-24 12:19:46 +00001264 if (blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +00001265 SkIRect ir;
1266 ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001267
reed@google.com045e62d2011-10-24 12:19:46 +00001268 SkScan::FillIRect(ir, *fRC, blitter);
1269 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001270 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001271 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001272 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001273
reed@android.com8a1c16f2008-12-17 15:59:43 +00001274 // now make a temp draw on the stack, and use it
1275 //
1276 SkDraw draw(*this);
1277 draw.fMatrix = &matrix;
reed@google.coma76de3d2011-01-13 18:30:42 +00001278
reed@google.com900ecf22014-02-20 20:55:37 +00001279 if (bitmap.colorType() == kAlpha_8_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001280 draw.drawBitmapAsMask(bitmap, paint);
1281 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +00001282 SkAutoBitmapShaderInstall install(bitmap, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001283
1284 SkRect r;
1285 r.set(0, 0, SkIntToScalar(bitmap.width()),
1286 SkIntToScalar(bitmap.height()));
reed@google.coma76de3d2011-01-13 18:30:42 +00001287 // is this ok if paint has a rasterizer?
reed@google.com40c2ba22011-07-25 19:41:22 +00001288 draw.drawRect(r, install.paintWithShader());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001289 }
1290}
1291
1292void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
reed@google.com40c2ba22011-07-25 19:41:22 +00001293 const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001294 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +00001295
reed@android.com8a1c16f2008-12-17 15:59:43 +00001296 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001297 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001298 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001299 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001300 return;
1301 }
1302
1303 SkIRect bounds;
1304 bounds.set(x, y, x + bitmap.width(), y + bitmap.height());
1305
reed@google.com045e62d2011-10-24 12:19:46 +00001306 if (fRC->quickReject(bounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001307 return; // nothing to draw
1308 }
1309
reed@google.com40c2ba22011-07-25 19:41:22 +00001310 SkPaint paint(origPaint);
1311 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001312
reed@google.com045e62d2011-10-24 12:19:46 +00001313 if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001314 SkTBlitterAllocator allocator;
1315 // blitter will be owned by the allocator.
1316 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1317 x, y, &allocator);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001318
1319 if (blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +00001320 SkScan::FillIRect(bounds, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001321 return;
1322 }
1323 }
1324
reed@android.com8a1c16f2008-12-17 15:59:43 +00001325 SkMatrix matrix;
1326 SkRect r;
1327
1328 // get a scalar version of our rect
1329 r.set(bounds);
1330
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001331 // create shader with offset
reed@android.com8a1c16f2008-12-17 15:59:43 +00001332 matrix.setTranslate(r.fLeft, r.fTop);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001333 SkAutoBitmapShaderInstall install(bitmap, paint, &matrix);
1334 const SkPaint& shaderPaint = install.paintWithShader();
reed@google.coma76de3d2011-01-13 18:30:42 +00001335
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336 SkDraw draw(*this);
1337 matrix.reset();
1338 draw.fMatrix = &matrix;
1339 // call ourself with a rect
reed@google.coma76de3d2011-01-13 18:30:42 +00001340 // is this OK if paint has a rasterizer?
reed@google.com40c2ba22011-07-25 19:41:22 +00001341 draw.drawRect(r, shaderPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342}
1343
1344///////////////////////////////////////////////////////////////////////////////
1345
1346#include "SkScalerContext.h"
1347#include "SkGlyphCache.h"
reed@google.come6913762012-08-07 15:19:47 +00001348#include "SkTextToPathIter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001349#include "SkUtils.h"
1350
1351static void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
1352 const char text[], size_t byteLength, SkVector* stopVector) {
1353 SkFixed x = 0, y = 0;
1354 const char* stop = text + byteLength;
1355
1356 SkAutoKern autokern;
reed@google.coma76de3d2011-01-13 18:30:42 +00001357
reed@android.com8a1c16f2008-12-17 15:59:43 +00001358 while (text < stop) {
1359 // don't need x, y here, since all subpixel variants will have the
1360 // same advance
1361 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1362
1363 x += autokern.adjust(glyph) + glyph.fAdvanceX;
1364 y += glyph.fAdvanceY;
1365 }
1366 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
1367
1368 SkASSERT(text == stop);
1369}
1370
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001371bool SkDraw::ShouldDrawTextAsPaths(const SkPaint& paint, const SkMatrix& ctm) {
1372 // hairline glyphs are fast enough so we don't need to cache them
1373 if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) {
1374 return true;
1375 }
1376
1377 // we don't cache perspective
1378 if (ctm.hasPerspective()) {
1379 return true;
1380 }
1381
1382 SkMatrix textM;
1383 return SkPaint::TooBigToUseCache(ctm, *paint.setTextMatrix(&textM));
1384}
1385
reed@android.com8a1c16f2008-12-17 15:59:43 +00001386void SkDraw::drawText_asPaths(const char text[], size_t byteLength,
1387 SkScalar x, SkScalar y,
1388 const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001389 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001390
djsollen@google.com166e6532012-03-20 14:24:38 +00001391 SkTextToPathIter iter(text, byteLength, paint, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392
1393 SkMatrix matrix;
1394 matrix.setScale(iter.getPathScale(), iter.getPathScale());
1395 matrix.postTranslate(x, y);
1396
1397 const SkPath* iterPath;
1398 SkScalar xpos, prevXPos = 0;
1399
reed@google.com7b4531f2012-08-07 15:53:00 +00001400 while (iter.next(&iterPath, &xpos)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001401 matrix.postTranslate(xpos - prevXPos, 0);
reed@google.com7b4531f2012-08-07 15:53:00 +00001402 if (iterPath) {
1403 const SkPaint& pnt = iter.getPaint();
1404 if (fDevice) {
1405 fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
1406 } else {
1407 this->drawPath(*iterPath, pnt, &matrix, false);
1408 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001409 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001410 prevXPos = xpos;
1411 }
1412}
1413
reed@android.com8a1c16f2008-12-17 15:59:43 +00001414// disable warning : local variable used without having been initialized
reed@google.coma76de3d2011-01-13 18:30:42 +00001415#if defined _WIN32 && _MSC_VER >= 1300
reed@android.com8a1c16f2008-12-17 15:59:43 +00001416#pragma warning ( push )
1417#pragma warning ( disable : 4701 )
1418#endif
1419
1420//////////////////////////////////////////////////////////////////////////////
1421
reed868074b2014-06-03 10:53:59 -07001422static void D1G_RectClip(const SkDraw1Glyph& state, SkFixed fx, SkFixed fy, const SkGlyph& glyph) {
mike@reedtribe.org9fb00412014-01-06 03:02:37 +00001423 int left = SkFixedFloorToInt(fx);
1424 int top = SkFixedFloorToInt(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001426 SkASSERT((NULL == state.fClip && state.fAAClip) ||
1427 (state.fClip && NULL == state.fAAClip && state.fClip->isRect()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001428
1429 left += glyph.fLeft;
1430 top += glyph.fTop;
1431
1432 int right = left + glyph.fWidth;
1433 int bottom = top + glyph.fHeight;
1434
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001435 SkMask mask;
1436 SkIRect storage;
1437 SkIRect* bounds = &mask.fBounds;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001438
tomhudson@google.com83a44462011-10-27 15:27:51 +00001439 mask.fBounds.set(left, top, right, bottom);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001440
tomhudson@google.com83a44462011-10-27 15:27:51 +00001441 // this extra test is worth it, assuming that most of the time it succeeds
1442 // since we can avoid writing to storage
1443 if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
1444 if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
1445 return;
1446 bounds = &storage;
1447 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001448
tomhudson@google.com83a44462011-10-27 15:27:51 +00001449 uint8_t* aa = (uint8_t*)glyph.fImage;
1450 if (NULL == aa) {
1451 aa = (uint8_t*)state.fCache->findImage(glyph);
1452 if (NULL == aa) {
1453 return; // can't rasterize glyph
reed@android.com8a1c16f2008-12-17 15:59:43 +00001454 }
tomhudson@google.com83a44462011-10-27 15:27:51 +00001455 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001456
tomhudson@google.com83a44462011-10-27 15:27:51 +00001457 mask.fRowBytes = glyph.rowBytes();
1458 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1459 mask.fImage = aa;
reed@google.com5bdfb332013-05-02 18:55:44 +00001460 state.blitMask(mask, *bounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001461}
1462
reed868074b2014-06-03 10:53:59 -07001463static void D1G_RgnClip(const SkDraw1Glyph& state, SkFixed fx, SkFixed fy, const SkGlyph& glyph) {
mike@reedtribe.org9fb00412014-01-06 03:02:37 +00001464 int left = SkFixedFloorToInt(fx);
1465 int top = SkFixedFloorToInt(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001466 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001467 SkASSERT(!state.fClip->isRect());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001468
1469 SkMask mask;
1470
1471 left += glyph.fLeft;
1472 top += glyph.fTop;
1473
1474 mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001475 SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001476
tomhudson@google.com83a44462011-10-27 15:27:51 +00001477 if (!clipper.done()) {
1478 const SkIRect& cr = clipper.rect();
1479 const uint8_t* aa = (const uint8_t*)glyph.fImage;
1480 if (NULL == aa) {
1481 aa = (uint8_t*)state.fCache->findImage(glyph);
1482 if (NULL == aa) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001483 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001484 }
tomhudson@google.com83a44462011-10-27 15:27:51 +00001485 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001486
tomhudson@google.com83a44462011-10-27 15:27:51 +00001487 mask.fRowBytes = glyph.rowBytes();
1488 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1489 mask.fImage = (uint8_t*)aa;
1490 do {
reed@google.com5bdfb332013-05-02 18:55:44 +00001491 state.blitMask(mask, cr);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001492 clipper.next();
1493 } while (!clipper.done());
1494 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001495}
1496
reed@google.comfd4236e2011-07-25 21:16:22 +00001497static bool hasCustomD1GProc(const SkDraw& draw) {
1498 return draw.fProcs && draw.fProcs->fD1GProc;
1499}
1500
1501static bool needsRasterTextBlit(const SkDraw& draw) {
1502 return !hasCustomD1GProc(draw);
1503}
1504
reed868074b2014-06-03 10:53:59 -07001505SkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter, SkGlyphCache* cache,
1506 const SkPaint& pnt) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001507 fDraw = draw;
bungeman@google.com2211b622012-01-13 15:02:58 +00001508 fBlitter = blitter;
1509 fCache = cache;
reed@google.com5bdfb332013-05-02 18:55:44 +00001510 fPaint = &pnt;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001511
bungeman@google.com94471032013-02-25 15:55:13 +00001512 if (cache->isSubpixel()) {
1513 fHalfSampleX = fHalfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits);
1514 } else {
1515 fHalfSampleX = fHalfSampleY = SK_FixedHalf;
1516 }
1517
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001518 if (hasCustomD1GProc(*draw)) {
reed@google.com045e62d2011-10-24 12:19:46 +00001519 // todo: fix this assumption about clips w/ custom
1520 fClip = draw->fClip;
1521 fClipBounds = fClip->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001522 return draw->fProcs->fD1GProc;
1523 }
1524
reed@google.com045e62d2011-10-24 12:19:46 +00001525 if (draw->fRC->isBW()) {
1526 fAAClip = NULL;
1527 fClip = &draw->fRC->bwRgn();
1528 fClipBounds = fClip->getBounds();
reed868074b2014-06-03 10:53:59 -07001529 if (fClip->isRect()) {
1530 return D1G_RectClip;
reed@google.com045e62d2011-10-24 12:19:46 +00001531 } else {
reed868074b2014-06-03 10:53:59 -07001532 return D1G_RgnClip;
reed@google.com045e62d2011-10-24 12:19:46 +00001533 }
1534 } else { // aaclip
1535 fAAClip = &draw->fRC->aaRgn();
1536 fClip = NULL;
1537 fClipBounds = fAAClip->getBounds();
reed868074b2014-06-03 10:53:59 -07001538 return D1G_RectClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001539 }
1540}
1541
reed@google.com5bdfb332013-05-02 18:55:44 +00001542void SkDraw1Glyph::blitMaskAsSprite(const SkMask& mask) const {
1543 SkASSERT(SkMask::kARGB32_Format == mask.fFormat);
1544
1545 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001546 bm.installPixels(SkImageInfo::MakeN32Premul(mask.fBounds.width(), mask.fBounds.height()),
1547 (SkPMColor*)mask.fImage, mask.fRowBytes);
reed@google.com5bdfb332013-05-02 18:55:44 +00001548
1549 fDraw->drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), *fPaint);
1550}
1551
reed@android.com8a1c16f2008-12-17 15:59:43 +00001552///////////////////////////////////////////////////////////////////////////////
1553
1554void SkDraw::drawText(const char text[], size_t byteLength,
1555 SkScalar x, SkScalar y, const SkPaint& paint) const {
1556 SkASSERT(byteLength == 0 || text != NULL);
1557
reed@android.comf2b98d62010-12-20 18:26:13 +00001558 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001559
1560 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001561 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001562 return;
1563 }
1564
bsalomon@google.com36d6eda2012-10-10 16:12:14 +00001565 // SkScalarRec doesn't currently have a way of representing hairline stroke and
1566 // will fill if its frame-width is 0.
reed@google.comed43dff2013-06-04 16:56:27 +00001567 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001568 this->drawText_asPaths(text, byteLength, x, y, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001569 return;
1570 }
1571
1572 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1573
bungeman@google.com94471032013-02-25 15:55:13 +00001574 SkAutoGlyphCache autoCache(paint, &fDevice->fLeakyProperties, fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001575 SkGlyphCache* cache = autoCache.getCache();
reed@google.coma76de3d2011-01-13 18:30:42 +00001576
reed@android.com8a1c16f2008-12-17 15:59:43 +00001577 // transform our starting point
1578 {
1579 SkPoint loc;
bungeman@google.com94471032013-02-25 15:55:13 +00001580 fMatrix->mapXY(x, y, &loc);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001581 x = loc.fX;
1582 y = loc.fY;
1583 }
1584
1585 // need to measure first
1586 if (paint.getTextAlign() != SkPaint::kLeft_Align) {
1587 SkVector stop;
1588
1589 measure_text(cache, glyphCacheProc, text, byteLength, &stop);
1590
1591 SkScalar stopX = stop.fX;
1592 SkScalar stopY = stop.fY;
1593
1594 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
1595 stopX = SkScalarHalf(stopX);
1596 stopY = SkScalarHalf(stopY);
1597 }
1598 x -= stopX;
1599 y -= stopY;
1600 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001601
reed@android.com8a1c16f2008-12-17 15:59:43 +00001602 const char* stop = text + byteLength;
1603
reed@google.com045e62d2011-10-24 12:19:46 +00001604 SkAAClipBlitter aaBlitter;
1605 SkAutoBlitterChoose blitterChooser;
1606 SkBlitter* blitter = NULL;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001607 if (needsRasterTextBlit(*this)) {
bungeman@google.com94471032013-02-25 15:55:13 +00001608 blitterChooser.choose(*fBitmap, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +00001609 blitter = blitterChooser.get();
1610 if (fRC->isAA()) {
1611 aaBlitter.init(blitter, &fRC->aaRgn());
1612 blitter = &aaBlitter;
1613 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001614 }
1615
reed@android.com8a1c16f2008-12-17 15:59:43 +00001616 SkAutoKern autokern;
bungeman@google.com52c748b2011-08-22 21:30:43 +00001617 SkDraw1Glyph d1g;
reed@google.com5bdfb332013-05-02 18:55:44 +00001618 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001619
bungeman@google.com94471032013-02-25 15:55:13 +00001620 SkFixed fxMask = ~0;
1621 SkFixed fyMask = ~0;
1622 if (cache->isSubpixel()) {
1623 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix);
1624 if (kX_SkAxisAlignment == baseline) {
1625 fyMask = 0;
1626 d1g.fHalfSampleY = SK_FixedHalf;
1627 } else if (kY_SkAxisAlignment == baseline) {
1628 fxMask = 0;
1629 d1g.fHalfSampleX = SK_FixedHalf;
1630 }
1631 }
1632
1633 SkFixed fx = SkScalarToFixed(x) + d1g.fHalfSampleX;
1634 SkFixed fy = SkScalarToFixed(y) + d1g.fHalfSampleY;
1635
reed@android.com8a1c16f2008-12-17 15:59:43 +00001636 while (text < stop) {
bungeman@google.com2211b622012-01-13 15:02:58 +00001637 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001638
1639 fx += autokern.adjust(glyph);
1640
1641 if (glyph.fWidth) {
bungeman@google.com52c748b2011-08-22 21:30:43 +00001642 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001643 }
jvanverth@google.comd830d132013-11-11 20:54:09 +00001644
reed@android.com8a1c16f2008-12-17 15:59:43 +00001645 fx += glyph.fAdvanceX;
1646 fy += glyph.fAdvanceY;
1647 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001648}
1649
reed@android.com8a1c16f2008-12-17 15:59:43 +00001650//////////////////////////////////////////////////////////////////////////////
1651
reed@google.comed43dff2013-06-04 16:56:27 +00001652void SkDraw::drawPosText_asPaths(const char text[], size_t byteLength,
1653 const SkScalar pos[], SkScalar constY,
1654 int scalarsPerPosition,
1655 const SkPaint& origPaint) const {
1656 // setup our std paint, in hopes of getting hits in the cache
1657 SkPaint paint(origPaint);
1658 SkScalar matrixScale = paint.setupForAsPaths();
1659
reed@google.com5a649022013-06-05 18:00:30 +00001660 SkMatrix matrix;
1661 matrix.setScale(matrixScale, matrixScale);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001662
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001663 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
1664 paint.setStyle(SkPaint::kFill_Style);
1665 paint.setPathEffect(NULL);
1666
reed@google.comed43dff2013-06-04 16:56:27 +00001667 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1668 SkAutoGlyphCache autoCache(paint, NULL, NULL);
1669 SkGlyphCache* cache = autoCache.getCache();
1670
1671 const char* stop = text + byteLength;
kkinnunencb9a2c82014-06-12 23:06:28 -07001672 SkTextAlignProcScalar alignProc(paint.getTextAlign());
1673 SkTextMapStateProc tmsProc(SkMatrix::I(), constY, scalarsPerPosition);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001674
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001675 // Now restore the original settings, so we "draw" with whatever style/stroking.
1676 paint.setStyle(origPaint.getStyle());
1677 paint.setPathEffect(origPaint.getPathEffect());
1678
reed@google.comed43dff2013-06-04 16:56:27 +00001679 while (text < stop) {
1680 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1681 if (glyph.fWidth) {
1682 const SkPath* path = cache->findPath(glyph);
1683 if (path) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001684 SkPoint tmsLoc;
1685 tmsProc(pos, &tmsLoc);
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001686 SkPoint loc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001687 alignProc(tmsLoc, glyph, &loc);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001688
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001689 matrix[SkMatrix::kMTransX] = loc.fX;
1690 matrix[SkMatrix::kMTransY] = loc.fY;
reed@google.com5a649022013-06-05 18:00:30 +00001691 if (fDevice) {
1692 fDevice->drawPath(*this, *path, paint, &matrix, false);
1693 } else {
1694 this->drawPath(*path, paint, &matrix, false);
1695 }
reed@google.comed43dff2013-06-04 16:56:27 +00001696 }
1697 }
1698 pos += scalarsPerPosition;
1699 }
1700}
1701
reed@android.com8a1c16f2008-12-17 15:59:43 +00001702void SkDraw::drawPosText(const char text[], size_t byteLength,
1703 const SkScalar pos[], SkScalar constY,
1704 int scalarsPerPosition, const SkPaint& paint) const {
1705 SkASSERT(byteLength == 0 || text != NULL);
1706 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1707
reed@android.comf2b98d62010-12-20 18:26:13 +00001708 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001709
1710 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001711 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001712 return;
1713 }
1714
reed@google.comed43dff2013-06-04 16:56:27 +00001715 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
1716 this->drawPosText_asPaths(text, byteLength, pos, constY,
1717 scalarsPerPosition, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001718 return;
1719 }
1720
1721 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
bungeman@google.com94471032013-02-25 15:55:13 +00001722 SkAutoGlyphCache autoCache(paint, &fDevice->fLeakyProperties, fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001723 SkGlyphCache* cache = autoCache.getCache();
reed@google.coma76de3d2011-01-13 18:30:42 +00001724
reed@google.com045e62d2011-10-24 12:19:46 +00001725 SkAAClipBlitterWrapper wrapper;
1726 SkAutoBlitterChoose blitterChooser;
1727 SkBlitter* blitter = NULL;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001728 if (needsRasterTextBlit(*this)) {
bungeman@google.com94471032013-02-25 15:55:13 +00001729 blitterChooser.choose(*fBitmap, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +00001730 blitter = blitterChooser.get();
1731 if (fRC->isAA()) {
1732 wrapper.init(*fRC, blitter);
1733 blitter = wrapper.getBlitter();
1734 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001735 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001736
reed@android.com8a1c16f2008-12-17 15:59:43 +00001737 const char* stop = text + byteLength;
kkinnunencb9a2c82014-06-12 23:06:28 -07001738 SkTextAlignProc alignProc(paint.getTextAlign());
bungeman@google.com2211b622012-01-13 15:02:58 +00001739 SkDraw1Glyph d1g;
reed@google.com5bdfb332013-05-02 18:55:44 +00001740 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
kkinnunencb9a2c82014-06-12 23:06:28 -07001741 SkTextMapStateProc tmsProc(*fMatrix, constY, scalarsPerPosition);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001742
bungeman@google.com2211b622012-01-13 15:02:58 +00001743 if (cache->isSubpixel()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001744 // maybe we should skip the rounding if linearText is set
bungeman@google.com94471032013-02-25 15:55:13 +00001745 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix);
1746
1747 SkFixed fxMask = ~0;
1748 SkFixed fyMask = ~0;
1749 if (kX_SkAxisAlignment == baseline) {
1750 fyMask = 0;
1751#ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX
1752 d1g.fHalfSampleY = SK_FixedHalf;
1753#endif
1754 } else if (kY_SkAxisAlignment == baseline) {
1755 fxMask = 0;
1756#ifndef SK_IGNORE_SUBPIXEL_AXIS_ALIGN_FIX
1757 d1g.fHalfSampleX = SK_FixedHalf;
1758#endif
1759 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001760
1761 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1762 while (text < stop) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001763 SkPoint tmsLoc;
1764 tmsProc(pos, &tmsLoc);
1765 SkFixed fx = SkScalarToFixed(tmsLoc.fX) + d1g.fHalfSampleX;
1766 SkFixed fy = SkScalarToFixed(tmsLoc.fY) + d1g.fHalfSampleY;
reed@google.coma76de3d2011-01-13 18:30:42 +00001767
reed@android.comf2b98d62010-12-20 18:26:13 +00001768 const SkGlyph& glyph = glyphCacheProc(cache, &text,
1769 fx & fxMask, fy & fyMask);
reed@google.coma76de3d2011-01-13 18:30:42 +00001770
reed@android.com8a1c16f2008-12-17 15:59:43 +00001771 if (glyph.fWidth) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001772 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001773 }
1774 pos += scalarsPerPosition;
1775 }
1776 } else {
1777 while (text < stop) {
bungeman@google.com9330cfe2012-01-04 14:17:00 +00001778 const char* currentText = text;
bungeman@google.com94471032013-02-25 15:55:13 +00001779 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001780
bungeman@google.com94471032013-02-25 15:55:13 +00001781 if (metricGlyph.fWidth) {
1782 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
1783 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
kkinnunencb9a2c82014-06-12 23:06:28 -07001784 SkPoint tmsLoc;
1785 tmsProc(pos, &tmsLoc);
bungeman@google.com94471032013-02-25 15:55:13 +00001786 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001787 alignProc(tmsLoc, metricGlyph, &fixedLoc);
reed@google.coma76de3d2011-01-13 18:30:42 +00001788
bungeman@google.com94471032013-02-25 15:55:13 +00001789 SkFixed fx = fixedLoc.fX + d1g.fHalfSampleX;
1790 SkFixed fy = fixedLoc.fY + d1g.fHalfSampleY;
reed@google.coma76de3d2011-01-13 18:30:42 +00001791
reed@android.com8a1c16f2008-12-17 15:59:43 +00001792 // have to call again, now that we've been "aligned"
bungeman@google.com94471032013-02-25 15:55:13 +00001793 const SkGlyph& glyph = glyphCacheProc(cache, &currentText,
1794 fx & fxMask, fy & fyMask);
1795 // the assumption is that the metrics haven't changed
1796 SkASSERT(prevAdvX == glyph.fAdvanceX);
1797 SkASSERT(prevAdvY == glyph.fAdvanceY);
1798 SkASSERT(glyph.fWidth);
reed@google.coma76de3d2011-01-13 18:30:42 +00001799
bungeman@google.com94471032013-02-25 15:55:13 +00001800 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001801 }
1802 pos += scalarsPerPosition;
1803 }
1804 }
1805 } else { // not subpixel
reed@google.comaeb07862012-04-18 18:32:04 +00001806 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1807 while (text < stop) {
1808 // the last 2 parameters are ignored
1809 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001810
reed@google.comaeb07862012-04-18 18:32:04 +00001811 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001812 SkPoint tmsLoc;
1813 tmsProc(pos, &tmsLoc);
reed@google.coma76de3d2011-01-13 18:30:42 +00001814
reed@google.comaeb07862012-04-18 18:32:04 +00001815 proc(d1g,
kkinnunencb9a2c82014-06-12 23:06:28 -07001816 SkScalarToFixed(tmsLoc.fX) + SK_FixedHalf, //d1g.fHalfSampleX,
1817 SkScalarToFixed(tmsLoc.fY) + SK_FixedHalf, //d1g.fHalfSampleY,
reed@google.comaeb07862012-04-18 18:32:04 +00001818 glyph);
1819 }
1820 pos += scalarsPerPosition;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001821 }
reed@google.comaeb07862012-04-18 18:32:04 +00001822 } else {
1823 while (text < stop) {
1824 // the last 2 parameters are ignored
1825 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1826
1827 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001828 SkPoint tmsLoc;
1829 tmsProc(pos, &tmsLoc);
reed@google.comaeb07862012-04-18 18:32:04 +00001830
1831 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001832 alignProc(tmsLoc, glyph, &fixedLoc);
reed@google.comaeb07862012-04-18 18:32:04 +00001833
1834 proc(d1g,
bungeman@google.com94471032013-02-25 15:55:13 +00001835 fixedLoc.fX + SK_FixedHalf, //d1g.fHalfSampleX,
1836 fixedLoc.fY + SK_FixedHalf, //d1g.fHalfSampleY,
reed@google.comaeb07862012-04-18 18:32:04 +00001837 glyph);
1838 }
1839 pos += scalarsPerPosition;
1840 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001841 }
1842 }
1843}
1844
1845#if defined _WIN32 && _MSC_VER >= 1300
1846#pragma warning ( pop )
1847#endif
1848
1849///////////////////////////////////////////////////////////////////////////////
1850
1851#include "SkPathMeasure.h"
1852
1853static void morphpoints(SkPoint dst[], const SkPoint src[], int count,
1854 SkPathMeasure& meas, const SkMatrix& matrix) {
1855 SkMatrix::MapXYProc proc = matrix.getMapXYProc();
1856
1857 for (int i = 0; i < count; i++) {
1858 SkPoint pos;
1859 SkVector tangent;
1860
1861 proc(matrix, src[i].fX, src[i].fY, &pos);
1862 SkScalar sx = pos.fX;
1863 SkScalar sy = pos.fY;
1864
reed@google.com8f17b0d2012-04-12 13:24:30 +00001865 if (!meas.getPosTan(sx, &pos, &tangent)) {
1866 // set to 0 if the measure failed, so that we just set dst == pos
1867 tangent.set(0, 0);
1868 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001869
1870 /* This is the old way (that explains our approach but is way too slow
1871 SkMatrix matrix;
1872 SkPoint pt;
1873
1874 pt.set(sx, sy);
1875 matrix.setSinCos(tangent.fY, tangent.fX);
1876 matrix.preTranslate(-sx, 0);
1877 matrix.postTranslate(pos.fX, pos.fY);
1878 matrix.mapPoints(&dst[i], &pt, 1);
1879 */
1880 dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy),
1881 pos.fY + SkScalarMul(tangent.fX, sy));
1882 }
1883}
1884
1885/* TODO
1886
1887 Need differentially more subdivisions when the follow-path is curvy. Not sure how to
1888 determine that, but we need it. I guess a cheap answer is let the caller tell us,
1889 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
1890*/
1891static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
1892 const SkMatrix& matrix) {
1893 SkPath::Iter iter(src, false);
1894 SkPoint srcP[4], dstP[3];
1895 SkPath::Verb verb;
1896
1897 while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
1898 switch (verb) {
1899 case SkPath::kMove_Verb:
1900 morphpoints(dstP, srcP, 1, meas, matrix);
1901 dst->moveTo(dstP[0]);
1902 break;
1903 case SkPath::kLine_Verb:
1904 // turn lines into quads to look bendy
1905 srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX);
1906 srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY);
1907 morphpoints(dstP, srcP, 2, meas, matrix);
1908 dst->quadTo(dstP[0], dstP[1]);
1909 break;
1910 case SkPath::kQuad_Verb:
1911 morphpoints(dstP, &srcP[1], 2, meas, matrix);
1912 dst->quadTo(dstP[0], dstP[1]);
1913 break;
1914 case SkPath::kCubic_Verb:
1915 morphpoints(dstP, &srcP[1], 3, meas, matrix);
1916 dst->cubicTo(dstP[0], dstP[1], dstP[2]);
1917 break;
1918 case SkPath::kClose_Verb:
1919 dst->close();
1920 break;
1921 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001922 SkDEBUGFAIL("unknown verb");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001923 break;
1924 }
1925 }
1926}
1927
1928void SkDraw::drawTextOnPath(const char text[], size_t byteLength,
1929 const SkPath& follow, const SkMatrix* matrix,
1930 const SkPaint& paint) const {
1931 SkASSERT(byteLength == 0 || text != NULL);
1932
1933 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001934 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001935 return;
1936 }
1937
djsollen@google.com166e6532012-03-20 14:24:38 +00001938 SkTextToPathIter iter(text, byteLength, paint, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001939 SkPathMeasure meas(follow, false);
1940 SkScalar hOffset = 0;
1941
1942 // need to measure first
1943 if (paint.getTextAlign() != SkPaint::kLeft_Align) {
1944 SkScalar pathLen = meas.getLength();
1945 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
1946 pathLen = SkScalarHalf(pathLen);
1947 }
1948 hOffset += pathLen;
1949 }
1950
1951 const SkPath* iterPath;
1952 SkScalar xpos;
1953 SkMatrix scaledMatrix;
1954 SkScalar scale = iter.getPathScale();
1955
1956 scaledMatrix.setScale(scale, scale);
reed@google.coma76de3d2011-01-13 18:30:42 +00001957
reed@google.com7b4531f2012-08-07 15:53:00 +00001958 while (iter.next(&iterPath, &xpos)) {
1959 if (iterPath) {
1960 SkPath tmp;
1961 SkMatrix m(scaledMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001962
reed@google.com7b4531f2012-08-07 15:53:00 +00001963 m.postTranslate(xpos + hOffset, 0);
1964 if (matrix) {
1965 m.postConcat(*matrix);
1966 }
1967 morphpath(&tmp, *iterPath, meas, m);
1968 if (fDevice) {
1969 fDevice->drawPath(*this, tmp, iter.getPaint(), NULL, true);
1970 } else {
1971 this->drawPath(tmp, iter.getPaint(), NULL, true);
1972 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001973 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001974 }
1975}
1976
1977///////////////////////////////////////////////////////////////////////////////
1978
reed@google.com045e62d2011-10-24 12:19:46 +00001979typedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRasterClip&,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001980 SkBlitter*);
1981
1982static HairProc ChooseHairProc(bool doAntiAlias) {
1983 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
1984}
1985
1986static bool texture_to_matrix(const VertState& state, const SkPoint verts[],
1987 const SkPoint texs[], SkMatrix* matrix) {
1988 SkPoint src[3], dst[3];
reed@google.coma76de3d2011-01-13 18:30:42 +00001989
reed@android.com8a1c16f2008-12-17 15:59:43 +00001990 src[0] = texs[state.f0];
1991 src[1] = texs[state.f1];
1992 src[2] = texs[state.f2];
1993 dst[0] = verts[state.f0];
1994 dst[1] = verts[state.f1];
1995 dst[2] = verts[state.f2];
1996 return matrix->setPolyToPoly(src, dst, 3);
1997}
1998
1999class SkTriColorShader : public SkShader {
2000public:
2001 SkTriColorShader() {}
2002
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002003 virtual size_t contextSize() const SK_OVERRIDE;
reed@google.coma76de3d2011-01-13 18:30:42 +00002004
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002005 class TriColorShaderContext : public SkShader::Context {
2006 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00002007 TriColorShaderContext(const SkTriColorShader& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002008 virtual ~TriColorShaderContext();
2009
2010 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
2011
2012 virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
2013
2014 private:
2015 SkMatrix fDstToUnit;
2016 SkPMColor fColors[3];
2017
2018 typedef SkShader::Context INHERITED;
2019 };
reed@google.coma76de3d2011-01-13 18:30:42 +00002020
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00002021 SK_TO_STRING_OVERRIDE()
reed9fa60da2014-08-21 07:59:51 -07002022 SK_DECLARE_NOT_FLATTENABLE_PROCS(SkTriColorShader)
djsollen@google.comba28d032012-03-26 17:57:35 +00002023
reed@android.com8a1c16f2008-12-17 15:59:43 +00002024protected:
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +00002025 virtual Context* onCreateContext(const ContextRec& rec, void* storage) const SK_OVERRIDE {
2026 return SkNEW_PLACEMENT_ARGS(storage, TriColorShaderContext, (*this, rec));
2027 }
2028
reed@android.com8a1c16f2008-12-17 15:59:43 +00002029private:
reed@android.com8a1c16f2008-12-17 15:59:43 +00002030 typedef SkShader INHERITED;
2031};
2032
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002033bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const SkColor colors[],
2034 int index0, int index1, int index2) {
reed@google.coma76de3d2011-01-13 18:30:42 +00002035
reed@android.com8a1c16f2008-12-17 15:59:43 +00002036 fColors[0] = SkPreMultiplyColor(colors[index0]);
2037 fColors[1] = SkPreMultiplyColor(colors[index1]);
2038 fColors[2] = SkPreMultiplyColor(colors[index2]);
reed@google.coma76de3d2011-01-13 18:30:42 +00002039
reed@android.com8a1c16f2008-12-17 15:59:43 +00002040 SkMatrix m, im;
2041 m.reset();
2042 m.set(0, pts[index1].fX - pts[index0].fX);
2043 m.set(1, pts[index2].fX - pts[index0].fX);
2044 m.set(2, pts[index0].fX);
2045 m.set(3, pts[index1].fY - pts[index0].fY);
2046 m.set(4, pts[index2].fY - pts[index0].fY);
2047 m.set(5, pts[index0].fY);
2048 if (!m.invert(&im)) {
2049 return false;
2050 }
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00002051 // We can't call getTotalInverse(), because we explicitly don't want to look at the localmatrix
2052 // as our interators are intrinsically tied to the vertices, and nothing else.
2053 SkMatrix ctmInv;
2054 if (!this->getCTM().invert(&ctmInv)) {
2055 return false;
2056 }
2057 fDstToUnit.setConcat(im, ctmInv);
commit-bot@chromium.org92362382014-03-18 12:51:48 +00002058 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002059}
2060
2061#include "SkColorPriv.h"
reed@android.com689411a2008-12-18 02:52:32 +00002062#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00002063
2064static int ScalarTo256(SkScalar v) {
2065 int scale = SkScalarToFixed(v) >> 8;
2066 if (scale < 0) {
2067 scale = 0;
2068 }
2069 if (scale > 255) {
2070 scale = 255;
2071 }
2072 return SkAlpha255To256(scale);
2073}
2074
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002075
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00002076SkTriColorShader::TriColorShaderContext::TriColorShaderContext(const SkTriColorShader& shader,
2077 const ContextRec& rec)
2078 : INHERITED(shader, rec) {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002079
2080SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {}
2081
2082size_t SkTriColorShader::contextSize() const {
2083 return sizeof(TriColorShaderContext);
2084}
2085void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00002086 const int alphaScale = Sk255To256(this->getPaintAlpha());
2087
reed@android.com8a1c16f2008-12-17 15:59:43 +00002088 SkPoint src;
reed@google.coma76de3d2011-01-13 18:30:42 +00002089
reed@android.com8a1c16f2008-12-17 15:59:43 +00002090 for (int i = 0; i < count; i++) {
2091 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
2092 x += 1;
reed@google.coma76de3d2011-01-13 18:30:42 +00002093
reed@android.com8a1c16f2008-12-17 15:59:43 +00002094 int scale1 = ScalarTo256(src.fX);
2095 int scale2 = ScalarTo256(src.fY);
2096 int scale0 = 256 - scale1 - scale2;
2097 if (scale0 < 0) {
2098 if (scale1 > scale2) {
2099 scale2 = 256 - scale1;
2100 } else {
2101 scale1 = 256 - scale2;
2102 }
2103 scale0 = 0;
2104 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002105
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00002106 if (256 != alphaScale) {
2107 scale0 = SkAlphaMul(scale0, alphaScale);
2108 scale1 = SkAlphaMul(scale1, alphaScale);
2109 scale2 = SkAlphaMul(scale2, alphaScale);
2110 }
2111
reed@android.com8a1c16f2008-12-17 15:59:43 +00002112 dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00002113 SkAlphaMulQ(fColors[1], scale1) +
2114 SkAlphaMulQ(fColors[2], scale2);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002115 }
2116}
2117
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00002118#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00002119void SkTriColorShader::toString(SkString* str) const {
2120 str->append("SkTriColorShader: (");
2121
2122 this->INHERITED::toString(str);
2123
2124 str->append(")");
2125}
2126#endif
2127
reed@android.com8a1c16f2008-12-17 15:59:43 +00002128void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
2129 const SkPoint vertices[], const SkPoint textures[],
2130 const SkColor colors[], SkXfermode* xmode,
2131 const uint16_t indices[], int indexCount,
2132 const SkPaint& paint) const {
2133 SkASSERT(0 == count || NULL != vertices);
reed@google.coma76de3d2011-01-13 18:30:42 +00002134
reed@android.com8a1c16f2008-12-17 15:59:43 +00002135 // abort early if there is nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00002136 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002137 return;
2138 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002139
reed@android.com8a1c16f2008-12-17 15:59:43 +00002140 // transform out vertices into device coordinates
2141 SkAutoSTMalloc<16, SkPoint> storage(count);
2142 SkPoint* devVerts = storage.get();
2143 fMatrix->mapPoints(devVerts, vertices, count);
reed@google.coma76de3d2011-01-13 18:30:42 +00002144
reed@android.com8a1c16f2008-12-17 15:59:43 +00002145 /*
2146 We can draw the vertices in 1 of 4 ways:
2147
2148 - solid color (no shader/texture[], no colors[])
2149 - just colors (no shader/texture[], has colors[])
2150 - just texture (has shader/texture[], no colors[])
2151 - colors * texture (has shader/texture[], has colors[])
reed@google.coma76de3d2011-01-13 18:30:42 +00002152
reed@android.com8a1c16f2008-12-17 15:59:43 +00002153 Thus for texture drawing, we need both texture[] and a shader.
2154 */
2155
2156 SkTriColorShader triShader; // must be above declaration of p
2157 SkPaint p(paint);
2158
2159 SkShader* shader = p.getShader();
2160 if (NULL == shader) {
2161 // if we have no shader, we ignore the texture coordinates
2162 textures = NULL;
2163 } else if (NULL == textures) {
2164 // if we don't have texture coordinates, ignore the shader
2165 p.setShader(NULL);
2166 shader = NULL;
2167 }
2168
2169 // setup the custom shader (if needed)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002170 SkAutoTUnref<SkComposeShader> composeShader;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002171 if (NULL != colors) {
2172 if (NULL == textures) {
2173 // just colors (no texture)
reed@google.coma641f3f2012-12-13 22:16:30 +00002174 shader = p.setShader(&triShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002175 } else {
2176 // colors * texture
2177 SkASSERT(shader);
2178 bool releaseMode = false;
2179 if (NULL == xmode) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00002180 xmode = SkXfermode::Create(SkXfermode::kModulate_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002181 releaseMode = true;
2182 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002183 composeShader.reset(SkNEW_ARGS(SkComposeShader, (&triShader, shader, xmode)));
2184 p.setShader(composeShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002185 if (releaseMode) {
2186 xmode->unref();
2187 }
2188 }
2189 }
2190
2191 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002192 // Abort early if we failed to create a shader context.
reed@google.comea033602012-12-14 13:13:55 +00002193 if (blitter->isNullBlitter()) {
2194 return;
2195 }
2196
reed@android.com8a1c16f2008-12-17 15:59:43 +00002197 // setup our state and function pointer for iterating triangles
2198 VertState state(count, indices, indexCount);
2199 VertState::Proc vertProc = state.chooseProc(vmode);
reed@google.coma76de3d2011-01-13 18:30:42 +00002200
reed@android.com8a1c16f2008-12-17 15:59:43 +00002201 if (NULL != textures || NULL != colors) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002202 while (vertProc(&state)) {
2203 if (NULL != textures) {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00002204 SkMatrix tempM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002205 if (texture_to_matrix(state, vertices, textures, &tempM)) {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00002206 SkShader::ContextRec rec(*fBitmap, p, *fMatrix);
2207 rec.fLocalMatrix = &tempM;
2208 if (!blitter->resetShaderContext(rec)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002209 continue;
2210 }
2211 }
2212 }
2213 if (NULL != colors) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002214 // Find the context for triShader.
2215 SkTriColorShader::TriColorShaderContext* triColorShaderContext;
2216
2217 SkShader::Context* shaderContext = blitter->getShaderContext();
2218 SkASSERT(shaderContext);
2219 if (p.getShader() == &triShader) {
2220 triColorShaderContext =
2221 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContext);
2222 } else {
2223 // The shader is a compose shader and triShader is its first shader.
2224 SkASSERT(p.getShader() == composeShader);
2225 SkASSERT(composeShader->getShaderA() == &triShader);
2226 SkComposeShader::ComposeShaderContext* composeShaderContext =
2227 static_cast<SkComposeShader::ComposeShaderContext*>(shaderContext);
2228 SkShader::Context* shaderContextA = composeShaderContext->getShaderContextA();
2229 triColorShaderContext =
2230 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContextA);
2231 }
2232
2233 if (!triColorShaderContext->setup(vertices, colors,
2234 state.f0, state.f1, state.f2)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002235 continue;
2236 }
2237 }
reed@google.com045e62d2011-10-24 12:19:46 +00002238
2239 SkPoint tmp[] = {
2240 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2241 };
2242 SkScan::FillTriangle(tmp, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002243 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002244 } else {
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00002245 // no colors[] and no texture, stroke hairlines with paint's color.
reed@android.com8a1c16f2008-12-17 15:59:43 +00002246 HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
reed@google.com045e62d2011-10-24 12:19:46 +00002247 const SkRasterClip& clip = *fRC;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002248 while (vertProc(&state)) {
reed@google.com045e62d2011-10-24 12:19:46 +00002249 hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get());
2250 hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get());
2251 hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002252 }
2253 }
2254}
2255
reed@google.com0a0a2362011-03-23 13:51:55 +00002256///////////////////////////////////////////////////////////////////////////////
2257///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00002258
2259#ifdef SK_DEBUG
2260
reed@android.comf2b98d62010-12-20 18:26:13 +00002261void SkDraw::validate() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002262 SkASSERT(fBitmap != NULL);
2263 SkASSERT(fMatrix != NULL);
2264 SkASSERT(fClip != NULL);
reed@google.com045e62d2011-10-24 12:19:46 +00002265 SkASSERT(fRC != NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002266
reed@google.com045e62d2011-10-24 12:19:46 +00002267 const SkIRect& cr = fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002268 SkIRect br;
2269
reed@android.comf2b98d62010-12-20 18:26:13 +00002270 br.set(0, 0, fBitmap->width(), fBitmap->height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002271 SkASSERT(cr.isEmpty() || br.contains(cr));
2272}
2273
2274#endif
2275
reed@android.com8a1c16f2008-12-17 15:59:43 +00002276////////////////////////////////////////////////////////////////////////////////////////////////
2277
2278#include "SkPath.h"
2279#include "SkDraw.h"
2280#include "SkRegion.h"
2281#include "SkBlitter.h"
2282
2283static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
reed@google.com30711b72012-12-18 19:18:39 +00002284 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
reed@android.com8a1c16f2008-12-17 15:59:43 +00002285 SkIRect* bounds) {
2286 if (devPath.isEmpty()) {
2287 return false;
2288 }
2289
reed@android.com8a1c16f2008-12-17 15:59:43 +00002290 // init our bounds from the path
2291 {
reed@android.comd252db02009-04-01 18:31:44 +00002292 SkRect pathBounds = devPath.getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002293 pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf);
2294 pathBounds.roundOut(bounds);
2295 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002296
tomhudson@google.com6db75fc2012-03-23 14:46:48 +00002297 SkIPoint margin = SkIPoint::Make(0, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002298 if (filter) {
2299 SkASSERT(filterMatrix);
reed@google.coma76de3d2011-01-13 18:30:42 +00002300
bungeman@google.com5af16f82011-09-02 15:06:44 +00002301 SkMask srcM, dstM;
reed@google.coma76de3d2011-01-13 18:30:42 +00002302
reed@android.com8a1c16f2008-12-17 15:59:43 +00002303 srcM.fBounds = *bounds;
2304 srcM.fFormat = SkMask::kA8_Format;
2305 srcM.fImage = NULL;
2306 if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
2307 return false;
2308 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002309 }
2310
bungeman@google.com5af16f82011-09-02 15:06:44 +00002311 // (possibly) trim the bounds to reflect the clip
reed@android.com8a1c16f2008-12-17 15:59:43 +00002312 // (plus whatever slop the filter needs)
bungeman@google.com5af16f82011-09-02 15:06:44 +00002313 if (clipBounds) {
2314 SkIRect tmp = *clipBounds;
reed@android.com35555912009-03-16 18:46:55 +00002315 // Ugh. Guard against gigantic margins from wacky filters. Without this
2316 // check we can request arbitrary amounts of slop beyond our visible
2317 // clip, and bring down the renderer (at least on finite RAM machines
2318 // like handsets, etc.). Need to balance this invented value between
2319 // quality of large filters like blurs, and the corresponding memory
2320 // requests.
2321 static const int MAX_MARGIN = 128;
2322 tmp.inset(-SkMin32(margin.fX, MAX_MARGIN),
2323 -SkMin32(margin.fY, MAX_MARGIN));
bungeman@google.com5af16f82011-09-02 15:06:44 +00002324 if (!bounds->intersect(tmp)) {
2325 return false;
2326 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002327 }
2328
2329 return true;
2330}
2331
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002332static void draw_into_mask(const SkMask& mask, const SkPath& devPath,
2333 SkPaint::Style style) {
reed@google.com045e62d2011-10-24 12:19:46 +00002334 SkBitmap bm;
2335 SkDraw draw;
2336 SkRasterClip clip;
2337 SkMatrix matrix;
2338 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002339
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00002340 bm.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
2341 mask.fImage, mask.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002342
reed@google.com045e62d2011-10-24 12:19:46 +00002343 clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00002344 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
2345 -SkIntToScalar(mask.fBounds.fTop));
2346
2347 draw.fBitmap = &bm;
reed@google.com045e62d2011-10-24 12:19:46 +00002348 draw.fRC = &clip;
2349 draw.fClip = &clip.bwRgn();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002350 draw.fMatrix = &matrix;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002351 paint.setAntiAlias(true);
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002352 paint.setStyle(style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002353 draw.drawPath(devPath, paint);
2354}
2355
2356bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
reed@google.com30711b72012-12-18 19:18:39 +00002357 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002358 SkMask* mask, SkMask::CreateMode mode,
2359 SkPaint::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002360 if (SkMask::kJustRenderImage_CreateMode != mode) {
2361 if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
2362 return false;
2363 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002364
reed@android.com8a1c16f2008-12-17 15:59:43 +00002365 if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
2366 mask->fFormat = SkMask::kA8_Format;
2367 mask->fRowBytes = mask->fBounds.width();
reed@android.com543ed932009-04-24 12:43:40 +00002368 size_t size = mask->computeImageSize();
2369 if (0 == size) {
2370 // we're too big to allocate the mask, abort
2371 return false;
2372 }
2373 mask->fImage = SkMask::AllocImage(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002374 memset(mask->fImage, 0, mask->computeImageSize());
2375 }
2376
2377 if (SkMask::kJustComputeBounds_CreateMode != mode) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002378 draw_into_mask(*mask, devPath, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002379 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002380
reed@android.com8a1c16f2008-12-17 15:59:43 +00002381 return true;
2382}