blob: f40c1bbcb0ea94f3a402b7f51e2a98a07bf3d36e [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 */ {
mtklein7ef849d2014-11-24 09:11:45 -080080 fPaint.setShader(SkCreateBitmapShader(src, SkShader::kClamp_TileMode,
81 SkShader::kClamp_TileMode,
82 localMatrix, &fAllocator));
reed@google.com40c2ba22011-07-25 19:41:22 +000083 // we deliberately left the shader with an owner-count of 2
84 SkASSERT(2 == fPaint.getShader()->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 }
reed@google.com82065d62011-02-07 15:30:46 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 ~SkAutoBitmapShaderInstall() {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000088 // since fAllocator will destroy shader, we insist that owners == 2
89 SkASSERT(2 == fPaint.getShader()->getRefCnt());
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
reed@google.com40c2ba22011-07-25 19:41:22 +000091 fPaint.setShader(NULL); // unref the shader by 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 }
reed@google.com82065d62011-02-07 15:30:46 +000094
reed@google.com40c2ba22011-07-25 19:41:22 +000095 // return the new paint that has the shader applied
96 const SkPaint& paintWithShader() const { return fPaint; }
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098private:
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000099 // copy of caller's paint (which we then modify)
100 SkPaint fPaint;
101 // Stores the shader.
102 SkTBlitterAllocator fAllocator;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000104#define SkAutoBitmapShaderInstall(...) SK_REQUIRE_LOCAL_VAR(SkAutoBitmapShaderInstall)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106///////////////////////////////////////////////////////////////////////////////
107
reed@android.comf2b98d62010-12-20 18:26:13 +0000108SkDraw::SkDraw() {
109 sk_bzero(this, sizeof(*this));
110}
111
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112SkDraw::SkDraw(const SkDraw& src) {
113 memcpy(this, &src, sizeof(*this));
114}
115
reed@google.com4bbdeac2013-01-24 21:03:11 +0000116bool SkDraw::computeConservativeLocalClipBounds(SkRect* localBounds) const {
117 if (fRC->isEmpty()) {
118 return false;
119 }
120
121 SkMatrix inverse;
122 if (!fMatrix->invert(&inverse)) {
123 return false;
124 }
125
126 SkIRect devBounds = fRC->getBounds();
127 // outset to have slop for antialasing and hairlines
128 devBounds.outset(1, 1);
129 inverse.mapRect(localBounds, SkRect::Make(devBounds));
130 return true;
131}
132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133///////////////////////////////////////////////////////////////////////////////
134
135typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data);
136
137static void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) {
reed@android.com4516f472009-06-29 16:25:36 +0000138 sk_bzero(pixels, bytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139}
140
141static void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {}
142
143static void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000144 sk_memset32((uint32_t*)pixels, data, SkToInt(bytes >> 2));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145}
146
147static void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000148 sk_memset16((uint16_t*)pixels, data, SkToInt(bytes >> 1));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149}
150
151static void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) {
152 memset(pixels, data, bytes);
153}
154
155static BitmapXferProc ChooseBitmapXferProc(const SkBitmap& bitmap,
156 const SkPaint& paint,
157 uint32_t* data) {
158 // todo: we can apply colorfilter up front if no shader, so we wouldn't
159 // need to abort this fastpath
160 if (paint.getShader() || paint.getColorFilter()) {
161 return NULL;
162 }
163
reed@android.com845fdac2009-06-23 03:01:32 +0000164 SkXfermode::Mode mode;
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +0000165 if (!SkXfermode::AsMode(paint.getXfermode(), &mode)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 return NULL;
167 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000168
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 SkColor color = paint.getColor();
reed@google.coma76de3d2011-01-13 18:30:42 +0000170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 // collaps modes based on color...
reed@android.com845fdac2009-06-23 03:01:32 +0000172 if (SkXfermode::kSrcOver_Mode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 unsigned alpha = SkColorGetA(color);
174 if (0 == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000175 mode = SkXfermode::kDst_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 } else if (0xFF == alpha) {
reed@android.com845fdac2009-06-23 03:01:32 +0000177 mode = SkXfermode::kSrc_Mode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 }
179 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000180
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 switch (mode) {
reed@android.com845fdac2009-06-23 03:01:32 +0000182 case SkXfermode::kClear_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183// SkDebugf("--- D_Clear_BitmapXferProc\n");
184 return D_Clear_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000185 case SkXfermode::kDst_Mode:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186// SkDebugf("--- D_Dst_BitmapXferProc\n");
187 return D_Dst_BitmapXferProc; // ignore data
reed@android.com845fdac2009-06-23 03:01:32 +0000188 case SkXfermode::kSrc_Mode: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 /*
reed@google.coma76de3d2011-01-13 18:30:42 +0000190 should I worry about dithering for the lower depths?
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 */
192 SkPMColor pmc = SkPreMultiplyColor(color);
reed@google.com900ecf22014-02-20 20:55:37 +0000193 switch (bitmap.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000194 case kN32_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 if (data) {
196 *data = pmc;
197 }
198// SkDebugf("--- D32_Src_BitmapXferProc\n");
199 return D32_Src_BitmapXferProc;
reed@google.com900ecf22014-02-20 20:55:37 +0000200 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 if (data) {
202 *data = SkPixel32ToPixel16(pmc);
203 }
204// SkDebugf("--- D16_Src_BitmapXferProc\n");
205 return D16_Src_BitmapXferProc;
reed@google.com900ecf22014-02-20 20:55:37 +0000206 case kAlpha_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 if (data) {
208 *data = SkGetPackedA32(pmc);
209 }
210// SkDebugf("--- DA8_Src_BitmapXferProc\n");
211 return DA8_Src_BitmapXferProc;
212 default:
213 break;
214 }
215 break;
216 }
217 default:
218 break;
219 }
220 return NULL;
221}
222
223static void CallBitmapXferProc(const SkBitmap& bitmap, const SkIRect& rect,
224 BitmapXferProc proc, uint32_t procData) {
225 int shiftPerPixel;
reed@google.com900ecf22014-02-20 20:55:37 +0000226 switch (bitmap.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000227 case kN32_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 shiftPerPixel = 2;
229 break;
reed@google.com900ecf22014-02-20 20:55:37 +0000230 case kRGB_565_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 shiftPerPixel = 1;
232 break;
reed@google.com900ecf22014-02-20 20:55:37 +0000233 case kAlpha_8_SkColorType:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 shiftPerPixel = 0;
235 break;
236 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000237 SkDEBUGFAIL("Can't use xferproc on this config");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 return;
239 }
240
241 uint8_t* pixels = (uint8_t*)bitmap.getPixels();
242 SkASSERT(pixels);
243 const size_t rowBytes = bitmap.rowBytes();
244 const int widthBytes = rect.width() << shiftPerPixel;
245
246 // skip down to the first scanline and X position
247 pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel);
248 for (int scans = rect.height() - 1; scans >= 0; --scans) {
249 proc(pixels, widthBytes, procData);
250 pixels += rowBytes;
251 }
252}
253
254void SkDraw::drawPaint(const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000255 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256
reed@google.com045e62d2011-10-24 12:19:46 +0000257 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 return;
259 }
260
261 SkIRect devRect;
262 devRect.set(0, 0, fBitmap->width(), fBitmap->height());
reed@google.coma76de3d2011-01-13 18:30:42 +0000263
reed@google.com045e62d2011-10-24 12:19:46 +0000264 if (fRC->isBW()) {
265 /* If we don't have a shader (i.e. we're just a solid color) we may
266 be faster to operate directly on the device bitmap, rather than invoking
267 a blitter. Esp. true for xfermodes, which require a colorshader to be
268 present, which is just redundant work. Since we're drawing everywhere
269 in the clip, we don't have to worry about antialiasing.
270 */
271 uint32_t procData = 0; // to avoid the warning
272 BitmapXferProc proc = ChooseBitmapXferProc(*fBitmap, paint, &procData);
273 if (proc) {
274 if (D_Dst_BitmapXferProc == proc) { // nothing to do
275 return;
276 }
277
278 SkRegion::Iterator iter(fRC->bwRgn());
279 while (!iter.done()) {
280 CallBitmapXferProc(*fBitmap, iter.rect(), proc, procData);
281 iter.next();
282 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 return;
284 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 }
reed@google.com045e62d2011-10-24 12:19:46 +0000286
287 // normal case: use a blitter
288 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
289 SkScan::FillIRect(devRect, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290}
291
292///////////////////////////////////////////////////////////////////////////////
293
294struct PtProcRec {
295 SkCanvas::PointMode fMode;
296 const SkPaint* fPaint;
297 const SkRegion* fClip;
reed@google.com045e62d2011-10-24 12:19:46 +0000298 const SkRasterClip* fRC;
reed@google.coma76de3d2011-01-13 18:30:42 +0000299
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 // computed values
301 SkFixed fRadius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000302
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count,
304 SkBlitter*);
305
306 bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
reed@google.com045e62d2011-10-24 12:19:46 +0000307 const SkRasterClip*);
308 Proc chooseProc(SkBlitter** blitter);
309
310private:
311 SkAAClipBlitterWrapper fWrapper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312};
313
314static void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
315 int count, SkBlitter* blitter) {
316 SkASSERT(rec.fClip->isRect());
317 const SkIRect& r = rec.fClip->getBounds();
reed@google.coma76de3d2011-01-13 18:30:42 +0000318
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 for (int i = 0; i < count; i++) {
reed@google.com2d47a212012-11-29 21:01:00 +0000320 int x = SkScalarFloorToInt(devPts[i].fX);
321 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 if (r.contains(x, y)) {
323 blitter->blitH(x, y, 1);
324 }
325 }
326}
327
328static void bw_pt_rect_16_hair_proc(const PtProcRec& rec,
329 const SkPoint devPts[], int count,
330 SkBlitter* blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +0000331 SkASSERT(rec.fRC->isRect());
332 const SkIRect& r = rec.fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 uint32_t value;
334 const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
335 SkASSERT(bitmap);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000336
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337 uint16_t* addr = bitmap->getAddr16(0, 0);
scroggo@google.come5f48242013-02-25 21:47:41 +0000338 size_t rb = bitmap->rowBytes();
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000339
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 for (int i = 0; i < count; i++) {
reed@google.com2d47a212012-11-29 21:01:00 +0000341 int x = SkScalarFloorToInt(devPts[i].fX);
342 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 if (r.contains(x, y)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value);
345 }
346 }
347}
348
reed@google.com2d47a212012-11-29 21:01:00 +0000349static void bw_pt_rect_32_hair_proc(const PtProcRec& rec,
350 const SkPoint devPts[], int count,
351 SkBlitter* blitter) {
352 SkASSERT(rec.fRC->isRect());
353 const SkIRect& r = rec.fRC->getBounds();
354 uint32_t value;
355 const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value);
356 SkASSERT(bitmap);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000357
reed@google.com2d47a212012-11-29 21:01:00 +0000358 SkPMColor* addr = bitmap->getAddr32(0, 0);
scroggo@google.come5f48242013-02-25 21:47:41 +0000359 size_t rb = bitmap->rowBytes();
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000360
reed@google.com2d47a212012-11-29 21:01:00 +0000361 for (int i = 0; i < count; i++) {
362 int x = SkScalarFloorToInt(devPts[i].fX);
363 int y = SkScalarFloorToInt(devPts[i].fY);
364 if (r.contains(x, y)) {
365 ((SkPMColor*)((char*)addr + y * rb))[x] = value;
366 }
367 }
368}
369
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370static void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
371 int count, SkBlitter* blitter) {
372 for (int i = 0; i < count; i++) {
reed@google.come1ca7052013-12-17 19:22:07 +0000373 int x = SkScalarFloorToInt(devPts[i].fX);
374 int y = SkScalarFloorToInt(devPts[i].fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375 if (rec.fClip->contains(x, y)) {
376 blitter->blitH(x, y, 1);
377 }
378 }
379}
380
381static void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
382 int count, SkBlitter* blitter) {
383 for (int i = 0; i < count; i += 2) {
reed@google.com045e62d2011-10-24 12:19:46 +0000384 SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 }
386}
387
388static void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
389 int count, SkBlitter* blitter) {
390 for (int i = 0; i < count - 1; i++) {
reed@google.com045e62d2011-10-24 12:19:46 +0000391 SkScan::HairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392 }
393}
394
395// aa versions
396
397static void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
398 int count, SkBlitter* blitter) {
399 for (int i = 0; i < count; i += 2) {
reed@google.com045e62d2011-10-24 12:19:46 +0000400 SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 }
402}
403
404static void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[],
405 int count, SkBlitter* blitter) {
406 for (int i = 0; i < count - 1; i++) {
reed@google.com045e62d2011-10-24 12:19:46 +0000407 SkScan::AntiHairLine(devPts[i], devPts[i+1], *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000408 }
409}
410
411// square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
412
413static void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[],
414 int count, SkBlitter* blitter) {
415 const SkFixed radius = rec.fRadius;
416 for (int i = 0; i < count; i++) {
417 SkFixed x = SkScalarToFixed(devPts[i].fX);
418 SkFixed y = SkScalarToFixed(devPts[i].fY);
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000419
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 SkXRect r;
421 r.fLeft = x - radius;
422 r.fTop = y - radius;
423 r.fRight = x + radius;
424 r.fBottom = y + radius;
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000425
reed@google.com045e62d2011-10-24 12:19:46 +0000426 SkScan::FillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427 }
428}
429
430static void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[],
431 int count, SkBlitter* blitter) {
432 const SkFixed radius = rec.fRadius;
433 for (int i = 0; i < count; i++) {
434 SkFixed x = SkScalarToFixed(devPts[i].fX);
435 SkFixed y = SkScalarToFixed(devPts[i].fY);
reed@google.coma76de3d2011-01-13 18:30:42 +0000436
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437 SkXRect r;
438 r.fLeft = x - radius;
439 r.fTop = y - radius;
440 r.fRight = x + radius;
441 r.fBottom = y + radius;
reed@google.coma76de3d2011-01-13 18:30:42 +0000442
reed@google.com045e62d2011-10-24 12:19:46 +0000443 SkScan::AntiFillXRect(r, *rec.fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 }
445}
446
reed@android.comb4f404a2009-07-10 17:02:17 +0000447// If this guy returns true, then chooseProc() must return a valid proc
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
reed@google.com045e62d2011-10-24 12:19:46 +0000449 const SkMatrix* matrix, const SkRasterClip* rc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450 if (paint.getPathEffect()) {
451 return false;
452 }
453 SkScalar width = paint.getStrokeWidth();
454 if (0 == width) {
455 fMode = mode;
456 fPaint = &paint;
reed@google.com045e62d2011-10-24 12:19:46 +0000457 fClip = NULL;
458 fRC = rc;
reed@google.com2d47a212012-11-29 21:01:00 +0000459 fRadius = SK_FixedHalf;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 return true;
461 }
reed@android.comb4f404a2009-07-10 17:02:17 +0000462 if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
robertphillips9f2251c2014-11-04 13:33:50 -0800463 matrix->isScaleTranslate() && SkCanvas::kPoints_PointMode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464 SkScalar sx = matrix->get(SkMatrix::kMScaleX);
465 SkScalar sy = matrix->get(SkMatrix::kMScaleY);
466 if (SkScalarNearlyZero(sx - sy)) {
467 if (sx < 0) {
468 sx = -sx;
469 }
470
471 fMode = mode;
472 fPaint = &paint;
reed@google.com045e62d2011-10-24 12:19:46 +0000473 fClip = NULL;
474 fRC = rc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475 fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1;
476 return true;
477 }
478 }
479 return false;
480}
481
reed@google.com045e62d2011-10-24 12:19:46 +0000482PtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
reed@android.comb4f404a2009-07-10 17:02:17 +0000483 Proc proc = NULL;
reed@google.coma76de3d2011-01-13 18:30:42 +0000484
reed@google.com045e62d2011-10-24 12:19:46 +0000485 SkBlitter* blitter = *blitterPtr;
486 if (fRC->isBW()) {
487 fClip = &fRC->bwRgn();
488 } else {
489 fWrapper.init(*fRC, blitter);
490 fClip = &fWrapper.getRgn();
491 blitter = fWrapper.getBlitter();
492 *blitterPtr = blitter;
493 }
494
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495 // for our arrays
496 SkASSERT(0 == SkCanvas::kPoints_PointMode);
497 SkASSERT(1 == SkCanvas::kLines_PointMode);
498 SkASSERT(2 == SkCanvas::kPolygon_PointMode);
499 SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
500
reed@google.com2d47a212012-11-29 21:01:00 +0000501 if (fPaint->isAntiAlias()) {
502 if (0 == fPaint->getStrokeWidth()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503 static const Proc gAAProcs[] = {
504 aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
505 };
506 proc = gAAProcs[fMode];
reed@google.com2d47a212012-11-29 21:01:00 +0000507 } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
508 SkASSERT(SkCanvas::kPoints_PointMode == fMode);
509 proc = aa_square_proc;
510 }
511 } else { // BW
512 if (fRadius <= SK_FixedHalf) { // small radii and hairline
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) {
514 uint32_t value;
515 const SkBitmap* bm = blitter->justAnOpaqueColor(&value);
reed@google.com900ecf22014-02-20 20:55:37 +0000516 if (bm && kRGB_565_SkColorType == bm->colorType()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517 proc = bw_pt_rect_16_hair_proc;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000518 } else if (bm && kN32_SkColorType == bm->colorType()) {
reed@google.com2d47a212012-11-29 21:01:00 +0000519 proc = bw_pt_rect_32_hair_proc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 } else {
521 proc = bw_pt_rect_hair_proc;
522 }
523 } else {
524 static Proc gBWProcs[] = {
525 bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
526 };
527 proc = gBWProcs[fMode];
528 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529 } else {
530 proc = bw_square_proc;
531 }
532 }
533 return proc;
534}
535
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536// each of these costs 8-bytes of stack space, so don't make it too large
537// must be even for lines/polygon to work
538#define MAX_DEV_PTS 32
539
540void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count,
reed@android.comf2b98d62010-12-20 18:26:13 +0000541 const SkPoint pts[], const SkPaint& paint,
542 bool forceUseDevice) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000543 // if we're in lines mode, force count to be even
544 if (SkCanvas::kLines_PointMode == mode) {
545 count &= ~(size_t)1;
546 }
547
548 if ((long)count <= 0) {
549 return;
550 }
reed@google.coma76de3d2011-01-13 18:30:42 +0000551
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552 SkASSERT(pts != NULL);
reed@android.comf2b98d62010-12-20 18:26:13 +0000553 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +0000554
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000556 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557 return;
558 }
559
560 PtProcRec rec;
reed@google.com045e62d2011-10-24 12:19:46 +0000561 if (!forceUseDevice && rec.init(mode, paint, fMatrix, fRC)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000562 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
563
564 SkPoint devPts[MAX_DEV_PTS];
565 const SkMatrix* matrix = fMatrix;
566 SkBlitter* bltr = blitter.get();
reed@google.com045e62d2011-10-24 12:19:46 +0000567 PtProcRec::Proc proc = rec.chooseProc(&bltr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568 // we have to back up subsequent passes if we're in polygon mode
569 const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
reed@google.coma76de3d2011-01-13 18:30:42 +0000570
reed@android.com8a1c16f2008-12-17 15:59:43 +0000571 do {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000572 int n = SkToInt(count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573 if (n > MAX_DEV_PTS) {
574 n = MAX_DEV_PTS;
575 }
576 matrix->mapPoints(devPts, pts, n);
577 proc(rec, devPts, n, bltr);
578 pts += n - backup;
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000579 SkASSERT(SkToInt(count) >= n);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000580 count -= n;
581 if (count > 0) {
582 count += backup;
583 }
584 } while (count != 0);
585 } else {
586 switch (mode) {
587 case SkCanvas::kPoints_PointMode: {
588 // temporarily mark the paint as filling.
reed@google.com40c2ba22011-07-25 19:41:22 +0000589 SkPaint newPaint(paint);
590 newPaint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591
reed@google.com40c2ba22011-07-25 19:41:22 +0000592 SkScalar width = newPaint.getStrokeWidth();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000593 SkScalar radius = SkScalarHalf(width);
reed@google.coma76de3d2011-01-13 18:30:42 +0000594
reed@google.com40c2ba22011-07-25 19:41:22 +0000595 if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000596 SkPath path;
597 SkMatrix preMatrix;
mtklein7ef849d2014-11-24 09:11:45 -0800598
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599 path.addCircle(0, 0, radius);
600 for (size_t i = 0; i < count; i++) {
601 preMatrix.setTranslate(pts[i].fX, pts[i].fY);
602 // pass true for the last point, since we can modify
603 // then path then
jvanverthb3eb6872014-10-24 07:12:51 -0700604 path.setIsVolatile((count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000605 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000606 fDevice->drawPath(*this, path, newPaint, &preMatrix,
reed@android.comf2b98d62010-12-20 18:26:13 +0000607 (count-1) == i);
608 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000609 this->drawPath(path, newPaint, &preMatrix,
610 (count-1) == i);
reed@android.comf2b98d62010-12-20 18:26:13 +0000611 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000612 }
613 } else {
614 SkRect r;
reed@google.coma76de3d2011-01-13 18:30:42 +0000615
reed@android.com8a1c16f2008-12-17 15:59:43 +0000616 for (size_t i = 0; i < count; i++) {
617 r.fLeft = pts[i].fX - radius;
618 r.fTop = pts[i].fY - radius;
619 r.fRight = r.fLeft + width;
620 r.fBottom = r.fTop + width;
reed@android.comf2b98d62010-12-20 18:26:13 +0000621 if (fDevice) {
reed@google.com40c2ba22011-07-25 19:41:22 +0000622 fDevice->drawRect(*this, r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000623 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +0000624 this->drawRect(r, newPaint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000625 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000626 }
627 }
628 break;
629 }
630 case SkCanvas::kLines_PointMode:
bsalomon49f085d2014-09-05 13:34:00 -0700631 if (2 == count && paint.getPathEffect()) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000632 // most likely a dashed line - see if it is one of the ones
633 // we can accelerate
634 SkStrokeRec rec(paint);
robertphillips@google.com6d875572012-12-17 18:56:29 +0000635 SkPathEffect::PointData pointData;
robertphillips@google.com629ab542012-11-28 17:18:11 +0000636
637 SkPath path;
638 path.moveTo(pts[0]);
639 path.lineTo(pts[1]);
640
reed@google.com4bbdeac2013-01-24 21:03:11 +0000641 SkRect cullRect = SkRect::Make(fRC->getBounds());
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000642
reed@google.com4bbdeac2013-01-24 21:03:11 +0000643 if (paint.getPathEffect()->asPoints(&pointData, path, rec,
644 *fMatrix, &cullRect)) {
robertphillips@google.com6d875572012-12-17 18:56:29 +0000645 // 'asPoints' managed to find some fast path
646
robertphillips@google.com629ab542012-11-28 17:18:11 +0000647 SkPaint newP(paint);
648 newP.setPathEffect(NULL);
robertphillips@google.com935ad022012-12-05 19:07:21 +0000649 newP.setStyle(SkPaint::kFill_Style);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000650
robertphillips@google.com6d875572012-12-17 18:56:29 +0000651 if (!pointData.fFirst.isEmpty()) {
652 if (fDevice) {
653 fDevice->drawPath(*this, pointData.fFirst, newP);
654 } else {
655 this->drawPath(pointData.fFirst, newP);
656 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000657 }
robertphillips@google.com6d875572012-12-17 18:56:29 +0000658
659 if (!pointData.fLast.isEmpty()) {
660 if (fDevice) {
661 fDevice->drawPath(*this, pointData.fLast, newP);
662 } else {
663 this->drawPath(pointData.fLast, newP);
664 }
robertphillips@google.com935ad022012-12-05 19:07:21 +0000665 }
robertphillips@google.com6d875572012-12-17 18:56:29 +0000666
667 if (pointData.fSize.fX == pointData.fSize.fY) {
668 // The rest of the dashed line can just be drawn as points
669 SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth()));
670
671 if (SkPathEffect::PointData::kCircles_PointFlag & pointData.fFlags) {
672 newP.setStrokeCap(SkPaint::kRound_Cap);
673 } else {
674 newP.setStrokeCap(SkPaint::kButt_Cap);
675 }
676
677 if (fDevice) {
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000678 fDevice->drawPoints(*this,
robertphillips@google.com6d875572012-12-17 18:56:29 +0000679 SkCanvas::kPoints_PointMode,
680 pointData.fNumPoints,
681 pointData.fPoints,
682 newP);
683 } else {
684 this->drawPoints(SkCanvas::kPoints_PointMode,
685 pointData.fNumPoints,
686 pointData.fPoints,
687 newP,
688 forceUseDevice);
689 }
690 break;
691 } else {
692 // The rest of the dashed line must be drawn as rects
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000693 SkASSERT(!(SkPathEffect::PointData::kCircles_PointFlag &
robertphillips@google.com6d875572012-12-17 18:56:29 +0000694 pointData.fFlags));
695
696 SkRect r;
697
698 for (int i = 0; i < pointData.fNumPoints; ++i) {
699 r.set(pointData.fPoints[i].fX - pointData.fSize.fX,
700 pointData.fPoints[i].fY - pointData.fSize.fY,
701 pointData.fPoints[i].fX + pointData.fSize.fX,
702 pointData.fPoints[i].fY + pointData.fSize.fY);
703 if (fDevice) {
704 fDevice->drawRect(*this, r, newP);
705 } else {
706 this->drawRect(r, newP);
707 }
708 }
709 }
710
robertphillips@google.com629ab542012-11-28 17:18:11 +0000711 break;
712 }
713 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000714 // couldn't take fast path so fall through!
reed@android.com8a1c16f2008-12-17 15:59:43 +0000715 case SkCanvas::kPolygon_PointMode: {
716 count -= 1;
717 SkPath path;
718 SkPaint p(paint);
719 p.setStyle(SkPaint::kStroke_Style);
720 size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
jvanverthb3eb6872014-10-24 07:12:51 -0700721 path.setIsVolatile(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000722 for (size_t i = 0; i < count; i += inc) {
723 path.moveTo(pts[i]);
724 path.lineTo(pts[i+1]);
reed@android.comf2b98d62010-12-20 18:26:13 +0000725 if (fDevice) {
726 fDevice->drawPath(*this, path, p, NULL, true);
727 } else {
728 this->drawPath(path, p, NULL, true);
729 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000730 path.rewind();
731 }
732 break;
733 }
734 }
735 }
736}
737
fmalita1a178ca2015-01-15 06:01:23 -0800738static inline SkPoint compute_stroke_size(const SkPaint& paint, const SkMatrix& matrix) {
739 SkASSERT(matrix.rectStaysRect());
740 SkASSERT(SkPaint::kFill_Style != paint.getStyle());
741
742 SkVector size;
743 SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() };
744 matrix.mapVectors(&size, &pt, 1);
745 return SkPoint::Make(SkScalarAbs(size.fX), SkScalarAbs(size.fY));
746}
747
reed@google.com761fb622011-04-04 18:58:05 +0000748static bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix,
749 SkPoint* strokeSize) {
750 if (SkPaint::kMiter_Join != paint.getStrokeJoin() ||
751 paint.getStrokeMiter() < SK_ScalarSqrt2) {
752 return false;
753 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000754
fmalita1a178ca2015-01-15 06:01:23 -0800755 *strokeSize = compute_stroke_size(paint, matrix);
reed@google.com761fb622011-04-04 18:58:05 +0000756 return true;
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000757}
758
reed@google.com62ab7ad2011-04-05 14:08:25 +0000759SkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint,
760 const SkMatrix& matrix,
761 SkPoint* strokeSize) {
762 RectType rtype;
763 const SkScalar width = paint.getStrokeWidth();
764 const bool zeroWidth = (0 == width);
765 SkPaint::Style style = paint.getStyle();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000766
reed@google.com62ab7ad2011-04-05 14:08:25 +0000767 if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
768 style = SkPaint::kFill_Style;
769 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000770
reed@google.com62ab7ad2011-04-05 14:08:25 +0000771 if (paint.getPathEffect() || paint.getMaskFilter() ||
772 paint.getRasterizer() || !matrix.rectStaysRect() ||
773 SkPaint::kStrokeAndFill_Style == style) {
774 rtype = kPath_RectType;
775 } else if (SkPaint::kFill_Style == style) {
776 rtype = kFill_RectType;
777 } else if (zeroWidth) {
778 rtype = kHair_RectType;
779 } else if (easy_rect_join(paint, matrix, strokeSize)) {
780 rtype = kStroke_RectType;
781 } else {
782 rtype = kPath_RectType;
783 }
784 return rtype;
785}
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000786
reed@google.com73244152012-05-11 14:35:23 +0000787static const SkPoint* rect_points(const SkRect& r) {
reed@google.comfc2f0d02013-03-29 14:23:56 +0000788 return SkTCast<const SkPoint*>(&r);
reed@google.com73244152012-05-11 14:35:23 +0000789}
790
791static SkPoint* rect_points(SkRect& r) {
reed@google.comfc2f0d02013-03-29 14:23:56 +0000792 return SkTCast<SkPoint*>(&r);
reed@google.com40c2ba22011-07-25 19:41:22 +0000793}
794
reed03939122014-12-15 13:42:51 -0800795void SkDraw::drawRect(const SkRect& prePaintRect, const SkPaint& paint,
796 const SkMatrix* paintMatrix, const SkRect* postPaintRect) const {
reed@android.comf2b98d62010-12-20 18:26:13 +0000797 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000798
799 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +0000800 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801 return;
802 }
803
reed03939122014-12-15 13:42:51 -0800804 const SkMatrix* matrix;
805 SkMatrix combinedMatrixStorage;
806 if (paintMatrix) {
807 SkASSERT(postPaintRect);
808 combinedMatrixStorage.setConcat(*fMatrix, *paintMatrix);
809 matrix = &combinedMatrixStorage;
810 } else {
811 SkASSERT(!postPaintRect);
812 matrix = fMatrix;
813 }
814
reed@google.com761fb622011-04-04 18:58:05 +0000815 SkPoint strokeSize;
reed@google.com62ab7ad2011-04-05 14:08:25 +0000816 RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize);
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000817
mike@reedtribe.org7ff678b2011-04-04 14:38:12 +0000818 if (kPath_RectType == rtype) {
reed03939122014-12-15 13:42:51 -0800819 SkDraw draw(*this);
820 if (paintMatrix) {
821 draw.fMatrix = matrix;
822 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823 SkPath tmp;
reed03939122014-12-15 13:42:51 -0800824 tmp.addRect(prePaintRect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000825 tmp.setFillType(SkPath::kWinding_FillType);
reed03939122014-12-15 13:42:51 -0800826 draw.drawPath(tmp, paint, NULL, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000827 return;
828 }
829
reed03939122014-12-15 13:42:51 -0800830 SkRect devRect;
fmalita1a178ca2015-01-15 06:01:23 -0800831 const SkRect& paintRect = paintMatrix ? *postPaintRect : prePaintRect;
832 // skip the paintMatrix when transforming the rect by the CTM
833 fMatrix->mapPoints(rect_points(devRect), rect_points(paintRect), 2);
reed@google.com73244152012-05-11 14:35:23 +0000834 devRect.sort();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835
reed@android.com8a1c16f2008-12-17 15:59:43 +0000836 // look for the quick exit, before we build a blitter
fmalita1a178ca2015-01-15 06:01:23 -0800837 SkRect bbox = devRect;
reed@google.com1c028bd2013-08-28 15:23:19 +0000838 if (paint.getStyle() != SkPaint::kFill_Style) {
839 // extra space for hairlines
georgeb3eba472014-09-09 11:33:57 -0700840 if (paint.getStrokeWidth() == 0) {
fmalita1a178ca2015-01-15 06:01:23 -0800841 bbox.outset(1, 1);
georgeb3eba472014-09-09 11:33:57 -0700842 } else {
fmalita1a178ca2015-01-15 06:01:23 -0800843 // For kStroke_RectType, strokeSize is already computed.
844 const SkPoint& ssize = (kStroke_RectType == rtype)
845 ? strokeSize
846 : compute_stroke_size(paint, *fMatrix);
847 bbox.outset(SkScalarHalf(ssize.x()), SkScalarHalf(ssize.y()));
georgeb3eba472014-09-09 11:33:57 -0700848 }
reed@google.com1c028bd2013-08-28 15:23:19 +0000849 }
fmalita1a178ca2015-01-15 06:01:23 -0800850
851 SkIRect ir = bbox.roundOut();
reed@google.com1c028bd2013-08-28 15:23:19 +0000852 if (fRC->quickReject(ir)) {
853 return;
rmistry@google.come09d6f42013-08-27 18:53:41 +0000854 }
855
reed@google.com1c028bd2013-08-28 15:23:19 +0000856 SkDeviceLooper looper(*fBitmap, *fRC, ir, paint.isAntiAlias());
857 while (looper.next()) {
858 SkRect localDevRect;
859 looper.mapRect(&localDevRect, devRect);
860 SkMatrix localMatrix;
reed03939122014-12-15 13:42:51 -0800861 looper.mapMatrix(&localMatrix, *matrix);
rmistry@google.come09d6f42013-08-27 18:53:41 +0000862
reed03939122014-12-15 13:42:51 -0800863 SkAutoBlitterChoose blitterStorage(looper.getBitmap(), localMatrix, paint);
reed@google.com1c028bd2013-08-28 15:23:19 +0000864 const SkRasterClip& clip = looper.getRC();
865 SkBlitter* blitter = blitterStorage.get();
866
867 // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
868 // case we are also hairline (if we've gotten to here), which devolves to
869 // effectively just kFill
870 switch (rtype) {
871 case kFill_RectType:
872 if (paint.isAntiAlias()) {
873 SkScan::AntiFillRect(localDevRect, clip, blitter);
874 } else {
875 SkScan::FillRect(localDevRect, clip, blitter);
876 }
877 break;
878 case kStroke_RectType:
879 if (paint.isAntiAlias()) {
880 SkScan::AntiFrameRect(localDevRect, strokeSize, clip, blitter);
881 } else {
882 SkScan::FrameRect(localDevRect, strokeSize, clip, blitter);
883 }
884 break;
885 case kHair_RectType:
886 if (paint.isAntiAlias()) {
887 SkScan::AntiHairRect(localDevRect, clip, blitter);
888 } else {
889 SkScan::HairRect(localDevRect, clip, blitter);
890 }
891 break;
892 default:
893 SkDEBUGFAIL("bad rtype");
894 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000895 }
896}
897
898void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const {
899 if (srcM.fBounds.isEmpty()) {
900 return;
901 }
902
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000903 const SkMask* mask = &srcM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000904
bungeman@google.com0a60b3d2011-09-26 19:09:08 +0000905 SkMask dstM;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000906 if (paint.getMaskFilter() &&
907 paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) {
908 mask = &dstM;
bungeman@google.combf2ac7e2011-09-26 19:51:33 +0000909 } else {
910 dstM.fImage = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000911 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000912 SkAutoMaskFreeImage ami(dstM.fImage);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000913
reed@google.com045e62d2011-10-24 12:19:46 +0000914 SkAutoBlitterChoose blitterChooser(*fBitmap, *fMatrix, paint);
915 SkBlitter* blitter = blitterChooser.get();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000916
reed@google.com045e62d2011-10-24 12:19:46 +0000917 SkAAClipBlitterWrapper wrapper;
918 const SkRegion* clipRgn;
919
920 if (fRC->isBW()) {
921 clipRgn = &fRC->bwRgn();
922 } else {
923 wrapper.init(*fRC, blitter);
924 clipRgn = &wrapper.getRgn();
925 blitter = wrapper.getBlitter();
926 }
927 blitter->blitMaskRegion(*mask, *clipRgn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000928}
929
reed@android.comebdeeb82009-09-03 21:45:49 +0000930static SkScalar fast_len(const SkVector& vec) {
931 SkScalar x = SkScalarAbs(vec.fX);
932 SkScalar y = SkScalarAbs(vec.fY);
933 if (x < y) {
934 SkTSwap(x, y);
935 }
936 return x + SkScalarHalf(y);
937}
938
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000939bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix& matrix,
940 SkScalar* coverage) {
941 SkASSERT(strokeWidth > 0);
942 // We need to try to fake a thick-stroke with a modulated hairline.
reed@google.comecadf992011-09-19 19:18:18 +0000943
tomhudson@google.com8d430182011-06-06 19:11:19 +0000944 if (matrix.hasPerspective()) {
reed@android.comebdeeb82009-09-03 21:45:49 +0000945 return false;
946 }
reed@google.comecadf992011-09-19 19:18:18 +0000947
reed@android.comebdeeb82009-09-03 21:45:49 +0000948 SkVector src[2], dst[2];
reed@google.comecadf992011-09-19 19:18:18 +0000949 src[0].set(strokeWidth, 0);
950 src[1].set(0, strokeWidth);
reed@android.comebdeeb82009-09-03 21:45:49 +0000951 matrix.mapVectors(dst, src, 2);
952 SkScalar len0 = fast_len(dst[0]);
953 SkScalar len1 = fast_len(dst[1]);
agl@chromium.org652807b2010-04-27 15:47:34 +0000954 if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
bsalomon49f085d2014-09-05 13:34:00 -0700955 if (coverage) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000956 *coverage = SkScalarAve(len0, len1);
957 }
reed@android.comebdeeb82009-09-03 21:45:49 +0000958 return true;
959 }
960 return false;
961}
962
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000963void SkDraw::drawRRect(const SkRRect& rrect, const SkPaint& paint) const {
964 SkDEBUGCODE(this->validate());
965
966 if (fRC->isEmpty()) {
967 return;
968 }
969
970 {
971 // TODO: Investigate optimizing these options. They are in the same
972 // order as SkDraw::drawPath, which handles each case. It may be
973 // that there is no way to optimize for these using the SkRRect path.
974 SkScalar coverage;
975 if (SkDrawTreatAsHairline(paint, *fMatrix, &coverage)) {
976 goto DRAW_PATH;
977 }
978
979 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
980 goto DRAW_PATH;
981 }
982
983 if (paint.getRasterizer()) {
984 goto DRAW_PATH;
985 }
986 }
987
988 if (paint.getMaskFilter()) {
989 // Transform the rrect into device space.
990 SkRRect devRRect;
991 if (rrect.transform(*fMatrix, &devRRect)) {
992 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint);
reed868074b2014-06-03 10:53:59 -0700993 if (paint.getMaskFilter()->filterRRect(devRRect, *fMatrix, *fRC, blitter.get(),
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000994 SkPaint::kFill_Style)) {
995 return; // filterRRect() called the blitter, so we're done
996 }
997 }
998 }
999
1000DRAW_PATH:
1001 // Now fall back to the default case of using a path.
1002 SkPath path;
1003 path.addRRect(rrect);
1004 this->drawPath(path, paint, NULL, true);
1005}
1006
reed05d90442015-02-12 13:35:52 -08001007static SkScalar compute_res_scale_for_stroking(const SkMatrix& matrix) {
1008 if (!matrix.hasPerspective()) {
1009 SkScalar sx = SkPoint::Length(matrix[SkMatrix::kMScaleX], matrix[SkMatrix::kMSkewY]);
1010 SkScalar sy = SkPoint::Length(matrix[SkMatrix::kMSkewX], matrix[SkMatrix::kMScaleY]);
1011 if (SkScalarsAreFinite(sx, sy)) {
1012 return SkTMax(sx, sy);
1013 }
1014 }
1015 return 1;
1016}
1017
reed@google.com32e5d972011-07-27 18:25:57 +00001018void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint,
reed@google.com126f7f52013-11-07 16:06:53 +00001019 const SkMatrix* prePathMatrix, bool pathIsMutable,
krajcevski53f09592014-08-06 11:12:14 -07001020 bool drawCoverage, SkBlitter* customBlitter) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001021 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001022
1023 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001024 if (fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001025 return;
1026 }
1027
1028 SkPath* pathPtr = (SkPath*)&origSrcPath;
1029 bool doFill = true;
1030 SkPath tmpPath;
1031 SkMatrix tmpMatrix;
1032 const SkMatrix* matrix = fMatrix;
jvanverthb3eb6872014-10-24 07:12:51 -07001033 tmpPath.setIsVolatile(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001034
1035 if (prePathMatrix) {
reed@google.com32e5d972011-07-27 18:25:57 +00001036 if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style ||
1037 origPaint.getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001038 SkPath* result = pathPtr;
reed@google.coma76de3d2011-01-13 18:30:42 +00001039
reed@android.com8a1c16f2008-12-17 15:59:43 +00001040 if (!pathIsMutable) {
1041 result = &tmpPath;
1042 pathIsMutable = true;
1043 }
1044 pathPtr->transform(*prePathMatrix, result);
1045 pathPtr = result;
1046 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001047 tmpMatrix.setConcat(*matrix, *prePathMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001048 matrix = &tmpMatrix;
1049 }
1050 }
1051 // at this point we're done with prePathMatrix
1052 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.coma76de3d2011-01-13 18:30:42 +00001053
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001054 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
reed@google.coma76de3d2011-01-13 18:30:42 +00001055
reed@google.comecadf992011-09-19 19:18:18 +00001056 {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001057 SkScalar coverage;
1058 if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) {
1059 if (SK_Scalar1 == coverage) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001060 paint.writable()->setStrokeWidth(0);
egdanieldcfb7cf2015-01-22 06:52:29 -08001061 } else if (SkXfermode::SupportsCoverageAsAlpha(origPaint.getXfermode())) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001062 U8CPU newAlpha;
1063#if 0
1064 newAlpha = SkToU8(SkScalarRoundToInt(coverage *
1065 origPaint.getAlpha()));
1066#else
1067 // this is the old technique, which we preserve for now so
1068 // we don't change previous results (testing)
1069 // the new way seems fine, its just (a tiny bit) different
1070 int scale = (int)SkScalarMul(coverage, 256);
1071 newAlpha = origPaint.getAlpha() * scale >> 8;
1072#endif
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001073 SkPaint* writablePaint = paint.writable();
1074 writablePaint->setStrokeWidth(0);
1075 writablePaint->setAlpha(newAlpha);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001076 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001077 }
1078 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001079
reed@google.com32e5d972011-07-27 18:25:57 +00001080 if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) {
reed@google.com4bbdeac2013-01-24 21:03:11 +00001081 SkRect cullRect;
1082 const SkRect* cullRectPtr = NULL;
1083 if (this->computeConservativeLocalClipBounds(&cullRect)) {
1084 cullRectPtr = &cullRect;
1085 }
reed05d90442015-02-12 13:35:52 -08001086 doFill = paint->getFillPath(*pathPtr, &tmpPath, cullRectPtr,
1087 compute_res_scale_for_stroking(*fMatrix));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001088 pathPtr = &tmpPath;
1089 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001090
reed@google.com32e5d972011-07-27 18:25:57 +00001091 if (paint->getRasterizer()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001092 SkMask mask;
reed@google.com32e5d972011-07-27 18:25:57 +00001093 if (paint->getRasterizer()->rasterize(*pathPtr, *matrix,
reed@google.com045e62d2011-10-24 12:19:46 +00001094 &fRC->getBounds(), paint->getMaskFilter(), &mask,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001095 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
reed@google.com32e5d972011-07-27 18:25:57 +00001096 this->drawDevMask(mask, *paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001097 SkMask::FreeImage(mask.fImage);
1098 }
1099 return;
1100 }
1101
1102 // avoid possibly allocating a new path in transform if we can
1103 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1104
1105 // transform the path into device space
1106 pathPtr->transform(*matrix, devPathPtr);
1107
krajcevski53f09592014-08-06 11:12:14 -07001108 SkBlitter* blitter = NULL;
1109 SkAutoBlitterChoose blitterStorage;
1110 if (NULL == customBlitter) {
1111 blitterStorage.choose(*fBitmap, *fMatrix, *paint, drawCoverage);
1112 blitter = blitterStorage.get();
1113 } else {
1114 blitter = customBlitter;
1115 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001116
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001117 if (paint->getMaskFilter()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001118 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001119 SkPaint::kStroke_Style;
krajcevski53f09592014-08-06 11:12:14 -07001120 if (paint->getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fRC, blitter, style)) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001121 return; // filterPath() called the blitter, so we're done
1122 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123 }
1124
reed@google.com045e62d2011-10-24 12:19:46 +00001125 void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001126 if (doFill) {
reed@google.com32e5d972011-07-27 18:25:57 +00001127 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001128 proc = SkScan::AntiFillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001129 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001130 proc = SkScan::FillPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001131 }
1132 } else { // hairline
reed@google.com32e5d972011-07-27 18:25:57 +00001133 if (paint->isAntiAlias()) {
reed@google.com045e62d2011-10-24 12:19:46 +00001134 proc = SkScan::AntiHairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001135 } else {
reed@google.com045e62d2011-10-24 12:19:46 +00001136 proc = SkScan::HairPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001137 }
1138 }
krajcevski53f09592014-08-06 11:12:14 -07001139 proc(*devPathPtr, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001140}
1141
reed@android.com0baf1932009-06-24 12:41:42 +00001142/** For the purposes of drawing bitmaps, if a matrix is "almost" translate
1143 go ahead and treat it as if it were, so that subsequent code can go fast.
1144 */
1145static bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) {
reed@google.com070dcd82013-01-07 20:27:52 +00001146 unsigned bits = 0; // TODO: find a way to allow the caller to tell us to
1147 // respect filtering.
1148 return SkTreatAsSprite(matrix, bitmap.width(), bitmap.height(), bits);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149}
1150
1151void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap,
1152 const SkPaint& paint) const {
reed@google.com900ecf22014-02-20 20:55:37 +00001153 SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001154
reed@google.coma76de3d2011-01-13 18:30:42 +00001155 if (just_translate(*fMatrix, bitmap)) {
reed@google.come1ca7052013-12-17 19:22:07 +00001156 int ix = SkScalarRoundToInt(fMatrix->getTranslateX());
1157 int iy = SkScalarRoundToInt(fMatrix->getTranslateY());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001158
reed@google.coma641f3f2012-12-13 22:16:30 +00001159 SkAutoLockPixels alp(bitmap);
1160 if (!bitmap.readyToDraw()) {
1161 return;
1162 }
1163
reed@android.com8a1c16f2008-12-17 15:59:43 +00001164 SkMask mask;
1165 mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
1166 mask.fFormat = SkMask::kA8_Format;
scroggo@google.come5f48242013-02-25 21:47:41 +00001167 mask.fRowBytes = SkToU32(bitmap.rowBytes());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001168 mask.fImage = bitmap.getAddr8(0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001169
reed@android.com8a1c16f2008-12-17 15:59:43 +00001170 this->drawDevMask(mask, paint);
1171 } else { // need to xform the bitmap first
1172 SkRect r;
1173 SkMask mask;
reed@google.coma76de3d2011-01-13 18:30:42 +00001174
reed@android.com8a1c16f2008-12-17 15:59:43 +00001175 r.set(0, 0,
1176 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
1177 fMatrix->mapRect(&r);
1178 r.round(&mask.fBounds);
reed@google.coma76de3d2011-01-13 18:30:42 +00001179
reed@android.com8a1c16f2008-12-17 15:59:43 +00001180 // set the mask's bounds to the transformed bitmap-bounds,
1181 // clipped to the actual device
1182 {
1183 SkIRect devBounds;
1184 devBounds.set(0, 0, fBitmap->width(), fBitmap->height());
1185 // need intersect(l, t, r, b) on irect
1186 if (!mask.fBounds.intersect(devBounds)) {
1187 return;
1188 }
1189 }
reed@android.com543ed932009-04-24 12:43:40 +00001190
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191 mask.fFormat = SkMask::kA8_Format;
1192 mask.fRowBytes = SkAlign4(mask.fBounds.width());
reed@android.com543ed932009-04-24 12:43:40 +00001193 size_t size = mask.computeImageSize();
1194 if (0 == size) {
1195 // the mask is too big to allocated, draw nothing
1196 return;
1197 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001198
1199 // allocate (and clear) our temp buffer to hold the transformed bitmap
reed@android.com8a1c16f2008-12-17 15:59:43 +00001200 SkAutoMalloc storage(size);
1201 mask.fImage = (uint8_t*)storage.get();
1202 memset(mask.fImage, 0, size);
reed@google.coma76de3d2011-01-13 18:30:42 +00001203
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 // now draw our bitmap(src) into mask(dst), transformed by the matrix
1205 {
1206 SkBitmap device;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001207 device.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
1208 mask.fImage, mask.fRowBytes);
reed@google.coma76de3d2011-01-13 18:30:42 +00001209
reed@android.com8a1c16f2008-12-17 15:59:43 +00001210 SkCanvas c(device);
1211 // need the unclipped top/left for the translate
1212 c.translate(-SkIntToScalar(mask.fBounds.fLeft),
1213 -SkIntToScalar(mask.fBounds.fTop));
1214 c.concat(*fMatrix);
reed@android.com3469c762009-02-24 19:03:20 +00001215
1216 // We can't call drawBitmap, or we'll infinitely recurse. Instead
reed@android.comfb12c3e2009-03-05 20:43:42 +00001217 // we manually build a shader and draw that into our new mask
reed@android.com3469c762009-02-24 19:03:20 +00001218 SkPaint tmpPaint;
1219 tmpPaint.setFlags(paint.getFlags());
reed@google.com40c2ba22011-07-25 19:41:22 +00001220 SkAutoBitmapShaderInstall install(bitmap, tmpPaint);
reed@android.com3469c762009-02-24 19:03:20 +00001221 SkRect rr;
1222 rr.set(0, 0, SkIntToScalar(bitmap.width()),
1223 SkIntToScalar(bitmap.height()));
reed@google.com40c2ba22011-07-25 19:41:22 +00001224 c.drawRect(rr, install.paintWithShader());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001225 }
1226 this->drawDevMask(mask, paint);
1227 }
1228}
1229
reed@google.com045e62d2011-10-24 12:19:46 +00001230static bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001231 const SkRect& srcR) {
1232 SkRect dstR;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001233 m.mapRect(&dstR, srcR);
reedb07a94f2014-11-19 05:03:18 -08001234 return c.quickReject(dstR.roundOut());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001235}
1236
reed@google.com045e62d2011-10-24 12:19:46 +00001237static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001238 int width, int height) {
1239 SkRect r;
1240 r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
1241 return clipped_out(matrix, clip, r);
1242}
1243
reed@google.com045e62d2011-10-24 12:19:46 +00001244static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y,
1245 const SkBitmap& bitmap) {
1246 return clip.isBW() ||
1247 clip.quickContains(x, y, x + bitmap.width(), y + bitmap.height());
1248}
1249
reed@android.com8a1c16f2008-12-17 15:59:43 +00001250void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
reed03939122014-12-15 13:42:51 -08001251 const SkRect* dstBounds, const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001252 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253
1254 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001255 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001256 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001257 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001258 return;
1259 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001260
reed@google.com40c2ba22011-07-25 19:41:22 +00001261 SkPaint paint(origPaint);
1262 paint.setStyle(SkPaint::kFill_Style);
reed@google.coma76de3d2011-01-13 18:30:42 +00001263
reed@android.com8a1c16f2008-12-17 15:59:43 +00001264 SkMatrix matrix;
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001265 matrix.setConcat(*fMatrix, prematrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001266
reed@google.com045e62d2011-10-24 12:19:46 +00001267 if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001268 return;
1269 }
1270
reed868074b2014-06-03 10:53:59 -07001271 if (bitmap.colorType() != kAlpha_8_SkColorType && just_translate(matrix, bitmap)) {
reed@google.comf7ef56d2012-12-14 13:46:53 +00001272 //
1273 // It is safe to call lock pixels now, since we know the matrix is
1274 // (more or less) identity.
1275 //
1276 SkAutoLockPixels alp(bitmap);
1277 if (!bitmap.readyToDraw()) {
1278 return;
1279 }
reed@google.come1ca7052013-12-17 19:22:07 +00001280 int ix = SkScalarRoundToInt(matrix.getTranslateX());
1281 int iy = SkScalarRoundToInt(matrix.getTranslateY());
reed@google.com045e62d2011-10-24 12:19:46 +00001282 if (clipHandlesSprite(*fRC, ix, iy, bitmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001283 SkTBlitterAllocator allocator;
1284 // blitter will be owned by the allocator.
1285 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1286 ix, iy, &allocator);
reed@google.com045e62d2011-10-24 12:19:46 +00001287 if (blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +00001288 SkIRect ir;
1289 ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001290
reed@google.com045e62d2011-10-24 12:19:46 +00001291 SkScan::FillIRect(ir, *fRC, blitter);
1292 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001293 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001294 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001295 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001296
reed@android.com8a1c16f2008-12-17 15:59:43 +00001297 // now make a temp draw on the stack, and use it
1298 //
1299 SkDraw draw(*this);
1300 draw.fMatrix = &matrix;
reed@google.coma76de3d2011-01-13 18:30:42 +00001301
reed@google.com900ecf22014-02-20 20:55:37 +00001302 if (bitmap.colorType() == kAlpha_8_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001303 draw.drawBitmapAsMask(bitmap, paint);
1304 } else {
reed@google.com40c2ba22011-07-25 19:41:22 +00001305 SkAutoBitmapShaderInstall install(bitmap, paint);
reed03939122014-12-15 13:42:51 -08001306 const SkPaint& paintWithShader = install.paintWithShader();
1307 const SkRect srcBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
1308 if (dstBounds) {
1309 this->drawRect(srcBounds, paintWithShader, &prematrix, dstBounds);
1310 } else {
1311 draw.drawRect(srcBounds, paintWithShader);
1312 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001313 }
1314}
1315
1316void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
reed@google.com40c2ba22011-07-25 19:41:22 +00001317 const SkPaint& origPaint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001318 SkDEBUGCODE(this->validate();)
reed@google.coma76de3d2011-01-13 18:30:42 +00001319
reed@android.com8a1c16f2008-12-17 15:59:43 +00001320 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001321 if (fRC->isEmpty() ||
reed@android.com8a1c16f2008-12-17 15:59:43 +00001322 bitmap.width() == 0 || bitmap.height() == 0 ||
reed@google.com900ecf22014-02-20 20:55:37 +00001323 bitmap.colorType() == kUnknown_SkColorType) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001324 return;
1325 }
1326
1327 SkIRect bounds;
1328 bounds.set(x, y, x + bitmap.width(), y + bitmap.height());
1329
reed@google.com045e62d2011-10-24 12:19:46 +00001330 if (fRC->quickReject(bounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331 return; // nothing to draw
1332 }
1333
reed@google.com40c2ba22011-07-25 19:41:22 +00001334 SkPaint paint(origPaint);
1335 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336
reed@google.com045e62d2011-10-24 12:19:46 +00001337 if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001338 SkTBlitterAllocator allocator;
1339 // blitter will be owned by the allocator.
1340 SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
1341 x, y, &allocator);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342
1343 if (blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +00001344 SkScan::FillIRect(bounds, *fRC, blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001345 return;
1346 }
1347 }
1348
reed@android.com8a1c16f2008-12-17 15:59:43 +00001349 SkMatrix matrix;
1350 SkRect r;
1351
1352 // get a scalar version of our rect
1353 r.set(bounds);
1354
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001355 // create shader with offset
reed@android.com8a1c16f2008-12-17 15:59:43 +00001356 matrix.setTranslate(r.fLeft, r.fTop);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001357 SkAutoBitmapShaderInstall install(bitmap, paint, &matrix);
1358 const SkPaint& shaderPaint = install.paintWithShader();
reed@google.coma76de3d2011-01-13 18:30:42 +00001359
reed@android.com8a1c16f2008-12-17 15:59:43 +00001360 SkDraw draw(*this);
1361 matrix.reset();
1362 draw.fMatrix = &matrix;
1363 // call ourself with a rect
reed@google.coma76de3d2011-01-13 18:30:42 +00001364 // is this OK if paint has a rasterizer?
reed@google.com40c2ba22011-07-25 19:41:22 +00001365 draw.drawRect(r, shaderPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001366}
1367
1368///////////////////////////////////////////////////////////////////////////////
1369
1370#include "SkScalerContext.h"
1371#include "SkGlyphCache.h"
reed@google.come6913762012-08-07 15:19:47 +00001372#include "SkTextToPathIter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001373#include "SkUtils.h"
1374
1375static void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
1376 const char text[], size_t byteLength, SkVector* stopVector) {
1377 SkFixed x = 0, y = 0;
1378 const char* stop = text + byteLength;
1379
1380 SkAutoKern autokern;
reed@google.coma76de3d2011-01-13 18:30:42 +00001381
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382 while (text < stop) {
1383 // don't need x, y here, since all subpixel variants will have the
1384 // same advance
1385 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1386
1387 x += autokern.adjust(glyph) + glyph.fAdvanceX;
1388 y += glyph.fAdvanceY;
1389 }
1390 stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
1391
1392 SkASSERT(text == stop);
1393}
1394
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001395bool SkDraw::ShouldDrawTextAsPaths(const SkPaint& paint, const SkMatrix& ctm) {
1396 // hairline glyphs are fast enough so we don't need to cache them
1397 if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) {
1398 return true;
1399 }
1400
1401 // we don't cache perspective
1402 if (ctm.hasPerspective()) {
1403 return true;
1404 }
1405
1406 SkMatrix textM;
1407 return SkPaint::TooBigToUseCache(ctm, *paint.setTextMatrix(&textM));
1408}
1409
reed@android.com8a1c16f2008-12-17 15:59:43 +00001410void SkDraw::drawText_asPaths(const char text[], size_t byteLength,
1411 SkScalar x, SkScalar y,
1412 const SkPaint& paint) const {
reed@android.comf2b98d62010-12-20 18:26:13 +00001413 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001414
djsollen@google.com166e6532012-03-20 14:24:38 +00001415 SkTextToPathIter iter(text, byteLength, paint, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001416
1417 SkMatrix matrix;
1418 matrix.setScale(iter.getPathScale(), iter.getPathScale());
1419 matrix.postTranslate(x, y);
1420
1421 const SkPath* iterPath;
1422 SkScalar xpos, prevXPos = 0;
1423
reed@google.com7b4531f2012-08-07 15:53:00 +00001424 while (iter.next(&iterPath, &xpos)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425 matrix.postTranslate(xpos - prevXPos, 0);
reed@google.com7b4531f2012-08-07 15:53:00 +00001426 if (iterPath) {
1427 const SkPaint& pnt = iter.getPaint();
1428 if (fDevice) {
1429 fDevice->drawPath(*this, *iterPath, pnt, &matrix, false);
1430 } else {
1431 this->drawPath(*iterPath, pnt, &matrix, false);
1432 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001433 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001434 prevXPos = xpos;
1435 }
1436}
1437
reed@android.com8a1c16f2008-12-17 15:59:43 +00001438// disable warning : local variable used without having been initialized
reed@google.coma76de3d2011-01-13 18:30:42 +00001439#if defined _WIN32 && _MSC_VER >= 1300
reed@android.com8a1c16f2008-12-17 15:59:43 +00001440#pragma warning ( push )
1441#pragma warning ( disable : 4701 )
1442#endif
1443
1444//////////////////////////////////////////////////////////////////////////////
1445
reed868074b2014-06-03 10:53:59 -07001446static void D1G_RectClip(const SkDraw1Glyph& state, SkFixed fx, SkFixed fy, const SkGlyph& glyph) {
mike@reedtribe.org9fb00412014-01-06 03:02:37 +00001447 int left = SkFixedFloorToInt(fx);
1448 int top = SkFixedFloorToInt(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001449 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001450 SkASSERT((NULL == state.fClip && state.fAAClip) ||
1451 (state.fClip && NULL == state.fAAClip && state.fClip->isRect()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001452
1453 left += glyph.fLeft;
1454 top += glyph.fTop;
1455
1456 int right = left + glyph.fWidth;
1457 int bottom = top + glyph.fHeight;
1458
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001459 SkMask mask;
1460 SkIRect storage;
1461 SkIRect* bounds = &mask.fBounds;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001462
tomhudson@google.com83a44462011-10-27 15:27:51 +00001463 mask.fBounds.set(left, top, right, bottom);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001464
tomhudson@google.com83a44462011-10-27 15:27:51 +00001465 // this extra test is worth it, assuming that most of the time it succeeds
1466 // since we can avoid writing to storage
1467 if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) {
1468 if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds))
1469 return;
1470 bounds = &storage;
1471 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001472
tomhudson@google.com83a44462011-10-27 15:27:51 +00001473 uint8_t* aa = (uint8_t*)glyph.fImage;
1474 if (NULL == aa) {
1475 aa = (uint8_t*)state.fCache->findImage(glyph);
1476 if (NULL == aa) {
1477 return; // can't rasterize glyph
reed@android.com8a1c16f2008-12-17 15:59:43 +00001478 }
tomhudson@google.com83a44462011-10-27 15:27:51 +00001479 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001480
tomhudson@google.com83a44462011-10-27 15:27:51 +00001481 mask.fRowBytes = glyph.rowBytes();
1482 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1483 mask.fImage = aa;
reed@google.com5bdfb332013-05-02 18:55:44 +00001484 state.blitMask(mask, *bounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001485}
1486
reed868074b2014-06-03 10:53:59 -07001487static void D1G_RgnClip(const SkDraw1Glyph& state, SkFixed fx, SkFixed fy, const SkGlyph& glyph) {
mike@reedtribe.org9fb00412014-01-06 03:02:37 +00001488 int left = SkFixedFloorToInt(fx);
1489 int top = SkFixedFloorToInt(fy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001490 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001491 SkASSERT(!state.fClip->isRect());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001492
1493 SkMask mask;
1494
1495 left += glyph.fLeft;
1496 top += glyph.fTop;
1497
1498 mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001499 SkRegion::Cliperator clipper(*state.fClip, mask.fBounds);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001500
tomhudson@google.com83a44462011-10-27 15:27:51 +00001501 if (!clipper.done()) {
1502 const SkIRect& cr = clipper.rect();
1503 const uint8_t* aa = (const uint8_t*)glyph.fImage;
1504 if (NULL == aa) {
1505 aa = (uint8_t*)state.fCache->findImage(glyph);
1506 if (NULL == aa) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001507 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001508 }
tomhudson@google.com83a44462011-10-27 15:27:51 +00001509 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001510
tomhudson@google.com83a44462011-10-27 15:27:51 +00001511 mask.fRowBytes = glyph.rowBytes();
1512 mask.fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
1513 mask.fImage = (uint8_t*)aa;
1514 do {
reed@google.com5bdfb332013-05-02 18:55:44 +00001515 state.blitMask(mask, cr);
tomhudson@google.com83a44462011-10-27 15:27:51 +00001516 clipper.next();
1517 } while (!clipper.done());
1518 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001519}
1520
reed@google.comfd4236e2011-07-25 21:16:22 +00001521static bool hasCustomD1GProc(const SkDraw& draw) {
1522 return draw.fProcs && draw.fProcs->fD1GProc;
1523}
1524
1525static bool needsRasterTextBlit(const SkDraw& draw) {
1526 return !hasCustomD1GProc(draw);
1527}
1528
reed868074b2014-06-03 10:53:59 -07001529SkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter, SkGlyphCache* cache,
1530 const SkPaint& pnt) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001531 fDraw = draw;
bungeman@google.com2211b622012-01-13 15:02:58 +00001532 fBlitter = blitter;
1533 fCache = cache;
reed@google.com5bdfb332013-05-02 18:55:44 +00001534 fPaint = &pnt;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001535
bungeman@google.com94471032013-02-25 15:55:13 +00001536 if (cache->isSubpixel()) {
1537 fHalfSampleX = fHalfSampleY = (SK_FixedHalf >> SkGlyph::kSubBits);
1538 } else {
1539 fHalfSampleX = fHalfSampleY = SK_FixedHalf;
1540 }
1541
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001542 if (hasCustomD1GProc(*draw)) {
reed@google.com045e62d2011-10-24 12:19:46 +00001543 // todo: fix this assumption about clips w/ custom
1544 fClip = draw->fClip;
1545 fClipBounds = fClip->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001546 return draw->fProcs->fD1GProc;
1547 }
1548
reed@google.com045e62d2011-10-24 12:19:46 +00001549 if (draw->fRC->isBW()) {
1550 fAAClip = NULL;
1551 fClip = &draw->fRC->bwRgn();
1552 fClipBounds = fClip->getBounds();
reed868074b2014-06-03 10:53:59 -07001553 if (fClip->isRect()) {
1554 return D1G_RectClip;
reed@google.com045e62d2011-10-24 12:19:46 +00001555 } else {
reed868074b2014-06-03 10:53:59 -07001556 return D1G_RgnClip;
reed@google.com045e62d2011-10-24 12:19:46 +00001557 }
1558 } else { // aaclip
1559 fAAClip = &draw->fRC->aaRgn();
1560 fClip = NULL;
1561 fClipBounds = fAAClip->getBounds();
reed868074b2014-06-03 10:53:59 -07001562 return D1G_RectClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001563 }
1564}
1565
reed@google.com5bdfb332013-05-02 18:55:44 +00001566void SkDraw1Glyph::blitMaskAsSprite(const SkMask& mask) const {
1567 SkASSERT(SkMask::kARGB32_Format == mask.fFormat);
1568
1569 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001570 bm.installPixels(SkImageInfo::MakeN32Premul(mask.fBounds.width(), mask.fBounds.height()),
1571 (SkPMColor*)mask.fImage, mask.fRowBytes);
reed@google.com5bdfb332013-05-02 18:55:44 +00001572
1573 fDraw->drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), *fPaint);
1574}
1575
reed@android.com8a1c16f2008-12-17 15:59:43 +00001576///////////////////////////////////////////////////////////////////////////////
1577
1578void SkDraw::drawText(const char text[], size_t byteLength,
1579 SkScalar x, SkScalar y, const SkPaint& paint) const {
1580 SkASSERT(byteLength == 0 || text != NULL);
1581
reed@android.comf2b98d62010-12-20 18:26:13 +00001582 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001583
1584 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001585 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001586 return;
1587 }
1588
bsalomon@google.com36d6eda2012-10-10 16:12:14 +00001589 // SkScalarRec doesn't currently have a way of representing hairline stroke and
1590 // will fill if its frame-width is 0.
reed@google.comed43dff2013-06-04 16:56:27 +00001591 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001592 this->drawText_asPaths(text, byteLength, x, y, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001593 return;
1594 }
1595
1596 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1597
reede010f1c2014-09-17 10:49:38 -07001598 SkAutoGlyphCache autoCache(paint, &fDevice->getLeakyProperties(), fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001599 SkGlyphCache* cache = autoCache.getCache();
reed@google.coma76de3d2011-01-13 18:30:42 +00001600
reed@android.com8a1c16f2008-12-17 15:59:43 +00001601 // transform our starting point
1602 {
1603 SkPoint loc;
bungeman@google.com94471032013-02-25 15:55:13 +00001604 fMatrix->mapXY(x, y, &loc);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001605 x = loc.fX;
1606 y = loc.fY;
1607 }
1608
1609 // need to measure first
1610 if (paint.getTextAlign() != SkPaint::kLeft_Align) {
1611 SkVector stop;
1612
1613 measure_text(cache, glyphCacheProc, text, byteLength, &stop);
1614
1615 SkScalar stopX = stop.fX;
1616 SkScalar stopY = stop.fY;
1617
1618 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
1619 stopX = SkScalarHalf(stopX);
1620 stopY = SkScalarHalf(stopY);
1621 }
1622 x -= stopX;
1623 y -= stopY;
1624 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001625
reed@android.com8a1c16f2008-12-17 15:59:43 +00001626 const char* stop = text + byteLength;
1627
reed@google.com045e62d2011-10-24 12:19:46 +00001628 SkAAClipBlitter aaBlitter;
1629 SkAutoBlitterChoose blitterChooser;
1630 SkBlitter* blitter = NULL;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001631 if (needsRasterTextBlit(*this)) {
bungeman@google.com94471032013-02-25 15:55:13 +00001632 blitterChooser.choose(*fBitmap, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +00001633 blitter = blitterChooser.get();
1634 if (fRC->isAA()) {
1635 aaBlitter.init(blitter, &fRC->aaRgn());
1636 blitter = &aaBlitter;
1637 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001638 }
1639
reed@android.com8a1c16f2008-12-17 15:59:43 +00001640 SkAutoKern autokern;
bungeman@google.com52c748b2011-08-22 21:30:43 +00001641 SkDraw1Glyph d1g;
reed@google.com5bdfb332013-05-02 18:55:44 +00001642 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001643
bungeman@google.com94471032013-02-25 15:55:13 +00001644 SkFixed fxMask = ~0;
1645 SkFixed fyMask = ~0;
1646 if (cache->isSubpixel()) {
1647 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix);
1648 if (kX_SkAxisAlignment == baseline) {
1649 fyMask = 0;
1650 d1g.fHalfSampleY = SK_FixedHalf;
1651 } else if (kY_SkAxisAlignment == baseline) {
1652 fxMask = 0;
1653 d1g.fHalfSampleX = SK_FixedHalf;
1654 }
1655 }
1656
1657 SkFixed fx = SkScalarToFixed(x) + d1g.fHalfSampleX;
1658 SkFixed fy = SkScalarToFixed(y) + d1g.fHalfSampleY;
1659
reed@android.com8a1c16f2008-12-17 15:59:43 +00001660 while (text < stop) {
bungeman@google.com2211b622012-01-13 15:02:58 +00001661 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001662
1663 fx += autokern.adjust(glyph);
1664
1665 if (glyph.fWidth) {
bungeman@google.com52c748b2011-08-22 21:30:43 +00001666 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001667 }
jvanverth@google.comd830d132013-11-11 20:54:09 +00001668
reed@android.com8a1c16f2008-12-17 15:59:43 +00001669 fx += glyph.fAdvanceX;
1670 fy += glyph.fAdvanceY;
1671 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001672}
1673
reed@android.com8a1c16f2008-12-17 15:59:43 +00001674//////////////////////////////////////////////////////////////////////////////
1675
reed@google.comed43dff2013-06-04 16:56:27 +00001676void SkDraw::drawPosText_asPaths(const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001677 const SkScalar pos[], int scalarsPerPosition,
1678 const SkPoint& offset, const SkPaint& origPaint) const {
reed@google.comed43dff2013-06-04 16:56:27 +00001679 // setup our std paint, in hopes of getting hits in the cache
1680 SkPaint paint(origPaint);
1681 SkScalar matrixScale = paint.setupForAsPaths();
1682
reed@google.com5a649022013-06-05 18:00:30 +00001683 SkMatrix matrix;
1684 matrix.setScale(matrixScale, matrixScale);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001685
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001686 // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache.
1687 paint.setStyle(SkPaint::kFill_Style);
1688 paint.setPathEffect(NULL);
1689
reed@google.comed43dff2013-06-04 16:56:27 +00001690 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
1691 SkAutoGlyphCache autoCache(paint, NULL, NULL);
1692 SkGlyphCache* cache = autoCache.getCache();
1693
1694 const char* stop = text + byteLength;
kkinnunencb9a2c82014-06-12 23:06:28 -07001695 SkTextAlignProcScalar alignProc(paint.getTextAlign());
fmalita05c4a432014-09-29 06:29:53 -07001696 SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001697
commit-bot@chromium.org4e82cdb2014-05-23 12:32:23 +00001698 // Now restore the original settings, so we "draw" with whatever style/stroking.
1699 paint.setStyle(origPaint.getStyle());
1700 paint.setPathEffect(origPaint.getPathEffect());
1701
reed@google.comed43dff2013-06-04 16:56:27 +00001702 while (text < stop) {
1703 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1704 if (glyph.fWidth) {
1705 const SkPath* path = cache->findPath(glyph);
1706 if (path) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001707 SkPoint tmsLoc;
1708 tmsProc(pos, &tmsLoc);
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001709 SkPoint loc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001710 alignProc(tmsLoc, glyph, &loc);
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +00001711
bungeman@google.comcfd90d62013-12-13 21:26:02 +00001712 matrix[SkMatrix::kMTransX] = loc.fX;
1713 matrix[SkMatrix::kMTransY] = loc.fY;
reed@google.com5a649022013-06-05 18:00:30 +00001714 if (fDevice) {
1715 fDevice->drawPath(*this, *path, paint, &matrix, false);
1716 } else {
1717 this->drawPath(*path, paint, &matrix, false);
1718 }
reed@google.comed43dff2013-06-04 16:56:27 +00001719 }
1720 }
1721 pos += scalarsPerPosition;
1722 }
1723}
1724
reed@android.com8a1c16f2008-12-17 15:59:43 +00001725void SkDraw::drawPosText(const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -07001726 const SkScalar pos[], int scalarsPerPosition,
1727 const SkPoint& offset, const SkPaint& paint) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001728 SkASSERT(byteLength == 0 || text != NULL);
1729 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition);
1730
reed@android.comf2b98d62010-12-20 18:26:13 +00001731 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001732
1733 // nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00001734 if (text == NULL || byteLength == 0 || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001735 return;
1736 }
1737
reed@google.comed43dff2013-06-04 16:56:27 +00001738 if (ShouldDrawTextAsPaths(paint, *fMatrix)) {
fmalita05c4a432014-09-29 06:29:53 -07001739 this->drawPosText_asPaths(text, byteLength, pos, scalarsPerPosition, offset, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001740 return;
1741 }
1742
1743 SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc();
reede010f1c2014-09-17 10:49:38 -07001744 SkAutoGlyphCache autoCache(paint, &fDevice->getLeakyProperties(), fMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001745 SkGlyphCache* cache = autoCache.getCache();
reed@google.coma76de3d2011-01-13 18:30:42 +00001746
reed@google.com045e62d2011-10-24 12:19:46 +00001747 SkAAClipBlitterWrapper wrapper;
1748 SkAutoBlitterChoose blitterChooser;
1749 SkBlitter* blitter = NULL;
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001750 if (needsRasterTextBlit(*this)) {
bungeman@google.com94471032013-02-25 15:55:13 +00001751 blitterChooser.choose(*fBitmap, *fMatrix, paint);
reed@google.com045e62d2011-10-24 12:19:46 +00001752 blitter = blitterChooser.get();
1753 if (fRC->isAA()) {
1754 wrapper.init(*fRC, blitter);
1755 blitter = wrapper.getBlitter();
1756 }
reed@google.com1d6ee0b2011-07-05 17:44:56 +00001757 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001758
reed@android.com8a1c16f2008-12-17 15:59:43 +00001759 const char* stop = text + byteLength;
kkinnunencb9a2c82014-06-12 23:06:28 -07001760 SkTextAlignProc alignProc(paint.getTextAlign());
bungeman@google.com2211b622012-01-13 15:02:58 +00001761 SkDraw1Glyph d1g;
reed@google.com5bdfb332013-05-02 18:55:44 +00001762 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint);
fmalita05c4a432014-09-29 06:29:53 -07001763 SkTextMapStateProc tmsProc(*fMatrix, offset, scalarsPerPosition);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001764
bungeman@google.com2211b622012-01-13 15:02:58 +00001765 if (cache->isSubpixel()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001766 // maybe we should skip the rounding if linearText is set
bungeman@google.com94471032013-02-25 15:55:13 +00001767 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix);
1768
1769 SkFixed fxMask = ~0;
1770 SkFixed fyMask = ~0;
1771 if (kX_SkAxisAlignment == baseline) {
1772 fyMask = 0;
bungeman@google.com94471032013-02-25 15:55:13 +00001773 d1g.fHalfSampleY = SK_FixedHalf;
bungeman@google.com94471032013-02-25 15:55:13 +00001774 } else if (kY_SkAxisAlignment == baseline) {
1775 fxMask = 0;
bungeman@google.com94471032013-02-25 15:55:13 +00001776 d1g.fHalfSampleX = SK_FixedHalf;
bungeman@google.com94471032013-02-25 15:55:13 +00001777 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001778
1779 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1780 while (text < stop) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001781 SkPoint tmsLoc;
1782 tmsProc(pos, &tmsLoc);
1783 SkFixed fx = SkScalarToFixed(tmsLoc.fX) + d1g.fHalfSampleX;
1784 SkFixed fy = SkScalarToFixed(tmsLoc.fY) + d1g.fHalfSampleY;
reed@google.coma76de3d2011-01-13 18:30:42 +00001785
reed@android.comf2b98d62010-12-20 18:26:13 +00001786 const SkGlyph& glyph = glyphCacheProc(cache, &text,
1787 fx & fxMask, fy & fyMask);
reed@google.coma76de3d2011-01-13 18:30:42 +00001788
reed@android.com8a1c16f2008-12-17 15:59:43 +00001789 if (glyph.fWidth) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001790 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001791 }
1792 pos += scalarsPerPosition;
1793 }
1794 } else {
1795 while (text < stop) {
bungeman@google.com9330cfe2012-01-04 14:17:00 +00001796 const char* currentText = text;
bungeman@google.com94471032013-02-25 15:55:13 +00001797 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0);
reed@google.coma76de3d2011-01-13 18:30:42 +00001798
bungeman@google.com94471032013-02-25 15:55:13 +00001799 if (metricGlyph.fWidth) {
1800 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;)
1801 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;)
kkinnunencb9a2c82014-06-12 23:06:28 -07001802 SkPoint tmsLoc;
1803 tmsProc(pos, &tmsLoc);
bungeman@google.com94471032013-02-25 15:55:13 +00001804 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001805 alignProc(tmsLoc, metricGlyph, &fixedLoc);
reed@google.coma76de3d2011-01-13 18:30:42 +00001806
bungeman@google.com94471032013-02-25 15:55:13 +00001807 SkFixed fx = fixedLoc.fX + d1g.fHalfSampleX;
1808 SkFixed fy = fixedLoc.fY + d1g.fHalfSampleY;
reed@google.coma76de3d2011-01-13 18:30:42 +00001809
reed@android.com8a1c16f2008-12-17 15:59:43 +00001810 // have to call again, now that we've been "aligned"
bungeman@google.com94471032013-02-25 15:55:13 +00001811 const SkGlyph& glyph = glyphCacheProc(cache, &currentText,
1812 fx & fxMask, fy & fyMask);
1813 // the assumption is that the metrics haven't changed
1814 SkASSERT(prevAdvX == glyph.fAdvanceX);
1815 SkASSERT(prevAdvY == glyph.fAdvanceY);
1816 SkASSERT(glyph.fWidth);
reed@google.coma76de3d2011-01-13 18:30:42 +00001817
bungeman@google.com94471032013-02-25 15:55:13 +00001818 proc(d1g, fx, fy, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001819 }
1820 pos += scalarsPerPosition;
1821 }
1822 }
1823 } else { // not subpixel
reed@google.comaeb07862012-04-18 18:32:04 +00001824 if (SkPaint::kLeft_Align == paint.getTextAlign()) {
1825 while (text < stop) {
1826 // the last 2 parameters are ignored
1827 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001828
reed@google.comaeb07862012-04-18 18:32:04 +00001829 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001830 SkPoint tmsLoc;
1831 tmsProc(pos, &tmsLoc);
reed@google.coma76de3d2011-01-13 18:30:42 +00001832
reed@google.comaeb07862012-04-18 18:32:04 +00001833 proc(d1g,
kkinnunencb9a2c82014-06-12 23:06:28 -07001834 SkScalarToFixed(tmsLoc.fX) + SK_FixedHalf, //d1g.fHalfSampleX,
1835 SkScalarToFixed(tmsLoc.fY) + SK_FixedHalf, //d1g.fHalfSampleY,
reed@google.comaeb07862012-04-18 18:32:04 +00001836 glyph);
1837 }
1838 pos += scalarsPerPosition;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001839 }
reed@google.comaeb07862012-04-18 18:32:04 +00001840 } else {
1841 while (text < stop) {
1842 // the last 2 parameters are ignored
1843 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
1844
1845 if (glyph.fWidth) {
kkinnunencb9a2c82014-06-12 23:06:28 -07001846 SkPoint tmsLoc;
1847 tmsProc(pos, &tmsLoc);
reed@google.comaeb07862012-04-18 18:32:04 +00001848
1849 SkIPoint fixedLoc;
kkinnunencb9a2c82014-06-12 23:06:28 -07001850 alignProc(tmsLoc, glyph, &fixedLoc);
reed@google.comaeb07862012-04-18 18:32:04 +00001851
1852 proc(d1g,
bungeman@google.com94471032013-02-25 15:55:13 +00001853 fixedLoc.fX + SK_FixedHalf, //d1g.fHalfSampleX,
1854 fixedLoc.fY + SK_FixedHalf, //d1g.fHalfSampleY,
reed@google.comaeb07862012-04-18 18:32:04 +00001855 glyph);
1856 }
1857 pos += scalarsPerPosition;
1858 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001859 }
1860 }
1861}
1862
1863#if defined _WIN32 && _MSC_VER >= 1300
1864#pragma warning ( pop )
1865#endif
1866
1867///////////////////////////////////////////////////////////////////////////////
1868
reed@google.com045e62d2011-10-24 12:19:46 +00001869typedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRasterClip&,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001870 SkBlitter*);
1871
1872static HairProc ChooseHairProc(bool doAntiAlias) {
1873 return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
1874}
1875
1876static bool texture_to_matrix(const VertState& state, const SkPoint verts[],
1877 const SkPoint texs[], SkMatrix* matrix) {
1878 SkPoint src[3], dst[3];
reed@google.coma76de3d2011-01-13 18:30:42 +00001879
reed@android.com8a1c16f2008-12-17 15:59:43 +00001880 src[0] = texs[state.f0];
1881 src[1] = texs[state.f1];
1882 src[2] = texs[state.f2];
1883 dst[0] = verts[state.f0];
1884 dst[1] = verts[state.f1];
1885 dst[2] = verts[state.f2];
1886 return matrix->setPolyToPoly(src, dst, 3);
1887}
1888
1889class SkTriColorShader : public SkShader {
1890public:
1891 SkTriColorShader() {}
1892
mtklein72c9faa2015-01-09 10:06:39 -08001893 size_t contextSize() const SK_OVERRIDE;
reed@google.coma76de3d2011-01-13 18:30:42 +00001894
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001895 class TriColorShaderContext : public SkShader::Context {
1896 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00001897 TriColorShaderContext(const SkTriColorShader& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001898 virtual ~TriColorShaderContext();
1899
1900 bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
1901
mtklein72c9faa2015-01-09 10:06:39 -08001902 void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001903
1904 private:
1905 SkMatrix fDstToUnit;
1906 SkPMColor fColors[3];
1907
1908 typedef SkShader::Context INHERITED;
1909 };
reed@google.coma76de3d2011-01-13 18:30:42 +00001910
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001911 SK_TO_STRING_OVERRIDE()
mtklein7e44bb12015-01-07 09:06:08 -08001912
1913 // For serialization. This will never be called.
1914 Factory getFactory() const SK_OVERRIDE { sk_throw(); return NULL; }
djsollen@google.comba28d032012-03-26 17:57:35 +00001915
reed@android.com8a1c16f2008-12-17 15:59:43 +00001916protected:
mtklein72c9faa2015-01-09 10:06:39 -08001917 Context* onCreateContext(const ContextRec& rec, void* storage) const SK_OVERRIDE {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +00001918 return SkNEW_PLACEMENT_ARGS(storage, TriColorShaderContext, (*this, rec));
1919 }
1920
reed@android.com8a1c16f2008-12-17 15:59:43 +00001921private:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001922 typedef SkShader INHERITED;
1923};
1924
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001925bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const SkColor colors[],
1926 int index0, int index1, int index2) {
reed@google.coma76de3d2011-01-13 18:30:42 +00001927
reed@android.com8a1c16f2008-12-17 15:59:43 +00001928 fColors[0] = SkPreMultiplyColor(colors[index0]);
1929 fColors[1] = SkPreMultiplyColor(colors[index1]);
1930 fColors[2] = SkPreMultiplyColor(colors[index2]);
reed@google.coma76de3d2011-01-13 18:30:42 +00001931
reed@android.com8a1c16f2008-12-17 15:59:43 +00001932 SkMatrix m, im;
1933 m.reset();
1934 m.set(0, pts[index1].fX - pts[index0].fX);
1935 m.set(1, pts[index2].fX - pts[index0].fX);
1936 m.set(2, pts[index0].fX);
1937 m.set(3, pts[index1].fY - pts[index0].fY);
1938 m.set(4, pts[index2].fY - pts[index0].fY);
1939 m.set(5, pts[index0].fY);
1940 if (!m.invert(&im)) {
1941 return false;
1942 }
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00001943 // We can't call getTotalInverse(), because we explicitly don't want to look at the localmatrix
1944 // as our interators are intrinsically tied to the vertices, and nothing else.
1945 SkMatrix ctmInv;
1946 if (!this->getCTM().invert(&ctmInv)) {
1947 return false;
1948 }
1949 fDstToUnit.setConcat(im, ctmInv);
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001950 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001951}
1952
1953#include "SkColorPriv.h"
reed@android.com689411a2008-12-18 02:52:32 +00001954#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001955
1956static int ScalarTo256(SkScalar v) {
1957 int scale = SkScalarToFixed(v) >> 8;
1958 if (scale < 0) {
1959 scale = 0;
1960 }
1961 if (scale > 255) {
1962 scale = 255;
1963 }
1964 return SkAlpha255To256(scale);
1965}
1966
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001967
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +00001968SkTriColorShader::TriColorShaderContext::TriColorShaderContext(const SkTriColorShader& shader,
1969 const ContextRec& rec)
1970 : INHERITED(shader, rec) {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001971
1972SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {}
1973
1974size_t SkTriColorShader::contextSize() const {
1975 return sizeof(TriColorShaderContext);
1976}
1977void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00001978 const int alphaScale = Sk255To256(this->getPaintAlpha());
1979
reed@android.com8a1c16f2008-12-17 15:59:43 +00001980 SkPoint src;
reed@google.coma76de3d2011-01-13 18:30:42 +00001981
reed@android.com8a1c16f2008-12-17 15:59:43 +00001982 for (int i = 0; i < count; i++) {
1983 fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src);
1984 x += 1;
reed@google.coma76de3d2011-01-13 18:30:42 +00001985
reed@android.com8a1c16f2008-12-17 15:59:43 +00001986 int scale1 = ScalarTo256(src.fX);
1987 int scale2 = ScalarTo256(src.fY);
1988 int scale0 = 256 - scale1 - scale2;
1989 if (scale0 < 0) {
1990 if (scale1 > scale2) {
1991 scale2 = 256 - scale1;
1992 } else {
1993 scale1 = 256 - scale2;
1994 }
1995 scale0 = 0;
1996 }
reed@google.coma76de3d2011-01-13 18:30:42 +00001997
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00001998 if (256 != alphaScale) {
1999 scale0 = SkAlphaMul(scale0, alphaScale);
2000 scale1 = SkAlphaMul(scale1, alphaScale);
2001 scale2 = SkAlphaMul(scale2, alphaScale);
2002 }
2003
reed@android.com8a1c16f2008-12-17 15:59:43 +00002004 dstC[i] = SkAlphaMulQ(fColors[0], scale0) +
commit-bot@chromium.org06a32062014-05-05 21:35:09 +00002005 SkAlphaMulQ(fColors[1], scale1) +
2006 SkAlphaMulQ(fColors[2], scale2);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002007 }
2008}
2009
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00002010#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +00002011void SkTriColorShader::toString(SkString* str) const {
2012 str->append("SkTriColorShader: (");
2013
2014 this->INHERITED::toString(str);
2015
2016 str->append(")");
2017}
2018#endif
2019
reed@android.com8a1c16f2008-12-17 15:59:43 +00002020void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
2021 const SkPoint vertices[], const SkPoint textures[],
2022 const SkColor colors[], SkXfermode* xmode,
2023 const uint16_t indices[], int indexCount,
2024 const SkPaint& paint) const {
bsalomon49f085d2014-09-05 13:34:00 -07002025 SkASSERT(0 == count || vertices);
reed@google.coma76de3d2011-01-13 18:30:42 +00002026
reed@android.com8a1c16f2008-12-17 15:59:43 +00002027 // abort early if there is nothing to draw
reed@google.com045e62d2011-10-24 12:19:46 +00002028 if (count < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002029 return;
2030 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002031
reed@android.com8a1c16f2008-12-17 15:59:43 +00002032 // transform out vertices into device coordinates
2033 SkAutoSTMalloc<16, SkPoint> storage(count);
2034 SkPoint* devVerts = storage.get();
2035 fMatrix->mapPoints(devVerts, vertices, count);
reed@google.coma76de3d2011-01-13 18:30:42 +00002036
reed@android.com8a1c16f2008-12-17 15:59:43 +00002037 /*
2038 We can draw the vertices in 1 of 4 ways:
2039
2040 - solid color (no shader/texture[], no colors[])
2041 - just colors (no shader/texture[], has colors[])
2042 - just texture (has shader/texture[], no colors[])
2043 - colors * texture (has shader/texture[], has colors[])
reed@google.coma76de3d2011-01-13 18:30:42 +00002044
reed@android.com8a1c16f2008-12-17 15:59:43 +00002045 Thus for texture drawing, we need both texture[] and a shader.
2046 */
2047
2048 SkTriColorShader triShader; // must be above declaration of p
2049 SkPaint p(paint);
2050
2051 SkShader* shader = p.getShader();
2052 if (NULL == shader) {
2053 // if we have no shader, we ignore the texture coordinates
2054 textures = NULL;
2055 } else if (NULL == textures) {
2056 // if we don't have texture coordinates, ignore the shader
2057 p.setShader(NULL);
2058 shader = NULL;
2059 }
2060
2061 // setup the custom shader (if needed)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002062 SkAutoTUnref<SkComposeShader> composeShader;
bsalomon49f085d2014-09-05 13:34:00 -07002063 if (colors) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002064 if (NULL == textures) {
2065 // just colors (no texture)
reed@google.coma641f3f2012-12-13 22:16:30 +00002066 shader = p.setShader(&triShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002067 } else {
2068 // colors * texture
2069 SkASSERT(shader);
2070 bool releaseMode = false;
2071 if (NULL == xmode) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00002072 xmode = SkXfermode::Create(SkXfermode::kModulate_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002073 releaseMode = true;
2074 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002075 composeShader.reset(SkNEW_ARGS(SkComposeShader, (&triShader, shader, xmode)));
2076 p.setShader(composeShader);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002077 if (releaseMode) {
2078 xmode->unref();
2079 }
2080 }
2081 }
2082
2083 SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002084 // Abort early if we failed to create a shader context.
reed@google.comea033602012-12-14 13:13:55 +00002085 if (blitter->isNullBlitter()) {
2086 return;
2087 }
2088
reed@android.com8a1c16f2008-12-17 15:59:43 +00002089 // setup our state and function pointer for iterating triangles
2090 VertState state(count, indices, indexCount);
2091 VertState::Proc vertProc = state.chooseProc(vmode);
reed@google.coma76de3d2011-01-13 18:30:42 +00002092
bsalomon49f085d2014-09-05 13:34:00 -07002093 if (textures || colors) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002094 while (vertProc(&state)) {
bsalomon49f085d2014-09-05 13:34:00 -07002095 if (textures) {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00002096 SkMatrix tempM;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002097 if (texture_to_matrix(state, vertices, textures, &tempM)) {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +00002098 SkShader::ContextRec rec(*fBitmap, p, *fMatrix);
2099 rec.fLocalMatrix = &tempM;
2100 if (!blitter->resetShaderContext(rec)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002101 continue;
2102 }
2103 }
2104 }
bsalomon49f085d2014-09-05 13:34:00 -07002105 if (colors) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00002106 // Find the context for triShader.
2107 SkTriColorShader::TriColorShaderContext* triColorShaderContext;
2108
2109 SkShader::Context* shaderContext = blitter->getShaderContext();
2110 SkASSERT(shaderContext);
2111 if (p.getShader() == &triShader) {
2112 triColorShaderContext =
2113 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContext);
2114 } else {
2115 // The shader is a compose shader and triShader is its first shader.
2116 SkASSERT(p.getShader() == composeShader);
2117 SkASSERT(composeShader->getShaderA() == &triShader);
2118 SkComposeShader::ComposeShaderContext* composeShaderContext =
2119 static_cast<SkComposeShader::ComposeShaderContext*>(shaderContext);
2120 SkShader::Context* shaderContextA = composeShaderContext->getShaderContextA();
2121 triColorShaderContext =
2122 static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContextA);
2123 }
2124
2125 if (!triColorShaderContext->setup(vertices, colors,
2126 state.f0, state.f1, state.f2)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002127 continue;
2128 }
2129 }
reed@google.com045e62d2011-10-24 12:19:46 +00002130
2131 SkPoint tmp[] = {
2132 devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
2133 };
2134 SkScan::FillTriangle(tmp, *fRC, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002135 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002136 } else {
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00002137 // no colors[] and no texture, stroke hairlines with paint's color.
reed@android.com8a1c16f2008-12-17 15:59:43 +00002138 HairProc hairProc = ChooseHairProc(paint.isAntiAlias());
reed@google.com045e62d2011-10-24 12:19:46 +00002139 const SkRasterClip& clip = *fRC;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002140 while (vertProc(&state)) {
reed@google.com045e62d2011-10-24 12:19:46 +00002141 hairProc(devVerts[state.f0], devVerts[state.f1], clip, blitter.get());
2142 hairProc(devVerts[state.f1], devVerts[state.f2], clip, blitter.get());
2143 hairProc(devVerts[state.f2], devVerts[state.f0], clip, blitter.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002144 }
2145 }
2146}
2147
reed@google.com0a0a2362011-03-23 13:51:55 +00002148///////////////////////////////////////////////////////////////////////////////
2149///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00002150
2151#ifdef SK_DEBUG
2152
reed@android.comf2b98d62010-12-20 18:26:13 +00002153void SkDraw::validate() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002154 SkASSERT(fBitmap != NULL);
2155 SkASSERT(fMatrix != NULL);
2156 SkASSERT(fClip != NULL);
reed@google.com045e62d2011-10-24 12:19:46 +00002157 SkASSERT(fRC != NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002158
reed@google.com045e62d2011-10-24 12:19:46 +00002159 const SkIRect& cr = fRC->getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002160 SkIRect br;
2161
reed@android.comf2b98d62010-12-20 18:26:13 +00002162 br.set(0, 0, fBitmap->width(), fBitmap->height());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002163 SkASSERT(cr.isEmpty() || br.contains(cr));
2164}
2165
2166#endif
2167
reed@android.com8a1c16f2008-12-17 15:59:43 +00002168////////////////////////////////////////////////////////////////////////////////////////////////
2169
2170#include "SkPath.h"
2171#include "SkDraw.h"
2172#include "SkRegion.h"
2173#include "SkBlitter.h"
2174
2175static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds,
reedb07a94f2014-11-19 05:03:18 -08002176 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
reed@android.com8a1c16f2008-12-17 15:59:43 +00002177 SkIRect* bounds) {
2178 if (devPath.isEmpty()) {
2179 return false;
2180 }
2181
reed@android.com8a1c16f2008-12-17 15:59:43 +00002182 // init our bounds from the path
2183 {
reed@android.comd252db02009-04-01 18:31:44 +00002184 SkRect pathBounds = devPath.getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002185 pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf);
2186 pathBounds.roundOut(bounds);
2187 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002188
tomhudson@google.com6db75fc2012-03-23 14:46:48 +00002189 SkIPoint margin = SkIPoint::Make(0, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002190 if (filter) {
2191 SkASSERT(filterMatrix);
reed@google.coma76de3d2011-01-13 18:30:42 +00002192
bungeman@google.com5af16f82011-09-02 15:06:44 +00002193 SkMask srcM, dstM;
reed@google.coma76de3d2011-01-13 18:30:42 +00002194
reed@android.com8a1c16f2008-12-17 15:59:43 +00002195 srcM.fBounds = *bounds;
2196 srcM.fFormat = SkMask::kA8_Format;
2197 srcM.fImage = NULL;
2198 if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
2199 return false;
2200 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002201 }
2202
bungeman@google.com5af16f82011-09-02 15:06:44 +00002203 // (possibly) trim the bounds to reflect the clip
reed@android.com8a1c16f2008-12-17 15:59:43 +00002204 // (plus whatever slop the filter needs)
bungeman@google.com5af16f82011-09-02 15:06:44 +00002205 if (clipBounds) {
2206 SkIRect tmp = *clipBounds;
reed@android.com35555912009-03-16 18:46:55 +00002207 // Ugh. Guard against gigantic margins from wacky filters. Without this
2208 // check we can request arbitrary amounts of slop beyond our visible
2209 // clip, and bring down the renderer (at least on finite RAM machines
2210 // like handsets, etc.). Need to balance this invented value between
2211 // quality of large filters like blurs, and the corresponding memory
2212 // requests.
2213 static const int MAX_MARGIN = 128;
2214 tmp.inset(-SkMin32(margin.fX, MAX_MARGIN),
2215 -SkMin32(margin.fY, MAX_MARGIN));
bungeman@google.com5af16f82011-09-02 15:06:44 +00002216 if (!bounds->intersect(tmp)) {
2217 return false;
2218 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002219 }
2220
2221 return true;
2222}
2223
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002224static void draw_into_mask(const SkMask& mask, const SkPath& devPath,
2225 SkPaint::Style style) {
reed@google.com045e62d2011-10-24 12:19:46 +00002226 SkBitmap bm;
2227 SkDraw draw;
2228 SkRasterClip clip;
2229 SkMatrix matrix;
2230 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002231
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00002232 bm.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
2233 mask.fImage, mask.fRowBytes);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002234
reed@google.com045e62d2011-10-24 12:19:46 +00002235 clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +00002236 matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft),
2237 -SkIntToScalar(mask.fBounds.fTop));
2238
2239 draw.fBitmap = &bm;
reed@google.com045e62d2011-10-24 12:19:46 +00002240 draw.fRC = &clip;
2241 draw.fClip = &clip.bwRgn();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002242 draw.fMatrix = &matrix;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002243 paint.setAntiAlias(true);
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002244 paint.setStyle(style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002245 draw.drawPath(devPath, paint);
2246}
2247
2248bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
reed@google.com30711b72012-12-18 19:18:39 +00002249 const SkMaskFilter* filter, const SkMatrix* filterMatrix,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002250 SkMask* mask, SkMask::CreateMode mode,
2251 SkPaint::Style style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002252 if (SkMask::kJustRenderImage_CreateMode != mode) {
2253 if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds))
2254 return false;
2255 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002256
reed@android.com8a1c16f2008-12-17 15:59:43 +00002257 if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
2258 mask->fFormat = SkMask::kA8_Format;
2259 mask->fRowBytes = mask->fBounds.width();
reed@android.com543ed932009-04-24 12:43:40 +00002260 size_t size = mask->computeImageSize();
2261 if (0 == size) {
2262 // we're too big to allocate the mask, abort
2263 return false;
2264 }
2265 mask->fImage = SkMask::AllocImage(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002266 memset(mask->fImage, 0, mask->computeImageSize());
2267 }
2268
2269 if (SkMask::kJustComputeBounds_CreateMode != mode) {
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00002270 draw_into_mask(*mask, devPath, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002271 }
reed@google.coma76de3d2011-01-13 18:30:42 +00002272
reed@android.com8a1c16f2008-12-17 15:59:43 +00002273 return true;
2274}